Title: Course Overview
1Course Overview
- PART I overview material
- 1 Introduction
- 2 Language processors (tombstone diagrams,
bootstrapping) - 3 Architecture of a compiler
- PART II inside a compiler
- 4 Syntax analysis
- 5 Contextual analysis
- 6 Runtime organization
- 7 Code generation
- PART III conclusion
- Interpretation
- 9 Review
2What This Lecture is About
A compiler translates a program from a high-level
language into an equivalent program in a
low-level language.
Triangle Program
Compile
TAM Program
Run
Result
3The Phases of a Compiler
Source Program
Syntax Analysis
Error Reports
Abstract Syntax Tree
Contextual Analysis
Error Reports
Decorated Abstract Syntax Tree
Code Generation
This chapter
Object Code
4Multi Pass Compiler
A multi pass compiler makes several passes over
the program. The output of a preceding phase is
stored in a data structure and used by subsequent
phases.
Dependency diagram of a typical Multi Pass
Compiler
Compiler Driver
calls
calls
This chapter
calls
Syntactic Analyzer
Contextual Analyzer
Code Generator
5Issues in Code Generation
- Code Selection
- Deciding which sequence of target machine
instructions will be used to implement each
phrase in the source language. (Phrases are
syntactic constructs such as commands,
expressions, etc.) - Storage Allocation
- Deciding the storage address for each variable in
the source program. (Static allocation, stack,
heap, etc.) - Register Allocation (for register-based machines)
- How to use registers efficiently to store
intermediate results.
Well use a stack-based machine, so this is not
an issue for us.
6Code Generation
Source Program
Target program
let var n integer var c charin begin c
n n1end
PUSH 2LOADL 38STORE 1SBLOAD 0SBLOADL
1CALL addSTORE 0SBPOP 2HALT
Source and target program must be semantically
equivalent
Semantic specification of the source language is
structured in terms of the phrases in the source
language. gt Code generation follows the same
inductive structure.
7Inductive Code Generation
Inductive means code generation for a big
structure is defined in terms of putting together
chunks of code that correspond to the
sub-structures.
Example Sequential Command code generation
Semantic specification The sequential command C1
C2 is executed as follows first C1 is executed
then C2 is executed. Code generation
function execute Command -gt Instruction execut
e C1 C2 execute C1 execute C2
instructions for C1
instructions for C1 C2
instructions for C2
8Inductive Code Generation
Example Assignment Command code generation
Code generation function execute I E
evaluate E STORE address I
instructions for E yield value for E on top of
the stack
instruction to store result into variable
These pictures of the code layout for a
particular source language construct are called
code templates. Inductive means A code
template specifies the object code to which a
phrase is translated in terms of the object code
to which its sub-phrases are translated.
9Inductive Code Generation
Example code generation for a larger phrase in
terms of its subphrases.
LOAD f LOAD n CALL mult STORE f
execute f fn
execute f fn n n-1
LOAD n CALL pred STORE n
execute n n-1
10Specifying Code Generation with Code Templates
For each phrase class P in the abstract syntax
of the source language Define code function fP
P -gt Instruction that translates each phrase in
class P to object code.
We specify the function fP by code templates.
Typically they look like
fP QR fQ Q fR R
Note A phrase class typically corresponds to
a non-terminal of the abstract syntax. This in
turn corresponds to an abstract class in the
Java/C classes that implement the AST nodes.
(For example Expression, Command, Declaration,
etc.)
11Specifying Code Generation with Code Templates
Example Code templates specification for Mini
Triangle
Recap The mini triangle AST
Program Command Program Command
V-name Expression AssignCmd
let Declaration in Command LetCmd
... Expression Integer-Literal IntegerExp
V-name VnameExp Operator
Expression UnaryExp Expression Op
Expression BinaryExp Declaration
... V-name Identifier SimpleVName
12Specifying Code Generation with Code Templates
The code generation functions for Mini Triangle
Phrase Class Function Effect of the generated
code
Run program P then halt. Start and finish with
empty stack. Execute command C. May update
variables but does not shrink or grow the
stack! Evaluate expression E. Net result is
pushing the value of E onto the stack. Push the
value of constant or variable onto the stack. Pop
value from stack and store in variable
V. Elaborate declaration D. Make space on the
stack for constants and variables in D.
Program Command Expres- sion V-name V-name Decl
a-ration
run P execute C evaluate E fetch V assign
V elaborate D
13Code Generation with Code Templates
Code generation functions for Mini Triangle
Programs
run C execute C HALT
Commands
execute V E evaluate E assign
V execute I ( E ) evaluate E CALL
p where p is address of the routine named I
14Code Generation with Code Templates
Commands
execute C1 C2 execute C1 execute
C2 execute if E then C1 else C2 evaluate
E JUMPIF(0) g execute C1 JUMP h g execute
C2 h
15Code Generation with Code Templates
Commands
execute while E do C JUMP h g execute
C h evaluateE JUMPIF(1) g execute let D
in C elaborateD execute C POP(0) s if s
gt 0 where s amount of storage allocated by D
16Code Generation with Code Templates
Expressions
evaluate IL note IL is an integer
literal LOADL v where v the integer value of
IL evaluate V note V is variable
name fetchV evaluate O E note O is a
unary operator evaluateE CALL p where p
address of routine for O evaluate E1 O E2
note O is a binary operator evaluateE1 eval
uateE2 CALL p where p address of routine
for O
17Code Generation with Code Templates
Variables Note Mini Triangle only needs
static allocation. Q Why not stack
allocation? Q Why not heap allocation?
fetch V LOAD dSB where d address of V
relative to SB assign V STORE dSB where
d address of V relative to SB
18Code Generation with Code Templates
Declarations
elaborate const I E evaluateE elaborat
e var I T PUSH s where s size of
T elaborate D1 D2 elaborate
D1 elaborate D2
THE END these are all the code templates for
Mini Triangle. Now lets put them to use in an
example.
19Example of Mini Triangle Code Generation
execute while igt0 do ii2 JUMP h g LOAD
i LOADL 2 CALL sub STORE i h LOAD i LOADL
0 CALL gt JUMPIF(1) g
evaluate i2
execute ii2
evaluate igt0
Q Show how we get to this end result by
successively applying the code templates we
defined. Picture shows a few steps but not
all. Fill in the missing details.
20Alternative Code for While Loop
execute while igt0 do ii2 g LOAD i LOADL
0 CALL gt JUMPIF(0) h LOAD i LOADL 2 CALL
sub STORE i JUMP g h
evaluate igt0
evaluate i2
execute ii2
Q Which is better and why?
21Example of Mini Triangle Code Generation
execute let var i Integer in ii3 PUSH
1 LOAD i LOADL 3 CALL add STORE i POP(0) 1
Note This only shows the end result. Q Show
how we get to this end result by successively
applying the code templates defined previously.
22Special Case Code Templates
There are often several ways to generate code for
an expression, command, etc. The templates we
defined work, but sometimes we can get more
efficient code for special cases gt use special
case code templates.
Example
evaluate i1 LOAD i LOADL 1 CALL add
evaluate i1 LOAD i CALL succ
more efficient code for the special case 1
what we get with the general code templates
23Special Case Code Templates
Example some special case code template for
1, -1,
evaluate E 1 evaluate E CALL succ
evaluate 1 E evaluate E CALL succ
evaluate E - 1 evaluate E CALL pred
A special-case code template is one that is
applicable to phrases of a special form. Such
phrases are also covered by a more general form.
24Special Case Code Templates
Example In-lining known constants.
execute let const n7 var iInteger
in inn LOADL 7 PUSH 1 LOAD
n LOAD n CALL mult STORE i POP(0) 2
elaborate const n7
elaborate var iInteger
execute inn
This is how the code looks like with no special
case templates.
25Special Case Code Templates
Example In-lining known constants.
Special case templates for inlining literals.
elaborate const I IL no code
fetch I special case if I is a known literal
constant LOADL v where v is the known value of I
execute let const n7 var iInteger in inn
PUSH 1 LOADL 7 LOADL 7 CALL mult STORE
i POP(0) 1
26Code Generation Algorithm
The code templates specify how code is to be
generated gt Determines code generation
algorithm. Generating code traversal of the AST
emitting instructions one by one. The code
templates determine the order of the traversal
and the instructions to be emitted.
We will next look at how to implement a Mini
Triangle code generator in Java (or C). Use
the visitor design pattern!