views:

201

answers:

4

I know that __stdcall functions can't have ellipses, but I want to be sure there are no platforms that support the stdarg.h functions for calling conventions other than __cdecl or __stdcall.

+6  A: 

The calling convention has to be one where the caller clears the arguments from the stack (because the callee doesn't know what will be passed).

That doesn't necessarily correspond to what Microsoft calls "__cdecl" though. Just for example, on a SPARC, it'll normally pass the arguments in registers, because that's how the SPARC is designed to work -- its registers basically act as a call stack that gets spilled to main memory if the calls get deep enough that they won't fit into register anymore.

Though I'm less certain about it, I'd expect roughly the same on IA64 (Itanium) -- it also has a huge register set (a couple hundred if memory serves). If I'm not mistaken, it's a bit more permissive about how you use the registers, but I'd expect it to be used similarly at least a lot of the time.

Why does this matter to you? The point of using stdarg.h and its macros is to hide differences in calling convention from your code, so it can work with variable arguments portably.

Edit, based on comments: Okay, now I understand what you're doing (at least enough to improve the answer). Given that you already (apparently) have code to handle the variations in the default ABI, things are simpler. That only leaves the question of whether variadic functions always use the "default ABI", whatever that happens to be for the platform at hand. With "stdcall" and "default" as the only options, I think the answer to that is yes. Just for example, on Windows, wsprintf and wprintf break the rule of thumb, and uses cdecl calling convention instead of stdcall.

Jerry Coffin
Your comment about SPARC applies to many if not all RISC architectures. Since the registers are so plentiful, the compiler just uses those to pass arguments. Not to mention, they're faster than memory by quite a bit, esp. since you're likely passing data that's already in registers (although I guess it'll need to push them onto the stack to restore them if it needs them later, but...). Anyway, I know that this is how it works on MIPS and I'm pretty certain that PPC works the same way.
dash-tom-bang
*"Why does this matter to you?"* Ben is working on js-ctypes for Mozilla. You can see the exact context in https://bugzilla.mozilla.org/show_bug.cgi?id=554790 (search for "crazy idea") and you can some docs explaining js-ctypes at https://developer.mozilla.org/en/JavaScript_code_modules/ctypes.jsm
Jason Orendorff
Needless to say, js-ctypes is only available to application code and add-ons, not web pages.
Jason Orendorff
Currently we have a method for defining C functions from JavaScript, and that method has a parameter specifying the calling convention. I'm considering writing a version of that method for defining variadic C functions, and I wondered if it would need a similar parameter or not (in case the calling convention could just be inferred).
Ben
A: 

Do you mean 'platforms supported by MSVC" or as a general rule? Even if you confine yourself to the platforms supported by MSVC, you still have situations like IA64 and AMD64 where there is only "one" calling convention, and that calling convention is called __stdcall, but it's certainly not the same __stdcall you get on x86.

Logan Capaldo
A: 

AFAIK, the diversity of calling conventions is unique to DOS/Windows on x86. Most other platforms had compilers come with the OS and standardize the convention.

dan04
+1  A: 

The most definitive way that you can determine this is to analyze the calling conventions. For variadic functions to work, your calling convention needs a couple of attributes:

  • The callee must be able to access the parameters that aren't part of the variable argument list from a fixed offset from the top of the stack. This requires that the compiler push the parameters onto the stack from right to left. (This includes such things as the first parameter to printf, the format specification. Also, the address of the variable argument list itself must also be derived from a known location.)
  • The caller must be responsible for removing the parameters off the stack once the function has returned, because only the compiler, while generating the code for the caller, knows how many parameters were pushed onto the stack in the first place. The variadic function itself does not have this information.

stdcall won't work because the callee is responsible for popping parameters off the stack. In the old 16-bit Windows days, pascal wouldn't work because it pushed parameters onto the stack from left to right.

Of course, as the other answers have alluded to, many platforms don't give you any choice in terms of calling convention, making this question irrelevant for those ones.

Aaron Klotz