implementing the forth inner interpreter in high
play

Implementing the Forth Inner Interpreter in High Printing inline - PowerPoint PPT Presentation

Implementing the Forth Inner Interpreter in High Level Forth Introduction EuroForth 2016, Reichenau/Konstanz Threaded Code Number literals Printing string literals Control structures U. Hoffmann <uh@fh-wedel.de> Limitations


  1. Implementing the Forth Inner Interpreter in High Level Forth Introduction EuroForth 2016, Reichenau/Konstanz Threaded Code Number literals Printing string literals Control structures U. Hoffmann <uh@fh-wedel.de> Limitations Implementation FH Wedel Inner Interpreter Return stack operations Inline number literals Implementing the Forth Inner Interpreter in High Printing inline string literals Control structures Level Forth Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 1 / 22

  2. Overview Introduction Implementing the Forth Inner Threaded Code Interpreter in High Level Forth Number literals Introduction Printing string literals Threaded Code Control structures Number literals Printing string literals Limitations Control structures Limitations Implementation Implementation Inner Interpreter Inner Interpreter Return stack operations Inline number literals Return stack operations Printing inline string literals Control structures Inline number literals Compiler to threaded code Interpreter in threaded code Printing inline string literals Conclusion Control structures Demo Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 2 / 22

  3. Introduction Implementing the Forth Inner Interpreter in High Level Forth Introduction Threaded Code Number literals ◮ development of hard real time system Printing string literals Control structures Limitations ◮ desire to have an interactive command shell while still meeting Implementation the real time requirements Inner Interpreter Return stack operations ◮ idea: have a controllable Forth interpreter (inner & outer) Inline number literals stepwise execution Printing inline string literals Control structures Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 3 / 22

  4. Threaded Code Implementing the Forth Inner ◮ words of underlying system become primitives Interpreter in High Level Forth ◮ only threaded code no structure of header/dictionary Introduction Threaded Code ~: sq ( x - x ) dup * ~; Number literals Printing string literals Control structures Limitations body Implementation of sq Inner Interpreter |-----------+---------+-------------| Return stack operations Inline number literals | xt of dup | xt of * | xt of ~exit | Printing inline string literals |-----------+---------+-------------| Control structures Compiler to threaded code ^ Interpreter in threaded code | Conclusion | Demo |----| | IP | Interpreter pointer |----| U. Hoffmann Kap. 4 / 22

  5. Number literals Implementing the Forth Inner Interpreter in High Level Forth Introduction Threaded Code Number literals ~: test ( - u ) 3 4 + ~; Printing string literals Control structures Limitations body Implementation of test Inner Interpreter Return stack operations |------------+---+------------+---+---------+-------------| Inline number literals | xt of ~lit | 3 | xt of ~lit | 4 | xt of + | xt of ~EXIT | Printing inline string literals Control structures |------------+---+------------+---+---------+-------------| Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 5 / 22

  6. Printing string literals Implementing the Forth Inner Interpreter in High Level Forth Introduction Threaded Code Number literals Printing string literals ~: test3 ( - ) ~." it works" ~; Control structures Limitations Implementation body Inner Interpreter of test3 aligned Return stack operations |------------+---+-----+-----+-----+-----+-----+---+-------------| Inline number literals | xt of (~." | 8 | ’i’ | ’t’ | ’ ’ | ... | ’s’ | | xt of ~exit | Printing inline string literals |------------+---+-----+-----+-----+-----+-----+---+-------------| Control structures Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 6 / 22

  7. Control structures Implementing the Forth Inner : looptest ( -- ) Interpreter in High Level Forth 0 10 ~BEGIN Introduction 1- dup Threaded Code ~WHILE Number literals Printing string literals swap over + swap Control structures Limitations ~REPEAT drop ~; Implementation Inner Interpreter Return stack operations |------------+---+------------+----| Inline number literals A | xt of ~lit | 0 | xt of ~lit | 10 | Printing inline string literals |------------+---+------------+----| Control structures Compiler to threaded code |----------+-----------+----------------+--------------| Interpreter in threaded code B | xt of 1- | xt of dup | xt of ~?branch | address of D | Conclusion |----------+-----------+----------------+--------------| Demo |---------+------------+---------+------------+---------------+--------------| C | xt swap | xt of over | xt of + | xt of swap | xt of ~branch | address of B | |---------+------------+---------+------------+---------------+--------------| |---------+-------------| D | xt drop | xt of ~exit | |---------+-------------| U. Hoffmann Kap. 7 / 22

  8. Limitations Implementing the Forth Inner Interpreter in High Level Forth Introduction Threaded Code Number literals ◮ (user) variables or constants. Printing string literals Control structures Limitations ◮ DO LOOP s. Implementation ◮ Neither defining words nor DOES> Inner Interpreter Return stack operations ◮ a primitive for pushing address and length of string literals on the Inline number literals Printing inline string literals stack (like S" ). Control structures Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 8 / 22

  9. Implementation Implementing the Forth Inner Interpreter in High Level Forth Introduction Threaded Code Number literals Printing string literals ◮ Inner Interpreter Control structures Limitations ◮ Return Stack Implementation ◮ Compiler to Threaded Code Inner Interpreter Return stack operations Inline number literals ◮ Outer Interpreter in Threaded Code Printing inline string literals Control structures Compiler to threaded code Interpreter in threaded code Conclusion Demo U. Hoffmann Kap. 9 / 22

  10. Inner Interpreter Implementing the Forth Inner Interpreter in High Level Forth Introduction Variable IP 0 IP ! Threaded Code Number literals Printing string literals Control structures \ Perform a single interpretation step Limitations : step ( i*x -- j*x ) Implementation IP @ dup cell+ IP ! Inner Interpreter Return stack operations @ catch Inline number literals ?dup IF cr ." Error " . reset THEN ; Printing inline string literals Control structures Compiler to threaded code \ Loop steps Interpreter in threaded code Conclusion : run ( i*x -- j*x ) BEGIN IP @ WHILE step REPEAT ; Demo U. Hoffmann Kap. 10 / 22

  11. Interpreter State Implementing the Forth Inner Interpreter in High Level Forth Introduction ◮ Interpreter Pointer IP : Threaded Code Number literals Variable IP 0 IP ! Printing string literals Control structures Limitations ◮ ~R0 with corresponding return stack pointer RP . Implementation Inner Interpreter Create ~R0 20 cells allot Return stack operations Inline number literals Variable RP ~R0 RP ! Printing inline string literals Control structures ◮ a data stack shared with the underlying system Compiler to threaded code Interpreter in threaded code ◮ memory (code and data) also shared with the underlying system. Conclusion Demo U. Hoffmann Kap. 11 / 22

  12. Return stack operations Implementing the Forth Inner Interpreter in High Level Forth Create ~R0 20 cells allot Introduction Variable RP ~R0 RP ! Threaded Code Number literals Printing string literals Control structures Limitations Implementation Inner Interpreter Return stack operations : ~>r ( x -- ) Inline number literals Printing inline string literals \ push a cell to the return stack Control structures RP @ ! 1 cells RP +! ; Compiler to threaded code Interpreter in threaded code Conclusion : ~r> ( -- x ) \ pop a cell from the return stack Demo -1 cells RP +! RP @ @ ; U. Hoffmann Kap. 12 / 22

  13. Inline number literals Implementing the Forth Inner Interpreter in High Level Forth Introduction |----| Threaded Code | IP |--------------+...... Number literals |----| | . Printing string literals Control structures | . Limitations V V Implementation |-----+------------+-----+-----| Inner Interpreter Return stack operations | ... | xt of ~lit | val | ... | Inline number literals |-----+------------+-----+-----| Printing inline string literals Control structures Compiler to threaded code Interpreter in threaded code : ~lit ( -- n ) Conclusion \ extract inline number literal Demo IP @ @ 1 cells IP +! ; U. Hoffmann Kap. 13 / 22

Recommend


More recommend