1 / 48

Een echte re stack-machine

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.

kasi
Download Presentation

Een echte re stack-machine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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…

  22. + * * ?: 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

  23. \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  ) [ ] (EnvCode) [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)

  24. 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  ), (…)) [ ] (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)]

  25. 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  ), (…)) [ ] (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)]

  26. 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); }

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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; …… }

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. Methoden met een resultaat • Laat het resultaat achter op de stack(nog meer gepruts bij het opruimen) • Laat het resultaat achter in R3 òf

More Related