Re: calling convention stdcalll and cdecl call

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 its
parameters from the stack. To do this, it must know how many
parameters there are, and thus cannot take variable number of
parameters.
I'm sorry, that's incorrect (counter-example below).

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


.



Relevant Pages

  • Re: How to export afunction in class?
    ... CONCURRENTLY with the fetch of the top of stack to the IP, so it all occurs in a SINGLE ... But if it wants stdcall, the cdecl will definitely screw the stack over. ... the calling convention required by Visual Basic clients to call ... C-interface DLL code. ...
    (microsoft.public.vc.mfc)
  • Re: StdCall vs. CDecl
    ... Because the worst thing that happens when you call a cdecl function as a stdcall function is that you leave the arguments on the stack after the function returns. ... several different home grown libraries, all of which use CDecl calling semantics. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: hpux11 PA-RISC ABI Specification
    ... Yes, it does say that, but the convention did change. ... Copy" cache control hint that we wanted to use when storing to the stack ... whether any 16-byte stack frames were present in the code, ...
    (comp.arch)
  • Re: back online again...
    ... really "acceptable") convention. ... stack machine, but the design ... target with my compiler (may need a different name for this though, ... most of this code is not likely to ever go to object files anyways... ...
    (alt.lang.asm)
  • Re: Function arguments
    ... > The default calling convention for C is _cdecl. ... > pushed onto the stack in the reversed order and the caller ... > needs to clean up the stack. ... Second, there is no such thing as "_cdecl" in standard C, nor does C ...
    (comp.lang.c)