EmitContext.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // EmitContext.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Jb Evain ([email protected])
  7. //
  8. // (C) 2008 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Reflection;
  34. using System.Reflection.Emit;
  35. namespace System.Linq.Expressions {
  36. abstract class EmitContext {
  37. protected LambdaExpression owner;
  38. protected Type [] param_types;
  39. protected Type return_type;
  40. public ILGenerator ig;
  41. protected EmitContext (LambdaExpression lambda)
  42. {
  43. this.owner = lambda;
  44. param_types = owner.Parameters.Select (p => p.Type).ToArray ();
  45. return_type = owner.GetReturnType ();
  46. }
  47. public static EmitContext Create (LambdaExpression lambda)
  48. {
  49. #if !NET_2_1
  50. if (Environment.GetEnvironmentVariable ("LINQ_DBG") != null)
  51. return new DebugEmitContext (lambda);
  52. #endif
  53. return new DynamicEmitContext (lambda);
  54. }
  55. public int GetParameterPosition (ParameterExpression p)
  56. {
  57. int position = owner.Parameters.IndexOf (p);
  58. if (position == -1)
  59. throw new InvalidOperationException ("Parameter not in scope");
  60. return position;
  61. }
  62. public abstract Delegate CreateDelegate ();
  63. public void Emit (Expression expression)
  64. {
  65. expression.Emit (this);
  66. }
  67. public LocalBuilder EmitStored (Expression expression)
  68. {
  69. var local = ig.DeclareLocal (expression.Type);
  70. expression.Emit (this);
  71. ig.Emit (OpCodes.Stloc, local);
  72. return local;
  73. }
  74. public void EmitLoad (Expression expression)
  75. {
  76. if (expression.Type.IsValueType) {
  77. var local = EmitStored (expression);
  78. ig.Emit (OpCodes.Ldloca, local);
  79. } else
  80. expression.Emit (this);
  81. }
  82. public void EmitLoad (LocalBuilder local)
  83. {
  84. ig.Emit (OpCodes.Ldloc, local);
  85. }
  86. public void EmitCall (LocalBuilder local, IEnumerable<Expression> arguments, MethodInfo method)
  87. {
  88. EmitLoad (local);
  89. EmitCollection (arguments);
  90. EmitCall (method);
  91. }
  92. public void EmitCall (Expression expression, IEnumerable<Expression> arguments, MethodInfo method)
  93. {
  94. if (expression != null)
  95. EmitLoad (expression);
  96. EmitCollection (arguments);
  97. EmitCall (method);
  98. }
  99. public void EmitCall (MethodInfo method)
  100. {
  101. ig.Emit (
  102. method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call,
  103. method);
  104. }
  105. public void EmitCollection<T> (IEnumerable<T> collection) where T : Expression
  106. {
  107. foreach (var expression in collection)
  108. expression.Emit (this);
  109. }
  110. public void EmitCollection (IEnumerable<ElementInit> initializers, LocalBuilder local)
  111. {
  112. foreach (var initializer in initializers)
  113. initializer.Emit (this, local);
  114. }
  115. public void EmitCollection (IEnumerable<MemberBinding> bindings, LocalBuilder local)
  116. {
  117. foreach (var binding in bindings)
  118. binding.Emit (this, local);
  119. }
  120. public void EmitIsInst (Expression expression, Type candidate)
  121. {
  122. expression.Emit (this);
  123. if (expression.Type.IsValueType)
  124. ig.Emit (OpCodes.Box, expression.Type);
  125. ig.Emit (OpCodes.Isinst, candidate);
  126. }
  127. }
  128. class DynamicEmitContext : EmitContext {
  129. DynamicMethod method;
  130. static object mlock = new object ();
  131. static int method_count;
  132. public DynamicMethod Method {
  133. get { return method; }
  134. }
  135. public DynamicEmitContext (LambdaExpression lambda)
  136. : base (lambda)
  137. {
  138. // FIXME: Need to force this to be verifiable, see:
  139. // https://bugzilla.novell.com/show_bug.cgi?id=355005
  140. method = new DynamicMethod (GenerateName (), return_type, param_types, typeof (EmitContext), true);
  141. ig = method.GetILGenerator ();
  142. owner.Emit (this);
  143. }
  144. public override Delegate CreateDelegate ()
  145. {
  146. return method.CreateDelegate (owner.Type);
  147. }
  148. static string GenerateName ()
  149. {
  150. lock (mlock) {
  151. return "lambda_method-" + (method_count++);
  152. }
  153. }
  154. }
  155. #if !NET_2_1
  156. class DebugEmitContext : EmitContext {
  157. DynamicEmitContext dynamic_context;
  158. public DebugEmitContext (LambdaExpression lambda)
  159. : base (lambda)
  160. {
  161. dynamic_context = new DynamicEmitContext (lambda);
  162. var name = dynamic_context.Method.Name;
  163. var file_name = name + ".dll";
  164. var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
  165. new AssemblyName (name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
  166. var type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
  167. var method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, return_type, param_types);
  168. ig = method.GetILGenerator ();
  169. owner.Emit (this);
  170. type.CreateType ();
  171. assembly.Save (file_name);
  172. }
  173. public override Delegate CreateDelegate ()
  174. {
  175. return dynamic_context.CreateDelegate ();
  176. }
  177. }
  178. #endif
  179. }