* Muthu:
I've read calling conventions to be the order(reverse or forward) in
which the parameters are being read & understood by compilers.
For ex. the following function.
int Add(int p1, int p2, int p3);
The parameters here can be read either in the forward order from p1
till p3 or reverse order from p3 till p1.
Generally a calling convention includes
* General mechanism for parameters (in registers, static memory
locations, on the stack and if so in what order).
* Specifics of passing certain types of parameters, e.g. is a source
code pass-by-value struct passed as a pointer? If nested functions
are supported, how are outer function arguments accessed (Dijkstra
display or what)? And so on.
* Who is responsible for clean-up, e.g. with stack-based parameter
passing, who pops the stack on function return?
The calling convention in turn usually determines or constrains
* Transformation of source code names to linkage level names.
Can anyone explain what is the advantage/disadvantage of either of
these?
What is commonly called "C" calling convention is stack-based with
arguments pushed from right to left and caller responsible for popping,
and source code names transformed by prepending a single underscore.
That does not mean that a C compiler necessarily uses that convention,
but most do.
Main advantage is that it allows for variable length argument lists,
the "..." notation in C++, since the called function has the named
arguments at known locations, and since the caller has the knowledge
needed to pop the stack no matter how many arguments were pushed.
What is commonly called "Pascal" calling convention is stack-based
with arguments pushed from left to right and called function
responsible for popping (no common convention for name transform).
Pascal supports nested functions but how that aspect is implemented
is not specified when one says "Pascal" calling convention.
Main advantage is reduced machine code size.
What does C++ standard say about this?
It doesn't say much at all, only that 'extern "C"' should give
compatibility with some C compiler.
I think that is one aspect that would be nice to have in upcoming
"version 2" of the standard: support for specifying calling convention
and name transformation.
Another aspect: how can the stack be properly cleaned up in
void f( int x, ... )
{
throw std::runtime_error( "Oops!" );
}
void g()
{
f( 0, "What?", 42 );
}
using a variable length argument list.