Re: calling convention stdcalll and cdecl call
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Fri, 18 Jul 2008 14:11:10 -0400
Alf P. Steinbach <alfps@xxxxxxxx> wrote:
* Igor Tandetnik:
"Alf P. Steinbach" <alfps@xxxxxxxx> wrote in message
news:XMudnXxazN5UzR3VnZ2dnUVZ_oWdnZ2d@xxxxxxxxxxxxx
* Igor Tandetnik:
With stdcall, the function is responsible for removing itsI'm sorry, that's incorrect (counter-example below).
parameters from the stack. To do this, it must know how many
parameters there are, and thus cannot take variable number of
parameters.
Your example uses additional domain-specific information that the
compiler doesn't have, in general: that the first parameter specifies
the total number of remaining parameters,
Are you of the impression that a vararg routine can access its
arguments without some information about them?
No, but the precise way the routine acheives that is not known to the
compiler.
Or are you suggesting that the caller should pass the total size of
all parameters as a hidden first parameter?
That is how it has worked on at least one OS, so it's not a bad idea,
but neither it is a required way to do it.
What other ways would you suggest to allow the callee to clean up the
stack space occupied by a variable number of parameters? Preferably ways
that a compiler could apply mechanically, without having to understand
the meaning of individual parameters (like having to parse printf-like
format string).
That defeats the whole point of
__stdcall convention, which is to shave one machine instruction off
the call site. If you are willing to push the size of parameters
onto the stack before the call, you can just as well update ESP
after the call instead - at which point you are happily back in
__cdecl land.
I'm sorry, but there's no connection between premise and conclusion,
and the conclusion is wrong (e.g. printf is a counter-example).
printf is a counterexample of what? It uses cdecl convention, where the
caller is responsible for clearing up the stack. Consider also:
printf("%d", 1, 2);
How would your hypothetical stdcall printf know to remove 12 bytes worth
of parameters, rather than 8?
Let's put it this way. Yes, it is possible to design a calling
convention that a) allows for variable number of arguments, and b)
requires the callee to clean the stack. All you have to do is to have
the caller set up and pass sufficient additional information to the
callee (e.g. total size of all arguments).
However, it's not clear _why_ you would want such a thing. It has no
advantage over __cdecl - you still need an extra machine instruction or
two at every call site to set up this additional information (which is
the problem __stdcall was designed to solve). And, it complicates the
callee, too - witness the dance you had to perform in your assembly code
to simulate RET instruction (where __cdecl function would just have
plain RET, and __stdcall one would have RET n for some constant n).
You complained that tools and libraries don't support such a calling
convention. This is the reason why - this convention doesn't make much
sense. It doesn't improve in any way on those already supported, and in
fact makes things (slightly) worse. Just because something is, in
principle, possible, doesn't necessarily mean it's a good idea.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: calling convention stdcalll and cdecl call
- From: Alexander Grigoriev
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- References:
- Re: calling convention stdcalll and cdecl call
- From: Igor Tandetnik
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- From: Igor Tandetnik
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- Prev by Date: Re: calling convention stdcalll and cdecl call
- Next by Date: Re: calling convention stdcalll and cdecl call
- Previous by thread: Re: calling convention stdcalll and cdecl call
- Next by thread: Re: calling convention stdcalll and cdecl call
- Index(es):
Relevant Pages
|