480 likes | 575 Vues
PC. R0. SP. R1. MP. R2. R3. R4. Een echte re stack-machine. Memory Registers. Memory Program Stack Memory Registers Progr.counter Stack pointer Mark pointer. PC. R0. SP. R1. MP. R2. R3. R4. Instructies: load on stack. LDC Load from constant. LDC 17. 12. LDR R4.
E N D
PC R0 SP R1 MP R2 R3 R4 Een echtere stack-machine • Memory • Registers • Memory • Program • Stack • Memory • Registers • Progr.counter • Stack pointer • Mark pointer
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 LDL 2 LDS -3 LDLA 1 LDA 2 NOP HALT 59 63 75 31 81 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 LDS -3 LDLA 1 LDA 2 NOP HALT 59 63 75 31 81 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 43 LDS -3 LDLA 1 • LDLLoad from local LDA 2 NOP HALT 59 63 75 31 81 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 43 LDS -3 75 LDLA 1 • LDLLoad from local LDA 2 NOP HALT • LDSLoad from stack 59 63 75 31 81 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 43 LDS -3 75 LDLA 1 12 • LDLLoad from local LDA 2 NOP HALT • LDSLoad from stack 59 63 75 • LDLALoad local address 31 81 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 43 LDS -3 75 LDLA 1 12 • LDLLoad from local LDA 2 NOP HALT • LDSLoad from stack 59 63 75 • LDLALoad local address 31 81 • LDALoad via address 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 43 LDS -3 75 LDLA 1 12 • LDLLoad from local LDA 2 81 NOP HALT • LDSLoad from stack 59 63 75 • LDLALoad local address 31 81 • LDALoad via address 43
PC R0 SP R1 MP R2 R3 R4 Instructies: load on stack • LDCLoad from constant LDC 17 12 LDR R4 17 • LDRLoad from register LDL 2 43 LDS -3 75 LDLA 1 12 • LDLLoad from local LDA 2 81 NOP HALT • LDSLoad from stack 59 63 75 • LDLALoad local address 31 81 • LDALoad via address 43
STRStore to register • STLStore to local • STSStore to stack • STAStore via address Instructies: store from stack • LDCLoad from constant • LDRLoad from register • LDLLoad from local • LDSLoad from stack • LDLALoad local address • LDALoad via address
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 LDL 2 43 LDS -3 75 LDLA 1 12 LDA 2 81 NOP LDRR3 4 AJS -2 59 BRA +3 63 LDC 91 75 STR R3 31 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 12 LDA 2 81 NOP LDRR3 4 AJS -2 59 BRA +3 63 LDC 91 75 STR R3 43 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 12 • BRABranch (Jump) LDA 2 81 NOP LDRR3 4 AJS -2 59 BRA +3 63 LDC 91 75 STR R3 43 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 12 • BRABranch (Jump) LDA 2 81 NOP LDRR3 4 • BSRBranch to subroutine AJS -2 59 BRA +3 63 LDC 91 75 STR R3 43 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 • BRABranch (Jump) LDA 2 81 NOP LDRR3 4 • BSRBranch to subroutine AJS -2 59 BRA +3 63 LDC 91 75 STR R3 43 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 • BRABranch (Jump) LDA 2 91 NOP LDRR3 4 • BSRBranch to subroutine AJS -2 59 BRA +3 63 LDC 91 75 STR R3 43 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 • BRABranch (Jump) LDA 2 91 NOP LDRR3 4 • BSRBranch to subroutine AJS -2 59 BRA +3 63 LDC 91 75 • RETReturn from subroutine STR R3 91 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 • BRABranch (Jump) LDA 2 91 NOP LDRR3 4 • BSRBranch to subroutine AJS -2 59 BRA +3 63 LDC 91 75 • RETReturn from subroutine STR R3 91 81 RET BSR -4 LDR R3 43
PC R0 SP R1 MP R2 R3 R4 Instructies: Register • LDRRLoad reg. to reg. LDC 17 12 LDR R4 17 • AJSAdjust stackpointer LDL 2 43 LDS -3 75 LDLA 1 91 • BRABranch (Jump) LDA 2 91 NOP LDRR3 4 • BSRBranch to subroutine AJS -2 59 BRA +3 63 LDC 91 75 • RETReturn from subroutine STR R3 91 81 RET BSR -4 LDR R3 43
Instructies: Arithmetic Haal twee waarden van de stack, en vervang ze door het resultaat van… één of • ADD • SUB • MUL • DIV • MOD • AND • OR • XOR • EQ • NE • LT • GT • LE • GE • NOT • NEG
Instructies: Branch • BRA Onvoorwaardelijke sprong • BSR Zet eerst returnadres op stack • BRT Voorwaardelijke sprong: alleen als er True op stack staat • BRF Voorwaardelijke sprong: …False…
+ * * ?: 3 5 3 2 2 3 3 7 3 + + == + 3 5 3 5 1 2 3 5 LDC 1 LDC 2 LDC 3 LDC 3 LDC 2 LDC 5 LDC 5 LDC 3 EQ ADD ADD LDC 5 BRF +2 LDC 2 ADD LDC 7 MUL MUL BRA +3 LDC 3 LDC 5 ADD Codegeneratie voor Expressies
\e \e e e \e e e \e e e \e ye ze e ye ze Codegenereer-Algebra dataExpr = Con Int | Add Expr Expr | MulExpr Expr | EqExpr Expr | IfExpr Expr Expr code b = foldExpr codeAlg b where codeAlg :: ExpAlg Code = (\n ,\x y ,\x y ,\x y ,\x y z ) [ ] (EnvCode) [LDC n] x ++ y ++ [ADD] x ++ y ++ [MUL] x ++ y ++ [EQ] ye=y e; ze=z e; let sy=size y ; sz=size z in x ++[BRF (sy+1)] ++ y ++[BRA sz] ++ z | Var Str | Let Str Expr Expr ,\s ,\s d b \e [LDL (e?s) ] [LDL s ] \e d e ++ [STL (length e)] ++ b ((s,a):e)
Codegenereer-Algebra dataStat = Asmt Str Expr | IfExpr Stat Stat |While Expr Stat |CallStr[Expr] code b = foldStEx codeAlg b where codeAlg :: StExAlg Code = ((\s d \e ,\c b a \e ,\c b \e ,\m ps \e ), (…)) [ ] (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 ce concat [p e | pps] ++ [BSR (f e m)]
Codegenereer-Algebra dataStat = Asmt Str Expr | IfExpr Stat Stat |While Expr Stat |CallStr[Expr] code b = foldStEx codeAlg b where codeAlg :: StExAlg Code = ((\s d \e ,\c b a \e ,\c b \e ,\m ps \e ), (…)) [ ] (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 ce concat [p e | pps] ++ [BSR (f e m)]
LDC 7 LDC 12 PC R0 BSR m SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep m(7, 12); • Method-definitie void m(int x, int y) { p(x+37); q(x*y); }
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m 7 • Method-definitie 37 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m 44 • Method-definitie 37 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m 44 • Method-definitie 37 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL Wij ruimen onze eigen rommel op! BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m 96 • Method-definitie 12 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL Wij ruimen onze eigen rommel op! BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 m(7, 12); LDC 12 12 BSR m 96 • Method-definitie 12 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL Wij ruimen onze eigen rommel op! BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 m(7, 12); LDC 12 12 BSR m 96 • Method-definitie 12 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL Wij ruimen onze eigen rommel op! BSR q STS -2 AJS -1 RET
PC R0 SP R1 MP R2 R3 R4 Methodes met parameters • Method-aanroep LDC 7 m(7, 12); LDC 12 12 BSR m 96 • Method-definitie 12 m: LDS -2 void m(int x, int y) { p(x+37); q(x*y); } LDC 37 ADD BSR p LDS -2 LDS -2 MUL Wij ruimen onze eigen rommel op! BSR q STS -2 AJS -1 RET
Methode-aanroep Zet parametersop de stack Call Methode-definitie Gebruik parameters … Ruim stack op Return • Methode-aanroep • Zet parametersop de stack • Call • Ruim stack op • Methode-definitie • Gebruik parameters … • Return BSR m AJS -n LDS –(n+d) LDS –(1+d) RET Alternatieve strategie:aanroeper ruimt op Methodes met n parameters BSR m LDS –(n+d) LDS –(1+d) STS –n AJS –(n-1) RET
PC R0 SP R1 MP R2 R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie m: LDR MP void m(int x, int y) { int a, b; a = -x; …… }
PC R0 SP R1 MP R2 R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP
PC R0 SP R1 MP R2 R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP AJS +2
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP AJS +2
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie -7 m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1 SP=MP
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie -7 m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1 SP=MP STR MP
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 7 m(7, 12); LDC 12 12 BSR m • Method-definitie -7 m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1 SP=MP STR MP STS -2
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 m(7, 12); LDC 12 12 BSR m • Method-definitie -7 m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1 SP=MP STR MP STS -2 AJS -1
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 m(7, 12); LDC 12 12 BSR m • Method-definitie -7 m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1 SP=MP STR MP STS -2 AJS -1 RET
PC R0 SP R1 MP R2 LDL -3 NEG R3 R4 Methode met lokale variabelen • Method-aanroep LDC 7 m(7, 12); LDC 12 12 BSR m • Method-definitie -7 m: LDR MP void m(int x, int y) { int a, b; a = -x; …… } MP=SP -7 AJS +2 STL +1 SP=MP STR MP STS -2 AJS -1 RET
MP=SP AJS +k LDL –(n+1) LDL –2 LDL +1 LDL +k SP=MP STR MP STS –n AJS –(n-1) Methode met n parameters en k lokale variabelen LINK k • 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 UNLINK RET
Methoden met een resultaat • Laat het resultaat achter op de stack(nog meer gepruts bij het opruimen) • Laat het resultaat achter in R3 òf