abstract machines
play

Abstract machines 270/593 G. Castagna (CNRS) Cours de - PowerPoint PPT Presentation

Abstract machines 270/593 G. Castagna (CNRS) Cours de Programmation Avance 270 / 593 Outline 21 A simple stack machine 22 The SECD machine 23 Adding Tail Call Elimination 24 The Krivine Machine 25 The lazy Krivine machine 26 Eval-apply vs.


  1. Abstract machines 270/593 G. Castagna (CNRS) Cours de Programmation Avancée 270 / 593

  2. Outline 21 A simple stack machine 22 The SECD machine 23 Adding Tail Call Elimination 24 The Krivine Machine 25 The lazy Krivine machine 26 Eval-apply vs. Push-enter 27 The ZAM 28 Stackless Machine for CPS terms 271/593 G. Castagna (CNRS) Cours de Programmation Avancée 271 / 593

  3. Execution models for a language Interpretation: control (sequencing of computations) is expressed by a 1 term of the source language, represented by a tree-shaped data structure. The interpreter traverses this tree during execution. Compilation to native code: control is compiled to a sequence of 2 machine instructions, before execution. These instructions are those of a real microprocessor and are executed in hardware. Compilation to abstract machine code: control is compiled to a 3 sequence of instructions. These instructions are those of an abstract machine. They do not correspond to that of an existing hardware processor, but are chosen close to the basic operations of the source language.Yet another example of program transformations between different languages 272/593 G. Castagna (CNRS) Cours de Programmation Avancée 272 / 593

  4. Execution models for a language Interpretation: control (sequencing of computations) is expressed by a 1 term of the source language, represented by a tree-shaped data structure. The interpreter traverses this tree during execution. Compilation to native code: control is compiled to a sequence of 2 machine instructions, before execution. These instructions are those of a real microprocessor and are executed in hardware. Compilation to abstract machine code: control is compiled to a 3 sequence of instructions. These instructions are those of an abstract machine. They do not correspond to that of an existing hardware processor, but are chosen close to the basic operations of the source language.Yet another example of program transformations between different languages 272/593 G. Castagna (CNRS) Cours de Programmation Avancée 272 / 593

  5. Execution models for a language Interpretation: control (sequencing of computations) is expressed by a 1 term of the source language, represented by a tree-shaped data structure. The interpreter traverses this tree during execution. Compilation to native code: control is compiled to a sequence of 2 machine instructions, before execution. These instructions are those of a real microprocessor and are executed in hardware. Compilation to abstract machine code: control is compiled to a 3 sequence of instructions. These instructions are those of an abstract machine. They do not correspond to that of an existing hardware processor, but are chosen close to the basic operations of the source language.Yet another example of program transformations between different languages Next: short overview of abstract machines for functional languages 272/593 G. Castagna (CNRS) Cours de Programmation Avancée 272 / 593

  6. Outline 21 A simple stack machine 22 The SECD machine 23 Adding Tail Call Elimination 24 The Krivine Machine 25 The lazy Krivine machine 26 Eval-apply vs. Push-enter 27 The ZAM 28 Stackless Machine for CPS terms 273/593 G. Castagna (CNRS) Cours de Programmation Avancée 273 / 593

  7. Abstract machine for arithmetic expressions Arithmetic expressions: a :: “ N | a ` a | a ´ a Machine Components A code pointer 1 A stack 2 Instruction set: CONST( N ) push integer N on stack ADD pop two integers, push their sum SUB pop two integers, push their difference 274/593 G. Castagna (CNRS) Cours de Programmation Avancée 274 / 593

  8. Compilation scheme Compilation (translation of expressions to sequences of instructions) is just translation to “reverse Polish notation”: rr N ss “ CONST( N ) rr a 1 ` a 2 ss “ rr a 1 ss ; rr a 2 ss ; ADD rr a 1 ´ a 2 ss “ rr a 1 ss ; rr a 2 ss ; SUB Example [ 5 ´ (1 + 2)] [ ] = CONST(5) ; CONST(1) ; CONST(2) ; ADD ; SUB 275/593 G. Castagna (CNRS) Cours de Programmation Avancée 275 / 593

  9. Transitions BEFORE AFTER Code Stack Code Stack CONST( N ) ; c s c N . s p n 1 ` n 2 q . s ADD ; c n 2 . n 1 . s c p n 1 ´ n 2 q . s SUB ; c n 2 . n 1 . s c 276/593 G. Castagna (CNRS) Cours de Programmation Avancée 276 / 593

  10. Transitions BEFORE AFTER Code Stack Code Stack CONST( N ) ; c s c N . s p n 1 ` n 2 q . s ADD ; c n 2 . n 1 . s c p n 1 ´ n 2 q . s SUB ; c n 2 . n 1 . s c Let us try to execute the compilation of 5 ´p 1 ` 2 q with an empty stack 276/593 G. Castagna (CNRS) Cours de Programmation Avancée 276 / 593

  11. Transitions BEFORE AFTER Code Stack Code Stack CONST( N ) ; c s c N . s p n 1 ` n 2 q . s ADD ; c n 2 . n 1 . s c p n 1 ´ n 2 q . s SUB ; c n 2 . n 1 . s c Let us try to execute the compilation of 5 ´p 1 ` 2 q with an empty stack Code Stack CONST(5) ; CONST(1) ; CONST(2) ; ADD ; SUB ε CONST(1) ; CONST(2) ; ADD ; SUB 5 CONST(2) ; ADD ; SUB 1.5 ADD ; SUB 2.1.5 SUB 3.5 ε 2 276/593 G. Castagna (CNRS) Cours de Programmation Avancée 276 / 593

  12. Transitions BEFORE AFTER Code Stack Code Stack CONST( N ) ; c s c N . s p n 1 ` n 2 q . s ADD ; c n 2 . n 1 . s c p n 1 ´ n 2 q . s SUB ; c n 2 . n 1 . s c Let us try to execute the compilation of 5 ´p 1 ` 2 q with an empty stack Code Stack CONST(5) ; CONST(1) ; CONST(2) ; ADD ; SUB ε CONST(1) ; CONST(2) ; ADD ; SUB 5 CONST(2) ; ADD ; SUB 1.5 ADD ; SUB 2.1.5 SUB 3.5 ε 2 Notice the right-to-left execution order 276/593 G. Castagna (CNRS) Cours de Programmation Avancée 276 / 593

  13. Outline 21 A simple stack machine 22 The SECD machine 23 Adding Tail Call Elimination 24 The Krivine Machine 25 The lazy Krivine machine 26 Eval-apply vs. Push-enter 27 The ZAM 28 Stackless Machine for CPS terms 277/593 G. Castagna (CNRS) Cours de Programmation Avancée 277 / 593

  14. SECD: abstract-machine for call by value Machine Components A code pointer 1 An environment 2 A stack 3 Instruction set: (+ previous arithmetic operations) ACCESS( n ) push n-th field of the environment CLOSURE( c ) push closure of code c with current environment LET pop value and add it to environment ENDLET discard first entry of environment APPLY pop function closure and argument, perform application RETURN terminate current function, jump back to caller Historical note: (S)tack, (E)nvironment, (C)ontrol, (D)ump. (SCD) are implemented by stacks, (E) is an array. (C) is our code pointer, (D) is the return stack as in the first version of the ZAM later on. 278/593 G. Castagna (CNRS) Cours de Programmation Avancée 278 / 593

  15. Compilation scheme rr n ss “ ACCESS( n ) rr λ a ss “ CLOSURE( rr a ss ; RETURN) rr let a in b ss “ rr a ss ; LET ; rr b ss ; ENDLET rr ab ss “ rr a ss ; rr b ss ; APPLY (constants and arithmetic as before) 279/593 G. Castagna (CNRS) Cours de Programmation Avancée 279 / 593

  16. Compilation scheme rr n ss “ ACCESS( n ) rr λ a ss “ CLOSURE( rr a ss ; RETURN) rr let a in b ss “ rr a ss ; LET ; rr b ss ; ENDLET rr ab ss “ rr a ss ; rr b ss ; APPLY (constants and arithmetic as before) Example Term: p λ p 0 ` 1 qq 2 (i.e., p λ x . x ` 1 q 2) Code: CLOSURE(ACCESS(0);CONST(1);ADD;RETURN) ; CONST(2) ; APPLY 279/593 G. Castagna (CNRS) Cours de Programmation Avancée 279 / 593

  17. Transitions BEFORE AFTER Code Env Stack Code Env Stack e p n q . s ACCESS(n) ; c e s c e LET ; c e v . s c v . e s ENDLET ; c v . e s c e s CLOSURE( c 1 ) ; c c 1 r e s . s e s c e v . c 1 r e 1 s . s c 1 v . e 1 c . e . s APPLY ; c e v . c 1 . e 1 . s c 1 e 1 RETURN ; c v . s e where c r e s denotes the closure of code c with environment e . 280/593 G. Castagna (CNRS) Cours de Programmation Avancée 280 / 593

  18. Example Code: CLOSURE( c ) ; CONST(2) ; APPLY where: c = ACCESS(0) ; CONST(1) ; ADD ; RETURN Code Env Stack CLOSURE( c ) ; CONST(2) ; APPLY e s c r e s . s CONST(2) ; APPLY e APPLY 2 . c r e s . s e ε . e . s c 2 . e CONST(1) ; ADD ; RETURN 2 . e 2 . ε . e . s ADD ; RETURN 2 . e 1 . 2 . ε . e . s 2 . e 3 . ε . e . s RETURN ε e 3 . s 281/593 G. Castagna (CNRS) Cours de Programmation Avancée 281 / 593

  19. Soundness Of course we always have to show that the compilation is correct, in the sense that it preserves the semantics of the reduction. This is stated as follows Theorem (soundness of SECD) If e ñ v then the SECD machine in the state prr e ss , ε , ε q reduces to the state p ε , ε , ¯ v q , where ¯ v is the machine value for v (the same integer for an integer, and the corresponding closure for a λ -abstraction.) (where ñ is the call-by-value, weak-reduction, big-step semantics defined in the “Refresher course on operational semantics”) 282/593 G. Castagna (CNRS) Cours de Programmation Avancée 282 / 593

  20. Outline 21 A simple stack machine 22 The SECD machine 23 Adding Tail Call Elimination 24 The Krivine Machine 25 The lazy Krivine machine 26 Eval-apply vs. Push-enter 27 The ZAM 28 Stackless Machine for CPS terms 283/593 G. Castagna (CNRS) Cours de Programmation Avancée 283 / 593

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend