[LLVMdev] Getting exceptions to work?

Talin viridia at gmail.com
Tue May 19 21:00:59 PDT 2009

Duncan Sands wrote:
> I would just use the C personality function, __gcc_personality_v0, if
> I were you.  It should know where to jump to because the code generators
> record the invoke unwind target in the dwarf exception handling info in
> the object file.
So I tried what you suggested, and it just gives me a bus error:

    define i32 @main(i32, i8**) nounwind {
       call void @print(i8* bitcast ([6 x i8]* @str_begin to i8 *))
       invoke fastcc void @throwSomething() to label %nounwind unwind
    label %catch;

      %eh_ptr = call i8* @llvm.eh.exception();
      %eh_select34 = call i32 (i8*, i8*, ...)*
          @llvm.eh.selector.i32 (
              i8* %eh_ptr,
              i8* bitcast (i32 (i32, i32, i64, i8*, %UnwindContext*)*
    @__gcc_personality_v0 to i8*),
              i32 1)

      call void @print(i8* bitcast ([6 x i8]* @str_catch to i8 *))
      ret i32 0

      call void @print(i8* bitcast ([8 x i8]* @str_nocatch to i8 *))
      ret i32 -1

However, when I use my own personality function instead of 
__gcc_personality_v0, it doesn't crash, but it doesn't work right either 
(for reasons we've already discussed - it doesn't know how to redirect 
the execution to the landing pad):

    @str_persn = internal constant [10 x i8] c"persn %d\0a\00"
    define i32 @personality_func(i32 %version, i32 %action, i64
    %eh_class, i8* %eh_ptr, %UnwindContext* %eh_context) nounwind {
      call void (i8*, ...)* @printf(i8* bitcast ([10 x i8]* @str_persn
    to i8*), i32 %action)

      %phase = icmp eq i32 %action, 1
      br i1 %phase, label %search, label %handler

      ret i32 6
      call void @_Unwind_SetIP(%UnwindContext* %eh_context, i8* %cc0)
      ret i32 7

-- Talin

More information about the llvm-dev mailing list