EmitContext.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 (Environment.GetEnvironmentVariable ("LINQ_DBG") != null)
  50. return new DebugEmitContext (lambda);
  51. return new DynamicEmitContext (lambda);
  52. }
  53. public int GetParameterPosition (ParameterExpression p)
  54. {
  55. int position = owner.Parameters.IndexOf (p);
  56. if (position == -1)
  57. throw new InvalidOperationException ("Parameter not in scope");
  58. return position;
  59. }
  60. public abstract Delegate CreateDelegate ();
  61. public void Emit (Expression expression)
  62. {
  63. expression.Emit (this);
  64. }
  65. public LocalBuilder EmitStored (Expression expression)
  66. {
  67. var local = ig.DeclareLocal (expression.Type);
  68. expression.Emit (this);
  69. ig.Emit (OpCodes.Stloc, local);
  70. return local;
  71. }
  72. public void EmitLoad (Expression expression)
  73. {
  74. if (expression.Type.IsValueType) {
  75. var local = EmitStored (expression);
  76. ig.Emit (OpCodes.Ldloca, local);
  77. } else
  78. expression.Emit (this);
  79. }
  80. public void EmitLoad (LocalBuilder local)
  81. {
  82. ig.Emit (OpCodes.Ldloc, local);
  83. }
  84. public void EmitCall (LocalBuilder local, IEnumerable<Expression> arguments, MethodInfo method)
  85. {
  86. EmitLoad (local);
  87. EmitCollection (arguments);
  88. EmitCall (method);
  89. }
  90. public void EmitCall (Expression expression, IEnumerable<Expression> arguments, MethodInfo method)
  91. {
  92. if (expression != null)
  93. EmitLoad (expression);
  94. EmitCollection (arguments);
  95. EmitCall (method);
  96. }
  97. public void EmitCall (MethodInfo method)
  98. {
  99. ig.Emit (
  100. method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call,
  101. method);
  102. }
  103. public void EmitCollection<T> (IEnumerable<T> collection) where T : Expression
  104. {
  105. foreach (var expression in collection)
  106. expression.Emit (this);
  107. }
  108. public void EmitCollection (IEnumerable<ElementInit> initializers, LocalBuilder local)
  109. {
  110. foreach (var initializer in initializers)
  111. initializer.Emit (this, local);
  112. }
  113. public void EmitCollection (IEnumerable<MemberBinding> bindings, LocalBuilder local)
  114. {
  115. foreach (var binding in bindings)
  116. binding.Emit (this, local);
  117. }
  118. public void EmitIsInst (Expression expression, Type candidate)
  119. {
  120. expression.Emit (this);
  121. if (expression.Type.IsValueType)
  122. ig.Emit (OpCodes.Box, expression.Type);
  123. ig.Emit (OpCodes.Isinst, candidate);
  124. }
  125. }
  126. class DynamicEmitContext : EmitContext {
  127. DynamicMethod method;
  128. static object mlock = new object ();
  129. static int method_count;
  130. public DynamicMethod Method {
  131. get { return method; }
  132. }
  133. public DynamicEmitContext (LambdaExpression lambda)
  134. : base (lambda)
  135. {
  136. // FIXME: Need to force this to be verifiable, see:
  137. // https://bugzilla.novell.com/show_bug.cgi?id=355005
  138. method = new DynamicMethod (GenerateName (), return_type, param_types, typeof (EmitContext), true);
  139. ig = method.GetILGenerator ();
  140. owner.Emit (this);
  141. }
  142. public override Delegate CreateDelegate ()
  143. {
  144. return method.CreateDelegate (owner.Type);
  145. }
  146. static string GenerateName ()
  147. {
  148. lock (mlock) {
  149. return "lambda_method-" + (method_count++);
  150. }
  151. }
  152. }
  153. class DebugEmitContext : EmitContext {
  154. DynamicEmitContext dynamic_context;
  155. public DebugEmitContext (LambdaExpression lambda)
  156. : base (lambda)
  157. {
  158. dynamic_context = new DynamicEmitContext (lambda);
  159. var name = dynamic_context.Method.Name;
  160. var file_name = name + ".dll";
  161. var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
  162. new AssemblyName (name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
  163. var type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
  164. var method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, return_type, param_types);
  165. ig = method.GetILGenerator ();
  166. owner.Emit (this);
  167. type.CreateType ();
  168. assembly.Save (file_name);
  169. }
  170. public override Delegate CreateDelegate ()
  171. {
  172. return dynamic_context.CreateDelegate ();
  173. }
  174. }
  175. }