public ExprFieldAddress(Ctx ctx, Expr obj, FieldReference field)
     : base(ctx) {
     this.Obj = obj;
     this.Field = field;
     this.ElementType = field.FieldType.FullResolve(field);
     this.type = this.ElementType.MakePointer();
 }
 public ExprVariableAddress(Ctx ctx, Expr variable, TypeReference type)
     : base(ctx) {
     //this.Index = index;
     this.Variable = variable;
     this.ElementType = type;
     this.type = type.MakePointer();
 }
Example #3
0
 public StmtTry(Ctx ctx, Instruction @try, Instruction @catch, Instruction @finally, TypeReference catchType)
     : base(ctx) {
     this.@try = @try;
     this.@catch = @catch;
     this.@finally = @finally;
     this.catchType = catchType;
 }
 public ExprBinary(Ctx ctx, BinaryOp op, TypeReference type, Expr left, Expr right)
     : base(ctx) {
     this.Op = op;
     this.type = type;
     this.Left = left;
     this.Right = right;
 }
 public ExprArgAddress(Ctx ctx, Expr arg, TypeReference type)
     : base(ctx) {
     //this.Index = index;
     this.Arg = arg;
     this.ElementType = type;
     this.type = type.MakePointer();
 }
 public ExprTernary(Ctx ctx, Expr condition, Expr ifTrue, Expr ifFalse)
     : base(ctx) {
     this.Condition = condition;
     this.IfTrue = ifTrue;
     this.IfFalse = ifFalse;
     this.type = TypeCombiner.Combine(ctx, ifTrue, ifFalse);
 }
 public ExprJsResolvedMethod(Ctx ctx, TypeReference returnType, Expr obj, string methodName, IEnumerable<Expr> args)
     : base(ctx) {
     this.returnType = returnType;
     this.Obj = obj;
     this.MethodName = methodName;
     this.Args = args;
 }
Example #8
0
 public static Stmt ctor(Ctx ctx) {
     Expression<Func<int>> eHashCode = () => hashCode;
     var field = (FieldInfo)((MemberExpression)eHashCode.Body).Member;
     var f = ctx.Module.Import(field);
     var fExpr = new ExprFieldAccess(ctx, null, f).Named("hashCode");
     var stmt = new StmtJsExplicit(ctx, "this.$=++hashCode;", ctx.ThisNamed, fExpr);
     return stmt;
 }
 public CilProcessor(Ctx ctx, Stack<Expr> stack, Expr[] locals, Expr[] args, Dictionary<Instruction, ExprVarInstResult> instResults) {
     this.ctx = ctx;
     this.stack = stack;
     this.locals = locals;
     this.args = args;
     this.instResults = instResults;
     this.localTypes = ctx.MDef.Body.Variables.Select(x => x.VariableType.FullResolve(ctx)).ToArray();
 }
Example #10
0
 public StmtCil(Ctx ctx, IEnumerable<Instruction> insts, Stmt endCil, SpecialBlock blockType = SpecialBlock.Normal)
     : base(ctx) {
     this.Insts = insts;
     this.BlockType = blockType;
     this.EndCil = endCil;
     this.StartStackSize = 0;
     this.EndStackSize = 0;
 }
 //public ExprJsVirtualCall(Ctx ctx, MethodReference callMethod, Expr objInit, Expr objRef, IEnumerable<Expr> args)
 public ExprJsVirtualCall(Ctx ctx, MethodReference callMethod, Expr runtimeType, Expr objRef, IEnumerable<Expr> args)
     : base(ctx) {
     this.CallMethod = callMethod;
     //this.ObjInit = objInit;
     this.RuntimeType = runtimeType;
     this.ObjRef = objRef;
     this.Args = args;
     this.returnType = callMethod.ReturnType.FullResolve(callMethod);
 }
