"Chuck F. " <> writes:
> Grumble wrote:
>> As far as I understand, C89 and C99 leave the details of
>> parameter passing up to the implementation. For that reason,
>> I've claimed in comp.lang.asm.x86 that there is no such thing as
>> "the C calling convention". Am I mistaken? ]
>
> No. However, C is almost unique among languages in having the error
> prone variadic functions available, which in turn mandate having the
> first parameter in a known place, and makes the reverse order
> (assuming a stack, which is not necessary) attractive.
Not just the first parameter; there can be one or more parameters
before the "...".
Before C89's introduction of prototypes, an particularly the "..."
syntax, there was generally no way for a compiler to tell whether
some_func(a, b, c, d);
was an ordinary function call or a call to a function expecting a
variable number and type of arguments. The compiler had to generate
the same kind of call for either case, and the function had to use
some technique that knew where to find the parameter values (typically
using the old <varargs.h> header, but there were uglier techniques
before that was introduced).
With modern prototypes, compilers are free to use a different and more
efficient calling convention for ordinary functions than for functions
that take a variable number of arguments (since a call to the latter
without a prototype invokes undefined behavior) -- but as far as I
know, most compilers have kept the old calling conventions. So, for
example, printf("%s\n", "Hello, world") is likely to work correctly
even if no prototype is visible, because the calling convention was
designed before prototypes existed.
This is, of course, no excuse the required "#include <stdio.h>".
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.