6.3 Calling mechanism

By default, the calling mechanism the compiler uses is register, that is, the compiler will try to pass as much parameters as posible by storing them in a free register. Not all registers are used, because some registers have a special meaning, but this depends on the CPU.

Function results are returned in the accumulator (first register), if they fit in the register. Methods calls (from either objects or clases) have an additional invisible parameter which is self. This parameter is the leftmost parameter within a method call (it is therefore the last parameter passed to the method).

When the procedure or function exits, it clears the stack.

Other calling methods are available for linking with external object files and libraries, these are summarized in table (6.3). The first column lists the modifier you specify for a procedure declaration. The second one lists the order the paramaters are pushed on the stack. The third column specifies who is responsible for cleaning the stack: the caller or the called function. The alignment column indicates the alignment of the parameters sent to the stack area. Finally, the fifth column indicates if any registers are saved in the entry code of the subroutine.


Table 6.3: Calling mechanisms in Free Pascal






ModifierPushing orderStack cleaned byalignment registers saved





ínone┐ Left-to-right Function default None
register Left-to-right Function default None
cdecl Right-to-left Caller GCC alignmentGCC registers
interrupt Right-to-left Function default all registers
pascal Left-to-right Function default None
safecall Right-to-left Function default GCC registers
stdcall Right-to-left Function GCC alignmentGCC registers
oldfpccallRight-to-left Callee default None






Note that the oldfpccall calling convention equals the default calling convention on processors other than 32-bit Intel 386 or higher.

More about this can be found in chapter 7, page 390 on linking. Information on GCC registers saved, GCC stack alignment and general stack alignment on an operating system basis can be found in Appendix H. As of version 2.0 (actually, in 1.9.x somewhere) , the register modifier is the default calling convention, prior to that, it was the oldfpccall convention.

The default calling convention, i.e., the calling convention used when none is specified explicitly, can be set using the {$calling } directive, section 1.1.7, page 44. The default calling convention for the current platform can be specified with

{$CALLING DEFAULT}

Remark: The popstack modifier is no longer supported as of version 2.0, but has been renamed to oldfpccall. The saveregisters modifier can no longer be used.