[英]LLVM - setting insert point of BasicBlock after adding instructions causes segfault
I'm writing a compiler frontend using LLVM.我正在使用 LLVM 编写编译器前端。 If I create a
BasicBlock
, add some Instructions to it and finally set the insert point, everything work fine.如果我创建一个
BasicBlock
,向它添加一些指令并最后设置插入点,一切正常。 But when I call SetInsertPoint
and THEN add some instruction like this:但是当我调用
SetInsertPoint
然后添加一些这样的指令时:
Function * MainFunction = Function::Create( FT, Function::ExternalLinkage, "main", m_Module );
BasicBlock * BB = BasicBlock::Create( m_Parser->m_Context, "entry", MainFunction );
m_Builder.SetInsertPoint( BB );
CallInst * call = m_Builder.CreateCall( m_Module.getFunction( "writeln" ), {ConstantInt::get( m_Context, APInt( INT_SIZE, 1 ) )}, "calltmp" );
BB->getInstList().push_back( call );
m_Builder.CreateRet( ConstantInt::get( Type::getInt32Ty( m_Context ), 0 ) );
the program generates LLVM IR correctly, but then at the very end (when calling destructors of LLVM module, context and builder?) it gives segfault.该程序正确生成 LLVM IR,但在最后(当调用 LLVM 模块、上下文和构建器的析构函数时?)它给出了段错误。 I would really like to do it this way, because then the functions generating the instructions could refer to
BB
as m_Builder.GetInsertBlock()
.我真的很想这样做,因为生成指令的函数可以将
BB
称为m_Builder.GetInsertBlock()
。 And I can't think of any other way how to implement ifs, nested blocks etc.而且我想不出任何其他方式来实现 ifs、嵌套块等。
Why is it generating code properly and crashing at the end?为什么它会正确生成代码并最终崩溃? Is there a small problem or am I missing something and it just can't be done like this?
是有一个小问题还是我错过了一些东西而不能像这样完成?
m_Builder.SetInsertPoint( BB );
m_Builder.CreateCall( m_Module.getFunction( "writeln" ), {ConstantInt::get( m_Context, APInt( INT_SIZE, 1 ) )}, "calltmp" );
m_Builder.CreateRet( ConstantInt::get( Type::getInt32Ty( m_Context ), 0 ) );
Try this.尝试这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.