LLVMのお勉強1

資料

きつねさんでもわかるLLVM-1.0.0

環境

OS:Lubuntu
clang:version 3.8.1

勉強内容

資料を参考に、CファイルをLLVM-IRへ

#include <stdio.h>

int main (){
  printf("HelloWorld\n");
  return 0;
}

コマンドは次のを使った。

clang -emit-llvm -S -o HelloWorld.ll HelloWorld.c

…何やってるかわからないので、調べる。 man clangを叩く。

-o ***: 出力ファイルを***に指定
-S: LLVMアセンブリファイルの生成
-emit-llvm: -Sオプションと一緒に指定すると、出力ファイルがLLVM中間言語アセンブリになる

というわけで、LLVM中間言語アセンブリを出力していることが分かった。

で、出力された内容(HelloWorld.ll)が次。

; ModuleID = 'HelloWorld.c'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

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

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

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

attributes #0 = { nounwind uwtable "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-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "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-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.8.1-12ubuntu1 (tags/RELEASE_381/final)"}

あれ、資料にのってるのと違う…
資料では、clang3.2系をベースに解説しているので、多少違いがあっても仕方ない。
次回は、HelloWorld.llの中身を紐解いていこうと思う。