Inheritance: clojure.lang.CljCompiler.Ast.Expr
示例#1
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            int n = _bindingInits.count();
            List<ParameterExpression> parms = new List<ParameterExpression>(n);
            List<Expression> forms = new List<Expression>(n);

            /// First, set up the environment.
            for (int i = 0; i < n; i++)
            {
                BindingInit bi = (BindingInit)_bindingInits.nth(i);
                ParameterExpression parmExpr = Expression.Parameter(typeof(IFn), bi.Binding.Name);
                bi.Binding.ParamExpression = parmExpr;
                parms.Add(parmExpr);
            }

            // Then initialize
            for (int i = 0; i < n; i++)
            {
                BindingInit bi = (BindingInit)_bindingInits.nth(i);
                ParameterExpression parmExpr = (ParameterExpression)bi.Binding.ParamExpression;
                forms.Add(Expression.Assign(parmExpr, bi.Init.GenCode(RHC.Expression,objx,context)));
            }

            // The work
            forms.Add(_body.GenCode(rhc,objx,context));
            return Expression.Block(parms,forms);
        }
示例#2
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     EmitUnboxed(rhc, objx, ilg);
     HostExpr.EmitBoxReturn(objx, ilg, typeof(bool));
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#3
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     objx.EmitVar(ilg, _var);
     if (_isDynamic)
     {
         ilg.Emit(OpCodes.Call, Compiler.Method_Var_setDynamic0);
     }
     if (_meta != null)
     {
         if (_initProvided || true) //IncludesExplicitMetadata((MapExpr)_meta))
         {
             ilg.Emit(OpCodes.Dup);
             _meta.Emit(RHC.Expression, objx, ilg);
             ilg.Emit(OpCodes.Castclass, typeof(IPersistentMap));
             ilg.Emit(OpCodes.Call, Compiler.Method_Var_setMeta);
         }
     }
     if (_initProvided)
     {
         ilg.Emit(OpCodes.Dup);
         if (_init is FnExpr)
             ((FnExpr)_init).EmitForDefn(objx, ilg);
         else
             _init.Emit(RHC.Expression, objx, ilg);
         ilg.Emit(OpCodes.Call,Compiler.Method_Var_bindRoot);
     }
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#4
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            List<Expression> exprs = new List<Expression>();

            ParameterExpression parm = Expression.Parameter(typeof(Var), "v");

            Expression varExpr = objx.GenVar(context,_var);

            exprs.Add(Expression.Assign(parm, varExpr));

            // The Java code does the following in the opposite order (as modified in commit #430dd4f, Jan 19, 2010)
            // However, the call to bindRoot sets :macro to False.  I'm not sure how it works now in the JVM version.

            if (_initProvided )
                // Java doesn't Box here, but we have to deal with unboxed bool values
                exprs.Add(Expression.Call(parm, Compiler.Method_Var_bindRoot, Compiler.MaybeBox(_init.GenCode(RHC.Expression,objx,context))));

            if (_meta != null)
            {
                if (_initProvided || IncludesExplicitMetadata((MapExpr)_meta))
                {
                    // Java casts to IPersistentMap on the _meta, but Expression.Call can handle that for us.
                    exprs.Add(Expression.Call(parm, Compiler.Method_Var_setMeta, _meta.GenCode(RHC.Expression,objx,context)));
                }
            }

            exprs.Add(parm);

            return Expression.Block(new ParameterExpression[] { parm }, exprs);
        }
示例#5
0
 public override void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     ilg.EmitBoolean(_val);
     ilg.Emit(OpCodes.Box,typeof(bool));
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#6
0
        public override Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression exc = _excExpr.GenCode(RHC.Expression, objx, context);
            Expression exc2 = Expression.Convert(exc, typeof(Exception));

            return Expression.Throw(exc2,typeof(object));
        }
示例#7
0
 public void EmitAssign(RHC rhc, ObjExpr objx, CljILGen ilg, Expr val)
 {
     objx.EmitVar(ilg, _var);
     val.Emit(RHC.Expression, objx, ilg);
     ilg.Emit(OpCodes.Call, Compiler.Method_Var_set);
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#8
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     for (int i = 0; i < _exprs.count() - 1; i++)
     {
         Expr e = (Expr)_exprs.nth(i);
         e.Emit(RHC.Statement, objx, ilg);
     }
     LastExpr.Emit(rhc, objx, ilg);
 }
示例#9
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     ilg.Emit(OpCodes.Call,Compiler.Method_Compiler_CurrentNamespace.GetGetMethod());
     ilg.Emit(OpCodes.Ldstr, _c);
     ilg.Emit(OpCodes.Call, Compiler.Method_RT_classForName);
     ilg.Emit(OpCodes.Call, Compiler.Method_Namespace_importClass1);
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#10
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     _expr.Emit(RHC.Expression, objx, ilg);
     ilg.Emit(OpCodes.Castclass, typeof(IObj));
     _meta.Emit(RHC.Expression, objx, ilg);
     ilg.Emit(OpCodes.Castclass, typeof(IPersistentMap));
     ilg.EmitCall(Compiler.Method_IObj_withMeta);
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#11
0
 public void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     for (int i = 0; i < _exprs.count() - 1; i++)
     {
         Expr e = (Expr)_exprs.nth(i);
         e.Emit(RHC.Statement, objx, ilg);
     }
     MaybePrimitiveExpr mbe = (MaybePrimitiveExpr)LastExpr;
     mbe.EmitUnboxed(rhc, objx, ilg);
 }
示例#12
0
        public override Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression call;

            if (_method != null)
                call = GenDlrForMethod(objx, context);
            else
                call = GenerateComplexCall(objx, context);
            call = Compiler.MaybeAddDebugInfo(call, _spanMap, context.IsDebuggable);
            return call;
        }
