<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 23 November 2015 at 18:31, Walter Bright via Digitalmars-d <span dir="ltr"><<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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:<br>
<br>
void foo1() {<br>
abc();<br>
try {<br>
def();<br>
}<br>
catch(DD t) {<br>
ghi(t);<br>
}<br>
catch(CC t) {<br>
ghi(t);<br>
}<br>
catch {<br>
mno();<br>
}<br>
jkl();<br>
}<br>
<br>
the code generated looks like this:<br>
<br>
_D3eh54foo1FZv:<br>
push RBP<br>
mov RBP,RSP<br>
sub RSP,010h<br>
call abc<br>
call def<br>
L12: call jkl<br>
jmp short L86<br>
cmp RDX,2<br>
je L59<br>
cmp RDX,3<br>
je L7F<br>
cmp RDX,1<br>
je L33<br>
mov RDI,RAX<br>
call _Unwind_Resume<br>
L33: sub RAX,8<br>
mov RAX,[RAX]<br>
mov ESI,offset _D3eh52DD7__ClassZ<br>
mov RDI,RAX<br>
call _d_dynamic_cast<br>
mov -8[RBP],RAX<br>
mov RAX,-8[RBP]<br>
mov RDI,RAX<br>
call ghi<br>
jmp short L12<br>
L59: sub RAX,8<br>
mov RAX,[RAX]<br>
mov ESI,offset _D3eh52CC7__ClassZ<br>
mov RDI,RAX<br>
call _d_dynamic_cast<br>
mov -010h[RBP],RAX<br>
mov RAX,-010h[RBP]<br>
mov RDI,RAX<br>
call ghi<br>
jmp short L12<br>
L7F: call mno<br>
jmp short L12<br>
L86: leave<br>
ret<br>
<br>
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?<br>
</blockquote></div><br><br></div><div class="gmail_extra">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 _d_dynamic_cast.<br><br></div><div class="gmail_extra">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.<br><br></div><div class="gmail_extra">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. :-)<br></div><div class="gmail_extra"><br></div></div>