een echte re stack-machine
Post on 12-Jan-2016
24 Views
Preview:
DESCRIPTION
TRANSCRIPT
Een echtere stack-machine
Memory
Registers
R0
R1
R2
R3
R4
PC
SP
MP
Memory Program Stack Memory
Registers Progr.counter Stack pointer Mark pointer
Instructies: load on stack
R0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
LDCLoad from constant
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
43
LDLLoad from local
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
43
LDLLoad from local
75
LDSLoad from stack
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
43
LDLLoad from local
75
LDSLoad from stack
12
LDLALoad local address
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
43
LDLLoad from local
75
LDSLoad from stack
12
LDLALoad local address
LDALoad via address
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
43
LDLLoad from local
75
LDSLoad from stack
12
LDLALoad local address
81
LDALoad via address
Instructies: load on stack
LDCLoad from constantR
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
HALT
17 LDR
Load from register
43
LDLLoad from local
75
LDSLoad from stack
12
LDLALoad local address
81
LDALoad via address
Instructies: store from stack
LDCLoad from constant
LDRLoad from register
LDLLoad from local
LDSLoad from stack
LDLALoad local address
LDALoad via address
STRStore to register
STLStore to local
STSStore to stack
STAStore via address
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
31
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75 12 81
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
43
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75 12 81
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
43
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75 12 81
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
43
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75 12 81
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
BSRBranch to subroutine
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
43
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75
81
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
BSRBranch to subroutine
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
43
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75
91
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
BSRBranch to subroutine
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
91
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75
91
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
BSRBranch to subroutine
RETReturn from subroutine
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
91
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75
91
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
BSRBranch to subroutine
RETReturn from subroutine
Instructies: Register
LDRRLoad reg. to reg.R
0
R1
R2
R3
R4
PC
SP
MP
91
43
59 63 75 81
LDC 17
LDR R4
LDL 2
LDS -3
LDLA 1
LDA 2
NOP
12
LDRR 3 4
17 43 75
91
AJS -2
BRA +3
BSR -4
LDC 91
STR R3
RET
LDR R3
AJSAdjust stackpointer
BRABranch (Jump)
BSRBranch to subroutine
RETReturn from subroutine
91
Instructies: Arithmetic
ADD SUB MUL DIV MOD
Haal twee waarden van de stack,
en vervang ze door het resultaat van…
AND OR XOR
EQ NE LT GT LE GE
NOT NEG
één of
Instructies: Branch
BRAOnvoorwaardelijke sprong
BSRZet eerst returnadres op stack
BRTVoorwaardelijke sprong:alleen als er True op stack staat
BRFVoorwaardelijke sprong: …False…
Codegeneratie voor Expressies
3 5
+
3 2
*
3 5
+ 32
*
3 5
+ 3 7
?:
1 2
== 3
3 5
+
LDC 3
LDC 5
ADD
LDC 3
LDC 5
ADD
LDC 2
MUL
LDC 3
LDC 5
ADD
LDC 2
MUL
LDC 1
LDC 2
EQ
BRF +2
LDC 7
BRA +3
LDC 3
LDC 5
ADD
Codegenereer-Algebracode b = foldExpr codeAlg bwhere codeAlg :: ExpAlg Code =(\n ,\x y ,\x y ,\x y ,\x y z
)
[LDC n] x ++ y ++ [ADD] x ++ y ++ [MUL] x ++ y ++ [EQ]
x ++[BRF (sy+1)] ++ y ++[BRA sz] ++ z
\e
data Expr= Con Int| Add Expr Expr| Mul Expr Expr| Eq Expr Expr| If Expr Expr Expr let sy=size y
; sz=size z in
,\s ,\s d b
| Var Str| Let Str Expr Expr
[LDL s ] [LDL (e?s) ]
(EnvCode)
\e \e \e \e \e
e ee e
ee
ye=y e;ze=z e;
e
yeze
yeze
\e d e ++ [STL (length e)] ++ b ((s,a):e)
[ ]
Codegenereer-Algebracode b = foldStEx codeAlg bwhere codeAlg :: StExAlg Code =((\s d \e ,\c b a \e
,\c b \e
,\m ps \e
), (…))
data Stat= Asmt Str Expr| If Expr Stat Stat
| While Expr Stat
| Call Str [Expr]
(EnvCode)[ ]
d e ++ [STL (e?s)] ce ++ [BRF(size be+1)]++be ++ [BRA(size ae)]++ae ce ++ [BRF (n+1)] ++be ++ [BRA –(n+k+2)]where n = size be k = size ceconcat [p e | pps]++ [BSR (f e m)]
Codegenereer-Algebracode b = foldStEx codeAlg bwhere codeAlg :: StExAlg Code =((\s d \e ,\c b a \e
,\c b \e
,\m ps \e
), (…))
data Stat= Asmt Str Expr| If Expr Stat Stat
| While Expr Stat
| Call Str [Expr]
(EnvCode)[ ]
d e ++ [STL (e?s)] ce ++ [BRF(size be)] ++be ++ [BRA(size ae)]++ae [BRA n] ++ be ++ ce++ [BRT –(n+k+1)]where n = size be k = size ceconcat [p e | pps]++ [BSR (f e m)]
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
Method-definitie
m(7, 12);
void m(int x, int y){ p(x+37); q(x*y);}
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
7 12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
m:void m(int x, int y){ p(x+37); q(x*y);}
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
7 12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
m:
7 37
void m(int x, int y){ p(x+37); q(x*y);}
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
7 12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
m:
44 37
void m(int x, int y){ p(x+37); q(x*y);}
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
7 12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
void m(int x, int y){ p(x+37); q(x*y);}
m:
44 37
Wij ruimen onze eigen rommel op!
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
7 12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
void m(int x, int y){ p(x+37); q(x*y);}
m:
96 12
Wij ruimen onze eigen rommel op!
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
void m(int x, int y){ p(x+37); q(x*y);}
m:
96 12
Wij ruimen onze eigen rommel op!
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
void m(int x, int y){ p(x+37); q(x*y);}
m:
96 12
Wij ruimen onze eigen rommel op!
Methodes met parameters
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDS -2
LDC 37
12
BSR p
LDS -2
STS -2
LDS -2
MUL
BSR q
AJS -1
ADD
RET
Method-definitie
m(7, 12);
void m(int x, int y){ p(x+37); q(x*y);}
m:
96 12
Wij ruimen onze eigen rommel op!
Methodes met n parameters
Methode-aanroep Zet parameters
op de stack Call
Methode-definitie
Gebruik parameters …
Ruim stack op
Return
BSR m
LDS –(n+d) LDS –(1+d)
STS –n
AJS –(n-1)
RET
Methode-aanroep Zet parameters
op de stack Call Ruim stack op
Methode-definitie Gebruik parameters
… Return
BSR m
RET
AJS -n
Alternatieve strategie:
aanroeper ruimt op
LDS –(n+d) LDS –(1+d)
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
AJS +2
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
AJS +2
LDL -3
NEG
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
-7
SP=MP
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
SP=MP
STR MP
-7
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
7 12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
SP=MP
STR MP
-7
STS -2
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
SP=MP
STR MP
-7
STS -2
AJS -1
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
SP=MP
STR MP
-7
STS -2
AJS -1
RET
Methode met lokale variabelen
Method-aanroepR0
R1
R2
R3
R4
PC
SP
MP
LDC 7
LDC 12
BSR m
LDR MP
Method-definitie
m(7, 12);
void m(int x, int y){ int a, b; a = -x; ……}
m:
12
MP=SP
AJS +2
LDL -3
NEG
-7
STL +1
SP=MP
STR MP
-7
STS -2
AJS -1
RET
Methode met n parameters en k lokale variabelen
Creëer ruimte voor lokalen Bewaar oude MP MP wijst naar stukje stack
Gebruik variabelen in de body Parameters Lokale variabelen
Opruimen Lokale variabelen Parameters
LDR MP
MP=SP
AJS +k
LDL –(n+1)LDL –2
LDL +1 LDL +k
STR MP
SP=MP
STS –n
AJS –(n-1) RET
LINK k
UNLINK
Methoden met een resultaat
Laat het resultaat achter op de stack(nog meer gepruts bij het opruimen)
Laat het resultaat achter in R3
òf
top related