Throwing and catching exceptions
Exception handling in LLVM IR is closely tied to platform support. Here, we will look at the most common type of exception handling using libunwind
. Its full potential is used by C++, so we will look at an example in C++ first, where the bar()
function can throw an int
or double
value:
int bar(int x) { if (x == 1) throw 1; if (x == 2) throw 42.0; return x; }
The foo()
function calls bar()
, but only handles a thrown int
. It also declares that it only throws int
values:
int foo(int x) { int y = 0; try { y = bar(x); } catch (int e) { y = e; } return y; }
Throwing an exception requires two calls into the runtime library; this can be seen in the bar()
function. First, memory for the exception is allocated with a call to __cxa_allocate_exception()
. This function takes the number of bytes to allocate...