예제 #1
0
        public override void Emit(EmitContext ec)
        {
            Label end_label = ec.DefineLabel();

            if (unwrap != null)
            {
                Label is_null_label = ec.DefineLabel();

                unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);

                //
                // When both expressions are nullable the unwrap
                // is needed only for null check not for value uwrap
                //
                if (type.IsNullableType && TypeSpecComparer.IsEqual(NullableInfo.GetUnderlyingType(type), unwrap.Type))
                {
                    unwrap.Load(ec);
                }
                else
                {
                    left.Emit(ec);
                }

                ec.Emit(OpCodes.Br, end_label);

                ec.MarkLabel(is_null_label);
                right.Emit(ec);

                ec.MarkLabel(end_label);
                return;
            }

            left.Emit(ec);
            ec.Emit(OpCodes.Dup);

            // Only to make verifier happy
            if (left.Type.IsGenericParameter)
            {
                ec.Emit(OpCodes.Box, left.Type);
            }

            ec.Emit(OpCodes.Brtrue, end_label);

            ec.Emit(OpCodes.Pop);
            right.Emit(ec);

            ec.MarkLabel(end_label);
        }
예제 #2
0
        public override void Emit(EmitContext ec)
        {
            Label is_null_label = ec.DefineLabel ();
            Label end_label = ec.DefineLabel ();

            unwrap.EmitCheck (ec);
            ec.Emit (OpCodes.Brfalse, is_null_label);

            expr.Emit (ec);

            ec.Emit (OpCodes.Br, end_label);
            ec.MarkLabel (is_null_label);

            null_value.Emit (ec);
            ec.MarkLabel (end_label);
        }
예제 #3
0
        public override void Emit(EmitContext ec)
        {
            Label is_null_label = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();

            unwrap.EmitCheck(ec);
            ec.Emit(OpCodes.Brfalse, is_null_label);

            expr.Emit(ec);

            ec.Emit(OpCodes.Br, end_label);
            ec.MarkLabel(is_null_label);

            null_value.Emit(ec);
            ec.MarkLabel(end_label);
        }
예제 #4
0
        protected override void DoEmit(EmitContext ec)
        {
            var falseLabel = ec.DefineLabel();
            var endLabel   = ec.DefineLabel();

            Condition.Emit(ec);
            ec.EmitCastToBoolean(Condition.GetEvaluatedCType(ec));
            ec.Emit(OpCode.BranchIfFalse, falseLabel);

            TrueValue.Emit(ec);
            ec.Emit(OpCode.Jump, endLabel);

            ec.EmitLabel(falseLabel);
            FalseValue.Emit(ec);

            ec.EmitLabel(endLabel);
        }
예제 #5
0
        protected override void DoEmit(EmitContext ec)
        {
            if (IsDo)
            {
                throw new NotImplementedException(GetType().Name + ": Do Emit");
            }
            else
            {
                var condLabel = ec.DefineLabel();
                var loopLabel = ec.DefineLabel();
                var endLabel  = ec.DefineLabel();

                ec.EmitLabel(condLabel);
                Condition.Emit(ec);
                ec.EmitCastToBoolean(Condition.GetEvaluatedCType(ec));
                ec.Emit(OpCode.BranchIfFalse, endLabel);
                ec.EmitLabel(loopLabel);
                Loop.Emit(ec);
                ec.Emit(OpCode.Jump, condLabel);
                ec.EmitLabel(endLabel);
            }
        }
예제 #6
0
        public override void Emit(EmitContext ec)
        {
            Label is_null_label = ec.DefineLabel ();
            Label end_label = ec.DefineLabel ();

            unwrap.EmitCheck (ec);
            ec.Emit (OpCodes.Brfalse, is_null_label);

            if (user_operator != null) {
                user_operator.Emit (ec);
            } else {
                EmitOperator (ec, NullableInfo.GetUnderlyingType (type));
            }

            ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
            ec.Emit (OpCodes.Br_S, end_label);

            ec.MarkLabel (is_null_label);
            LiftedNull.Create (type, loc).Emit (ec);

            ec.MarkLabel (end_label);
        }
예제 #7
0
        public override void Emit(EmitContext ec)
        {
            Label end_label = ec.DefineLabel();

            if (unwrap != null)
            {
                Label is_null_label = ec.DefineLabel();

                unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);

                left.Emit(ec);
                ec.Emit(OpCodes.Br, end_label);

                ec.MarkLabel(is_null_label);
                right.Emit(ec);

                ec.MarkLabel(end_label);
                return;
            }

            left.Emit(ec);
            ec.Emit(OpCodes.Dup);

            // Only to make verifier happy
            if (left.Type.IsGenericParameter)
            {
                ec.Emit(OpCodes.Box, left.Type);
            }

            ec.Emit(OpCodes.Brtrue, end_label);

            ec.Emit(OpCodes.Pop);
            right.Emit(ec);

            ec.MarkLabel(end_label);
        }
예제 #8
0
        protected override void DoEmit(EmitContext ec)
        {
            var endLabel = ec.DefineLabel();

            Condition.Emit(ec);
            ec.EmitCastToBoolean(Condition.GetEvaluatedCType(ec));

            if (FalseStatement == null)
            {
                ec.Emit(OpCode.BranchIfFalse, endLabel);
                TrueStatement.Emit(ec);
            }
            else
            {
                var falseLabel = ec.DefineLabel();
                ec.Emit(OpCode.BranchIfFalse, falseLabel);
                TrueStatement.Emit(ec);
                ec.Emit(OpCode.Jump, endLabel);
                ec.EmitLabel(falseLabel);
                FalseStatement.Emit(ec);
            }

            ec.EmitLabel(endLabel);
        }
