Sign in Join
FPDoc Onlineexperiment

Parameter passing

When a function or procedure is called, then the following is done by the compiler:

  1. If there are any parameters to be passed to the procedure, they are stored in well-known registers, and if there are more parameters than free registers, they are pushed from left to right on the stack.

  2. If a function is called that returns a variable of type String, Set, Record, Object or Array, then an address to store the function result in, is also passed to the procedure.

  3. If the called procedure or function is an object method, then the pointer to self is passed to the procedure.

  4. If the procedure or function is nested in another function or procedure, then the frame pointer of the parent procedure is passed to the stack.

  5. The return address is pushed on the stack (This is done automatically by the instruction which calls the subroutine).

The resulting stack frame upon entering looks as in table Code issues.

Stack frame when calling a procedure (32-bit model)
Offset What is stored Optional?
+x extra parameters Yes
+12 function result Yes
+8 self Yes
+4 Return address No
+0 Frame pointer of parent procedure Yes

Parameter alignment

Each parameter passed to a routine is guaranteed to decrement the stack pointer by a certain minimum amount. This behavior varies from one operating system to another. For example, passing a byte as a value parameter to a routine could either decrement the stack pointer by 1, 2, 4 or even 8 bytes depending on the target operating system and processor.

For example, on FreeBSD, all parameters passed to a routine guarantee a minimal stack decrease of four bytes per parameter, even if the parameter actually takes less than 4 bytes to store on the stack (such as passing a byte value parameter to the stack).

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.