LLVM红外打印一个号码(LLVM IR printing a number)

编程入门 行业动态 更新时间:2024-10-22 10:53:39
LLVM红外打印一个号码(LLVM IR printing a number)

我试图打印一个数字,但是我收到错误,说我的打印功能是错误的:

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*, ...)

更多推荐

本文发布于:2023-07-24 10:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:号码   LLVM   IR   number   printing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!