cojocar
cojocar

Reputation: 1813

Force gcc to pass the parameters on the stack

Is there a way to force gcc to pass the parameters of a function on the stack?

I don't want to use the registers for parameter passing.

Update: I'am using arm-gcc from CodeSourcery

Upvotes: 8

Views: 4618

Answers (3)

Jiang Esson
Jiang Esson

Reputation: 11

Where to store a local variable is depend on how you will use it. If you need to get a local variable's address, the local variable can only be stored on the stack. So when you pass your subroutine a pointer, this parameter will be passed through the stack.

Upvotes: 0

anatolyg
anatolyg

Reputation: 28320

You can try wrapping the parameters in a structure; for example, if your function is int calc_my_sum(int x, int y) {return x+y;} you can change it as follows (ugly):

struct my_x_y {
    int x, y;
    my_x_y(): x(0), y(0) {} // a non-trivial constructor to make the type non-POD
};

int calc_my_sum(my_x_y x_and_y) {
    // passing non-POD object by value forces to use the stack
    return x_and_y.x + x_and_y.y;
}

Alternatively, you can just add 4 dummy parameters to use up the registers, so other parameters will use stack:

struct force_stack_usage {
    int dummy0, dummy1, dummy2, dummy3;
}

int calc_my_sum(force_stack_usage, int x, int y) {
    return x + y;
}

Upvotes: 2

user257111
user257111

Reputation:

According to: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf

The first four registers r0-r3 (a1-a4) are used to pass argument values into a subroutine and to return a result value from a function. They may also be used to hold intermediate values within a routine (but, in general, only between subroutine calls).

There are, on ARM, no other calling conventions but the default, that I know of. And here is why:

  1. Why would you want to? Your function would not be callable in compiled form by other functions, creating a compatibility mess.
  2. Your calls to system functions which comply with the ABI wouldn't work, unless the compiler was able to differentiate between calling conventions. Now, I know in the past there are different calling conventions for x86-32, but notice how x64 is simpler (AMD64 vs whatever Microsoft did). Why, when designing the ARM calling convention, would you allow so many different calling conventions? It creates a compatibility mess.

Upvotes: 0

Related Questions