GH-100222: Redefine _Py_CODEUNIT as a union to clarify structure of code unit. (GH-100223)

This commit is contained in:
Mark Shannon 2022-12-14 11:12:53 +00:00 committed by GitHub
parent 985a71032b
commit 6997e77bdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 150 additions and 134 deletions

View file

@ -16,21 +16,24 @@ extern "C" {
* 2**32 - 1, rather than INT_MAX.
*/
typedef uint16_t _Py_CODEUNIT;
typedef union {
uint16_t cache;
struct {
uint8_t opcode;
uint8_t oparg;
};
} _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
#define _Py_OPCODE(word) ((word).opcode)
#define _Py_OPARG(word) ((word).oparg)
// 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)
static inline void
_py_set_opocde(_Py_CODEUNIT *word, uint8_t opcode)
{
word->opcode = opcode;
}
#define _Py_SET_OPCODE(word, opcode) _py_set_opocde(&(word), opcode)
typedef struct {
PyObject *_co_code;