3

I'm looking at the AMD64 ABI and it does not appear to specify how to pass empty class types. For empty class member functions, it seems that this is passed as usual, but for empty classes, Clang generates code that appears to simply ignore this class. Is this correct according to the AMD64 ABI?

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Can you give an example that we can run in gcc explorer to compare clang and gcc generated code? And maybe specify with more precision what makes you think it is ignoring the this pointer? – PlasmaHH Apr 24 '13 at 10:11
  • You mean if you pass a class object by value? – Mats Petersson Apr 24 '13 at 10:11
  • For C++, the document you linked refers to the [IA-64 C++ ABI](http://mentorembedded.github.io/cxx-abi/abi.html), which states in chapter 3.1.3: `Empty classes will be passed no differently from ordinary classes.` Not completely sure if this is the context you are asking about, but I would say that Clang is wrong ... – Andreas Fester Apr 24 '13 at 10:15

1 Answers1

2

Whilst it is unclear exactly what the question is, the answer is pretty clear as to what the C++ ABI is:

For the C++ ABI we will use the IA-64 C++ ABI and instantiate it appropriately. The current draft of that ABI is available at:

Link to current location of Itanium ABI spec

This says:

Empty classes will be passed no differently from ordinary classes. If passed in registers the NaT bit must not be set on all registers that make up the class.

The contents of the single byte parameter slot are unspecified, and the callee may not depend on any particular value. On Itanium, the associated NaT bit must not be set if the parameter slot is associated with a register.

Another hint is available here:

A result of an empty class type will be returned as though it were a struct containing a single char, i.e. struct S { char c; };. The actual content of the return register is unspecified. On Itanium, the associated NaT bit must not be set.

So, an empty class should be treated like a struct containing a singe char, but that that single char is never used.

Of course, it's possible that there is a bug in clang with regards to empty classes. Although I doubt it, to be fair.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I raised a suggestion in #llvm that the binary interface might be identical, depending on the calling convention- that is, if I were to cast a pointer to a function that ommitted an empty class as it's last parameter, and then passed in a value, that this would be non-observable to the callee. – Puppy Apr 24 '13 at 12:20
  • Not sure I understand your comment, but as far as the ABI explains, you can't "ignore" empty classes. But passing an extra parameter to a function is nearly always "non-observable to the callee" - only case where that is not true is when the arguments are passed in right to left order or where the callee is responsible for cleaning up arguments from the stack. – Mats Petersson Apr 24 '13 at 12:25