On the richness of C++
Kevin Bealer
kevinbealer at gmail.com
Wed Apr 16 14:49:41 PDT 2008
Sean Kelly Wrote:
> == Quote from Sean Kelly (sean at invisibleduck.org)'s article
> > == Quote from Janice Caron (caron800 at googlemail.com)'s article
> > > On 16/04/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
> > > > The only way to merge function pointers with delegates is to have the
> > > > compiler generate trampolines. This isn't very runtime efficient - the
> > > > template approach will be faster.
> > > I think that all that is being suggested is that function (that which
> > > you get when you take the address of a static or global function)
> > > should implicitly cast to delegate (that which you get when you take
> > > the address of a local or member function).
> > I think the problem is that functions and delegates are called differently.
> > That is, the context pointer is passed as an invisible first parameter to
> > a delegate, while functions have no such invisible argument. Interestingly,
> > because the first parameter of a D function is supposed to be passed in a
> > register (EAX) on x86, I think you're right that it should be possible to
> > allow such efficient conversion so long as the D calling convention
> > accounts for it properly (and the platform supports such things).
>
> Er... forget that. A D function would obviously expect its first argument to
> be in EAX regardless of whether it's a delegate. Some conversion would be
> necessary unless I'm missing something.
>
>
> Sean
I'm not much of an ASM guru, but I would think that this could be done by
"multiple entry point" functions, i.e. prefixes that move registers around.
Non-delegate users would not pay for this code, but register-to-register
"MOV" should be cheap anyway, since this part of the code only touches
fields that must be touched soon anyway, and no branching, jumping, or
register saving/unsaving cycles is needed, so it should be pipelinable.
Pseudo ASM code:
method_start:
debug { assert(! this); }
mov arg1, arg2
mov arg2, arg3
mov arg3, [non-register argument]
(adjust stack fields to indicate 1 fewer arguments?)
function_start:
(code expecting a1, a2, a3)
return ...;
I guess this could only apply to "D linkage" functions, since we probably
don't know the parentage of C linkage functions, and some won't have the
special code.
Kevin
More information about the Digitalmars-d
mailing list