protected override bool EmitInternal(BinaryExpression node, EmittingContext context, GroboIL.Label returnDefaultValueLabel, ResultType whatReturn, bool extend, out Type resultType) { Expression left = node.Left; Expression right = node.Right; Type leftType, rightType; context.EmitLoadArgument(left, false, out leftType); context.EmitLoadArgument(right, false, out rightType); context.EmitArithmeticOperation(node.NodeType, node.Type, leftType, rightType, node.Method); resultType = node.Type; return(false); }
protected override bool EmitInternal(UnaryExpression node, EmittingContext context, GroboIL.Label returnDefaultValueLabel, ResultType whatReturn, bool extend, out Type resultType) { if (node.Type != typeof(bool) && node.Type != typeof(bool?)) { return(ExpressionEmittersCollection.Emit(Expression.OnesComplement(node.Operand, node.Method), context, returnDefaultValueLabel, whatReturn, extend, out resultType)); } GroboIL il = context.Il; if (node.Method != null) { throw new NotSupportedException("Custom operator '" + node.NodeType + "' is not supported"); } var operand = node.Operand; context.EmitLoadArgument(operand, false, out resultType); if (resultType == typeof(bool)) { il.Ldc_I4(1); il.Xor(); } else if (resultType == typeof(bool?)) { using (var value = context.DeclareLocal(typeof(bool?))) { il.Stloc(value); il.Ldloca(value); context.EmitHasValueAccess(typeof(bool?)); var returnLabel = il.DefineLabel("return"); il.Brfalse(returnLabel); il.Ldloca(value); context.EmitValueAccess(typeof(bool?)); il.Ldc_I4(1); il.Xor(); il.Newobj(nullableBoolConstructor); il.Stloc(value); context.MarkLabelAndSurroundWithSP(returnLabel); il.Ldloc(value); } } else { throw new InvalidOperationException("Cannot perform '" + node.NodeType + "' operator on type '" + resultType + "'"); } return(false); }
protected override bool EmitInternal(BinaryExpression node, EmittingContext context, GroboIL.Label returnDefaultValueLabel, ResultType whatReturn, bool extend, out Type resultType) { var left = node.Left; var right = node.Right; Type leftType, rightType; context.EmitLoadArgument(left, false, out leftType); context.EmitLoadArgument(right, false, out rightType); var il = context.Il; if (node.Method != null) { if (!leftType.IsNullable() && !rightType.IsNullable()) { il.Call(node.Method); } else { using (var localLeft = context.DeclareLocal(leftType)) using (var localRight = context.DeclareLocal(rightType)) { il.Stloc(localRight); il.Stloc(localLeft); var returnNullLabel = il.DefineLabel("returnNull"); if (leftType.IsNullable()) { il.Ldloca(localLeft); context.EmitHasValueAccess(leftType); il.Brfalse(returnNullLabel); } if (rightType.IsNullable()) { il.Ldloca(localRight); context.EmitHasValueAccess(rightType); il.Brfalse(returnNullLabel); } if (!leftType.IsNullable()) { il.Ldloc(localLeft); } else { il.Ldloca(localLeft); context.EmitValueAccess(leftType); } if (!rightType.IsNullable()) { il.Ldloc(localRight); } else { il.Ldloca(localRight); context.EmitValueAccess(rightType); } il.Call(node.Method); var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(returnNullLabel); context.EmitLoadDefaultValue(node.Type); context.MarkLabelAndSurroundWithSP(doneLabel); } } resultType = node.Method.ReturnType; } else { var type = leftType; if (type != rightType) { throw new InvalidOperationException("Cannot compare objects of different types '" + leftType + "' and '" + rightType + "'"); } if (!type.IsNullable()) { switch (node.NodeType) { case ExpressionType.GreaterThan: il.Cgt(type.Unsigned()); break; case ExpressionType.LessThan: il.Clt(type.Unsigned()); break; case ExpressionType.GreaterThanOrEqual: il.Clt(type.Unsigned()); il.Ldc_I4(1); il.Xor(); break; case ExpressionType.LessThanOrEqual: il.Cgt(type.Unsigned()); il.Ldc_I4(1); il.Xor(); break; default: throw new InvalidOperationException(); } resultType = typeof(bool); } else { if (!context.Options.HasFlag(CompilerOptions.UseTernaryLogic)) { using (var localLeft = context.DeclareLocal(type)) using (var localRight = context.DeclareLocal(type)) { il.Stloc(localRight); il.Stloc(localLeft); il.Ldloca(localLeft); context.EmitValueAccess(type); il.Ldloca(localRight); context.EmitValueAccess(type); var returnFalseLabel = il.DefineLabel("returnFalse"); var argument = type.GetGenericArguments()[0]; switch (node.NodeType) { case ExpressionType.GreaterThan: il.Ble(returnFalseLabel, argument.Unsigned()); break; case ExpressionType.LessThan: il.Bge(returnFalseLabel, argument.Unsigned()); break; case ExpressionType.GreaterThanOrEqual: il.Blt(returnFalseLabel, argument.Unsigned()); break; case ExpressionType.LessThanOrEqual: il.Bgt(returnFalseLabel, argument.Unsigned()); break; default: throw new InvalidOperationException(); } il.Ldloca(localLeft); context.EmitHasValueAccess(type); il.Ldloca(localRight); context.EmitHasValueAccess(type); il.And(); var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(returnFalseLabel); il.Ldc_I4(0); context.MarkLabelAndSurroundWithSP(doneLabel); resultType = typeof(bool); } } else { using (var localLeft = context.DeclareLocal(type)) using (var localRight = context.DeclareLocal(type)) { il.Stloc(localRight); il.Stloc(localLeft); il.Ldloca(localLeft); context.EmitHasValueAccess(type); il.Ldloca(localRight); context.EmitHasValueAccess(type); il.And(); var returnNullLabel = il.DefineLabel("returnNull"); il.Brfalse(returnNullLabel); il.Ldloca(localLeft); context.EmitValueAccess(type); il.Ldloca(localRight); context.EmitValueAccess(type); var argumentType = type.GetGenericArguments()[0]; switch (node.NodeType) { case ExpressionType.GreaterThan: il.Cgt(argumentType.Unsigned()); break; case ExpressionType.LessThan: il.Clt(argumentType.Unsigned()); break; case ExpressionType.GreaterThanOrEqual: il.Clt(argumentType.Unsigned()); il.Ldc_I4(1); il.Xor(); break; case ExpressionType.LessThanOrEqual: il.Cgt(argumentType.Unsigned()); il.Ldc_I4(1); il.Xor(); break; default: throw new InvalidOperationException(); } il.Newobj(nullableBoolConstructor); var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(returnNullLabel); context.EmitLoadDefaultValue(typeof(bool?)); context.MarkLabelAndSurroundWithSP(doneLabel); resultType = typeof(bool?); } } } } return(false); }
protected override bool EmitInternal(MethodCallExpression node, EmittingContext context, GroboIL.Label returnDefaultValueLabel, ResultType whatReturn, bool extend, out Type resultType) { var result = false; GroboIL il = context.Il; var method = node.Method; Expression obj; IEnumerable <Expression> arguments; IEnumerable <ParameterInfo> parameters; bool isStatic = method.IsStatic; if (!isStatic) { obj = node.Object; arguments = node.Arguments; parameters = method.GetParameters(); } else if (method.DeclaringType == typeof(Enumerable)) { obj = node.Arguments[0]; arguments = node.Arguments.Skip(1); parameters = method.GetParameters().Skip(1); } else { obj = null; arguments = node.Arguments; parameters = method.GetParameters(); } Type type = obj == null ? null : obj.Type; if (obj != null) { Type actualType; result |= ExpressionEmittersCollection.Emit(obj, context, returnDefaultValueLabel, isStatic ? ResultType.Value : ResultType.ByRefValueTypesOnly, extend, out actualType); // stack: [obj] if (actualType == typeof(void)) { throw new InvalidOperationException("Unable to call method on void"); } if (actualType.IsValueType && !isStatic) { using (var temp = context.DeclareLocal(actualType)) { il.Stloc(temp); il.Ldloca(temp); } actualType = actualType.MakeByRefType(); } if (context.Options.HasFlag(CompilerOptions.CheckNullReferences) && !actualType.IsValueType) { if (method.DeclaringType != typeof(Enumerable)) { result |= context.EmitNullChecking(type, returnDefaultValueLabel); } else { var arrIsNotNullLabel = il.DefineLabel("arrIsNotNull"); il.Dup(); il.Brtrue(arrIsNotNullLabel); il.Pop(); il.Ldc_I4(0); il.Newarr(GetElementType(type)); context.MarkLabelAndSurroundWithSP(arrIsNotNullLabel); } } } var parametersArray = parameters.ToArray(); var argumentsArray = arguments.ToArray(); for (int i = 0; i < argumentsArray.Length; i++) { var argument = argumentsArray[i]; var parameter = parametersArray[i]; if (parameter.ParameterType.IsByRef) { Type argumentType; var options = context.Options; context.Options = CompilerOptions.None; ExpressionEmittersCollection.Emit(argument, context, null, ResultType.ByRefAll, false, out argumentType); context.Options = options; if (!argumentType.IsByRef) { throw new InvalidOperationException("Expected type by reference"); } } else { Type argumentType; context.EmitLoadArgument(argument, true, out argumentType); } } il.Call(method, type); resultType = node.Type; return(result); }
protected override bool EmitInternal(BinaryExpression node, EmittingContext context, GroboIL.Label returnDefaultValueLabel, ResultType whatReturn, bool extend, out Type resultType) { Expression left = node.Left; Expression right = node.Right; Type leftType, rightType; context.EmitLoadArgument(left, false, out leftType); context.EmitLoadArgument(right, false, out rightType); GroboIL il = context.Il; if (!leftType.IsNullable() && !rightType.IsNullable()) { if (node.Method != null) { il.Call(node.Method); } else { if (leftType.IsStruct() || rightType.IsStruct()) { throw new InvalidOperationException("Cannot compare structs"); } il.Ceq(); if (node.NodeType == ExpressionType.NotEqual) { il.Ldc_I4(1); il.Xor(); } } } else { var type = leftType; if (type != rightType) { throw new InvalidOperationException("Cannot compare objects of different types '" + leftType + "' and '" + rightType + "'"); } using (var localLeft = context.DeclareLocal(type)) using (var localRight = context.DeclareLocal(type)) { il.Stloc(localRight); il.Stloc(localLeft); if (node.Method != null) { il.Ldloca(localLeft); // stack: [&left] context.EmitHasValueAccess(type); // stack: [left.HasValue] il.Dup(); // stack: [left.HasValue, left.HasValue] il.Ldloca(localRight); // stack: [left.HasValue, left.HasValue, &right] context.EmitHasValueAccess(type); // stack: [left.HasValue, left.HasValue, right.HasValue] var notEqualLabel = il.DefineLabel("notEqual"); il.Bne_Un(notEqualLabel); // stack: [left.HasValue] var equalLabel = il.DefineLabel("equal"); il.Brfalse(equalLabel); il.Ldloca(localLeft); context.EmitValueAccess(type); il.Ldloca(localRight); context.EmitValueAccess(type); il.Call(node.Method); var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(notEqualLabel); il.Pop(); il.Ldc_I4(node.NodeType == ExpressionType.Equal ? 0 : 1); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(equalLabel); il.Ldc_I4(node.NodeType == ExpressionType.Equal ? 1 : 0); context.MarkLabelAndSurroundWithSP(doneLabel); } else { il.Ldloca(localLeft); context.EmitValueAccess(type); il.Ldloca(localRight); context.EmitValueAccess(type); var notEqualLabel = il.DefineLabel("notEqual"); il.Bne_Un(notEqualLabel); il.Ldloca(localLeft); context.EmitHasValueAccess(type); il.Ldloca(localRight); context.EmitHasValueAccess(type); il.Ceq(); var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(notEqualLabel); il.Ldc_I4(0); context.MarkLabelAndSurroundWithSP(doneLabel); if (node.NodeType == ExpressionType.NotEqual) { il.Ldc_I4(1); il.Xor(); } } } } resultType = typeof(bool); return(false); }
protected override bool EmitInternal(BinaryExpression node, EmittingContext context, GroboIL.Label returnDefaultValueLabel, ResultType whatReturn, bool extend, out Type resultType) { GroboIL il = context.Il; if (node.Method != null) { throw new NotSupportedException("Custom operator '" + node.NodeType + "' is not supported"); } Expression left = node.Left; Expression right = node.Right; Type leftType; context.EmitLoadArgument(left, false, out leftType); // stack: [left] if (leftType == typeof(bool)) { switch (node.NodeType) { case ExpressionType.AndAlso: { var returnFalseLabel = il.DefineLabel("returnFalse"); il.Brfalse(returnFalseLabel); // if(left == false) return false; stack: [] Type rightType; context.EmitLoadArgument(right, false, out rightType); // stack: [right] var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); // goto done; stack: [right] context.MarkLabelAndSurroundWithSP(returnFalseLabel); il.Ldc_I4(0); // stack: [false] if (rightType == typeof(bool?)) { il.Newobj(nullableBoolConstructor); // stack: [new bool?(false)] } context.MarkLabelAndSurroundWithSP(doneLabel); resultType = rightType; break; } case ExpressionType.OrElse: { var returnTrueLabel = il.DefineLabel("returnTrue"); il.Brtrue(returnTrueLabel); // if(left == true) return true; stack: [] Type rightType; context.EmitLoadArgument(right, false, out rightType); // stack: [right] var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); // goto done; stack: [right] context.MarkLabelAndSurroundWithSP(returnTrueLabel); il.Ldc_I4(1); // stack: [true] if (rightType == typeof(bool?)) { il.Newobj(nullableBoolConstructor); // stack: [new bool?(true)] } context.MarkLabelAndSurroundWithSP(doneLabel); resultType = rightType; break; } default: throw new NotSupportedException("Node type '" + node.NodeType + "' is not supported"); } } else // bool? { switch (node.NodeType) { case ExpressionType.AndAlso: { /* * +-------+-------+-------+-------+ * | && | null | false | true | * +-------+-------+-------+-------+ * | null | null | false | null | * +-------+-------+-------+-------+ * | false | false | false | false | * +-------+-------+-------+-------+ * | true | null | false | true | * +-------+-------+-------+-------+ */ using (var localLeft = context.DeclareLocal(typeof(bool?))) { il.Stloc(localLeft); // localLeft = left; stack: [] il.Ldloca(localLeft); // stack: [&localLeft] context.EmitHasValueAccess(typeof(bool?)); // stack: [localLeft.HasValue] il.Ldc_I4(1); // stack: [localLeft.HasValue, true] il.Xor(); // stack: [!localLeft.HasValue] il.Ldloca(localLeft); // stack: [!localLeft.HasValue, &localLeft] context.EmitValueAccess(typeof(bool?)); // stack: [!localLeft.HasValue, localLeft.Value] il.Or(); // stack: [!localLeft.HasValue || localLeft.Value] var returnFalseLabel = il.DefineLabel("returnFalse"); il.Brfalse(returnFalseLabel); // if(localLeft == false) goto returnFalse; stack: [] Type rightType; context.EmitLoadArgument(right, false, out rightType); // stack: [right] using (var localRight = context.DeclareLocal(rightType)) { il.Stloc(localRight); // localRight = right; stack: [] if (rightType == typeof(bool)) { il.Ldloc(localRight); // stack: [localRight] } else { il.Ldloca(localRight); // stack: [&localRight] context.EmitHasValueAccess(typeof(bool?)); // stack: [localRight.HasValue] il.Ldc_I4(1); // stack: [localRight.HasValue, true] il.Xor(); // stack: [!localRight.HasValue] il.Ldloca(localRight); // stack: [!localRight.HasValue, &localRight] context.EmitValueAccess(typeof(bool?)); // stack: [!localRight.HasValue, localRight.Value] il.Or(); // stack: [!localRight.HasValue || localRight.Value] } il.Brfalse(returnFalseLabel); // if(localRight == false) goto returnFalse; if (rightType == typeof(bool)) { il.Ldloc(localRight); // stack: [localRight] } else { il.Ldloca(localRight); // stack: [&localRight] context.EmitHasValueAccess(typeof(bool?)); // stack: [localRight.HasValue] il.Ldloca(localRight); // stack: [localRight.HasValue, &localRight] context.EmitValueAccess(typeof(bool?)); // stack: [localRight.HasValue, localRight.Value] il.And(); // stack: [localRight.HasValue && localRight.Value] } var returnLeftLabel = il.DefineLabel("returnLeft"); il.Brtrue(returnLeftLabel); // if(localRight == true) goto returnLeft; il.Ldloca(localLeft); // stack: [&localLeft] il.Initobj(typeof(bool?)); // localLeft = default(bool?); stack: [] context.MarkLabelAndSurroundWithSP(returnLeftLabel); il.Ldloc(localLeft); // stack: [localLeft] var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(returnFalseLabel); il.Ldc_I4(0); // stack: [false] il.Newobj(nullableBoolConstructor); // new bool?(false) context.MarkLabelAndSurroundWithSP(doneLabel); resultType = typeof(bool?); } } break; } case ExpressionType.OrElse: { /* * +-------+-------+-------+-------+ * | || | null | false | true | * +-------+-------+-------+-------+ * | null | null | null | true | * +-------+-------+-------+-------+ * | false | null | false | true | * +-------+-------+-------+-------+ * | true | true | true | true | * +-------+-------+-------+-------+ */ using (var localLeft = context.DeclareLocal(typeof(bool?))) { il.Stloc(localLeft); // localLeft = left; stack: [] il.Ldloca(localLeft); // stack: [&localLeft] context.EmitHasValueAccess(typeof(bool?)); // stack: [localLeft.HasValue] il.Ldloca(localLeft); // stack: [localLeft.HasValue, &localLeft] context.EmitValueAccess(typeof(bool?)); // stack: [localLeft.HasValue, localLeft.Value] il.And(); // stack: [localLeft.HasValue && localLeft.Value] var returnTrueLabel = il.DefineLabel("returnTrue"); il.Brtrue(returnTrueLabel); // if(localLeft == true) goto returnTrue; stack: [] Type rightType; context.EmitLoadArgument(right, false, out rightType); // stack: [right] using (var localRight = context.DeclareLocal(rightType)) { il.Stloc(localRight); // localRight = right; stack: [] if (rightType == typeof(bool)) { il.Ldloc(localRight); // stack: [localRight] } else { il.Ldloca(localRight); // stack: [&localRight] context.EmitHasValueAccess(typeof(bool?)); // stack: [localRight.HasValue] il.Ldloca(localRight); // stack: [localRight.HasValue, &localRight] context.EmitValueAccess(typeof(bool?)); // stack: [localRight.HasValue, localRight.Value] il.And(); // stack: [localRight.HasValue && localRight.Value] } il.Brtrue(returnTrueLabel); // if(localRight == true) goto returnTrue; stack: [] if (rightType == typeof(bool)) { il.Ldloc(localRight); // stack: [localRight] } else { il.Ldloca(localRight); // stack: [&localRight] context.EmitHasValueAccess(typeof(bool?)); // stack: [localRight.HasValue] il.Ldc_I4(1); // stack: [localRight.HasValue, true] il.Xor(); // stack: [!localRight.HasValue] il.Ldloca(localRight); // stack: [!localRight.HasValue, &localRight] context.EmitValueAccess(typeof(bool?)); // stack: [!localRight.HasValue, localRight.Value] il.Or(); // stack: [!localRight.HasValue || localRight.Value] } var returnLeftLabel = il.DefineLabel("returnLeft"); il.Brfalse(returnLeftLabel); // if(localRight == false) goto returnLeft; il.Ldloca(localLeft); // stack: [&localLeft] il.Initobj(typeof(bool?)); // localLeft = default(bool?); stack: [] context.MarkLabelAndSurroundWithSP(returnLeftLabel); il.Ldloc(localLeft); // stack: [localLeft] var doneLabel = il.DefineLabel("done"); il.Br(doneLabel); context.MarkLabelAndSurroundWithSP(returnTrueLabel); il.Ldc_I4(1); // stack: [true] il.Newobj(nullableBoolConstructor); // new bool?(true) context.MarkLabelAndSurroundWithSP(doneLabel); resultType = typeof(bool?); } } break; } default: throw new NotSupportedException("Node type '" + node.NodeType + "' is not supported"); } } return(false); }