Dwarf Exception Handling question

Iain Buclaw via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 23 10:07:22 PST 2015

On 23 November 2015 at 18:31, Walter Bright via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> I'm struggling to understand dwarf EH, and figure it's a good idea to try
> and make it binary compatible with what gdc does, or at least not
> gratuitously different. If I use gdc to compile this:
> void foo1() {
>         abc();
>         try {
>                 def();
>         }
>         catch(DD t) {
>                 ghi(t);
>         }
>         catch(CC t) {
>                 ghi(t);
>         }
>         catch {
>                 mno();
>         }
>         jkl();
> }
> the code generated looks like this:
> _D3eh54foo1FZv:
>                 push    RBP
>                 mov     RBP,RSP
>                 sub     RSP,010h
>                 call      abc
>                 call      def
> L12:            call      jkl
>                 jmp short       L86
>                 cmp     RDX,2
>                 je      L59
>                 cmp     RDX,3
>                 je      L7F
>                 cmp     RDX,1
>                 je      L33
>                 mov     RDI,RAX
>                 call    _Unwind_Resume
> L33:            sub     RAX,8
>                 mov     RAX,[RAX]
>                 mov     ESI,offset _D3eh52DD7__ClassZ
>                 mov     RDI,RAX
>                 call    _d_dynamic_cast
>                 mov     -8[RBP],RAX
>                 mov     RAX,-8[RBP]
>                 mov     RDI,RAX
>                 call      ghi
>                 jmp short       L12
> L59:            sub     RAX,8
>                 mov     RAX,[RAX]
>                 mov     ESI,offset _D3eh52CC7__ClassZ
>                 mov     RDI,RAX
>                 call    _d_dynamic_cast
>                 mov     -010h[RBP],RAX
>                 mov     RAX,-010h[RBP]
>                 mov     RDI,RAX
>                 call      ghi
>                 jmp short       L12
> L7F:            call      mno
>                 jmp short       L12
> L86:            leave
>                 ret
> The calls to _d_dynamic cast appear to be redundant - shouldn't the value
> in RDX be sufficient? And why is there a 'default' call to _Unwind_Resume
> if RDX isn't an expected value? Shouldn't the personality routine simply
> not jump to the landing pad if none of the catch types are satisfied?

Yes, the _d_dynamic_cast is redundant.  This happened because the EH
pointer was treated as an Object, then upcasted to the catch type via the
normal convert() routines.  This is what caused the unnecessary call to

This switch statement is generated by GCC itself, and it tries to be
accommodating for all supported languages.  I imagine that the default case
is there to support `catch(...)` in C++ code, however D has no notion of
this construct, so what is instead generated is _Unwind_Resume to tell
unwind to keep looking up the call chain.

In other words, the default case should never really happen in D code
except in the event of a logic bug in the unwind EH personality function
(or possibly corruption). If you feel it more appropriate, I don't see the
harm in replacing it with whatever HLT or abort instruction you like. :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20151123/19de94df/attachment.html>

More information about the Digitalmars-d mailing list