cdecl keyword ( re: C Decl ... )

Marc J. Sabatella mjs at ernie.Berkeley.EDU
Sun Apr 3 05:43:30 AEST 1988


Microsoft (and Borland) both push use of the pascal keyword (and corresponding
cdecl) for efficiency purposes as well as mixed language applications.  Pascal
calling sequence is more efficient than C, so gains in space and time can be
achieved by compiling an entire file with Pascal calling conventions, even if
there is no Pascal code being linked anywhere.  This is why they include a
command line option to process a whole file as Pascal calling, and it is also
why there is a cdecl keyword to override this.  If you include the correct
header files for all library functions, they are all declared cdecl in the
prototype, so your source file can be compiled using Pascal or C conventions
with no change, and without ever using the cdecl or pascal keywords yourself.

	The reason Pascal sequences are more efficient than C is that varargs
constructions are not allowed.  The called routine knows EXACTLY how many
arguments it has been passed, and hence it can clean up the stack itself.
This can save space, because a routine that is called several times will only
have the code to restore the stack appear once, as opposed to C, in which it
must appear after each call.  In addiion, the 80x86 has a form of the return
instruction that taks an argument: "RET n".  This says to pop the return
address (and load it into PC), and, while you're at it, adjust the stack
pointer by n.  Thus the procedure return and stack restoration may be combined
into one instruction, provided the called routine knows how many bytes of
arguments it got.
	By the way, Pascal compilers often push the arguments left to right,
whereas C compilers almost always go right to left (so the first argument is
on top of the stack) because implementing varargs is much easier that way
[besides, I find it more convenient to access the parameters, anyhow].
This is another difference between Pascal and C conventions, but it is really
only a byproduct of the the above discussion; there is no reason Pascal
compilers can't push arguments right to left.

Marc Sabatella
mjs at ernie.Berkeley.EDU



More information about the Comp.lang.c mailing list