Virtual Machines
Syntax-Directed Translation
Syntax-Directed Translation
fun(y) {
x = y + 2;
};
function {
local_vars = [y, x],
constants = [None, 2],
instructions = [
load_local 0
load_const 1
add
store_local 1
load_const 0
return
]
}
function __function_1 (y) : {
%x = alloca int64_t
%0 = %y
%1 = 2
%2 = add %0 %1
store %x %2
%3 = 0
return %3
}
__function_1 :
sub 8 %rsp
mov %rsp %rbp
mov (%rbp, 2, 8) %rax
mov 2 %rcx
add %rcx %rax
mov %rax (%rbp)
mov 0 %rax
ret
High-Level
VM
Low-Level
VM
Machine Code
Language
Syntax-Directed Translation
Simple Language
(sequential composition)�(if-then-else)
(loop)
(assignment)
AST (Parsing)
x > 1
x > 10
x = x – 1
x = x – 2
while
if
while(x > 1) {
if (x > 10) {
x = x – 2;
} else {
x = x – 1;
}
}
e
s
e
s1
s2
Control Flow Graph: an alternative program representation.
while(x > 1) {
if (x > 10) {
x = x – 2;
} else {
x = x – 1;
}
}
x > 1
x > 10
x = x - 1
x = x - 2
True
False
False
True
CFG
Control Flow Graph: an alternative program representation.
x > 1
x > 10
x = x - 1
x = x - 2
True
False
False
True
AST
CFG
x > 1
x > 10
x = x – 1
x = x – 2
while
if
e
s
e
s1
s2
Execution model: recursion
Execution model: iteration
CFG Formally
Syntax-Direct Translation
x > 1
x > 10
x = x - 1
x = x - 2
True
False
False
True
AST
CFG
x > 1
x > 10
x = x – 1
x = x – 2
while
if
e
s
e
s1
s2
Goal: Translate Expression
function {
local_vars = [],
constants = [1, 2, None],
instructions = [
load_const 0
load_const 1
add
return
]
}
f = fun() {
return 1 + 2;
}
Inference Rules for Translation
Semantics:
Translation:
Step 1: Generating Basic Blocks
Simple Language: E has no control flow
(sequential composition)�(if-then-else)
(loop)
(assignment)
Translation: Integer Constant
// Description: push a constant onto the operand stack
// Mnemonic: load_const i
// Operand 0: index of constant in enclosing function's list of constants
// Stack: S => S :: f.constants()[i]
LoadConst,
Where does i come from?
Generating Basic Blocks
Generating Basic Blocks
Translation: Load Constant
// Description: push a constant onto the operand stack
// Mnemonic: load_const i
// Operand 0: index of constant in enclosing function's list of constants
// Stack: S => S :: f.constants()[i]
LoadConst,
Allocating a Constant
Translation: Variable reference
MITScript Virtual Machine
function {
functions = [ ],
constants = [None, 0, 1],
names = [f],
instructions = [
load_const 1
load_func 0
alloc_closure
store_global 0
load_global 0
load_const 2
call 1
pop
load_const 0
return
]}
function {
local_vars = [y, x],
constants = [None, 2],
instructions = [
load_local 0
load_const 1
add
store_local 1
load_const 0
return
]
}
f = fun(y) {
x = y + 2;
};
f(1);
We’ll come back to this later!
Translation: Unary Minus
// Description: computes the unary minus of the integer operation
// Mnemonic: neg
// Operand 0: N/A
// Operand 1: value
// Stack: S :: operand 1 => S:: - operand 1�Neg,
Observation
Plus
// Description: implements addition (as given in the semantics of Assignment #2)
// Mnemonic: add
// Operand 0: N/A
// Operand 1: right value
// Operand 2: left value
// Result: value of the operation as specified by the semantics of Assignment #2
// Stack: S:: operand 2 :: operand 1 => S :: op(operand 2, operand 1)
Add,
Goal: Translate Expression
function {
local_vars = [],
constants = [1, 2, None],
instructions = [
load_const 0
load_const 1
add
return
]
}
f = fun() {
return 1 + 2;
}
Example
Translate:
1 + 2
Example
Translate:
1 + 2
Example
Translate:
1 + 2
Goal: Translate Statements
function {
local_vars = [],
constants = [1, 2, None],
instructions = [
load_const 0
load_const 1
add
return
]
}
f = fun() {
return 1 + 2;
}
Inference Rules for Translation
Semantics:
Translation:
Translation
while (x > 1) {
if (x > 10) {
x = x – 2;
} else {
x = x – 1;
}
}
x > 1
x > 10
x = x - 1
x = x - 2
True
False
False
True
CFG
Sequential Composition: (missing constants!)
Translation
while (x > 1) {
if (x > 10) {
x = x – 2;
} else {
x = x – 1;
}
}
x > 1
x > 10
x = x - 1
x = x - 2
True
False
False
True
CFG
If statement: (missing constants!)
Translation
while (x > 1) {
if (x > 10) {
x = x – 2;
} else {
x = x – 1;
}
}
x > 1
x > 10
x = x - 1
x = x - 2
True
False
False
True
CFG
While statement: (missing constants!)
Inference Rules for Translation
Semantics:
Translation:
MITScript Virtual Machine
function {
functions = [ ],
constants = [None, 0, 1],
names = [f],
instructions = [
load_const 1
load_func 0
alloc_closure
store_global 0
load_global 0
load_const 2
call 1
pop
load_const 0
return
]}
function {
local_vars = [y, x],
constants = [None, 2],
instructions = [
load_local 0
load_const 1
add
store_local 1
load_const 0
return
]
}
f = fun(y) {
x = y + 2;
};
f(1);