C++ on x86-64: when are structs/classes passed and returned in registers?

前端 未结 2 1795
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 17:51

Assuming the x86-64 ABI on Linux, under what conditions in C++ are structs passed to functions in registers vs. on the stack? Under what conditions are they returned in regi

2条回答
  •  不知归路
    2020-12-03 18:03

    The x86-64 ABI is documented here with version 252 (the latest ABI as of my answer) downloadable here.

    If I have read page 21 et seq correctly, it says that if sizeof(struct) is 8 bytes or less, then it will be passed in a normal register. The rules get complicated after that, but I think if it 9-16 bytes, it may get passed in SSE registers.

    As to classes, remember the only difference between a class and a struct is default access. However the rules do clearly say that if there is a non-trivial copy constructor or non-trivial destructor, the struct will be passed as a hidden reference.

提交回复
热议问题