Call Conventions

Alan Knowles alan at akbkhome.com
Fri Jan 4 00:08:31 PST 2008


While it's not going to solve that issue, I think this should achieve 
what you are trying to do, although not quite as clean...


import std.string;
import std.stdio;

class Value {
	static Value[] emptyAr;
	int type = 0;
	int ival;
	this(int v) {
		this.type = 1;
		this.ival = v;
	}
	char[] cval;
	this(char[] v) {
		this.type = 2;
		this.cval = v;
	}
	char[] asString() {
		if (this.type == 2){
			return cval;
		}
		return std.string.toString(ival);
	}
}



void testArgs(char[] x, Value[] somevals) {
	foreach (v; somevals) {
		writefln("got vals %s", v.asString());
	}
}


void main ()
{
	
	testArgs("fred",
		Value.emptyAr
		~ (new Value(3))
		~ (new Value("dog"))
		~ (new Value(6)));
	
}






Dan wrote:
> While I must admit it's most probably a very rare thing to need to
> specify a calling convention; when you do need to it's excrutiating.
> 
> For Walnut, I'm currently doing this:
> 
> struct Value.sizeof = 16;
> 
> Value x(Value self, Value cc, Value[] arguments ...){ bla bla bla }
> 
> 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)
> 
> Would be fabulous.
> 
> Regards, Dan



More information about the Digitalmars-d mailing list