6.3 Calling mechanism

By default, the calling mechanism the compiler uses is register, that is, the compiler will try to pass as many parameters as possible 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. Method calls (from either objects or classes) have an additional invisible parameter which is self.

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 parameters 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.

Subroutines will modify a number of registers (the volatile registers). The list of registers that are modified is highly dependent on the processor, calling convention and ABI of the target platform.


Table 6.3: Calling mechanisms in Free Pascal






ModifierPushing orderStack cleaned byalignment





ínone┐ Left-to-right Callee default
register Left-to-right Callee default
cdecl Right-to-left Caller GCC alignment
interrupt Right-to-left Callee default
pascal Left-to-right Callee default
safecall Right-to-left Callee default
stdcall Right-to-left Callee GCC alignment
oldfpccallRight-to-left Callee default






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 430 on linking. Information on GCC registers saved, GCC stack alignment and general stack alignment on an operating system basis is beyond the scope of this manual.

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.2.7, page 46. 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.