BinaryExpression.cs 12 KB

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