[TOC]
Trying to guess learn how the llvm IR works
Check the core part of the llvm, and then imitate it.
module
: the whole programfunction
: containsbasic block
basic block
: containsinstruction
, need to be labeledinstruction
just imitate the design of llvm, that is, look at .ll
file to find out the solution
then
block, else
block, after
block
condition
is in the block before if statement.
condition
should be a separate block, as it may be entered from continue
.
we can find that the before
block and after
block are like the entry and exit of if
/while
/for
statement.
if(a == b){ // if1
if(b == c){ // if2
}
if(c == d){ // if3
}
}
To implement the above code, we need to link if3
's after
block to if1
's after
block, but we can't find if3
's after
block. So we need set a tail
block for every block.
For example, if1then
's tail
block is if3after
, we can find it recursively.
Eevery virtual register can only be assigned once.
Every mutable variable is in memory, register is only used to pass value.
For some special reason, llvm-ir uses the SSA form. After mem2reg pass, all allocas are removed, so there will be many phi
instructions.
I have designed the scope
system in semantic phase.
- local scope : s stack-like to maintain the variables.
- global scope : global variables, functions, classes
How to catch the corresponding value ?
Set an llvm-value in every ASTnode
and every variable registry
.
- an ASTnode has
addr
andvalue
, sometimes we just need theaddr
. registry
's value is always a address.- if
registry
is a member variable, the value is a const int representing it's id in the class. - ASTfunction has IRfunction type
function's formal argument is set as a special parameter
type in llvm type system, and in llvm document, it is called argument
.
Almost everything in llvm is a value. A value can be used by another value(user).
Every value has a type.
- For a function, we record the return type and the parameter types.
- For a instruction, we record the type of the result.
A store instruction have no result, so it's type is void
.
a value can be used as oprand must have a name.
- global's name is it's own name
- local's name should be renamed.
- a type can be printed.
- a value can be def and used.
- def : print the name and in special format
- use : just use the name