PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang/Runtime/DispatcherEmitter.cs

https://github.com/boo/boo-lang
C# | 171 lines | 126 code | 20 blank | 25 comment | 7 complexity | abc80996e0ba151d6b7789f784e0198e MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2003, 2004, 2005 Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Reflection;
  30. using System.Reflection.Emit;
  31. namespace Boo.Lang.Runtime
  32. {
  33. public abstract class DispatcherEmitter
  34. {
  35. private DynamicMethod _dynamicMethod;
  36. protected readonly ILGenerator _il;
  37. public DispatcherEmitter(Type owner, string dynamicMethodName)
  38. {
  39. _dynamicMethod = new DynamicMethod(owner.Name + "$" + dynamicMethodName, typeof(object), new Type[] { typeof(object), typeof(object[]) }, owner);
  40. _il = _dynamicMethod.GetILGenerator();
  41. }
  42. public Dispatcher Emit()
  43. {
  44. EmitMethodBody();
  45. return CreateMethodDispatcher();
  46. }
  47. protected abstract void EmitMethodBody();
  48. protected Dispatcher CreateMethodDispatcher()
  49. {
  50. return (Dispatcher)_dynamicMethod.CreateDelegate(typeof(Dispatcher));
  51. }
  52. protected bool IsStobj(OpCode code)
  53. {
  54. return OpCodes.Stobj.Value == code.Value;
  55. }
  56. protected void EmitCastOrUnbox(Type type)
  57. {
  58. if (type.IsValueType)
  59. {
  60. _il.Emit(OpCodes.Unbox, type);
  61. _il.Emit(OpCodes.Ldobj, type);
  62. }
  63. else
  64. {
  65. _il.Emit(OpCodes.Castclass, type);
  66. }
  67. }
  68. protected void BoxIfNeeded(Type returnType)
  69. {
  70. if (returnType.IsValueType)
  71. {
  72. _il.Emit(OpCodes.Box, returnType);
  73. }
  74. }
  75. protected void EmitLoadTargetObject(Type expectedType)
  76. {
  77. _il.Emit(OpCodes.Ldarg_0); // target object is the first argument
  78. if (expectedType.IsValueType)
  79. {
  80. _il.Emit(OpCodes.Unbox, expectedType);
  81. }
  82. else
  83. {
  84. _il.Emit(OpCodes.Castclass, expectedType);
  85. }
  86. }
  87. protected void EmitReturn(Type typeOnStack)
  88. {
  89. if (typeOnStack == typeof(void))
  90. {
  91. _il.Emit(OpCodes.Ldnull);
  92. }
  93. else
  94. {
  95. BoxIfNeeded(typeOnStack);
  96. }
  97. _il.Emit(OpCodes.Ret);
  98. }
  99. protected MethodInfo EmitPromotion(Type expectedType)
  100. {
  101. _il.Emit(OpCodes.Castclass, typeof(IConvertible));
  102. _il.Emit(OpCodes.Ldnull);
  103. MethodInfo method = GetPromotionMethod(expectedType);
  104. _il.Emit(OpCodes.Callvirt, method);
  105. return method;
  106. }
  107. protected void EmitArgArrayElement(int argumentIndex)
  108. {
  109. _il.Emit(OpCodes.Ldarg_1);
  110. _il.Emit(OpCodes.Ldc_I4, argumentIndex);
  111. _il.Emit(OpCodes.Ldelem_Ref);
  112. }
  113. private MethodInfo GetPromotionMethod(Type type)
  114. {
  115. return typeof(IConvertible).GetMethod("To" + Type.GetTypeCode(type));
  116. }
  117. protected void Dup()
  118. {
  119. _il.Emit(OpCodes.Dup);
  120. }
  121. protected void EmitCoercion(Type actualType, Type expectedType, int score)
  122. {
  123. switch (score)
  124. {
  125. case CandidateMethod.WideningPromotion:
  126. case CandidateMethod.NarrowingPromotion:
  127. EmitPromotion(expectedType);
  128. break;
  129. case CandidateMethod.ImplicitConversionScore:
  130. EmitCastOrUnbox(actualType);
  131. _il.Emit(OpCodes.Call, RuntimeServices.FindImplicitConversionOperator(actualType, expectedType));
  132. break;
  133. default:
  134. EmitCastOrUnbox(expectedType);
  135. break;
  136. }
  137. }
  138. protected void LoadLocal(LocalBuilder value)
  139. {
  140. _il.Emit(OpCodes.Ldloc, value);
  141. }
  142. protected void StoreLocal(LocalBuilder value)
  143. {
  144. _il.Emit(OpCodes.Stloc, value);
  145. }
  146. protected LocalBuilder DeclareLocal(Type type)
  147. {
  148. return _il.DeclareLocal(type);
  149. }
  150. }
  151. }