Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a calling convention for arrays? [duplicate]

I am reading the System V Application Binary Interface, and there is one part that I cannot make sense of.

First, the document states that

No attempt has been made to specify an ABI for languages other than C

(page 10).

Later, on page 20, arrays are classified as MEMORY, POINTER etc.:

The classification of aggregate (structures and arrays) and union types works as follows:
...

The classification is then used to define the calling conventions — how the values and bounds on them are passed to and returned from functions. If I am reading the algorithm correctly, an array could be classified as INTEGER, MEMORY, or SSE.

But in the C language, arrays are always passed and returned as pointers. So why is it useful to classify arrays and in which situation does the array class matter?

like image 279
Roman Cheplyaka Avatar asked Nov 08 '22 02:11

Roman Cheplyaka


1 Answers

I figured it out: if an array is part of a struct or union, it may be passed in a register.

This C code

#include <stdint.h>

struct somebytes {
  uint8_t bytes[8];
};

uint8_t plus(struct somebytes p) {
  return p.bytes[3]+p.bytes[5];
}

translates to this assembly:

mov    %rdi,%rax
shr    $0x28,%rdi
shr    $0x18,%rax
add    %edi,%eax
retq   
like image 124
Roman Cheplyaka Avatar answered Nov 15 '22 04:11

Roman Cheplyaka