예제 #9
0
        /// <summary>
        /// Emit code
        /// </summary>
        /// <returns>Success or fail</returns>
        public override bool Emit(EmitContext ec)
        {
            Label mlb = ec.DefineLabel(Method.Signature.ToString());

            mlb.Method = true;
            ec.MarkLabel(mlb);
            ec.EmitComment("Method: Name = " + Method.Name);
            // create stack frame
            ec.EmitComment("create stackframe");
            ec.EmitInstruction(new Push()
            {
                DestinationReg = EmitContext.BP, Size = 80
            });
            ec.EmitInstruction(new Mov()
            {
                DestinationReg = EmitContext.BP, SourceReg = EmitContext.SP, Size = 80
            });
            // allocate variables

            ushort         size   = 0;
            List <VarSpec> locals = ec.CurrentResolve.GetLocals();

            foreach (VarSpec v in locals)
            {
                size += 2;
            }



            if (size != 0)         // no allocation
            {
                ec.EmitInstruction(new Sub()
                {
                    DestinationReg = EmitContext.SP, SourceValue = size, Size = 80
                });
            }
            //EMit params
            var parameters = new List <ParameterSpec>();

            if (Parameters.Count > 0)
            {
                ec.EmitComment("Parameters Definitions");
                foreach (var par in Parameters)
                {
                    ec.EmitComment("Parameter " + par.Name + " @BP" + par.ParameterName.StackIdx);
                    parameters.Add(par.ParameterName);
                }
            }

            if (locals.Count > 0)
            {
                ec.EmitComment("Local Vars Definitions");
                foreach (VarSpec v in locals)
                {
                    ec.EmitComment("Local " + v.Name + " @BP" + v.VariableStackIndex);
                }
            }
            ec.EmitComment("Block");
            // Emit Code
            if (Block != null)
            {
                Block.Emit(ec);
            }

            ec.EmitComment("return label");
            // Return Label
            ec.MarkLabel(ec.DefineLabel(Method.Signature + "_ret"));

            // Destroy Stack Frame
            ec.EmitComment("destroy stackframe");
            ec.EmitInstruction(new Leave());
            // ret
            ccvh.EmitDecl(ec, ref parameters, CallingConventions.StdCall);
            return(true);
        }
예제 #10
0
		//
		// Emits optimized equality or inequality operator when possible
		//
		void EmitEquality (EmitContext ec)
		{
			//
			// Either left or right is null
			// 
			if (UnwrapLeft != null && Binary.Right.IsNull) { // TODO: Optimize for EmitBranchable
				//
				// left.HasValue == false 
				//
				UnwrapLeft.EmitCheck (ec);
				if (Binary.Oper == Binary.Operator.Equality) {
					ec.EmitInt (0);
					ec.Emit (OpCodes.Ceq);
				}
				return;
			}

			if (UnwrapRight != null && Binary.Left.IsNull) {
				//
				// right.HasValue == false 
				//
				UnwrapRight.EmitCheck (ec);
				if (Binary.Oper == Binary.Operator.Equality) {
					ec.EmitInt (0);
					ec.Emit (OpCodes.Ceq);
				}
				return;
			}

			Label dissimilar_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			if (UserOperator != null) {
				var left = Left;

				if (UnwrapLeft != null) {
					UnwrapLeft.EmitCheck (ec);
				} else {
					// Keep evaluation order same
					if (!(Left is VariableReference)) {
						Left.Emit (ec);
						var lt = new LocalTemporary (Left.Type);
						lt.Store (ec);
						left = lt;
					}
				}

				if (UnwrapRight != null) {
					UnwrapRight.EmitCheck (ec);

					if (UnwrapLeft != null) {
						ec.Emit (OpCodes.Bne_Un, dissimilar_label);

						Label compare_label = ec.DefineLabel ();
						UnwrapLeft.EmitCheck (ec);
						ec.Emit (OpCodes.Brtrue, compare_label);

						if (Binary.Oper == Binary.Operator.Equality)
							ec.EmitInt (1);
						else
							ec.EmitInt (0);

						ec.Emit (OpCodes.Br, end_label);

						ec.MarkLabel (compare_label);
					} else {
						ec.Emit (OpCodes.Brfalse, dissimilar_label);
					}
				} else {
					ec.Emit (OpCodes.Brfalse, dissimilar_label);
				}

				var args = new Arguments (2);
				args.Add (new Argument (left));
				args.Add (new Argument (Right));

				var call = new CallEmitter ();
				call.EmitPredefined (ec, UserOperator, args);
			} else {
				if (ec.HasSet (BuilderContext.Options.AsyncBody) && Binary.Right.ContainsEmitWithAwait ()) {
					Left = Left.EmitToField (ec);
					Right = Right.EmitToField (ec);
				}

				//
				// Emit underlying value comparison first.
				//
				// For this code: int? a = 1; bool b = a == 1;
				//
				// We emit something similar to this. Expressions with side effects have local
				// variable created by Unwrap expression
				//
				//	left.GetValueOrDefault ()
				//	right
				//	bne.un.s   dissimilar_label
				//  left.HasValue
				//	br.s       end_label
				// dissimilar_label:
				//	ldc.i4.0
				// end_label:
				//

				Left.Emit (ec);
				Right.Emit (ec);

				ec.Emit (OpCodes.Bne_Un_S, dissimilar_label);

				//
				// Check both left and right expressions for Unwrap call in which
				// case we need to run get_HasValue() check because the type is
				// nullable and could have null value
				//
				if (UnwrapLeft != null)
					UnwrapLeft.EmitCheck (ec);

				if (UnwrapRight != null)
					UnwrapRight.EmitCheck (ec);

				if (UnwrapLeft != null && UnwrapRight != null) {
					if (Binary.Oper == Binary.Operator.Inequality)
						ec.Emit (OpCodes.Xor);
					else
						ec.Emit (OpCodes.Ceq);
				} else {
					if (Binary.Oper == Binary.Operator.Inequality) {
						ec.EmitInt (0);
						ec.Emit (OpCodes.Ceq);
					}
				}
			}

			ec.Emit (OpCodes.Br_S, end_label);

			ec.MarkLabel (dissimilar_label);
			if (Binary.Oper == Binary.Operator.Inequality)
				ec.EmitInt (1);
			else
				ec.EmitInt (0);

			ec.MarkLabel (end_label);
		}
예제 #11
0
파일: nullable.cs 프로젝트: speier/shake
        public override void Emit(EmitContext ec)
        {
            Label end_label = ec.DefineLabel ();

            if (unwrap != null) {
                Label is_null_label = ec.DefineLabel ();

                unwrap.EmitCheck (ec);
                ec.Emit (OpCodes.Brfalse, is_null_label);

                left.Emit (ec);
                ec.Emit (OpCodes.Br, end_label);

                ec.MarkLabel (is_null_label);
                right.Emit (ec);

                ec.MarkLabel (end_label);
                return;
            }

            left.Emit (ec);

            ec.Emit (OpCodes.Dup);
            ec.Emit (OpCodes.Brtrue, end_label);

            ec.Emit (OpCodes.Pop);
            right.Emit (ec);

            ec.MarkLabel (end_label);
        }
