Thread overview
less-than-ideal codegen for Interfaces
Aug 08, 2004
van eeshan
Aug 16, 2004
Walter
Aug 16, 2004
van eeshan
Aug 16, 2004
Walter
Aug 16, 2004
van eeshan
Aug 16, 2004
Arcane Jill
Aug 16, 2004
Walter
August 08, 2004
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;
        }






August 10, 2004
"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.


August 16, 2004
"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.


August 16, 2004
"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.


August 16, 2004
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


August 16, 2004
"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.


August 16, 2004
"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 !


August 16, 2004
"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!