gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C


From: Benjamin Kowarsch
Subject: Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C
Date: Mon, 29 Jan 2024 08:29:22 +0900



On Mon, 29 Jan 2024 at 05:28, Rudolf Schubert wrote:

... are you sure the hidden parameter is passed BEFORE the open
array parameter?

If the array is passed by value, then the caller will push the size and a copy of the array onto the stack frame. Without knowing the size, the callee (the function) cannot determine where the array ends since its size may vary from call to call. This means that the size has to be stored at a static offset to the current frame pointer.

And since the stack doesn't have neither before nor after, what you consider before and after depends on your starting point, that is to say, whether you look at it from the viewpoint of the caller or callee.

From the viewpoint of the caller, the new stack frame is created and parameters and local variables are placed on it by subtracting values from the current frame pointer, that is, the stack pointer of the calling context.

PROCEDURE P ( p1 : T1; p2 : T2 );

=> FP(caller)-Offset(p1) > FP(caller)-Offset(p2)

From the viewpoint of the callee, the new stack frame has already been created and all parameters and local variables have already been placed on it, they are accessed by adding offsets to the current frame pointer, that is the stack pointer of the called (local) context.

PROCEDURE P ( p1 : T1; p2 : T2 );

=> FP(callee)+Offset(p1) < FP(callee)+Offset(p2)

This is the convention on the vast majority of architectures, if not all of them.

Thus, from the viewpoint of the callee, a parameter at a lower address is said to come before a parameter at a higher address.

If there is any parameter whose size is dynamic, that is to say it is only determined at runtime, then all offsets for parameters at higher addresses than said parameter with dynamic size can only be calculated if the size of the dynamic parameter can be determined beforehand, either by a static offset or by a combination of an initial static offset for the size of the very first dynamically sized parameter and any number of dynamic offsets for the sizes of any following dynamically sized parameters. The latter would be the case if there are multiple open parameters.

You may have looked at the stack frame in a top down representation and considered slots on the stack further up to be before slots further down, but the convention is to look at the current frame pointer represented by the bottom of the stack. This is so because it is the view of the called function.

I hope this clarifies.

At least with GPM and GM2 I use several C functions
where the (hidden) high value is passed AFTER the open array parameter.
So my C function would look like this:

void p ( char str[], unsigned argc );

If the array is passed by reference, then the size of its slot on the stack is known and the compiler may reorder the parameters for optimisation purposes since placing parameters in the order of their storage size makes it easier to avoid fragmentation. I can only assume that this is what happened there.

regards
benjamin

reply via email to

[Prev in Thread] Current Thread [Next in Thread]