BinaryExpression.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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, is_lifted;
  45. public Expression Left {
  46. get { return left; }
  47. }
  48. public Expression Right {
  49. get { return right; }
  50. }
  51. public MethodInfo Method {
  52. get { return method; }
  53. }
  54. public bool IsLifted {
  55. get { return is_lifted; }
  56. }
  57. public bool IsLiftedToNull {
  58. get { return lift_to_null; }
  59. }
  60. public LambdaExpression Conversion {
  61. get { return conversion; }
  62. }
  63. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right)
  64. : base (node_type, type)
  65. {
  66. this.left = left;
  67. this.right = right;
  68. }
  69. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, MethodInfo method)
  70. : base (node_type, type)
  71. {
  72. this.left = left;
  73. this.right = right;
  74. this.method = method;
  75. }
  76. internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, bool lift_to_null,
  77. bool is_lifted, MethodInfo method, LambdaExpression conversion) : base (node_type, type)
  78. {
  79. this.left = left;
  80. this.right = right;
  81. this.method = method;
  82. this.conversion = conversion;
  83. this.lift_to_null = lift_to_null;
  84. this.is_lifted = is_lifted;
  85. }
  86. void EmitMethod (EmitContext ec)
  87. {
  88. left.Emit (ec);
  89. right.Emit (ec);
  90. ec.ig.Emit (OpCodes.Call, Method);
  91. }
  92. static MethodInfo GetMethodNoPar (Type t, string name)
  93. {
  94. var method = t.GetMethod (name, Type.EmptyTypes);
  95. if (method == null)
  96. throw new ArgumentException (
  97. string.Format ("Internal error: method {0} with no parameters not found on {1}", name, t));
  98. return method;
  99. }
  100. void EmitLiftedLogical (EmitContext ec, bool and, bool short_circuit)
  101. {
  102. var ig = ec.ig;
  103. LocalBuilder ret = ig.DeclareLocal (Type);
  104. LocalBuilder vleft = null, vright = null;
  105. MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
  106. MethodInfo get_value = GetMethodNoPar (left.Type, "get_Value");
  107. vleft = EmitStored (ec, left);
  108. if (!short_circuit)
  109. vright = EmitStored (ec, right);
  110. Label left_is_null = ig.DefineLabel ();
  111. Label left_is_false = ig.DefineLabel ();
  112. Label right_is_null = ig.DefineLabel ();
  113. Label create = ig.DefineLabel ();
  114. Label exit = ig.DefineLabel ();
  115. Label both_are_null = ig.DefineLabel ();
  116. // Check left
  117. ig.Emit (OpCodes.Ldloca, vleft);
  118. ig.Emit (OpCodes.Call, has_value);
  119. ig.Emit (OpCodes.Brfalse, left_is_null);
  120. ig.Emit (OpCodes.Ldloca, vleft);
  121. ig.Emit (OpCodes.Call, get_value);
  122. ig.Emit (OpCodes.Dup);
  123. ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, create);
  124. // Deal with right
  125. if (short_circuit)
  126. vright = EmitStored (ec, right);
  127. ig.Emit (OpCodes.Ldloca, vright);
  128. ig.Emit (OpCodes.Call, has_value);
  129. ig.Emit (OpCodes.Brfalse, right_is_null);
  130. ig.Emit (OpCodes.Ldloca, vright);
  131. ig.Emit (OpCodes.Call, get_value);
  132. ig.Emit (and ? OpCodes.And : OpCodes.Or);
  133. ig.Emit (OpCodes.Br, create);
  134. // left_is_null:
  135. ig.MarkLabel (left_is_null);
  136. ig.Emit (OpCodes.Ldloca, vright);
  137. ig.Emit (OpCodes.Call, has_value);
  138. ig.Emit (OpCodes.Brfalse, both_are_null);
  139. ig.Emit (OpCodes.Ldloca, vright);
  140. ig.Emit (OpCodes.Call, get_value);
  141. ig.Emit (OpCodes.Dup);
  142. ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, create);
  143. // right_is_null:
  144. ig.MarkLabel (right_is_null);
  145. ig.Emit (OpCodes.Pop);
  146. // both_are_null:
  147. ig.MarkLabel (both_are_null);
  148. ig.Emit (OpCodes.Ldloca, ret);
  149. ig.Emit (OpCodes.Initobj, Type);
  150. ig.Emit (OpCodes.Ldloc, ret);
  151. ig.Emit (OpCodes.Br, exit);
  152. // create:
  153. ig.MarkLabel (create);
  154. ig.Emit (OpCodes.Newobj, Type.GetConstructors () [0]);
  155. // exit:
  156. ig.MarkLabel (exit);
  157. }
  158. void EmitLogical (EmitContext ec, bool and, bool short_circuit)
  159. {
  160. ILGenerator ig = ec.ig;
  161. if (IsLifted) {
  162. EmitLiftedLogical (ec, and, short_circuit);
  163. return;
  164. }
  165. left.Emit (ec);
  166. right.Emit (ec);
  167. ig.Emit (and ? OpCodes.And : OpCodes.Or);
  168. }
  169. void EmitCoalesce (EmitContext ec)
  170. {
  171. ILGenerator ig = ec.ig;
  172. LocalBuilder vleft;
  173. LocalBuilder vright;
  174. MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
  175. Label exit = ig.DefineLabel ();
  176. Label try_right = ig.DefineLabel ();
  177. Label setup_null = ig.DefineLabel ();
  178. vleft = EmitStored (ec, left);
  179. if (IsNullable (left.Type)){
  180. ig.Emit (OpCodes.Ldloca, vleft);
  181. ig.Emit (OpCodes.Call, has_value);
  182. } else
  183. ig.Emit (OpCodes.Ldloc, vleft);
  184. ig.Emit (OpCodes.Brfalse, try_right);
  185. ig.Emit (OpCodes.Ldloc, vleft);
  186. ig.Emit (OpCodes.Br, exit);
  187. // try_right;
  188. ig.MarkLabel (try_right);
  189. vright = EmitStored (ec, right);
  190. if (IsNullable (right.Type)){
  191. ig.Emit (OpCodes.Ldloca, vright);
  192. ig.Emit (OpCodes.Call, has_value);
  193. } else
  194. ig.Emit (OpCodes.Ldloc, vright);
  195. ig.Emit (OpCodes.Brfalse, setup_null);
  196. ig.Emit (OpCodes.Ldloc, vright);
  197. ig.Emit (OpCodes.Br, exit);
  198. // setup_null:
  199. ig.MarkLabel (setup_null);
  200. LocalBuilder ret = ig.DeclareLocal (Type);
  201. ig.Emit (OpCodes.Ldloca, ret);
  202. ig.Emit (OpCodes.Initobj, Type);
  203. ig.Emit (OpCodes.Ldloc, ret);
  204. // exit:
  205. ig.MarkLabel (exit);
  206. }
  207. void EmitBinaryOperator (EmitContext ec)
  208. {
  209. OpCode opcode;
  210. var ig = ec.ig;
  211. bool is_unsigned = IsUnsigned (left.Type);
  212. switch (NodeType) {
  213. case ExpressionType.Add:
  214. opcode = OpCodes.Add;
  215. break;
  216. case ExpressionType.AddChecked:
  217. if (left.Type == typeof (int) || left.Type == typeof (long))
  218. opcode = OpCodes.Add_Ovf;
  219. else if (is_unsigned)
  220. opcode = OpCodes.Add_Ovf_Un;
  221. else
  222. opcode = OpCodes.Add;
  223. break;
  224. case ExpressionType.Subtract:
  225. opcode = OpCodes.Sub;
  226. break;
  227. case ExpressionType.SubtractChecked:
  228. if (left.Type == typeof (int) || left.Type == typeof (long))
  229. opcode = OpCodes.Sub_Ovf;
  230. else if (is_unsigned)
  231. opcode = OpCodes.Sub_Ovf_Un;
  232. else
  233. opcode = OpCodes.Sub;
  234. break;
  235. case ExpressionType.Multiply:
  236. opcode = OpCodes.Mul;
  237. break;
  238. case ExpressionType.MultiplyChecked:
  239. if (left.Type == typeof (int) || left.Type == typeof (long))
  240. opcode = OpCodes.Mul_Ovf;
  241. else if (is_unsigned)
  242. opcode = OpCodes.Mul_Ovf_Un;
  243. else
  244. opcode = OpCodes.Mul;
  245. break;
  246. case ExpressionType.Divide:
  247. if (is_unsigned)
  248. opcode = OpCodes.Div_Un;
  249. else
  250. opcode = OpCodes.Div;
  251. break;
  252. case ExpressionType.Modulo:
  253. if (is_unsigned)
  254. opcode = OpCodes.Rem_Un;
  255. else
  256. opcode = OpCodes.Rem;
  257. break;
  258. case ExpressionType.RightShift:
  259. if (is_unsigned)
  260. opcode = OpCodes.Shr_Un;
  261. else
  262. opcode = OpCodes.Shr;
  263. break;
  264. case ExpressionType.LeftShift:
  265. opcode = OpCodes.Shl;
  266. break;
  267. case ExpressionType.And:
  268. opcode = OpCodes.And;
  269. break;
  270. case ExpressionType.Or:
  271. opcode = OpCodes.Or;
  272. break;
  273. case ExpressionType.ExclusiveOr:
  274. opcode = OpCodes.Xor;
  275. break;
  276. case ExpressionType.GreaterThan:
  277. if (is_unsigned)
  278. opcode = OpCodes.Cgt_Un;
  279. else
  280. opcode = OpCodes.Cgt;
  281. break;
  282. case ExpressionType.GreaterThanOrEqual:
  283. Type le = left.Type;
  284. if (is_unsigned || (le == typeof (double) || le == typeof (float)))
  285. ig.Emit (OpCodes.Clt_Un);
  286. else
  287. ig.Emit (OpCodes.Clt);
  288. ig.Emit (OpCodes.Ldc_I4_0);
  289. opcode = OpCodes.Ceq;
  290. break;
  291. case ExpressionType.LessThan:
  292. if (is_unsigned)
  293. opcode = OpCodes.Clt_Un;
  294. else
  295. opcode = OpCodes.Clt;
  296. break;
  297. case ExpressionType.LessThanOrEqual:
  298. Type lt = left.Type;
  299. if (is_unsigned || (lt == typeof (double) || lt == typeof (float)))
  300. ig.Emit (OpCodes.Cgt_Un);
  301. else
  302. ig.Emit (OpCodes.Cgt);
  303. ig.Emit (OpCodes.Ldc_I4_0);
  304. opcode = OpCodes.Ceq;
  305. break;
  306. case ExpressionType.Equal:
  307. opcode = OpCodes.Ceq;
  308. break;
  309. case ExpressionType.NotEqual:
  310. ig.Emit (OpCodes.Ceq);
  311. ig.Emit (OpCodes.Ldc_I4_0);
  312. opcode = OpCodes.Ceq;
  313. break;
  314. default:
  315. throw new InvalidOperationException (string.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  316. }
  317. ig.Emit (opcode);
  318. }
  319. void EmitLiftedSimpleBinary (EmitContext ec)
  320. {
  321. Label empty_value;
  322. LocalBuilder ret = null;
  323. LocalBuilder vleft, vright;
  324. var ig = ec.ig;
  325. empty_value = ig.DefineLabel ();
  326. ret = ig.DeclareLocal (Type);
  327. vleft = EmitStored (ec, left);
  328. vright = EmitStored (ec, right);
  329. MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
  330. MethodInfo get_value = GetMethodNoPar (left.Type, "get_Value");
  331. ig.Emit (OpCodes.Ldloca, vleft);
  332. ig.Emit (OpCodes.Call, has_value);
  333. ig.Emit (OpCodes.Brfalse, empty_value);
  334. ig.Emit (OpCodes.Ldloca, vright);
  335. ig.Emit (OpCodes.Call, has_value);
  336. ig.Emit (OpCodes.Brfalse, empty_value);
  337. ig.Emit (OpCodes.Ldloca, vleft);
  338. ig.Emit (OpCodes.Call, get_value);
  339. ig.Emit (OpCodes.Ldloca, vright);
  340. ig.Emit (OpCodes.Call, get_value);
  341. EmitBinaryOperator (ec);
  342. ig.Emit (OpCodes.Newobj, left.Type.GetConstructors () [0]);
  343. Label skip = ig.DefineLabel ();
  344. ig.Emit (OpCodes.Br_S, skip);
  345. ig.MarkLabel (empty_value);
  346. ig.Emit (OpCodes.Ldloc, ret);
  347. ig.Emit (OpCodes.Ldloca, ret);
  348. ig.Emit (OpCodes.Initobj, Type);
  349. ig.MarkLabel (skip);
  350. }
  351. void EmitSimpleBinary (EmitContext ec)
  352. {
  353. if (IsLifted) {
  354. EmitLiftedSimpleBinary (ec);
  355. return;
  356. }
  357. left.Emit (ec);
  358. right.Emit (ec);
  359. EmitBinaryOperator (ec);
  360. }
  361. internal override void Emit (EmitContext ec)
  362. {
  363. ILGenerator ig = ec.ig;
  364. OpCode opcode;
  365. if (method != null){
  366. EmitMethod (ec);
  367. return;
  368. }
  369. switch (NodeType){
  370. case ExpressionType.And:
  371. EmitLogical (ec, true, false);
  372. return;
  373. case ExpressionType.Or:
  374. EmitLogical (ec, false, false);
  375. return;
  376. case ExpressionType.AndAlso:
  377. EmitLogical (ec, true, true);
  378. return;
  379. case ExpressionType.OrElse:
  380. EmitLogical (ec, false, true);
  381. return;
  382. case ExpressionType.Coalesce:
  383. EmitCoalesce (ec);
  384. return;
  385. case ExpressionType.Power:
  386. left.Emit (ec);
  387. right.Emit (ec);
  388. ig.Emit (OpCodes.Call, typeof (System.Math).GetMethod ("Pow"));
  389. return;
  390. }
  391. EmitSimpleBinary (ec);
  392. }
  393. }
  394. }