◐ Shell
clean mode source ↗

Use a correct and transparent definition of "code unit" in C code

We should define _Py_CODEUNIT properly without the need for type punning.

Currently _Py_CODEUNIT is define as typedef uint16_t _Py_CODEUNIT; but it really an 8 bit opcode followed a bit operand aligned to 16 bits. Which means we need to resort to type punning to access the operand and oparg individually.
E.g. https://github.com/python/cpython/blob/main/Include/cpython/code.h#L32

PEP 7 states that "Python 3.11 and newer versions use C11 without optional_features".
So let's use a union with anonymous struct to define it properly:

typedef union {
    int16_t align;
    struct {
         uint8_t opcode;
         uint8_t oparg;
    };
} _Py_CODEUNIT;

@iritkatriel thoughts?

Linked PRs