Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DynamicILGenerator.cs
Go to the documentation of this file.
2
4
5internal sealed class DynamicILGenerator : ILGenerator
6{
8
9 private int m_methodSigToken;
10
11 internal DynamicILGenerator(DynamicMethod method, byte[] methodSignature, int size)
12 : base(method, size)
13 {
14 m_scope = new DynamicScope();
15 m_methodSigToken = m_scope.GetTokenFor(methodSignature);
16 }
17
19 {
20 dm.m_methodHandle = ModuleHandle.GetDynamicMethod(dm, module, m_methodBuilder.Name, (byte[])m_scope[m_methodSigToken], new DynamicResolver(this));
21 }
22
23 public override LocalBuilder DeclareLocal(Type localType, bool pinned)
24 {
25 if (localType == null)
26 {
27 throw new ArgumentNullException("localType");
28 }
29 RuntimeType runtimeType = localType as RuntimeType;
30 if (runtimeType == null)
31 {
33 }
34 LocalBuilder result = new LocalBuilder(m_localCount, localType, m_methodBuilder);
35 m_localSignature.AddArgument(localType, pinned);
37 return result;
38 }
39
40 public override void Emit(OpCode opcode, MethodInfo meth)
41 {
42 if (meth == null)
43 {
44 throw new ArgumentNullException("meth");
45 }
46 int num = 0;
47 int num2 = 0;
48 DynamicMethod dynamicMethod = meth as DynamicMethod;
49 if (dynamicMethod == null)
50 {
51 RuntimeMethodInfo runtimeMethodInfo = meth as RuntimeMethodInfo;
52 if (runtimeMethodInfo == null)
53 {
55 }
56 RuntimeType runtimeType = runtimeMethodInfo.GetRuntimeType();
57 num2 = ((!(runtimeType != null) || (!runtimeType.IsGenericType && !runtimeType.IsArray)) ? GetTokenFor(runtimeMethodInfo) : GetTokenFor(runtimeMethodInfo, runtimeType));
58 }
59 else
60 {
61 if (opcode.Equals(OpCodes.Ldtoken) || opcode.Equals(OpCodes.Ldftn) || opcode.Equals(OpCodes.Ldvirtftn))
62 {
64 }
65 num2 = GetTokenFor(dynamicMethod);
66 }
68 InternalEmit(opcode);
69 if (opcode.StackBehaviourPush == StackBehaviour.Varpush && meth.ReturnType != typeof(void))
70 {
71 num++;
72 }
73 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
74 {
75 num -= meth.GetParametersNoCopy().Length;
76 }
77 if (!meth.IsStatic && !opcode.Equals(OpCodes.Newobj) && !opcode.Equals(OpCodes.Ldtoken) && !opcode.Equals(OpCodes.Ldftn))
78 {
79 num--;
80 }
81 UpdateStackSize(opcode, num);
82 PutInteger4(num2);
83 }
84
85 public override void Emit(OpCode opcode, ConstructorInfo con)
86 {
87 if (con == null)
88 {
89 throw new ArgumentNullException("con");
90 }
91 RuntimeConstructorInfo runtimeConstructorInfo = con as RuntimeConstructorInfo;
92 if (runtimeConstructorInfo == null)
93 {
95 }
96 RuntimeType runtimeType = runtimeConstructorInfo.GetRuntimeType();
97 int value = ((!(runtimeType != null) || (!runtimeType.IsGenericType && !runtimeType.IsArray)) ? GetTokenFor(runtimeConstructorInfo) : GetTokenFor(runtimeConstructorInfo, runtimeType));
99 InternalEmit(opcode);
100 UpdateStackSize(opcode, 1);
102 }
103
104 public override void Emit(OpCode opcode, Type type)
105 {
106 if (type == null)
107 {
108 throw new ArgumentNullException("type");
109 }
110 RuntimeType runtimeType = type as RuntimeType;
111 if (runtimeType == null)
112 {
114 }
115 int tokenFor = GetTokenFor(runtimeType);
117 InternalEmit(opcode);
118 PutInteger4(tokenFor);
119 }
120
121 public override void Emit(OpCode opcode, FieldInfo field)
122 {
123 if (field == null)
124 {
125 throw new ArgumentNullException("field");
126 }
127 RuntimeFieldInfo runtimeFieldInfo = field as RuntimeFieldInfo;
128 if (runtimeFieldInfo == null)
129 {
131 }
132 int value = ((!(field.DeclaringType == null)) ? GetTokenFor(runtimeFieldInfo, runtimeFieldInfo.GetRuntimeType()) : GetTokenFor(runtimeFieldInfo));
134 InternalEmit(opcode);
136 }
137
138 public override void Emit(OpCode opcode, string str)
139 {
140 if (str == null)
141 {
142 throw new ArgumentNullException("str");
143 }
144 int tokenForString = GetTokenForString(str);
146 InternalEmit(opcode);
147 PutInteger4(tokenForString);
148 }
149
150 public override void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
151 {
152 int num = 0;
153 if (optionalParameterTypes != null && (callingConvention & CallingConventions.VarArgs) == 0)
154 {
156 }
157 SignatureHelper memberRefSignature = GetMemberRefSignature(callingConvention, returnType, parameterTypes, optionalParameterTypes);
159 Emit(OpCodes.Calli);
160 if (returnType != typeof(void))
161 {
162 num++;
163 }
164 if (parameterTypes != null)
165 {
166 num -= parameterTypes.Length;
167 }
168 if (optionalParameterTypes != null)
169 {
170 num -= optionalParameterTypes.Length;
171 }
172 if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis)
173 {
174 num--;
175 }
176 num--;
178 int tokenForSig = GetTokenForSig(memberRefSignature.GetSignature(appendEndOfSig: true));
179 PutInteger4(tokenForSig);
180 }
181
182 public override void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
183 {
184 int num = 0;
185 int num2 = 0;
186 if (parameterTypes != null)
187 {
188 num2 = parameterTypes.Length;
189 }
190 SignatureHelper methodSigHelper = SignatureHelper.GetMethodSigHelper(unmanagedCallConv, returnType);
191 if (parameterTypes != null)
192 {
193 for (int i = 0; i < num2; i++)
194 {
195 methodSigHelper.AddArgument(parameterTypes[i]);
196 }
197 }
198 if (returnType != typeof(void))
199 {
200 num++;
201 }
202 if (parameterTypes != null)
203 {
204 num -= num2;
205 }
206 num--;
209 Emit(OpCodes.Calli);
210 int tokenForSig = GetTokenForSig(methodSigHelper.GetSignature(appendEndOfSig: true));
211 PutInteger4(tokenForSig);
212 }
213
214 public override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes)
215 {
216 if (methodInfo == null)
217 {
218 throw new ArgumentNullException("methodInfo");
219 }
220 if (!opcode.Equals(OpCodes.Call) && !opcode.Equals(OpCodes.Callvirt) && !opcode.Equals(OpCodes.Newobj))
221 {
223 }
224 if (methodInfo.ContainsGenericParameters)
225 {
226 throw new ArgumentException(SR.Argument_GenericsInvalid, "methodInfo");
227 }
228 if (methodInfo.DeclaringType != null && methodInfo.DeclaringType.ContainsGenericParameters)
229 {
230 throw new ArgumentException(SR.Argument_GenericsInvalid, "methodInfo");
231 }
232 int num = 0;
233 int memberRefToken = GetMemberRefToken(methodInfo, optionalParameterTypes);
235 InternalEmit(opcode);
236 if (methodInfo.ReturnType != typeof(void))
237 {
238 num++;
239 }
240 num -= methodInfo.GetParameterTypes().Length;
241 if (!(methodInfo is SymbolMethod) && !methodInfo.IsStatic && !opcode.Equals(OpCodes.Newobj))
242 {
243 num--;
244 }
245 if (optionalParameterTypes != null)
246 {
247 num -= optionalParameterTypes.Length;
248 }
249 UpdateStackSize(opcode, num);
250 PutInteger4(memberRefToken);
251 }
252
253 public override void Emit(OpCode opcode, SignatureHelper signature)
254 {
255 if (signature == null)
256 {
257 throw new ArgumentNullException("signature");
258 }
259 int num = 0;
261 InternalEmit(opcode);
262 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
263 {
264 num -= signature.ArgumentCount;
265 num--;
266 UpdateStackSize(opcode, num);
267 }
268 int tokenForSig = GetTokenForSig(signature.GetSignature(appendEndOfSig: true));
269 PutInteger4(tokenForSig);
270 }
271
272 public override void BeginExceptFilterBlock()
273 {
274 if (base.CurrExcStackCount == 0)
275 {
277 }
278 __ExceptionInfo _ExceptionInfo = base.CurrExcStack[base.CurrExcStackCount - 1];
279 Label endLabel = _ExceptionInfo.GetEndLabel();
280 Emit(OpCodes.Leave, endLabel);
282 _ExceptionInfo.MarkFilterAddr(ILOffset);
283 }
284
285 public override void BeginCatchBlock(Type exceptionType)
286 {
287 if (base.CurrExcStackCount == 0)
288 {
290 }
291 __ExceptionInfo _ExceptionInfo = base.CurrExcStack[base.CurrExcStackCount - 1];
292 RuntimeType runtimeType = exceptionType as RuntimeType;
293 if (_ExceptionInfo.GetCurrentState() == 1)
294 {
295 if (exceptionType != null)
296 {
298 }
299 Emit(OpCodes.Endfilter);
300 _ExceptionInfo.MarkCatchAddr(ILOffset, null);
301 return;
302 }
303 if (exceptionType == null)
304 {
305 throw new ArgumentNullException("exceptionType");
306 }
307 if (runtimeType == null)
308 {
310 }
311 Label endLabel = _ExceptionInfo.GetEndLabel();
312 Emit(OpCodes.Leave, endLabel);
314 _ExceptionInfo.MarkCatchAddr(ILOffset, exceptionType);
315 _ExceptionInfo.m_filterAddr[_ExceptionInfo.m_currentCatch - 1] = GetTokenFor(runtimeType);
316 }
317
318 public override void UsingNamespace(string ns)
319 {
321 }
322
323 public override void BeginScope()
324 {
326 }
327
328 public override void EndScope()
329 {
331 }
332
333 private int GetMemberRefToken(MethodBase methodInfo, Type[] optionalParameterTypes)
334 {
335 if (optionalParameterTypes != null && (methodInfo.CallingConvention & CallingConventions.VarArgs) == 0)
336 {
338 }
339 RuntimeMethodInfo runtimeMethodInfo = methodInfo as RuntimeMethodInfo;
340 DynamicMethod dynamicMethod = methodInfo as DynamicMethod;
341 if (runtimeMethodInfo == null && dynamicMethod == null)
342 {
344 }
345 ParameterInfo[] parametersNoCopy = methodInfo.GetParametersNoCopy();
346 Type[] array;
347 Type[][] array2;
348 Type[][] array3;
349 if (parametersNoCopy != null && parametersNoCopy.Length != 0)
350 {
351 array = new Type[parametersNoCopy.Length];
352 array2 = new Type[array.Length][];
353 array3 = new Type[array.Length][];
354 for (int i = 0; i < parametersNoCopy.Length; i++)
355 {
356 array[i] = parametersNoCopy[i].ParameterType;
357 array2[i] = parametersNoCopy[i].GetRequiredCustomModifiers();
358 array3[i] = parametersNoCopy[i].GetOptionalCustomModifiers();
359 }
360 }
361 else
362 {
363 array = null;
364 array2 = null;
365 array3 = null;
366 }
367 SignatureHelper memberRefSignature = GetMemberRefSignature(methodInfo.CallingConvention, MethodBuilder.GetMethodBaseReturnType(methodInfo), array, array2, array3, optionalParameterTypes);
368 if (runtimeMethodInfo != null)
369 {
370 return GetTokenForVarArgMethod(runtimeMethodInfo, memberRefSignature);
371 }
372 return GetTokenForVarArgMethod(dynamicMethod, memberRefSignature);
373 }
374
375 internal override SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, Type[] optionalParameterTypes)
376 {
377 SignatureHelper methodSigHelper = SignatureHelper.GetMethodSigHelper(null, call, returnType, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
378 if (optionalParameterTypes != null && optionalParameterTypes.Length != 0)
379 {
380 methodSigHelper.AddSentinel();
381 methodSigHelper.AddArguments(optionalParameterTypes, null, null);
382 }
383 return methodSigHelper;
384 }
385
386 internal override void RecordTokenFixup()
387 {
388 }
389
390 private int GetTokenFor(RuntimeType rtType)
391 {
392 return m_scope.GetTokenFor(rtType.TypeHandle);
393 }
394
395 private int GetTokenFor(RuntimeFieldInfo runtimeField)
396 {
397 return m_scope.GetTokenFor(runtimeField.FieldHandle);
398 }
399
400 private int GetTokenFor(RuntimeFieldInfo runtimeField, RuntimeType rtType)
401 {
402 return m_scope.GetTokenFor(runtimeField.FieldHandle, rtType.TypeHandle);
403 }
404
406 {
407 return m_scope.GetTokenFor(rtMeth.MethodHandle);
408 }
409
411 {
412 return m_scope.GetTokenFor(rtMeth.MethodHandle, rtType.TypeHandle);
413 }
414
415 private int GetTokenFor(RuntimeMethodInfo rtMeth)
416 {
417 return m_scope.GetTokenFor(rtMeth.MethodHandle);
418 }
419
420 private int GetTokenFor(RuntimeMethodInfo rtMeth, RuntimeType rtType)
421 {
422 return m_scope.GetTokenFor(rtMeth.MethodHandle, rtType.TypeHandle);
423 }
424
426 {
427 return m_scope.GetTokenFor(dm);
428 }
429
431 {
432 VarArgMethod varArgMethod = new VarArgMethod(rtMeth, sig);
433 return m_scope.GetTokenFor(varArgMethod);
434 }
435
437 {
438 VarArgMethod varArgMethod = new VarArgMethod(dm, sig);
439 return m_scope.GetTokenFor(varArgMethod);
440 }
441
442 private int GetTokenForString(string s)
443 {
444 return m_scope.GetTokenFor(s);
445 }
446
447 private int GetTokenForSig(byte[] sig)
448 {
449 return m_scope.GetTokenFor(sig);
450 }
451}
int GetTokenFor(RuntimeConstructorInfo rtMeth, RuntimeType rtType)
override void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
DynamicILGenerator(DynamicMethod method, byte[] methodSignature, int size)
int GetTokenFor(RuntimeMethodInfo rtMeth)
override void Emit(OpCode opcode, MethodInfo meth)
int GetTokenFor(RuntimeMethodInfo rtMeth, RuntimeType rtType)
override void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
override void Emit(OpCode opcode, Type type)
int GetTokenFor(RuntimeFieldInfo runtimeField)
override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes)
int GetTokenForVarArgMethod(DynamicMethod dm, SignatureHelper sig)
override LocalBuilder DeclareLocal(Type localType, bool pinned)
override void BeginCatchBlock(Type exceptionType)
void GetCallableMethod(RuntimeModule module, DynamicMethod dm)
int GetTokenForVarArgMethod(RuntimeMethodInfo rtMeth, SignatureHelper sig)
int GetTokenFor(RuntimeFieldInfo runtimeField, RuntimeType rtType)
override void Emit(OpCode opcode, ConstructorInfo con)
int GetMemberRefToken(MethodBase methodInfo, Type[] optionalParameterTypes)
override void Emit(OpCode opcode, FieldInfo field)
override SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, Type[] optionalParameterTypes)
override void Emit(OpCode opcode, string str)
override void Emit(OpCode opcode, SignatureHelper signature)
int GetTokenFor(RuntimeConstructorInfo rtMeth)
int GetTokenFor(VarArgMethod varArgMethod)
void UpdateStackSize(OpCode opcode, int stackchange)
void InternalEmit(OpCode opcode)
static Type GetMethodBaseReturnType(MethodBase method)
static readonly OpCode Leave
Definition OpCodes.cs:379
static readonly OpCode Callvirt
Definition OpCodes.cs:225
static readonly OpCode Newobj
Definition OpCodes.cs:233
static readonly OpCode Ldftn
Definition OpCodes.cs:415
static readonly OpCode Endfilter
Definition OpCodes.cs:433
static readonly OpCode Ldtoken
Definition OpCodes.cs:353
static readonly OpCode Call
Definition OpCodes.cs:83
static readonly OpCode Nop
Definition OpCodes.cs:5
static readonly OpCode Ldvirtftn
Definition OpCodes.cs:417
static readonly OpCode Calli
Definition OpCodes.cs:85
void AddArguments(Type[]? arguments, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers)
static SignatureHelper GetMethodSigHelper(Module? mod, Type? returnType, Type[]? parameterTypes)
void MarkCatchAddr(int catchAddr, Type catchException)
RuntimeFieldHandle FieldHandle
Definition FieldInfo.cs:45
virtual Type[] GetParameterTypes()
virtual CallingConventions CallingConvention
Definition MethodBase.cs:28
virtual ParameterInfo[] GetParametersNoCopy()
virtual bool ContainsGenericParameters
Definition MethodBase.cs:82
virtual Type[] GetRequiredCustomModifiers()
virtual Type[] GetOptionalCustomModifiers()
override RuntimeMethodHandle MethodHandle
override RuntimeTypeHandle TypeHandle
override bool IsGenericType
static string Argument_ShouldNotSpecifyExceptionType
Definition SR.cs:870
static string InvalidOperation_NotAVarArgCallingConvention
Definition SR.cs:1482
static string InvalidOperation_NotAllowedInDynamicMethod
Definition SR.cs:1480
static string Argument_InvalidOpCodeOnDynamicMethod
Definition SR.cs:724
static string Argument_NotMethodCallOpcode
Definition SR.cs:816
static string Argument_MustBeRuntimeMethodInfo
Definition SR.cs:778
static string Argument_MustBeRuntimeFieldInfo
Definition SR.cs:776
static string Argument_GenericsInvalid
Definition SR.cs:610
static string Argument_NotInExceptionBlock
Definition SR.cs:814
static string Argument_MustBeRuntimeType
Definition SR.cs:782
Definition SR.cs:7
bool IsArray
Definition Type.cs:71
virtual bool ContainsGenericParameters
Definition Type.cs:336
static IRuntimeMethodInfo GetDynamicMethod(DynamicMethod method, RuntimeModule module, string name, byte[] sig, Resolver resolver)
StackBehaviour StackBehaviourPop
Definition OpCode.cs:20
StackBehaviour StackBehaviourPush
Definition OpCode.cs:22
override bool Equals([NotNullWhen(true)] object? obj)
Definition OpCode.cs:78