Call Conventions

Dan murpsoft at hotmail.com
Thu Jan 3 21:27:08 PST 2008


Jason House Wrote:
> Dan Wrote:
...
> > Now, the way that's getting done by D is absolutely horrid compared to the
> > way I'd like to do it - by taking advantage of XMM registers, using the
> > functions to mutate self, cc, and a third variable which keeps getting
> > returned, while arguments get copied.
> > 
> > I don't understand how I can do that in D without declaring every single
> > function naked, templating in raw assembler, and then processing the
> > function using raw assembler because the variables aren't where D expects.
> > 
> > *sigh*
> > 
> > How about a way to specify call convention and use it via
> > 
> > extern(MyCallConvention)
...
> 
> That sounds like a coll feature that D should have.  I have no idea how it'd
> get implemented under the hood.

Well, I would suggest utilizing opCall for the caller side... 

The idea is that if you inline opCall(), then essentially the code being generated is the contents of it (which could be inline assembler, or whatever works)

The real problem comes in the callee side, because you need to run arbitrary code before what you see (typically the ebp/esp stuff), bind registers and/or stack variables to names correctly, and then run arbitrary code after what you see (closing the context by add ebp or whatever)

My first suggestion would be that one could probably declare a given function to be a convention, at which point, any functions declared with that convention are passed as delegates to it.

extern(MyConvention) = MyConventionFunction;

void MyConventionFunction(lazy function f, *whatever*)
{
    bla bla bla
   f();
   bla bla bla
}

extern(MyConvention)
void bob()
{
  bla bla bla // this gets wrapped according to MyConventionFunction
}

Beyond that, using inline assembler correctly should handle the rest; and anyone who can't do that probably shouldn't be writing a call convention.

Is that correct enough?
How hard would it be to do that?

Is it possible to achieve this with mixins/templates without needing to butcher the "void bob(){}" functions?  (there'll be hundreds, and they *should* be legible)

Regards,
Dan



More information about the Digitalmars-d mailing list