Ejemplo n.º 1
0
        public void main(IList <CompilationUnit> trees)
        {
            if (trees.Count == 0)
            {
                return;
            }

            LLVMPassManagerRef passManager = LLVM.CreatePassManager();

            LLVM.AddStripDeadPrototypesPass(passManager);
            LLVM.AddPromoteMemoryToRegisterPass(passManager);
            CompilerNative.AddRewriteStatepointsForGCPass(passManager);
            LLVM.AddEarlyCSEPass(passManager);
            LLVM.AddCFGSimplificationPass(passManager);
            LLVM.AddNewGVNPass(passManager);
            LLVM.AddLateCFGSimplificationPass(passManager);

            context = LLVM.GetGlobalContext();
            module  = LLVM.ModuleCreateWithNameInContext("TheProgram", context);
            builder = context.CreateBuilderInContext();

            foreach ((Type type, ArrayType arrayType) in symtab.arrayTypes)
            {
                if (type.IsPrimitive)
                {
                    declareLLVMStructType(arrayType);
                    createArrayMetadataRecord(arrayType);
                }
            }
            declareBuiltins();
            declareRuntimeHelpers();

            variableAllocator = new VariableAllocator(builder, typeResolver);

            build(trees);

            if (options.DumpIR)
            {
                dumpIR();
            }

            LLVMBool success = new LLVMBool(0);

            if (LLVM.VerifyModule(module, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != success)
            {
                Console.WriteLine($"Verfy Error: {error}");
                return;
            }

            LLVM.RunPassManager(passManager, module);

            if (options.DumpIR)
            {
                dumpIR();
            }

            if (options.Execute)
            {
                mcJit();
            }
            else
            {
                makeObjectFile();
            }

            LLVM.DisposePassManager(passManager);
        }