A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer Science April 3, 2002 Center for Distributed Object Computing Department of Computer Science Washington University By Norman Ramsey, & Simon Peyton Jones
April 3, Overview Motivation - Goal of C-- What does this paper attempt to explain? Survey of well know exception mechanisms So what’s the problem? How does C– attempt to solve the problem? Overview of C-- / Example Implementing high-level Exception is C-- / Example Related work Conclusions
April 3, The Goal of the authors To present a compiler target - language C-- –4 of the best known techniques for implementing exceptions within a single framework –Exceptions do not require special treatment in optimizer –Allows a single optimizer to handle a variety of implementation techniques Overall Goal –Allow a source language the freedom to choose its exception handling policy –For C-- to be independent of source programming languages and target architecture
April 3, What C-- is Not Universal Intermediate Language –Does not works for all Write once run anywhere Language –Not like Java One Implementation Different platforms
April 3, What C-- really is Encapsulates compilation techniques –These are well understood but difficult to implement –These techniques include: Instruction selection Register allocation Instruction Scheduling Scalar optimizations of imperative code with loops What does C-- do beyond this? –C-- encapsulates architecture-specific run-time support for high-level run-time services Garbage collection Concurrency
April 3, What C-- really is (continued) What does C-- do beyond this? –C-- encapsulates architecture-specific run-time support for high-level run-time services Debugging Exception Dispatch
April 3, What does this paper explain? How C-- encapsulates techniques compilers use to support exception dispatch Presents 2 mechanisms C-- uses to specify interprocedural control flow –Weak continuations that do not outlive their procedure activation –Call site annotations C-- also supports continuation-passing style – a 4 th implementation technique Abstract C-- is used to to define these mechanisms precisely
April 3, Survey of Exception Mechanisms Stack cutting –Set stack pointer and PC to point directly to handler –In native compiler, fast but does not restore callee- saves registers –This techniques is used in Common Lisp and pre- Scheme Lisp Run-time stack unwinding –Use the run-time system to unwind the stack frames until the handle is reached –Run-time system restores callee-saves registers –Operating systems like Digital Unix provide support for run-time stack unwinding –The JVM and C++ compilers use this technique
April 3, Survey of Exception Mechanisms (cont.) Native-code stack unwinding –Use specialized code in each procedure to unwind stack When nonlocal return Exceptional termination is called for –Self compiler uses this technique Continuation passing style –Exception continuation represent Potential exception handlers –Generated code raises exception by making tail call to continuation –Continuation decides which handler applies –Standard ML of NJ uses this technique
April 3, So what’s the problem? Correct exception dispatch depends on factors –Semantics of exception in source language –Representation of call stack on target machine Compiler optimization is fundamentally affected by exceptions So what is the easy way out? –Code-generator know language-specific details of exception semantics –Run-time system know code-generator’s stack layout –Run-time system know code-generator’s register- saving protocols
April 3, So what’s the role of C--? To show how a reusable code-generator does the following: –Cooperate with front ends at arm’s length –Still support a variety of exception semantics in architecture-independent way Can a language alone provide sufficiently flexible interface? –No –C– contains a language which cooperates with source language compiler –Run-time system which cooperates with the same for source language
April 3, Overview of the C-- language C-- has parameterized procedures with declared local variables –Body of procedure consists of sequence of statement, which include Assignments Conditionals Gotos Calls Jumps (tail calls) C-- maps source language local and global variables to machine registers, not memory locations
April 3, Example Code
April 3, The C-- Language 2 Features common to assemblers but not programming languages –Procedures return multiple values –Procedures explicitly tail call other procedures Type system is extremely modest –Only types are words and floating point values of various sizes e.g. Bits8, bits16, bits32, bits64 Float32, float64 –For each target architecture, each implementation of C-- designates one of the bitsn types as The native data pointer type And 1 as the native code pointer type
April 3, C-- run time system Assumption: executable program is built by linking 3 parts, which may be found in Object files, or Libraries –Front end translates high level source programs into C– modules –C– compiler translates modules into generated object code –Front end comes with front end run-time system –Run-time system implements policy and mechanism of source Include garbage collection Exception dispatch Thread scheduler, etc. C-- implementation comes with a C-- run-time system –Encapsulates architecture-specific mechanisms –Provides services to front end run-time system through C run time interface Main service provided by C-- run-time interface is to represent the state of a C-- thread as a stack of abstract activations
April 3, C-- run time system The table above give lists the operations to: –Walk down the stack –Get stack info from an activation –Make a particular activation become the topmost one –Change resumption point of topmost activation
April 3, Implementing High-Level Exceptions in C-- Things to note: –Exceptions change flow of control –Exception Handler – destination of exception control transfer –Exception dispatch – transferring of control to exception handler –C-- models exception handlers as continuations Label with parameters Declarable only inside a procedure Denotes a value which can be used to transfer control to it – encaps. a stack pointer and a PC –C-- uses annotations on call sites to tell optimizer what exception control transfers can take place
April 3, Example
April 3, Transferring control to a continuation
April 3, Related Work
April 3, Discussion