cs471 16 Ir
cs471 16 Ir
Source code
lexical
Lexical Analysis errors
Intermediate Code tokens
syntax
Syntactic Analysis errors
AST
CS 471 semantic
Semantic Analysis errors
October 29, 2007
AST’
1
Intermediate Representations Intermediate representation
The machine has an infinite number of • Goal: get program closer to machine code without
temporaries losing information needed to do useful optimizations
• Call them t0, t1, t2, .... • Need multiple IR stages
• Temporaries can hold values of any type opt
• The type of the temporary is derived from the x86 (LIR)
generation optimize optimize
• Temporaries go out of scope with each function opt
AST HIR MIR PowerPC (LIR)
opt
Alpha (LIR)
2
High-Level IR (HIR) Medium-Level IR (MIR)
• used early in the process • Try to reflect the range of features in the source
• usually converted to lower form later on language in a language-independent way
• Preserves high-level language constructs • Intermediate between AST and assembly
• –structured flow, variables, methods • Unstructured jumps, registers, memory locations
• Allows high-level optimizations based on properties • Convenient for translation to high-quality machine
of source language (e.g. inlining, reuse of constant code
variables) • OtherMIRs:
• Example: AST – quadruples: a = b OP c (“a” is explicit, not arc)
– UCODE: stack machine based (like Java bytecode)
– advantage of tree IR: easy to generate, easier to do
reasonable instruction selection
– advantage of quadruples: easier optimization
3
Graphical IRs: Often Use Basic Blocks Basic blocks
Leaders:
entry Tree, for basic block*
read(n)
• root: operator
read(n)
f0 := 0 f0 := 0 • up to two children: operands
f1 := 1 f1 := 1 • can be combined
if n<=1 goto L0 n <= 1
i := 2 Uses:
L2: if i<=n goto L1
return f2 return n
• algebraic simplifications gt, t2
i := 2
L1: f2 := f0+f1 • may generate locally optimal code.
f0 := f1
f1 := f2 i<=n add, t1 0
L1: i := 2 assgn, i add, t1 gt, t2
i := i+1
go to L2 t1:= i+1
L0: return n f2 := f0+f1 return f2 assgn, i 1
t2 := t1>0
f0 := f1 2 i 1 t1 0
f1 := f2 if t2 goto L1
i := i+1 2
exit *straight-line code with no branches or branch targets.
20 CS 471 – Fall 2007 21 CS 471 – Fall 2007
4
Graphical IRs Graphical IRs
5
Linear IRs Example: Three-Address Code
Consider the AST • IRs provide the interface between the front
and back ends of the compiler
• Should be machine and language independent
• Should be amenable to optimization
t1t1:=
:=--cc
t2t2:=
:=bb**tt11 Next Time: Tiger IR
t3t3:=
:=--cc
t4t4:=
:=bb**tt33
t5t5:=
:=tt22++t4t4
aa := :=tt55