예제 #12
0
파일: nullable.cs 프로젝트: saga/mono
		//
		// Emits optimized equality or inequality operator when possible
		//
		void EmitEquality (EmitContext ec)
		{
			//
			// Either left or right is null
			//
			if (left_unwrap != null && (IsRightNullLifted || right.IsNull)) {
				left_unwrap.EmitCheck (ec);
				if (Oper == Binary.Operator.Equality) {
					ec.Emit (OpCodes.Ldc_I4_0);
					ec.Emit (OpCodes.Ceq);
				}
				return;
			}

			if (right_unwrap != null && (IsLeftNullLifted || left.IsNull)) {
				right_unwrap.EmitCheck (ec);
				if (Oper == Binary.Operator.Equality) {
					ec.Emit (OpCodes.Ldc_I4_0);
					ec.Emit (OpCodes.Ceq);
				}
				return;
			}

			Label dissimilar_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			if (user_operator != null) {
				user_operator.Emit (ec);
				ec.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
			} else {
				left.Emit (ec);
				right.Emit (ec);

				ec.Emit (OpCodes.Bne_Un_S, dissimilar_label);
			}

			if (left_unwrap != null)
				left_unwrap.EmitCheck (ec);

			if (right_unwrap != null)
				right_unwrap.EmitCheck (ec);

			if (left_unwrap != null && right_unwrap != null) {
				if (Oper == Operator.Inequality)
					ec.Emit (OpCodes.Xor);
				else
					ec.Emit (OpCodes.Ceq);
			} else {
				if (Oper == Operator.Inequality) {
					ec.Emit (OpCodes.Ldc_I4_0);
					ec.Emit (OpCodes.Ceq);
				}
			}

			ec.Emit (OpCodes.Br_S, end_label);

			ec.MarkLabel (dissimilar_label);
			if (Oper == Operator.Inequality)
				ec.Emit (OpCodes.Ldc_I4_1);
			else
				ec.Emit (OpCodes.Ldc_I4_0);

			ec.MarkLabel (end_label);
		}
예제 #13
0
파일: nullable.cs 프로젝트: saga/mono
		public override void Emit (EmitContext ec)
		{
			Label is_null_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			unwrap.EmitCheck (ec);
			ec.Emit (OpCodes.Brfalse, is_null_label);

			if (user_operator != null) {
				user_operator.Emit (ec);
			} else {
				EmitOperator (ec, NullableInfo.GetUnderlyingType (type));
			}

			ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
			ec.Emit (OpCodes.Br_S, end_label);

			ec.MarkLabel (is_null_label);
			LiftedNull.Create (type, loc).Emit (ec);

			ec.MarkLabel (end_label);
		}
예제 #14
0
파일: nullable.cs 프로젝트: saga/mono
		public override void Emit (EmitContext ec)
		{
			Label end_label = ec.DefineLabel ();

			if (unwrap != null) {
				Label is_null_label = ec.DefineLabel ();

				unwrap.EmitCheck (ec);
				ec.Emit (OpCodes.Brfalse, is_null_label);

				left.Emit (ec);
				ec.Emit (OpCodes.Br, end_label);

				ec.MarkLabel (is_null_label);
				right.Emit (ec);

				ec.MarkLabel (end_label);
				return;
			}

			left.Emit (ec);
			ec.Emit (OpCodes.Dup);

			// Only to make verifier happy
			if (left.Type.IsGenericParameter)
				ec.Emit (OpCodes.Box, left.Type);

			ec.Emit (OpCodes.Brtrue, end_label);

			ec.Emit (OpCodes.Pop);
			right.Emit (ec);

			ec.MarkLabel (end_label);
		}
예제 #15
0
        public override void Emit(EmitContext ec)
        {
            if (IsBitwiseBoolean && UserOperator == null)
            {
                EmitBitwiseBoolean(ec);
                return;
            }

            if ((Binary.Oper & Binary.Operator.EqualityMask) != 0)
            {
                EmitEquality(ec);
                return;
            }

            Label is_null_label = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();

            if (ec.HasSet(BuilderContext.Options.AsyncBody) && Right.ContainsEmitWithAwait())
            {
                Left  = Left.EmitToField(ec);
                Right = Right.EmitToField(ec);
            }

            if (UnwrapLeft != null)
            {
                UnwrapLeft.EmitCheck(ec);
            }

            //
            // Don't emit HasValue check when left and right expressions are same
            //
            if (UnwrapRight != null && !Binary.Left.Equals(Binary.Right))
            {
                UnwrapRight.EmitCheck(ec);
                if (UnwrapLeft != null)
                {
                    ec.Emit(OpCodes.And);
                }
            }

            ec.Emit(OpCodes.Brfalse, is_null_label);

            if (UserOperator != null)
            {
                var args = new Arguments(2);
                args.Add(new Argument(Left));
                args.Add(new Argument(Right));

                var call = new CallEmitter();
                call.EmitPredefined(ec, UserOperator, args);
            }
            else
            {
                Binary.EmitOperator(ec, Left, Right);
            }

            //
            // Wrap the result when the operator return type is nullable type
            //
            if (type.IsNullableType)
            {
                ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
            }

            ec.Emit(OpCodes.Br_S, end_label);
            ec.MarkLabel(is_null_label);

            if ((Binary.Oper & Binary.Operator.ComparisonMask) != 0)
            {
                ec.EmitInt(0);
            }
            else
            {
                LiftedNull.Create(type, loc).Emit(ec);
            }

            ec.MarkLabel(end_label);
        }
예제 #16
0
        public override void Emit(EmitContext ec)
        {
            //
            // Optimize same expression operation
            //
            if (right_unwrap != null && right.Equals(left))
            {
                right_unwrap = left_unwrap;
            }

            if (user_operator == null && IsBitwiseBoolean)
            {
                EmitBitwiseBoolean(ec);
                return;
            }

            if ((Oper & Operator.EqualityMask) != 0)
            {
                EmitEquality(ec);
                return;
            }

            Label is_null_label = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();

            if (left_unwrap != null)
            {
                left_unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);
            }

            //
            // Don't emit HasValue check when left and right expressions are same
            //
            if (right_unwrap != null && !left.Equals(right))
            {
                right_unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);
            }

            EmitOperator(ec, left.Type);

            if (wrap_ctor != null)
            {
                ec.Emit(OpCodes.Newobj, wrap_ctor);
            }

            ec.Emit(OpCodes.Br_S, end_label);
            ec.MarkLabel(is_null_label);

            if ((Oper & Operator.ComparisonMask) != 0)
            {
                ec.Emit(OpCodes.Ldc_I4_0);
            }
            else
            {
                LiftedNull.Create(type, loc).Emit(ec);
            }

            ec.MarkLabel(end_label);
        }
