Why return object address in %rax when returning a MEMORY type?

Viewed 145

According to the AMD64 calling convention when returning an object that is stored in memory the steps are the following:

  1. Caller allocates memory for the returned object and passes address as a hidden parameter in %rdi.
  2. Callee fills in object.
  3. Callee returns the address of the object in %rax.

I'm wondering why the 3rd requirement was added to the standard? The memory is already allocated by the caller, so the caller doesn't need to be reminded where it made the allocation. What's the point of returning the address of the memory in %rax?

1 Answers

RDI is the destination index for string operations, its value might be overridden as part of the callee processing. It makes sense to return the address via the accumulator RAX.

Related