$ clang -emit-llvm -c sum.c -o sum.bc
$ clang -emit-llvm -S -c sum.c -o sum.ll
$ clang -S sum.c -o sum.asm
一,C源文件
sum.c
int sum(int x, int y){
return x+y;
}
二,clang 14.0.3 生成的 LLVM IR 和 asm程序
1. clang 14.0.3 编译出来的 LLVM IR结果
cat sum.ll
; ModuleID = 'sumPara.c'
source_filename = "sumPara.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @sum(i32 noundef %a, i32 noundef %b) #0 {
entry:
%a.addr = alloca i32, align 4
%b.addr = alloca i32, align 4
store i32 %a, i32* %a.addr, align 4
store i32 %b, i32* %b.addr, align 4
%0 = load i32, i32* %a.addr, align 4
%1 = load i32, i32* %b.addr, align 4
%add = add nsw i32 %0, %1
ret i32 %add
}
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 1}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{!"clang version 14.0.3"}
加注释:
// 以;开始的行,是注释行,直到行尾;
// 以@开头的标识符,是全局标识符; 可能是函数,也可能是全局变量;
// 以%开头的标识符,是局部标识符; 即局部变量的名称,也就是寄存器变量或C语言中的内存变量,其前边是类型;
// 函数的函数体在IR中,是基本块; 基本块以entry:开始, 以ret i32 %add 结束, 表示返回一个int32的局部变量add的值;
// i32 表示整型,即 c语言的int型; i是int,32是32bit;
// align 4 表示 4 字节对齐;
// alloca 是分配堆栈内存的指令,生命周期结束时会自动释放;
// store 是将数据写入局部变量、寄存器变量或叫做内存变量;
// load 是加载数据;
; ModuleID = 'sumPara.c' //注释行
source_filename = "sumPara.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: noinline nounwind optnone uwtable //注释行
define dso_local i32 @sum(i32 noundef %a, i32 noundef %b) #0 { //@sum 是全局函数; %a是一个局部变量,即寄存器,类型是 i32,int32;
entry:
%a.addr = alloca i32, align 4
%b.addr = alloca i32, align 4
store i32 %a, i32* %a.addr, align 4
store i32 %b, i32* %b.addr, align 4
%0 = load i32, i32* %a.addr, align 4
%1 = load i32, i32* %b.addr, align 4
%add = add nsw i32 %0, %1
ret i32 %add
}
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 1}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{!"clang version 14.0.3"}
2. clang 14.0.3 编译出来的 asm
.text
.file "sumPara.c"
.globl sum # -- Begin function sum
.p2align 4, 0x90
.type sum,@function
sum: # @sum
.cfi_startproc
# %bb.0: # %entry
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
addl -8(%rbp), %eax
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size sum, .Lfunc_end0-sum
.cfi_endproc
# -- End function
.ident "clang version 14.0.3"
.section ".note.GNU-stack","",@progbits
.addrsig
三,clang 7.0.0 生成的 LLVM IR 和 asm程序
这个第三部分整体可以忽略,只为增加历史感。
1. clang 7.0.0 编译出来的 LLVM IR结果
cat sum.ll
//$ cat sum.ll
; ModuleID = 'sum.c'
source_filename = "sum.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @sum(i32, i32) #0 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
store i32 %0, i32* %3, align 4
store i32 %1, i32* %4, align 4
%5 = load i32, i32* %3, align 4
%6 = load i32, i32* %4, align 4
%7 = add nsw i32 %5, %6
ret i32 %7
}
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.0.0 (tags/RELEASE_700/final)"}
2. clang 7.0.0 编译出来的 asm
//$ cat sum.asm
.text
.file "sum.c"
.globl sum # -- Begin function sum
.p2align 4, 0x90
.type sum,@function
sum: # @sum
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %esi
addl -8(%rbp), %esi
movl %esi, %eax
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size sum, .Lfunc_end0-sum
.cfi_endproc
# -- End function
.ident "clang version 7.0.0 (tags/RELEASE_700/final)"
.section ".note.GNU-stack","",@progbits
.addrsig
.addrsig_sym sum
四,参考资料
1,阅读 LLVM IR的文档
先入个门,再详读官方文档
LLVM Language Reference Manual — LLVM 15.0.0git documentationhttps://llvm.org/docs/LangRef.html
https://llvm.liuxfe.comhttps://llvm.liuxfe.com/
2,阅读LLVM编译出来的 x86 汇编语言辅助文档
其中目标机器的汇编语言格式,是llvm特定的规范组织而成的文件。这样可以由llvm中的工具llvm-mc 来处理机器汇编语言文件,翻译成 机器语言的目标文件。
规范说明文档:
LLVM Extensions — LLVM 15.0.0git documentationhttps://llvm.org/docs/Extensions.html