Example #12
0
 public static Stmt IsWhiteSpace(Ctx ctx) {
     // See http://msdn.microsoft.com/en-us/library/t809ektx.aspx for list
     var whiteSpace = " \u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d\u0085\u00a0";
     var arg0 = ctx.MethodParameter(0);
     var c = new ExprJsResolvedMethod(ctx, ctx.String, null, "String.fromCharCode", arg0);
     var indexOf = new ExprJsResolvedMethod(ctx, ctx.Boolean, ctx.Literal(whiteSpace), "indexOf", c);
     return new StmtReturn(ctx,
         new ExprBinary(ctx, BinaryOp.GreaterThanOrEqual, ctx.Boolean, indexOf, ctx.Literal(0)));
 }
Example #13
0
 public ExprCall(Ctx ctx, MethodReference callMethod, Expr obj, IEnumerable<Expr> args, bool isVirtualCall, TypeReference constrainedType, TypeReference forceReturnType)
     : base(ctx) {
     this.CallMethod = callMethod;
     this.Obj = obj;
     this.Args = args;
     this.IsVirtualCall = isVirtualCall;
     this.ConstrainedType = constrainedType;
     this.returnType = forceReturnType ?? callMethod.ReturnType.FullResolve(callMethod);
 }
 public static TypeReference Combine(Ctx ctx, TypeReference a, TypeReference b) {
     if (a.IsSame(b)) {
         return a;
     }
     var t = Tuple.Create(a, b);
     if (t.Perms((x, y) => x.IsBoolean() && y.IsInt32())) {
         return ctx.Boolean;
     }
     return ctx.Object;
 }
Example #15
0
 public StmtBlock(Ctx ctx, IEnumerable<Stmt> statements)
     : base(ctx) {
     // Expand any nested blocks. Evaluate to array.
     this.Statements = statements.SelectMany(x => {
         if (x == null) {
             return Enumerable.Empty<Stmt>();
         }
         if (x.StmtType == NodeType.Block) {
             return ((StmtBlock)x).Statements;
         }
         return new[] { x };
     }).Where(x => x != null).ToArray();
 }
Example #16
0
 public Ctx(TypeReference tRef, MethodReference mRef, Ctx fromCtx = null) {
     this.TRef = tRef;
     this.TDef = tRef.Resolve();
     this.MRef = mRef;
     this.MDef = mRef.Resolve();
     this.HasFakeThis = mRef.Parameters.FirstOrDefault().NullThru(x => x.GetCustomAttribute<JsFakeThisAttribute>() != null);
     this.Module = mRef.Module;
     this.TypeSystem = mRef.Module.TypeSystem;
     this.ExprGen = Expr.CreateExprGen(this);
     this.This = fromCtx == null ? (this.MDef.IsStatic ? null : new ExprVarThis(this, tRef)) : fromCtx.This;
     this.type = new Lazy<TypeReference>(() => this.Module.Import(typeof(Type)));
     this._int64 = new Lazy<TypeReference>(() => this.Module.Import(typeof(Cls._Int64)));
     this._uint64 = new Lazy<TypeReference>(() => this.Module.Import(typeof(Cls._UInt64)));
 }
