No Base Blocker generated in llvm

I am new to llvm and only an online tutorial here: http://llvm.org/docs/tutorial/LangImpl1.html Now I wanted to make my own little language and got a little problem. I want to analyze this:

(def i 1) 

He has to do two things:

  • Define a new function that returns 1
  • It returns a value, so it can be used as an expression

The function is created correctly, but I have a problem with using it as an expression. AST looks like this:

 FunctionAST // the whole statement - Prototype // is an nameless statement - Body // contains the definition expression - DefExprAST - Body // contains the Function definition - FunctionAST - Prototype // named i - Body // the value 1 

The code to create the code for the function is as follows:

 Function *FunctionAST::Codegen() { NamedValues.clear(); Function *TheFunction = Proto->Codegen(); if ( TheFunction == 0 ) return 0; BasicBlock *BB = BasicBlock::Create( getGlobalContext(), "entry", TheFunction ); Builder.SetInsertPoint( BB ); if ( Value *RetVal = Body->Codegen() ) { Builder.CreateRet( RetVal ); verifyFunction( *TheFunction ); return TheFunction; } return 0; } 

And DefExprAST like this:

 Value *DefExprAST::Codegen() { if ( Body->Codegen() == 0 ) return 0; return ConstantFP::get( getGlobalContext(), APFloat( 0.0 ) ); } 

verifyFunction gives the following error:

 Basic Block in function '' does not have terminator! label %entry LLVM ERROR: Broken module, no Basic Block terminator! 

Indeed, the generated function does not have a ret entry. Its empty:

 define double @0() { entry: } 

But RetVal correctly populated with double, and Builder.CreateRet( RetVal ) returns a ret statement, but it is not inserted into the record.

+6
source share
1 answer

Sometimes formulating a question and taking a short break helps you solve problems very well. I modified DefExprAST::Codegen to remember the parent block and set it as the insertion point for the return value.

 Value *DefExprAST::Codegen() { BasicBlock *Parent = Builder.GetInsertBlock(); if ( Body->Codegen() == 0 ) return 0; Builder.SetInsertPoint( Parent ); return ConstantFP::get( getGlobalContext(), APFloat( 0.0 ) ); } 
+8
source

All Articles