edA-qa mort-ora-y
2013-Mar-29 17:15 UTC
[LLVMdev] How to initiate/throw an exception (resume just continues)
I'm trying to add some basic exception handling to my language now and I'm uncertain of how I actually start the exception propagation. It appears that "resume" will continue the exception, but I see no mention of how the exception actually starts. If I look at the IR output of a simple C++ program I see that it calls @__cxa_throw, but that is a library function. Somehow I must be able to initiate the exception in LLVM? -- edA-qa mort-ora-y -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Sign: Please digitally sign your emails. Encrypt: I'm also happy to receive encrypted mail. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 261 bytes Desc: OpenPGP digital signature URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130329/76eb9bbf/attachment.sig>
David Chisnall
2013-Mar-29 17:34 UTC
[LLVMdev] How to initiate/throw an exception (resume just continues)
On 29 Mar 2013, at 17:15, edA-qa mort-ora-y <eda-qa at disemia.com> wrote:> I'm trying to add some basic exception handling to my language now and > I'm uncertain of how I actually start the exception propagation. It > appears that "resume" will continue the exception, but I see no mention > of how the exception actually starts. > > If I look at the IR output of a simple C++ program I see that it calls > @__cxa_throw, but that is a library function. Somehow I must be able to > initiate the exception in LLVM?Yes, by calling a library function. You will need to implement this for your language, along with a personality function (or reuse ones from some existing language). The __cxa_throw() function is responsible for allocating an exception structure that contains two things, the C++ information (such as the thrown object, its type_info, the current values of the terminate and unexpected 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. The _Unwind_RaiseException() function walks up the stack, invoking the personality routine once to find a landing pad for a catch. If it fails to find one then it returns an error. If it succeeds, then it invokes them again to run cleanup code. The personality function is passed the thrown object and also a reference to the language-specific data area. This area is filled in by LLVM via landingpad instructions. The resume instruction is just a convenience. It will be expanded to a call to another library function (_Unwind_Resume(), I think, but possible _Unwind_ResumeOrRethrow()), and it exists so that the IR doesn't have to have hard-coded knowledge of the semantics of one these functions. The exact implementation of the personality function differs slightly between ARM and pretty much everything else for *NIX platforms. Win64 also has a very similar mechanism for exceptions, but I don't believe it is currently supported by LLVM. ARM now works in some cases, but is still quite buggy. 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... David
edA-qa mort-ora-y
2013-Mar-29 17:52 UTC
[LLVMdev] How to initiate/throw an exception (resume just continues)
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? 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 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? -- edA-qa mort-ora-y -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Sign: Please digitally sign your emails. Encrypt: I'm also happy to receive encrypted mail. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 261 bytes Desc: OpenPGP digital signature URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130329/d9e31333/attachment.sig>
Seemingly Similar Threads
- [LLVMdev] How to initiate/throw an exception (resume just continues)
- [LLVMdev] How to initiate/throw an exception (resume just continues)
- [LLVMdev] object file/linking is missing my exception handlers
- [LLVMdev] object file/linking is missing my exception handlers
- [LLVMdev] Adding comments to generated IR