lisp racket and implementation 2
play

Lisp/Racket and Implementation (2) Translation (a.k.a. - PowerPoint PPT Presentation

9/25/15 How to implement a programming language Interpretation An interpreter written in the implementation language reads a program written in the source language


  1. 9/25/15 How ¡to ¡implement ¡ a ¡programming ¡ language Interpretation An ¡ interpreter written ¡ in ¡the ¡ implementation ¡ language ¡ reads ¡ a ¡program ¡written ¡ in ¡the ¡ source ¡language ¡ and ¡ evaluates it. Lisp/Racket ¡and ¡Implementation ¡(2) Translation ¡(a.k.a. ¡compilation) An ¡ translator ¡ ( a.k.a. compiler) ¡ written ¡ in ¡the ¡ implementation ¡ language reads ¡a ¡program ¡ written ¡in ¡the ¡ source ¡language and ¡ Interpretation, ¡Translation, ¡and ¡everything ¡in ¡between translates it ¡to ¡an ¡equivalent ¡ program ¡ in ¡the ¡ target ¡ language. Programs ¡as ¡Data If ¡time: ¡Implementing ¡Racket ¡in ¡Racket -­‑ hands-­‑on But ¡now ¡we ¡need ¡implementations ¡of: -­‑ how ¡Lisp ¡was ¡first ¡implemented implementation ¡language target ¡language Metacircularity How ¡to ¡implement ¡ a ¡programming ¡ language Interpreter ¡Rule Lisp ¡in ¡Lisp ¡/ ¡Racket ¡ in ¡Racket: ¡ eval • Python ¡in ¡Python: ¡PyPy P-­‑in-­‑L ¡program L ¡interpreter ¡machine • Java ¡in ¡Java: ¡ Jikes RVM, ¡Maxine ¡VM • P ¡machine … • Translator ¡Rule C-­‑to-­‑x86 ¡compiler ¡in ¡C • P-­‑in-­‑S ¡program S-­‑to-­‑T ¡translator ¡machine • Let's ¡try ¡to ¡draw ¡out ¡that ¡proof ¡of ¡existence... P-­‑in-­‑T ¡program Prove ¡how ¡to ¡implement ¡a ¡"251 ¡web ¡page ¡machine" ¡using: • 251-­‑web-­‑page-­‑in-­‑HTML ¡program ¡(a ¡web ¡page ¡written ¡in ¡HTML) • HTML-­‑interpreter-­‑in-­‑C ¡program ¡(a ¡web ¡browser ¡written ¡in ¡C) • C-­‑to-­‑x86-­‑translator-­‑in-­‑x86 ¡program ¡(a ¡C ¡compiler ¡written ¡in ¡x86) • x86 ¡interpreter ¡machine ¡(an ¡x86 ¡computer) 1

  2. 9/25/15 Compiler Typical ¡Compiler C ¡Source ¡ x86 ¡Target ¡ Program C ¡Compiler Program Source ¡ Lexical ¡Analyzer Program if (x == 0) { cmp (1000), $0 Syntax ¡Analyzer x = x + 1; bne L Analysis Semantic ¡Analyzer } add (1000), $1 ... L: Intermediate ¡Code ¡ Synthesis ... Generator Code ¡Optimizer x86 ¡Targe t ¡ Program Code ¡Generator Output x86 ¡computer Target ¡ Data Program 5 6 Interpreters Interpreters ¡vs Compilers Interpreters No ¡work ¡ahead ¡of ¡time Source ¡ Incremental Program maybe ¡inefficient Compilers Interpreter ¡= All ¡work ¡ahead ¡of ¡time See ¡whole ¡program ¡(or ¡more ¡of ¡program) virtual ¡machine Time ¡and ¡resources ¡for ¡analysis ¡and ¡optimization Output Data 7 2

  3. 9/25/15 Compilers... ¡whose ¡output ¡is ¡interpreted Java ¡Compiler Source ¡ Source ¡ Target ¡ Target ¡ Program Java ¡Compiler Program Java ¡Compiler Program Program if (x == 0) { load 0 x = x + 1; ifne L Target ¡ } load 0 Program Java ... inc Output Virtual store 0 Machine L: Data ... (compare compiled C to compiled Java) Doesn’t ¡this ¡look ¡familiar? 9 10 Interpreters... ¡that ¡use ¡compilers. JIT ¡Compilers ¡and ¡Optimization Source ¡ Source ¡ Program Program Compiler • HotSpot JVM Just ¡In ¡Time ¡ • Jikes RVM Compiler • SpiderMonkey Compiler • v8 Target ¡ • T ransmeta Program Virtual • ... Output Target ¡ Performance Machine Monitor Program Output Data Virtual Machine Data 11 12 3

  4. 9/25/15 Virtual ¡Machine ¡Model Remember: ¡language ¡!= ¡implementation Easy ¡to ¡confuse ¡"the ¡way ¡this ¡language ¡ is ¡usually ¡ implemented" ¡ or ¡"the ¡implementation ¡ I ¡use" ¡with ¡"the ¡language ¡ itself." High-­‑Level ¡ Language ¡ Program ¡ Bytecode Ahead-­‑of-­‑time compiler compiler Java ¡ and ¡Racket ¡can ¡be ¡compiled ¡to ¡x86 compile ¡time Virtual ¡Machine ¡ Language run ¡time C ¡can ¡be ¡interpreted ¡in ¡Racket JIT Virtual ¡machine compiler (interpreter) x86 ¡can ¡be ¡compiled ¡to ¡JavaScript Native ¡ Machine ¡Language 13 Programs ¡as ¡Data Racket ¡programs ¡ look ¡a ¡lot ¡like… Symbols: ¡ 'a • Number ¡and ¡boolean symbols ¡identical ¡to ¡values: ¡' #f is ¡ #f Atoms: symbols, ¡numbers, ¡booleans, ¡null General ¡quoting: • 1 2 3) produces ¡a ¡value ¡DrRacket draws ¡as ¡ '(1 (list 2 3) • 1 2) is ¡the ¡same ¡as ¡ (list '(cons 'cons '1 '2) • '(lambda (x) (+ x x)) is ¡the ¡same ¡as (list 'lambda (list 'x) (list '+ 'x 'x)) 4

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