Example #17
0
 public static Stmt Atan2(Ctx ctx) {
     var arg0 = ctx.MethodParameter(0);
     var arg1 = ctx.MethodParameter(1);
     var eg = ctx.ExprGen;
     return new StmtReturn(ctx,
         new ExprTernary(ctx,
             eg.And(
                 eg.Not(new ExprJsResolvedMethod(ctx, ctx.Boolean, null, "Number.isFinite", arg0)),
                 eg.Not(new ExprJsResolvedMethod(ctx, ctx.Boolean, null, "Number.isFinite", arg1))
             ),
             ctx.Literal(Double.NaN),
             new ExprJsResolvedMethod(ctx, ctx.Double, null, "Math.atan2", arg0, arg1)
         )
     );
 }
            public Stmt GetImpl(Ctx ctx) {
                var a = ctx.MethodParameter(0, "a");
                var b = ctx.MethodParameter(1, "b");
                var hi = ctx.Local(ctx.UInt64, "hi");
                var lo = ctx.Local(ctx.UInt64, "lo");
                var limit = ctx.Literal(0x100000000UL, ctx._UInt64, "limit");
                var js = @"
hi = a[0] - b[0];
lo = a[1] - b[1];
if (lo < 0) {
    lo += limit;
    hi--;
}
if (hi < 0) hi += limit;
return [hi, lo];
";
                var stmt = new StmtJsExplicit(ctx, js, a, b, hi, lo, limit);
                return stmt;
            }
            public Stmt GetImpl(Ctx ctx) {
                var a = ctx.MethodParameter(0).Named("a");
                var b = ctx.MethodParameter(1).Named("b");
                var hi = ctx.Local(ctx.UInt64, "hi");
                var lo = ctx.Local(ctx.UInt64, "lo");
                var limit = ctx.Literal(0x100000000UL, ctx._UInt64, "limit");
                var js = @"
hi = a[0] + b[0];
lo = a[1] + b[1];
if (lo >= limit) {
    lo -= limit;
    hi++;
}
if (hi >= limit) hi -= limit;
return [hi, lo];
";
                var stmt = new StmtJsExplicit(ctx, js, a, b, hi, lo, limit);
                return stmt;
            }
Example #20
0
 public static Stmt Abs(Ctx ctx) {
     var arg = ctx.MethodParameter(0);
     ExprLiteral minVal;
     switch (arg.Type.MetadataType) {
     case MetadataType.SByte: minVal = ctx.Literal(Int8.MinValue); break;
     case MetadataType.Int16: minVal = ctx.Literal(Int16.MinValue); break;
     case MetadataType.Int32: minVal = ctx.Literal(Int32.MinValue); break;
     case MetadataType.Int64: minVal = ctx.Literal(Int64.MinValue); break;
     default: throw new InvalidOperationException("Cannot handle: " + arg.Type);
     }
     var exCtor = ctx.Module.Import(typeof(OverflowException).GetConstructor(Type.EmptyTypes));
     return new StmtIf(ctx,
         ctx.ExprGen.Equal(arg, minVal),
         new StmtThrow(ctx, new ExprNewObj(ctx, exCtor)),
         new StmtReturn(ctx,
             arg.Type.IsInt64() ?
             (Expr)new ExprCall(ctx, (Func<Int64, Int64>)_Int64.Abs, null, arg) :
             (Expr)new ExprJsResolvedMethod(ctx, arg.Type, null, "Math.abs", arg))
         );
 }
            public Stmt GetImpl(Ctx ctx) {
                var a = ctx.MethodParameter(0, "a");
                var b = ctx.MethodParameter(1, "b");
                var aa = ctx.Local(ctx.Int32.MakeArray(), "aa");
                var bb = ctx.Local(ctx.Int32.MakeArray(), "bb");
                var ia = ctx.Local(ctx.Int32, "ia");
                var ib = ctx.Local(ctx.Int32, "ib");
                var mul = ctx.Local(ctx.Int32, "mul");
                var add = ctx.Local(ctx.Int32, "add");
                var mulCarry = ctx.Local(ctx.Int32, "mulCarry");
                var addCarry = ctx.Local(ctx.Int32, "addCarry");
                var rrOfs = ctx.Local(ctx.Int32, "rrOfs");
                var rr = ctx.Local(ctx.Int32.MakeArray(), "rr");
                var mask = ctx.Literal(0xffff, ctx.Int32, "mask");
                var limit = ctx.Literal(0x10000, ctx.Int32, "limit");
                var js = @"
aa = [a[0] >>> 16, a[0] & mask, a[1] >>> 16, a[1] & mask];
bb = [b[0] >>> 16, b[0] & mask, b[1] >>> 16, b[1] & mask];
rr = [0, 0, 0, 0];
for (ib = 3; ib >= 0; ib--) {
    mulCarry = 0;
    addCarry = 0;
    for(ia = 3; ia >= 3 - ib; ia--) {
        rrOfs = ia + ib - 3;
        mul = aa[ia] * bb[ib] + mulCarry;
        mulCarry = mul >>> 16;
        add = rr[rrOfs] + (mul & mask) + addCarry;
        if (add >= limit){
            rr[rrOfs] = add - limit;
            addCarry = 1;
        } else {
            rr[rrOfs] = add;
            addCarry = 0;
        }
    }
}
return [rr[1] + rr[0] * limit, rr[3] + rr[2] * limit];
";
                var stmt = new StmtJsExplicit(ctx, js, a, b, aa, bb, rr, mask, limit, ia, ib, mul, add, mulCarry, addCarry, rrOfs);
                return stmt;
            }
 public ExprRuntimeHandle(Ctx ctx, MemberReference member)
     : base(ctx) {
     var tokenType = member.MetadataToken.TokenType;
     switch (tokenType) {
     case TokenType.TypeDef:
     case TokenType.TypeRef:
     case TokenType.TypeSpec:
     case TokenType.GenericParam:
         this.Member = ((TypeReference)member).FullResolve(ctx);
         this.type = ctx.Module.Import(typeof(RuntimeTypeHandle));
         break;
     case TokenType.Method:
     case TokenType.MethodSpec:
         this.Member = ((MethodReference)member).FullResolve(ctx);
         this.type = ctx.Module.Import(typeof(RuntimeMethodHandle));
         break;
     case TokenType.Field:
         this.Member = ((FieldReference)member).FullResolve(ctx);
         this.type = ctx.Module.Import(typeof(RuntimeFieldHandle));
         break;
     default:
         throw new NotImplementedException("Cannot handle token type: " + tokenType);
     }
 }
 public ExprJsExplicit(Ctx ctx, string js, TypeReference type, IEnumerable<NamedExpr> namedExprs)
     : base(ctx) {
     this.JavaScript = js;
     this.type = type;
     this.NamedExprs = namedExprs;
 }
 public ExprJsExplicit(Ctx ctx, string js, TypeReference type, params NamedExpr[] namedExprs)
     : this(ctx, js, type, (IEnumerable<NamedExpr>)namedExprs) {
 }
 public ExprJsTypeVarName(Ctx ctx, TypeReference type)
     : base(ctx) {
     this.TypeRef = type;
 }
