Parameter passing
When a function or procedure is called, then the following is done by the compiler:
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.
If a function is called that returns a variable of type
String,Set,Record,ObjectorArray, then an address to store the function result in, is also passed to the procedure.If the called procedure or function is an object method, then the pointer to
selfis passed to the procedure.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.
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.
| 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.
