我试图打印一个数字,但是我收到错误,说我的打印功能是错误的:
define i32 @main() { entry: %d = shl i32 2, 3 %call = call i32 (i8*, ...)* @printf(i8* %d) ret i32 1 } declare i32 @printf(i8*, ...)这是错误:
Error in compilation: /bin/this.program: llvm.ll:4:44: error: '%d' defined with type 'i8' %call = call i32 (i8*, ...)* @printf(i8* %d) ^是否有其他打印功能修复此问题?
I'm trying to print a number, but I'm getting errors saying my print function is wrong:
define i32 @main() { entry: %d = shl i32 2, 3 %call = call i32 (i8*, ...)* @printf(i8* %d) ret i32 1 } declare i32 @printf(i8*, ...)And here's the error:
Error in compilation: /bin/this.program: llvm.ll:4:44: error: '%d' defined with type 'i8' %call = call i32 (i8*, ...)* @printf(i8* %d) ^Is there some other print function that fixes this?
最满意答案
LLVM IR没有隐式转换(显式转换是单独的指令)。 您的%d变量的类型为i32 ,来自第一条指令(奇怪的是错误消息是'%d' defined with type 'i8' ,可能您的示例不是您的真实代码?)。
至于printf函数,它恰好是C printf 。 你应该传递完全相同的参数 - 一个格式字符串( i8*指向空终止的"%d" )和一个数字。
对于字符串,你应该定义全局
@formatString = private constant [2 x i8] c"%d"并将其作为printf第一个参数传递给:
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @formatString , i32 0, i32 0), i32 %d)完整代码:
@formatString = private constant [2 x i8] c"%d" define i32 @main() { entry: %d = shl i32 2, 3 %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @formatString , i32 0, i32 0), i32 %d) ret i32 1 } declare i32 @printf(i8*, ...)LLVM IR has no implicit casts (and explicit casts are separate instructions). Your %d variable has type i32, from the first instruction (it's odd that error message is '%d' defined with type 'i8', probably your example is not your real code?).
As for printf function, it is exactly C printf. And you should pass exactly the same arguments - a format string (i8* pointing to null terminated "%d"), and a number.
For string, you should define global
@formatString = private constant [2 x i8] c"%d"And pass it as first argument to printf:
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @formatString , i32 0, i32 0), i32 %d)Full code:
@formatString = private constant [2 x i8] c"%d" define i32 @main() { entry: %d = shl i32 2, 3 %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @formatString , i32 0, i32 0), i32 %d) ret i32 1 } declare i32 @printf(i8*, ...)更多推荐
发布评论