BinaryExpression.cs 15 KB

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