| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- //
- // BinaryExpression.cs
- //
- // Author:
- // Jb Evain ([email protected])
- // Miguel de Icaza ([email protected])
- //
- // Contains code from the Mono C# compiler:
- // Marek Safar ([email protected])
- // Martin Baulig ([email protected])
- // Raja Harinath ([email protected])
- //
- // (C) 2001-2003 Ximian, Inc.
- // (C) 2004-2008 Novell, Inc. (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Reflection;
- using System.Reflection.Emit;
- namespace System.Linq.Expressions {
- public sealed class BinaryExpression : Expression {
- Expression left;
- Expression right;
- LambdaExpression conversion;
- MethodInfo method;
- bool lift_to_null, is_lifted;
- public Expression Left {
- get { return left; }
- }
- public Expression Right {
- get { return right; }
- }
- public MethodInfo Method {
- get { return method; }
- }
- public bool IsLifted {
- get { return is_lifted; }
- }
- public bool IsLiftedToNull {
- get { return lift_to_null; }
- }
- public LambdaExpression Conversion {
- get { return conversion; }
- }
- internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right)
- : base (node_type, type)
- {
- this.left = left;
- this.right = right;
- }
- internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, MethodInfo method)
- : base (node_type, type)
- {
- this.left = left;
- this.right = right;
- this.method = method;
- }
- internal BinaryExpression (ExpressionType node_type, Type type, Expression left, Expression right, bool lift_to_null,
- bool is_lifted, MethodInfo method, LambdaExpression conversion) : base (node_type, type)
- {
- this.left = left;
- this.right = right;
- this.method = method;
- this.conversion = conversion;
- this.lift_to_null = lift_to_null;
- this.is_lifted = is_lifted;
- }
- void EmitMethod (EmitContext ec)
- {
- left.Emit (ec);
- right.Emit (ec);
- ec.EmitCall (method);
- }
- static MethodInfo GetMethodNoPar (Type t, string name)
- {
- var method = t.GetMethod (name, Type.EmptyTypes);
- if (method == null)
- throw new ArgumentException (
- string.Format ("Internal error: method {0} with no parameters not found on {1}", name, t));
- return method;
- }
- void EmitArrayAccess (EmitContext ec)
- {
- left.Emit (ec);
- right.Emit (ec);
- ec.ig.Emit (OpCodes.Ldelem, this.Type);
- }
- void EmitLiftedLogical (EmitContext ec, bool and, bool short_circuit)
- {
- var ig = ec.ig;
- LocalBuilder ret = ig.DeclareLocal (Type);
- LocalBuilder vleft = null, vright = null;
- MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
- MethodInfo get_value = GetMethodNoPar (left.Type, "get_Value");
- vleft = ec.EmitStored (left);
- if (!short_circuit)
- vright = ec.EmitStored (right);
- Label left_is_null = ig.DefineLabel ();
- Label right_is_null = ig.DefineLabel ();
- Label create = ig.DefineLabel ();
- Label exit = ig.DefineLabel ();
- Label both_are_null = ig.DefineLabel ();
- // Check left
- ig.Emit (OpCodes.Ldloca, vleft);
- ig.Emit (OpCodes.Call, has_value);
- ig.Emit (OpCodes.Brfalse, left_is_null);
- ig.Emit (OpCodes.Ldloca, vleft);
- ig.Emit (OpCodes.Call, get_value);
- ig.Emit (OpCodes.Dup);
- ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, create);
- // Deal with right
- if (short_circuit)
- vright = ec.EmitStored (right);
- ig.Emit (OpCodes.Ldloca, vright);
- ig.Emit (OpCodes.Call, has_value);
- ig.Emit (OpCodes.Brfalse, right_is_null);
- ig.Emit (OpCodes.Ldloca, vright);
- ig.Emit (OpCodes.Call, get_value);
- ig.Emit (and ? OpCodes.And : OpCodes.Or);
- ig.Emit (OpCodes.Br, create);
- // left_is_null:
- ig.MarkLabel (left_is_null);
- ig.Emit (OpCodes.Ldloca, vright);
- ig.Emit (OpCodes.Call, has_value);
- ig.Emit (OpCodes.Brfalse, both_are_null);
- ig.Emit (OpCodes.Ldloca, vright);
- ig.Emit (OpCodes.Call, get_value);
- ig.Emit (OpCodes.Dup);
- ig.Emit (and ? OpCodes.Brfalse : OpCodes.Brtrue, create);
- // right_is_null:
- ig.MarkLabel (right_is_null);
- ig.Emit (OpCodes.Pop);
- // both_are_null:
- ig.MarkLabel (both_are_null);
- ig.Emit (OpCodes.Ldloca, ret);
- ig.Emit (OpCodes.Initobj, Type);
- ig.Emit (OpCodes.Ldloc, ret);
- ig.Emit (OpCodes.Br, exit);
- // create:
- ig.MarkLabel (create);
- ig.Emit (OpCodes.Newobj, Type.GetConstructors () [0]);
- // exit:
- ig.MarkLabel (exit);
- }
- void EmitLogical (EmitContext ec, bool and, bool short_circuit)
- {
- if (IsLifted) {
- EmitLiftedLogical (ec, and, short_circuit);
- return;
- }
- left.Emit (ec);
- right.Emit (ec);
- ec.ig.Emit (and ? OpCodes.And : OpCodes.Or);
- }
- void EmitCoalesce (EmitContext ec)
- {
- ILGenerator ig = ec.ig;
- LocalBuilder vleft;
- LocalBuilder vright;
- MethodInfo has_value = left.Type.GetMethod ("get_HasValue");
- Label exit = ig.DefineLabel ();
- Label try_right = ig.DefineLabel ();
- Label setup_null = ig.DefineLabel ();
- vleft = ec.EmitStored (left);
- if (IsNullable (left.Type)){
- ig.Emit (OpCodes.Ldloca, vleft);
- ig.Emit (OpCodes.Call, has_value);
- } else
- ig.Emit (OpCodes.Ldloc, vleft);
- ig.Emit (OpCodes.Brfalse, try_right);
- ig.Emit (OpCodes.Ldloc, vleft);
- ig.Emit (OpCodes.Br, exit);
- // try_right;
- ig.MarkLabel (try_right);
- vright = ec.EmitStored (right);
- if (IsNullable (right.Type)){
- ig.Emit (OpCodes.Ldloca, vright);
- ig.Emit (OpCodes.Call, has_value);
- } else
- ig.Emit (OpCodes.Ldloc, vright);
- ig.Emit (OpCodes.Brfalse, setup_null);
- ig.Emit (OpCodes.Ldloc, vright);
- ig.Emit (OpCodes.Br, exit);
- // setup_null:
- ig.MarkLabel (setup_null);
- LocalBuilder ret = ig.DeclareLocal (Type);
- ig.Emit (OpCodes.Ldloca, ret);
- ig.Emit (OpCodes.Initobj, Type);
- ig.Emit (OpCodes.Ldloc, ret);
- // exit:
- ig.MarkLabel (exit);
- }
- void EmitBinaryOperator (EmitContext ec)
- {
- OpCode opcode;
- var ig = ec.ig;
- bool is_unsigned = IsUnsigned (left.Type);
- switch (NodeType) {
- case ExpressionType.Add:
- opcode = OpCodes.Add;
- break;
- case ExpressionType.AddChecked:
- if (left.Type == typeof (int) || left.Type == typeof (long))
- opcode = OpCodes.Add_Ovf;
- else if (is_unsigned)
- opcode = OpCodes.Add_Ovf_Un;
- else
- opcode = OpCodes.Add;
- break;
- case ExpressionType.Subtract:
- opcode = OpCodes.Sub;
- break;
- case ExpressionType.SubtractChecked:
- if (left.Type == typeof (int) || left.Type == typeof (long))
- opcode = OpCodes.Sub_Ovf;
- else if (is_unsigned)
- opcode = OpCodes.Sub_Ovf_Un;
- else
- opcode = OpCodes.Sub;
- break;
- case ExpressionType.Multiply:
- opcode = OpCodes.Mul;
- break;
- case ExpressionType.MultiplyChecked:
- if (left.Type == typeof (int) || left.Type == typeof (long))
- opcode = OpCodes.Mul_Ovf;
- else if (is_unsigned)
- opcode = OpCodes.Mul_Ovf_Un;
- else
- opcode = OpCodes.Mul;
- break;
- case ExpressionType.Divide:
- if (is_unsigned)
- opcode = OpCodes.Div_Un;
- else
- opcode = OpCodes.Div;
- break;
- case ExpressionType.Modulo:
- if (is_unsigned)
- opcode = OpCodes.Rem_Un;
- else
- opcode = OpCodes.Rem;
- break;
- case ExpressionType.RightShift:
- if (is_unsigned)
- opcode = OpCodes.Shr_Un;
- else
- opcode = OpCodes.Shr;
- break;
- case ExpressionType.LeftShift:
- opcode = OpCodes.Shl;
- break;
- case ExpressionType.And:
- opcode = OpCodes.And;
- break;
- case ExpressionType.Or:
- opcode = OpCodes.Or;
- break;
- case ExpressionType.ExclusiveOr:
- opcode = OpCodes.Xor;
- break;
- case ExpressionType.GreaterThan:
- if (is_unsigned)
- opcode = OpCodes.Cgt_Un;
- else
- opcode = OpCodes.Cgt;
- break;
- case ExpressionType.GreaterThanOrEqual:
- Type le = left.Type;
- if (is_unsigned || (le == typeof (double) || le == typeof (float)))
- ig.Emit (OpCodes.Clt_Un);
- else
- ig.Emit (OpCodes.Clt);
- ig.Emit (OpCodes.Ldc_I4_0);
- opcode = OpCodes.Ceq;
- break;
- case ExpressionType.LessThan:
- if (is_unsigned)
- opcode = OpCodes.Clt_Un;
- else
- opcode = OpCodes.Clt;
- break;
- case ExpressionType.LessThanOrEqual:
- Type lt = left.Type;
- if (is_unsigned || (lt == typeof (double) || lt == typeof (float)))
- ig.Emit (OpCodes.Cgt_Un);
- else
- ig.Emit (OpCodes.Cgt);
- ig.Emit (OpCodes.Ldc_I4_0);
- opcode = OpCodes.Ceq;
- break;
- case ExpressionType.Equal:
- opcode = OpCodes.Ceq;
- break;
- case ExpressionType.NotEqual:
- ig.Emit (OpCodes.Ceq);
- ig.Emit (OpCodes.Ldc_I4_0);
- opcode = OpCodes.Ceq;
- break;
- default:
- throw new InvalidOperationException (string.Format ("Internal error: BinaryExpression contains non-Binary nodetype {0}", NodeType));
- }
- ig.Emit (opcode);
- }
- void EmitLiftedArithmeticBinary (EmitContext ec)
- {
- EmitLiftedToNullBinary (ec);
- }
- void EmitLiftedToNullBinary (EmitContext ec)
- {
- var ig = ec.ig;
- var left = ec.EmitStored (this.left);
- var right = ec.EmitStored (this.right);
- var result = ig.DeclareLocal (Type);
- var has_value = ig.DefineLabel ();
- var done = ig.DefineLabel ();
- ec.EmitNullableHasValue (left);
- ec.EmitNullableHasValue (right);
- ig.Emit (OpCodes.And);
- ig.Emit (OpCodes.Brtrue, has_value);
- ig.Emit (OpCodes.Ldloca, result);
- ig.Emit (OpCodes.Initobj, result.LocalType);
- ig.Emit (OpCodes.Ldloc, result);
- ig.Emit (OpCodes.Br, done);
- ig.MarkLabel (has_value);
- ec.EmitNullableGetValueOrDefault (left);
- ec.EmitNullableGetValueOrDefault (right);
- EmitBinaryOperator (ec);
- ec.EmitNullableNew (result.LocalType);
- ig.MarkLabel (done);
- }
- void EmitLiftedRelationalBinary (EmitContext ec)
- {
- var ig = ec.ig;
- var left = ec.EmitStored (this.left);
- var right = ec.EmitStored (this.right);
- var ret = ig.DefineLabel ();
- var done = ig.DefineLabel ();
- ec.EmitNullableGetValueOrDefault (left);
- ec.EmitNullableGetValueOrDefault (right);
- switch (NodeType) {
- case ExpressionType.Equal:
- case ExpressionType.NotEqual:
- ig.Emit (OpCodes.Bne_Un, ret);
- break;
- default:
- EmitBinaryOperator (ec);
- ig.Emit (OpCodes.Brfalse, ret);
- break;
- }
- ec.EmitNullableHasValue (left);
- ec.EmitNullableHasValue (right);
- switch (NodeType) {
- case ExpressionType.Equal:
- ig.Emit (OpCodes.Ceq);
- break;
- case ExpressionType.NotEqual:
- ig.Emit (OpCodes.Ceq);
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Ceq);
- break;
- default:
- ig.Emit (OpCodes.And);
- break;
- }
- ig.Emit (OpCodes.Br, done);
- ig.MarkLabel (ret);
- ig.Emit (NodeType == ExpressionType.NotEqual ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
- ig.MarkLabel (done);
- }
- void EmitArithmeticBinary (EmitContext ec)
- {
- if (!IsLifted) {
- left.Emit (ec);
- right.Emit (ec);
- EmitBinaryOperator (ec);
- } else
- EmitLiftedArithmeticBinary (ec);
- }
- void EmitRelationalBinary (EmitContext ec)
- {
- if (!IsLifted) {
- left.Emit (ec);
- right.Emit (ec);
- EmitBinaryOperator (ec);
- } else if (IsLiftedToNull)
- EmitLiftedToNullBinary (ec);
- else
- EmitLiftedRelationalBinary (ec);
- }
- internal override void Emit (EmitContext ec)
- {
- if (method != null){
- EmitMethod (ec);
- return;
- }
- switch (NodeType){
- case ExpressionType.ArrayIndex:
- EmitArrayAccess (ec);
- return;
- case ExpressionType.And:
- EmitLogical (ec, true, false);
- return;
- case ExpressionType.Or:
- EmitLogical (ec, false, false);
- return;
- case ExpressionType.AndAlso:
- EmitLogical (ec, true, true);
- return;
- case ExpressionType.OrElse:
- EmitLogical (ec, false, true);
- return;
- case ExpressionType.Coalesce:
- EmitCoalesce (ec);
- return;
- case ExpressionType.Power:
- // likely broken if lifted
- left.Emit (ec);
- right.Emit (ec);
- ec.EmitCall (typeof (Math).GetMethod ("Pow"));
- return;
- case ExpressionType.Add:
- case ExpressionType.AddChecked:
- case ExpressionType.Divide:
- case ExpressionType.ExclusiveOr:
- case ExpressionType.LeftShift:
- case ExpressionType.Modulo:
- case ExpressionType.Multiply:
- case ExpressionType.MultiplyChecked:
- case ExpressionType.RightShift:
- case ExpressionType.Subtract:
- case ExpressionType.SubtractChecked:
- EmitArithmeticBinary (ec);
- return;
- case ExpressionType.Equal:
- case ExpressionType.GreaterThan:
- case ExpressionType.GreaterThanOrEqual:
- case ExpressionType.LessThan:
- case ExpressionType.LessThanOrEqual:
- case ExpressionType.NotEqual:
- EmitRelationalBinary (ec);
- return;
- default:
- throw new NotSupportedException (this.NodeType.ToString ());
- }
- }
- }
- }
|