Example #26
0
 public static Stmt Equals(Ctx ctx) {
     var other = ctx.MethodParameter(0).Named("other");
     var type = new ExprJsTypeVarName(ctx, ctx.Single).Named("type");
     return new StmtJsExplicit(ctx, "return other._ === type && this === other.v;", ctx.ThisNamed, other, type);
 }
Example #27
0
 public static Stmt GetHashCode(Ctx ctx) {
     var toString = new ExprJsResolvedMethod(ctx, ctx.String, ctx.This, "toExponential");
     var getHashCode = new ExprCall(ctx, typeof(string).GetMethod("GetHashCode"), toString);
     var stmt = new StmtReturn(ctx, getHashCode);
     return stmt;
 }
Example #28
0
 public StmtEmpty(Ctx ctx)
     : base(ctx) {
 }
Example #29
0
 public StmtThrow(Ctx ctx, Expr expr)
     : base(ctx)
 {
     this.Expr = expr;
 }
 public ExprVarParameter(Ctx ctx, ParameterDefinition parameter)
     : base(ctx) {
     this.Parameter = parameter;
     this.type = parameter.ParameterType;
 }
 private void Add(TypeReference tRef, Ctx ctx) {
     var cctor = tRef.Resolve().Methods.FirstOrDefault(x => x.Name == ".cctor");
     if (cctor != null) {
         this.staticConstructors.Add(cctor.FullResolve(ctx));
     }
 }
Example #32
0
 public ExprLoadIndirect(Ctx ctx, Expr expr, TypeReference loadType)
     : base(ctx)
 {
     this.Expr     = expr;
     this.loadType = loadType;
 }