示例#13
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression objExpr = _expr.GenCode(RHC.Expression, objx, context);
            Expression iobjExpr = Expression.Convert(objExpr, typeof(IObj));

            Expression metaExpr = _meta.GenCode(RHC.Expression, objx, context);
            metaExpr = Expression.Convert(metaExpr, typeof(IPersistentMap));

            Expression ret = Expression.Call(iobjExpr, Compiler.Method_IObj_withMeta, metaExpr);

            return ret;
        }
示例#14
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression objExpr = _expr.GenCode(RHC.Expression, objx, context);
            Expression iobjExpr = Expression.Convert(objExpr, typeof(IObj));

            Expression metaExpr = _meta.GenCode(RHC.Expression, objx, context);
            // Do we need a conversion here?  probably not.

            Expression ret = Expression.Call(iobjExpr, Compiler.Method_IObj_withMeta, metaExpr);

            return ret;
        }
示例#15
0
        internal static void EmitBoxReturn(ObjExpr objx, CljILGen ilg, Type returnType)

        {
            if (returnType == typeof(void))
            {
                ilg.Emit(OpCodes.Ldnull);
            }
            else if (returnType.IsPrimitive || returnType.IsValueType)
            {
                ilg.Emit(OpCodes.Box, returnType);
            }
        }
示例#16
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     EmitUnboxed(rhc, objx, ilg);
     if (rhc != RHC.Statement)
     {
         HostExpr.EmitBoxReturn(objx, ilg, _retType);
     }
     if (rhc == RHC.Statement)
     {
         ilg.Emit(OpCodes.Pop);
     }
 }
示例#17
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     _expr.Emit(RHC.Expression, objx, ilg);
     ilg.Emit(OpCodes.Castclass, typeof(IObj));
     _meta.Emit(RHC.Expression, objx, ilg);
     ilg.Emit(OpCodes.Castclass, typeof(IPersistentMap));
     ilg.EmitCall(Compiler.Method_IObj_withMeta);
     if (rhc == RHC.Statement)
     {
         ilg.Emit(OpCodes.Pop);
     }
 }
示例#18
0
        public override void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            GenContext.EmitDebugInfo(ilg, _spanMap);
            if (_tinfo.IsLiteral)
            {
                // literal fields need to be inlined directly in here... We use GetRawConstant
                // which will work even in partial trust if the constant is protected.
                object value = _tinfo.GetRawConstantValue();
                switch (Type.GetTypeCode(_tinfo.FieldType))
                {
                case TypeCode.Boolean:
                    if ((bool)value)
                    {
                        ilg.Emit(OpCodes.Ldc_I4_1);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Ldc_I4_0);
                    }
                    break;

                case TypeCode.Byte: ilg.EmitInt((int)(byte)value); break;

                case TypeCode.Char: ilg.EmitInt((int)(char)value); break;

                case TypeCode.Double: ilg.EmitDouble((double)value); break;

                case TypeCode.Int16: ilg.EmitInt((int)(short)value); break;

                case TypeCode.Int32: ilg.EmitInt((int)value); break;

                case TypeCode.Int64: ilg.EmitLong((long)value); break;

                case TypeCode.SByte: ilg.EmitInt((int)(sbyte)value); break;

                case TypeCode.Single: ilg.EmitSingle((float)value); break;

                case TypeCode.String: ilg.EmitString((string)value); break;

                case TypeCode.UInt16: ilg.EmitInt((int)(ushort)value); break;

                case TypeCode.UInt32: ilg.Emit(OpCodes.Ldc_I4, (uint)value); break;

                case TypeCode.UInt64: ilg.Emit(OpCodes.Ldc_I8, (ulong)value); break;
                }
            }
            else
            {
                ilg.MaybeEmitVolatileOp(_tinfo);
                ilg.EmitFieldGet(_tinfo);
            }
        }
示例#19
0
        protected static void EmitBody(ObjExpr objx, CljILGen ilg, Type retType, Expr body)
        {
            MaybePrimitiveExpr be = (MaybePrimitiveExpr)body;

            if (Util.IsPrimitive(retType) && be.CanEmitPrimitive)
            {
                Type bt = Compiler.MaybePrimitiveType(be);
                if (bt == retType)
                {
                    be.EmitUnboxed(RHC.Return, objx, ilg);
                }
                else if (retType == typeof(long) && bt == typeof(int))
                {
                    be.EmitUnboxed(RHC.Return, objx, ilg);
                    ilg.Emit(OpCodes.Conv_I8);
                }
                else if (retType == typeof(double) && bt == typeof(float))
                {
                    be.EmitUnboxed(RHC.Return, objx, ilg);
                    ilg.Emit(OpCodes.Conv_R8);
                }
                else if (retType == typeof(int) && bt == typeof(long))
                {
                    be.EmitUnboxed(RHC.Return, objx, ilg);
                    ilg.Emit(OpCodes.Call, Compiler.Method_RT_intCast_long);
                }
                else if (retType == typeof(float) && bt == typeof(double))
                {
                    be.EmitUnboxed(RHC.Return, objx, ilg);
                    ilg.Emit(OpCodes.Conv_R4);
                }
                else
                {
                    throw new ArgumentException(String.Format("Mismatched primitive return, expected: {0}, had: {1}", retType, be.ClrType));
                }
            }
            else
            {
                body.Emit(RHC.Return, objx, ilg);
                if (body.HasNormalExit())
                {
                    if (retType == typeof(void))
                    {
                        ilg.Emit(OpCodes.Pop);
                    }
                    else
                    {
                        EmitUnboxArg(ilg, typeof(object), retType);
                    }
                }
            }
        }
