Lisp/Racket,and,ImplementaCon,(2), Metaprogramming, These!slides!borrow!heavily!from!Ben!Wood’s!Fall!‘15!slides.! InterpretaDon,!TranslaDon,!and!everything!in!between! CS251,Programming,Languages, Programs!as!Data! If!Dme:!ImplemenDng!Racket!in!Racket! Spring,2016,,Lyn,Turbak, !!!!J!handsJon! ! !!!!J!how!Lisp!was!first!implemented! Department!of!Computer!Science! ! Wellesley!College! How,to,implement,a,programming,language, How!to!implement!a!programming!language! InterpretaCon, Can!describe!by!deriving!a!“proof”!of!the!implementaDon!! using!these!inference!rules:! An! interpreter !wriNen!in!the! implementaCon,language, reads! a!program!wriNen!in!the! source,language, and! evaluates !it.! Interpreter,Rule , PMinML,program ,L,interpreter,machine, TranslaCon,(a.k.a.,compilaCon), P,machine, An! translator,( a.k.a. ,compiler), wriNen!in!the! implementaCon, Translator,Rule, language !reads!a!program!wriNen!in!the! source,language !and! PMinMS,program ,SMtoMT,translator,machine, translates !it!to!an!equivalent!program!in!the! target,language., , PMinMT,program, But,now,we,need,implementaCons,of:, , ,implementaCon,language, ,target,language, 3 4
ImplementaCon,DerivaCon,Example, ImplementaCon,DerivaCon,Example,SoluCon, , Prove,how,to,implement,a,"251,web,page,machine",using:, • 251JwebJpageJinJHTML!program!(a!web!page!wriNen!in!HTML)! • HTMLJinterpreterJinJC!program!(a!web!browser!wriNen!in!C)! • CJtoJx86JtranslatorJinJx86!program!(a!C!compiler!wriNen!in!x86)! We!can!omit!“program”!and!“machine”:!! • x86!interpreter!machine!(an!x86!computer)! ! No,peaking,ahead!, 5 6 Metacircularity,and,Bootstrapping, ImplementaCon,DerivaCon,Are,Trees, Many!examples:! And!so!we!can!represent!them!as!nested!structures,!like!nested!bulleted!lists:!! Lisp!in!Lisp!/!Racket!in!Racket:!eval! • Python!in!Python:!PyPy! • Java!in!Java:!Jikes!RVM,!Maxine!VM! • …! • CJtoJx86!compiler!in!C! • How!can!this!be!possible?!! ! Key$insights$to$bootstrapping:$$ The!first!implementaDon!of!a!language! cannot !be!in! • itself,!but!must!be!in!some!other!language.! Once!you!have!one!implementaDon!of!a!language,!you! • can!implement!it!in!itself.!! 7 8
Metacircularity,Example,1, Metacircularity,Example,2, Suppose!you!are!given:!! Suppose!you!are!given:!! • RacketJinJSML!interpreter! CJtoJx86JtranslatorJinJx86!program!(a!C!compiler!wriNen!in!x86)! • • x86!interpreter!machine!(an!x86!computer)! SML!machine! • • CJtoJx86JtranslatorJinJC!! RacketJinJRacket!interpreter! • How!do!you!compile!the!CJtoJx86JtranslatorJinJC!?! How!do!you!run!the!RacketJinJRacket!interpreter?! ! 9 10 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 Target Program Code Generator Output x86 computer Target Data Program 11 12
Interpreters, Interpreters,vs,Compilers, Interpreters, No!work!ahead!of!Dme! Source Incremental! Program maybe!inefficient! ! Compilers, Interpreter = All!work!ahead!of!Dme! See!whole!program!(or!more!of!program)! virtual machine Time!and!resources!for!analysis!and!opDmizaDon! Output ! Data 13 14 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?! 15 16
Interpreters...,that,use,compilers., JIT,Compilers,and,OpCmizaCon, Source Source Program Program Compiler • HotSpot!JVM! Just In Time • Jikes!RVM! Compiler • SpiderMonkey! Compiler • v8! Target • Transmeta! Program Virtual • ...! Target Performance! Output Machine Monitor! Program Output Data Virtual Machine Data 17 18 Virtual,Machine,Model, Remember:,language,!=,implementaCon, Easy!to!confuse!"the!way!this!language!is!usually! • implemented"!or!"the!implementaDon!I!use"!with!"the! language!itself.”! HighJLevel!Language!Program!! ! Bytecode! AheadJofJDme! Java!and!Racket!can!be!compiled!to!x86! • compiler! compiler! compile,Cme, C!can!be!interpreted!in!Racket! • Virtual!Machine!Language! run,Cme, JIT! Virtual!machine! x86!can!be!compiled!to!JavaScript! • compiler! (interpreter)! Can!we!compile!C/C++!to!Javascript?! • NaDve!Machine!Language! hNp://kripken.github.io/emscriptenJsite/! 20 19!
Next,Topic:,Metaprogramming,in,SML, • PostFix!in!SML!(see! postfix.sml )! • A!sequences!of!expression!languages!implemented!in! SML!that!look!closer!and!closer!to!Racket:! • Intex! • Bindex! • Valex! • HOFL!(higherJorder!funcDonal!langauge)! 21
Recommend
More recommend