Metaprogramming: Interpretaaon Interpreters

Metaprogramming: Interpretaaon Interpreters

How to implement a programming language Metaprogramming InterpretaAon An interpreter wriDen in the implementaAon language reads a program wriDen in the source language and evaluates it. These slides borrow heavily from Ben Wood’s Fall ‘15 slides. TranslaAon (a.k.a. compilaAon) An translator (a.k.a. compiler) wriDen in the implementaAon language reads a program wriDen in the source language and CS251 Programming Languages translates it to an equivalent program in the target language. Spring 2018, Lyn Turbak But now we need implementaAons of: Department of Computer Science Wellesley College implementaAon language target language Metaprogramming 2 Metaprogramming: InterpretaAon Interpreters Source Program Interpreter = Program in Interpreter Machine M virtual machine language L for language L Output on machine M Data Metaprogramming 3 Metaprogramming 4 Metaprogramming: TranslaAon Compiler C Source x86 Target Program C Compiler Program if (x == 0) { cmp (1000), $0 Program in Program in x = x + 1; bne L language A } add (1000), $1 A to B translator language B ... L: ... x86 Target Program x86 computer Output Interpreter Machine M Thanks to Ben Wood for these for language B Data and following pictures on machine M Metaprogramming 5 Metaprogramming 6 Interpreters vs Compilers Java Compiler Interpreters No work ahead of Lme Source Target Incremental Program Java Compiler Program maybe inefficient if (x == 0) { load 0 Compilers x = x + 1; ifne L } load 0 All work ahead of Lme ... inc See whole program (or more of program) store 0 Time and resources for analysis and opLmizaLon L: ... (compare compiled C to compiled Java) Metaprogramming 7 Metaprogramming 8 Compilers... whose output is interpreted Interpreters... that use compilers. Source Target Source Program Java Compiler Program Program Compiler Target Target Program Program Virtual Output Java Machine Virtual Output Machine Data Data Doesn’t this look familiar? Metaprogramming 9 Metaprogramming 10 JIT Compilers and OpAmizaAon Virtual Machine Model Source Program High-Level Language Program • HotSpot JVM Just In Time • Jikes RVM Ahead-of-Lme Compiler Bytecode Compiler • SpiderMonkey compiler compiler • v8 compile Ame • Transmeta Virtual Machine Language run Ame Target Performance • ... JIT Program Monitor Virtual machine compiler Output (interpreter) Virtual Machine Data NaLve Machine Language Metaprogramming 11 Metaprogramming 12 Typical Compiler How to implement a programming language Can describe by deriving a “proof” of the implementaLon Source Lexical Analyzer Program using these inference rules: Syntax Analyzer Interpreter Rule Semantic Analyzer Analysis P-in-L program L interpreter machine P machine Intermediate Code Synthesis Generator Translator Rule Code Optimizer P-in-S program S-to-T translator machine Code Generator P-in-T program Target Program Metaprogramming 13 Metaprogramming 14 ImplementaAon DerivaAon Example ImplementaAon DerivaAon Example SoluAon Prove how to implement a "251 web page machine" using: • 251-web-page-in-HTML program (a web page wriDen in HTML) • HTML-interpreter-in-C program (a web browser wriDen in C) • C-to-x86-translator-in-x86 program (a C compiler wriDen in x86) • x86 interpreter machine (an x86 computer) We can omit some occurrences of “program” and “machine”: No peaking ahead! Metaprogramming 15 Metaprogramming 16 ImplementaAon DerivaAon Are Trees DerivaAon Exercise And so we can represent them as nested structures, like nested bulleted lists: How to execute the Racket factorial o factorial-in-Racket program program given these parts? o Racket-to-Python-translator-in-Python program q 251-web-page-in-HTML program o Python-interpreter-in-C program Version that shows o HTML-interpreter-in-C program o C-to-x86-translator-in-x86 program conclusions below bullets. Warning: cannot start • C-to-x86 compiler-in-x86 program o x86 computer (i.e., x86 interpreter machine) • X86 computer More similar to derivaLons the following way: with horizontal lines, but o C-to-x86 compiler machine (I) harder to create and read factorial machine (I) ² HTML-interpreter-in-x86 program (T) q factorial-in-Racket program ² x86 computer q Racket interpreter machine (I) q HTML interpreter machine (I) …. 251 web page machine (I) Why not? 251 web page machine (I) Preferred “top-down” q 251-web-page-in-HTML program version that shows q HTML interpreter machine (I) conclusions above bullets. ² HTML-interpreter-in-x86 program (T) o HTML-interpreter-in-C program o C-to-x86 compiler machine (I) • C-to-x86 compiler-in-x86 program • X86 computer ² x86 computer Metaprogramming 17 Metaprogramming 18 DerivaAon Exercise: SoluAon Metaprogramming: Bootstrapping Puzzles How to execute the Racket factorial o factorial-in-Racket program program given these parts? o Racket-to-Python-translator-in-Python program o Python-interpreter-in-C program How can a Racket interpreter be wriDen in Racket? o C-to-x86-translator-in-x86 program Put your soluAon here: o x86 computer (i.e., x86 interpreter machine) How can a Java compiler be wriDen in Java? How can gcc (a C-to-x86 compiler) be wriDen in C? Metaprogramming 19 Metaprogramming 20 Metacircularity and Bootstrapping Metacircularity Example 1: Problem Many examples: Suppose you are given: • Lisp in Lisp / Scheme in Scheme/Racket in Racket • Racket-interpreter-in-Python program • Python in Python: PyPy • Python machine • Java in Java: Jikes RVM, Maxine VM • Racket-interpreter-in-Racket program • … How do you create a Racket interpreter machine using the • C-to-x86 compiler in C: gcc Racket-interpreter-in-Racket program? • eval construct in languages like Lisp, JavaScript How can this be possible? Key insights to bootstrapping: • The first implementaLon of a language cannot be in itself, but must be in some other language. • Once you have one implementaLon of a language L, you can can implement (enhanced versions of) L in L. Metaprogramming 21 Metaprogramming 22 Metacircularity Example 1: SoluAon Metacircularity Example 1: More RealisAc Suppose you are given: Suppose you are given: • Racket-interpreter-in-Python program • Racket-subset-interpreter-in-Python program (implements • Python machine only core Racket features; no desugaring or other frills) • Racket-interpreter-in-Racket program • Python machine How do you create a Racket interpreter machine using the • Full-Racket-interpreter-in-Racket-subset program Racket-interpreter-in-Racket program? How do you create a Full-Racket interpreter machine using the Full-Racket-interpreter-in-Racket-subset program? Racket interpreter machine #2 (I) q Racket-interpreter-in-Racket program Full-Scheme interpreter machine (I) q Racket-interpreter machine #1 (I) q Full-Racket-interpreter-in-Racket-subset program ² Racket-interpreter-in-Python program q Racket-subset interpreter machine #1 (I) ² Python machine ² Racket-subset-interpreter-in-Python program ² Python machine But why create Racket interpreter machine #2 when you already have Racket-interpreter machine #1? Metaprogramming 23 Metaprogramming 24 Metacircularity Example 2: Problem Metacircularity Example 2: SoluAon Suppose you are given: Suppose you are given: • C-to-x86-translator-in-x86 program (a C compiler wriDen in x86) • C-to-x86-translator-in-x86 program (a C compiler wriDen in x86) • x86 interpreter machine (an x86 computer) • x86 interpreter machine (an x86 computer) • C-to-x86-translator-in-C program • C-to-x86-translator-in-C program How do you compile the C-to-x86-translator-in-C ? How do you compile the C-to-x86-translator-in-C ? C-to-x86-translator machine #2 (I) q C-to-x86-translator-in-x86 program #2 (T) ² C-to-x86-translator-in-C ² C-to-x86-translator machine #1 (I) o C-to-x86-translator-in-x86 program #1 o x86 computer q x86 computer But why create C-to-x86-translator-in-x86 program #2 (T) when you already have C-to-x86-translator-in-x86 program #1? Metaprogramming 25 Metaprogramming 26 A long line of C compilers Metacircularity Example 2: More RealisAc C-version_n-to-target_n-translator machine (I) Suppose you are given: q C-version_n-to-target_n-translator program in target_n-1 (T) ² C-version_n-to-target_n-translator program in C-version_n-1 • C-subset-to-x86-translator-in-x86 program ² C-version_n-1-to-target_n-1 translator machine (I) (a compiler for a subset of C wriDen in x86) o C-version_n-1-to-target_n_1-translator program in target_n-2 (T) • x86 interpreter machine (an x86 computer) … • Full-C-to-x86-translator-in-C-subset program Ø C-version_2-to-target_2-translator-program in target_1 (T) (a compiler for the full C language wriDen in a subset of C) § C-version_2-to-target_2-translator program in C-version_1 How do you create a Full-C-to-x86-translator machine ? § C-version_1-to-target_1 translator machine (I) • C-version_1-to-target_1-translator program in assembly_0 Full-C-to-x86-translator machine (I) • assembly_0 computer q Full-C-to-x86-translator-in-x86 program (T) Ø target_1 computer … ² Full-C-to-x86-translator-in-C-subset ² C-subset-to-x86-translator machine (I) o target_n-2 computer q target_n-1 computer o C-subset-to-x86-translator-in-x86 program o x86 computer o The versions of C and target languages can change at each stage. q x86 computer o Trojan horses from earlier source files can remain in translator machines even if they’re not in later source file! See Ken Thompson’s Reflec4on on Trus4ng Trust Metaprogramming 27 Metaprogramming 28 More Metaprogramming in SML Remember: language != implementaAon o We’ve already seen PostFix and s-expressions in • Easy to confuse "the way this language is usually Racket; next

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us