protected override void compileOperator(Context ctx) { var gen = ctx.CurrentILGenerator; GetExpressionType(ctx); loadAndConvertNumerics(ctx); gen.EmitRemainder(); }
public override void ProcessClosures(Context ctx) { if (MustInferArgTypes) { var name = Arguments.First(a => a.Type == typeof (UnspecifiedType)).Name; error(CompilerMessages.LambdaArgTypeUnknown, name); } // get evaluated return type var retType = _InferredReturnType ?? Body.Resolve(ctx); if (retType == typeof(NullType)) error(CompilerMessages.LambdaReturnTypeUnknown); if (retType.IsVoid()) retType = typeof (void); _Method = ctx.Scope.CreateClosureMethod(ctx, Arguments, retType); _Method.Body = Body; var outerMethod = ctx.CurrentMethod; ctx.CurrentMethod = _Method; _Method.Body.ProcessClosures(ctx); ctx.CurrentMethod = outerMethod; }
protected override void compile(Context ctx, bool mustReturn) { var gen = ctx.CurrentILGenerator; var id = ArgumentId + (ctx.CurrentMethod.IsStatic ? 0 : 1); gen.EmitLoadArgument(id); }
/// <summary> /// Generates the IL for this node. /// </summary> /// <param name="ctx">Pointer to current context.</param> /// <param name="mustReturn">Flag indicating the node should return a value.</param> public void Emit(Context ctx, bool mustReturn) { if (IsConstant && !mustReturn) return; emitCode(ctx, mustReturn); }
protected override NodeBase expand(Context ctx, bool mustReturn) { if (RightOperand.IsConstant && RightOperand.ConstantValue == 1) return LeftOperand; return base.expand(ctx, mustReturn); }
protected override void emitCode(Context ctx, bool mustReturn) { var gen = ctx.CurrentMethod.Generator; var loopType = Resolve(ctx); var saveLast = mustReturn && !loopType.IsVoid(); var beginLabel = gen.DefineLabel(); var endLabel = gen.DefineLabel(); Local tmpVar = null; if (saveLast) { tmpVar = ctx.Scope.DeclareImplicit(ctx, loopType, false); Expr.Set(tmpVar, Expr.Default(loopType)).Emit(ctx, false); } gen.MarkLabel(beginLabel); Expr.Cast(Condition, typeof(bool)).Emit(ctx, true); gen.EmitConstant(false); gen.EmitBranchEquals(endLabel); Body.Emit(ctx, mustReturn); if (saveLast) gen.EmitSaveLocal(tmpVar.LocalBuilder); gen.EmitJump(beginLabel); gen.MarkLabel(endLabel); if (saveLast) gen.EmitLoadLocal(tmpVar.LocalBuilder); }
protected override void emitCode(Context ctx, bool mustReturn) { if (_Getter == null) compileArray(ctx); else compileCustom(ctx); }
protected override void compile(Context ctx, bool mustReturn) { GetExpressionType(ctx); var gen = ctx.CurrentILGenerator; gen.EmitLoadArgument(0); }
public override void ProcessClosures(Context ctx) { if(Expression is GetIdentifierNode || Expression is GetMemberNode) Expression.ProcessClosures(ctx); base.ProcessClosures(ctx); }
private void compileCustom(Context ctx) { var gen = ctx.CurrentILGenerator; var exprType = Expression.GetExpressionType(ctx); var idxType = Index.GetExpressionType(ctx); try { var pty = ctx.ResolveIndexer(exprType, idxType, false); var idxDest = pty.ArgumentTypes[0]; var valDest = pty.ArgumentTypes[1]; Expression.Compile(ctx, true); Expr.Cast(Index, idxDest).Compile(ctx, true); Expr.Cast(Value, valDest).Compile(ctx, true); gen.EmitCall(pty.MethodInfo); } catch (LensCompilerException ex) { ex.BindToLocation(this); throw; } }
protected override void compile(Context ctx, bool mustReturn) { resolve(ctx); var gen = ctx.CurrentILGenerator; var destType = m_Field != null ? m_Field.FieldType : m_Property.PropertyType; var valType = Value.GetExpressionType(ctx); ctx.CheckTypedExpression(Value, valType, true); if(!destType.IsExtendablyAssignableFrom(valType)) Error(CompilerMessages.ImplicitCastImpossible, valType, destType); if (!m_IsStatic) { var exprType = Expression.GetExpressionType(ctx); if (Expression is IPointerProvider && exprType.IsStruct()) (Expression as IPointerProvider).PointerRequired = true; Expression.Compile(ctx, true); } Expr.Cast(Value, destType).Compile(ctx, true); if(m_Field != null) gen.EmitSaveField(m_Field.FieldInfo); else gen.EmitCall(m_Property.Setter, true); }
/// <summary> /// Emits the node ready for accessing members or invoking methods on it. /// </summary> public static void EmitNodeForAccess(this NodeBase node, Context ctx) { var type = node.Resolve(ctx); if (type.IsValueType) { if (node is IPointerProvider) { (node as IPointerProvider).PointerRequired = true; node.Emit(ctx, true); } else { var gen = ctx.CurrentMethod.Generator; var tmpVar = ctx.Scope.DeclareImplicit(ctx, type, true); gen.EmitLoadLocal(tmpVar.LocalBuilder, true); node.Emit(ctx, true); gen.EmitSaveObject(type); gen.EmitLoadLocal(tmpVar.LocalBuilder, true); } } else { node.Emit(ctx, true); } }
protected override Type resolve(Context ctx, bool mustReturn) { var exprType = Expression.Resolve(ctx); var idxType = Index.Resolve(ctx); if (!exprType.IsArray) { try { _Indexer = ReflectionHelper.ResolveIndexer(exprType, idxType, false); } catch (LensCompilerException ex) { ex.BindToLocation(this); throw; } } var idxDestType = exprType.IsArray ? typeof (int) : _Indexer.ArgumentTypes[0]; var valDestType = exprType.IsArray ? exprType.GetElementType() : _Indexer.ArgumentTypes[1]; if(!idxDestType.IsExtendablyAssignableFrom(idxType)) error(Index, CompilerMessages.ImplicitCastImpossible, idxType, idxDestType); ensureLambdaInferred(ctx, Value, valDestType); var valType = Value.Resolve(ctx); if (!valDestType.IsExtendablyAssignableFrom(valType)) error(Value, CompilerMessages.ImplicitCastImpossible, valType, valDestType); return base.resolve(ctx, mustReturn); }
protected override void emitCode(Context ctx, bool mustReturn) { var gen = ctx.CurrentMethod.Generator; var backup = ctx.CurrentTryBlock; ctx.CurrentTryBlock = this; EndLabel = gen.BeginExceptionBlock(); Code.Emit(ctx, false); gen.EmitLeave(EndLabel); foreach (var curr in CatchClauses) curr.Emit(ctx, false); if (Finally != null) { gen.BeginFinallyBlock(); Finally.Emit(ctx, false); } gen.EndExceptionBlock(); ctx.CurrentTryBlock = backup; }
protected override Type resolve(Context ctx, bool mustReturn) { if(ctx.CurrentMethod.IsStatic) error("Cannot access self-reference in static context!"); return ctx.CurrentType.TypeBuilder; }
protected override void emitCode(Context ctx, bool mustReturn) { if (_Indexer == null) emitSetArray(ctx); else emitSetCustomIndexer(ctx); }
protected override NodeBase expand(Context ctx, bool mustReturn) { var leftType = LeftOperand.Resolve(ctx, mustReturn); // create a lambda expression that passes the result of left function to the right one if (leftType.IsCallableType()) { var leftVar = ctx.Unique.TempVariableName(); var rightVar = ctx.Unique.TempVariableName(); var delegateType = ReflectionHelper.WrapDelegate(leftType); var argDefs = delegateType.ArgumentTypes.Select(x => Expr.Arg(ctx.Unique.AnonymousArgName(), x.FullName)).ToArray(); return Expr.Lambda( argDefs, Expr.Block( Expr.Let(leftVar, LeftOperand), Expr.Let(rightVar, RightOperand), Expr.Invoke( Expr.Get(rightVar), Expr.Invoke( Expr.Get(leftVar), argDefs.Select(x => Expr.Get(x.Name)).ToArray() ) ) ) ); } return base.expand(ctx, mustReturn); }
protected override void compileOperator(Context ctx) { var gen = ctx.CurrentILGenerator; Operand.Compile(ctx, true); gen.EmitNegate(); }
protected override System.Type resolveExpressionType(Context ctx, bool mustReturn = true) { if(ctx.CurrentMethod.IsStatic) Error("Cannot access self-reference in static context!"); return ctx.CurrentType.TypeBuilder; }
protected override void emitCode(Context ctx, bool mustReturn) { var gen = ctx.CurrentMethod.Generator; Expr.Cast<int>(Size).Emit(ctx, true); gen.EmitCreateArray(Type); }
protected override void emitCode(Context ctx, bool mustReturn) { var type = Type ?? ctx.ResolveType(TypeSignature); var gen = ctx.CurrentMethod.Generator; gen.EmitConstant(type); gen.EmitCall(_HandleMethod); }
protected override NodeBase expand(Context ctx, bool mustReturn) { var result = IsConstant && ctx.Options.UnrollConstants ? Expr.Constant(ConstantValue) : null; return result; }
public override IEnumerable<NodeBase> Expand(Context ctx, NodeBase expression, Label nextStatement) { foreach (var rule in KeyRule.Expand(ctx, Expr.GetMember(expression, "Key"), nextStatement)) yield return rule; foreach (var rule in ValueRule.Expand(ctx, Expr.GetMember(expression, "Value"), nextStatement)) yield return rule; }
protected override NodeBase expand(Context ctx, bool mustReturn) { var op = Operand as InversionOperatorNode; if (op != null) return op.Operand; return base.expand(ctx, mustReturn); }
protected override void compile(Context ctx, bool mustReturn) { var exprType = Expression.GetExpressionType(ctx); if (exprType.IsArray) compileArray(ctx); else compileCustom(ctx); }
protected override void compile(Context ctx, bool mustReturn) { var type = Type ?? ctx.ResolveType(TypeSignature); var gen = ctx.CurrentILGenerator; var method = typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }); gen.EmitConstant(type); gen.EmitCall(method); }
protected override void emitOperator(Context ctx) { var gen = ctx.CurrentMethod.Generator; LeftOperand.Emit(ctx, true); RightOperand.Emit(ctx, true); gen.EmitShift(IsLeft); }
protected override void compileOperator(Context ctx) { var gen = ctx.CurrentILGenerator; Expr.Cast(Operand, typeof(bool)).Compile(ctx, true); gen.EmitConstant(0); gen.EmitCompareEqual(); }
protected override void emitOperator(Context ctx) { var gen = ctx.CurrentMethod.Generator; Expr.Cast<bool>(Operand).Emit(ctx, true); gen.EmitConstant(0); gen.EmitCompareEqual(); }
protected override void compileOperator(Context ctx) { var gen = ctx.CurrentILGenerator; loadAndConvertNumerics(ctx, typeof(double)); var method = typeof(Math).GetMethod("Pow", new[] { typeof(double), typeof(double) }); gen.EmitCall(method); }