cpu_core: compose offsetof from direct members for constexpr correctness
Apple Clang fails the libretro build of the static Makefile path on
macOS:
cpu_core.h:80:24: error: constexpr function never produces a constant
expression [-Winvalid-constexpr]
static constexpr u32 GPRRegisterOffset(u32 index) {
return offsetof(State, regs.r) + ...
}
note: cannot access field of null pointer
The compound member access form 'offsetof(State, regs.r)' is a GNU
extension that the C++ standard does not permit - per the standard,
the second argument to offsetof must be a direct (non-nested) member.
GCC accepts it as an extension and Apple Clang accepts it at runtime
too, but Apple Clang refuses it in a constexpr context, where the
implementation has to walk it as &((State*)nullptr)->regs.r and trips
the constexpr null-pointer check.
Compose the offset from two standard offsetof calls:
offsetof(State, regs) + offsetof(Registers, r) + sizeof(u32) * index
Both 'regs' and 'r' (the latter being the first member of the
anonymous union directly inside Registers) are direct members of
their containing types, which is what the standard allows. The GTE
case is the same shape.
Verified the result is identical at runtime: x86_64 -O3 codegen for
the recompiler call sites is unchanged - the LEA still emits the
same '0xc(,%rdx,4),%ecx' pattern, where 0xc is offsetof(State, regs)
and offsetof(Registers, r) folds to 0 (it's the first member of the
first union). Linux GCC build is bit-for-bit identical (.so still
8048480 bytes, 25 retro_* exports). The fix unblocks the macOS
build without disturbing anyone else.
This commit is contained in:
parent
945e042d73
commit
238a38719c
1 changed files with 13 additions and 2 deletions
|
|
@ -77,8 +77,19 @@ struct State
|
|||
std::array<u32, ICACHE_LINES> icache_tags = {};
|
||||
std::array<u8, ICACHE_SIZE> icache_data = {};
|
||||
|
||||
static constexpr u32 GPRRegisterOffset(u32 index) { return offsetof(State, regs.r) + (sizeof(u32) * index); }
|
||||
static constexpr u32 GTERegisterOffset(u32 index) { return offsetof(State, gte_regs.r32) + (sizeof(u32) * index); }
|
||||
// offsetof on a compound member access (offsetof(State, regs.r)) is a GNU
|
||||
// extension that GCC accepts but Apple Clang rejects in a constexpr context
|
||||
// ("cannot access field of null pointer"). The C++ standard only allows
|
||||
// direct members. Compose from two offsetof calls instead - the result is
|
||||
// identical at runtime, and Clang/GCC both fold the constants.
|
||||
static constexpr u32 GPRRegisterOffset(u32 index)
|
||||
{
|
||||
return offsetof(State, regs) + offsetof(Registers, r) + (sizeof(u32) * index);
|
||||
}
|
||||
static constexpr u32 GTERegisterOffset(u32 index)
|
||||
{
|
||||
return offsetof(State, gte_regs) + offsetof(GTE::Regs, r32) + (sizeof(u32) * index);
|
||||
}
|
||||
};
|
||||
|
||||
extern State g_state;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue