BinaryExpression.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. var ig = ec.ig;
  200. var done = ig.DefineLabel ();
  201. var load_right = ig.DefineLabel ();
  202. var left = ec.EmitStored (this.left);
  203. var left_is_nullable = left.LocalType.IsNullable ();
  204. if (left_is_nullable)
  205. ec.EmitNullableHasValue (left);
  206. else
  207. ec.EmitLoad (left);
  208. ig.Emit (OpCodes.Brfalse, load_right);
  209. if (left_is_nullable && !Type.IsNullable ())
  210. ec.EmitNullableGetValue (left);
  211. else
  212. ec.EmitLoad (left);
  213. ig.Emit (OpCodes.Br, done);
  214. ig.MarkLabel (load_right);
  215. ec.Emit (this.right);
  216. ig.MarkLabel (done);
  217. }
  218. static bool IsInt32OrInt64 (Type type)
  219. {
  220. return type == typeof (int) || type == typeof (long);
  221. }
  222. static bool IsSingleOrDouble (Type type)
  223. {
  224. return type == typeof (float) || type == typeof (double);
  225. }
  226. void EmitBinaryOperator (EmitContext ec)
  227. {
  228. var ig = ec.ig;
  229. bool is_unsigned = IsUnsigned (left.Type);
  230. switch (NodeType) {
  231. case ExpressionType.Add:
  232. ig.Emit (OpCodes.Add);
  233. break;
  234. case ExpressionType.AddChecked:
  235. if (IsInt32OrInt64 (left.Type))
  236. ig.Emit (OpCodes.Add_Ovf);
  237. else
  238. ig.Emit (is_unsigned ? OpCodes.Add_Ovf_Un : OpCodes.Add);
  239. break;
  240. case ExpressionType.Subtract:
  241. ig.Emit (OpCodes.Sub);
  242. break;
  243. case ExpressionType.SubtractChecked:
  244. if (IsInt32OrInt64 (left.Type))
  245. ig.Emit (OpCodes.Sub_Ovf);
  246. else
  247. ig.Emit (is_unsigned ? OpCodes.Sub_Ovf_Un : OpCodes.Sub);
  248. break;
  249. case ExpressionType.Multiply:
  250. ig.Emit (OpCodes.Mul);
  251. break;
  252. case ExpressionType.MultiplyChecked:
  253. if (IsInt32OrInt64 (left.Type))
  254. ig.Emit (OpCodes.Mul_Ovf);
  255. else
  256. ig.Emit (is_unsigned ? OpCodes.Mul_Ovf_Un : OpCodes.Mul);
  257. break;
  258. case ExpressionType.Divide:
  259. ig.Emit (is_unsigned ? OpCodes.Div_Un : OpCodes.Div);
  260. break;
  261. case ExpressionType.Modulo:
  262. ig.Emit (is_unsigned ? OpCodes.Rem_Un : OpCodes.Rem);
  263. break;
  264. case ExpressionType.RightShift:
  265. ig.Emit (is_unsigned ? OpCodes.Shr_Un : OpCodes.Shr);
  266. break;
  267. case ExpressionType.LeftShift:
  268. ig.Emit (OpCodes.Shl);
  269. break;
  270. case ExpressionType.And:
  271. ig.Emit (OpCodes.And);
  272. break;
  273. case ExpressionType.Or:
  274. ig.Emit (OpCodes.Or);
  275. break;
  276. case ExpressionType.ExclusiveOr:
  277. ig.Emit (OpCodes.Xor);
  278. break;
  279. case ExpressionType.GreaterThan:
  280. ig.Emit (is_unsigned ? OpCodes.Cgt_Un : OpCodes.Cgt);
  281. break;
  282. case ExpressionType.GreaterThanOrEqual:
  283. if (is_unsigned || IsSingleOrDouble (left.Type))
  284. ig.Emit (OpCodes.Clt_Un);
  285. else
  286. ig.Emit (OpCodes.Clt);
  287. ig.Emit (OpCodes.Ldc_I4_0);
  288. ig.Emit (OpCodes.Ceq);
  289. break;
  290. case ExpressionType.LessThan:
  291. ig.Emit (is_unsigned ? OpCodes.Clt_Un : OpCodes.Clt);
  292. break;
  293. case ExpressionType.LessThanOrEqual:
  294. if (is_unsigned || IsSingleOrDouble (left.Type))
  295. ig.Emit (OpCodes.Cgt_Un);
  296. else
  297. ig.Emit (OpCodes.Cgt);
  298. ig.Emit (OpCodes.Ldc_I4_0);
  299. ig.Emit (OpCodes.Ceq);
  300. break;
  301. case ExpressionType.Equal:
  302. ig.Emit (OpCodes.Ceq);
  303. break;
  304. case ExpressionType.NotEqual:
  305. ig.Emit (OpCodes.Ceq);
  306. ig.Emit (OpCodes.Ldc_I4_0);
  307. ig.Emit (OpCodes.Ceq);
  308. break;
  309. case ExpressionType.Power:
  310. ig.Emit (OpCodes.Call, typeof (Math).GetMethod ("Pow"));
  311. break;
  312. default:
  313. throw new InvalidOperationException (
  314. string.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
  315. }
  316. }
  317. void EmitLiftedArithmeticBinary (EmitContext ec)
  318. {
  319. EmitLiftedToNullBinary (ec);
  320. }
  321. void EmitLiftedToNullBinary (EmitContext ec)
  322. {
  323. var ig = ec.ig;
  324. var left = ec.EmitStored (this.left);
  325. var right = ec.EmitStored (this.right);
  326. var result = ig.DeclareLocal (Type);
  327. var has_value = ig.DefineLabel ();
  328. var done = ig.DefineLabel ();
  329. ec.EmitNullableHasValue (left);
  330. ec.EmitNullableHasValue (right);
  331. ig.Emit (OpCodes.And);
  332. ig.Emit (OpCodes.Brtrue, has_value);
  333. ec.EmitNullableInitialize (result);
  334. ig.Emit (OpCodes.Br, done);
  335. ig.MarkLabel (has_value);
  336. ec.EmitNullableGetValueOrDefault (left);
  337. ec.EmitNullableGetValueOrDefault (right);
  338. EmitBinaryOperator (ec);
  339. ec.EmitNullableNew (result.LocalType);
  340. ig.MarkLabel (done);
  341. }
  342. void EmitLiftedRelationalBinary (EmitContext ec)
  343. {
  344. var ig = ec.ig;
  345. var left = ec.EmitStored (this.left);
  346. var right = ec.EmitStored (this.right);
  347. var ret = ig.DefineLabel ();
  348. var done = ig.DefineLabel ();
  349. ec.EmitNullableGetValueOrDefault (left);
  350. ec.EmitNullableGetValueOrDefault (right);
  351. switch (NodeType) {
  352. case ExpressionType.Equal:
  353. case ExpressionType.NotEqual:
  354. ig.Emit (OpCodes.Bne_Un, ret);
  355. break;
  356. default:
  357. EmitBinaryOperator (ec);
  358. ig.Emit (OpCodes.Brfalse, ret);
  359. break;
  360. }
  361. ec.EmitNullableHasValue (left);
  362. ec.EmitNullableHasValue (right);
  363. switch (NodeType) {
  364. case ExpressionType.Equal:
  365. ig.Emit (OpCodes.Ceq);
  366. break;
  367. case ExpressionType.NotEqual:
  368. ig.Emit (OpCodes.Ceq);
  369. ig.Emit (OpCodes.Ldc_I4_0);
  370. ig.Emit (OpCodes.Ceq);
  371. break;
  372. default:
  373. ig.Emit (OpCodes.And);
  374. break;
  375. }
  376. ig.Emit (OpCodes.Br, done);
  377. ig.MarkLabel (ret);
  378. ig.Emit (NodeType == ExpressionType.NotEqual ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
  379. ig.MarkLabel (done);
  380. }
  381. void EmitArithmeticBinary (EmitContext ec)
  382. {
  383. if (!IsLifted)
  384. EmitNonLiftedBinary (ec);
  385. else
  386. EmitLiftedArithmeticBinary (ec);
  387. }
  388. void EmitNonLiftedBinary (EmitContext ec)
  389. {
  390. ec.Emit (left);
  391. ec.Emit (right);
  392. EmitBinaryOperator (ec);
  393. }
  394. void EmitRelationalBinary (EmitContext ec)
  395. {
  396. if (!IsLifted)
  397. EmitNonLiftedBinary (ec);
  398. else if (IsLiftedToNull)
  399. EmitLiftedToNullBinary (ec);
  400. else
  401. EmitLiftedRelationalBinary (ec);
  402. }
  403. internal override void Emit (EmitContext ec)
  404. {
  405. if (method != null){
  406. EmitMethod (ec);
  407. return;
  408. }
  409. switch (NodeType){
  410. case ExpressionType.ArrayIndex:
  411. EmitArrayAccess (ec);
  412. return;
  413. case ExpressionType.Coalesce:
  414. EmitCoalesce (ec);
  415. return;
  416. case ExpressionType.Power:
  417. case ExpressionType.Add:
  418. case ExpressionType.AddChecked:
  419. case ExpressionType.Divide:
  420. case ExpressionType.ExclusiveOr:
  421. case ExpressionType.LeftShift:
  422. case ExpressionType.Modulo:
  423. case ExpressionType.Multiply:
  424. case ExpressionType.MultiplyChecked:
  425. case ExpressionType.RightShift:
  426. case ExpressionType.Subtract:
  427. case ExpressionType.SubtractChecked:
  428. EmitArithmeticBinary (ec);
  429. return;
  430. case ExpressionType.Equal:
  431. case ExpressionType.GreaterThan:
  432. case ExpressionType.GreaterThanOrEqual:
  433. case ExpressionType.LessThan:
  434. case ExpressionType.LessThanOrEqual:
  435. case ExpressionType.NotEqual:
  436. EmitRelationalBinary (ec);
  437. return;
  438. case ExpressionType.And:
  439. case ExpressionType.Or:
  440. case ExpressionType.AndAlso:
  441. case ExpressionType.OrElse:
  442. EmitLogicalBinary (ec);
  443. return;
  444. default:
  445. throw new NotSupportedException (this.NodeType.ToString ());
  446. }
  447. }
  448. }
  449. }