Een echte re stack-machine nMemory nRegisters R0 R1 R2 R3 R4 PC SP MP nMemory uProgram uStack uMemory nRegisters uProgr.counter uStack pointer uMark pointer
Instructies: load on stack R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT nLDC Load from constant
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register 43 nLDL Load from local
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register 43 nLDL Load from local 75 nLDS Load from stack
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register 43 nLDL Load from local 75 nLDS Load from stack 12 nLDLA Load local address
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register 43 nLDL Load from local 75 nLDS Load from stack 12 nLDLA Load local address nLDA Load via address
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register 43 nLDL Load from local 75 nLDS Load from stack 12 nLDLA Load local address 81 nLDA Load via address
Instructies: load on stack nLDC Load from constant R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 HALT 17 nLDR Load from register 43 nLDL Load from local 75 nLDS Load from stack 12 nLDLA Load local address 81 nLDA Load via address
Instructies: store from stack nLDC Load from constant nLDR Load from register nLDL Load from local nLDS Load from stack nLDLA Load local address nLDA Load via address nSTR Store to register nSTL Store to local nSTS Store to stack nSTA Store via address
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump)
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump) nBSR Branch to subroutine
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump) nBSR Branch to subroutine
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump) nBSR Branch to subroutine
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump) nBSR Branch to subroutine nRET Return from subroutine
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump) nBSR Branch to subroutine nRET Return from subroutine
Instructies: Register nLDRR Load reg. to reg. R0 R1 R2 R3 R4 PC SP MP LDC 17 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP 12 LDRR AJS -2 BRA +3 BSR -4 LDC 91 STR R3 RET LDR R3 nAJS Adjust stackpointer nBRA Branch (Jump) nBSR Branch to subroutine nRET Return from subroutine 91
Instructies: Arithmetic nADD nSUB nMUL nDIV nMOD Haal twee waarden van de stack, en vervang ze door het resultaat van… nAND nOR nXOR nEQ nNE nLT nGT nLE nGE nNOT nNEG één of
Instructies: Branch nBRA Onvoorwaardelijke sprong nBSR Zet eerst returnadres op stack nBRT Voorwaardelijke sprong: alleen als er True op stack staat nBRF Voorwaardelijke sprong: …False…
Codegeneratie voor Expressies * * ?: 12 == 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-Algebra code b = foldExpr codeAlg b where codeAlg :: ExpAlg Code = (\n ,\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) ] (Env Code) \e ee ee ee ye=y e; ze=z e; e ye ze ye ze \e d e ++ [STL (length e)] ++ b ((s,a):e) [ ]
Codegenereer-Algebra code b = foldStEx codeAlg b where 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] (Env Code) [ ] 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 ce concat [p e | p ps] ++ [BSR (f e m)]
Codegenereer-Algebra code b = foldStEx codeAlg b where 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] (Env Code) [ ] 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 ce concat [p e | p ps] ++ [BSR (f e m)]
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m nMethod-definitie m(7, 12); void m(int x, int y) { p(x+37); q(x*y); }
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); m: void m(int x, int y) { p(x+37); q(x*y); }
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); m: 7 37 void m(int x, int y) { p(x+37); q(x*y); }
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); m: void m(int x, int y) { p(x+37); q(x*y); }
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); void m(int x, int y) { p(x+37); q(x*y); } m: Wij ruimen onze eigen rommel op!
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); void m(int x, int y) { p(x+37); q(x*y); } m: Wij ruimen onze eigen rommel op!
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); void m(int x, int y) { p(x+37); q(x*y); } m: Wij ruimen onze eigen rommel op!
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); void m(int x, int y) { p(x+37); q(x*y); } m: Wij ruimen onze eigen rommel op!
Methodes met parameters nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDS -2 LDC BSR p LDS -2 STS -2 LDS -2 MUL BSR q AJS -1 ADD RET nMethod-definitie m(7, 12); void m(int x, int y) { p(x+37); q(x*y); } m: Wij ruimen onze eigen rommel op!
Methodes met n parameters nMethode-aanroep uZet parameters op de stack uCall nMethode-definitie uGebruik parameters … uRuim stack op uReturn BSR m LDS –(n+d)LDS –(1+d) STS –n AJS –(n-1) RET nMethode-aanroep uZet parameters op de stack uCall uRuim stack op nMethode-definitie uGebruik parameters … u Return BSR m RET AJS -n Alternatieve strategie: aanroeper ruimt op LDS –(n+d)LDS –(1+d)
Methode met lokale variabelen nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-definitie m(7, 12); void m(int x, int y) { int a, b; a = -x; …… } m: 7 12
Methode met lokale variabelen nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-definitie m(7, 12); void m(int x, int y) { int a, b; a = -x; …… } m: 7 12 MP=SP
Methode met lokale variabelen nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 SP=MP
Methode met lokale variabelen nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nMethod-aanroep R0 R1 R2 R3 R4 PC SP MP LDC 7 LDC 12 BSR m LDR MP nMethod-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 nCreëer ruimte voor lokalen uBewaar oude MP uMP wijst naar stukje stack nGebruik variabelen in de body uParameters uLokale variabelen nOpruimen uLokale variabelen uParameters LDR MP MP=SP AJS +k LDL –(n+1)LDL –2LDL +1LDL +k STR MP SP=MPSTS –n AJS –(n-1) RET LINK k UNLINK
Methoden met een resultaat nLaat het resultaat achter op de stack (nog meer gepruts bij het opruimen) nLaat het resultaat achter in R3 òf