예제 #17
0
        //
        // Emits optimized equality or inequality operator when possible
        //
        void EmitEquality(EmitContext ec)
        {
            //
            // Either left or right is null
            //
            if (left_unwrap != null && (IsRightNullLifted || right.IsNull))
            {
                left_unwrap.EmitCheck(ec);
                if (Oper == Binary.Operator.Equality)
                {
                    ec.Emit(OpCodes.Ldc_I4_0);
                    ec.Emit(OpCodes.Ceq);
                }
                return;
            }

            if (right_unwrap != null && (IsLeftNullLifted || left.IsNull))
            {
                right_unwrap.EmitCheck(ec);
                if (Oper == Binary.Operator.Equality)
                {
                    ec.Emit(OpCodes.Ldc_I4_0);
                    ec.Emit(OpCodes.Ceq);
                }
                return;
            }

            Label dissimilar_label = ec.DefineLabel();
            Label end_label        = ec.DefineLabel();

            if (user_operator != null)
            {
                user_operator.Emit(ec);
                ec.Emit(Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
            }
            else
            {
                left.Emit(ec);
                right.Emit(ec);

                ec.Emit(OpCodes.Bne_Un_S, dissimilar_label);
            }

            if (left_unwrap != null)
            {
                left_unwrap.EmitCheck(ec);
            }

            if (right_unwrap != null)
            {
                right_unwrap.EmitCheck(ec);
            }

            if (left_unwrap != null && right_unwrap != null)
            {
                if (Oper == Operator.Inequality)
                {
                    ec.Emit(OpCodes.Xor);
                }
                else
                {
                    ec.Emit(OpCodes.Ceq);
                }
            }
            else
            {
                if (Oper == Operator.Inequality)
                {
                    ec.Emit(OpCodes.Ldc_I4_0);
                    ec.Emit(OpCodes.Ceq);
                }
            }

            ec.Emit(OpCodes.Br_S, end_label);

            ec.MarkLabel(dissimilar_label);
            if (Oper == Operator.Inequality)
            {
                ec.Emit(OpCodes.Ldc_I4_1);
            }
            else
            {
                ec.Emit(OpCodes.Ldc_I4_0);
            }

            ec.MarkLabel(end_label);
        }
예제 #18
0
        void EmitBitwiseBoolean(EmitContext ec)
        {
            Label load_left  = ec.DefineLabel();
            Label load_right = ec.DefineLabel();
            Label end_label  = ec.DefineLabel();

            // null & value, null | value
            if (left_unwrap == null)
            {
                left_unwrap  = right_unwrap;
                right_unwrap = null;
                right        = left;
            }

            left_unwrap.Emit(ec);
            ec.Emit(OpCodes.Brtrue_S, load_right);

            // value & null, value | null
            if (right_unwrap != null)
            {
                right_unwrap.Emit(ec);
                ec.Emit(OpCodes.Brtrue_S, load_left);
            }

            left_unwrap.EmitCheck(ec);
            ec.Emit(OpCodes.Brfalse_S, load_right);

            // load left
            ec.MarkLabel(load_left);

            if (Oper == Operator.BitwiseAnd)
            {
                left_unwrap.Load(ec);
            }
            else
            {
                if (right_unwrap == null)
                {
                    right.Emit(ec);
                    if (right is EmptyConstantCast || right is EmptyCast)
                    {
                        ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
                    }
                }
                else
                {
                    right_unwrap.Load(ec);
                    right_unwrap = left_unwrap;
                }
            }
            ec.Emit(OpCodes.Br_S, end_label);

            // load right
            ec.MarkLabel(load_right);
            if (right_unwrap == null)
            {
                if (Oper == Operator.BitwiseAnd)
                {
                    right.Emit(ec);
                    if (right is EmptyConstantCast || right is EmptyCast)
                    {
                        ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
                    }
                }
                else
                {
                    left_unwrap.Load(ec);
                }
            }
            else
            {
                right_unwrap.Load(ec);
            }

            ec.MarkLabel(end_label);
        }
예제 #19
0
파일: nullable.cs 프로젝트: Profit0004/mono
		public override void Emit (EmitContext ec)
		{
			Label end_label = ec.DefineLabel ();

			if (unwrap != null) {
				Label is_null_label = ec.DefineLabel ();

				unwrap.EmitCheck (ec);
				ec.Emit (OpCodes.Brfalse, is_null_label);

				//
				// When both expressions are nullable the unwrap
				// is needed only for null check not for value uwrap
				//
				if (type.IsNullableType && TypeSpecComparer.IsEqual (NullableInfo.GetUnderlyingType (type), unwrap.Type))
					unwrap.Load (ec);
				else
					left.Emit (ec);

				ec.Emit (OpCodes.Br, end_label);

				ec.MarkLabel (is_null_label);
				right.Emit (ec);

				ec.MarkLabel (end_label);
				return;
			}

			//
			// Null check is done on original expression not after expression is converted to
			// result type. This is in most cases same but when user conversion is involved
			// we can end up in situation when user operator does the null handling which is
			// not what the operator is supposed to do.
			// There is tricky case where cast of left expression is meant to be cast of
			// whole source expression (null check is done on it) and cast from right-to-left
			// conversion needs to do null check on unconverted source expression.
			//
			if (user_conversion_left) {
				var op_expr = (UserCast) left;

				op_expr.Source.Emit (ec);
				LocalTemporary temp;

				// TODO: More load kinds can be special cased
				if (!(op_expr.Source is VariableReference)) {
					temp = new LocalTemporary (op_expr.Source.Type);
					temp.Store (ec);
					temp.Emit (ec);
					op_expr.Source = temp;
				} else {
					temp = null;
				}

				var right_label = ec.DefineLabel ();
				ec.Emit (OpCodes.Brfalse_S, right_label);
				left.Emit (ec);
				ec.Emit (OpCodes.Br, end_label);
				ec.MarkLabel (right_label);

				if (temp != null)
					temp.Release (ec);
			} else {
				//
				// Common case where expression is not modified before null check and
				// we generate better/smaller code
				//
				left.Emit (ec);
				ec.Emit (OpCodes.Dup);

				// Only to make verifier happy
				if (left.Type.IsGenericParameter)
					ec.Emit (OpCodes.Box, left.Type);

				ec.Emit (OpCodes.Brtrue, end_label);

				ec.Emit (OpCodes.Pop);
			}

			right.Emit (ec);

			ec.MarkLabel (end_label);
		}
예제 #20
0
파일: nullable.cs 프로젝트: raj581/Marvin
        //
        // Emits optimized equality or inequality operator when possible
        //
        void EmitEquality(EmitContext ec)
        {
            //
            // Either left or right is null
            //
            if (left_unwrap != null && (IsRightNullLifted || right.IsNull))
            {
                left_unwrap.EmitCheck(ec);
                if (Oper == Binary.Operator.Equality)
                {
                    ec.EmitInt(0);
                    ec.Emit(OpCodes.Ceq);
                }
                return;
            }

            if (right_unwrap != null && (IsLeftNullLifted || left.IsNull))
            {
                right_unwrap.EmitCheck(ec);
                if (Oper == Binary.Operator.Equality)
                {
                    ec.EmitInt(0);
                    ec.Emit(OpCodes.Ceq);
                }
                return;
            }

            Label dissimilar_label = ec.DefineLabel();
            Label end_label        = ec.DefineLabel();

            if (user_operator != null)
            {
                user_operator.Emit(ec);
                ec.Emit(Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
            }
            else
            {
                if (ec.HasSet(BuilderContext.Options.AsyncBody) && right.ContainsEmitWithAwait())
                {
                    left  = left.EmitToField(ec);
                    right = right.EmitToField(ec);
                }

                left.Emit(ec);
                right.Emit(ec);

                ec.Emit(OpCodes.Bne_Un_S, dissimilar_label);
            }

            if (left_unwrap != null)
            {
                left_unwrap.EmitCheck(ec);
            }

            if (right_unwrap != null)
            {
                right_unwrap.EmitCheck(ec);
            }

            if (left_unwrap != null && right_unwrap != null)
            {
                if (Oper == Operator.Inequality)
                {
                    ec.Emit(OpCodes.Xor);
                }
                else
                {
                    ec.Emit(OpCodes.Ceq);
                }
            }
            else
            {
                if (Oper == Operator.Inequality)
                {
                    ec.EmitInt(0);
                    ec.Emit(OpCodes.Ceq);
                }
            }

            ec.Emit(OpCodes.Br_S, end_label);

            ec.MarkLabel(dissimilar_label);
            if (Oper == Operator.Inequality)
            {
                ec.EmitInt(1);
            }
            else
            {
                ec.EmitInt(0);
            }

            ec.MarkLabel(end_label);
        }
예제 #21
0
		void EmitBitwiseBoolean (EmitContext ec)
		{
			Label load_left = ec.DefineLabel ();
			Label load_right = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			left_unwrap.Emit (ec);
			ec.Emit (OpCodes.Brtrue_S, load_right);

			right_unwrap.Emit (ec);
			ec.Emit (OpCodes.Brtrue_S, load_left);

			left_unwrap.EmitCheck (ec);
			ec.Emit (OpCodes.Brfalse_S, load_right);

			// load left
			ec.MarkLabel (load_left);

			if (Oper == Operator.BitwiseAnd) {
				left_unwrap.Load (ec);
			} else {
				right_unwrap.Load (ec);
				right_unwrap = left_unwrap;
			}
			ec.Emit (OpCodes.Br_S, end_label);

			// load right
			ec.MarkLabel (load_right);
			right_unwrap.Load (ec);

			ec.MarkLabel (end_label);
		}
예제 #22
0
        //
        // Emits optimized equality or inequality operator when possible
        //
        void EmitEquality(EmitContext ec)
        {
            //
            // Either left or right is null
            //
            if (UnwrapLeft != null && Binary.Right.IsNull)               // TODO: Optimize for EmitBranchable
            //
            // left.HasValue == false
            //
            {
                UnwrapLeft.EmitCheck(ec);
                if (Binary.Oper == Binary.Operator.Equality)
                {
                    ec.EmitInt(0);
                    ec.Emit(OpCodes.Ceq);
                }
                return;
            }

            if (UnwrapRight != null && Binary.Left.IsNull)
            {
                //
                // right.HasValue == false
                //
                UnwrapRight.EmitCheck(ec);
                if (Binary.Oper == Binary.Operator.Equality)
                {
                    ec.EmitInt(0);
                    ec.Emit(OpCodes.Ceq);
                }
                return;
            }

            Label dissimilar_label = ec.DefineLabel();
            Label end_label        = ec.DefineLabel();

            if (UserOperator != null)
            {
                var left = Left;

                if (UnwrapLeft != null)
                {
                    UnwrapLeft.EmitCheck(ec);
                }
                else
                {
                    // Keep evaluation order same
                    if (!(Left is VariableReference))
                    {
                        Left.Emit(ec);
                        var lt = new LocalTemporary(Left.Type);
                        lt.Store(ec);
                        left = lt;
                    }
                }

                if (UnwrapRight != null)
                {
                    UnwrapRight.EmitCheck(ec);

                    if (UnwrapLeft != null)
                    {
                        ec.Emit(OpCodes.Bne_Un, dissimilar_label);

                        Label compare_label = ec.DefineLabel();
                        UnwrapLeft.EmitCheck(ec);
                        ec.Emit(OpCodes.Brtrue, compare_label);

                        if (Binary.Oper == Binary.Operator.Equality)
                        {
                            ec.EmitInt(1);
                        }
                        else
                        {
                            ec.EmitInt(0);
                        }

                        ec.Emit(OpCodes.Br, end_label);

                        ec.MarkLabel(compare_label);
                    }
                    else
                    {
                        ec.Emit(OpCodes.Brfalse, dissimilar_label);
                    }
                }
                else
                {
                    ec.Emit(OpCodes.Brfalse, dissimilar_label);
                }

                var args = new Arguments(2);
                args.Add(new Argument(left));
                args.Add(new Argument(Right));

                var call = new CallEmitter();
                call.EmitPredefined(ec, UserOperator, args);
            }
            else
            {
                if (ec.HasSet(BuilderContext.Options.AsyncBody) && Binary.Right.ContainsEmitWithAwait())
                {
                    Left  = Left.EmitToField(ec);
                    Right = Right.EmitToField(ec);
                }

                //
                // Emit underlying value comparison first.
                //
                // For this code: int? a = 1; bool b = a == 1;
                //
                // We emit something similar to this. Expressions with side effects have local
                // variable created by Unwrap expression
                //
                //	left.GetValueOrDefault ()
                //	right
                //	bne.un.s   dissimilar_label
                //  left.HasValue
                //	br.s       end_label
                // dissimilar_label:
                //	ldc.i4.0
                // end_label:
                //

                Left.Emit(ec);
                Right.Emit(ec);

                ec.Emit(OpCodes.Bne_Un_S, dissimilar_label);

                //
                // Check both left and right expressions for Unwrap call in which
                // case we need to run get_HasValue() check because the type is
                // nullable and could have null value
                //
                if (UnwrapLeft != null)
                {
                    UnwrapLeft.EmitCheck(ec);
                }

                if (UnwrapRight != null)
                {
                    UnwrapRight.EmitCheck(ec);
                }

                if (UnwrapLeft != null && UnwrapRight != null)
                {
                    if (Binary.Oper == Binary.Operator.Inequality)
                    {
                        ec.Emit(OpCodes.Xor);
                    }
                    else
                    {
                        ec.Emit(OpCodes.Ceq);
                    }
                }
                else
                {
                    if (Binary.Oper == Binary.Operator.Inequality)
                    {
                        ec.EmitInt(0);
                        ec.Emit(OpCodes.Ceq);
                    }
                }
            }

            ec.Emit(OpCodes.Br_S, end_label);

            ec.MarkLabel(dissimilar_label);
            if (Binary.Oper == Binary.Operator.Inequality)
            {
                ec.EmitInt(1);
            }
            else
            {
                ec.EmitInt(0);
            }

            ec.MarkLabel(end_label);
        }
예제 #23
0
        public override void Emit(EmitContext ec)
        {
            Label end_label = ec.DefineLabel();

            if (unwrap != null)
            {
                Label is_null_label = ec.DefineLabel();

                unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);

                //
                // When both expressions are nullable the unwrap
                // is needed only for null check not for value uwrap
                //
                if (type.IsNullableType && TypeSpecComparer.IsEqual(NullableInfo.GetUnderlyingType(type), unwrap.Type))
                {
                    unwrap.Load(ec);
                }
                else
                {
                    left.Emit(ec);
                }

                ec.Emit(OpCodes.Br, end_label);

                ec.MarkLabel(is_null_label);
                right.Emit(ec);

                ec.MarkLabel(end_label);
                return;
            }

            //
            // Null check is done on original expression not after expression is converted to
            // result type. This is in most cases same but when user conversion is involved
            // we can end up in situation when use operator does the null handling which is
            // not what the operator is supposed to do
            //
            var op_expr = left as UserCast;

            if (op_expr != null)
            {
                op_expr.Source.Emit(ec);
                LocalTemporary temp;

                // TODO: More load kinds can be special cased
                if (!(op_expr.Source is VariableReference))
                {
                    temp = new LocalTemporary(op_expr.Source.Type);
                    temp.Store(ec);
                    temp.Emit(ec);
                    op_expr.Source = temp;
                }
                else
                {
                    temp = null;
                }

                var right_label = ec.DefineLabel();
                ec.Emit(OpCodes.Brfalse_S, right_label);
                left.Emit(ec);
                ec.Emit(OpCodes.Br, end_label);
                ec.MarkLabel(right_label);

                if (temp != null)
                {
                    temp.Release(ec);
                }
            }
            else
            {
                //
                // Common case where expression is not modified before null check and
                // we generate better/smaller code
                //
                left.Emit(ec);
                ec.Emit(OpCodes.Dup);

                // Only to make verifier happy
                if (left.Type.IsGenericParameter)
                {
                    ec.Emit(OpCodes.Box, left.Type);
                }

                ec.Emit(OpCodes.Brtrue, end_label);

                ec.Emit(OpCodes.Pop);
            }

            right.Emit(ec);

            ec.MarkLabel(end_label);
        }
예제 #24
0
파일: nullable.cs 프로젝트: bl8/mono
		public override void Emit (EmitContext ec)
		{
			Label end_label = ec.DefineLabel ();

			if (unwrap != null) {
				Label is_null_label = ec.DefineLabel ();

				unwrap.EmitCheck (ec);
				ec.Emit (OpCodes.Brfalse, is_null_label);

				//
				// When both expressions are nullable the unwrap
				// is needed only for null check not for value uwrap
				//
				if (type.IsNullableType)
					unwrap.Load (ec);
				else
					left.Emit (ec);

				ec.Emit (OpCodes.Br, end_label);

				ec.MarkLabel (is_null_label);
				right.Emit (ec);

				ec.MarkLabel (end_label);
				return;
			}

			left.Emit (ec);
			ec.Emit (OpCodes.Dup);

			// Only to make verifier happy
			if (left.Type.IsGenericParameter)
				ec.Emit (OpCodes.Box, left.Type);

			ec.Emit (OpCodes.Brtrue, end_label);

			ec.Emit (OpCodes.Pop);
			right.Emit (ec);

			ec.MarkLabel (end_label);
		}
예제 #25
0
        void EmitBitwiseBoolean(EmitContext ec)
        {
            Label load_left     = ec.DefineLabel();
            Label load_right    = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();
            Label is_null_label = ec.DefineLabel();

            bool or = Binary.Oper == Binary.Operator.BitwiseOr;

            //
            // Both operands are bool? types
            //
            if (UnwrapLeft != null && UnwrapRight != null)
            {
                if (ec.HasSet(BuilderContext.Options.AsyncBody) && Binary.Right.ContainsEmitWithAwait())
                {
                    Left  = Left.EmitToField(ec);
                    Right = Right.EmitToField(ec);
                }
                else
                {
                    UnwrapLeft.Store(ec);
                    UnwrapRight.Store(ec);
                }

                Left.Emit(ec);
                ec.Emit(OpCodes.Brtrue_S, load_right);

                Right.Emit(ec);
                ec.Emit(OpCodes.Brtrue_S, load_left);

                UnwrapLeft.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse_S, load_right);

                // load left
                ec.MarkLabel(load_left);
                if (or)
                {
                    UnwrapRight.Load(ec);
                }
                else
                {
                    UnwrapLeft.Load(ec);
                }

                ec.Emit(OpCodes.Br_S, end_label);

                // load right
                ec.MarkLabel(load_right);
                if (or)
                {
                    UnwrapLeft.Load(ec);
                }
                else
                {
                    UnwrapRight.Load(ec);
                }

                ec.MarkLabel(end_label);
                return;
            }

            //
            // Faster version when one operand is bool
            //
            if (UnwrapLeft == null)
            {
                //
                // (bool, bool?)
                //
                // Optimizes remaining (false & bool?), (true | bool?) which are not easy to handle
                // in binary expression reduction
                //
                var c = Left as BoolConstant;
                if (c != null)
                {
                    // Keep evaluation order
                    UnwrapRight.Store(ec);

                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
                }
                else if (Left.IsNull)
                {
                    UnwrapRight.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, is_null_label);

                    UnwrapRight.Load(ec);
                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(is_null_label);
                    LiftedNull.Create(type, loc).Emit(ec);
                }
                else
                {
                    Left.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, load_right);

                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));

                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(load_right);
                    UnwrapRight.Original.Emit(ec);
                }
            }
            else
            {
                //
                // (bool?, bool)
                //
                // Keep left-right evaluation order
                UnwrapLeft.Store(ec);

                //
                // Optimizes remaining (bool? & false), (bool? | true) which are not easy to handle
                // in binary expression reduction
                //
                var c = Right as BoolConstant;
                if (c != null)
                {
                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
                }
                else if (Right.IsNull)
                {
                    UnwrapLeft.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, is_null_label);

                    UnwrapLeft.Load(ec);
                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(is_null_label);
                    LiftedNull.Create(type, loc).Emit(ec);
                }
                else
                {
                    Right.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, load_right);

                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));

                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(load_right);

                    UnwrapLeft.Load(ec);
                }
            }

            ec.MarkLabel(end_label);
        }
