Inheritance: IDisposable, IPointerWrapper
Example #1
0
        static void Main(string[] args)
        {
            var context = new LLVMContext();
            var module = new Module("NoName", context);
            var builder = new IRBuilder(context);
            var t = builder.getInt32Ty();
            var funcType = FunctionType.get(t, false);
            var main = Function.Create(funcType, GlobalValue.LinkageTypes.ExternalLinkage, "main", module);
            var entry = BasicBlock.Create(context, "entrypoint", main);
            builder.SetInsertPoint(entry);

            var hello = builder.CreateGlobalStringPtr("Hello, World!");

            var putsType = FunctionType.get(builder.getInt32Ty(),
                new[] { builder.getInt8Ty().getPointerTo(0) }, false);
            var putsFunc = module.getOrInsertFunction("puts", putsType);

            builder.CreateCall(putsFunc, hello);

            var variable = builder.CreateAlloca(t);
            var val = ConstantInt.get(t, 127);
            builder.CreateStore(val, variable);

            builder.CreateRetVoid();

            var pm = new PassManager();
            var os = new raw_fd_ostream("code.ll");
            pm.add(Passes.createPrintModulePass(os));
            pm.run(module);
            os.close();

            Console.ReadKey();
        }
Example #2
0
        public Function CodeGen(IRBuilder builder, PassManager passManager)
        {
            CodeGenManager.NamedValues.Clear();
            Function func = this.Proto.CodeGen(builder);
            if(func == null)
                return null;

            // Create a new basic block to start insertion into.
            BasicBlock bb = func.AppendBasicBlock("entry");
            builder.SetInsertPoint(bb);
            Value retVal = Body.CodeGen(builder);

            if(!retVal.IsNull)
            {
                builder.BuildReturn(retVal);

                // Validate the generated code, checking for consistency.
                func.Validate(LLVMVerifierFailureAction.PrintMessageAction);

                // Optimize the function.
                passManager.Run(func);

                return func;
            }

            // Error reading body, remove function.
            func.Delete();
            return null;
        }