From 6997e77bdf2297375962aaf82876da4e7ecdd61a Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 14 Dec 2022 11:12:53 +0000 Subject: GH-100222: Redefine _Py_CODEUNIT as a union to clarify structure of code unit. (GH-100223) --- Include/cpython/code.h | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'Include/cpython') diff --git a/Include/cpython/code.h b/Include/cpython/code.h index fc7c5ed702..6a13ff2dfd 100644 --- a/Include/cpython/code.h +++ b/Include/cpython/code.h @@ -16,21 +16,24 @@ extern "C" { * 2**32 - 1, rather than INT_MAX. */ -typedef uint16_t _Py_CODEUNIT; - -#ifdef WORDS_BIGENDIAN -# define _Py_OPCODE(word) ((word) >> 8) -# define _Py_OPARG(word) ((word) & 255) -# define _Py_MAKECODEUNIT(opcode, oparg) (((opcode)<<8)|(oparg)) -#else -# define _Py_OPCODE(word) ((word) & 255) -# define _Py_OPARG(word) ((word) >> 8) -# define _Py_MAKECODEUNIT(opcode, oparg) ((opcode)|((oparg)<<8)) -#endif +typedef union { + uint16_t cache; + struct { + uint8_t opcode; + uint8_t oparg; + }; +} _Py_CODEUNIT; + +#define _Py_OPCODE(word) ((word).opcode) +#define _Py_OPARG(word) ((word).oparg) + +static inline void +_py_set_opocde(_Py_CODEUNIT *word, uint8_t opcode) +{ + word->opcode = opcode; +} -// Use "unsigned char" instead of "uint8_t" here to avoid illegal aliasing: -#define _Py_SET_OPCODE(word, opcode) \ - do { ((unsigned char *)&(word))[0] = (opcode); } while (0) +#define _Py_SET_OPCODE(word, opcode) _py_set_opocde(&(word), opcode) typedef struct { PyObject *_co_code; -- cgit v1.2.1