Introduction to Compiling - Part 2
Infix / Postfix Notation
Consider Binary Operators
Infix Notation:
operand
operator
operand
Can be ambiguous!
X + (Y - Z)
X + Y - Z
(X + Y) - Z
Need rules of precedence, associativity, parentheses.
Postfix Notation:
operand
operand
Eliminates ambiguity!
X Y Z - +
X Y + Z -
operator
means
X + (Y - Z)
means (X
+ Y) - Z
Assumption: No confusion about how many operands an operator requires.
binary- versus unaryInfix:
X + -(Y - Z)
Postfix: X Y Z -bin -un +
X + (Y - -Z)
X Y Z -un -bin +
© Harry H. Porter, 2005
1
Introduction to Compiling - Part 2
Converting Expressions to Postfix
Let E be an infix expression.
Define POSTFIX(E) to be the same expression in postfix.
(Ignore unary operators.)
• If E is a variable or constant...
then POSTFIX ( E ) = E
• If E is of the form E1 op E2 ...
then POSTFIX ( E1 op E2 ) = POSTFIX ( E1 ) || POSTFIX ( E2 ) || op
• If E is of the form ( E1 ) ...
then POSTFIX ( ( E1 ) ) = POSTFIX ( E1 )
© Harry H. Porter, 2005
String concatenation
2
Introduction to Compiling - Part 2
Syntax-Directed Translation
Problem/Goal:
Translate infix expressions into postfix.
The input is described by a CFG.
Approach:
Start with the grammar.
Each production is augmented with semantic rules.
Each non-terminal has an associated attribute.
Expr . trans
Attribute has a name
and a value
Non-terminal
Semantic rules added to grammar productions
...tell how to compute the attributes’ values.
trans = “a b + c +”;
© Harry H. Porter, 2005
3
Introduction to Compiling - Part 2
An Example Attribute Grammar
CFG Grammar
Expr ! Expr + Term
! Expr - Term
! Term
Term ! ID
Terminals:
“+”, “-”, ID
Token attribute: ID.svalue
Non-terminals:
Expr
Term
Attributes:
Expr.t
Term.s
Attribute Values:
Strings, e.g., “x y + z -”
Concatenation: ||
© Harry H. Porter, 2005
4
Introduction to Compiling - Part 2
Attribute Grammar
Expr ! Expr + Term
Expr ! Expr - Term
Expr ! Term
Term ! ID
© Harry H. Porter, 2005
5
Introduction to Compiling - Part 2
Attribute Grammar
Expr0 ! Expr1 + Term
Expr0 ! Expr1 - Term
Expr0 ! Term
Subscripts added
...to tell different
non-terminals apart
Term ! ID
© Harry H. Porter, 2005
6
Introduction to Compiling - Part 2
Attribute Grammar
Expr0 ! Expr1 + Term
Expr0.t = Expr1.t || Term.s || “+”;
Expr0 ! Expr1 - Term
Expr0.t = Expr1.t || Term.s || “-”;
Expr0 ! Term
Expr0.t = Term.s;
Term ! ID
Term.s = ID.svalue;
© Harry H. Porter, 2005
7
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Expr0
Expr0
Expr0
Term
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Step 1: Find a parse tree
© Harry H. Porter, 2005
8
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr0
Expr0
Expr0
Term
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
Expr
Expr
-
Term
+
Term
Term
ID
svalue = “W”
ID
svalue = “Y”
ID
svalue = “X”
© Harry H. Porter, 2005
9
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr
t =
t =
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
t =
Step 2: Compute the attribute values
Expr
Expr0
Expr0
Expr0
Term
-
Term
s =
+
Term
s =
Term
s =
ID
svalue = “W”
ID
svalue = “Y”
ID
svalue = “X”
© Harry H. Porter, 2005
10
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr0
Expr0
Expr0
Term
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
t =
Step 2: Compute the attribute values
+
Expr
t =
Expr
s = “W”
-
t =
Term
ID
Term
svalue = “W”
s = “Y”
Term
ID
s = “X”
svalue = “Y”
ID
svalue = “X”
Use the rule:
Term ! ID
Term.s = ID.svalue;
© Harry H. Porter, 2005
11
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr
t =
t = “X”
-
Term
s = “X”
ID
svalue = “X”
t =
+
Term
s = “W”
Term
s = “Y”
ID
svalue = “W”
ID
svalue = “Y”
Use the rule:
Expr0 ! Term
© Harry H. Porter, 2005
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
Step 2: Compute the attribute values
Expr
Expr0
Expr0
Expr0
Term
Expr0.t = Term.s;
12
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr0
Expr0
Expr0
Term
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
t =
Step 2: Compute the attribute values
Expr
t = “X Y -”
Expr
-
t = “X”
+
Term
s = “W”
ID
Term
svalue = “W”
s = “Y”
Term
ID
s = “X”
svalue = “Y”
Use the rule:
ID
Expr0 ! Expr1 - Term
svalue = “X”
Expr0.t = Expr1.t || Term.s || “-”;
© Harry H. Porter, 2005
13
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr
t = “X Y - W +”
t = “X Y -”
t = “X”
-
Term
s = “X”
ID
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
Step 2: Compute the attribute values
Expr
Expr0
Expr0
Expr0
Term
svalue = “X”
+
Term
s = “W”
Term
s = “Y”
ID
svalue = “W”
ID
svalue = “Y”
Use the rule:
Expr0 ! Expr1 + Term
Expr0.t = Expr1.t || Term.s || “+”;
© Harry H. Porter, 2005
14
Introduction to Compiling - Part 2
Example
Translate: “X - Y + W”
Step 1: Find a parse tree
Expr0
Expr0
Expr0
Term
! Expr1 + Term
! Expr1 - Term
! Term
! ID
Expr
t = “X Y - W +”
Step 2: Compute the attribute values
+
Expr
t = “X Y -”
Expr
t = “X”
-
Term
s = “X”
Term
s = “W”
Term
s = “Y”
ID
svalue = “W”
ID
svalue = “Y”
ID
svalue = “X”
© Harry H. Porter, 2005
15
Introduction to Compiling - Part 2
Synthesized v. Inherited
Synthesized Attributes
(see previous example)
Compute the attributes bottom-up
From leaves toward root
Example Semantic Rule:
Expr0 ! Expr1 - Term
Expr0.t = Expr1.t ||
All rules compute the attribute of the left-hand side
... as a function of the attributes from the right-hand side.
X !ABC
X.t = f(A.t, B.t, C.t);
Information flows up the tree.
A Bottom-Up Approach
Term.s || “-”;
Inherited Attributes
Information flows down the tree
Example:
X !ABC
B.t = f(X.t);
A Top-Down Approach
© Harry H. Porter, 2005
16
Introduction to Compiling - Part 2
Depth-First Traversal
function Visit (N: Node)
for each child of N do
Visit (child)
endFor
”process” N
endFunction
A
C
B
D
F
E
G
Sythesized Attributes
Evaluate children first
Then move up the tree
... and take care of parents’ attributes
© Harry H. Porter, 2005
17
Introduction to Compiling - Part 2
Translator Schemes
Embed semantic actions into grammar rules.
Example
X ! A { print(“+”) } B { print(“.”)
Enclose actions in braces { ... }
Arbitrary code (e.g., Java statements)
© Harry H. Porter, 2005
}
C
18
Introduction to Compiling - Part 2
Translator Schemes
Embed semantic actions into grammar rules.
Example
X ! A { print(“+”) } B { print(“.”)
Enclose actions in braces { ... }
Arbitrary code (e.g., Java statements)
}
C
How to execute?
Step 1: Construct a parse tree
Add the actions to the parse tree
X
A
{ print(“+”) }
B
{ print(“.”) }
C
© Harry H. Porter, 2005
19
Introduction to Compiling - Part 2
Translator Schemes
Embed semantic actions into grammar rules.
Example
X ! A { print(“+”) } B { print(“.”)
Enclose actions in braces { ... }
Arbitrary code (e.g., Java statements)
}
C
How to execute?
Step 1: Construct a parse tree
Add the actions to the parse tree
X
A
{ print(“+”) }
B
{ print(“.”) }
C
Step 2: Perform a depth-first traversal
Execute actions as they are encountered in traversal
© Harry H. Porter, 2005
20
Introduction to Compiling - Part 2
Example: Convert Infix Expressions to Postfix
Expr
Expr
Expr
Term
! Expr + Term { print(“+”) }
! Expr - Term { print(“-”) }
! Term
! ID { print(ID.svalue) }
© Harry H. Porter, 2005
21
Introduction to Compiling - Part 2
Example: Convert Infix Expressions to Postfix
Expr
Expr
Expr
Term
! Expr + Term { print(“+”) }
! Expr - Term { print(“-”) }
! Term
! ID { print(ID.svalue) }
Expr
+
Expr
Expr
-
Term
Term
ID
print(“-”)
Term
ID
sval=“W”
print(“+”)
print(“W”)
print(“Y”)
sval=“Y”
ID
sval=“X”
print(“X”)
© Harry H. Porter, 2005
Example
Source: X - Y + W
Output: X Y - W +
22
Introduction to Compiling - Part 2
Assume we have a translator scheme...
Assume we have a parser...
Can we execute the actions while we do the parsing?
© Harry H. Porter, 2005
23
Introduction to Compiling - Part 2
Assume we have a translator scheme...
Assume we have a parser...
Can we execute the actions while we do the parsing?
Depth-first traversal ! Recursive descent parser!
© Harry H. Porter, 2005
24
Introduction to Compiling - Part 2
Assume we have a translator scheme...
Assume we have a parser...
Can we execute the actions while we do the parsing?
Depth-first traversal ! Recursive descent parser!
Example:
Expr ! Expr + Term { print(“+”) }
! Expr - Term { print(“-”) }
! Term
Term! ID { print(ID.svalue) }
© Harry H. Porter, 2005
25
Introduction to Compiling - Part 2
Assume we have a translator scheme...
Assume we have a parser...
Can we execute the actions while we do the parsing?
Depth-first traversal ! Recursive descent parser!
Example:
Expr ! Expr + Term { print(“+”) }
! Expr - Term { print(“-”) }
! Term
Term! ID { print(ID.svalue) }
First, we’ll need to eliminate left-recursion:
Expr ! Term Rest
Rest ! + Term { print(“+”) } Rest
! - Term { print(“-”) } Rest
!"
Term! ID { print(ID.svalue) }
© Harry H. Porter, 2005
26
Introduction to Compiling - Part 2
Implementation
Expr ! Term Rest
Rest ! + Term { print(“+”) } Rest
! - Term { print(“-”) } Rest
!"
Term ! ID { print(ID.svalue) }
function ParseExpr ()
ParseTerm ()
ParseRest ()
endFunction
© Harry H. Porter, 2005
27
Introduction to Compiling - Part 2
Implementation
Expr ! Term Rest
Rest ! + Term { print(“+”) } Rest
! - Term { print(“-”) } Rest
!"
Term ! ID { print(ID.svalue) }
function ParseTerm ()
if nextToken == ID then
s = token.svalue
MustHave (ID)
print (s)
else
Error “Expecting ID”
endIf
endFunction
© Harry H. Porter, 2005
28
Introduction to Compiling - Part 2
Implementation
Expr ! Term Rest
Rest ! + Term { print(“+”) } Rest
! - Term { print(“-”) } Rest
!"
Term ! ID { print(ID.svalue) }
function ParseRest ()
if nextToken == ‘+’ then
MustHave (‘+’)
ParseTerm ()
print (“+”)
ParseRest ()
elseIf nextToken == ‘-’ then
MustHave (‘-’)
ParseTerm ()
print (“-”)
ParseRest ()
else
// Epsilon -- do nothing
endIf
endFunction
© Harry H. Porter, 2005
29
Introduction to Compiling - Part 2
Repeating...
function ParseExpr ()
ParseTerm ()
ParseRest ()
endFunction
function ParseRest ()
if nextToken == ‘+’ then
MustHave (‘+’)
ParseTerm ()
print (“+”)
ParseRest ()
elseIf nextToken == ‘-’ then
MustHave (‘-’)
ParseTerm ()
print (“-”)
ParseRest ()
else
// Epsilon -- do nothing
endIf
endFunction
© Harry H. Porter, 2005
30
Introduction to Compiling - Part 2
Optimizing “Tail Recursion”
function ParseExpr ()
ParseTerm ()
ParseRest ()
endFunction
function ParseRest ()
if nextToken == ‘+’ then
MustHave (‘+’)
ParseTerm ()
print (“+”)
ParseRest ()
elseIf nextToken == ‘-’ then
MustHave (‘-’)
ParseTerm ()
print (“-”)
ParseRest ()
else
// Epsilon -- do nothing
endIf
endFunction
function ParseRest ()
while true
if nextToken == ‘+’ then
MustHave (‘+’)
ParseTerm ()
print (“+”)
elseIf nextToken == ‘-’ then
MustHave (‘-’)
ParseTerm ()
print (“-”)
else
return
endIf
endWhile
endFunction
© Harry H. Porter, 2005
31
Introduction to Compiling - Part 2
In-Lining...
function ParseExpr ()
ParseTerm ()
ParseRest ()
endFunction
function ParseRest ()
if nextToken == ‘+’ then
MustHave (‘+’)
ParseTerm ()
print (“+”)
ParseRest ()
elseIf nextToken == ‘-’ then
MustHave (‘-’)
ParseTerm ()
print (“-”)
ParseRest ()
else
// Epsilon -- do nothing
endIf
endFunction
© Harry H. Porter, 2005
function ParseExpr ()
ParseTerm ()
while true
if nextToken == ‘+’ then
MustHave (‘+’)
ParseTerm ()
print (“+”)
elseIf nextToken == ‘-’ then
MustHave (‘-’)
ParseTerm ()
print (“-”)
else
return
endIf
endWhile
endFunction
32
Introduction to Compiling - Part 2
Generating Target Code
Output of compiler
•!Assembly code (e.g., SPARC code)
•!Machine code (e.g., 0x3b4E0F0F...)
•!“Bytecodes” (e.g., PUSH, POP, GOTO, ...)
© Harry H. Porter, 2005
33
Introduction to Compiling - Part 2
Generating Target Code
Output of compiler
•!Assembly code (e.g., SPARC code)
•!Machine code (e.g., 0x3b4E0F0F...)
•!“Bytecodes” (e.g., PUSH, POP, GOTO, ...)
Bytecodes
Higher level than machine-specific code.
Example: Java Bytecodes
Software to execute the instructions
Interpreter (aka: “Virtual Machine”, “Emulator”)
Or translate the bytecodes into machine-specific code
“Just-in-time” compilers
© Harry H. Porter, 2005
34
Introduction to Compiling - Part 2
Generating Target Code
Output of compiler
•!Assembly code (e.g., SPARC code)
•!Machine code (e.g., 0x3b4E0F0F...)
•!“Bytecodes” (e.g., PUSH, POP, GOTO, ...)
Bytecodes
Higher level than machine-specific code.
Example: Java Bytecodes
Software to execute the instructions
Interpreter (aka: “Virtual Machine”, “Emulator”)
Or translate the bytecodes into machine-specific code
“Just-in-time” compilers
Code for
Abstract Stack Machine
Abstract Stack Machine
A virtual machine architecture
Based on a stack
Can be used as intermediate code
Replace with back-end
For Intel, Power-PC, ...
Source
Front-end
Intermediate
Representation
Back-end
Code for SPARC
© Harry H. Porter, 2005
35
Introduction to Compiling - Part 2
Abstract Stack Machine
•
•
•
•
•
Limited / simple instruction set
No registers
Stack of data values
Program memory
Data memory
...
W:
X:
Y:
(3+(6-X))
17
5
350
Data
Memory
...
36X-+
PUSH
PUSH
PUSH
SUBTRACT
ADD
© Harry H. Porter, 2005
Runtime
Stack
3
6
X
Program
36
Introduction to Compiling - Part 2
Abstract Stack Machine
•
•
•
•
•
Limited / simple instruction set
No registers
Stack of data values
Program memory
Data memory
...
W:
X:
Y:
(3+(6-X))
17
5
350
Data
Memory
...
36X-+
PUSH
PUSH
PUSH
SUBTRACT
ADD
Runtime
Stack
3
6
X
3
Program
© Harry H. Porter, 2005
37
Introduction to Compiling - Part 2
Abstract Stack Machine
•
•
•
•
•
Limited / simple instruction set
No registers
Stack of data values
Program memory
Data memory
...
W:
X:
Y:
(3+(6-X))
17
5
350
Data
Memory
...
36X-+
PUSH
PUSH
PUSH
SUBTRACT
ADD
© Harry H. Porter, 2005
Runtime
Stack
3
6
X
Program
6
3
38
Introduction to Compiling - Part 2
Abstract Stack Machine
•
•
•
•
•
Limited / simple instruction set
No registers
Stack of data values
Program memory
Data memory
...
W:
X:
Y:
(3+(6-X))
17
5
350
Data
Memory
...
36X-+
PUSH
PUSH
PUSH
SUBTRACT
ADD
3
6
X
5
6
3
Program
Runtime
Stack
© Harry H. Porter, 2005
39
Introduction to Compiling - Part 2
Abstract Stack Machine
•
•
•
•
•
Limited / simple instruction set
No registers
Stack of data values
Program memory
Data memory
...
W:
X:
Y:
(3+(6-X))
17
5
350
Data
Memory
...
36X-+
PUSH
PUSH
PUSH
SUBTRACT
ADD
© Harry H. Porter, 2005
Runtime
Stack
3
6
X
Program
1
3
40
Introduction to Compiling - Part 2
Abstract Stack Machine
•
•
•
•
•
Limited / simple instruction set
No registers
Stack of data values
Program memory
Data memory
...
W:
X:
Y:
(3+(6-X))
17
5
350
Data
Memory
...
36X-+
PUSH
PUSH
PUSH
SUBTRACT
ADD
Runtime
Stack
3
6
X
Program
© Harry H. Porter, 2005
4
41
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
© Harry H. Porter, 2005
42
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
R-Value
L-Value
R-Value
© Harry H. Porter, 2005
43
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
Example:
p.computeTaxes (x);
© Harry H. Porter, 2005
44
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
Example:
p.computeTaxes (x);
R-Value
R-Value
© Harry H. Porter, 2005
45
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
Example:
p.computeTaxes (x);
Example:
for i = 1 to 100 do ...
© Harry H. Porter, 2005
46
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
Example:
p.computeTaxes (x);
Example:
for i = 1 to 100 do ...
L-Value
© Harry H. Porter, 2005
47
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
Example:
p.computeTaxes (x);
Example:
for i = 1 to 100 do ...
Example:
read(x);
© Harry H. Porter, 2005
48
Introduction to Compiling - Part 2
L-Values versus R-Values
L-Value:
•!Need the variable’s location.
R-Value:
•!Need the variable’s value.
Example:
x = y * (z + 5);
Example:
p.computeTaxes (x);
Example:
for i = 1 to 100 do ...
Example:
read(x);
L-Value
© Harry H. Porter, 2005
49
Introduction to Compiling - Part 2
Stack Machine Instructions
Arithmetic Instructions
ADD
SUB
MULT
DIV
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
© Harry H. Porter, 2005
L
L
L
L
50
Introduction to Compiling - Part 2
Stack Machine Instructions
Arithmetic Instructions
ADD
SUB
MULT
DIV
top
...etc...
Stack
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
...
N
N
N
...
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
L
L
L
L
17
5
350
Data Memory
...
© Harry H. Porter, 2005
51
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
Stack/Data Manipulation Instructions
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
© Harry H. Porter, 2005
23
top
...etc...
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
After
L
L
L
L
...
...
...
...
17
5
350
...
...
PUSH
...
100:
101:
102:
top
17
5
350
...
23
52
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
top
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
L
L
L
L
After
Y
X
result
...
...
...
...
17
5
350
100:
101:
102:
17
5
350
...
...
...
PUSH ...X...
PUSH ...Y...
SUB
...
top
© Harry H. Porter, 2005
53
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
Stack/Data Manipulation Instructions
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
© Harry H. Porter, 2005
350
top
...etc...
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
After
L
L
L
L
...
...
...
...
17
5
350
...
...
RVALUE
...
100:
101:
102:
top
17
5
350
...
102
54
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
102
top
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
After
L
L
L
L
...
...
...
...
17
5
350
100:
101:
102:
17
5
350
...
...
...
LVALUE
...
top
102
© Harry H. Porter, 2005
55
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
top
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
© Harry H. Porter, 2005
L
L
L
L
...
LVALUE 102
PUSH
XXX
ASSIGN
...
After
XXX
102
top
...
...
...
...
17
5
350
...
100:
101:
102:
17
5
XXX
...
56
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
top
After
456
top
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
L
L
L
L
...
...
...
...
17
5
350
100:
101:
102:
17
5
350
...
...
...
POP
...
© Harry H. Porter, 2005
57
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
top
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
© Harry H. Porter, 2005
L
L
L
L
After
456
456
456
...
...
...
...
17
5
350
...
100:
101:
102:
top
17
5
350
...
...
COPY
...
58
Introduction to Compiling - Part 2
Flow of Control
Option 1: Absolute Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
1004
© Harry H. Porter, 2005
59
Introduction to Compiling - Part 2
Flow of Control
Option 1: Absolute Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
© Harry H. Porter, 2005
Assembly Code:
123
1004
...
MyLab:
PUSH
SUB
GOTO
...
123
MyLab
60
Introduction to Compiling - Part 2
Flow of Control
Option 1: Absolute Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
1004
Option 2: Relative Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
-2
© Harry H. Porter, 2005
61
Introduction to Compiling - Part 2
Flow of Control
Option 1: Absolute Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
1004
Option 2: Relative Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
© Harry H. Porter, 2005
This code can
be relocated!!!
123
-2
62
Introduction to Compiling - Part 2
Flow of Control
Option 1: Absolute Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
1004
Option 2: Relative Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
-2
Option 3: Symbolic Labels
...
1003:
1004:
1005:
1006:
...
LABEL MyLab
PUSH 123
SUB
GOTO MyLab
© Harry H. Porter, 2005
63
Introduction to Compiling - Part 2
Flow of Control
Option 1: Absolute Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
1004
Option 2: Relative Addresses
...
1004:
1005:
1006:
...
PUSH
SUB
GOTO
123
-2
Option 3: Symbolic Labels
...
1003:
1004:
1005:
1006:
...
LABEL MyLab
PUSH 123
SUB
GOTO MyLab
© Harry H. Porter, 2005
...
MyLab:
PUSH
SUB
GOTO
...
123
MyLab
64
Introduction to Compiling - Part 2
Stack Machine Instructions
Before
Arithmetic Instructions
ADD
SUB
MULT
DIV
top
After
0
top
...etc...
Stack/Data Manipulation Instructions
PUSH
RVALUE
LVALUE
ASSIGN
POP
COPY
N
N
N
100:
101:
102:
Flow of Control Instructions
GOTO
LABEL
GOFALSE
GOTRUE
HALT
L
L
L
L
Boolean values can be
pushed onto stack:
0 = FALSE
Any other = TRUE
...
...
...
...
17
5
350
100:
101:
102:
17
5
350
...
...
© Harry H. Porter, 2005
65
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
© Harry H. Porter, 2005
451
452
2
3
453
...
X
Y
Z
451:
452:
453:
300
4
1
Data
Memory
...
Runtime
Stack
66
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
...
X
Y
Z
451
452
2
451:
452:
453:
300
4
1
Data
Memory
...
Runtime
Stack
3
453
451
© Harry H. Porter, 2005
67
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
© Harry H. Porter, 2005
451
452
2
...
X
Y
Z
451:
452:
453:
300
4
1
Data
Memory
...
Runtime
Stack
3
453
4
451
68
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
...
X
Y
Z
451
452
2
451:
452:
453:
300
4
1
Data
Memory
...
3
453
2
4
451
Runtime
Stack
© Harry H. Porter, 2005
69
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
© Harry H. Porter, 2005
451
452
2
...
X
Y
Z
451:
452:
453:
300
4
1
Data
Memory
...
Runtime
Stack
3
453
6
451
70
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
...
X
Y
Z
451
452
2
451:
452:
453:
300
4
1
Data
Memory
...
3
453
3
6
451
Runtime
Stack
© Harry H. Porter, 2005
71
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
© Harry H. Porter, 2005
451
452
2
3
453
...
X
Y
Z
451:
452:
453:
300
4
1
Data
Memory
...
1
3
6
451
Runtime
Stack
72
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
...
X
Y
Z
451
452
2
451:
452:
453:
300
4
1
Data
Memory
...
3
453
2
6
451
Runtime
Stack
© Harry H. Porter, 2005
73
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
© Harry H. Porter, 2005
451
452
2
...
X
Y
Z
451:
452:
453:
300
4
1
Data
Memory
...
Runtime
Stack
3
453
12
451
74
Introduction to Compiling - Part 2
Example
Source Code:
x=(y+2)*(3-z);
Postfix:
xy2+3z-*=
Y
Instruction Memory:
...
LVALUE
RVALUE
PUSH
ADD
PUSH
RVALUE
SUB
MULT
ASSIGN
...
...
X
Z
451
452
2
451:
452:
453:
12
4
1
Data
Memory
...
Runtime
Stack
3
453
© Harry H. Porter, 2005
75
Introduction to Compiling - Part 2
Producing Translations
Target: Code for Abstract Stack Machine
ParseExpr ()
Parses an expression
... and produces the code for it.
ParseStmt ()
Parses a statement
... and produces the code for it
... using ParseExpr and ParseStmt recursively.
Assignment Stmt:
ID = Expr ;
Translation:
LVALUE
ID
... Code for Expr...
ASSIGN
© Harry H. Porter, 2005
For example: (X-3)*Y
RVALUE X
PUSH
3
SUB
RVALUE Y
MULT
76
Introduction to Compiling - Part 2
Translating a WHILE statement
Source:
...
while Expr do
Stmts
endWhile
...
Translation:
...
LABEL
Generating Unique Labels
Lab_43
... Code for Expr...
GOFALSE Lab_44
... Code for Stmts...
GOTO
LABEL
...
Lab_43
Lab_44
Function called: NewLabel
Returns a hitherto unused label.
Example:
Lab_17
Lab_18
Lab_19
...
© Harry H. Porter, 2005
77
Introduction to Compiling - Part 2
Helper Function: EMIT()
Function: Emit()
Passed:
•!An op-code
•!Additional argument, if any
Writes one instruction to the output.
•!To “stdout”
•!To internal storage area
! internal representation ! target code ! output file
Example of compiler code:
...
lab = NewLabel ();
Emit (“label”, lab);
...
Emit (“goto”, lab);
...
© Harry H. Porter, 2005
78
Introduction to Compiling - Part 2
Translating Statements
Stmt
Expr
!
!
!
!
!
ID “=” Expr “;”
while Expr do Stmts endWhile
if Expr then Stmts else Stmts endIf
...
...
© Harry H. Porter, 2005
79
Introduction to Compiling - Part 2
Translating Statements
Stmt
Expr
!
!
!
!
!
ID “=” Expr “;”
while Expr do Stmts endWhile
if Expr then Stmts else Stmts endIf
...
...
Source:
W = X + Y + Z;
Expr
Code:
LVALUE
RVALUE
RVALUE
RVALUE
ADD
ADD
ASSIGN
© Harry H. Porter, 2005
W
X
Y
Z
Emitted
by
ParseExpr
80
Introduction to Compiling - Part 2
Translating Statements
Stmt
Expr
!
!
!
!
!
ID “=” Expr “;”
while Expr do Stmts endWhile
if Expr then Stmts else Stmts endIf
...
...
Source:
W = X + Y + Z;
Translation Scheme for ASSIGN-STMT:
Stmt
! ID
{ Emit (“LVALUE”, ID.svalue) }
“=”
Expr
{ Emit (“ASSIGN”) }
“;”
Expr
Code:
LVALUE
RVALUE
RVALUE
RVALUE
ADD
ADD
ASSIGN
W
X
Y
Z
Emitted
by
ParseExpr
© Harry H. Porter, 2005
81
Introduction to Compiling - Part 2
Translating Statements
Stmt
Expr
!
!
!
!
!
ID “=” Expr “;”
while Expr do Stmts endWhile
if Expr then Stmts else Stmts endIf
...
...
Source:
while A-B do
X=Y;
endWhile
Code:
LABEL
RVALUE
RVALUE
SUB
GOFALSE
LVALUE
RVALUE
ASSIGN
GOTO
LABEL
© Harry H. Porter, 2005
Lab_4
A
Emitted
B
by
ParseExpr
Lab_5
X
Emitted
Y
by
Lab_4
Lab_5
ParseStmts
82
Introduction to Compiling - Part 2
Translating Statements
Stmt
Expr
!
!
!
!
!
ID “=” Expr “;”
while Expr do Stmts endWhile
if Expr then Stmts else Stmts endIf
...
Source:
...
while A-B do
X=Y;
endWhile
Translation Scheme for WHILE-STMT:
Code:
LABEL
Stmt ! while
RVALUE
{ topLabel = NewLabel()
RVALUE
bottomLabel = NewLabel()
SUB
Emit (“LABEL”, topLabel) }
GOFALSE
Expr
LVALUE
{ Emit (“GOFALSE”, bottomLabel) }
RVALUE
do Stmts endWhile
ASSIGN
GOTO
{ Emit (“GOTO”, topLabel)
LABEL
Emit (“LABEL”, bottomLabel) }
Lab_4
A
Emitted
B
by
ParseExpr
Lab_5
X
Emitted
Y
by
Lab_4
Lab_5
ParseStmts
© Harry H. Porter, 2005
83
Introduction to Compiling - Part 2
function ParseStmt ()
var topLabel, bottomLabel: String
if nextToken == ID then
Emit (“LVALUE”, token.svalue)
MustHave (ID)
MustHave (“=”)
Stmt !
ParseExpr ()
Emit (“ASSIGN”)
MustHave (“;”)
elseIf nextToken == WHILE then
MustHave (WHILE)
topLabel = NewLabel ()
bottomLabel = NewLabel ()
Stmt !
Emit (“LABEL”, topLabel)
ParseExpr ()
Emit (“GOFALSE”, bottomLabel)
MustHave (DO)
ParseStmts ()
MustHave (ENDWHILE)
Emit (“GOTO”, topLabel)
Emit (“LABEL”, bottomLabel)
elseIf
...
endIf
endFunction
© Harry H. Porter, 2005
ID
{ Emit(“LVALUE”,ID.svalue) }
“=”
Expr
{ Emit(“ASSIGN”) }
“;”
while
{ topLabel = NewLabel()
bottomLabel = NewLabel()
Emit(“LABEL”,topLabel) }
Expr
{ Emit(“GOFALSE”,bottomLabel)}
do Stmts endWhile
{ Emit(“GOTO”,topLabel)
Emit(“LABEL”,bottomLabel) }
84
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
© Harry H. Porter, 2005
85
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
... Code for Expr2...
LABEL
...
© Harry H. Porter, 2005
Lab_43
86
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 1: Expr-1 is true:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
true
...
... Code for Expr2...
LABEL
...
Lab_43
© Harry H. Porter, 2005
87
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 1: Expr-1 is true:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
... Code for Expr2...
LABEL
...
© Harry H. Porter, 2005
Lab_43
true
true
...
88
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 1: Expr-1 is true:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
true
...
... Code for Expr2...
LABEL
...
Lab_43
© Harry H. Porter, 2005
89
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 1: Expr-1 is true:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
... Code for Expr2...
LABEL
...
© Harry H. Porter, 2005
Lab_43
...
90
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 1: Expr-1 is true:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
Expr-2
...
... Code for Expr2...
LABEL
...
Lab_43
© Harry H. Porter, 2005
91
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 2: Expr-1 is false:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
... Code for Expr2...
LABEL
...
© Harry H. Porter, 2005
Lab_43
false
...
92
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 2: Expr-1 is false:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
false
false
...
... Code for Expr2...
LABEL
...
Lab_43
© Harry H. Porter, 2005
93
Introduction to Compiling - Part 2
Short-Circuit Operators
if (i <= max) and (a[i] == -1) then ...
Do we need to evaluate the second expression?
b = Expr1 and Expr2
b = if Expr1 then Expr2 else FALSE endIf
Translation:
Case 2: Expr-1 is false:
...
... Code for Expr1...
COPY
GOFALSE Lab_43
POP
... Code for Expr2...
LABEL
...
© Harry H. Porter, 2005
Lab_43
false
...
94
Introduction to Compiling - Part 2
Short-Circuit Operators
And
b = Expr1 and Expr2
Or
b = Expr1 or Expr2
Conditional (ternary) operator
b = Expr1 ? Expr2 : Expr3
Means: b = (if Expr1 then Expr2 else Expr3 endIf)
Same as: if Expr1 then
b = Expr2
else
b = Expr3
endIf
© Harry H. Porter, 2005
95
© Copyright 2026 Paperzz