RE: [GD-General] variadic functions
Brought to you by:
vexxed72
From: Stefan B. <ste...@di...> - 2003-11-27 11:46:56
|
No matter how you do it, it's going to be very architecture dependent so don't worry about portability. There is no portable way using C++. You might want to take a peek at libffi, which is a library which implements function calling for various platforms. I think the latest version is 2.0 but I don't have a link handy. You will probably find it at the Portable.Net site (probably in the core runtime): http://www.southern-storm.com.au/portable_net.html Last time I checked that had a more recent version than what could be found on the official libffi site. Cheers, Stef! :) Stefan Boberg Chief Technical Officer Digital Illusions CE AB -----Original Message----- From: Brett Bibby [mailto:res...@ga...] Sent: den 27 november 2003 11:03 To: gam...@li... Subject: Re: [GD-General] variadic functions Andras, Thanks for the post. Pretty much my conclusion too. But I'm not using this for printf so I need a generalized solution, although I could theoretically create bindings for every possible case (which is normal for scripting engine binding anyway). Since the callee doesn't know how many parameters there are, it seems I just need to push the values to the stack in assembly code and then call the variadic function. Does that sound right? Brett ----- Original Message ----- From: "Andras Balogh" <bn...@ma...> To: <gam...@li...> Sent: Thursday, November 27, 2003 4:28 PM Subject: Re: [GD-General] variadic functions > Hi Brett, > > > The compiler strips the "..." off the back and makes a normal function > > out of the variadic one, and the preceeding four functions are then used > > to access these unknown parameters. The start function takes the last > > known parameter as a value so at first glance it seems the function > > knows how many paramters there are because because it can compare the > > last known argument with the value on the stack. The problem is that > > since the parameters were pushed in reverse order how would it know how > > many parameters there are? If it were in direct order, you could take > > the stack pointer minus the stack pointer at the last parameter position > > (which is provided) and the diffference would be the number of > > arguments. But since they are reversed how does the va_arg function > > know when to stop? > > The answer is simple: It does not know! :( > > That's exactly why vararg functions always use the cdecl calling > convention. This requires that the caller cleans the stack, because > the caller always knows the number of params on the stack, while the > calle doesn't (in the case of vararg). > > Think of printf! The first param is supposed to be a pointer to a > format string. Reading that string printf determines how many (and of > what type) values it has to read from the stack. I'm pretty sure that > every one of us crashes a program every once in a while because of > unmatching params and format string. The reason for this is that there > is just no way printf can verify the stuff you gave is valid. > > > Has anybody else run into this before? How did you solve it? > > Well, you could use a format string like printf, or something similar. > > If you do this for scripting, then your script binding engine knows > all the params, so it might be a good idea to generate the format > description string automatically. This way the script writers could > call the function with any number of params without explicitly > describing their type and number in a format string. > > The ultimate solution would be of course the introduction of a new > calling convention, but I don't really know who's responsibility is > this... anyone? :) > > > Andras > > > > ------------------------------------------------------- > This SF.net email is sponsored by: SF.net Giveback Program. > Does SourceForge.net help you be more productive? Does it > help you create better code? SHARE THE LOVE, and help us help > YOU! Click Here: http://sourceforge.net/donate/ > _______________________________________________ > Gamedevlists-general mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-general > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_id=557 ------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive? Does it help you create better code? SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/ _______________________________________________ Gamedevlists-general mailing list Gam...@li... https://lists.sourceforge.net/lists/listinfo/gamedevlists-general Archives: http://sourceforge.net/mailarchive/forum.php?forum_id=557 |