Reputation: 65
I have a question about what value is passed to the C++ member function to invoke. When I disassemble a C++ member function like
void myClass::memberFunction() const;
I suppose it passed one implicit parameter this
as the first and only parameter, but in fact, there could be two or more parameters passed.
I disassembled libobjc.A.dylib (iOS14.8-arm64e) with hopper. this function class_rw::method() const
has no parameters, so I consider it pass only this
pointer as the only parameter, but it has another (x1) parameter passed.
Then actual code is opensource on apple
https://opensource.apple.com/source/objc4/objc4-818.2/runtime/objc-runtime-new.mm.auto.html
And the snapshot is like
It seems the compiler auto-generated additional variables for this member function. My question is that:
This should only happen when we put the implementation in the class declaration right? This seems non-portable for classes that hides its implementation in another cpp file.
Is there a name for such an implicit added variable? I mean when we develop a compiler for c++, what do we call for such optimization?
Upvotes: 2
Views: 212
Reputation: 126203
From the ARM64 procedure call standard:
Result Return
The manner in which a result is returned from a function is determined by the type of that result:
- If the type, T, of the result of a function is such that void func(T arg) would require that arg be passed as a value in a register (or set of registers) according to the rules in Parameter Passing, then the result is returned in the same registers as would be used for such an argument.
- Otherwise, the caller shall reserve a block of memory of sufficient size and alignment to hold the result. The address of the memory block shall be passed as an additional argument to the function in x8. The callee may modify the result memory block at any point during the execution of the subroutine (there is no requirement for the callee to preserve the value stored in x8).
So this seems to be an example of the second type -- the return type method_array_t
is something that is more than 16 bytes, so the caller allocates space for it and passes a pointer to that space as an extra argument.
Upvotes: 2