예제 #26
0
		protected override void EmitOperation (EmitContext ec)
		{
			Label is_null_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			LocalTemporary lt = new LocalTemporary (type);

			// Value is on the stack
			lt.Store (ec);

			var call = new CallEmitter ();
			call.InstanceExpression = lt;
			call.EmitPredefined (ec, NullableInfo.GetHasValue (expr.Type), null);

			ec.Emit (OpCodes.Brfalse, is_null_label);

			call = new CallEmitter ();
			call.InstanceExpression = lt;
			call.EmitPredefined (ec, NullableInfo.GetGetValueOrDefault (expr.Type), null);

			lt.Release (ec);

			base.EmitOperation (ec);

			ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
			ec.Emit (OpCodes.Br_S, end_label);

			ec.MarkLabel (is_null_label);
			LiftedNull.Create (type, loc).Emit (ec);

			ec.MarkLabel (end_label);
		}
예제 #27
0
파일: nullable.cs 프로젝트: saga/mono
		void DoEmit (EmitContext ec, bool is_expr)
		{
			Label is_null_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			unwrap.EmitCheck (ec);
			ec.Emit (OpCodes.Brfalse, is_null_label);

			if (is_expr) {
				underlying.Emit (ec);
				ec.Emit (OpCodes.Br_S, end_label);
			} else {
				underlying.EmitStatement (ec);
			}

			ec.MarkLabel (is_null_label);
			if (is_expr)
				LiftedNull.Create (type, loc).Emit (ec);

			ec.MarkLabel (end_label);
		}
