1 Meeting notes: Implementation idea: Exception Handling in C++/Java
3 The 5/18/01 meeting discussed ideas for implementing exceptions in LLVM.
4 We decided that the best solution requires a set of library calls provided by
5 the VM, as well as an extension to the LLVM function invocation syntax.
7 The LLVM function invocation instruction previously looks like this (ignoring
10 call func(arg1, arg2, arg3)
12 The extension discussed today adds an optional "with" clause that
13 associates a label with the call site. The new syntax looks like this:
15 call func(arg1, arg2, arg3) with funcCleanup
17 This funcHandler always stays tightly associated with the call site (being
18 encoded directly into the call opcode itself), and should be used whenever
19 there is cleanup work that needs to be done for the current function if
20 an exception is thrown by func (or if we are in a try block).
22 To support this, the VM/Runtime provide the following simple library
23 functions (all syntax in this document is very abstract):
25 typedef struct { something } %frame;
26 The VM must export a "frame type", that is an opaque structure used to
27 implement different types of stack walking that may be used by various
28 language runtime libraries. We imagine that it would be typical to
29 represent a frame with a PC and frame pointer pair, although that is not
32 %frame getStackCurrentFrame();
33 Get a frame object for the current function. Note that if the current
34 function was inlined into its caller, the "current" frame will belong to
37 bool isFirstFrame(%frame f);
38 Returns true if the specified frame is the top level (first activated) frame
39 for this thread. For the main thread, this corresponds to the main()
40 function, for a spawned thread, it corresponds to the thread function.
42 %frame getNextFrame(%frame f);
43 Return the previous frame on the stack. This function is undefined if f
44 satisfies the predicate isFirstFrame(f).
46 Label *getFrameLabel(%frame f);
47 If a label was associated with f (as discussed below), this function returns
48 it. Otherwise, it returns a null pointer.
50 doNonLocalBranch(Label *L);
51 At this point, it is not clear whether this should be a function or
52 intrinsic. It should probably be an intrinsic in LLVM, but we'll deal with
56 Here is a motivating example that illustrates how these facilities could be
57 used to implement the C++ exception model:
59 void TestFunction(...) {
61 foo(); // Any function call may throw
70 // execution continues after the try block: the exception is consumed
73 throw; // Exception is propogated
77 This function would compile to approximately the following code (heavy
82 A::A(%a) // These ctors & dtors could throw, but we ignore this
83 %b = alloca B // minor detail for this example
86 call foo() with fooCleanup // An exception in foo is propogated to fooCleanup
87 call bar() with barCleanup // An exception in bar is propogated to barCleanup
93 call baz() with bazCleanup // An exception in baz is propogated to bazCleanup
95 EndTry: // This label corresponds to the end of the try block
96 c->~C() // These could also throw, these are also ignored
101 Note that this is a very straight forward and literal translation: exactly
102 what we want for zero cost (when unused) exception handling. Especially on
103 platforms with many registers (ie, the IA64) setjmp/longjmp style exception
104 handling is *very* impractical. Also, the "with" clauses describe the
105 control flow paths explicitly so that analysis is not adversly effected.
107 The foo/barCleanup labels are implemented as:
109 TryCleanup: // Executed if an exception escapes the try block
111 barCleanup: // Executed if an exception escapes from bar()
113 fooCleanup: // Executed if an exception escapes from foo()
116 Exception *E = getThreadLocalException()
117 call throw(E) // Implemented by the C++ runtime, described below
119 Which does the work one would expect. getThreadLocalException is a function
120 implemented by the C++ support library. It returns the current exception
121 object for the current thread. Note that we do not attempt to recycle the
122 shutdown code from before, because performance of the mainline code is
123 critically important. Also, obviously fooCleanup and barCleanup may be
124 merged and one of them eliminated. This just shows how the code generator
125 would most likely emit code.
127 The bazCleanup label is more interesting. Because the exception may be caught
128 by the try block, we must dispatch to its handler... but it does not exist
129 on the call stack (it does not have a VM Call->Label mapping installed), so
130 we must dispatch statically with a goto. The bazHandler thus appears as:
133 d->~D(); // destruct D as it goes out of scope when entering catch clauses
136 In general, TryHandler is not the same as bazHandler, because multiple
137 function calls could be made from the try block. In this case, trivial
138 optimization could merge the two basic blocks. TryHandler is the code
139 that actually determines the type of exception, based on the Exception object
140 itself. For this discussion, assume that the exception object contains *at
143 1. A pointer to the RTTI info for the contained object
144 2. A pointer to the dtor for the contained object
145 3. The contained object itself
147 Note that it is necessary to maintain #1 & #2 in the exception object itself
148 because objects without virtual function tables may be thrown (as in this
149 example). Assuming this, TryHandler would look something like this:
152 Exception *E = getThreadLocalException();
153 switch (E->RTTIType) {
155 ...int Stuff... // The action to perform from the catch block
158 ...double Stuff... // The action to perform from the catch block
159 goto TryCleanup // This catch block rethrows the exception
160 break; // Redundant, eliminated by the optimizer
162 goto TryCleanup // Exception not caught, rethrow
165 // Exception was consumed
167 E->dtor(E->object) // Invoke the dtor on the object if it exists
168 goto EndTry // Continue mainline code...
170 And that is all there is to it.
172 The throw(E) function would then be implemented like this (which may be
173 inlined into the caller through standard optimization):
175 function throw(Exception *E) {
176 // Get the start of the stack trace...
177 %frame %f = call getStackCurrentFrame()
179 // Get the label information that corresponds to it
180 label * %L = call getFrameLabel(%f)
181 while (%L == 0 && !isFirstFrame(%f)) {
182 // Loop until a cleanup handler is found
183 %f = call getNextFrame(%f)
184 %L = call getFrameLabel(%f)
188 call setThreadLocalException(E) // Allow handlers access to this...
189 call doNonLocalBranch(%L)
192 call BlowUp() // Ends up calling the terminate() method in use
195 That's a brief rundown of how C++ exception handling could be implemented in
196 llvm. Java would be very similar, except it only uses destructors to unlock
197 synchronized blocks, not to destroy data. Also, it uses two stack walks: a
198 nondestructive walk that builds a stack trace, then a destructive walk that
199 unwinds the stack as shown here.
201 It would be trivial to get exception interoperability between C++ and Java.