示例#20
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression basicFn = _fexpr.GenCode(RHC.Expression, objx, context);

            basicFn = Expression.Convert(basicFn, typeof(IFn));

            if (_isProtocol)
            {
                return(GenProto(rhc, objx, context, basicFn));
            }

            return(GenNonProto(rhc, objx, context, basicFn));
        }
示例#21
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression objExpr  = _expr.GenCode(RHC.Expression, objx, context);
            Expression iobjExpr = Expression.Convert(objExpr, typeof(IObj));

            Expression metaExpr = _meta.GenCode(RHC.Expression, objx, context);

            metaExpr = Expression.Convert(metaExpr, typeof(IPersistentMap));

            Expression ret = Expression.Call(iobjExpr, Compiler.Method_IObj_withMeta, metaExpr);

            return(ret);
        }
示例#22
0
 public override void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     if (_excExpr == null)
     {
         ilg.Emit(OpCodes.Rethrow);
     }
     else
     {
         _excExpr.Emit(RHC.Expression, objx, ilg);
         ilg.Emit(OpCodes.Castclass, typeof(Exception));
         ilg.Emit(OpCodes.Throw);
     }
 }
示例#23
0
        public void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            Type t = _n.GetType();

            if (t == typeof(int))
                ilg.EmitLong((long)(int)_n);
            else if (t == typeof(double))
                ilg.EmitDouble((double)_n);
            else if (t == typeof(long))
                ilg.EmitLong((long)_n);
            else
                throw new ArgumentException("Unsupported Number type: " + _n.GetType().Name);
        }
示例#24
0
        public Expression GenCodeUnboxed(RHC rhc, ObjExpr objx, GenContext context)
        {
            Type t = _n.GetType();

            if (t == typeof(int))
                return Expression.Constant((long)(int)_n, typeof(long));
            else if (t == typeof(double))
                return Expression.Constant((double)_n, typeof(double));
            else if ( t == typeof(long) )
                return Expression.Constant((long)_n,typeof(long));

            throw new ArgumentException("Unsupported Number type: " + _n.GetType().Name);
        }
示例#25
0
        void DoEmit(RHC rhc, ObjExpr objx, CljILGen ilg, bool emitUnboxed)
        {
            Label nullLabel  = ilg.DefineLabel();
            Label falseLabel = ilg.DefineLabel();
            Label endLabel   = ilg.DefineLabel();
            Label trueLabel  = ilg.DefineLabel();

            GenContext.EmitDebugInfo(ilg, _sourceSpan);

            if (_testExpr is StaticMethodExpr sme && sme.CanEmitIntrinsicPredicate())
            {
                sme.EmitIntrinsicPredicate(RHC.Expression, objx, ilg, falseLabel);
            }
示例#26
0
        internal static void EmitArgsAsArray(IPersistentVector args, ObjExpr objx, CljILGen ilg)
        {
            ilg.EmitInt(args.count());
            ilg.Emit(OpCodes.Newarr, typeof(Object));

            for (int i = 0; i < args.count(); i++)
            {
                ilg.Emit(OpCodes.Dup);
                ilg.EmitInt(i);
                ((Expr)args.nth(i)).Emit(RHC.Expression, objx, ilg);
                ilg.Emit(OpCodes.Stelem_Ref);
            }
        }
示例#27
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            List <Expression> exprs = new List <Expression>(_exprs.count());

            for (int i = 0; i < _exprs.count() - 1; i++)
            {
                Expr e = (Expr)_exprs.nth(i);
                exprs.Add(e.GenCode(RHC.Statement, objx, context));
            }
            exprs.Add(LastExpr.GenCode(rhc, objx, context));

            return(Expression.Block(exprs));
        }
示例#28
0
        private static void EmitExpr(ObjExpr objx, CljILGen ilg, Expr expr, bool emitUnboxed)
        {
            MaybePrimitiveExpr mbe = expr as MaybePrimitiveExpr;

            if (emitUnboxed && mbe != null)
            {
                mbe.EmitUnboxed(RHC.Expression, objx, ilg);
            }
            else
            {
                expr.Emit(RHC.Expression, objx, ilg);
            }
        }
示例#29
0
 public override void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     if (_excExpr == null)
     {
         ilg.Emit(OpCodes.Rethrow);
     }
     else
     {
         _excExpr.Emit(RHC.Expression, objx, ilg);
         ilg.Emit(OpCodes.Castclass, typeof(Exception));
         ilg.Emit(OpCodes.Throw);
     }
 }
示例#30
0
        public override void EmitAssign(RHC rhc, ObjExpr objx, CljILGen ilg, Expr val)
        {
            GenContext.EmitDebugInfo(ilg, _spanMap);

            val.Emit(RHC.Expression, objx, ilg);
            ilg.Emit(OpCodes.Dup);
            HostExpr.EmitUnboxArg(objx, ilg, FieldType);
            ilg.EmitPropertySet(_tinfo);
            if (rhc == RHC.Statement)
            {
                ilg.Emit(OpCodes.Pop);
            }
        }
示例#31
0
 public override Expression GenCodeUnboxed(RHC rhc, ObjExpr objx, GenContext context)
 {
     if (_method != null)
     {
         Expression call = GenDlrForMethod(objx, context);
         call = Compiler.MaybeAddDebugInfo(call, _spanMap, context.IsDebuggable);
         return(call);
     }
     else
     {
         throw new InvalidOperationException("Unboxed emit of unknown member.");
     }
 }
示例#32
0
        // TODO: remove duplicate code between GenProtoLight and GenProtoFull

        private Expression GenProtoLight(RHC rhc, ObjExpr objx, GenContext context, Expression fn)
        {
            Var  v = ((VarExpr)_fexpr).Var;
            Expr e = (Expr)_args.nth(0);

            ParameterExpression targetParam     = Expression.Parameter(typeof(Object), "target");
            ParameterExpression targetTypeParam = Expression.Parameter(typeof(Type), "targetType");
            ParameterExpression vpfnParam       = Expression.Parameter(typeof(AFunction), "vpfn");
            //ParameterExpression thisParam = objx.ThisParam;

            Expression targetParamAssign     = Expression.Assign(targetParam, Compiler.MaybeBox(e.GenCode(RHC.Expression, objx, context)));
            Expression targetTypeParamAssign =
                Expression.Assign(
                    targetTypeParam,
                    Expression.Call(null, Compiler.Method_Util_classOf, targetParam));

            Expression vpfnParamAssign =
                Expression.Assign(
                    vpfnParam,
                    Expression.Convert(Expression.Call(objx.GenVar(context, v), Compiler.Method_Var_getRawRoot), typeof(AFunction)));

            if (_protocolOn == null)
            {
                return(Expression.Block(
                           new ParameterExpression[] { targetParam, targetTypeParam, vpfnParam },
                           targetParamAssign,
                           targetTypeParamAssign,
                           vpfnParamAssign,
                           GenerateArgsAndCall(rhc, objx, context, vpfnParam, targetParam)));
            }
            else
            {
                Expression[] args = new Expression[_args.count() - 1];
                for (int i = 1; i < _args.count(); i++)
                {
                    Expression bare = ((Expr)_args.nth(i)).GenCode(RHC.Expression, objx, context);
                    args[i - 1] = Compiler.MaybeBox(bare);
                }

                return(Expression.Block(
                           new ParameterExpression[] { targetParam, targetTypeParam, vpfnParam },
                           targetParamAssign,
                           targetTypeParamAssign,
                           Expression.Condition(
                               Expression.Not(Expression.TypeIs(targetParam, _protocolOn)),
                               Expression.Block(
                                   vpfnParamAssign,
                                   GenerateArgsAndCall(rhc, objx, context, vpfnParam, targetParam)),
                               Compiler.MaybeBox(Expression.Call(Expression.Convert(targetParam, _protocolOn), _onMethod, args)))));
            }
        }
示例#33
0
        public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            objx.EmitVar(ilg, _var);

            if ( _shadowsCoreMapping )
            {
                LocalBuilder locNs = ilg.DeclareLocal(typeof(Namespace));
                GenContext.SetLocalName(locNs, "ns");

                ilg.Emit(OpCodes.Dup);
                ilg.EmitFieldGet(VarNsFI);
                ilg.Emit(OpCodes.Stloc,locNs);

                LocalBuilder locSym = ilg.DeclareLocal(typeof(Symbol));
                GenContext.SetLocalName(locSym, "sym");

                ilg.Emit(OpCodes.Dup);
                ilg.EmitFieldGet(VarSymFI);
                ilg.Emit(OpCodes.Stloc, locSym);

                ilg.Emit(OpCodes.Ldloc, locNs);
                ilg.Emit(OpCodes.Ldloc, locSym);
                ilg.Emit(OpCodes.Call, NamespaceReferMI);
            }

            if (_isDynamic)
            {
                ilg.Emit(OpCodes.Call, Compiler.Method_Var_setDynamic0);
            }
            if (_meta != null)
            {
                if (_initProvided || true) //IncludesExplicitMetadata((MapExpr)_meta))
                {
                    ilg.Emit(OpCodes.Dup);
                    _meta.Emit(RHC.Expression, objx, ilg);
                    ilg.Emit(OpCodes.Castclass, typeof(IPersistentMap));
                    ilg.Emit(OpCodes.Call, Compiler.Method_Var_setMeta);
                }
            }
            if (_initProvided)
            {
                ilg.Emit(OpCodes.Dup);
                if (_init is FnExpr)
                    ((FnExpr)_init).EmitForDefn(objx, ilg);
                else
                    _init.Emit(RHC.Expression, objx, ilg);
                ilg.Emit(OpCodes.Call,Compiler.Method_Var_bindRoot);
            }
            if (rhc == RHC.Statement)
                ilg.Emit(OpCodes.Pop);
        }
示例#34
0
        void GenerateMethod(ObjExpr objx, GenContext context)
        {
            if (Prim != null)
            {
                GeneratePrimMethod(objx, context);
            }

            TypeBuilder tb = objx.TypeBuilder;

            MethodBuilder mb = tb.DefineMethod(MethodName, MethodAttributes.ReuseSlot | MethodAttributes.Public | MethodAttributes.Virtual, ReturnType, ArgTypes);


            //Console.Write("InMd: {0} {1}(", ReturnType.Name, MethodName);
            //foreach (Type t in ArgTypes)
            //    Console.Write("{0}", t.Name);
            //Console.WriteLine(")");

            GenInterface.SetCustomAttributes(mb, _methodMeta);
            if (_parms != null)
            {
                for (int i = 0; i < _parms.count(); i++)
                {
                    IPersistentMap meta = GenInterface.ExtractAttributes(RT.meta(_parms.nth(i)));
                    if (meta != null && meta.count() > 0)
                    {
                        ParameterBuilder pb = mb.DefineParameter(i + 1, ParameterAttributes.None, ((Symbol)_parms.nth(i)).Name);
                        GenInterface.SetCustomAttributes(pb, meta);
                    }
                }
            }

            ILGen gen = new ILGen(mb.GetILGenerator());

            gen.EmitLoadArg(0);
            for (int i = 1; i <= _argLocals.count(); i++)
            {
                gen.EmitLoadArg(i);
            }
            gen.EmitCall(_staticMethodBuilder);
            if (ReturnType != StaticReturnType)
            {
                gen.Emit(OpCodes.Castclass, ReturnType);
            }
            gen.Emit(OpCodes.Ret);

            if (IsExplicit)
            {
                tb.DefineMethodOverride(mb, _explicitMethodInfo);
            }
        }