예제 #28
0
		public override void Emit (EmitContext ec)
		{
			if (IsBitwiseBoolean && UserOperator == null) {
				EmitBitwiseBoolean (ec);
				return;
			}

			if ((Binary.Oper & Binary.Operator.EqualityMask) != 0) {
				EmitEquality (ec);
				return;
			}

			Label is_null_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			if (ec.HasSet (BuilderContext.Options.AsyncBody) && Right.ContainsEmitWithAwait ()) {
				Left = Left.EmitToField (ec);
				Right = Right.EmitToField (ec);
			}

			if (UnwrapLeft != null) {
				UnwrapLeft.EmitCheck (ec);
			}

			//
			// Don't emit HasValue check when left and right expressions are same
			//
			if (UnwrapRight != null && !Binary.Left.Equals (Binary.Right)) {
				UnwrapRight.EmitCheck (ec);
				if (UnwrapLeft != null) {
					ec.Emit (OpCodes.And);
				}
			}

			ec.Emit (OpCodes.Brfalse, is_null_label);

			if (UserOperator != null) {
				var args = new Arguments (2);
				args.Add (new Argument (Left));
				args.Add (new Argument (Right));

				var call = new CallEmitter ();
				call.EmitPredefined (ec, UserOperator, args);
			} else {
				Binary.EmitOperator (ec, Left, Right);
			}

			//
			// Wrap the result when the operator return type is nullable type
			//
			if (type.IsNullableType)
				ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));

			ec.Emit (OpCodes.Br_S, end_label);
			ec.MarkLabel (is_null_label);

			if ((Binary.Oper & Binary.Operator.ComparisonMask) != 0) {
				ec.EmitInt (0);
			} else {
				LiftedNull.Create (type, loc).Emit (ec);
			}

			ec.MarkLabel (end_label);
		}
