简体   繁体   中英

Calling conventions with composite date types

I understand the calling conventions for passing 32-bit and 64-bit integers (and pointers), floats, and doubles for 64-bit code for Micrsoft and the System V AMD64 ABI. But it's not clear to me what the calling conventions there are for composite data types.

To be more clear what are the calling conventions for passing structures, classes, and unions by value in functions with external linkage (ie not static inline functions)? I'm particular interested in simple structures such as

typedef struct doublefloat { float hi; float lo; } doublefloat;
typedef struct doubledouble { double hi; double lo; } doubledouble;
typedef struct int128 { int64_t hi; int64_t lo; } int128; 

doublefloat foof(float a, float b);    
doubledouble food(double a, double b);
float foo3(doubledouble a, doubledouble b);    
int128 fooi(int64_t a, int64_t b);

Here is what I observed in GCC (with -O3)

  • foof returns hi and lo packed into the first 64-bits of XMM0 .
  • food returns hi and lo into XMM0 and XMM1 .
  • foo3 passes hi and lo from a and b in XMM0 , XMM1 , XMM2 , and XMM3 .
  • fooi returns hi and lo into rda and rdx

Agner Fog describes the details (which agree with by observations) for each compiler at http://www.agner.org/optimize/calling_conventions.pdf

See Table 6. Methods for passing structure, class and union objects and Table 7. Methods for returning structure, class and union objects .

For 64-bit code his tables are divided into Windows and Linux/BSD/Mac and not per compiler so this implies to me that there is some standard for composite data types. Is this correct or is the passing and returning of composite data type potentially defined by each compiler or each version of a compiler (ie could change with the next version).

Note that I understand that in practice that in many of these cases static inline would probably be best to use anyway. Also note that even though C does not have classes I'm still interested in how structures and unions are passed by value in C and not just C++ which is why I have included the C tag.

Some standards do exist. For example, the SystemV AMD64 ABI document describes parameter passing for aggregates in some detail (starting at page 17). I won't copy the relevant part of the text here as there are several pages.

Not all platforms are likely to be so well documented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM