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
CMSC 430 Lecture 9, Page 1
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); }
Instruction selection
Code templates
- template for each language construct
- ignore surrounding context
- simple recursive approach
Language constructs
- simple expressions
- control structures
- procedure calls
- complex expressions
Applying templates
- use syntax during parse
- apply tree rewriting to AST
CMSC 430 Lecture 9, Page 3
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
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)
CMSC 430 Lecture 9, Page 7
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
if-then-else
- evaluate the expression to true or false
- if true, fall through to then part branch around else part
- if false, branch to else part fall through to next statement
Example
r1 ← expr evaluate the expression if not(r1) br L1 compare and branch ... stmts for then part br L2 branch to exit L1: ... stmts for else part L2: ... following stmt
CMSC 430 Lecture 9, Page 9
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
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.
CMSC 430 Lecture 9, Page 13
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
Numerical Values
Source Expression Generated Code b or ( c and not d ) t1 ← not d t2 ← c and t t3 ← b or t
a < b if (a<b) br L t1 ← 0 br L L1: t1 ← 1 L2: nop
A numerical representation handles logic well.
CMSC 430 Lecture 9, Page 15
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
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