simple ABI change to enable implicit conversion of functions to delegates?
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Mon May 15 14:56:53 PDT 2017
On Monday, 15 May 2017 at 21:06:57 UTC, ag0aep6g wrote:
> On 05/15/2017 10:34 PM, kinke wrote:
>> If you just want to append an extra context arg by passing it
>> as last
>> actual arg, it'll end up in the stack sooner or later, and
>> that, I
>> guess, is where bad things may happen by just pushing an
>> additional arg,
>> not matching the function signature.
>
> I'm certainly reaching the boundaries of my limited knowledge
> here, but wouldn't it work when we push the context arg first,
> before any other arguments, and pop it in the caller?
It really depends on the target platform. There may be ways to
optimize it for particular systems with particular argument sets,
but in the long run if you want to support this in the general
case on all functions, your proposal would require every function
to use the same ABI as it's delegate counterpart, which includes
the code to unwind the stack if the context pointer was passed in
there or any extra setup code in the caller.
If this proposal was integrated I could also imagine applications
wanting to create functions that are not delegate compatible so
they don't have to incur the delegate ABI overhead, i.e.
extern(NoContextPointer) void foo(int x)
{
}
Of course I suppose extern(C) would already do this.
In short, this proposal modifies existing the function ABI to use
the delegate ABI which will have a runtime cost in the general
case (even though some cases may have zero cost). It's
sacrificing performance for ALL functions when in practice
applications will only need this for a small subset of their
functions.
I like the concept but I wouldn't want it to be the default for
all functions, I think it should be an "opt-in" feature for every
function. My DIP is one way you could cover this use case (a bit
differently) or you could introduce a new extern like
'extern(delegate)' to say that the function should use the same
ABI as it's delegate counterpart.
extern(delegate) void foo(int x)
{
}
void delegate(int) x = &foo;
x();
I'd definitely be ok with this. It doesn't handle the use cases
where you actually want the function to use the context pointer,
but it handles your situation well.
More information about the Digitalmars-d
mailing list