Build a Compiler from Scratch(IR Basic)
Simple Grammar of the LLVM IR
Type
i32
: 32-bits int
i1
: 1-bit int
void
: nothing, just take a place
label
: label a block, help the machine to jump
Array
: an Array
Function
Function Definition: define i32 @main(i32 %a,i32 %b, \cdots) {}
Function Declaration: declare void @putarray(i32, i32*)
Basic Block
Wrapping a bunch of instructions and end with a terminator instruction, without any loop or brunch. Just move linearly.
Attention: A basic block could have a
call
instruction in it.
Terminator Instruction
e.g. :ret
br
ret
:
1 | return 0 |
br
1 | if (a = b) {} else {} |
The %9
should be set ahead with instructions like icmp
. %10 and %11 are two labeled blocks.
Or just:
1 | br label %10 when it finishes a block |
The basic Structure of the LLVM IR
The most important classes are Value, Use, and User.
BasicBlock
, Argument
, User
are inherited from Value
.
Constant
and Instruction
are inherited from User
.
Function
inherited from Constant
by multiple inheritances, so Function
is also Value
and User
.