public object Interpret(object exp)
        {
            IASTNode node = ASTCompiler.Compile(null, ListProcess.TransformLibraryForms(exp));

            ReserveGlobalVaraible(SymbolTable.GetGlobalSymbolCount());
            ASTNode_Lambda lambdaNode = new ASTNode_Lambda {
                bodyNode    = node, bodyFreeAddresses = new List <FreeAddress>(), childrenFreeAddresses = new List <FreeAddress>(),
                formalCount = 0, locals = new List <string>(),
            };

            var fileName        = "____test.exe";
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName(GenernateUniqueString("assemly")), AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder  = assemblyBuilder.DefineDynamicModule(GenernateUniqueString("module"), fileName, HasSymbolInfo());
            var envTypeBuilder = moduleBuilder.DefineType(GenernateUniqueString("class"));

            var methodBuilder = new ASTNodeVisitor_JITCompiler(null, envTypeBuilder, lambdaNode).MethodBuilder;

            var type = envTypeBuilder.CreateType();

            assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);
            // assemblyBuilder.Save(fileName);

            return(((Func <object>)type.GetMethod(methodBuilder.Name).CreateDelegate(typeof(Func <object>)))());
        }
 public void Visit(ASTNode_Lambda node)
 {
     Value = (Procedure)(actuals => {
         Env newEnv = new Env(mEnv, node.locals.Count);
         for (int i = 0; i < actuals.Count; ++i)
         {
             Env.SetVar(newEnv, new LocalAddress {
                 index = i
             }, actuals[i]);
         }
         return(new ASTNodeVisitor_Interpreter(newEnv, node.bodyNode).Value);
     });
 }
            public ASTNodeVisitor_JITCompiler(TypeBuilder parentTypeBuilder, ASTNode_Lambda node)
            {
                mLambdaNode = node;
                TypeBuilder = parentTypeBuilder.DefineNestedType(JITInterpreter_DS2.Instance().GenernateUniqueString("closure"), TypeAttributes.NestedPublic);

                ConstructorBuilder = TypeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

                FieldBuilders = new Dictionary <FreeAddress, FieldBuilder>();
                foreach (var address in mLambdaNode.GetFreeAddresses())
                {
                    FieldBuilders[address] = TypeBuilder.DefineField(address.ToString(), typeof(SharedValue), FieldAttributes.Public);
                }

                if (HasThisArgument())
                {
                    MethodBuilder = TypeBuilder.DefineMethod(
                        "Invoke",
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }
                else
                {
                    MethodBuilder = TypeBuilder.DefineMethod(
                        "Invoke",
                        MethodAttributes.Static | MethodAttributes.Public,
                        CallingConventions.Standard,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }


                mILGenerator = MethodBuilder.GetILGenerator();

                DeclareLocals();

                mTaillCallFlags.Push(true);
                mLambdaNode.bodyNode.AcceptVisitor(this);
                mTaillCallFlags.Pop();

                mILGenerator.Emit(OpCodes.Ret);

                TypeBuilder.CreateType();
            }
            public ASTNodeVisitor_JITCompiler(ASTNodeVisitor_JITCompiler parent, TypeBuilder envTypeBuilder, ASTNode_Lambda node)
            {
                mParent         = parent;
                mEnvTypeBuilder = envTypeBuilder;
                mLambdaNode     = node;

                mHeapEnvTypeBuilder = envTypeBuilder.DefineNestedType(JITInterpreter_DS.Instance().GenernateUniqueString("nested_class"));

                if (HasThisArgument())
                {
                    MethodBuilder = envTypeBuilder.DefineMethod(
                        JITInterpreter_DS.Instance().GenernateUniqueString("method"),
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }
                else
                {
                    MethodBuilder = envTypeBuilder.DefineMethod(
                        JITInterpreter_DS.Instance().GenernateUniqueString("static_method"),
                        MethodAttributes.Static | MethodAttributes.Public,
                        CallingConventions.Standard,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }


                mILGenerator = MethodBuilder.GetILGenerator();

                DeclareArguments();
                DeclareLocals();
                EmitInitHeapEnv();

                mTailCallFlags.Push(true);
                mLambdaNode.bodyNode.AcceptVisitor(this);
                mTailCallFlags.Pop();

                mILGenerator.Emit(OpCodes.Ret);
                mHeapEnvTypeBuilder.CreateType();
            }
            public void Visit(ASTNode_Lambda node)
            {
                var methodBuilder = new ASTNodeVisitor_JITCompiler(this, mHeapEnvTypeBuilder, node).MethodBuilder;

                if (methodBuilder.IsStatic)
                {
                    mILGenerator.Emit(OpCodes.Ldnull);
                    mILGenerator.Emit(OpCodes.Ldftn, methodBuilder);
                    mILGenerator.Emit(OpCodes.Newobj,
                                      JITInterpreter_DS.Instance().GetDelegateType(node.formalCount).
                                      GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                }
                else
                {
                    JITInterpreter_DS.EmitLoadLocal(mILGenerator, mHeapEnvBuilder);
                    mILGenerator.Emit(OpCodes.Ldftn, methodBuilder);
                    mILGenerator.Emit(OpCodes.Newobj,
                                      JITInterpreter_DS.Instance().GetDelegateType(node.formalCount)
                                      .GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                }
            }
            public void Visit(ASTNode_Lambda node)
            {
                var childCompiler = new ASTNodeVisitor_JITCompiler(TypeBuilder, node);

                if (childCompiler.MethodBuilder.IsStatic)
                {
                    mILGenerator.Emit(OpCodes.Ldnull);
                    mILGenerator.Emit(OpCodes.Ldftn, childCompiler.MethodBuilder);
                    mILGenerator.Emit(OpCodes.Newobj,
                                      JITInterpreter_DS2.Instance().GetDelegateType(node.formalCount).
                                      GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                }
                else
                {
                    mILGenerator.Emit(OpCodes.Newobj, childCompiler.ConstructorBuilder);

                    foreach (var address in node.GetFreeAddresses())
                    {
                        mILGenerator.Emit(OpCodes.Dup);
                        if (address.envIndex == 1)
                        {
                            JITInterpreter_DS2.EmitLoadLocal(mILGenerator, (LocalBuilder)mLocalBuilders[address.index]);
                        }
                        else
                        {
                            JITInterpreter_DS2.EmitLoadThis(mILGenerator);
                            mILGenerator.Emit(OpCodes.Ldfld, FieldBuilders[address.GetOuterAddress()]);
                        }
                        mILGenerator.Emit(OpCodes.Stfld, childCompiler.FieldBuilders[address]);
                    }

                    mILGenerator.Emit(OpCodes.Ldftn, childCompiler.MethodBuilder);
                    mILGenerator.Emit(OpCodes.Newobj,
                                      JITInterpreter_DS2.Instance().GetDelegateType(node.formalCount)
                                      .GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                }
            }
Beispiel #7
0
 public void Visit(ASTNode_Lambda node)
 {
     Value = (Procedure)(actuals => {
         Env newEnv = new Env(mEnv, node.locals.Count);
         for (int i = 0; i < actuals.Count; ++i) {
             Env.SetVar(newEnv, new LocalAddress { index = i }, actuals[i]);
         }
         return new ASTNodeVisitor_Interpreter(newEnv, node.bodyNode).Value;
     });
 }
Beispiel #8
0
 public void Visit(ASTNode_Lambda node)
 {
     ChildrenFreeAddresses.UnionWith(node.GetFreeAddresses().Select(
                                         address => address.GetOuterAddress()));
 }
            public void Visit(ASTNode_Lambda node)
            {
                var childCompiler = new ASTNodeVisitor_JITCompiler(TypeBuilder, node);
                if (childCompiler.MethodBuilder.IsStatic) {
                    mILGenerator.Emit(OpCodes.Ldnull);
                    mILGenerator.Emit(OpCodes.Ldftn, childCompiler.MethodBuilder);
                    mILGenerator.Emit(OpCodes.Newobj,
                        JITInterpreter_DS2.Instance().GetDelegateType(node.formalCount).
                        GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                } else {
                    mILGenerator.Emit(OpCodes.Newobj, childCompiler.ConstructorBuilder);

                    foreach (var address in node.GetFreeAddresses()) {
                        mILGenerator.Emit(OpCodes.Dup);
                        if (address.envIndex == 1) {
                            JITInterpreter_DS2.EmitLoadLocal(mILGenerator, (LocalBuilder)mLocalBuilders[address.index]);
                        } else {
                            JITInterpreter_DS2.EmitLoadThis(mILGenerator);
                            mILGenerator.Emit(OpCodes.Ldfld, FieldBuilders[address.GetOuterAddress()]);
                        }
                        mILGenerator.Emit(OpCodes.Stfld, childCompiler.FieldBuilders[address]);
                    }

                    mILGenerator.Emit(OpCodes.Ldftn, childCompiler.MethodBuilder);
                    mILGenerator.Emit(OpCodes.Newobj,
                        JITInterpreter_DS2.Instance().GetDelegateType(node.formalCount)
                        .GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
                }
            }
            public ASTNodeVisitor_JITCompiler(TypeBuilder parentTypeBuilder, ASTNode_Lambda node)
            {
                mLambdaNode = node;
                TypeBuilder = parentTypeBuilder.DefineNestedType(JITInterpreter_DS2.Instance().GenernateUniqueString("closure"), TypeAttributes.NestedPublic);

                ConstructorBuilder = TypeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

                FieldBuilders = new Dictionary<FreeAddress, FieldBuilder>();
                foreach (var address in mLambdaNode.GetFreeAddresses()) {
                    FieldBuilders[address] = TypeBuilder.DefineField(address.ToString(), typeof(SharedValue), FieldAttributes.Public);
                }

                if (HasThisArgument()) {
                    MethodBuilder = TypeBuilder.DefineMethod(
                        "Invoke",
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                } else {
                    MethodBuilder = TypeBuilder.DefineMethod(
                        "Invoke",
                        MethodAttributes.Static | MethodAttributes.Public,
                        CallingConventions.Standard,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }

                mILGenerator = MethodBuilder.GetILGenerator();

                DeclareLocals();

                mTaillCallFlags.Push(true);
                mLambdaNode.bodyNode.AcceptVisitor(this);
                mTaillCallFlags.Pop();

                mILGenerator.Emit(OpCodes.Ret);

                TypeBuilder.CreateType();
            }
        public object Interpret(object exp)
        {
            IASTNode node = ASTCompiler.Compile(null, ListProcess.TransformLibraryForms(exp));
            ReserveGlobalVaraible(SymbolTable.GetGlobalSymbolCount());
            ASTNode_Lambda lambdaNode = new ASTNode_Lambda {
                bodyNode = node, bodyFreeAddresses = new List<FreeAddress>(), childrenFreeAddresses = new List<FreeAddress>(),
                formalCount=0, locals=new List<string>(),
            };

            var fileName = "____test.exe";
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName(GenernateUniqueString("assemly")), AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(GenernateUniqueString("module"), fileName, HasSymbolInfo());

            var mainTypeBuilder = moduleBuilder.DefineType(GenernateUniqueString("class"));
            var compiler = new ASTNodeVisitor_JITCompiler(mainTypeBuilder, lambdaNode);
            var type = mainTypeBuilder.CreateType();

            assemblyBuilder.SetEntryPoint(compiler.MethodBuilder, PEFileKinds.ConsoleApplication);
            // assemblyBuilder.Save(fileName);

            return ((Func<object>)type.GetNestedType(compiler.TypeBuilder.Name).GetMethod(compiler.MethodBuilder.Name)
                .CreateDelegate(typeof(Func<object>)))();
        }
Beispiel #12
0
 public void Visit(ASTNode_Lambda node)
 {
     ChildrenFreeAddresses.UnionWith(node.GetFreeAddresses().Select(
         address => address.GetOuterAddress()));
 }
 public void Visit(ASTNode_Lambda node)
 {
     var methodBuilder = new ASTNodeVisitor_JITCompiler(this, mHeapEnvTypeBuilder, node).MethodBuilder;
     if (methodBuilder.IsStatic) {
         mILGenerator.Emit(OpCodes.Ldnull);
         mILGenerator.Emit(OpCodes.Ldftn, methodBuilder);
         mILGenerator.Emit(OpCodes.Newobj,
             JITInterpreter_DS.Instance().GetDelegateType(node.formalCount).
             GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
     } else {
         JITInterpreter_DS.EmitLoadLocal(mILGenerator, mHeapEnvBuilder);
         mILGenerator.Emit(OpCodes.Ldftn, methodBuilder);
         mILGenerator.Emit(OpCodes.Newobj,
             JITInterpreter_DS.Instance().GetDelegateType(node.formalCount)
             .GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }));
     }
 }
            public ASTNodeVisitor_JITCompiler(ASTNodeVisitor_JITCompiler parent, TypeBuilder envTypeBuilder, ASTNode_Lambda node)
            {
                mParent = parent;
                mEnvTypeBuilder = envTypeBuilder;
                mLambdaNode = node;

                mHeapEnvTypeBuilder = envTypeBuilder.DefineNestedType(JITInterpreter_DS.Instance().GenernateUniqueString("nested_class"));

                if (HasThisArgument()) {
                    MethodBuilder = envTypeBuilder.DefineMethod(
                        JITInterpreter_DS.Instance().GenernateUniqueString("method"),
                        MethodAttributes.Public,
                        CallingConventions.HasThis,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                } else {
                    MethodBuilder = envTypeBuilder.DefineMethod(
                        JITInterpreter_DS.Instance().GenernateUniqueString("static_method"),
                        MethodAttributes.Static | MethodAttributes.Public,
                        CallingConventions.Standard,
                        typeof(object),
                        Enumerable.Repeat(typeof(object), mLambdaNode.formalCount).ToArray());
                }

                mILGenerator = MethodBuilder.GetILGenerator();

                DeclareArguments();
                DeclareLocals();
                EmitInitHeapEnv();

                mTailCallFlags.Push(true);
                mLambdaNode.bodyNode.AcceptVisitor(this);
                mTailCallFlags.Pop();

                mILGenerator.Emit(OpCodes.Ret);
                mHeapEnvTypeBuilder.CreateType();
            }