LLVM Workshop Osaka Umeda, Japan

3,190 views

Published on

LLVM in 20 minutes

Published in: Technology, Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,190
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
39
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

LLVM Workshop Osaka Umeda, Japan

  1. 1. LLVM 2009 1 15 ( )
  2. 2. LLVM Tatsuhiro Ujihisa
  3. 3. LLVM The Low Level Virtual Machine (LLVM) is a compiler infrastructure, written in C++, which is designed for compile- time, link-time, run-time, and "idle-time" optimization of programs written in arbitrary programming languages. LLVM was originally developed as a research infrastructure at the University of Illinois at Urbana-Champaign to investigate dynamic compilation techniques for static and dynamic programming languages... http://en.wikipedia.org/wiki/Low_Level_Virtual_Machine
  4. 4. In short, C JVM gcc ( ) ( )
  5. 5. C C→ → → $ vim a.c $ gcc a.c -S -o a.s $ gcc a.s -o a $ ./a
  6. 6. LLVM (1) C → LLVM → LLVM → $ vim a.c $ llvm-gcc a.c -S -o a.ll $ llvm-as a.ll -o a.bc $ lli a.bc
  7. 7. LLVM (2) C → LLVM → LLVM → → $ vim a.c $ llvm-gcc a.c -S -o a.ll $ llvm-as a.ll -o a.bc $ llc a.bc -o a $ ./a
  8. 8. LLVM (3) LLVM → LLVM → $ vim a.ll $ llvm-as a.ll -o a.bc $ lli a.bc
  9. 9. LLVM (4) LLVM → LLVM → → $ vim a.ll $ llvm-as a.ll -o a.bc $ opt a.bc -o a2.bc $ lli a2.bc
  10. 10. LLVM (5) LLVM → LLVM → → → $ vim a.ll $ llvm-as a.ll -o a.bc $ opt -O3 a.bc -o a2.bc $ opt -O3 a2.bc -o a3.bc $ lli a3.bc
  11. 11. LLVM $ llvm-as a.ll -o a.bc $ opt -O3 a.bc -o a2.bc $ opt -O3 a2.bc -o a3.bc $ llvm-dis a3.bc -o a3.ll $ vim a3.ll
  12. 12. llvm-as: LLVM (.ll) LLVM (.bc) lli: LLVM (.bc) opt: LLVM (.bc) LLVM (.bc) llvm-dis: llvm-as llvm-gcc: C (.c) LLVM (.ll)
  13. 13. LLVM Hello, world! Hello, world! @str = internal constant [14 x i8] c"Hello, world!00" declare i32 @puts(i8*) define i32 @main() { call i32 @puts( i8* getelementptr ([14 x i8]* @str, i32 ret i32 0 }
  14. 14. LLVM ( ) C
  15. 15. Vim quickrun LLVM .ll <Space>r llvm-as lli
  16. 16. LLVM LLVM OK (C OK) LLVM Mac, Linux, Windows
  17. 17. LLVM ( ) C (llvm-gcc) Perl Python (pypy) Ruby (Rubinius, MacRuby, etc) Haskell Brainf**k ... LLVM
  18. 18. LLVM Pure ( ) Haskell sudo port install pure
  19. 19. LLVM LLVM LLVM
  20. 20. ( ) LLVM (MacRuby) ( svn up)
  21. 21. ! Brainf**k → LLVM BFC: Brainf**k Compiler git clone git://github.com/ujihisa/bfc.git vim bfc/bfc.rb
  22. 22. :+ +: /* C */ /* char *h */ ++*h; LLVM !→ OK
  23. 23. (bfc.rb ) when '+' a = tc += 1; b = tc += 1; c = tc += 1; d = tc += 1 "%tmp#{a} = load i32* %i, align 4n" << "%tmp#{b} = getelementptr [1024 x i8]* %h, i32 0, i32 % "%tmp#{c} = load i8* %tmp#{b}, align 1n" << "%tmp#{d} = add i8 1, %tmp#{c}n" << "store i8 %tmp#{d}, i8* %tmp#{b}, align 1n" 1. 2. 3. 4. 1
  24. 24. $ cat helloworld.bf $ cat helloworld.bf | ruby bfc.rb --llvm > helloworld.ll $ llvm-as helloworld.ll > helloworld.bc $ opt -O3 helloworld.bc # $ lli helloworld.bc Hello, world! $ ruby bfc.rb --llvm helloworld.bf --run
  25. 25. : BFC: Brainf**k Compilers http://ujihisa.blogspot.com/2009/12/bfc- brainfk-compilers.html LLVM For Starters http://ujihisa.blogspot.com/2009/12/llvm- for-starters.html Let's Try LLVM http://ujihisa.blogspot.com/2009/12/let- try-llvm.html
  26. 26. nil try-llvm.html

×