Presentation on theme: "From OODL Interpreters to VM’s and Beyond Jonathan Bachrach MIT AI Lab."— Presentation transcript:
From OODL Interpreters to VM’s and Beyond Jonathan Bachrach MIT AI Lab
Outline Material mostly from “Lisp In Small Pieces” Metacircular Interpreters CPS Interpreters Fast Interpreters VM’s and Compilation Speeding up VM’s VVM’s In Language VM’s Assignment #2
Lisp In Small Pieces Written by Christian Queinnec Highly recommend looking at this book Basis for a fair amount of today’s lecture
Q: Why Interpreters?
A: Why Interpreters? Semantics of language –is manifest –is measurable Allows more –Dynamism –Extensibility
Metacircular Interpreters Write interpreter in same language using own special forms Complexity of resulting interpreter is a measure of expressivity and simplicity of language Dream is to get a compiler for free from this!
Proto in Proto (dm evol ((e ) (env )) e) (dm evol ((e ‘()) (env )) e) (dm evol ((e ) (env )) (env-binding-value env e)) (dm evol ((e ) (env )) (evol-list (head e) e env)) (dm evol-list ((_ 'quote) (e ) (env )) ( sexpr-text-of-quotation e))
Q: What’s the Problem? Why might a metacircular interpreter not be acceptable?
A: What’s the Problem? Creates bootstrapping issues Very inefficient
Tail Position / Calls If an expression is in tail position then it is not necessary to restore the environment –(fun (x) (f x)) –(fun (x) (if (f x) 5 6)) Tail calls are calls that are in tail position and don’t save/restore environment –Bind arguments –Goto function
Continuation Passing Style (CPS) CPS = Continuation Passing Style –Explicitly telling a computation where to send the result of that computation –Once computation is complete, executor applies receiver to result rather than returning it as a normal value Example –(fun (x) (f x)) => (fun (x k) (f’ x k))
CPS Interpreter Control is more explicit by passing continuations through interpreter Allows one to code complicated control forms without built-in support. –E.g., call/cc
CPS Loses Problem is that some special forms like fin (cf. Lisp’s unwind-protect ) are difficult* to implement without resorting to built-in support –*It appears that they are possible with enough continuations Conses (allocates) lots of closures for continuations during interpretation
Interpreter Pretreatment Lighten up environment Invent beginnings of instruction set –Implemented in terms of thunks (zero parameter functions) Threaded as tree of calls to thunks Reject environment (rep’d as global *env*) Keep track of tail position
Fast Environment Globals stored in vector Locals are in list of inlined frames Model environment structure in static environment Resolve environment accesses to fixed or relative offsets –Globals are fixed offset –Locals are two relative offsets N.B., Can do even better by flattening ribcage environment into a stack
Pretreatment Pretreatment of programs is handled by the function meaning (compile) Converts into thunks that will serve as threading mechanism Each thunk can be thought of as an address to which we simply jump to execute
Inventing Instructions Use combinators which are like instructions for an abstract machine: (dm CONSTANT (value) (fun () value) ) (dm meaning-list ((_ 'quote) (e ) r t?) (fun () (CONSTANT (sexpr-text-of-quotation e)) ))
VM Dispatch Loop (dm run () (let ((instruction (head *pc*))) (set *pc* (tail *pc*)) (instruction) (run))) Already removed decode overhead by translating incoming bytecodes to direct calls Still extra overhead –Return from instruction –Loop
Reducing Dispatch Overhead Convert back to threaded code Each instruction is responsible for calling next one: (dm CONSTANT (value) (fun () (set *val* value) (let ((instruction (head *pc*))) (set *pc* (tail *pc*)) (instruction)) Example Push3 on PowerPC takes –2 instrs to do real push3 –11 total instrs for pc loop version –5 total instrs for threaded version Can do same with fast interpreter –Use CPS threading –Consing not an issue if closures created during pretreatment
Hand Selected Macro Instructions Still have overhead especially for simple instructions Can do better by combining oft-occurring sequences of instructions –amortizing the decode/dispatch overhead –customizing resulting instruction to remove any other interpretive overhead –shrinks byte-code size
Automatic Macro Instruction Detection Can run statistics off-line to determine best macros Can also run statistics on-line
VVM’s Tower of VM’s Record expansions Concatenate Peephole optimize at each level –Specialization of instructions Now that actual arguments are available –Between instruction optimizations
Boxing/Unboxing Objects all the way down Integers are objects and must be self-identifying One way to encode them is to wrap integer data in a “box” which also points to integer prototype Integer ops must then first unbox before running machine level integer op and finally box result We’ll be talking about alternative representations later
R/VVM’s Provides extensible VM infrastructure Permits the running of many different instruction sets customized to different languages Examples –Greg Sullivan’s DVM –Ian Piumarta’s VVM
Bytecodes can be Faster than Direct Execution When –There is a high price for cache misses –VM can fit inside cache –The interpretive overhead of the bytecodes is small (i.e., they are high-level) But –There will be cases where interpretive overhead is high Suggests hybrid approach –Demand driven translation –Analysis determines interpretive overhead
In C VM’s + Portable - Hard to interoperate with native code - Needs own versions of special forms - Needs its own FFI to C
In Language VM’s + Write VM in Same Language - Challenging to get high performance type system can be overly restrictive - Tough Bootstrap
IDVM In Dylan Virtual Machine was developed at Harlequin by Tony Mann and Eliot Miranda Consider IPVM
The Trick Considerable effort was expending optimizing the following idiom: (dm foo (a b c (thingies …)) … (apply somefunction a b c thingies)) Converted into a tail call with thingies being stack allocated Each IPVM instruction tail calls the next with code, pc, and locals as arguments.
Open Problems Compiler for Free with VVM’s Hybrid VM’s Type system’s powerful enough to do in language VM’s Analyzable VM
Readings Queinnec: Lisp In Small Pieces Deutsch, Schiffman: Efficient Implementation of the Smalltalk-80 System Piumarta: Optimizing threaded code by selected inlining Doyle, Moss: When are bytecodes faster than direct execution? Withington, McKay, Palter, Robertson: The symbolics virtual lisp machine or using the DEC alpha as a programmable micro-engine Mann, Miranda: A Dylan virtual machine in Dylan or son of Dylan Compiler in Dylan Folliot, Piumarta, Riccardi: A dynamically configurable multi-language execution platform Miranda: BrouHaHa - a portable smalltalk interpreter Ford et al: microkernels meet recursive virtual machines Chambers, Unger: Making pure object-oriented languages practical Ingalls: The Evolution of the Smalltalk Virtual Machine
Assignment #2 Write a meta circular interpreter for proto Implement all the special forms Assume (i.e., use) –the object system –the reader and parsing functions –the special forms themselves