BinaryExpression.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // BinaryExpression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // Contains code from the Mono C# compiler:
  9. // Marek Safar ([email protected])
  10. // Martin Baulig ([email protected])
  11. // Raja Harinath ([email protected])
  12. //
  13. // (C) 2001-2003 Ximian, Inc.
  14. // (C) 2004-2008 Novell, Inc. (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System;
  36. using System.Reflection;
  37. using System.Reflection.Emit;
  38. namespace System.Linq.Expressions {
  39. public sealed class BinaryExpression : Expression {
  40. Expression left;
  41. Expression right;
  42. LambdaExpression conversion;
  43. MethodInfo method;
  44. bool lift_to_null;
  45. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right)
  46. : base (node_type, type)
  47. {
  48. this.left = left;
  49. this.right = right;
  50. }
  51. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, MethodInfo method)
  52. : base (node_type, type)
  53. {
  54. this.left = left;
  55. this.right = right;
  56. this.method = method;
  57. }
  58. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, bool lift_to_null,
  59. MethodInfo method, LambdaExpression conversion) : base (node_type, type)
  60. {
  61. this.left = left;
  62. this.right = right;
  63. this.method = method;
  64. this.lift_to_null = lift_to_null;
  65. this.conversion = conversion;
  66. }
  67. public Expression Left {
  68. get { return left; }
  69. }
  70. public Expression Right {
  71. get { return right; }
  72. }
  73. public MethodInfo Method {
  74. get { return method; }
  75. }
  76. public bool IsLifted {
  77. get { throw new NotImplementedException (); }
  78. }
  79. public bool IsLiftedToNull {
  80. get { return lift_to_null; }
  81. }
  82. static bool IsUnsigned (Type t)
  83. {
  84. if (t.IsPointer)
  85. return IsUnsigned (t.GetElementType ());
  86. return t == typeof (ushort) || t == typeof (uint) || t == typeof (ulong) || t == typeof (byte);
  87. }
  88. internal override void Emit (EmitContext ec)
  89. {
  90. ILGenerator ig = ec.ig;
  91. OpCode opcode;
  92. left.Emit (ec);
  93. right.Emit (ec);
  94. bool is_unsigned = IsUnsigned (left.Type);
  95. switch (NodeType){
  96. case ExpressionType.Add:
  97. opcode = OpCodes.Add;
  98. break;
  99. case ExpressionType.AddChecked:
  100. if (left.Type == typeof (int) || left.Type == typeof (long))
  101. opcode = OpCodes.Add_Ovf;
  102. else if (is_unsigned)
  103. opcode = OpCodes.Add_Ovf_Un;
  104. else
  105. opcode = OpCodes.Add;
  106. break;
  107. case ExpressionType.Subtract:
  108. opcode = OpCodes.Sub;
  109. break;
  110. case ExpressionType.SubtractChecked:
  111. if (left.Type == typeof (int) || left.Type == typeof (long))
  112. opcode = OpCodes.Sub_Ovf;
  113. else if (is_unsigned)
  114. opcode = OpCodes.Sub_Ovf_Un;
  115. else
  116. opcode = OpCodes.Sub;
  117. break;
  118. case ExpressionType.Multiply:
  119. opcode = OpCodes.Mul;
  120. break;
  121. case ExpressionType.MultiplyChecked:
  122. if (left.Type == typeof (int) || left.Type == typeof (long))
  123. opcode = OpCodes.Mul_Ovf;
  124. else if (is_unsigned)
  125. opcode = OpCodes.Mul_Ovf_Un;
  126. else
  127. opcode = OpCodes.Mul;
  128. break;
  129. case ExpressionType.Divide:
  130. if (is_unsigned)
  131. opcode = OpCodes.Div_Un;
  132. else
  133. opcode = OpCodes.Div;
  134. break;
  135. case ExpressionType.Modulo:
  136. if (is_unsigned)
  137. opcode = OpCodes.Rem_Un;
  138. else
  139. opcode = OpCodes.Rem;
  140. break;
  141. case ExpressionType.RightShift:
  142. if (is_unsigned)
  143. opcode = OpCodes.Shr_Un;
  144. else
  145. opcode = OpCodes.Shr;
  146. break;
  147. case ExpressionType.LeftShift:
  148. opcode = OpCodes.Shl;
  149. break;
  150. case ExpressionType.And:
  151. opcode = OpCodes.And;
  152. break;
  153. case ExpressionType.Or:
  154. opcode = OpCodes.Or;
  155. break;
  156. case ExpressionType.ExclusiveOr:
  157. opcode = OpCodes.Xor;
  158. break;
  159. case ExpressionType.AndAlso:
  160. case ExpressionType.Coalesce:
  161. case ExpressionType.Equal:
  162. case ExpressionType.GreaterThan:
  163. case ExpressionType.GreaterThanOrEqual:
  164. case ExpressionType.LessThan:
  165. case ExpressionType.LessThanOrEqual:
  166. case ExpressionType.NotEqual:
  167. case ExpressionType.OrElse:
  168. case ExpressionType.Power:
  169. throw new NotImplementedException (String.Format ("No support for {0} node yet", NodeType));
  170. default:
  171. throw new Exception (String.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  172. }
  173. ig.Emit (opcode);
  174. }
  175. [MonoTODO]
  176. public LambdaExpression Conversion {
  177. get {
  178. if (this.NodeType != ExpressionType.Coalesce)
  179. return null;
  180. throw new System.NotImplementedException ();
  181. }
  182. }
  183. }
  184. }