PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Parameter Passing

I'm aware that passing an excessive amount of parameters to a function can hurt performance. Can someone experienced with ARM architecture please quantify this (ie how many parameters can be passed for free, does the size of the parameters matter, etc)?

Thanks.

Dan East
angedelamort · 12 Mar 2003, 16:18 UTC
I'm not an expert in ASM, but for each parameters, as u probably know, it will push a 32 bit pointer to your object on the stack. So, I think, even if you use just 1 parameter, it will not be "free".
IIRC the EVC++ compiler uses the APCS (ARM Procedure Call Standard) to call functions. That means that the first four parameters (up to 32bits each) are passed in registers R0-R3 and then the rest are stacked for the callee to unstack and use.

Basically you get 4 "free" parameters (size<=32bits) to pass to a function, and any more cost you a push/pull to/from the stack.
Addendum:

http://www.arm.com/support/566FQ9/$File/ATPCS.pdf

(I can't check the link because www.arm.com seems to be either down or my proxy can't see it at the moment).

From MSDN:

In general, the argument list is laid out as if it were a memory-based structure with padding inserted to achieve appropriate alignment. The first four 32-bit words (words 0 through 3) are passed in registers R0 through R3. If a callee takes the address of any of the arguments passed in these registers (either directly or through varargs), and the resulting pointer has any possibility of being used to reference subsequent arguments, at least that argument and all that follow it in the registers must be copied to the stack area reserved for that purpose. The reserved area is adjacent to the rest of the argument list so that the usual varargs techniques work.

Integer and integer-equivalent arguments (for example, char, pointer), which are 32-bits or smaller, are passed as 32-bit words. A 64-bit integer argument is passed as a 4-byte-aligned scalar.
A 64-bit floating-point argument (type double) is passed as a 4-byte aligned scalar. 32-bit floating-point arguments (type float) are passed as 32-bit words.
A structure or union parameter is passed as a copy.
The first four words of the resulting memory image are then passed in integer registers, and the remaining words for the argument list image are passed on the stack. If only part of a structure or union parameter fits into the first four words of the argument list; the remainder is stored on the stack.
The ARM/Thumb calling sequence provides a varargs implementation that supports all the typical abuses of the ANSI C varargs specification.

A va_list can be implemented as a real C pointer.
va_arg() can be implemented as properly casting and dereferencing the va_list, then incrementing the va_list by the size of the given type.
va_start() can be implemented as returning the address of the item immediately following the one given to it.
va_end() can be implemented as a no-op.
Thanks. So the next question is how many cycles are required to push and pop a parameter to the stack per function call?

Dan East
No problem.

I'm going to assume that the stack is cached (average case it should be):

Pushing: 1 cycle per 32-bit item stacked (always)

Popping:

1 register = 1 cycle (+ 1 cycle result delay for the result if it's sign-extended)

>1 register = (number of registers cycles + 1 cycle delay)
For future reference, there's a section in the EVC help file that documents the calling convention for ARM (as well as all the 'old' CPUs).

It's under \Microprocessor Reference\ARM\ARM Calling Standard.