auto-sync BitCastStdArray.py: Transform to union instead (#2257)

* auto-sync BitCastStdArray.py: Transform to `union` instead

* Do `typeof` manually for MSVC
This commit is contained in:
Khairul Azhar Kasmiran 2024-01-24 11:08:25 +08:00 committed by GitHub
parent eaf6d7ab67
commit cb2b87974d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 12 deletions

View File

@ -827,12 +827,12 @@ static inline uint64_t AArch64_AM_decodeAdvSIMDModImmType12(uint8_t Imm)
#define DEFINE_isSVEMaskOfIdenticalElements(T) \
static inline bool CONCAT(AArch64_AM_isSVEMaskOfIdenticalElements, T)(int64_t Imm) \
{ \
union { \
uint64_t L; \
T A[sizeof(int64_t) / sizeof(T)]; \
} U; \
U.L = Imm; \
T *Parts = U.A; \
union { \
int64_t In; \
T Out[sizeof(int64_t) / sizeof(T)]; \
} U_Parts; \
U_Parts.In = Imm; \
T *Parts = U_Parts.Out; \
for (int i = 0; i < (sizeof(int64_t) / sizeof(T)); i++) { \
if (Parts[i] != Parts[0]) \
return false; \
@ -887,9 +887,24 @@ AArch64_AM_isSVEMoveMaskPreferredLogicalImmediate(int64_t Imm)
if (isSVECpyImm64(Imm))
return false;
int32_t *S = (int32_t *)(&(Imm)); // arr len = 2
int16_t *H = (int16_t *)(&(Imm)); // arr len = 4
int8_t *B = (int8_t *)(&(Imm)); // arr len = 8
union {
int64_t In;
int32_t Out[2];
} U_S;
U_S.In = Imm;
int32_t *S = U_S.Out;
union {
int64_t In;
int16_t Out[4];
} U_H;
U_H.In = Imm;
int16_t *H = U_H.Out;
union {
int64_t In;
int8_t Out[8];
} U_B;
U_B.In = Imm;
int8_t *B = U_B.Out;
if (CONCAT(AArch64_AM_isSVEMaskOfIdenticalElements, int32_t)(Imm) &&
isSVECpyImm32(S[0]))

View File

@ -7,7 +7,14 @@ from CppTranslator.Patches.Patch import Patch
class BitCastStdArray(Patch):
"""
Patch auto S = bit_cast<std::array<int32_t, 2>>(Imm);
to int32_t *S = ((int32_t *)(&Imm)); // Array length = 2
to union {
typeof(Imm) In;
int32_t Out[2];
} U_S;
U_S.In = Imm;
int32_t *S = U_S.Out;
MSVC doesn't support typeof so it has to be resolved manually.
"""
def __init__(self, priority: int):
@ -38,8 +45,15 @@ class BitCastStdArray(Patch):
def get_patch(self, captures: [(Node, str)], src: bytes, **kwargs) -> bytes:
arr_name: bytes = captures[1][0].text
array_type: Node = captures[3][0]
cast_target: bytes = captures[4][0].text
cast_target: bytes = captures[4][0].text.strip(b"()")
array_templ_args: bytes = array_type.named_children[0].named_children[1].named_children[1].text.strip(b"<>")
arr_type = array_templ_args.split(b",")[0]
arr_len = array_templ_args.split(b",")[1]
return arr_type + b" *" + arr_name + b" = (" + arr_type + b"*)(&" + cast_target + b"); // arr len = " + arr_len
return (
b"union {\n"
+ b" typeof(" + cast_target + b") In;\n"
+ b" " + arr_type + b" Out[" + arr_len + b"];\n"
+ b"} U_" + arr_name + b";\n"
+ b"U_" + arr_name + b".In = " + cast_target + b";\n"
+ arr_type + b" *" + arr_name + b" = U_" + arr_name + b".Out;"
)