How to implement a programming language Metaprogramming InterpretaEon An interpreter wriDen in the implementaEon language reads a program wriDen in the source language and evaluates it. These slides borrow heavily from Ben Wood’s Fall ‘15 slides. TranslaEon (a.k.a. compilaEon) SOLUTIONS An translator ( a.k.a. compiler) wriDen in the implementaEon language reads a program wriDen in the source language and translates it to an equivalent program in the target language. CS251 Programming Languages Fall 2018, Lyn Turbak But now we need implementaEons of: Department of Computer Science implementaEon language Wellesley College target language Metaprogramming 2 Metaprogramming: InterpretaEon 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
Metaprogramming: TranslaEon 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
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 OpEmizaEon 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 Eme • Transmeta Virtual Machine Language run Eme • ... Target Performance JIT Monitor Program Virtual machine compiler Output (interpreter) Virtual Machine NaLve Machine Language Data Metaprogramming 11 Metaprogramming 12
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 ImplementaEon DerivaEon Example ImplementaEon DerivaEon Example SoluEon 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
ImplementaEon DerivaEon Are Trees DerivaEon 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? But the parts don’t include The derivaEon would need to begin: Preferred “top-down” 251 web page machine (I) Racket-interpreter-in-L program version that shows q 251-web-page-in-HTML program factorial machine (I) for any L! q factorial-in-Racket program q HTML interpreter machine (I) conclusions above bullets. q Racket interpreter machine (I) ² HTML-interpreter-in-x86 program (T) What to do? Explore translaEng o Racket-interpreter-in-L program o HTML-interpreter-in-C program … the factorial-in-Racket program o C-to-x86 compiler machine (I) L interpreter machine o to a factorial-in-L program • C-to-x86 compiler-in-x86 program … for some L for which we *can* • X86 computer make an interpreter machine! ² x86 computer Metaprogramming 17 Metaprogramming 18 DerivaEon Exercise: SoluEon 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) SOLUTION: factorial machine (I) How can gcc (a C-to-x86 compiler) be wriDen in C? q factorial-in-Python program (T) ² factorial-in-Racket program ² Racket-to-Python translaLon machine (I) Ø Racket-to-Python-translator-in-Python program Ø Python interpreter machine (I) u Python-interpreter-in-x86 program (T) o Python-interpreter-in-C program o C-to-x86-translator machine (I) • C-to-x86-translator-in-x86 program • x86 computer (= x86 interpreter machine u x86 computer (= x86 interpreter machine) q Python interpreter machine (I) # Deriva)on already given above; no need to rederive it! # A reused deriva)on is a lemma, which corresponds to # a helper func)on in programming 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: SoluEon Metacircularity Example 1: More RealisEc 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-Racket 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
Recommend
More recommend