public override CompilerResult VisitProgram(DesignScriptParser.ProgramContext context)
        {
            var success = new LLVMBool(0);

            module  = LLVM.ModuleCreateWithName("DesignScript");
            builder = LLVM.CreateBuilder();

            LLVM.LinkInMCJIT();
            LLVM.InitializeX86TargetInfo();
            LLVM.InitializeX86Target();
            LLVM.InitializeX86TargetMC();

            LLVM.InitializeX86AsmParser();
            LLVM.InitializeX86AsmPrinter();

            LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions {
                NoFramePointerElim = 1
            };

            LLVM.InitializeMCJITCompilerOptions(options);
            if (LLVM.CreateExecutionEngineForModule(out engine, module, out var errorMessage).Value == 1)
            {
                Console.WriteLine(errorMessage);
                return(new NullCompilerResult());
            }

            #region Add optimization passes
            // Create a function pass manager for this engine
            passManager = LLVM.CreateFunctionPassManagerForModule(module);

            // Do simple "peephole" optimizations and bit-twiddling optzns.
            LLVM.AddInstructionCombiningPass(passManager);

            // Reassociate expressions.
            LLVM.AddReassociatePass(passManager);

            // Eliminate Common SubExpressions.
            LLVM.AddGVNPass(passManager);

            // Simplify the control flow graph (deleting unreachable blocks, etc).
            LLVM.AddCFGSimplificationPass(passManager);

            LLVM.InitializeFunctionPassManager(passManager);
            #endregion

            base.VisitProgram(context);

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

            LLVM.DumpModule(module);
            LLVM.DisposeBuilder(builder);
            LLVM.DisposeExecutionEngine(engine);

            return(new NullCompilerResult());
        }
Example #2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="DesignScriptParser.program"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitProgram([NotNull] DesignScriptParser.ProgramContext context)
 {
     return(VisitChildren(context));
 }