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 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.
source share