示例#35
0
        // TODO: PRIORITY: IMPLEMENT protocolOn

        private Expression GenProto(RHC rhc, ObjExpr objx, GenContext context, Expression fn)
        {
            switch (objx.FnMode)
            {
            case FnMode.Light:
                return(GenProtoLight(rhc, objx, context, fn));

            case FnMode.Full:
                return(GenProtoFull(rhc, objx, context, fn));

            default:
                throw Util.UnreachableCode();
            }
        }
示例#36
0
        void DoEmit(RHC rhc, ObjExpr objx, CljILGen ilg, bool emitUnboxed)
        {
            List<LocalBuilder> locals = new List<LocalBuilder>();

            for (int i = 0; i < _bindingInits.count(); i++)
            {
                BindingInit bi = (BindingInit)_bindingInits.nth(i);
                Type primType = Compiler.MaybePrimitiveType(bi.Init);
                if (primType != null)
                {
                    LocalBuilder local = ilg.DeclareLocal(primType);
                    locals.Add(local);
                    GenContext.SetLocalName(local, bi.Binding.Name);
                    bi.Binding.LocalVar = local;

                    ((MaybePrimitiveExpr)bi.Init).EmitUnboxed(RHC.Expression, objx, ilg);
                    ilg.Emit(OpCodes.Stloc, local);
                }
                else
                {
                    LocalBuilder local = ilg.DeclareLocal(typeof(Object));
                    locals.Add(local);
                    GenContext.SetLocalName(local, bi.Binding.Name);
                    bi.Binding.LocalVar = local;

                    bi.Init.Emit(RHC.Expression, objx, ilg);
                    ilg.Emit(OpCodes.Stloc, local);
                }
             }

            Label loopLabel = ilg.DefineLabel();
            ilg.MarkLabel(loopLabel);

            try
            {
                if (_isLoop)
                    Var.pushThreadBindings(PersistentHashMap.create(Compiler.LoopLabelVar, loopLabel));

                if (emitUnboxed)
                    ((MaybePrimitiveExpr)_body).EmitUnboxed(rhc, objx, ilg);
                else
                    _body.Emit(rhc, objx, ilg);
            }
            finally
            {
                if (_isLoop)
                    Var.popThreadBindings();
            }
        }
示例#37
0
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     if (_coll is IPersistentList)
         ilg.EmitFieldGet(ListEmptyFI);
     else if (_coll is IPersistentVector)
         ilg.EmitFieldGet(VectorEmptyFI);
     else if (_coll is IPersistentMap)
         ilg.EmitFieldGet(HashMapEmptyFI);
     else if (_coll is IPersistentSet)
         ilg.EmitFieldGet(HashSetEmptyFI);
     else
         throw new InvalidOperationException("Unknown collection type.");
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#38
0
 public static void EmitByRefArg(HostArg ha, ObjExpr objx, CljILGen ilg)
 {
     if (ha.LocalBinding.IsArg)
     {
         ilg.Emit(OpCodes.Ldarga, ha.LocalBinding.Index);
     }
     else if (ha.LocalBinding.IsThis)
     {
         ilg.Emit(OpCodes.Ldarga, 0);
     }
     else
     {
         ilg.Emit(OpCodes.Ldloca, ha.LocalBinding.LocalVar);
     }
 }
示例#39
0
 public override void Emit(ObjExpr fn, TypeBuilder tb)
 {
     if (Prim != null)
     {
         DoEmitPrim(fn, tb);
     }
     else if (fn.IsStatic)
     {
         DoEmitStatic(fn, tb);
     }
     else
     {
         DoEmit(fn, tb);
     }
 }
示例#40
0
        internal void EmitIntrinsicPredicate(RHC rhc, ObjExpr objx, CljILGen ilg, Label falseLabel)
        {
            GenContext.EmitDebugInfo(ilg, _spanMap);

            if (_method != null)
            {
                MethodExpr.EmitTypedArgs(objx, ilg, _method.GetParameters(), _args);
                // JVM: clear locals
                Intrinsics.EmitPred(_method, ilg, falseLabel);
            }
            else
            {
                throw new InvalidOperationException("Unboxed emit of unknown member");
            }
        }
示例#41
0
 //static readonly FieldInfo VectorEmptyFI = typeof(PersistentVector).GetField("EMPTY");
 public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
 {
     if (_coll is IPersistentList || _coll is LazySeq) // JVM does not include LazySeq test.  I'm getting it in some places.  LazySeq of 0 size got us here, we'll treat as an empty list
         ilg.EmitFieldGet(ListEmptyFI);
     else if (_coll is IPersistentVector)
         ilg.EmitFieldGet(TupleEmptyFI);
     else if (_coll is IPersistentMap)
         ilg.EmitFieldGet(HashMapEmptyFI);
     else if (_coll is IPersistentSet)
         ilg.EmitFieldGet(HashSetEmptyFI);
     else
         throw new InvalidOperationException("Unknown collection type.");
     if (rhc == RHC.Statement)
         ilg.Emit(OpCodes.Pop);
 }
示例#42
0
        public void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            _expr.Emit(RHC.Expression, objx, ilg);

            // This corresponds to the most general case code in System.Linq.Expressions.Compiler.LambdaCompiler

            Type opType = _expr.HasClrType && _expr.ClrType != null ? _expr.ClrType : typeof(object);
            if (opType.IsValueType)
            {
                ilg.Emit(OpCodes.Box, opType);
            }
            ilg.Emit(OpCodes.Isinst, _t);
            ilg.Emit(OpCodes.Ldnull);
            ilg.Emit(OpCodes.Cgt_Un);
        }