예제 #29
0
파일: nullable.cs 프로젝트: saga/mono
		void EmitBitwiseBoolean (EmitContext ec)
		{
			Label load_left = ec.DefineLabel ();
			Label load_right = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			// null & value, null | value
			if (left_unwrap == null) {
				left_unwrap = right_unwrap;
				right_unwrap = null;
				right = left;
			}

			left_unwrap.Emit (ec);
			ec.Emit (OpCodes.Brtrue_S, load_right);

			// value & null, value | null
			if (right_unwrap != null) {
				right_unwrap.Emit (ec);
				ec.Emit (OpCodes.Brtrue_S, load_left);
			}

			left_unwrap.EmitCheck (ec);
			ec.Emit (OpCodes.Brfalse_S, load_right);

			// load left
			ec.MarkLabel (load_left);

			if (Oper == Operator.BitwiseAnd) {
				left_unwrap.Load (ec);
			} else {
				if (right_unwrap == null) {
					right.Emit (ec);
					if (right is EmptyConstantCast || right is EmptyCast)
						ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
				} else {
					right_unwrap.Load (ec);
					right_unwrap = left_unwrap;
				}
			}
			ec.Emit (OpCodes.Br_S, end_label);

			// load right
			ec.MarkLabel (load_right);
			if (right_unwrap == null) {
				if (Oper == Operator.BitwiseAnd) {
					right.Emit (ec);
					if (right is EmptyConstantCast || right is EmptyCast)
						ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
				} else {
					left_unwrap.Load (ec);
				}
			} else {
				right_unwrap.Load (ec);
			}

			ec.MarkLabel (end_label);
		}
