Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ILGenerator.cs
Go to the documentation of this file.
3using System.IO;
6
8
9public class ILGenerator
10{
11 private int m_length;
12
13 private byte[] m_ILStream;
14
15 private int[] m_labelList;
16
17 private int m_labelCount;
18
20
21 private int m_fixupCount;
22
23 private int[] m_RelocFixupList;
24
25 private int m_RelocFixupCount;
26
27 private int m_exceptionCount;
28
30
32
34
36
38
39 internal int m_localCount;
40
42
43 private int m_maxStackSize;
44
45 private int m_maxMidStack;
46
47 private int m_maxMidStackCur;
48
50
52
53 public virtual int ILOffset => m_length;
54
55 internal static T[] EnlargeArray<T>(T[] incoming)
56 {
57 return EnlargeArray(incoming, incoming.Length * 2);
58 }
59
60 internal static T[] EnlargeArray<T>(T[] incoming, int requiredSize)
61 {
62 T[] array = new T[requiredSize];
63 Array.Copy(incoming, array, incoming.Length);
64 return array;
65 }
66
67 internal ILGenerator(MethodInfo methodBuilder)
68 : this(methodBuilder, 64)
69 {
70 }
71
72 internal ILGenerator(MethodInfo methodBuilder, int size)
73 {
74 m_ILStream = new byte[Math.Max(size, 16)];
75 m_ScopeTree = new ScopeTree();
76 m_methodBuilder = methodBuilder;
78 }
79
80 internal virtual void RecordTokenFixup()
81 {
82 if (m_RelocFixupList == null)
83 {
84 m_RelocFixupList = new int[8];
85 }
86 else if (m_RelocFixupList.Length <= m_RelocFixupCount)
87 {
88 m_RelocFixupList = EnlargeArray(m_RelocFixupList);
89 }
91 }
92
93 internal void InternalEmit(OpCode opcode)
94 {
95 short value = opcode.Value;
96 if (opcode.Size != 1)
97 {
99 m_length += 2;
100 }
101 else
102 {
103 m_ILStream[m_length++] = (byte)value;
104 }
105 UpdateStackSize(opcode, opcode.StackChange());
106 }
107
108 [MethodImpl(MethodImplOptions.AggressiveInlining)]
109 internal void UpdateStackSize(OpCode opcode, int stackchange)
110 {
111 m_maxMidStackCur += stackchange;
113 {
115 }
116 else if (m_maxMidStackCur < 0)
117 {
119 }
120 if (opcode.EndsUncondJmpBlk())
121 {
123 m_maxMidStack = 0;
125 }
126 }
127
128 private int GetMethodToken(MethodBase method, Type[] optionalParameterTypes, bool useMethodDef)
129 {
130 return ((ModuleBuilder)m_methodBuilder.Module).GetMethodTokenInternal(method, optionalParameterTypes, useMethodDef);
131 }
132
133 internal SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
134 {
135 return GetMemberRefSignature(call, returnType, parameterTypes, null, null, optionalParameterTypes);
136 }
137
138 internal virtual SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, Type[] optionalParameterTypes)
139 {
140 return GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, 0);
141 }
142
143 private SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, Type[] optionalParameterTypes, int cGenericParameters)
144 {
145 return ((ModuleBuilder)m_methodBuilder.Module).GetMemberRefSignature(call, returnType, parameterTypes, requiredCustomModifiers, optionalCustomModifiers, optionalParameterTypes, cGenericParameters);
146 }
147
148 internal byte[] BakeByteArray()
149 {
150 if (m_currExcStackCount != 0)
151 {
153 }
154 if (m_length == 0)
155 {
156 return null;
157 }
158 byte[] array = new byte[m_length];
160 for (int i = 0; i < m_fixupCount; i++)
161 {
162 __FixupData _FixupData = m_fixupData[i];
163 int num = GetLabelPos(_FixupData.m_fixupLabel) - (_FixupData.m_fixupPos + _FixupData.m_fixupInstSize);
164 if (_FixupData.m_fixupInstSize == 1)
165 {
166 if (num < -128 || num > 127)
167 {
169 }
170 array[_FixupData.m_fixupPos] = (byte)num;
171 }
172 else
173 {
175 }
176 }
177 return array;
178 }
179
181 {
182 if (m_currExcStackCount != 0)
183 {
185 }
186 if (m_exceptionCount == 0)
187 {
188 return null;
189 }
193 return array;
194 }
195
196 internal void EnsureCapacity(int size)
197 {
198 if (m_length + size >= m_ILStream.Length)
199 {
200 IncreaseCapacity(size);
201 }
202 }
203
204 private void IncreaseCapacity(int size)
205 {
206 byte[] array = new byte[Math.Max(m_ILStream.Length * 2, m_length + size)];
209 }
210
211 [MethodImpl(MethodImplOptions.AggressiveInlining)]
212 internal void PutInteger4(int value)
213 {
215 m_length += 4;
216 }
217
218 private int GetLabelPos(Label lbl)
219 {
220 int labelValue = lbl.GetLabelValue();
221 if (labelValue < 0 || labelValue >= m_labelCount || m_labelList == null)
222 {
224 }
225 if (m_labelList[labelValue] < 0)
226 {
228 }
229 return m_labelList[labelValue];
230 }
231
232 private void AddFixup(Label lbl, int pos, int instSize)
233 {
234 if (m_fixupData == null)
235 {
236 m_fixupData = new __FixupData[8];
237 }
238 else if (m_fixupData.Length <= m_fixupCount)
239 {
240 m_fixupData = EnlargeArray(m_fixupData);
241 }
243 {
244 m_fixupPos = pos,
245 m_fixupLabel = lbl,
246 m_fixupInstSize = instSize
247 };
248 }
249
250 internal int GetMaxStackSize()
251 {
252 return m_maxStackSize;
253 }
254
256 {
257 for (int i = 0; i < exceptions.Length; i++)
258 {
259 int num = i;
260 for (int j = i + 1; j < exceptions.Length; j++)
261 {
262 if (exceptions[num].IsInner(exceptions[j]))
263 {
264 num = j;
265 }
266 }
267 __ExceptionInfo _ExceptionInfo = exceptions[i];
268 exceptions[i] = exceptions[num];
269 exceptions[num] = _ExceptionInfo;
270 }
271 }
272
273 internal int[] GetTokenFixups()
274 {
275 if (m_RelocFixupCount == 0)
276 {
277 return null;
278 }
279 int[] array = new int[m_RelocFixupCount];
281 return array;
282 }
283
284 public virtual void Emit(OpCode opcode)
285 {
287 InternalEmit(opcode);
288 }
289
290 public virtual void Emit(OpCode opcode, byte arg)
291 {
293 InternalEmit(opcode);
294 m_ILStream[m_length++] = arg;
295 }
296
297 [CLSCompliant(false)]
298 public void Emit(OpCode opcode, sbyte arg)
299 {
301 InternalEmit(opcode);
302 m_ILStream[m_length++] = (byte)arg;
303 }
304
305 public virtual void Emit(OpCode opcode, short arg)
306 {
308 InternalEmit(opcode);
310 m_length += 2;
311 }
312
313 public virtual void Emit(OpCode opcode, int arg)
314 {
315 if (opcode.Equals(OpCodes.Ldc_I4))
316 {
317 OpCode opCode;
318 switch (arg)
319 {
320 case -1:
321 opCode = OpCodes.Ldc_I4_M1;
322 goto IL_009b;
323 case 0:
324 opCode = OpCodes.Ldc_I4_0;
325 goto IL_009b;
326 case 1:
327 opCode = OpCodes.Ldc_I4_1;
328 goto IL_009b;
329 case 2:
330 opCode = OpCodes.Ldc_I4_2;
331 goto IL_009b;
332 case 3:
333 opCode = OpCodes.Ldc_I4_3;
334 goto IL_009b;
335 case 4:
336 opCode = OpCodes.Ldc_I4_4;
337 goto IL_009b;
338 case 5:
339 opCode = OpCodes.Ldc_I4_5;
340 goto IL_009b;
341 case 6:
342 opCode = OpCodes.Ldc_I4_6;
343 goto IL_009b;
344 case 7:
345 opCode = OpCodes.Ldc_I4_7;
346 goto IL_009b;
347 case 8:
348 {
349 opCode = OpCodes.Ldc_I4_8;
350 goto IL_009b;
351 }
352 IL_009b:
353 opcode = opCode;
354 Emit(opcode);
355 return;
356 }
357 if (arg >= -128 && arg <= 127)
358 {
359 Emit(OpCodes.Ldc_I4_S, (sbyte)arg);
360 return;
361 }
362 }
363 else if (opcode.Equals(OpCodes.Ldarg))
364 {
365 if ((uint)arg <= 3u)
366 {
367 Emit(arg switch
368 {
369 0 => OpCodes.Ldarg_0,
370 1 => OpCodes.Ldarg_1,
371 2 => OpCodes.Ldarg_2,
372 _ => OpCodes.Ldarg_3,
373 });
374 return;
375 }
376 if ((uint)arg <= 255u)
377 {
378 Emit(OpCodes.Ldarg_S, (byte)arg);
379 return;
380 }
381 if ((uint)arg <= 65535u)
382 {
383 Emit(OpCodes.Ldarg, (short)arg);
384 return;
385 }
386 }
387 else if (opcode.Equals(OpCodes.Ldarga))
388 {
389 if ((uint)arg <= 255u)
390 {
391 Emit(OpCodes.Ldarga_S, (byte)arg);
392 return;
393 }
394 if ((uint)arg <= 65535u)
395 {
396 Emit(OpCodes.Ldarga, (short)arg);
397 return;
398 }
399 }
400 else if (opcode.Equals(OpCodes.Starg))
401 {
402 if ((uint)arg <= 255u)
403 {
404 Emit(OpCodes.Starg_S, (byte)arg);
405 return;
406 }
407 if ((uint)arg <= 65535u)
408 {
409 Emit(OpCodes.Starg, (short)arg);
410 return;
411 }
412 }
414 InternalEmit(opcode);
415 PutInteger4(arg);
416 }
417
418 public virtual void Emit(OpCode opcode, MethodInfo meth)
419 {
420 if (meth == null)
421 {
422 throw new ArgumentNullException("meth");
423 }
424 if (opcode.Equals(OpCodes.Call) || opcode.Equals(OpCodes.Callvirt) || opcode.Equals(OpCodes.Newobj))
425 {
426 EmitCall(opcode, meth, null);
427 return;
428 }
429 bool useMethodDef = opcode.Equals(OpCodes.Ldtoken) || opcode.Equals(OpCodes.Ldftn) || opcode.Equals(OpCodes.Ldvirtftn);
430 int methodToken = GetMethodToken(meth, null, useMethodDef);
432 InternalEmit(opcode);
433 UpdateStackSize(opcode, 0);
435 PutInteger4(methodToken);
436 }
437
438 public virtual void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type[]? optionalParameterTypes)
439 {
440 int num = 0;
441 if (optionalParameterTypes != null && (callingConvention & CallingConventions.VarArgs) == 0)
442 {
444 }
446 SignatureHelper memberRefSignature = GetMemberRefSignature(callingConvention, returnType, parameterTypes, optionalParameterTypes);
448 Emit(OpCodes.Calli);
449 if (returnType != typeof(void))
450 {
451 num++;
452 }
453 if (parameterTypes != null)
454 {
455 num -= parameterTypes.Length;
456 }
457 if (optionalParameterTypes != null)
458 {
459 num -= optionalParameterTypes.Length;
460 }
461 if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis)
462 {
463 num--;
464 }
465 num--;
468 PutInteger4(moduleBuilder.GetSignatureToken(memberRefSignature));
469 }
470
471 public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes)
472 {
473 int num = 0;
474 int num2 = 0;
476 if (parameterTypes != null)
477 {
478 num2 = parameterTypes.Length;
479 }
480 SignatureHelper methodSigHelper = SignatureHelper.GetMethodSigHelper(moduleBuilder, unmanagedCallConv, returnType);
481 if (parameterTypes != null)
482 {
483 for (int i = 0; i < num2; i++)
484 {
485 methodSigHelper.AddArgument(parameterTypes[i]);
486 }
487 }
488 if (returnType != typeof(void))
489 {
490 num++;
491 }
492 if (parameterTypes != null)
493 {
494 num -= num2;
495 }
496 num--;
499 Emit(OpCodes.Calli);
501 PutInteger4(moduleBuilder.GetSignatureToken(methodSigHelper));
502 }
503
504 public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes)
505 {
506 if (methodInfo == null)
507 {
508 throw new ArgumentNullException("methodInfo");
509 }
510 if (!opcode.Equals(OpCodes.Call) && !opcode.Equals(OpCodes.Callvirt) && !opcode.Equals(OpCodes.Newobj))
511 {
513 }
514 int num = 0;
515 int methodToken = GetMethodToken(methodInfo, optionalParameterTypes, useMethodDef: false);
517 InternalEmit(opcode);
518 if (methodInfo.ReturnType != typeof(void))
519 {
520 num++;
521 }
522 Type[] parameterTypes = methodInfo.GetParameterTypes();
523 if (parameterTypes != null)
524 {
525 num -= parameterTypes.Length;
526 }
527 if (!(methodInfo is SymbolMethod) && !methodInfo.IsStatic && !opcode.Equals(OpCodes.Newobj))
528 {
529 num--;
530 }
531 if (optionalParameterTypes != null)
532 {
533 num -= optionalParameterTypes.Length;
534 }
535 UpdateStackSize(opcode, num);
537 PutInteger4(methodToken);
538 }
539
540 public virtual void Emit(OpCode opcode, SignatureHelper signature)
541 {
542 if (signature == null)
543 {
544 throw new ArgumentNullException("signature");
545 }
546 int num = 0;
548 int signatureToken = moduleBuilder.GetSignatureToken(signature);
549 int value = signatureToken;
551 InternalEmit(opcode);
552 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
553 {
554 num -= signature.ArgumentCount;
555 num--;
556 UpdateStackSize(opcode, num);
557 }
560 }
561
562 public virtual void Emit(OpCode opcode, ConstructorInfo con)
563 {
564 if (con == null)
565 {
566 throw new ArgumentNullException("con");
567 }
568 int num = 0;
569 int methodToken = GetMethodToken(con, null, useMethodDef: true);
571 InternalEmit(opcode);
572 if (opcode.StackBehaviourPush == StackBehaviour.Varpush)
573 {
574 num++;
575 }
576 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
577 {
578 Type[] parameterTypes = con.GetParameterTypes();
579 if (parameterTypes != null)
580 {
581 num -= parameterTypes.Length;
582 }
583 }
584 UpdateStackSize(opcode, num);
586 PutInteger4(methodToken);
587 }
588
589 public virtual void Emit(OpCode opcode, Type cls)
590 {
592 int value = ((!(opcode == OpCodes.Ldtoken) || !(cls != null) || !cls.IsGenericTypeDefinition) ? moduleBuilder.GetTypeTokenInternal(cls) : moduleBuilder.GetTypeToken(cls));
594 InternalEmit(opcode);
597 }
598
599 public virtual void Emit(OpCode opcode, long arg)
600 {
601 EnsureCapacity(11);
602 InternalEmit(opcode);
604 m_length += 8;
605 }
606
607 public virtual void Emit(OpCode opcode, float arg)
608 {
610 InternalEmit(opcode);
612 m_length += 4;
613 }
614
615 public virtual void Emit(OpCode opcode, double arg)
616 {
617 EnsureCapacity(11);
618 InternalEmit(opcode);
620 m_length += 8;
621 }
622
623 public virtual void Emit(OpCode opcode, Label label)
624 {
626 InternalEmit(opcode);
628 {
629 AddFixup(label, m_length++, 1);
630 return;
631 }
632 AddFixup(label, m_length, 4);
633 m_length += 4;
634 }
635
636 public virtual void Emit(OpCode opcode, Label[] labels)
637 {
638 if (labels == null)
639 {
640 throw new ArgumentNullException("labels");
641 }
642 int num = labels.Length;
643 EnsureCapacity(num * 4 + 7);
644 InternalEmit(opcode);
645 PutInteger4(num);
646 int num2 = num * 4;
647 int num3 = 0;
648 while (num2 > 0)
649 {
650 AddFixup(labels[num3], m_length, num2);
651 m_length += 4;
652 num2 -= 4;
653 num3++;
654 }
655 }
656
657 public virtual void Emit(OpCode opcode, FieldInfo field)
658 {
660 int fieldToken = moduleBuilder.GetFieldToken(field);
662 InternalEmit(opcode);
664 PutInteger4(fieldToken);
665 }
666
667 public virtual void Emit(OpCode opcode, string str)
668 {
670 int stringConstant = moduleBuilder.GetStringConstant(str);
672 InternalEmit(opcode);
673 PutInteger4(stringConstant);
674 }
675
676 public virtual void Emit(OpCode opcode, LocalBuilder local)
677 {
678 if (local == null)
679 {
680 throw new ArgumentNullException("local");
681 }
682 int localIndex = local.GetLocalIndex();
683 if (local.GetMethodBuilder() != m_methodBuilder)
684 {
686 }
687 if (opcode.Equals(OpCodes.Ldloc))
688 {
689 switch (localIndex)
690 {
691 case 0:
692 opcode = OpCodes.Ldloc_0;
693 break;
694 case 1:
695 opcode = OpCodes.Ldloc_1;
696 break;
697 case 2:
698 opcode = OpCodes.Ldloc_2;
699 break;
700 case 3:
701 opcode = OpCodes.Ldloc_3;
702 break;
703 default:
704 if (localIndex <= 255)
705 {
706 opcode = OpCodes.Ldloc_S;
707 }
708 break;
709 }
710 }
711 else if (opcode.Equals(OpCodes.Stloc))
712 {
713 switch (localIndex)
714 {
715 case 0:
716 opcode = OpCodes.Stloc_0;
717 break;
718 case 1:
719 opcode = OpCodes.Stloc_1;
720 break;
721 case 2:
722 opcode = OpCodes.Stloc_2;
723 break;
724 case 3:
725 opcode = OpCodes.Stloc_3;
726 break;
727 default:
728 if (localIndex <= 255)
729 {
730 opcode = OpCodes.Stloc_S;
731 }
732 break;
733 }
734 }
735 else if (opcode.Equals(OpCodes.Ldloca) && localIndex <= 255)
736 {
737 opcode = OpCodes.Ldloca_S;
738 }
740 InternalEmit(opcode);
741 if (opcode.OperandType == OperandType.InlineNone)
742 {
743 return;
744 }
745 if (!OpCodes.TakesSingleByteArgument(opcode))
746 {
748 m_length += 2;
749 return;
750 }
751 if (localIndex > 255)
752 {
754 }
755 m_ILStream[m_length++] = (byte)localIndex;
756 }
757
758 public virtual Label BeginExceptionBlock()
759 {
760 if (m_exceptions == null)
761 {
763 }
764 if (m_currExcStack == null)
765 {
767 }
768 if (m_exceptionCount >= m_exceptions.Length)
769 {
770 m_exceptions = EnlargeArray(m_exceptions);
771 }
773 {
774 m_currExcStack = EnlargeArray(m_currExcStack);
775 }
776 Label label = DefineLabel();
777 __ExceptionInfo _ExceptionInfo = new __ExceptionInfo(m_length, label);
778 m_exceptions[m_exceptionCount++] = _ExceptionInfo;
779 m_currExcStack[m_currExcStackCount++] = _ExceptionInfo;
780 return label;
781 }
782
783 public virtual void EndExceptionBlock()
784 {
785 if (m_currExcStackCount == 0)
786 {
788 }
791 Label endLabel = _ExceptionInfo.GetEndLabel();
792 switch (_ExceptionInfo.GetCurrentState())
793 {
794 case 0:
795 case 1:
797 case 2:
798 Emit(OpCodes.Leave, endLabel);
799 break;
800 case 3:
801 case 4:
802 Emit(OpCodes.Endfinally);
803 break;
804 }
805 Label loc = ((m_labelList[endLabel.GetLabelValue()] != -1) ? _ExceptionInfo.m_finallyEndLabel : endLabel);
806 MarkLabel(loc);
807 _ExceptionInfo.Done(m_length);
808 }
809
810 public virtual void BeginExceptFilterBlock()
811 {
812 if (m_currExcStackCount == 0)
813 {
815 }
817 Emit(OpCodes.Leave, _ExceptionInfo.GetEndLabel());
818 _ExceptionInfo.MarkFilterAddr(m_length);
819 }
820
821 public virtual void BeginCatchBlock(Type exceptionType)
822 {
823 if (m_currExcStackCount == 0)
824 {
826 }
828 if (_ExceptionInfo.GetCurrentState() == 1)
829 {
830 if (exceptionType != null)
831 {
833 }
834 Emit(OpCodes.Endfilter);
835 }
836 else
837 {
838 if (exceptionType == null)
839 {
840 throw new ArgumentNullException("exceptionType");
841 }
842 Emit(OpCodes.Leave, _ExceptionInfo.GetEndLabel());
843 }
844 _ExceptionInfo.MarkCatchAddr(m_length, exceptionType);
845 }
846
847 public virtual void BeginFaultBlock()
848 {
849 if (m_currExcStackCount == 0)
850 {
852 }
854 Emit(OpCodes.Leave, _ExceptionInfo.GetEndLabel());
855 _ExceptionInfo.MarkFaultAddr(m_length);
856 }
857
858 public virtual void BeginFinallyBlock()
859 {
860 if (m_currExcStackCount == 0)
861 {
863 }
865 int currentState = _ExceptionInfo.GetCurrentState();
866 Label endLabel = _ExceptionInfo.GetEndLabel();
867 int num = 0;
868 if (currentState != 0)
869 {
870 Emit(OpCodes.Leave, endLabel);
871 num = m_length;
872 }
873 MarkLabel(endLabel);
874 Label label = DefineLabel();
875 _ExceptionInfo.SetFinallyEndLabel(label);
876 Emit(OpCodes.Leave, label);
877 if (num == 0)
878 {
879 num = m_length;
880 }
881 _ExceptionInfo.MarkFinallyAddr(m_length, num);
882 }
883
884 public virtual Label DefineLabel()
885 {
886 if (m_labelList == null)
887 {
888 m_labelList = new int[4];
889 }
890 if (m_labelCount >= m_labelList.Length)
891 {
892 m_labelList = EnlargeArray(m_labelList);
893 }
895 return new Label(m_labelCount++);
896 }
897
898 public virtual void MarkLabel(Label loc)
899 {
900 int labelValue = loc.GetLabelValue();
901 if (m_labelList == null || labelValue < 0 || labelValue >= m_labelList.Length)
902 {
904 }
905 if (m_labelList[labelValue] != -1)
906 {
908 }
909 m_labelList[labelValue] = m_length;
910 }
911
912 public virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type excType)
913 {
914 if (excType == null)
915 {
916 throw new ArgumentNullException("excType");
917 }
918 if (!excType.IsSubclassOf(typeof(Exception)) && excType != typeof(Exception))
919 {
921 }
922 ConstructorInfo constructor = excType.GetConstructor(Type.EmptyTypes);
923 if (constructor == null)
924 {
926 }
927 Emit(OpCodes.Newobj, constructor);
928 Emit(OpCodes.Throw);
929 }
930
931 public virtual void EmitWriteLine(string value)
932 {
933 Emit(OpCodes.Ldstr, value);
934 Type[] types = new Type[1] { typeof(string) };
935 Type type = Type.GetType("System.Console, System.Console", throwOnError: true);
936 MethodInfo method = type.GetMethod("WriteLine", types);
937 Emit(OpCodes.Call, method);
938 }
939
940 public virtual void EmitWriteLine(LocalBuilder localBuilder)
941 {
942 if (m_methodBuilder == null)
943 {
945 }
946 Type type = Type.GetType("System.Console, System.Console", throwOnError: true);
947 MethodInfo method = type.GetMethod("get_Out");
948 Emit(OpCodes.Call, method);
949 Emit(OpCodes.Ldloc, localBuilder);
950 Type[] array = new Type[1];
951 Type localType = localBuilder.LocalType;
952 if (localType is TypeBuilder || localType is EnumBuilder)
953 {
955 }
956 array[0] = localType;
957 MethodInfo method2 = typeof(TextWriter).GetMethod("WriteLine", array);
958 if (method2 == null)
959 {
960 throw new ArgumentException(SR.Argument_EmitWriteLineType, "localBuilder");
961 }
962 Emit(OpCodes.Callvirt, method2);
963 }
964
965 public virtual void EmitWriteLine(FieldInfo fld)
966 {
967 if (fld == null)
968 {
969 throw new ArgumentNullException("fld");
970 }
971 Type type = Type.GetType("System.Console, System.Console", throwOnError: true);
972 MethodInfo method = type.GetMethod("get_Out");
973 Emit(OpCodes.Call, method);
974 if ((fld.Attributes & FieldAttributes.Static) != 0)
975 {
976 Emit(OpCodes.Ldsfld, fld);
977 }
978 else
979 {
980 Emit(OpCodes.Ldarg, (short)0);
981 Emit(OpCodes.Ldfld, fld);
982 }
983 Type[] array = new Type[1];
984 Type fieldType = fld.FieldType;
985 if (fieldType is TypeBuilder || fieldType is EnumBuilder)
986 {
988 }
989 array[0] = fieldType;
990 MethodInfo method2 = typeof(TextWriter).GetMethod("WriteLine", array);
991 if (method2 == null)
992 {
994 }
995 Emit(OpCodes.Callvirt, method2);
996 }
997
998 public virtual LocalBuilder DeclareLocal(Type localType)
999 {
1000 return DeclareLocal(localType, pinned: false);
1001 }
1002
1003 public virtual LocalBuilder DeclareLocal(Type localType, bool pinned)
1004 {
1005 MethodBuilder methodBuilder = m_methodBuilder as MethodBuilder;
1006 if (methodBuilder == null)
1007 {
1008 throw new NotSupportedException();
1009 }
1010 if (methodBuilder.IsTypeCreated())
1011 {
1013 }
1014 if (localType == null)
1015 {
1016 throw new ArgumentNullException("localType");
1017 }
1018 if (methodBuilder.m_bIsBaked)
1019 {
1021 }
1022 m_localSignature.AddArgument(localType, pinned);
1023 return new LocalBuilder(m_localCount++, localType, methodBuilder, pinned);
1024 }
1025
1026 public virtual void UsingNamespace(string usingNamespace)
1027 {
1028 if (usingNamespace == null)
1029 {
1030 throw new ArgumentNullException("usingNamespace");
1031 }
1032 if (usingNamespace.Length == 0)
1033 {
1034 throw new ArgumentException(SR.Argument_EmptyName, "usingNamespace");
1035 }
1036 MethodBuilder methodBuilder = m_methodBuilder as MethodBuilder;
1037 if (methodBuilder == null)
1038 {
1039 throw new NotSupportedException();
1040 }
1041 int currentActiveScopeIndex = methodBuilder.GetILGenerator().m_ScopeTree.GetCurrentActiveScopeIndex();
1042 if (currentActiveScopeIndex == -1)
1043 {
1044 methodBuilder.m_localSymInfo.AddUsingNamespace(usingNamespace);
1045 }
1046 else
1047 {
1049 }
1050 }
1051
1052 public virtual void BeginScope()
1053 {
1055 }
1056
1057 public virtual void EndScope()
1058 {
1060 }
1061}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static unsafe int SingleToInt32Bits(float value)
static unsafe long DoubleToInt64Bits(double value)
static void WriteInt32LittleEndian(Span< byte > destination, int value)
static void WriteInt16BigEndian(Span< byte > destination, short value)
static void WriteInt16LittleEndian(Span< byte > destination, short value)
static void WriteInt64LittleEndian(Span< byte > destination, long value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static void SortExceptions(__ExceptionInfo[] exceptions)
virtual void Emit(OpCode opcode, float arg)
virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes)
void UpdateStackSize(OpCode opcode, int stackchange)
virtual void Emit(OpCode opcode, LocalBuilder local)
void AddFixup(Label lbl, int pos, int instSize)
virtual void Emit(OpCode opcode, Type cls)
virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type excType)
virtual void EmitWriteLine(FieldInfo fld)
virtual SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, Type[] optionalParameterTypes)
SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, Type[] optionalParameterTypes, int cGenericParameters)
virtual LocalBuilder DeclareLocal(Type localType)
virtual void MarkLabel(Label loc)
static T[] EnlargeArray< T >(T[] incoming)
ILGenerator(MethodInfo methodBuilder)
virtual void Emit(OpCode opcode)
virtual void Emit(OpCode opcode, MethodInfo meth)
virtual void Emit(OpCode opcode, SignatureHelper signature)
virtual void Emit(OpCode opcode, FieldInfo field)
virtual void Emit(OpCode opcode, int arg)
virtual LocalBuilder DeclareLocal(Type localType, bool pinned)
virtual void Emit(OpCode opcode, double arg)
virtual void EmitWriteLine(LocalBuilder localBuilder)
virtual void Emit(OpCode opcode, ConstructorInfo con)
virtual void Emit(OpCode opcode, Label label)
virtual void UsingNamespace(string usingNamespace)
virtual void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type[]? optionalParameterTypes)
virtual void EmitWriteLine(string value)
virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes)
__ExceptionInfo[] GetExceptions()
void InternalEmit(OpCode opcode)
void Emit(OpCode opcode, sbyte arg)
virtual void Emit(OpCode opcode, Label[] labels)
virtual void Emit(OpCode opcode, string str)
ILGenerator(MethodInfo methodBuilder, int size)
virtual void Emit(OpCode opcode, long arg)
SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
virtual void BeginCatchBlock(Type exceptionType)
virtual void Emit(OpCode opcode, byte arg)
virtual void Emit(OpCode opcode, short arg)
int GetMethodToken(MethodBase method, Type[] optionalParameterTypes, bool useMethodDef)
void AddUsingNamespace(string strNamespace)
static int GetStringConstant(QCallModule module, string str, int length)
int GetSignatureToken(SignatureHelper sigHelper)
static readonly OpCode Ldloca
Definition OpCodes.cs:427
static readonly OpCode Leave
Definition OpCodes.cs:379
static readonly OpCode Ldarg_2
Definition OpCodes.cs:13
static readonly OpCode Stloc_S
Definition OpCodes.cs:43
static readonly OpCode Stloc_0
Definition OpCodes.cs:25
static readonly OpCode Callvirt
Definition OpCodes.cs:225
static readonly OpCode Stloc
Definition OpCodes.cs:429
static readonly OpCode Newobj
Definition OpCodes.cs:233
static readonly OpCode Ldloc_2
Definition OpCodes.cs:21
static readonly OpCode Ldarg_S
Definition OpCodes.cs:33
static readonly OpCode Ldc_I4_6
Definition OpCodes.cs:61
static readonly OpCode Stloc_3
Definition OpCodes.cs:31
static readonly OpCode Ldftn
Definition OpCodes.cs:415
static readonly OpCode Ldc_I4
Definition OpCodes.cs:69
static readonly OpCode Endfinally
Definition OpCodes.cs:377
static bool TakesSingleByteArgument(OpCode inst)
Definition OpCodes.cs:457
static readonly OpCode Ldc_I4_M1
Definition OpCodes.cs:47
static readonly OpCode Ldarg_3
Definition OpCodes.cs:15
static readonly OpCode Stloc_1
Definition OpCodes.cs:27
static readonly OpCode Ldfld
Definition OpCodes.cs:245
static readonly OpCode Ldarga
Definition OpCodes.cs:421
static readonly OpCode Ldc_I4_5
Definition OpCodes.cs:59
static readonly OpCode Ldloc_0
Definition OpCodes.cs:17
static readonly OpCode Ldc_I4_1
Definition OpCodes.cs:51
static readonly OpCode Starg_S
Definition OpCodes.cs:37
static readonly OpCode Ldc_I4_0
Definition OpCodes.cs:49
static readonly OpCode Throw
Definition OpCodes.cs:243
static readonly OpCode Ldstr
Definition OpCodes.cs:231
static readonly OpCode Ldarg_1
Definition OpCodes.cs:11
static readonly OpCode Ldsfld
Definition OpCodes.cs:251
static readonly OpCode Endfilter
Definition OpCodes.cs:433
static readonly OpCode Ldloc_3
Definition OpCodes.cs:23
static readonly OpCode Stloc_2
Definition OpCodes.cs:29
static readonly OpCode Ldtoken
Definition OpCodes.cs:353
static readonly OpCode Ldloc_1
Definition OpCodes.cs:19
static readonly OpCode Ldc_I4_8
Definition OpCodes.cs:65
static readonly OpCode Call
Definition OpCodes.cs:83
static readonly OpCode Ldloc
Definition OpCodes.cs:425
static readonly OpCode Ldc_I4_7
Definition OpCodes.cs:63
static readonly OpCode Ldloc_S
Definition OpCodes.cs:39
static readonly OpCode Starg
Definition OpCodes.cs:423
static readonly OpCode Ldarg_0
Definition OpCodes.cs:9
static readonly OpCode Ldc_I4_S
Definition OpCodes.cs:67
static readonly OpCode Ldarga_S
Definition OpCodes.cs:35
static readonly OpCode Ldvirtftn
Definition OpCodes.cs:417
static readonly OpCode Calli
Definition OpCodes.cs:85
static readonly OpCode Ldc_I4_2
Definition OpCodes.cs:53
static readonly OpCode Ldloca_S
Definition OpCodes.cs:41
static readonly OpCode Ldarg
Definition OpCodes.cs:419
static readonly OpCode Ldc_I4_3
Definition OpCodes.cs:55
static readonly OpCode Ldc_I4_4
Definition OpCodes.cs:57
void AddUsingNamespaceToCurrentScope(string strNamespace)
Definition ScopeTree.cs:37
void AddScopeInfo(ScopeAction sa, int iOffset)
Definition ScopeTree.cs:48
static SignatureHelper GetLocalVarSigHelper()
static SignatureHelper GetMethodSigHelper(Module? mod, Type? returnType, Type[]? parameterTypes)
void MarkFinallyAddr(int finallyAddr, int endCatchAddr)
void MarkCatchAddr(int catchAddr, Type catchException)
FieldAttributes Attributes
Definition FieldInfo.cs:11
virtual Type[] GetParameterTypes()
static string Argument_UnmatchedMethodForLocal
Definition SR.cs:908
static string Argument_MissingDefaultConstructor
Definition SR.cs:770
static string Argument_ShouldNotSpecifyExceptionType
Definition SR.cs:870
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_BadLabelContent
Definition SR.cs:494
static string Argument_UnclosedExceptionBlock
Definition SR.cs:902
static string InvalidOperation_NotAVarArgCallingConvention
Definition SR.cs:1482
static string InvalidOperation_BadILGeneratorUsage
Definition SR.cs:1402
static string InvalidOperation_TypeHasBeenCreated
Definition SR.cs:1524
static string Argument_BadExceptionCodeGen
Definition SR.cs:480
static string Argument_NotMethodCallOpcode
Definition SR.cs:816
static string Argument_RedefinedLabel
Definition SR.cs:848
static string NotSupported_IllegalOneByteBranch
Definition SR.cs:1678
static string Argument_InvalidLabel
Definition SR.cs:702
static string Argument_NotExceptionType
Definition SR.cs:812
static string InvalidOperation_BadInstructionOrIndexOutOfBound
Definition SR.cs:1404
static string Argument_EmptyName
Definition SR.cs:584
static string Argument_BadLabel
Definition SR.cs:492
static string NotSupported_OutputStreamUsingTypeBuilder
Definition SR.cs:1702
static string Argument_EmitWriteLineType
Definition SR.cs:578
static string Argument_NotInExceptionBlock
Definition SR.cs:814
static string InvalidOperation_MethodBaked
Definition SR.cs:1462
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
ConstructorInfo? GetConstructor(Type[] types)
Definition Type.cs:542
virtual bool IsSubclassOf(Type c)
Definition Type.cs:1542
static readonly Type[] EmptyTypes
Definition Type.cs:19
virtual bool IsGenericTypeDefinition
Definition Type.cs:113
StackBehaviour StackBehaviourPop
Definition OpCode.cs:20
StackBehaviour StackBehaviourPush
Definition OpCode.cs:22
override bool Equals([NotNullWhen(true)] object? obj)
Definition OpCode.cs:78