Re: Making C better (by borrowing from C++)

From: Chris Croughton (chris_at_keristor.net)
Date: 02/20/05


Date: Sun, 20 Feb 2005 11:45:23 +0000

On Sat, 19 Feb 2005 23:18:41 GMT, CBFalconer
   <cbfalconer@yahoo.com> wrote:

> jacob navia wrote:
>>
>> All API functions under win32 use the _stdcall calling
>> convention. This means that the called function cleans up the
>> stack, i.e. adds to the stack before returning the space allocate
>> to make the argument frame. This means that all code for stack
>> adjustment is moved fom all call sites to the single instruction
>> in the called function.
>>
>> This can't be done with functions that receive an unknown number
>> of arguments like printf, for instance.
>>
>> In C you prototype those functions as
>> int myfn(char *,...);
>>
>> It would be impossible for such functions to clean up the stack
>> since they can't know how much was pushed, they discover it at
>> run time, and it could be perfectly legal to pass more arguments
>> than needed.
>>
>> It is needed then to have two disctinct calling conventions,
>> (there are more like fastcall where the arguments are passed in
>> registers but let's leave it at that).
>
> Things like printf, and variadic functions in general, are NOT OS
> calls (although they are a source of bugs). They are the creation
> of the C library alone. If the C compiler has a different calling
> protocol than the OS, then you simply need a collection of
> interface functions to do the conversion. Those go in the library.

Thinking about it, it's quite possible for a compiler to use two
different calling conventions automatically. If it's a variadic
function, or (in C90) has a null prototype (), the calling code pops the
parameters, otherwise the called function pops them. If a function has
a fixed number of parameters (as anything using _stdcall must do) this
this is not a problem.

If there isn't a prototype in scope at the time of the function call,
assume whichever you like and issue a warning, it's te programmer's
problem...

(Sorry jacob, you're just not inventive enough...)

Chris C



Relevant Pages

  • RE: Calling convention
    ... For calling conventions, cleaning stack means remove parameters info from ... I will explain cdecl, ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Calling convention
    ... For calling conventions, cleaning stack means remove parameters info from ... Microsoft Online Community Support ... nature are best handled working with a dedicated Microsoft Support Engineer ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Order of stack items
    ... By following the conventions, ... the word SIGN in number conversions takes a signed ... expected its argument in the 3rd cell on the stack, and started with a ROT. ...
    (comp.lang.forth)
  • Re: GCC front-end for FORTH?
    ... more or less the same as generating C from Forth and then compiling ... No, of course not, but Java's stack is local to the function, and Java ... and the particular calling conventions. ... I guess this question amounts to "is it possible to stop the back-end ...
    (comp.lang.forth)
  • Re: Making C better (by borrowing from C++)
    ... All API functions under win32 use the _stdcall calling convention. ... This means that the called function cleans up the stack, ... stuff from C++ like operator overloading, generic functions, ...
    (comp.lang.c)