IR-code¶
The purpose of an intermediate representation (IR) of a program is to decouple the implementation of a front-end from the implementation of a back-end. That is, front ends generate IR-code, optimizers optimize this code and lastly backends transform it into machine code or something else.
A good IR has several characteristics:
- It should be simple enough for front-ends to generate code.
- It should be rich enough to exploit the target instructions best.
The IR in the ppci.ir module has the following properties:
- It is static single assignment form. Meaning a value can only be assigned once, and is then never changed. This has several advantages.
- It contains only basic types. Structures, arrays and void types are not represented.
Top level structure¶
The IR-code is implemented in the ir package.
-
class
ppci.ir.
Module
(name)¶ Container unit for variables and functions.
-
add_variable
(variable)¶ Add a variable to this module
-
functions
¶ Get all functions of this module
-
variables
¶ Get all variables of this module
-
-
class
ppci.ir.
Variable
(name, amount)¶ Global variable, reserves room in the data area. Has name and size
-
class
ppci.ir.
SubRoutine
(name)¶ Base class of function and procedure. These two differ in that a function returns a value, where as a procedure does not.
Design trade-off: In C, a void type is introduced to permit functions that return nothing (void). This seems somewhat artificial, but keeps things simple for the users. In pascal, the procedure and function types are explicit, and the void type is not needed. This is also the approach taken here.
So instead of a Function and Call types, we have Function, Procedure, FunctionCall and ProcedureCall types.
-
add_block
(block)¶ Add a block to this function
-
remove_block
(block)¶ Remove a block from this function
-
-
class
ppci.ir.
Procedure
(name)¶ A procedure definition that does not return a value
-
class
ppci.ir.
Function
(name, return_ty)¶ Represents a function.
-
class
ppci.ir.
Block
(name)¶ Uninterrupted sequence of instructions. A block is properly terminated if its last instruction is a
FinalInstruction
.-
add_instruction
(instruction)¶ Add an instruction to the end of this block
-
is_closed
¶ Determine whether this block is propert terminated
-
is_empty
¶ Determines whether the block is empty or not
-
predecessors
¶ Return all predecessing blocks
-
remove_instruction
(instruction)¶ Remove instruction from block
-
successors
¶ Get the direct successors of this block
-
Types¶
Only simple types are available.
-
ppci.ir.
ptr
¶ Pointer type
-
ppci.ir.
i64
¶ Signed 64-bit type
-
ppci.ir.
i32
¶ Signed 32-bit type
-
ppci.ir.
i16
¶ Signed 16-bit type
-
ppci.ir.
i8
¶ Signed 8-bit type
-
ppci.ir.
u64
¶ Unsigned 64-bit type
-
ppci.ir.
u32
¶ Unsigned 32-bit type
-
ppci.ir.
u16
¶ Unsigned 16-bit type
-
ppci.ir.
u8
= u8¶ Unsigned 8-bit type
-
ppci.ir.
f64
¶ 64-bit floating point type
-
ppci.ir.
f32
¶ 32-bit floating point type
Instructions¶
The following instructions are available.
Memory instructions
-
class
ppci.ir.
Load
(address, name, ty, volatile=False)¶ Load a value from memory
-
class
ppci.ir.
Store
(value, address, volatile=False)¶ Store a value into memory
-
class
ppci.ir.
Alloc
(name, amount)¶ Allocates space on the stack. The type of this value is a ptr
Data instructions
-
class
ppci.ir.
Const
(value, name, ty)¶ Represents a constant value
-
class
ppci.ir.
Binop
(a, operation, b, name, ty)¶ Generic binary operation
-
class
ppci.ir.
Cast
(value, name, ty)¶ Base type conversion instruction
Control flow instructions
-
class
ppci.ir.
ProcedureCall
(function_name, arguments)¶ Call a procedure with some arguments
-
class
ppci.ir.
FunctionCall
(function_name, arguments, name, ty)¶ Call a function with some arguments and a return value
-
class
ppci.ir.
Jump
(target)¶ Jump statement to another block within the same function
-
class
ppci.ir.
CJump
(a, cond, b, lab_yes, lab_no)¶ Conditional jump to true or false labels.
-
class
ppci.ir.
Return
(result)¶ This instruction returns a value and exits the function.
-
class
ppci.ir.
Exit
¶ Instruction that exits the procedure.
Other
-
class
ppci.ir.
Phi
(name, ty)¶ Imaginary phi instruction to make SSA possible.
-
class
ppci.ir.
Undefined
(name, ty)¶ Undefined value, this value must never be used.
Abstract instruction classes¶
There are some abstract instructions, which cannot be used directly but serve as base classes for other instructions.
-
class
ppci.ir.
Instruction
¶ Base class for all instructions that go into a basic block
-
function
¶ Return the function this instruction is part of
-
-
class
ppci.ir.
Value
(name, ty)¶ An instruction that results in a value has a type and a name
-
is_used
¶ Determine whether this value is used anywhere
-
-
class
ppci.ir.
FinalInstruction
¶ Final instruction in a basic block
Uml¶