SetInsertPoint() public method

public SetInsertPoint ( BasicBlock bb ) : void
bb BasicBlock
return void
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)
        {
            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);

                return func;
            }

            // Error reading body, remove function.
            func.Delete();
            return null;
        }
Example #3
0
        public override Value CodeGen(IRBuilder builder)
        {
            Value startV = this.Start.CodeGen(builder);
            if(startV.IsNull) return startV;

            BasicBlock startBlock = builder.GetInsertPoint();
            Function func = startBlock.GetParent();

            BasicBlock loopBB = func.AppendBasicBlock("loop");
            builder.BuildBr(loopBB);
            builder.SetInsertPoint(loopBB);

            Value variable = builder.BuildPhi(TypeRef.CreateDouble(), this.VarName, new PhiIncoming(startV, startBlock));

            /* Within the loop, the variable is defined equal to the PHI node. If it
            * shadows an existing variable, we have to restore it, so save it
            * now. */
            Value oldVal = Value.Null;
            CodeGenManager.NamedValues.TryGetValue(this.VarName, out oldVal);
            CodeGenManager.NamedValues[this.VarName] = variable;

            /* Emit the body of the loop.  This, like any other expr, can change the
            * current BB.  Note that we ignore the value computed by the body, but
            * don't allow an error */
            Body.CodeGen(builder);

            // Emit the step value;
            Value stepV = Value.Null;

            if(this.Step != null)
                stepV = this.Step.CodeGen(builder);
            else
                stepV = Value.CreateConstDouble(1);

            Value nextVar = builder.BuildFAdd(variable, stepV, "nextvar");

            // Compute the end condition
            Value endCond = this.End.CodeGen(builder);
            endCond = builder.BuildFCmp(endCond, LLVMRealPredicate.RealONE, Value.CreateConstDouble(0), "loopcond");

            BasicBlock loopEndBB = builder.GetInsertPoint();
            BasicBlock afterBB = func.AppendBasicBlock("afterloop");
            builder.BuildCondBr(endCond, loopBB, afterBB);
            builder.SetInsertPoint(afterBB);

            builder.AddPhiIncoming(variable, nextVar, loopEndBB);

            if(!oldVal.IsNull)
                CodeGenManager.NamedValues[this.VarName] = oldVal;

            return Value.CreateConstDouble(0);
        }
Example #4
0
        public override Value CodeGen(IRBuilder builder)
        {
            Value condV = this.Cond.CodeGen(builder);
            if(condV.IsNull) return condV;

            condV = builder.BuildFCmp(condV, LLVMRealPredicate.RealONE, 
                                      Value.CreateConstDouble(0));
            
            BasicBlock startBlock = builder.GetInsertPoint();
            Function func = startBlock.GetParent();

            BasicBlock thenBB = func.AppendBasicBlock("then");
            builder.SetInsertPoint(thenBB);

            Value thenV = this.Then.CodeGen(builder);
            if(thenV.IsNull) return thenV;
      
            /* Codegen of 'then' can change the current block, update then_bb for the
            * phi. We create a new name because one is used for the phi node, and the
            * other is used for the conditional branch. */
            BasicBlock newThenBB = builder.GetInsertPoint();

            // Emit else block
            BasicBlock elseBB = func.AppendBasicBlock("else");
            func.AppendBasicBlock(elseBB);
            builder.SetInsertPoint(elseBB);

            Value elseV = this.Else.CodeGen(builder);
            if(elseV.IsNull) return elseV;

            // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
            BasicBlock newElseBB = builder.GetInsertPoint();

            // Emit merge block
            BasicBlock mergeBB = func.AppendBasicBlock("ifcont");
            func.AppendBasicBlock(mergeBB);
            builder.SetInsertPoint(mergeBB);

            PhiIncoming incoming = new PhiIncoming();
            incoming.Add(thenV, thenBB);
            incoming.Add(elseV, elseBB);
            Value phi = builder.BuildPhi(TypeRef.CreateDouble(), "iftmp", incoming);

            builder.SetInsertPoint(startBlock);
            builder.BuildCondBr(condV, thenBB, elseBB);

            builder.SetInsertPoint(thenBB);
            builder.BuildBr(mergeBB);

            builder.SetInsertPoint(elseBB);
            builder.BuildBr(mergeBB);

            builder.SetInsertPoint(mergeBB);

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

            // If this is an operator, install it.
            if(this.Proto.IsBinaryOp)
                CodeGenManager.BinopPrecendence[Proto.OperatorName] = Proto.Precedence;

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

            Proto.CreateArgAllocas(func, builder);

            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;
        }
Example #6
0
        public override Value CodeGen(IRBuilder builder)
        {
            // Output this as:
            //   var = alloca double
            //   ...
            //   start = startexpr
            //   store start -> var
            //   goto loop
            // loop: 
            //   ...
            //   bodyexpr
            //   ...
            // loopend:
            //   step = stepexpr
            //   endcond = endexpr
            //
            //   curvar = load var
            //   nextvar = curvar + step
            //   store nextvar -> var
            //   br endcond, loop, endloop
            // outloop:

            BasicBlock startBlock = builder.GetInsertPoint();
            Function func = startBlock.GetParent();

            Value alloca = builder.BuildEntryBlockAlloca(func, TypeRef.CreateDouble(), this.VarName);

            Value startV = this.Start.CodeGen(builder);
            if(startV.IsNull) return startV;

            builder.BuildStore(startV, alloca);

            BasicBlock loopBB = func.AppendBasicBlock("loop");
            builder.BuildBr(loopBB);
            builder.SetInsertPoint(loopBB);

            /* Within the loop, the variable is defined equal to the PHI node. If it
            * shadows an existing variable, we have to restore it, so save it
            * now. */
            Value oldVal = Value.Null;
            CodeGenManager.NamedValues.TryGetValue(this.VarName, out oldVal);
            CodeGenManager.NamedValues[this.VarName] = alloca;

            /* Emit the body of the loop.  This, like any other expr, can change the
            * current BB.  Note that we ignore the value computed by the body, but
            * don't allow an error */
            Body.CodeGen(builder);

            // Emit the step value;
            Value stepV = Value.Null;

            if(this.Step != null)
                stepV = this.Step.CodeGen(builder);
            else
                stepV = Value.CreateConstDouble(1);

            // Compute the end condition
            Value endCond = this.End.CodeGen(builder);
            endCond = builder.BuildFCmp(endCond, LLVMRealPredicate.RealONE, Value.CreateConstDouble(0), "loopcond");

            Value curvar = builder.BuildLoad(alloca, VarName);
            Value nextVar = builder.BuildFAdd(curvar, stepV, "nextvar");
            builder.BuildStore(nextVar, alloca);

            BasicBlock loopEndBB = builder.GetInsertPoint();
            BasicBlock afterBB = func.AppendBasicBlock("afterloop");
            builder.BuildCondBr(endCond, loopBB, afterBB);
            builder.SetInsertPoint(afterBB);

            if(!oldVal.IsNull)
                CodeGenManager.NamedValues[this.VarName] = oldVal;
            else
                CodeGenManager.NamedValues.Remove(this.VarName);

            return Value.CreateConstDouble(0);
        }