Question

In C++, a member function may have up to 2 implicit arguments: the this pointer and the address of the returned object. They precede the explicit arguments, but: which one goes first?

In particular, I'm interested in what happens in Android NDK (gcc-based, ARM).

Example:

class MyClass {
public:
  int a,b;
  MyClass(int aa,int bb):a(aa),b(bb){};
  MyClass modif(int da, int db) {return MyClass(a+da,b+db);} //an object is returned
};
Was it helpful?

Solution

It seems like you're asking about the calling convention (which registers / where on the stack arguments & return values are stored before branching to a function).

It depends a lot on the target ABI and even then it's fairly complicated. For ARMv5/6/7, the Procedure call standard [pdf] is your bible.

You should really read the whole thing if you actually care, but here are the parts you're interested in:

  • A Composite Type not larger than 4 bytes is returned in r0.
  • A Composite Type larger than 4 bytes, or whose size cannot be determined statically by both caller and callee, is stored in memory at an address passed as an extra argument when the function was called.
    • If the subroutine is a function that returns a result in memory, then the address for the result is placed in r0 and the NCRN is set to r1.
  • If the size in words of the argument is not more than r4 minus NCRN, the argument is copied into core registers, starting at the NCRN

Also

  • For C++, an implicit this parameter is passed as an extra argument that immediately precedes the first user argument.

So in most cases the address of a returned composite value is in $r0 and the implicit this pointer is in $r1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top