BinaryExpression.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. void EmitArrayAccess (EmitContext ec)
  93. {
  94. left.Emit (ec);
  95. right.Emit (ec);
  96. ec.ig.Emit (OpCodes.Ldelem, this.Type);
  97. }
  98. void EmitLogicalBinary (EmitContext ec)
  99. {
  100. switch (NodeType) {
  101. case ExpressionType.And:
  102. case ExpressionType.Or:
  103. if (!IsLifted)
  104. EmitLogical (ec);
  105. else if (Type == typeof (bool?))
  106. EmitLiftedLogical (ec);
  107. else
  108. EmitLiftedArithmeticBinary (ec);
  109. break;
  110. case ExpressionType.AndAlso:
  111. case ExpressionType.OrElse:
  112. if (!IsLifted)
  113. EmitLogicalShortCircuit (ec);
  114. else
  115. EmitLiftedLogicalShortCircuit (ec);
  116. break;
  117. }
  118. }
  119. void EmitLogical (EmitContext ec)
  120. {
  121. EmitNonLiftedBinary (ec);
  122. }
  123. void EmitLiftedLogical (EmitContext ec)
  124. {
  125. var ig = ec.ig;
  126. var and = NodeType == ExpressionType.And;
  127. var left = ec.EmitStored (this.left);
  128. var right = ec.EmitStored (this.right);
  129. var ret_from_left = ig.DefineLabel ();
  130. var ret_from_right = ig.DefineLabel ();
  131. var done = ig.DefineLabel ();
  132. ec.EmitNullableGetValueOrDefault (left);
  133. ig.Emit (OpCodes.Brtrue, ret_from_left);
  134. ec.EmitNullableGetValueOrDefault (right);
  135. ig.Emit (OpCodes.Brtrue, ret_from_right);
  136. ec.EmitNullableHasValue (left);
  137. ig.Emit (OpCodes.Brfalse, ret_from_left);
  138. ig.MarkLabel (ret_from_right);
  139. ec.EmitLoad (and ? left : right);
  140. ig.Emit (OpCodes.Br, done);
  141. ig.MarkLabel (ret_from_left);
  142. ec.EmitLoad (and ? right : left);
  143. ig.MarkLabel (done);
  144. }
  145. void EmitLogicalShortCircuit (EmitContext ec)
  146. {
  147. var ig = ec.ig;
  148. var and = NodeType == ExpressionType.AndAlso;
  149. var ret = ig.DefineLabel ();
  150. var done = ig.DefineLabel ();
  151. ec.Emit (left);
  152. ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, ret);
  153. ec.Emit (right);
  154. ig.Emit (OpCodes.Br, done);
  155. ig.MarkLabel (ret);
  156. ig.Emit (and ? OpCodes.Ldc_I4_0 : OpCodes.Ldc_I4_1);
  157. ig.MarkLabel (done);
  158. }
  159. void EmitLiftedLogicalShortCircuit (EmitContext ec)
  160. {
  161. var ig = ec.ig;
  162. var and = NodeType == ExpressionType.AndAlso;
  163. var left_is_null = ig.DefineLabel ();
  164. var ret_from_left = ig.DefineLabel ();
  165. var ret_null = ig.DefineLabel ();
  166. var ret_new = ig.DefineLabel();
  167. var done = ig.DefineLabel();
  168. var left = ec.EmitStored (this.left);
  169. ec.EmitNullableHasValue (left);
  170. ig.Emit (OpCodes.Brfalse, left_is_null);
  171. ec.EmitNullableGetValueOrDefault (left);
  172. ig.Emit (OpCodes.Ldc_I4_0);
  173. ig.Emit (OpCodes.Ceq);
  174. ig.Emit (and ? OpCodes.Brtrue : OpCodes.Brfalse, ret_from_left);
  175. ig.MarkLabel (left_is_null);
  176. var right = ec.EmitStored (this.right);
  177. ec.EmitNullableHasValue (right);
  178. ig.Emit (OpCodes.Brfalse_S, ret_null);
  179. ec.EmitNullableGetValueOrDefault (right);
  180. ig.Emit (OpCodes.Ldc_I4_0);
  181. ig.Emit (OpCodes.Ceq);
  182. ig.Emit (and ? OpCodes.Brtrue : OpCodes.Brfalse, ret_from_left);
  183. ec.EmitNullableHasValue (left);
  184. ig.Emit (OpCodes.Brfalse, ret_null);
  185. ig.Emit (and ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  186. ig.Emit (OpCodes.Br_S, ret_new);
  187. ig.MarkLabel (ret_from_left);
  188. ig.Emit (and ? OpCodes.Ldc_I4_0 : OpCodes.Ldc_I4_1);
  189. ig.MarkLabel (ret_new);
  190. ec.EmitNullableNew (typeof (bool?));
  191. ig.Emit (OpCodes.Br, done);
  192. ig.MarkLabel (ret_null);
  193. var ret = ig.DeclareLocal (typeof (bool?));
  194. ec.EmitNullableInitialize (ret);
  195. ig.MarkLabel (done);
  196. }
  197. void EmitCoalesce (EmitContext ec)
  198. {
  199. ILGenerator ig = ec.ig;
  200. LocalBuilder vleft;
  201. LocalBuilder vright;
  202. MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
  203. Label exit = ig.DefineLabel ();
  204. Label try_right = ig.DefineLabel ();
  205. Label setup_null = ig.DefineLabel ();
  206. vleft = ec.EmitStored (left);
  207. if (IsNullable (left.Type)){
  208. ig.Emit (OpCodes.Ldloca, vleft);
  209. ig.Emit (OpCodes.Call, has_value);
  210. } else
  211. ig.Emit (OpCodes.Ldloc, vleft);
  212. ig.Emit (OpCodes.Brfalse, try_right);
  213. ig.Emit (OpCodes.Ldloc, vleft);
  214. ig.Emit (OpCodes.Br, exit);
  215. // try_right;
  216. ig.MarkLabel (try_right);
  217. vright = ec.EmitStored (right);
  218. if (IsNullable (right.Type)){
  219. ig.Emit (OpCodes.Ldloca, vright);
  220. ig.Emit (OpCodes.Call, has_value);
  221. } else
  222. ig.Emit (OpCodes.Ldloc, vright);
  223. ig.Emit (OpCodes.Brfalse, setup_null);
  224. ig.Emit (OpCodes.Ldloc, vright);
  225. ig.Emit (OpCodes.Br, exit);
  226. // setup_null:
  227. ig.MarkLabel (setup_null);
  228. LocalBuilder ret = ig.DeclareLocal (Type);
  229. ig.Emit (OpCodes.Ldloca, ret);
  230. ig.Emit (OpCodes.Initobj, Type);
  231. ig.Emit (OpCodes.Ldloc, ret);
  232. // exit:
  233. ig.MarkLabel (exit);
  234. }
  235. static bool IsInt32OrInt64 (Type type)
  236. {
  237. return type == typeof (int) || type == typeof (long);
  238. }
  239. static bool IsSingleOrDouble (Type type)
  240. {
  241. return type == typeof (float) || type == typeof (double);
  242. }
  243. void EmitBinaryOperator (EmitContext ec)
  244. {
  245. var ig = ec.ig;
  246. bool is_unsigned = IsUnsigned (left.Type);
  247. switch (NodeType) {
  248. case ExpressionType.Add:
  249. ig.Emit (OpCodes.Add);
  250. break;
  251. case ExpressionType.AddChecked:
  252. if (IsInt32OrInt64 (left.Type))
  253. ig.Emit (OpCodes.Add_Ovf);
  254. else
  255. ig.Emit (is_unsigned ? OpCodes.Add_Ovf_Un : OpCodes.Add);
  256. break;
  257. case ExpressionType.Subtract:
  258. ig.Emit (OpCodes.Sub);
  259. break;
  260. case ExpressionType.SubtractChecked:
  261. if (IsInt32OrInt64 (left.Type))
  262. ig.Emit (OpCodes.Sub_Ovf);
  263. else
  264. ig.Emit (is_unsigned ? OpCodes.Sub_Ovf_Un : OpCodes.Sub);
  265. break;
  266. case ExpressionType.Multiply:
  267. ig.Emit (OpCodes.Mul);
  268. break;
  269. case ExpressionType.MultiplyChecked:
  270. if (IsInt32OrInt64 (left.Type))
  271. ig.Emit (OpCodes.Mul_Ovf);
  272. else
  273. ig.Emit (is_unsigned ? OpCodes.Mul_Ovf_Un : OpCodes.Mul);
  274. break;
  275. case ExpressionType.Divide:
  276. ig.Emit (is_unsigned ? OpCodes.Div_Un : OpCodes.Div);
  277. break;
  278. case ExpressionType.Modulo:
  279. ig.Emit (is_unsigned ? OpCodes.Rem_Un : OpCodes.Rem);
  280. break;
  281. case ExpressionType.RightShift:
  282. ig.Emit (is_unsigned ? OpCodes.Shr_Un : OpCodes.Shr);
  283. break;
  284. case ExpressionType.LeftShift:
  285. ig.Emit (OpCodes.Shl);
  286. break;
  287. case ExpressionType.And:
  288. ig.Emit (OpCodes.And);
  289. break;
  290. case ExpressionType.Or:
  291. ig.Emit (OpCodes.Or);
  292. break;
  293. case ExpressionType.ExclusiveOr:
  294. ig.Emit (OpCodes.Xor);
  295. break;
  296. case ExpressionType.GreaterThan:
  297. ig.Emit (is_unsigned ? OpCodes.Cgt_Un : OpCodes.Cgt);
  298. break;
  299. case ExpressionType.GreaterThanOrEqual:
  300. if (is_unsigned || IsSingleOrDouble (left.Type))
  301. ig.Emit (OpCodes.Clt_Un);
  302. else
  303. ig.Emit (OpCodes.Clt);
  304. ig.Emit (OpCodes.Ldc_I4_0);
  305. ig.Emit (OpCodes.Ceq);
  306. break;
  307. case ExpressionType.LessThan:
  308. ig.Emit (is_unsigned ? OpCodes.Clt_Un : OpCodes.Clt);
  309. break;
  310. case ExpressionType.LessThanOrEqual:
  311. if (is_unsigned || IsSingleOrDouble (left.Type))
  312. ig.Emit (OpCodes.Cgt_Un);
  313. else
  314. ig.Emit (OpCodes.Cgt);
  315. ig.Emit (OpCodes.Ldc_I4_0);
  316. ig.Emit (OpCodes.Ceq);
  317. break;
  318. case ExpressionType.Equal:
  319. ig.Emit (OpCodes.Ceq);
  320. break;
  321. case ExpressionType.NotEqual:
  322. ig.Emit (OpCodes.Ceq);
  323. ig.Emit (OpCodes.Ldc_I4_0);
  324. ig.Emit (OpCodes.Ceq);
  325. break;
  326. case ExpressionType.Power:
  327. ig.Emit (OpCodes.Call, typeof (Math).GetMethod ("Pow"));
  328. break;
  329. default:
  330. throw new InvalidOperationException (
  331. string.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  332. }
  333. }
  334. void EmitLiftedArithmeticBinary (EmitContext ec)
  335. {
  336. EmitLiftedToNullBinary (ec);
  337. }
  338. void EmitLiftedToNullBinary (EmitContext ec)
  339. {
  340. var ig = ec.ig;
  341. var left = ec.EmitStored (this.left);
  342. var right = ec.EmitStored (this.right);
  343. var result = ig.DeclareLocal (Type);
  344. var has_value = ig.DefineLabel ();
  345. var done = ig.DefineLabel ();
  346. ec.EmitNullableHasValue (left);
  347. ec.EmitNullableHasValue (right);
  348. ig.Emit (OpCodes.And);
  349. ig.Emit (OpCodes.Brtrue, has_value);
  350. ec.EmitNullableInitialize (result);
  351. ig.Emit (OpCodes.Br, done);
  352. ig.MarkLabel (has_value);
  353. ec.EmitNullableGetValueOrDefault (left);
  354. ec.EmitNullableGetValueOrDefault (right);
  355. EmitBinaryOperator (ec);
  356. ec.EmitNullableNew (result.LocalType);
  357. ig.MarkLabel (done);
  358. }
  359. void EmitLiftedRelationalBinary (EmitContext ec)
  360. {
  361. var ig = ec.ig;
  362. var left = ec.EmitStored (this.left);
  363. var right = ec.EmitStored (this.right);
  364. var ret = ig.DefineLabel ();
  365. var done = ig.DefineLabel ();
  366. ec.EmitNullableGetValueOrDefault (left);
  367. ec.EmitNullableGetValueOrDefault (right);
  368. switch (NodeType) {
  369. case ExpressionType.Equal:
  370. case ExpressionType.NotEqual:
  371. ig.Emit (OpCodes.Bne_Un, ret);
  372. break;
  373. default:
  374. EmitBinaryOperator (ec);
  375. ig.Emit (OpCodes.Brfalse, ret);
  376. break;
  377. }
  378. ec.EmitNullableHasValue (left);
  379. ec.EmitNullableHasValue (right);
  380. switch (NodeType) {
  381. case ExpressionType.Equal:
  382. ig.Emit (OpCodes.Ceq);
  383. break;
  384. case ExpressionType.NotEqual:
  385. ig.Emit (OpCodes.Ceq);
  386. ig.Emit (OpCodes.Ldc_I4_0);
  387. ig.Emit (OpCodes.Ceq);
  388. break;
  389. default:
  390. ig.Emit (OpCodes.And);
  391. break;
  392. }
  393. ig.Emit (OpCodes.Br, done);
  394. ig.MarkLabel (ret);
  395. ig.Emit (NodeType == ExpressionType.NotEqual ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  396. ig.MarkLabel (done);
  397. }
  398. void EmitArithmeticBinary (EmitContext ec)
  399. {
  400. if (!IsLifted)
  401. EmitNonLiftedBinary (ec);
  402. else
  403. EmitLiftedArithmeticBinary (ec);
  404. }
  405. void EmitNonLiftedBinary (EmitContext ec)
  406. {
  407. ec.Emit (left);
  408. ec.Emit (right);
  409. EmitBinaryOperator (ec);
  410. }
  411. void EmitRelationalBinary (EmitContext ec)
  412. {
  413. if (!IsLifted)
  414. EmitNonLiftedBinary (ec);
  415. else if (IsLiftedToNull)
  416. EmitLiftedToNullBinary (ec);
  417. else
  418. EmitLiftedRelationalBinary (ec);
  419. }
  420. internal override void Emit (EmitContext ec)
  421. {
  422. if (method != null){
  423. EmitMethod (ec);
  424. return;
  425. }
  426. switch (NodeType){
  427. case ExpressionType.ArrayIndex:
  428. EmitArrayAccess (ec);
  429. return;
  430. case ExpressionType.Coalesce:
  431. EmitCoalesce (ec);
  432. return;
  433. case ExpressionType.Power:
  434. case ExpressionType.Add:
  435. case ExpressionType.AddChecked:
  436. case ExpressionType.Divide:
  437. case ExpressionType.ExclusiveOr:
  438. case ExpressionType.LeftShift:
  439. case ExpressionType.Modulo:
  440. case ExpressionType.Multiply:
  441. case ExpressionType.MultiplyChecked:
  442. case ExpressionType.RightShift:
  443. case ExpressionType.Subtract:
  444. case ExpressionType.SubtractChecked:
  445. EmitArithmeticBinary (ec);
  446. return;
  447. case ExpressionType.Equal:
  448. case ExpressionType.GreaterThan:
  449. case ExpressionType.GreaterThanOrEqual:
  450. case ExpressionType.LessThan:
  451. case ExpressionType.LessThanOrEqual:
  452. case ExpressionType.NotEqual:
  453. EmitRelationalBinary (ec);
  454. return;
  455. case ExpressionType.And:
  456. case ExpressionType.Or:
  457. case ExpressionType.AndAlso:
  458. case ExpressionType.OrElse:
  459. EmitLogicalBinary (ec);
  460. return;
  461. default:
  462. throw new NotSupportedException (this.NodeType.ToString ());
  463. }
  464. }
  465. }
  466. }