Skip to content

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

Closed
@markshannon

Description

@markshannon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions