0%

Output intermediate format for GCC/CLANG

為了很多因素(降低Playform depdent / Optimization等。 GCC 跟 CLANG 都引進了一層中間層,這曾的目的是定義一個平台無關的指令集, 以老朋友hello來示範如何輸出中間產物。

1
2
3
4
5
6
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}

GCC

GCC的中間語言叫做Register transfer language
利用下面的方式產生hello.c.xxxr.expand的檔案。

1
$ gcc -S -fdump-rtl-expand hello.c

輸出結果類似這個樣子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
;; Function main (main, funcdef_no=0, decl_uid=2197, symbol_order=0)


;; Generating RTL for gimple basic block 2

;; Generating RTL for gimple basic block 3


try_optimize_cfg iteration 1

Merging block 3 into block 2...
Merged blocks 2 and 3.
Merged 2 and 3 without moving.
Merging block 4 into block 2...
Merged blocks 2 and 4.
Merged 2 and 4 without moving.
Removing jump 11.
Merging block 5 into block 2...
Merged blocks 2 and 5.
Merged 2 and 5 without moving.


try_optimize_cfg iteration 2



;;
;; Full RTL generated for this function:
;;
(note 1 0 3 NOTE_INSN_DELETED)
(note 3 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
(note 2 3 5 2 NOTE_INSN_FUNCTION_BEG)
(insn 5 2 6 2 (set (reg:DI 5 di)
(symbol_ref/f:DI ("*.LC0") [flags 0x2] <var_decl 0x7f532282d850 *.LC0>)) hello.c:5 -1
(nil))
(call_insn 6 5 7 2 (set (reg:SI 0 ax)
(call (mem:QI (symbol_ref:DI ("puts") [flags 0x41] <function_decl 0x7f5322762800 __builtin_puts>) [0 __builtin_puts S1 A8])
(const_int 0 [0]))) hello.c:5 -1
(nil)
(expr_list:DI (use (reg:DI 5 di))
(nil)))
(insn 7 6 10 2 (set (reg:SI 83 [ D.2203 ])
(const_int 0 [0])) hello.c:6 -1
(nil))
(insn 10 7 14 2 (set (reg:SI 84 [ <retval> ])
(reg:SI 83 [ D.2203 ])) hello.c:6 -1
(nil))
(insn 14 10 15 2 (set (reg/i:SI 0 ax)
(reg:SI 84 [ <retval> ])) hello.c:7 -1
(nil))
(insn 15 14 0 2 (use (reg/i:SI 0 ax)) hello.c:7 -1
(nil))

如果對RTL有興趣的,可以參考

Clang

相較於GCC,Clang的中間語言叫做Bitcode
利用下面的方式產生hello.ll的檔案。

1
$ clang -S -emit-llvm hello.c

內容大概長這個樣子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
; ModuleID = 'hello.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [13 x i8] c"Hello world\0A\00", align 1

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
%1 = alloca i32, align 4
store i32 0, i32* %1
%2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 0, i32 0))
ret i32 0
}

declare i32 @printf(i8*, ...) #1

attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = metadata !{metadata !"Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)"}

同樣列出參考資料