Download Code Generation: From Source Code to Target Code and more Papers Computer Science in PDF only on Docsity!
Code generation
source code
front end
opt.
back end
target IR IR code
Code generation steps
- Source code → intermediate representation
- generate intermediate representation during parse based on syntax, symbol tables
- Intermediate representation → target code
- instruction selection choose instructions based on target instr set
- memory management decide storage for variables; allocate registers
- linkage code determine prolog/epilog code
- instruction scheduling choose instruction execution order
Abstract syntax trees
Abstract syntax tree (AST)
- stores syntactic structure of program
*
< 2 >
Building the AST
- construct node for LHS of production
- fill in fields using RHS of production
Example
E 0 ::= E 1 ’+’ E 2 { E 0 .val = node(’+’, E 1 .val, E 2 .val); } | E 1 ’’ E 2 { E 0 .val = node(’’, E 1 .val, E 2 .val); } | id { E 0 .val = node(id.val); } | num { E 0 .val = node(num.val); }
Intermediate representation
We’ll be targeting RISC-like processors
- load-store architecture
- register-transfer language
- three-address code
- explicit loads and stores
Examples
load r1, $ r1 ← value at loadi r1, $ r1 ← value of store r1, $ ← r move r1, r2 $ r1 ← r add r1, r2, r3 $ r1 ← r2 + r sub r1, r2, r3 $ r1 ← r2 - r mult r1, r2, r3 $ r1 ← r2 * r jmp $ jump to
Simple expressions
Expression trees:
- adopt a simple treewalk scheme
- assign a virtual register to each operator
- emit code in postorder walk
Support routines:
- addr( str ) — returns the name of a virtual register that contains the base address for str
- newtemp() — returns a new virtual register name
Assume:
- assume tree reflects precedence, associativity
- assume all operands are integers
Simple expressions
PLUS
ID x
PLUS
NUM 4
ID y
load r2, r1 $ r2 ← addr(x) loadi r3, 4 $ constant load r5, r4 $ r5 ← addr(y) add r6, r3, r5 $ r6 ← 4 + y add r7, r2, r6 $ r7 ← x + (4 + y)
Control structures
Assignment statement
lhs ← rhs
Strategy
- evaluate rhs to a value (an rvalue)
- evaluate lhs to an address (an lvalue) i) lvalue is register ⇒ move it ii) lvalue is address ⇒ store it
Registers versus memory
- non-aliased scalars ⇒ can go in a register
- aggregate or potentially aliased ⇒ in memory
Control structures
while loop
- evaluate the control expression
- if false, branch beyond end of loop if true, fall through into loop body
- at end, re-evaluate the control expression
- if true, branch to top of loop body if false, fall through
Example
r1 ← expr evaluate the expression if not(r1) br L2 compare and branch L1: ... loop body r1 ← expr if r1, br L L2: ... following stmt
Test at end ⇒ simple loop is one block
Control structures
case statement
- evaluate the controlling expression
- branch to the selected case
- execute its code
- branch to the following statement
Key Issue:
⇒ finding the right case
Method When Cost linear search few cases O(| cases |) binary search sparse O(log 2 (| cases |)) jump table dense O(1)
Boolean expressions
Two schools of thought on representation:
Numerical Values
- assign numerical values to true and false
- evaluate booleans like arithmetic expressions
Control Flow
- represent boolean value by location in code
- convert to numerical value when stored
Neither representation dominates the other.
Boolean expressions
Numerical Values
- assign a value to true (say 1 )
- assign a value to false (say 0 )
- use hardware — and, or, not, xor
Choose values that make the hardware work.
Boolean expressions
Control Flow
- use conditional branches and comparator
- chain of branches to evaluate expression
- code looks terrible
Control flow representation works well for expressions in conditional statements.
Clean up:
- branch to next statement
- branch to branch
Boolean expressions
Control Flow
Source Expression Generated Code a<b or ( c<d and e<f ) if a<b br LT br L L1: if c<d br L br LF L2: if e<f br LT br LF LF: code under false or t1 ← false br LEXIT LT: code under true or t1 ← true br LEXIT LEXIT:
This works well when the expression’s value is tested but not preserved for later reuse.
Boolean expressions
Reality
- either approach works fairly well
- numerical code reflects logical constructs
- control flow code works well for relations
- compiler can choose based on context
Control flow
- accounting nightmare — tracking labels
- backpatching is the right answer