Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
markshannon opened this issue Dec 13, 2022 · 2 comments
Closed

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

markshannon opened this issue Dec 13, 2022 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@markshannon
Copy link
Member

markshannon commented Dec 13, 2022

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

@markshannon markshannon added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Dec 13, 2022
@iritkatriel
Copy link
Member

Looks good, would it be a drop-in replacement?

@markshannon
Copy link
Member Author

Depends on how you define "drop-in"

carljm added a commit to carljm/cpython that referenced this issue Dec 14, 2022
* main: (103 commits)
  pythongh-100248: Add missing `ssl_shutdown_timeout` parameter in `asyncio` docs (python#100249)
  Assorted minor fixes for specialization stats. (pythonGH-100219)
  pythongh-100176: venv: Remove redundant compat code for Python <= 3.2 (python#100177)
  pythonGH-100222: Redefine _Py_CODEUNIT as a union to clarify structure of code unit. (pythonGH-100223)
  pythongh-99955: undef ERROR and SUCCESS before redefining (fixes sanitizer warning) (python#100215)
  pythonGH-100206: use versionadded for the addition of sysconfig.get_default_scheme (python#100207)
  pythongh-81057: Move _Py_RefTotal to the "Ignored Globals" List (pythongh-100203)
  pythongh-81057: Move Signal-Related Globals to _PyRuntimeState (pythongh-100085)
  pythongh-81057: Move faulthandler Globals to _PyRuntimeState (pythongh-100152)
  pythongh-81057: Move tracemalloc Globals to _PyRuntimeState (pythongh-100151)
  pythonGH-100143: Improve collecting pystats for parts of runs (pythonGH-100144)
  pythongh-99955: standardize return values of functions in compiler's code-gen (python#100010)
  pythongh-79218: Define `MS_WIN64` macro for Mingw-w64 64bit on Windows (pythonGH-100137)
  Fix: typo (Indention) (pythonGH-99904)
  pythongh-96715 Remove redundant NULL check in `profile_trampoline` function (python#96716)
  pythongh-100176: remove incorrect version compatibility check from argument clinic (python#100190)
  clarify the 4300-digit limit on int-str conversion (python#100175)
  pythongh-70393: Clarify mention of "middle" scope (python#98839)
  pythongh-99688: Fix outdated tests in test_unary (python#99712)
  pythongh-100174: [Enum] Correct PowersOfThree example. (pythonGH-100178)
  ...
miss-islington pushed a commit that referenced this issue Dec 15, 2022
Typo introduced in #100223.

Automerge-Triggered-By: GH:brandtbucher
carljm added a commit to carljm/cpython that referenced this issue Dec 16, 2022
* main:
  Improve stats presentation for calls. (pythonGH-100274)
  Better stats for `LOAD_ATTR` and `STORE_ATTR` (pythonGH-100295)
  pythongh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (python#100277)
  Document that zipfile's pwd parameter is a `bytes` object (python#100209)
  pythongh-99767: mark `PyTypeObject.tp_watched` as internal use only in table (python#100271)
  Fix typo in introduction.rst (python#100266)
  pythongh-78997: AttributeError if loading fails in LibraryLoader.__getattr__
  pythonGH-100234: Set a default value for random.expovariate() (pythonGH-100235)
  Remove uninformative itertools recipe (pythonGH-100253)
  pythonGH-99767: update PyTypeObject docs for type watchers (pythonGH-99928)
  Move stats for the method cache into the `Py_STAT` machinery (pythonGH-100255)
  pythonGH-100222: fix typo _py_set_opocde -> _py_set_opcode (pythonGH-100259)
  pythonGH-100000: Cleanup and polish various watchers code (pythonGH-99998)
  pythongh-90111: Minor Cleanup for Runtime-Global Objects (pythongh-100254)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

2 participants