项目作者: filletofish

项目描述 :
Sample of building control flow graph, SSA form, phi nodes, LLVM IR for imagined language.
高级语言: C++
项目地址: git://github.com/filletofish/compiler-touch.git
创建时间: 2017-05-31T19:18:32Z
项目社区:https://github.com/filletofish/compiler-touch

开源协议:

下载


Compiler-touch

Simple compiler frontend to generate intermediate representation for simple language.
The languange consists of integers, mutating variables, cycles and condition statements.

Two modes available:

  • Generating my own intermediate presentation with features:

    • Building Control Flow Graph
    • Using SSA Form
    • Using Phi Nodes
  • Generating LLVM intermediate presentation with power of LLVM Library.

Example:

Here is simple code written with our language.

Code:

  1. a = 3
  2. b = 0
  3. for i = 1, b in
  4. a = a + 3
  5. if b then
  6. a = a - 2
  7. else
  8. a = 0

Control flow graph:



Its intermediate representation (using my own statements):

  1. bb #0 entry
  2. preds:
  3. succs: bb #1 loop_cond
  4. dominatedBy:
  5. a_0 = 3
  6. b_0 = 0
  7. i_0 = 1
  8. branch to: bb #1 loop_cond
  9. bb #1 loop_cond
  10. preds: bb #0 entry bb #2 loop_body
  11. succs: bb #2 loop_body bb #3 loop_cont
  12. dominatedBy: bb #0 entry
  13. i_1 = [i_0 bb #0 entry; i_2 bb #2 loop_body; ]
  14. a_1 = [a_0 bb #0 entry; a_2 bb #2 loop_body; ]
  15. branch on: b_0 - i_1 to: bb #2 loop_body or: bb #3 loop_cont
  16. bb #2 loop_body
  17. preds: bb #1 loop_cond
  18. succs: bb #1 loop_cond
  19. dominatedBy: bb #1 loop_cond
  20. a_2 = a_1 + 3
  21. i_2 = i_1 + 1
  22. branch to: bb #1 loop_cond
  23. bb #3 loop_cont
  24. preds: bb #1 loop_cond
  25. succs: bb #5 else bb #4 then
  26. dominatedBy: bb #1 loop_cond
  27. branch on: b_0 to: bb #5 else or: bb #4 then
  28. bb #4 then
  29. preds: bb #3 loop_cont
  30. succs: bb #6 if_cont
  31. dominatedBy: bb #3 loop_cont
  32. a_3 = a_1 - 2
  33. branch to: bb #6 if_cont
  34. bb #5 else
  35. preds: bb #3 loop_cont
  36. succs: bb #6 if_cont
  37. dominatedBy: bb #3 loop_cont
  38. a_4 = 0
  39. branch to: bb #6 if_cont
  40. bb #6 if_cont
  41. preds: bb #4 then bb #5 else
  42. succs:
  43. dominatedBy: bb #3 loop_cont
  44. a_5 = [a_3 bb #4 then; a_4 bb #5 else; ]

Its intermediate representation produced with LLVM Library:

  1. ; ModuleID = 'My Module'
  2. source_filename = "My Module"
  3. define common i32 @main() {
  4. entrypoint:
  5. %a = alloca i32
  6. store i32 3, i32* %a
  7. %b = alloca i32
  8. store i32 0, i32* %b
  9. %i = alloca i32
  10. store i32 1, i32* %i
  11. br label %loopCoonditionBB
  12. loopCoonditionBB: ; preds = %loop, %entrypoint
  13. %b1 = load i32, i32* %b
  14. %i2 = load i32, i32* %i
  15. %loopcond = icmp slt i32 %i2, %b1
  16. br i1 %loopcond, label %loop, label %afterloop
  17. loop: ; preds = %loopCoonditionBB
  18. %a3 = load i32, i32* %a
  19. %addtmp = fadd i32 %a3, 3
  20. store i32 %addtmp, i32* %a
  21. %i4 = load i32, i32* %i
  22. %nextvar = fadd i32 %i4, 1
  23. store i32 %nextvar, i32* %i
  24. br label %loopCoonditionBB
  25. afterloop: ; preds = %loopCoonditionBB
  26. %b5 = load i32, i32* %b
  27. %ifcond = icmp ne i32 %b5, 0
  28. br i1 %ifcond, label %then, label %else
  29. then: ; preds = %afterloop
  30. %a6 = load i32, i32* %a
  31. %subtmp = fsub i32 %a6, 2
  32. store i32 %subtmp, i32* %a
  33. br label %ifcont
  34. else: ; preds = %afterloop
  35. store i32 0, i32* %a
  36. br label %ifcont
  37. ifcont: ; preds = %else, %then
  38. %iftmp = phi i32 [ %subtmp, %then ], [ 0, %else ]
  39. ret i32 %iftmp
  40. }

What is IR?

Wiki article

Ok, then. With LLVM installed you can look at IR of C language by yourself. Just write simple C program.

Use flag -emit-llvm and level of optimization -O0 (from 0 to 3):
clang -S -emit-llvm -O0 helloworld.c

Now hello.ll contains the IR.

Dependencies:

  • llvm 4.0
  • clang

Building:

  1. ./build.sh

Usage:

Printing intermediate representation:

  1. ./compiler <program.txt

Use cmd argument -gv for printing graph viz presentation

Use cmd argument -llvm for llvm mode

Sources: