Xref: utzoo comp.lang.c:8776 comp.sys.ibm.pc:13969 Path: utzoo!utgpu!water!watmath!clyde!rutgers!tut.cis.ohio-state.edu!bloom-beacon!think!ames!pasteur!ucbvax!ernie.Berkeley.EDU!mjs From: mjs@ernie.Berkeley.EDU (Marc J. Sabatella) Newsgroups: comp.lang.c,comp.sys.ibm.pc Subject: Re: cdecl keyword ( re: C Decl ... ) Summary: Pascal calling conventions more efficient Keywords: efficiency, varargs, "RET n" Message-ID: <23496@ucbvax.BERKELEY.EDU> Date: 2 Apr 88 19:43:30 GMT References: <1238@wjvax.UUCP> <297@ho7cad.ATT.COM> <1242@wjvax.UUCP> <7595@brl-smoke.ARPA> <2521@bsu-cs.UUCP> <7601@brl-smoke.ARPA> Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: mjs@ernie.Berkeley.EDU.UUCP (Marc J. Sabatella) Organization: University of California, Berkeley Lines: 33 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@ernie.Berkeley.EDU