Elaboration or: Semantic Analysis Compiler Baojian Hua [email protected].
Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua...
-
date post
20-Dec-2015 -
Category
Documents
-
view
228 -
download
2
Transcript of Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua...
![Page 2: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/2.jpg)
Our Goal We’ve defined miniC and miniPentium an
d written some sample programs Store them in memory
But how to run these miniC and miniPentium programs?
At least two approaches: Compile (translate) them to real Pentium Design and implement virtual machines
we discuss this first
![Page 3: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/3.jpg)
What’s a Virtual Machine? A virtual machine (VM) is machine
which built with software to execute programs like a real machine
Long history: Date back at least to 70’s last century
the Pascal P-Code, etc. Renew industry’s interest in the recent
decade Sun’s JVM and Microsoft’s CLR, etc.
![Page 4: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/4.jpg)
In Picture
Programs Programs
VM
Programs Programs
![Page 5: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/5.jpg)
Why VM Important?
Portability: VM is relatively high-level Easy to port to different machines
Managed code: Easy to control the behavior of
program many may be hard to implement on
real machine
![Page 6: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/6.jpg)
CVM
A virtual machine to run miniC programs (CVM)
CVM = (store, prog) prog is a miniC program (in assignment #
2) store is a machine memory, essentially a
mapping from identifiers to numbers store: id -> n
![Page 7: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/7.jpg)
An Example
Right hand are a sample program and a store
Store could be read and written store (id) store [id |-> n]
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
![Page 8: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/8.jpg)
Execution
x = 8;
y = 9;
z = x+y;
print (z);
![Page 9: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/9.jpg)
Execution
8xx = 8;
y = 9;
z = x+y;
print (z);
![Page 10: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/10.jpg)
Execution
8
9xy
x = 8;
y = 9;
z = x+y;
print (z);
![Page 11: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/11.jpg)
Execution
8
9
17
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
![Page 12: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/12.jpg)
Execution
8
9
17
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
// print “17” on screen terminated with a new line
![Page 13: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/13.jpg)
FormalizationWe need three relations:(store, exp) -> v(store, stm) -> store’(store, prog) -> store’
More specifically, we write them as functions:E(st, e) = vS(st, s) = st’P(st, p) = st’
The definitions follows in next slides:
![Page 14: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/14.jpg)
FormalizationE(st, num) = num
E(st, id) = st (id)
E(st, e1+e2) = E(st, e1) + E(st, e2)
E(st, e1-e2) = E(st, e1) - E(st, e2)
E(st, e1*e2) = E(st, e1) * E(st, e2)
S(st, id=e) = st [id |-> E(st, e)]
S(st, print(e)) = st // print E(st, e)
P(st, stm prog) = P(S(st, stm), prog)
P(st, stm) = S(st, stm)
![Page 15: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/15.jpg)
Execution// we denote store as {x1|->n1, x2|->n2, …}
P({}, x=8; y=9; z=x+y; print(z))
![Page 16: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/16.jpg)
ExecutionP({}, x=8; y=9; z=x+y; print(z)) =>
P(S({}, x=8), y=9; z=x+y; print(z))
![Page 17: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/17.jpg)
ExecutionP({}, x=8; y=9; z=x+y; print(z)) =>
P(S({}, x=8), y=9; z=x+y; print(z)) =>
P({}[x |-> E({}, 8)], y=9; z=x+y; print(z))
![Page 18: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/18.jpg)
ExecutionP({}, x=8; y=9; z=x+y; print(z)) =>
P(S({}, x=8), y=9; z=x+y; print(z)) =>
P({}[x |-> E({}, 8)], y=9; z=x+y; print(z)) =>
P({}[x |-> 8], y = 9; z=x+y; print(z))
![Page 19: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/19.jpg)
ExecutionP({}, x=8; y=9; z=x+y; print(z)) =>
P(S({}, x=8), y=9; z=x+y; print(z)) =>
P({}[x |-> E({}, 8)], y=9; z=x+y; print(z)) =>
P({}[x |-> 8], y = 9; z=x+y; print(z)) =>
P({x|->8}, y=9; z=x+y; print(z))
![Page 20: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/20.jpg)
ExecutionP({}, x=8; y=9; z=x+y; print(z)) =>
P(S({}, x=8), y=9; z=x+y; print(z)) =>
P({}[x |-> E({}, 8)], y=9; z=x+y; print(z)) =>
P({}[x |-> 8], y = 9; z=x+y; print(z)) =>
P({x|->8}, y=9; z=x+y; print(z)) =>
// the rest leave to you
![Page 21: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/21.jpg)
CVM in C
![Page 22: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/22.jpg)
What’s a Store?// Just an ADT, :-)// file “store.h”
#ifndef STORE_H#define STORE_H
typdef struct store *store;
store newStore ();// st[id |-> n]void storeUpdate (store st, str id, nat n);// st(id)nat storeLookup (store st, str id);
#endif
![Page 23: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/23.jpg)
What’s a Store?// file “store.c”#include “linkedList.h”#include “store.h”
struct store{ linkedList list; // any representation works :-)};
// functions are straightforward
(x, 8) (y, 9) (z, 17)
![Page 24: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/24.jpg)
CVM Interface in C// file “cvm.h”
#ifndef CVM_H
#define CVM_H
#include “miniC.h”
void cvmRun (prog p);
#endif
![Page 25: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/25.jpg)
CVM Implementation in C// file “cvm.c”#include “miniC.h”#include “store.h” // the memory#include “cvm.h”
void cvmRun (prog p){ store st = newStore (); while (p not empty) { stm = …; // take one statement from p; runStm (st, stm); } return;}
![Page 26: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/26.jpg)
CVM Implementation in C// file “cvm.c” continued
void runStm (store st, stm s){ switch (s->kind){ case ASSIGN: { // x = e; nat n = runExp (st, e); storeUpdate (st, x, n); break; } case PRINT: { // print (e) … }}}
![Page 27: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/27.jpg)
CVM Implementation in C// file “cvm.c” continued
nat runExp (store st, exp e){ switch (e->kind){ case ADD: { // e1 + e2; nat n1 = runExp (st, e1); nat n2 = runExp (st, e2); nat n = n1+n2; return n; } case … : // other cases are similar }}
![Page 28: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/28.jpg)
Client Code#include “miniC.h”#include “cvm.h”
int main(){ // build the syntax tree for: // p = “x = 8; y = 9; z = x+y; print(z);”
cvmRun (p);
return 0;}
![Page 29: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/29.jpg)
PVM---miniPentium VM The design and implementation of a v
irtual machine PVM for miniPentium is similar Some modules may be even reused
say the “store” module And also, the Java implementation for
CVM and PVM is similar All leave to you
![Page 30: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/30.jpg)
Stack Machine
![Page 31: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/31.jpg)
What’s a Stack Machine?
A stack machine has few (if no) registers, and all computation is done on a stack hence the name popular in 60-70’ last century, but
rare now What’s the pros and cons of it?
Answer this after we implement it
![Page 32: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/32.jpg)
Stack Machine for CVM
SCVM (Stack-based C Virtual Machine) SCVM is a triple: (store, stack, prog)
the new part is a stack Operations on stack:
push, pop +, -, *, print (all implicit call pop)
+: push(stk, pop(stk)+pop(stk))
![Page 33: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/33.jpg)
FormalizationWe need three relations:(store, stack, exp) -> stack’(store, stack, stm) -> store’(store, stack, prog) -> store’
More specifically, we write them as functions:E(st, stk, e) = stk’S(st, stk, s) = st’P(st, stk, p) = st’
The definitions follows in next slides:
![Page 34: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/34.jpg)
FormalizationE(st, stk, num) = push(stk, num)E(st, stk, id) = push(stk, st (id))E(st, stk, e1+e2) = E(st, stk, e1); E(st, stk, e2); +E(st, stk, e1-e2) = E(st, stk, e1); E(st, stk, e2); -E(st, stk, e1*e2) = E(st, stk, e1); E(st, stk, e2); *
S(st, stk, id=e) = st [id |->(E(st, e); pop(stk)]S(st, stk, print(e)) = st // print E(st, stk, e)
P(st, stk, stm prog) = P(S(st, stm), stk, prog) P(st, stk, stm) = S(st, stk, stm)
![Page 35: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/35.jpg)
An Example
Right hand are a sample program, a store and a stack
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
top
![Page 36: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/36.jpg)
Execution
x = 8;
y = 9;
z = x+y;
print (z);
####top
![Page 37: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/37.jpg)
Execution
x = 8;
y = 9;
z = x+y;
print (z);
####
top 8
![Page 38: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/38.jpg)
Execution
8xx = 8;
y = 9;
z = x+y;
print (z);
####top
![Page 39: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/39.jpg)
Execution
8xx = 8;
y = 9;
z = x+y;
print (z);
####
top 9
![Page 40: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/40.jpg)
Execution
8
9xy
x = 8;
y = 9;
z = x+y;
print (z);
####top
![Page 41: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/41.jpg)
Execution
8
9xy
x = 8;
y = 9;
z = x+y;
print (z);
####
top 8
![Page 42: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/42.jpg)
Execution
8
9xy
x = 8;
y = 9;
z = x+y;
print (z);
####
top
8
9
![Page 43: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/43.jpg)
Execution
8
9xy
x = 8;
y = 9;
z = x+y;
print (z);
####
top 17
![Page 44: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/44.jpg)
Execution
8
9
17
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
####top
![Page 45: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/45.jpg)
Execution
8
9
17
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
####
top 17
![Page 46: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/46.jpg)
Execution
8
9
17
x
z
yx = 8;
y = 9;
z = x+y;
print (z);
// print “17” on screen terminated with a new line
####top
![Page 47: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/47.jpg)
Implementation
It’s relatively straightforward to implement all these above stuffs Leave to you
A stack-based virtual machine for miniPentium (SPVM) is similar However, some instructions should be re
vised to make the stack operations explicit
![Page 48: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/48.jpg)
Stack Machine Summary Tow machine models:
register machines (CVM, PVM) stack machines (SCVM, SPVM)
What are the pros and cons of stack machines and register machines?
Why real machines are register-based? Say: Pentium, MIPS, ARM, …
Why virtual machines are stack-based? Say: P-Code, JVM, CLR, …
![Page 49: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/49.jpg)
A Compiler miniVCfrom miniC to miniPentium
![Page 50: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/50.jpg)
What’s a Compiler?
A compiler is a program that takes as input programs written in one (typically high-level) language, and output equivalent programs written in another (typically low-level) language
High-levelPrograms
Low-levelPrograms
Compiler
![Page 51: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/51.jpg)
In Picture
Programs Programs
VM
Programs Programs
Compiler
Compiler1
Compiler2
![Page 52: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/52.jpg)
Relation
The relation is:miniVC: miniC -> miniPentium Defined via three code translation fun
ctions: P, S and E
miniC miniPentiumminiVC
![Page 53: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/53.jpg)
FormalizationP(stm prog) =
S(stm)P(prog)
P(stm) =S(stm)
S(x=e) =E(e)movl result, x
S(print(e)) =E(e)print result
![Page 54: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/54.jpg)
FormalizationE(id) =
movl id, result
E(num) =movl num, result
E(e1+e2) =E(e1)movl result, t_1 // t1 is freshE(e2)movl result, t_2 // t2 is fresh
addl t2, t1movl t1, result
![Page 55: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/55.jpg)
Example
x = 8;
y = 9;
z = x+y;
print (z);
![Page 56: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/56.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) =
S(x=8)
P( y=9; z=x+y; print (z); )
![Page 57: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/57.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) =
S(x=8)
P( y=9; z=x+y; print (z); )
![Page 58: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/58.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) =
S(x=8)
P( y=9; z=x+y; print (z); )
S(x=8) =
E(8)
movl result, x
![Page 59: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/59.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) =
S(x=8)
P( y=9; z=x+y; print (z); )
S(x=8) =
E(8)
movl result, x
![Page 60: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/60.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) =
S(x=8)
P( y=9; z=x+y; print (z); )
S(x=8) =
E(8)
movl result, x
S(x=8) =
movl 8, result
movl result, x
![Page 61: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/61.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x P( y=9; z=x+y; print (z); )
![Page 62: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/62.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x P( y=9; z=x+y; print (z); )
![Page 63: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/63.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x S( y=9;) P (z=x+y; print (z); )
![Page 64: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/64.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y P(z=x+y; print (z); )
![Page 65: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/65.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y S(z=x+y; ) P(print (z); )
![Page 66: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/66.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y E(x) movl result, t_1 E(y) movl result, t_2 addl t2, t1 movl t1, result P(print (z); )
![Page 67: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/67.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y movl x, result movl result, t_1 movl y, result movl result, t_2 addl t2, t1 movl t1, result P(print (z); )
![Page 68: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/68.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y movl x, result movl result, t_1 movl y, result movl result, t_2 addl t2, t1 movl t1, result P(print (z); )
![Page 69: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/69.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y movl x, result movl result, t_1 movl y, result movl result, t_2 addl t2, t1 movl t1, result S(print (z); )
![Page 70: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/70.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y movl x, result movl result, t_1 movl y, result movl result, t_2 addl t2, t1 movl t1, result E(z) print result
![Page 71: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/71.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y movl x, result movl result, t_1 movl y, result movl result, t_2 addl t2, t1 movl t1, result movl z, result print result
![Page 72: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/72.jpg)
Example
P( x=8; y=9; z = x+y; print (z); ) = movl 8, result movl result, x movl 9, result movl result, y movl x, result movl result, t_1 movl y, result movl result, t_2 addl t2, t1 movl t1, result movl z, result print result
![Page 73: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/73.jpg)
miniVC in C
![Page 74: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/74.jpg)
Interface// file “miniVC.h”
#ifndef MINIVC_H
#define MINIVC_H
#include “miniC.h”
#include “miniPentium.h”
miniPentium miniVC (miniC p);
#endif
![Page 75: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/75.jpg)
Implementation// file “miniVC.c”#include “linkedList.h”#include “miniVC.h”
static linkedList pool; // any rep’ will do
miniPentuim miniVC (miniC p){ while (p not empty){ stm s = …; // take one statement from p; compileStm (s); } return …; // return the Pentium program}
![Page 76: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/76.jpg)
Implementation// file “miniVC.c” contiuned
void compileStm (stm s){ switch (s->kind){ case ASSIGN: { // x = e; compileExp (e); enterPool (movl result, x); break; } case …; }}
![Page 77: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/77.jpg)
Implementation// file “miniVC.c” contiunedvoid compileExp (exp e){ switch (e->kind){ case ADD: { // e1 + e2; compileExp (e1); enterPool (movl result, t_1); compileExp (e2); enterPool (movl result, t_2); enterPool (addl t2, t1); enterPool (movl t1, result); break; } case …;}}
![Page 78: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/78.jpg)
Client Code#include “miniC.h”#include “miniPentium.h”#include “miniVC.h”
int main(){ // build the syntax tree for: // p = “x = 8; y = 9; z = x+y; print(z);” cvm (p); // print out 17
miniPentium q = compile (p); pvm (q); // print out 17
return 0;}
![Page 79: Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d445503460f94a208cd/html5/thumbnails/79.jpg)
Rest of the Story Type checker:
Not all programs are meaningful Ex: y=x; print (y);
Statically check whether a program is valid Optimizer:
The output of a compiler may be stupid Ex: too many movs, :-(
Decrease space use and increase performance Generate real Pentium code
Which called JIT: Just-In-Time compiler (a hot research field)
All leave to you