metaprogramming interpretaaon
play

Metaprogramming: InterpretaAon Interpreters Source Program - PowerPoint PPT Presentation

How to implement a programming language InterpretaAon Metaprogramming An interpreter wriDen in the implementaAon language reads a program wriDen in the source language and evaluates it. These slides borrow heavily from Ben Woods Fall 15


  1. How to implement a programming language InterpretaAon Metaprogramming 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 translates it to an equivalent program in the target language. CS251 Programming Languages Spring 2018, Lyn Turbak But now we need implementaAons of: Department of Computer Science implementaAon language Wellesley College target language Metaprogramming 2 Metaprogramming: InterpretaAon Interpreters Source Program Interpreter = Program in Interpreter virtual machine Machine M language L for language L Output on machine M Data Metaprogramming Metaprogramming 3 4

  2. Metaprogramming: TranslaAon Compiler C Source x86 Target Program C Compiler Program if (x == 0) { cmp (1000), $0 Program in x = x + 1; bne L Program in language A A to B translator } add (1000), $1 language B ... L: ... x86 Target Program Output x86 computer Interpreter Machine M Thanks to Ben Wood for these for language B Data and following pictures on machine M Metaprogramming Metaprogramming 5 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 L: Time and resources for analysis and opLmizaLon ... (compare compiled C to compiled Java) Metaprogramming Metaprogramming 7 8

  3. Compilers... whose output is interpreted Interpreters... that use compilers. Source Source Target Program Java Compiler Program Compiler Program Target Program Target Virtual Output Program Java Machine Output Virtual Machine Data Data Doesn’t this look familiar? Metaprogramming Metaprogramming 10 9 JIT Compilers and OpAmizaAon Virtual Machine Model Source Program High-Level Language Program • HotSpot JVM Just In Time • Jikes RVM Bytecode Ahead-of-Lme Compiler • SpiderMonkey compiler compiler Compiler • v8 compile Ame • Transmeta Virtual Machine Language run Ame • ... Target Performance JIT Monitor Program Virtual machine compiler Output (interpreter) Virtual Machine NaLve Machine Language Data Metaprogramming 11 Metaprogramming 12

  4. Typical Compiler How to implement a programming language Can describe by deriving a “proof” of the implementaLon Source Lexical Analyzer using these inference rules: Program Interpreter Rule Syntax Analyzer P-in-L program L interpreter machine Analysis Semantic Analyzer P machine Intermediate Code Synthesis Translator Rule Generator P-in-S program S-to-T translator machine Code Optimizer P-in-T program Code Generator 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) We can omit some occurrences of “program” and “machine”: • x86 interpreter machine (an x86 computer) No peaking ahead! Metaprogramming 15 Metaprogramming 16

  5. 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) More similar to derivaLons the following way: • X86 computer with horizontal lines, but o C-to-x86 compiler machine (I) factorial machine (I) harder to create and read ² 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? Preferred “top-down” 251 web page machine (I) version that shows q 251-web-page-in-HTML program 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 How can a Racket interpreter be wriDen in Racket? program given these parts? o Racket-to-Python-translator-in-Python program o Python-interpreter-in-C program o C-to-x86-translator-in-x86 program How can a Java compiler be wriDen in Java? o x86 computer (i.e., x86 interpreter machine) Put your soluAon here: How can gcc (a C-to-x86 compiler) be wriDen in C? Metaprogramming 19 Metaprogramming 20

  6. 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 • • only core Racket features; no desugaring or other frills) Python machine • Python machine Racket-interpreter-in-Racket program • • Full-Racket-interpreter-in-Racket-subset program How do you create a Racket interpreter machine using the • 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

  7. 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) q C-version_n-to-target_n-translator program in target_n-1 (T) Suppose you are given: ² 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 … o target_n-2 computer ² C-subset-to-x86-translator machine (I) q target_n-1 computer o C-subset-to-x86-translator-in-x86 program o The versions of C and target languages can change at each stage. o x86 computer o Trojan horses from earlier source files can remain in translator machines even if q x86 computer they’re not in later source file! See Ken Thompson’s Reflec4on on Trus4ng Trust Metaprogramming 27 Metaprogramming 28

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