Professional Documents
Culture Documents
9 Intermediate Code Generation
9 Intermediate Code Generation
9 Intermediate Code Generation
Compiler Architecture
Intermediate
Code
tokens Syntactic
Scanner Parser Semantic Code
Source structure Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)
Intermediate
Code
Intermediate
Code Code
Optimizer
Symbol
Table
2
Intermediate Code
• Similar terms: Intermediate representation,
intermediate language
• Intermediate code is the interface between
front end and back end in a compiler
• Language and Machine neutral
• Many forms
• Level depends on how being processed
• More than one intermediate language may
be used by a compiler
3
Intermediate Languages Types
• Graphical IRs: Abstract Syntax trees, Direct
Access Graph (DAGs), Control Flow
Graphs
• Linear IRs:
– Stack based (postfix)
– Three address code (quadruples)
4
Graphical IRs
• Abstract Syntax Trees (AST) – retain essential structure
of the parse tree, eliminating unneeded nodes.
5
ASTs and DAGs:
a := b *-c + b*-c
:= :=
a + a +
* * *
b - (uni) b - (uni) b - (uni)
c c c
6
More Example ASTs and DAGs
• Intermediate Forms of a Program:
– ASTs: Abstract Syntax Trees (below left)
– DAGs: Directed Acyclic Graphs (below right)
– What is the Expression?
assign assign
a + a +
* *
*
b uminus b uminus
b uminus
c c
DAG Example
• Example: a+a*(b-c)+(b-c)*d
+ *
*
d
a -
b c
Linearized IC
• Stack based (one • Three address (quadruples) –
address) – compact up to three operands, one
push 2 operator
push y t1 <- 2
multiply t2 <- y
push x t3 <- t1 * t2
subtract t4 <- x
t5 <- t4 – t1
9
Attribute Grammar
• Attribute Grammar for Generating an AST
as a side Effect of the Translation Process:
Representing Attribute Grammars
assign
c c
Objective
• Directly Generate Code From AST or DAG as a Side Effect of Parsing
Process (Attribute Grammar)
• Consider Code Below: