ILGeneratorDebugger.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #if REFLECTION_EMIT_COMPILER
  2. #if DEBUG
  3. using System.Reflection.Emit;
  4. using System.Reflection;
  5. using System;
  6. using System.Diagnostics;
  7. namespace FastReport.Code.Expressions
  8. {
  9. internal sealed class ILGeneratorDebugger
  10. {
  11. private readonly ILGenerator _ilGenerator;
  12. public ILGeneratorDebugger(ILGenerator iLGenerator)
  13. {
  14. _ilGenerator = iLGenerator;
  15. }
  16. private void ToConsole(OpCode opcode)
  17. {
  18. string msg = $"IL_{_ilGenerator.ILOffset.ToString("X")}: {opcode.ToString()}";
  19. //Console.WriteLine(msg);
  20. Debug.WriteLine(msg);
  21. }
  22. private void ToConsole(OpCode opcode, object arg)
  23. {
  24. string argString;
  25. if(arg.GetType() == typeof(string))
  26. {
  27. argString = $"\"{arg}\"";
  28. }
  29. else
  30. argString = arg.ToString();
  31. string msg = $"IL_{_ilGenerator.ILOffset.ToString("X")}: {opcode.ToString()} {argString}";
  32. //Console.WriteLine(msg);
  33. Debug.WriteLine(msg);
  34. }
  35. public void Emit(OpCode opcode)
  36. {
  37. ToConsole(opcode);
  38. _ilGenerator.Emit(opcode);
  39. }
  40. public void Emit(OpCode opcode, byte arg)
  41. {
  42. ToConsole(opcode, arg);
  43. _ilGenerator.Emit(opcode, arg);
  44. }
  45. public void Emit(OpCode opcode, sbyte arg)
  46. {
  47. ToConsole(opcode, arg);
  48. _ilGenerator.Emit(opcode, arg);
  49. }
  50. public void Emit(OpCode opcode, short arg)
  51. {
  52. ToConsole(opcode, arg);
  53. _ilGenerator.Emit(opcode, arg);
  54. }
  55. public void Emit(OpCode opcode, int arg)
  56. {
  57. ToConsole(opcode, arg);
  58. _ilGenerator.Emit(opcode, arg);
  59. }
  60. public void Emit(OpCode opcode, MethodInfo meth)
  61. {
  62. _ilGenerator.Emit(opcode, meth);
  63. }
  64. public void EmitCalli(OpCode opcode, CallingConventions callingConvention,
  65. Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
  66. {
  67. _ilGenerator.EmitCalli(opcode, callingConvention, returnType, parameterTypes, optionalParameterTypes);
  68. }
  69. //public void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes)
  70. //{
  71. // _ilGenerator.EmitCalli(opcode, unmanagedCallConv, returnType, parameterTypes);
  72. //}
  73. public void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes)
  74. {
  75. ToConsole(opcode, methodInfo);
  76. _ilGenerator.EmitCall(opcode, methodInfo, optionalParameterTypes);
  77. }
  78. public void Emit(OpCode opcode, SignatureHelper signature)
  79. {
  80. _ilGenerator.Emit(opcode, signature);
  81. }
  82. public void Emit(OpCode opcode, ConstructorInfo con)
  83. {
  84. _ilGenerator.Emit(opcode, con);
  85. }
  86. public void Emit(OpCode opcode, Type cls)
  87. {
  88. ToConsole(opcode, cls);
  89. _ilGenerator.Emit(opcode, cls);
  90. }
  91. public void Emit(OpCode opcode, long arg)
  92. {
  93. ToConsole(opcode, arg);
  94. _ilGenerator.Emit(opcode, arg);
  95. }
  96. public void Emit(OpCode opcode, float arg)
  97. {
  98. ToConsole(opcode, arg);
  99. _ilGenerator.Emit(opcode, arg);
  100. }
  101. public void Emit(OpCode opcode, double arg)
  102. {
  103. ToConsole(opcode, arg);
  104. _ilGenerator.Emit(opcode, arg);
  105. }
  106. public void Emit(OpCode opcode, Label label)
  107. {
  108. ToConsole(opcode, "label #" + label.GetHashCode());
  109. _ilGenerator.Emit(opcode, label);
  110. }
  111. public void Emit(OpCode opcode, Label[] labels)
  112. {
  113. _ilGenerator.Emit(opcode, labels);
  114. }
  115. public void Emit(OpCode opcode, FieldInfo field)
  116. {
  117. ToConsole(opcode, field);
  118. _ilGenerator.Emit(opcode, field);
  119. }
  120. public void Emit(OpCode opcode, string str)
  121. {
  122. ToConsole(opcode, str);
  123. _ilGenerator.Emit(opcode, str);
  124. }
  125. public void Emit(OpCode opcode, LocalBuilder local)
  126. {
  127. ToConsole(opcode, local);
  128. _ilGenerator.Emit(opcode, local);
  129. }
  130. #region Exceptions
  131. public Label BeginExceptionBlock()
  132. {
  133. return _ilGenerator.BeginExceptionBlock();
  134. }
  135. public void EndExceptionBlock()
  136. {
  137. _ilGenerator.EndExceptionBlock();
  138. }
  139. public void BeginExceptFilterBlock()
  140. {
  141. _ilGenerator.BeginExceptFilterBlock();
  142. }
  143. public void BeginCatchBlock(Type exceptionType)
  144. {
  145. _ilGenerator.BeginCatchBlock(exceptionType);
  146. }
  147. public void BeginFaultBlock()
  148. {
  149. _ilGenerator.BeginFaultBlock();
  150. }
  151. public void BeginFinallyBlock()
  152. {
  153. _ilGenerator.BeginFinallyBlock();
  154. }
  155. #endregion
  156. #region Labels
  157. public Label DefineLabel()
  158. {
  159. return _ilGenerator.DefineLabel();
  160. }
  161. public void MarkLabel(Label loc)
  162. {
  163. _ilGenerator.MarkLabel(loc);
  164. }
  165. #endregion
  166. #region IL Macros
  167. public void ThrowException(Type excType)
  168. {
  169. _ilGenerator.ThrowException(excType);
  170. }
  171. public void EmitWriteLine(string value)
  172. {
  173. _ilGenerator.EmitWriteLine(value);
  174. }
  175. public void EmitWriteLine(LocalBuilder localBuilder)
  176. {
  177. _ilGenerator.EmitWriteLine(localBuilder);
  178. }
  179. public void EmitWriteLine(FieldInfo fld)
  180. {
  181. _ilGenerator.EmitWriteLine(fld);
  182. }
  183. #endregion
  184. public LocalBuilder DeclareLocal(Type localType)
  185. {
  186. Console.WriteLine($".locals init: {localType}");
  187. return _ilGenerator.DeclareLocal(localType);
  188. }
  189. public LocalBuilder DeclareLocal(Type localType, bool pinned)
  190. {
  191. return _ilGenerator.DeclareLocal(localType, pinned);
  192. }
  193. public void UsingNamespace(string usingNamespace)
  194. {
  195. _ilGenerator.UsingNamespace(usingNamespace);
  196. }
  197. public void BeginScope()
  198. {
  199. _ilGenerator.BeginScope();
  200. }
  201. public void EndScope()
  202. {
  203. _ilGenerator.EndScope();
  204. }
  205. public int ILOffset => _ilGenerator.ILOffset;
  206. }
  207. }
  208. #endif
  209. #endif