【发布时间】:2019-09-27 02:21:16
【问题描述】:
我正在尝试从 c 源代码生成 LLVM 位码和反汇编 (.ll) 代码。我希望位码中的指令具有与源代码相似的变量名称。
假设我有一个源代码(sample.c):
int test(int a){
return a++;
}
sample.ll 包含:
; Function Attrs: noinline nounwind uwtable
define i32 @test(i32) #0 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = add nsw i32 %3, 1
store i32 %4, i32* %2, align 4
ret i32 %3
}
这里,%0 类似于源代码中的变量 a。
如何生成这样的 sample.ll?
; Function Attrs: noinline nounwind
define i32 @test(i32 %a) #0 {
entry:
%a.addr = alloca i32, align 4
store i32 %a, i32* %a.addr, align 4
%0 = load i32, i32* %a.addr, align 4
%inc = add nsw i32 %0, 1
store i32 %inc, i32* %a.addr, align 4
ret i32 %0
}
其中 %a 类似于源代码中的变量 a。 注意:我使用的 clang 版本是 6.0.0-1ubuntu2~16.04.1
我正在使用命令:clang -Xclang -disable-O0-optnone -O0 -emit-llvm -c sample.c -o sample.bc 然后llvm-dis sample.bc
【问题讨论】:
-
clang 的运行情况如何?当我做
clang -cc1 test.c -S -emit-llvm时,我看到了第二个版本 -
我使用的是
clang -Xclang -disable-O0-optnone -O0 -emit-llvm -c sample.c -o sample.bc,然后是llvm-dis sample.bc -
你为什么需要这个?