示例#43
0
        public void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            _expr.Emit(RHC.Expression, objx, ilg);

            // This corresponds to the most general case code in System.Linq.Expressions.Compiler.LambdaCompiler

            Type opType = _expr.HasClrType && _expr.ClrType != null ? _expr.ClrType : typeof(object);

            if (opType.IsValueType)
            {
                ilg.Emit(OpCodes.Box, opType);
            }
            ilg.Emit(OpCodes.Isinst, _t);
            ilg.Emit(OpCodes.Ldnull);
            ilg.Emit(OpCodes.Cgt_Un);
        }
示例#44
0
        public Expression GenCodeUnboxed(RHC rhc, ObjExpr objx, GenContext context)
        {
            List <Expression> exprs = new List <Expression>(_exprs.count());

            for (int i = 0; i < _exprs.count() - 1; i++)
            {
                Expr e = (Expr)_exprs.nth(i);
                exprs.Add(e.GenCode(RHC.Statement, objx, context));
            }

            MaybePrimitiveExpr last = (MaybePrimitiveExpr)LastExpr;

            exprs.Add(last.GenCodeUnboxed(rhc, objx, context));

            return(Expression.Block(exprs));
        }
示例#45
0
 void EmitThenForHashes(ObjExpr objx, CljILGen ilg, Expr test, Expr then, Label defaultLabel, bool emitUnboxed)
 {
     _expr.Emit(RHC.Expression, objx, ilg);
     test.Emit(RHC.Expression, objx, ilg);
     if (_testType == _hashIdentityKey)
     {
         ilg.Emit(OpCodes.Ceq);
         ilg.Emit(OpCodes.Brfalse, defaultLabel);
     }
     else
     {
         ilg.Emit(OpCodes.Call, Compiler.Method_Util_equiv);
         ilg.Emit(OpCodes.Brfalse, defaultLabel);
     }
     EmitExpr(objx, ilg, then, emitUnboxed);
 }
示例#46
0
        void EmitProto(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            switch (objx.FnMode)
            {
            case FnMode.Light:
                EmitProtoLight(rhc, objx, ilg);
                break;

            case FnMode.Full:
                EmitProtoFull(rhc, objx, ilg);
                break;

            default:
                throw Util.UnreachableCode();
            }
        }
示例#47
0
        public override Expression GenCodeUnboxed(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression target = _target.GenCode(RHC.Expression, objx, context);

            if (_targetType != null && _tinfo != null)
            {
                Expression convTarget = Expression.Convert(target, _targetType);
                Expression access     = GenAccess(rhc, objx, convTarget);
                access = Compiler.MaybeAddDebugInfo(access, _spanMap, context.IsDebuggable);
                return(access);
            }
            else
            {
                throw new InvalidOperationException("Unboxed emit of unknown member.");
            }
        }
示例#48
0
        internal LambdaExpression GenerateImmediateLambda(RHC rhc, ObjExpr objx, GenContext context)
        {
            List <ParameterExpression> parmExprs = new List <ParameterExpression>(_argLocals.count());

            if (_thisBinding != null)
            {
                _thisBinding.ParamExpression = objx.ThisParam;
            }

            try
            {
                LabelTarget loopLabel = Expression.Label("top");

                Var.pushThreadBindings(RT.map(Compiler.LoopLabelVar, loopLabel, Compiler.MethodVar, this));

                for (int i = 0; i < _argLocals.count(); i++)
                {
                    LocalBinding b = (LocalBinding)_argLocals.nth(i);

                    ParameterExpression pexpr = Expression.Parameter(typeof(object), b.Name);
                    b.ParamExpression = pexpr;
                    parmExprs.Add(pexpr);
                }

                List <Expression> bodyExprs = new List <Expression>();
                //bodyExprs.AddRange(typedParmInitExprs);
                bodyExprs.Add(Expression.Label(loopLabel));
                bodyExprs.Add(Compiler.MaybeBox(_body.GenCode(rhc, objx, context)));


                Expression block;
                //if (typedParmExprs.Count > 0)
                //    block = Expression.Block(typedParmExprs, bodyExprs);
                //else
                block = Expression.Block(bodyExprs);

                return(Expression.Lambda(
                           FuncTypeHelpers.GetFFuncType(parmExprs.Count),
                           block,
                           Objx.ThisName,
                           parmExprs));
            }
            finally
            {
                Var.popThreadBindings();
            }
        }
示例#49
0
        internal static Expression GenTypedArg(ObjExpr objx, GenContext context, Type paramType, Expr arg)
        {
            Type primt = Compiler.MaybePrimitiveType(arg);

            if (primt == paramType)
            {
                Expression expr = ((MaybePrimitiveExpr)arg).GenCodeUnboxed(RHC.Expression, objx, context);
                return(expr);
            }
            else if (primt == typeof(int) && paramType == typeof(long))
            {
                Expression expr = ((MaybePrimitiveExpr)arg).GenCodeUnboxed(RHC.Expression, objx, context);
                expr = Expression.Convert(expr, typeof(long));
                return(expr);
            }
            else if (primt == typeof(long) && paramType == typeof(int))
            {
                Expression expr = ((MaybePrimitiveExpr)arg).GenCodeUnboxed(RHC.Expression, objx, context);
                if (RT.booleanCast(RT.UncheckedMathVar.deref()))
                {
                    expr = Expression.Call(Compiler.Method_RT_uncheckedIntCast_long, expr);
                }
                else
                {
                    expr = Expression.Call(Compiler.Method_RT_intCast_long, expr);
                }
                return(expr);
            }
            else if (primt == typeof(float) && paramType == typeof(double))
            {
                Expression expr = ((MaybePrimitiveExpr)arg).GenCodeUnboxed(RHC.Expression, objx, context);
                expr = Expression.Convert(expr, typeof(double));
                return(expr);
            }
            else if (primt == typeof(double) && paramType == typeof(float))
            {
                Expression expr = ((MaybePrimitiveExpr)arg).GenCodeUnboxed(RHC.Expression, objx, context);
                expr = Expression.Convert(expr, typeof(float));
                return(expr);
            }
            else
            {
                Expression argExpr = arg.GenCode(RHC.Expression, objx, context);
                return(GenUnboxArg(argExpr, paramType));
            }
        }
