Gal*_*axy 3 floating-point printf llvm llvm-ir
我想将浮点变量的值打印到屏幕上。我printf()在 LLVM IR 代码中声明函数,并且它已成功链接。
每当我打印整数或字符数据类型或字符串时,都会printf()像在 C 代码中打印它们一样将它们正常打印到屏幕上。但是,如果我将 a 传递给float,printf()它不会打印浮点数,而是打印0.000000。我检查了多次源代码,语法似乎是正确的。应该是打印的吧2.75!我正在查看这段代码,我完全不明白代码的行为与我编写的代码有何不同。
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@obj1 = global {i32, float, i8} zeroinitializer
@format_string = constant [10 x i8] c"%i %f %c\0A\00"
declare i32 @printf(i8*, ...)
define i32 @main() {
entry:
%obj1 = load {i32, float, i8}, {i32, float, i8}* @obj1
%obj2 = insertvalue {i32, float, i8} %obj1, i32 44, 0
%obj3 = insertvalue {i32, float, i8} %obj2, float 2.75, 1
%obj4 = insertvalue {i32, float, i8} %obj3, i8 36, 2
store {i32, float, i8} %obj4, {i32, float, i8}* @obj1
%ptr.i32 = getelementptr {i32, float, i8}, {i32, float, i8}* @obj1, i32 0, i32 0
%0 = load i32, i32* %ptr.i32
%ptr.float = getelementptr {i32, float, i8}, {i32, float, i8}* @obj1, i32 0, i32 1
%1 = load float, float* %ptr.float
%ptr.i8 = getelementptr {i32, float, i8}, {i32, float, i8}* @obj1, i32 0, i32 2
%2 = load i8, i8* %ptr.i8
%format_ptr = getelementptr [10 x i8], [10 x i8]* @format_string, i64 0, i64 0
call i32 (i8*, ...) @printf(i8* %format_ptr, i32 %0, float %1, i8 %2)
ret i32 0
}
Run Code Online (Sandbox Code Playgroud)
当我编译 LLVM IR 代码时,输出如下:
$ llvm-as code.ll -o code.bc
$ lli code.bc
44 0.000000 $
Run Code Online (Sandbox Code Playgroud)
它成功打印了整数和字符,但没有打印浮点数!
原因是printf可变参数函数和可变参数函数将float参数提升为double. 请参阅为什么 printf() 将浮点型提升为双精度型?
因此,您应该首先将其转换%1为 double,然后再将其传递给printf,这就是 clang 的作用。例如
void f() {
float a = 1;
printf("%f", a);
}
Run Code Online (Sandbox Code Playgroud)
给出
@.str = private unnamed_addr constant [3 x i8] c"%f\00", align 1
define void @f() {
%1 = alloca float, align 4
store float 1.000000e+00, float* %1, align 4
%2 = load float, float* %1, align 4
%3 = fpext float %2 to double
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x
i8], [3 x i8]* @.str, i64 0, i64 0), double %3)
ret void
}
Run Code Online (Sandbox Code Playgroud)
注意使用fpext