6

我对 llvm 很陌生,只在这里做了在线教程:http: //llvm.org/docs/tutorial/LangImpl1.html 现在我想做我自己的小语言并遇到了一个小问题。我想解析这个:

(def i 1)

它应该做两件事:

  1. 定义一个返回 1 的新函数
  2. 返回一个值,以便它可以用作表达式

该函数已正确创建,但我在将其用作表达式时遇到了问题。AST 看起来像这样:

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

该函数的代码创建代码如下所示:

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;
}

DefExprAST 像这样:

Value *DefExprAST::Codegen() {
  if ( Body->Codegen() == 0 ) return 0;

  return ConstantFP::get( getGlobalContext(), APFloat( 0.0 ) );
}

给出以下verifyFunction错误:

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

事实上,生成的函数没有 ret 条目。它是空的:

define double @0() {
entry:
}

But RetVal is correctly filled with an double and Builder.CreateRet( RetVal ) gives back the ret statement but it doesn't get inserted into the entry.

4

1 回答 1

12

Sometimes formulating an question and taking a little break helps to solve problems very well. I changed the 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 ) );
}
于 2013-04-08T10:38:51.257 回答