digitalmars.D - runtime vararg can easily be broken
- davidl (28/28) Jun 12 2009 The runtime vararg push into stack with align of 4, however programmer
- Robert Fraser (2/6) Jun 12 2009 Got my vote!
- Daniel Keep (3/8) Jun 12 2009 That's because you're not supposed to do that.
- grauzone (14/25) Jun 12 2009 Contains only a template. Here's the solution (untested):
- Jason House (3/32) Jun 13 2009 All non-digitalmars D compilers I've seen have done alternate implementa...
- davidl (7/15) Jun 12 2009 Then you probably want to accuse the spec examples of vararg functions. ...
The runtime vararg push into stack with align of 4, however programmer might neglect this easily and cause problem. import std.boxer; import std.stdio; void func(...) { Box[] arguments; arguments.length = _arguments.length; for(int i;i<_arguments.length;i++) { arguments[i] = box(_arguments[i], _argptr); _argptr += _arguments[i].tsize; } foreach(arg;arguments) writefln(arg); } void main() { func(34,43); func(cast(ushort)4, cast(ushort)5); // this fails } I don't know if tango Stdout suffers from this. But this really can easily cause problems. Also the whole paradigm of coding a runtime vararg func is so troublesome and even much complex compared to the compile time vararg. Maybe we should borrow something from compiletime to aid the runtime vararg programming. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Jun 12 2009
davidl wrote:Also the whole paradigm of coding a runtime vararg func is so troublesome and even much complex compared to the compile time vararg. Maybe we should borrow something from compiletime to aid the runtime vararg programming.Got my vote!
Jun 12 2009
davidl wrote:The runtime vararg push into stack with align of 4, however programmer might neglect this easily and cause problem. ...That's because you're not supposed to do that. std.stdarg
Jun 12 2009
Daniel Keep wrote:davidl wrote:Contains only a template. Here's the solution (untested): void* va_arg(inout void* _argptr, TypeInfo ti) { void* result = _argptr; _argptr = _argptr + ((ti.tsize + int.sizeof - 1) & ~(int.sizeof - 1)); return arg; } (int.sizeof is always 4, but I doubt the original code runs correctly on anything but 32 bit x86 anyway.) We all know that (dynamic) varargs in D really, really suck. A lot of simple enough solutions have been proposed. But nothing happens. "The trivialer the solution, the more likely it is that the problem itself will never be solved and the programmer always has to fight with this trivial garbage."The runtime vararg push into stack with align of 4, however programmer might neglect this easily and cause problem. ...That's because you're not supposed to do that. std.stdarg
Jun 12 2009
grauzone Wrote:Daniel Keep wrote:All non-digitalmars D compilers I've seen have done alternate implementations for this.davidl wrote:Contains only a template. Here's the solution (untested): void* va_arg(inout void* _argptr, TypeInfo ti) { void* result = _argptr; _argptr = _argptr + ((ti.tsize + int.sizeof - 1) & ~(int.sizeof - 1)); return arg; } (int.sizeof is always 4, but I doubt the original code runs correctly on anything but 32 bit x86 anyway.) We all know that (dynamic) varargs in D really, really suck. A lot of simple enough solutions have been proposed. But nothing happens.The runtime vararg push into stack with align of 4, however programmer might neglect this easily and cause problem. ...That's because you're not supposed to do that. std.stdarg"The trivialer the solution, the more likely it is that the problem itself will never be solved and the programmer always has to fight with this trivial garbage."
Jun 13 2009
在 Sat, 13 Jun 2009 02:17:03 +0800,Daniel Keep <daniel.keep.lists gmail.com> 写道:davidl wrote:Then you probably want to accuse the spec examples of vararg functions. Also runtime vararg code shouldn't care anything about the alignment of stack for the sake of performance improvement. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/The runtime vararg push into stack with align of 4, however programmer might neglect this easily and cause problem. ...That's because you're not supposed to do that. std.stdarg
Jun 12 2009