示例#50
0
        private void EmitTargetExpression(ObjExpr objx, CljILGen ilg)
        {
            if (Compiler.CompileStubOrigClassVar.isBound && Compiler.CompileStubOrigClassVar.deref() != null && objx.TypeBlder != null)
            {
                ilg.Emit(OpCodes.Ldtoken, objx.TypeBlder);
            }
            else if (_type != null)
            {
                ilg.Emit(OpCodes.Ldtoken, _type);
            }
            else
            {
                throw new ArgumentException("Cannot generate type for NewExpr. Serious!");
            }

            ilg.Emit(OpCodes.Call, Compiler.Method_Type_GetTypeFromHandle);
        }
示例#51
0
        private Expression GenTargetExpression(ObjExpr objx, GenContext context)
        {
            if (Compiler.CompileStubOrigClassVar.isBound && Compiler.CompileStubOrigClassVar.deref() != null && objx.TypeBuilder != null)
            {
                return(Expression.Constant(objx.TypeBuilder, typeof(Type)));
            }

            if (_type != null)
            {
                return(Expression.Constant(_type, typeof(Type)));
            }

            throw new ArgumentException("Cannot generate type for NewExpr. Serious!");

            //string name = Compiler.DestubClassName(_type.FullName);
            //return Expression.Call(null, Compiler.Method_RT_classForName, Expression.Constant(name));
        }
示例#52
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Type collType;

            if (_coll is IPersistentList)
                collType = typeof(PersistentList);
            else if (_coll is IPersistentVector)
                collType = typeof(PersistentVector);
            else if (_coll is IPersistentMap)
                collType = typeof(PersistentArrayMap);
            else if (_coll is IPersistentSet)
                collType = typeof(PersistentHashSet);
            else
                throw new InvalidOperationException("Unknown collection type.");

            return Expression.Field(null, collType, "EMPTY");
        }
示例#53
0
        public override void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            Type targetType = _targetType;

            GenContext.EmitDebugInfo(ilg, _spanMap);

            if (targetType != null && _tinfo != null)
            {
                _target.Emit(RHC.Expression, objx, ilg);
                MethodExpr.EmitPrepForCall(ilg, typeof(object), FieldDeclaringType);
                EmitGet(ilg);
            }
            else
            {
                throw new InvalidOperationException("Unboxed emit of unknown member.");
            }
        }
示例#54
0
        public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            GenContext.EmitDebugInfo(ilg, _spanMap);

            if (_ctor != null)
                EmitForMethod(rhc, objx, ilg);
            else if (_isNoArgValueTypeCtor)
                EmitForNoArgValueTypeCtor(rhc, objx, ilg);
            else
            {
                EmitComplexCall(rhc, objx, ilg);
                if (_type.IsValueType)
                    HostExpr.EmitBoxReturn(objx, ilg, _type);
            }

            if (rhc == RHC.Statement)
                ilg.Emit(OpCodes.Pop);
        }
示例#55
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            //if (context.Mode == CompilerMode.Immediate)
            if (objx.FnMode == FnMode.Light )
            {
                // This will emit a plain Keyword reference, rather than a callsite.
                InvokeExpr ie = new InvokeExpr(_source, _spanMap, (Symbol)_tag, _kw, RT.vector(_target));
                return ie.GenCode(rhc, objx, context);
            }
            else
            {

                ParameterExpression thunkParam = Expression.Parameter(typeof(ILookupThunk), "thunk");
                Expression assignThunk = Expression.Assign(thunkParam, Expression.Field(null, objx.ThunkField(_siteIndex)));

                ParameterExpression targetParam = Expression.Parameter(typeof(object), "target");
                Expression assignTarget = Expression.Assign(targetParam,_target.GenCode(RHC.Expression, objx, context));

                ParameterExpression valParam = Expression.Parameter(typeof(Object), "val");
                Expression assignVal = Expression.Assign(valParam, Expression.Call(thunkParam, Compiler.Method_ILookupThunk_get,targetParam));

                ParameterExpression siteParam = Expression.Parameter(typeof(KeywordLookupSite), "site");
                Expression assignSite = Expression.Assign(siteParam, Expression.Field(null, objx.KeywordLookupSiteField(_siteIndex)));

                Expression block =
                    Expression.Block(typeof(Object), new ParameterExpression[] { thunkParam, valParam, targetParam },
                        assignThunk,
                        assignTarget,
                        assignVal,
                        Expression.Condition(
                            Expression.NotEqual(valParam, thunkParam),
                            valParam,
                            Expression.Block(typeof(Object), new ParameterExpression[] { siteParam },
                                assignSite,
                                Expression.Call(siteParam, Compiler.Method_ILookupSite_fault, targetParam, objx.ThisParam)),
                            typeof(object)));

                block = Compiler.MaybeAddDebugInfo(block, _spanMap);
                return block;
            }
        }
示例#56
0
        public override void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            GenContext.EmitDebugInfo(ilg, _spanMap);

            Type retType;

            if (_method != null)
            {
                EmitForMethod(objx, ilg);
                retType = _method.ReturnType;
            }
            else
            {
                EmitComplexCall(objx, ilg);
                retType = typeof(object);
            }
            HostExpr.EmitBoxReturn(objx, ilg, retType);

            if (rhc == RHC.Statement)
                ilg.Emit(OpCodes.Pop);
        }
示例#57
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            Expression basicBody = _tryExpr.GenCode(rhc, objx, context);
            if (basicBody.Type == typeof(void))
                basicBody = Expression.Block(basicBody, Expression.Default(typeof(object)));
            Expression tryBody = Expression.Convert(basicBody,typeof(object));

            CatchBlock[] catches = new CatchBlock[_catchExprs.count()];
            for ( int i=0; i<_catchExprs.count(); i++ )
            {
                CatchClause clause = (CatchClause) _catchExprs.nth(i);
                ParameterExpression parmExpr = Expression.Parameter(clause.Type, clause.Lb.Name);
                clause.Lb.ParamExpression = parmExpr;
                catches[i] = Expression.Catch(parmExpr, Expression.Convert(clause.Handler.GenCode(rhc, objx, context), typeof(object)));
            }

            TryExpression tryStmt = _finallyExpr == null
                ? Expression.TryCatch(tryBody, catches)
                : Expression.TryCatchFinally(tryBody, _finallyExpr.GenCode(RHC.Statement, objx, context), catches);

            return tryStmt;
        }
示例#58
0
        public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            int n = _bindingInits.count();

            // Define our locals
            for (int i = 0; i < n; i++)
            {
                BindingInit bi = (BindingInit)_bindingInits.nth(i);
                LocalBuilder local = ilg.DeclareLocal(typeof(IFn));
                bi.Binding.LocalVar = local;
                ilg.Emit(OpCodes.Ldnull);
                ilg.Emit(OpCodes.Stloc, local);
            }

            // Then initialize

            IPersistentSet lbset = PersistentHashSet.EMPTY;

            for (int i = 0; i < n; i++)
            {
                BindingInit bi = (BindingInit)_bindingInits.nth(i);
                lbset = (IPersistentSet)lbset.cons(bi.Binding);

                bi.Init.Emit(RHC.Expression, objx, ilg);
                ilg.Emit(OpCodes.Stloc,bi.Binding.LocalVar);
            }

            for (int i = 0; i < n; i++)
            {
                BindingInit bi = (BindingInit)_bindingInits.nth(i);
                ObjExpr fe = (ObjExpr)bi.Init;

                ilg.Emit(OpCodes.Ldloc, bi.Binding.LocalVar);
                fe.EmitLetFnInits(ilg, bi.Binding.LocalVar, objx, lbset);
            }

            _body.Emit(rhc, objx, ilg);
        }
示例#59
0
        public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            if (_catchExprs.count() == 0 && _finallyExpr == null)
            {
                // degenerate case
                _tryExpr.Emit(rhc, objx, ilg);
                return;
            }

            LocalBuilder retLocal = ilg.DeclareLocal(typeof(Object));

            ilg.BeginExceptionBlock();
            _tryExpr.Emit(rhc, objx, ilg);
            if (rhc != RHC.Statement)
                ilg.Emit(OpCodes.Stloc, retLocal);

            for (int i = 0; i < _catchExprs.count(); i++)
            {
                CatchClause clause = (CatchClause)_catchExprs.nth(i);
                ilg.BeginCatchBlock(clause.Type);
                // Exception should be on the stack.  Put in clause local
                clause.Lb.LocalVar = ilg.DeclareLocal(clause.Type);
                ilg.Emit(OpCodes.Stloc, clause.Lb.LocalVar);
                clause.Handler.Emit(rhc, objx, ilg);
                if (clause.Handler.HasNormalExit() && rhc != RHC.Statement)
                    ilg.Emit(OpCodes.Stloc, retLocal);
            }

            if (_finallyExpr != null)
            {
                ilg.BeginFinallyBlock();
                _finallyExpr.Emit(RHC.Statement, objx, ilg);
            }
            ilg.EndExceptionBlock();
            if (rhc != RHC.Statement)
                ilg.Emit(OpCodes.Ldloc, retLocal);
        }
示例#60
0
        public Expression GenCode(RHC rhc, ObjExpr objx, GenContext context)
        {
            LabelTarget loopLabel = (LabelTarget)Compiler.LOOP_LABEL.deref();
            if (loopLabel == null)
                throw new InvalidOperationException("Recur not in proper context.");

            int argCount = _args.count();

            List<ParameterExpression> tempVars = new List<ParameterExpression>(argCount);
            List<Expression> tempAssigns = new List<Expression>(argCount);
            List<Expression> finalAssigns = new List<Expression>(argCount);

            // Evaluate all the init forms into local variables.
            for (int i = 0; i < _loopLocals.count(); i++)
            {
                LocalBinding b = (LocalBinding)_loopLocals.nth(i);
                Expr arg = (Expr)_args.nth(i);
                ParameterExpression tempVar = Expression.Parameter(b.ParamExpression.Type, "__local__" + i);
                Expression valExpr = ((Expr)_args.nth(i)).GenCode(RHC.Expression, objx, context);
                tempVars.Add(tempVar);

                if (tempVar.Type == typeof(Object))
                    tempAssigns.Add(Expression.Assign(tempVar, Compiler.MaybeBox(valExpr)));
                else
                    tempAssigns.Add(Expression.Assign(tempVar, Expression.Convert(valExpr, tempVar.Type)));

                finalAssigns.Add(Expression.Assign(b.ParamExpression, tempVar));
            }

            List<Expression> exprs = tempAssigns;
            exprs.AddRange(finalAssigns);
            exprs.Add(Expression.Goto(loopLabel));
            // need to do this to get a return value in the type inferencing -- else can't use this in a then or else clause.
            exprs.Add(Expression.Constant(null));
            return Expression.Block(tempVars, exprs);
        }