예제 #30
0
		void EmitBitwiseBoolean (EmitContext ec)
		{
			Label load_left = ec.DefineLabel ();
			Label load_right = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();
			Label is_null_label = ec.DefineLabel ();

			bool or = Binary.Oper == Binary.Operator.BitwiseOr;

			//
			// Both operands are bool? types
			//
			if (UnwrapLeft != null && UnwrapRight != null) {
				if (ec.HasSet (BuilderContext.Options.AsyncBody) && Binary.Right.ContainsEmitWithAwait ()) {
					Left = Left.EmitToField (ec);
					Right = Right.EmitToField (ec);
				}

				Left.Emit (ec);
				ec.Emit (OpCodes.Brtrue_S, load_right);

				Right.Emit (ec);
				ec.Emit (OpCodes.Brtrue_S, load_left);

				UnwrapLeft.EmitCheck (ec);
				ec.Emit (OpCodes.Brfalse_S, load_right);

				// load left
				ec.MarkLabel (load_left);
				if (or)
					UnwrapRight.Load (ec);
				else
					UnwrapLeft.Load (ec);

				ec.Emit (OpCodes.Br_S, end_label);

				// load right
				ec.MarkLabel (load_right);
				if (or)
					UnwrapLeft.Load (ec);
				else
					UnwrapRight.Load (ec);

				ec.MarkLabel (end_label);
				return;
			}

			//
			// Faster version when one operand is bool
			//
			if (UnwrapLeft == null) {
				//
				// (bool, bool?)
				//
				// Optimizes remaining (false & bool?), (true | bool?) which are not easy to handle
				// in binary expression reduction
				//
				var c = Left as BoolConstant;
				if (c != null) {
					// Keep evaluation order
					UnwrapRight.Store (ec);

					ec.EmitInt (or ? 1 : 0);
					ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
				} else if (Left.IsNull) {
					UnwrapRight.Emit (ec);
					ec.Emit (or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, is_null_label);

					UnwrapRight.Load (ec);
					ec.Emit (OpCodes.Br_S, end_label);

					ec.MarkLabel (is_null_label);
					LiftedNull.Create (type, loc).Emit (ec);
				} else {
					Left.Emit (ec);
					ec.Emit (or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, load_right);

					ec.EmitInt (or ? 1 : 0);
					ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));

					ec.Emit (OpCodes.Br_S, end_label);

					ec.MarkLabel (load_right);
					UnwrapRight.Original.Emit (ec);
				}
			} else {
				//
				// (bool?, bool)
				//
				// Keep left-right evaluation order
				UnwrapLeft.Store (ec);

				//
				// Optimizes remaining (bool? & false), (bool? | true) which are not easy to handle
				// in binary expression reduction
				//
				var c = Right as BoolConstant;
				if (c != null) {
					ec.EmitInt (or ? 1 : 0);
					ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
				} else if (Right.IsNull) {
					UnwrapLeft.Emit (ec);
					ec.Emit (or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, is_null_label);

					UnwrapLeft.Load (ec);
					ec.Emit (OpCodes.Br_S, end_label);

					ec.MarkLabel (is_null_label);
					LiftedNull.Create (type, loc).Emit (ec);
				} else {
					Right.Emit (ec);
					ec.Emit (or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, load_right);

					ec.EmitInt (or ? 1 : 0);
					ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));

					ec.Emit (OpCodes.Br_S, end_label);

					ec.MarkLabel (load_right);

					UnwrapLeft.Load (ec);
				}
			}

			ec.MarkLabel (end_label);
 		}
예제 #31
0
파일: nullable.cs 프로젝트: saga/mono
		public override void Emit (EmitContext ec)
		{
			//
			// Optimize same expression operation
			//
			if (right_unwrap != null && right.Equals (left))
				right_unwrap = left_unwrap;

			if (user_operator == null && IsBitwiseBoolean) {
				EmitBitwiseBoolean (ec);
				return;
			}

			if ((Oper & Operator.EqualityMask) != 0) {
				EmitEquality (ec);
				return;
			}

			Label is_null_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			if (left_unwrap != null) {
				left_unwrap.EmitCheck (ec);
				ec.Emit (OpCodes.Brfalse, is_null_label);
			}

			//
			// Don't emit HasValue check when left and right expressions are same
			//
			if (right_unwrap != null && !left.Equals (right)) {
				right_unwrap.EmitCheck (ec);
				ec.Emit (OpCodes.Brfalse, is_null_label);
			}

			EmitOperator (ec, left.Type);

			if (wrap_ctor != null)
				ec.Emit (OpCodes.Newobj, wrap_ctor);

			ec.Emit (OpCodes.Br_S, end_label);
			ec.MarkLabel (is_null_label);

			if ((Oper & Operator.ComparisonMask) != 0) {
				ec.Emit (OpCodes.Ldc_I4_0);
			} else {
				LiftedNull.Create (type, loc).Emit (ec);
			}

			ec.MarkLabel (end_label);
		}
예제 #32
0
		//
		// Emits optimized equality or inequality operator when possible
		//
		void EmitEquality (EmitContext ec)
		{
			//
			// Either left or right is null
			//
			if (left_unwrap != null && (IsRightNullLifted || right.IsNull)) {
				left_unwrap.EmitCheck (ec);
				if (Oper == Binary.Operator.Equality) {
					ec.EmitInt (0);
					ec.Emit (OpCodes.Ceq);
				}
				return;
			}

			if (right_unwrap != null && (IsLeftNullLifted || left.IsNull)) {
				right_unwrap.EmitCheck (ec);
				if (Oper == Binary.Operator.Equality) {
					ec.EmitInt (0);
					ec.Emit (OpCodes.Ceq);
				}
				return;
			}

			Label dissimilar_label = ec.DefineLabel ();
			Label end_label = ec.DefineLabel ();

			if (user_operator != null) {
				user_operator.Emit (ec);
				ec.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
			} else {
				if (ec.HasSet (BuilderContext.Options.AsyncBody) && right.ContainsEmitWithAwait ()) {
					left = left.EmitToField (ec);
					right = right.EmitToField (ec);
				}

				left.Emit (ec);
				right.Emit (ec);

				ec.Emit (OpCodes.Bne_Un_S, dissimilar_label);
			}

			if (left_unwrap != null)
				left_unwrap.EmitCheck (ec);

			if (right_unwrap != null)
				right_unwrap.EmitCheck (ec);

			if (left_unwrap != null && right_unwrap != null) {
				if (Oper == Operator.Inequality)
					ec.Emit (OpCodes.Xor);
				else
					ec.Emit (OpCodes.Ceq);
			} else {
				if (Oper == Operator.Inequality) {
					ec.EmitInt (0);
					ec.Emit (OpCodes.Ceq);
				}
			}

			ec.Emit (OpCodes.Br_S, end_label);

			ec.MarkLabel (dissimilar_label);
			if (Oper == Operator.Inequality)
				ec.EmitInt (1);
			else
				ec.EmitInt (0);

			ec.MarkLabel (end_label);
		}