[LLVMdev] How to initiate/throw an exception (resume just continues)

David Chisnall David.Chisnall at cl.cam.ac.uk
Fri Mar 29 11:18:15 PDT 2013


On 29 Mar 2013, at 17:52, edA-qa mort-ora-y <eda-qa at disemia.com> wrote:

> On 29/03/13 18:34, David Chisnall wrote:
>> handlers, and so on), and a generic structure for the unwind library.
>> It then passes it to _Unwind_RaiseException(), which is the generic
>> unwind routine.
> 
> Excuse me if this sounds silly, but what "library" does the _Unwind_
> series of functions come from? Is this a kind of target ABI instrinsic
> library, or must I link with a particular runtime library to get these
> functions?

It's usually either libunwind or libgcc_s.  

> I assume using the JIT there will somehow already be there, so at least
> I can start experimenting. Perhaps another question, since this does
> generate ABI compliant EH-tables, could my throw function just invoke a
> native throw? That is, for really quick testing can I just create a c++
> function which does a throw?

The usual way of doing this is to write your throw and personality functions in C (or possibly C++) and then call them from your compiler.  The JIT should correctly generate and register unwind tables, although you may have to explicitly opt in to this.  You can use the C++ personality function if you create a typeinfo structure for all of the exceptions in your language and then use this type for everything.  Apple's Objective-C runtime does something similar, creating typeinfo structures corresponding to Objective-C classes and delegating the exception handling to them.

You probably also need to think about bracketing your catch blocks with something so that you can rethrow foreign exceptions and correctly clean up the memory from yours.  The old GCC Objective-C ABI got this wrong, and it was a colossal pain to try to fix.  

>> The personality function is probably the least fun code I've ever
>> written (I've now done it three times, for three different languages,
>> and it doesn't become any more fun).  If you get it wrong, the stack
>> is gone by the time you get into the debugger, so lots and lots of
>> logging statements are your friend...
> 
> I feel excited already. :)
> I'm still quite unclear on this part as well. Don't suppose you ever
> wrote a tutorial on this aspect, or have some example code you could show?

No, I probably should write a tutorial, since it's a frequently asked question.  Drop me an email off-list and I can point you at some existing code.

David





More information about the llvm-dev mailing list