我想为PL / 0语言编写一个简单的前端,词法分析器和解析器似乎运行良好,但在生成调用语句LLVM IR时有一个错误。
这是我为PL / 0调用语句生成IR的代码:CALL ident
。它无法执行Builder.CreateCall(F,None,"calltmp");
控制台说:
pl0: /root/llvm-6.0/lib/IR/Value.cpp:247:
void llvm::Value::setNameImpl(const llvm::Twine&):
Assertion `!getType()->isVoidTy() && "Cannot assign a name to void values!"' failed.
Aborted (core dumped)
TheModule->getFunction()
获得正确的功能。我的实现中的所有函数都有函数类型void ..()
,没有返回值,也没有参数。
我尝试用gdb
调试它;似乎Builder.CreateCall(...)
创建的指令是"优化了"。
这有什么问题?
生成调用语句的代码:
bool CallStatAST::codegen()
{
auto F = TheModule->getFunction(Callee);
if(!F)
logErrorL("Failing to get function from module at CallStatAST::codegen()!\n");
//std::vector<Value*> Args;
Builder.CreateCall(F,None,"calltmp");
return true;
}
gdb
调试信息:
1. (gdb) break 391
Breakpoint 1 at 0x4713a5: file pl0.cpp, line 391.
2. (gdb) run < test1.pl0
Starting program: /root/my_llvm/PL0/pl0 < test1.pl0
3. Breakpoint 1, get (this=optimized out) at /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/bits/unique_ptr.h:234
234 { return std::get<0>(_M_t); }
4. (gdb) n
CallStatAST::codegen (this=0xa165d0) at pl0.cpp:392
392 auto F = TheModule->getFunction(Callee);
5. (gdb) p F->dump()
define internal void @myfun()
{
entry:
%i = alloca double
%a = load double, double* @a
%b = load double, double* @b
%addtmp = fadd double %a, %b
store double %addtmp, double* %i
ret void
}
$1 = void
6. (gdb) p F->getName()
$2 = {static npos = 18446744073709551615, Data = 0xa17310 "myfun", Length = 5}
(gdb) n
397 Builder.CreateCall(F,None,"calltmp");
7. (gdb) s
Twine (Str=0xa17278 "\300P\241", this=optimized out) at /usr/local/include/llvm/ADT/Twine.h:270
270 LHS.cString = Str;
8. (gdb) n
CallStatAST::codegen (this=optimized out) at pl0.cpp:397
397 Builder.CreateCall(F,None,"calltmp");
答案 0 :(得分:2)
我多么傻瓜! 我难以置信的想要为void返回值指定一个名称,没有值以及如何为它设置名称? 转换:
Builder.CreateCall(F,None,"calltmp");
为:
Builder.CreateCall(F,None);
一切都恢复了!