[LLVMdev] Getting exceptions to work?

Talin viridia at gmail.com
Sun May 17 23:54:09 PDT 2009

I've been struggling for several days, trying to get native exceptions 
to work in my code. I managed to boil down the IR to the simplest 
possible example that I can think of.

If anyone on this list can tell me what changes I need to make to the 
following code to get it to work (i.e. return 0 instead of a bus error), 
it would make my life immensely better.

    ; ModuleID = 'ExceptionTest'
        %Object = type {}
        %UnwindInfo = type { i64, void (i32, %UnwindInfo *)*, i16, i16 }
        %Throwable = type { %Object, %UnwindInfo }

    define i32 @main(i32, i8**) nounwind {
        invoke fastcc void @throwSomething() to label %nounwind unwind
    label %catch

        ret i32 0

        ret i32 -1

    define internal fastcc void @throwSomething() noreturn {
        %throwable = malloc %Throwable
        call fastcc void @Throwable.construct(%Throwable* %throwable)
        %unwindInfo = getelementptr %Throwable* %throwable, i32 0, i32 1
        %throw = call i32 @_Unwind_RaiseException(%UnwindInfo* %unwindInfo)

    define internal fastcc void @Throwable.construct(%Throwable* %self)
    nounwind {
        %exceptionClass = getelementptr %Throwable* %self, i32 0, i32 1,
    i32 0
        store i64 0, i64* %exceptionClass
        %exceptionCleanup = getelementptr %Throwable* %self, i32 0, i32
    1, i32 1
        store void (i32, %UnwindInfo *)* @exceptionCleanupFn, void (i32,
    %UnwindInfo *)** %exceptionCleanup
        %private1 = getelementptr %Throwable* %self, i32 0, i32 1, i32 2
        store i16 0, i16* %private1
        %private2 = getelementptr %Throwable* %self, i32 0, i32 1, i32 3
        store i16 0, i16* %private2
        ret void

    define internal void @exceptionCleanupFn(i32 %reason, %UnwindInfo *
    %exc) {
      ret void

    declare i32 @_Unwind_RaiseException(%UnwindInfo*) noreturn

-- Talin

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ExTest.ll
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20090517/6b2e2a8c/attachment.ksh>

More information about the llvm-dev mailing list