www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - less-than-ideal codegen for Interfaces

reply "van eeshan" <vanee hotmail.net> writes:
Given this ...

interface I
{
        I method();
}

class A : I
{
        I method()
        {
                return this;    // ???
        }
}

... the compiler generates a bunch of seemingly unnecessary code to convert
a 'this' into an Interface for the return value. Here's the optimized
codegen output:

10:   class A : I
11:   {
12:           I method()
00402018   enter       4,0
0040201C   mov         dword ptr [ebp-4],eax
13:           {
14:                   return this;
0040201F   cmp         dword ptr [this],0
00402023   je          _D5hello1A6methodFZC5hello1I+15h (0040202d)
00402025   mov         eax,dword ptr [this]
00402028   lea         eax,[eax+8]
0040202B   jmp         _D5hello1A6methodFZC5hello1I+17h (0040202f)
0040202D   xor         eax,eax
15:           }
0040202F   leave
00402030   ret

What's with the test of 'this' against zero? I'm hoping that this is only
something that fell through the cracks. I've seen the same code-pattern
dozens and dozens of times with respect to Interface usage, but never for a
class. It sucks ~ but then the compiler is still beta.

While we're on the subject, a class implementing an Interface should
probably be implicitly convertible to an Object: without going through a
dynamic cast. The following code currently requires a dynamic cast:

        Object convert(I i)
        {
                return i;  // fails. must be cast(Object) i;
        }
Aug 07 2004
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"What's with the test of 'this' against zero? I'm hoping that this is only
something that fell through the cracks"

indeed.. it doesn't make much sense to see if "this" is null before calling
a function on it.. especially since a member function can't be called with a
null "this" in the first place, as you'd get an access violation!  the cmp,
je, jmp, and xor lines could all be removed and there would be no effect.
Aug 09 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"van eeshan" <vanee hotmail.net> wrote in message
news:cf4bdn$2k3c$1 digitaldaemon.com...
 What's with the test of 'this' against zero? I'm hoping that this is only
 something that fell through the cracks. I've seen the same code-pattern
 dozens and dozens of times with respect to Interface usage, but never for
a
 class. It sucks ~ but then the compiler is still beta.
That's fixed now.
 While we're on the subject, a class implementing an Interface should
 probably be implicitly convertible to an Object: without going through a
 dynamic cast. The following code currently requires a dynamic cast:

         Object convert(I i)
         {
                 return i;  // fails. must be cast(Object) i;
         }
This still needs an explicit cast, because although all D interfaces are ultimately derived from Object, it is possible to have an interface passed in from some other language that is not from Object.
Aug 15 2004
next sibling parent reply "van eeshan" <vanee hotmail.net> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cfpk7u$1qgs$1 digitaldaemon.com...
 "van eeshan" <vanee hotmail.net> wrote in message
 news:cf4bdn$2k3c$1 digitaldaemon.com...
 What's with the test of 'this' against zero? I'm hoping that this is
only
 something that fell through the cracks. I've seen the same code-pattern
 dozens and dozens of times with respect to Interface usage, but never
for
 a
 class. It sucks ~ but then the compiler is still beta.
That's fixed now.
Thank you! May I respectfully ask about the bug named "Calling through an interface invokes the wrong method" ?
 While we're on the subject, a class implementing an Interface should
 probably be implicitly convertible to an Object: without going through a
 dynamic cast. The following code currently requires a dynamic cast:

         Object convert(I i)
         {
                 return i;  // fails. must be cast(Object) i;
         }
This still needs an explicit cast, because although all D interfaces are ultimately derived from Object, it is possible to have an interface passed in from some other language that is not from Object.
Good point. Glad to hear you hope other languages will adhere to the D binary spec.
Aug 15 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"van eeshan" <vanee hotmail.net> wrote in message
news:cfpktk$1qrl$1 digitaldaemon.com...
 Thank you!  May I respectfully ask about the bug named "Calling through an
 interface invokes the wrong method" ?
That's fixed too. I sent you an email, but your reply address is invalid.
 This still needs an explicit cast, because although all D interfaces are
 ultimately derived from Object, it is possible to have an interface
passed
 in from some other language that is not from Object.
Good point. Glad to hear you hope other languages will adhere to the D binary spec.
It has a lot more to do with D's interfaces supporting the way COM works, and so can interact with COM objects. COM support is common with win32 languages.
Aug 16 2004
parent "van eeshan" <vanee hotmail.net> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cfqt3v$2nem$2 digitaldaemon.com...
 Thank you!  May I respectfully ask about the bug named "Calling through
an
 interface invokes the wrong method" ?
That's fixed too.
Excellent. Much appreciated!
Aug 16 2004
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <cfpk7u$1qgs$1 digitaldaemon.com>, Walter says...

although all D interfaces are
ultimately derived from Object, it is possible to have an interface passed
in from some other language that is not from Object.
Intriguing. Care to tell us how? Some faked-up examples in C would be neat. Jill
Aug 16 2004
parent "Walter" <newshound digitalmars.com> writes:
"Arcane Jill" <Arcane_member pathlink.com> wrote in message
news:cfq9m7$29sj$1 digitaldaemon.com...
 In article <cfpk7u$1qgs$1 digitaldaemon.com>, Walter says...

although all D interfaces are
ultimately derived from Object, it is possible to have an interface
passed
in from some other language that is not from Object.
Intriguing. Care to tell us how? Some faked-up examples in C would be neat.
Sure. Check out the COM examples in \dmd\src\d !
Aug 16 2004