BinaryExpression.cs 12 KB

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