forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmitUtil.cs
211 lines (181 loc) · 5.72 KB
/
EmitUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using NHibernate.Linq;
using NHibernate.Util;
namespace NHibernate.Bytecode
{
public class EmitUtil
{
private EmitUtil()
{
}
/// <summary>
/// Emits an <c>ldc.i4</c> opcode using the fastest available opcode choice.
/// </summary>
public static void EmitFastInt(ILGenerator il, int value)
{
switch (value)
{
case -1:
il.Emit(OpCodes.Ldc_I4_M1);
return;
case 0:
il.Emit(OpCodes.Ldc_I4_0);
return;
case 1:
il.Emit(OpCodes.Ldc_I4_1);
return;
case 2:
il.Emit(OpCodes.Ldc_I4_2);
return;
case 3:
il.Emit(OpCodes.Ldc_I4_3);
return;
case 4:
il.Emit(OpCodes.Ldc_I4_4);
return;
case 5:
il.Emit(OpCodes.Ldc_I4_5);
return;
case 6:
il.Emit(OpCodes.Ldc_I4_6);
return;
case 7:
il.Emit(OpCodes.Ldc_I4_7);
return;
case 8:
il.Emit(OpCodes.Ldc_I4_8);
return;
}
if (value > -129 && value < 128)
{
il.Emit(OpCodes.Ldc_I4_S, (SByte) value);
}
else
{
il.Emit(OpCodes.Ldc_I4, value);
}
}
public static void EmitBoxIfNeeded(ILGenerator il, System.Type type)
{
if (type.IsValueType)
{
il.Emit(OpCodes.Box, type);
}
}
private static Dictionary<System.Type, OpCode> typeToOpcode;
static EmitUtil()
{
typeToOpcode = new Dictionary<System.Type, OpCode>(12);
typeToOpcode[typeof(bool)] = OpCodes.Ldind_I1;
typeToOpcode[typeof(sbyte)] = OpCodes.Ldind_I1;
typeToOpcode[typeof(byte)] = OpCodes.Ldind_U1;
typeToOpcode[typeof(char)] = OpCodes.Ldind_U2;
typeToOpcode[typeof(short)] = OpCodes.Ldind_I2;
typeToOpcode[typeof(ushort)] = OpCodes.Ldind_U2;
typeToOpcode[typeof(int)] = OpCodes.Ldind_I4;
typeToOpcode[typeof(uint)] = OpCodes.Ldind_U4;
typeToOpcode[typeof(long)] = OpCodes.Ldind_I8;
typeToOpcode[typeof(ulong)] = OpCodes.Ldind_I8;
typeToOpcode[typeof(float)] = OpCodes.Ldind_R4;
typeToOpcode[typeof(double)] = OpCodes.Ldind_R8;
}
/// <summary>
/// Emits IL to unbox a value type and if null, create a new instance of the value type.
/// </summary>
/// <remarks>
/// This does not work if the value type doesn't have a default constructor - we delegate
/// that to the ISetter.
/// </remarks>
public static void PreparePropertyForSet(ILGenerator il, System.Type propertyType)
{
// If this is a value type, we need to unbox it
if (propertyType.IsValueType)
{
// if (object[i] == null), create a new instance
Label notNullLabel = il.DefineLabel();
Label nullDoneLabel = il.DefineLabel();
LocalBuilder localNew = il.DeclareLocal(propertyType);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brtrue_S, notNullLabel);
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldloca, localNew);
il.Emit(OpCodes.Initobj, propertyType);
il.Emit(OpCodes.Ldloc, localNew);
il.Emit(OpCodes.Br_S, nullDoneLabel);
il.MarkLabel(notNullLabel);
il.Emit(OpCodes.Unbox, propertyType);
// Load the value indirectly, using ldobj or a specific opcode
OpCode specificOpCode;
if (typeToOpcode.TryGetValue(propertyType, out specificOpCode))
{
il.Emit(specificOpCode);
}
else
{
il.Emit(OpCodes.Ldobj, propertyType);
}
il.MarkLabel(nullDoneLabel);
}
else
{
if (propertyType != typeof(object))
{
il.Emit(OpCodes.Castclass, propertyType);
}
}
}
/// <summary>
/// Defines a new delegate type.
/// </summary>
public static System.Type DefineDelegateType(
string fullTypeName,
ModuleBuilder moduleBuilder,
System.Type returnType,
System.Type[] parameterTypes)
{
TypeBuilder delegateBuilder =
moduleBuilder.DefineType(
fullTypeName,
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.AnsiClass |
TypeAttributes.AutoClass, typeof(MulticastDelegate));
// Define a special constructor
ConstructorBuilder constructorBuilder =
delegateBuilder.DefineConstructor(
MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
CallingConventions.Standard, new System.Type[] {typeof(object), typeof(IntPtr)});
constructorBuilder.SetImplementationFlags(
MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
// Define the Invoke method for the delegate
MethodBuilder methodBuilder = delegateBuilder.DefineMethod("Invoke",
MethodAttributes.Public | MethodAttributes.HideBySig
| MethodAttributes.NewSlot | MethodAttributes.Virtual,
returnType, parameterTypes);
methodBuilder.SetImplementationFlags(
MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
return delegateBuilder.CreateType();
}
public static void EmitLoadType(ILGenerator il, System.Type type)
{
il.Emit(OpCodes.Ldtoken, type);
il.Emit(OpCodes.Call, ReflectionCache.TypeMethods.GetTypeFromHandle);
}
public static void EmitLoadMethodInfo(ILGenerator il, MethodInfo methodInfo)
{
il.Emit(OpCodes.Ldtoken, methodInfo);
il.Emit(OpCodes.Call, ReflectionCache.MethodBaseMethods.GetMethodFromHandle);
il.Emit(OpCodes.Castclass, typeof(MethodInfo));
}
private static readonly MethodInfo CreateDelegate = ReflectionHelper.GetMethod(
() => Delegate.CreateDelegate(null, null));
public static void EmitCreateDelegateInstance(ILGenerator il, System.Type delegateType, MethodInfo methodInfo)
{
EmitLoadType(il, delegateType);
EmitLoadMethodInfo(il, methodInfo);
il.EmitCall(OpCodes.Call, CreateDelegate, null);
il.Emit(OpCodes.Castclass, delegateType);
}
}
}