3
using System.Reflection;
4
using Cecil.FlowAnalysis.Utilities;
5
using Db4objects.Db4o.Instrumentation.Api;
9
namespace Db4objects.Db4o.Instrumentation.Cecil
11
internal class CecilMethodBuilder : IMethodBuilder
13
private readonly MethodDefinition _method;
14
private readonly ILProcessor _il;
16
public CecilMethodBuilder(MethodDefinition method)
19
_il = method.Body.GetILProcessor();
22
public IReferenceProvider References
24
get { return CecilReferenceProvider.ForModule(_method.DeclaringType.Module.Assembly.MainModule); }
27
public void Ldc(object value)
29
Type type = value.GetType();
30
TypeCode code = Type.GetTypeCode(type);
34
_il.Emit(OpCodes.Ldc_I4_S, (SByte)value);
37
_il.Emit(OpCodes.Ldc_I4, (Int32)value);
40
_il.Emit(OpCodes.Ldc_I8, (Int64)value);
43
_il.Emit(OpCodes.Ldstr, (String)value);
46
throw new NotImplementedException(code.ToString());
50
public void LoadArgument(int index)
55
_il.Emit(OpCodes.Ldarg_0);
58
_il.Emit(OpCodes.Ldarg_1);
61
// TODO: This is wrong. Emit expects an VariableDefinition for a Ldarg .
62
// But actually no code passes idexes other than 0 and 1
63
_il.Emit(OpCodes.Ldarg, index);
70
_il.Emit(OpCodes.Pop);
73
public void LoadArrayElement(ITypeRef elementType)
75
throw new NotImplementedException();
78
public void Add(ITypeRef operandType)
80
throw new NotImplementedException();
83
public void Subtract(ITypeRef operandType)
85
throw new NotImplementedException();
88
public void Multiply(ITypeRef operandType)
90
throw new NotImplementedException();
93
public void Divide(ITypeRef operandType)
95
throw new NotImplementedException();
98
public void Invoke(IMethodRef method, CallingConvention convention)
100
_il.Emit(OpCodeForConvention(convention), CecilMethodRef.GetReference(method));
103
private static OpCode OpCodeForConvention(CallingConvention convention)
105
return convention == CallingConvention.Static
110
public void Invoke(MethodInfo method)
112
throw new NotImplementedException();
115
public void LoadField(IFieldRef fieldRef)
117
_il.Emit(OpCodes.Ldfld, GetReference(fieldRef));
120
private static FieldReference GetReference(IFieldRef fieldRef)
122
return CecilFieldRef.GetReference(fieldRef);
125
public void LoadStaticField(IFieldRef fieldRef)
127
_il.Emit(OpCodes.Ldsfld, GetReference(fieldRef));
130
public void Box(ITypeRef boxedType)
132
TypeReference type = CecilTypeRef.GetReference(boxedType);
133
if (!type.IsValueType) return;
134
_il.Emit(OpCodes.Box, type);
137
public void EndMethod()
139
_il.Emit(OpCodes.Ret);
142
public void Print(TextWriter @out)
144
Formatter.WriteMethodBody(@out, _method);
b'\\ No newline at end of file'