8000 gh-105481: expose opcode metadata via the _opcode module by iritkatriel · Pull Request #106688 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105481: expose opcode metadata via the _opcode module #106688

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

Merged
merged 10 commits into from
Jul 14, 2023
Prev Previous commit
Next Next commit
add PyUnstable_OpcodeMetadata so Modules/ can get metadata
  • Loading branch information
iritkatriel committed Jul 13, 2023
commit b354891fac871d2bc04819abd5ef597af25284bc
1 change: 1 addition & 0 deletions Include/cpython/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ typedef struct {
#define PY_INVALID_STACK_EFFECT INT_MAX
PyAPI_FUNC(int) PyCompile_OpcodeStackEffect(int opcode, int oparg);
PyAPI_FUNC(int) PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump);
PyAPI_FUNC(struct opcode_metadata) PyUnstable_OpcodeMetadata(int opcode);
12 changes: 6 additions & 6 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "Python.h"
#include "compile.h"
#include "opcode.h"
#include "internal/pycore_code.h"
#define NEED_OPCODE_METADATA
#include "internal/pycore_opcode_metadata.h"
#undef NEED_OPCODE_METADATA

/*[clinic input]
module _opcode
Expand Down
6 changes: 6 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,12 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
return stack_effect(opcode, oparg, -1);
}

struct opcode_metadata
PyUnstable_OpcodeMetadata(int opcode)
{
return _PyOpcode_opcode_metadata[opcode];
}

static int
codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
{
Expand Down
6 changes: 3 additions & 3 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def emit_macros(cls, out: Formatter):
for name, value in flags.bitmask.items():
out.emit(
f"#define OPCODE_{name[:-len('_FLAG')]}(OP) "
f"(_PyOpcode_opcode_metadata[(OP)].flags & ({name}))")
f"(PyUnstable_OpcodeMetadata(OP).flags & ({name}))")


@dataclasses.dataclass
Expand Down Expand Up @@ -1218,7 +1218,7 @@ def write_metadata(self) -> None:
self.out.emit(
"#define IS_VALID_OPCODE(OP) \\\n"
" (((OP) >= 0) && ((OP) < OPCODE_METADATA_SIZE) && \\\n"
" (_PyOpcode_opcode_metadata[(OP)].valid_entry))")
" (PyUnstable_OpcodeMetadata(OP).valid_entry))")

self.out.emit("")
InstructionFlags.emit_macros(self.out)
Expand All @@ -1240,7 +1240,7 @@ def write_metadata(self) -> None:
self.out.emit("")

self.out.emit("#define OPCODE_METADATA_FMT(OP) "
"(_PyOpcode_opcode_metadata[(OP)].instr_format)")
"(PyUnstable_OpcodeMetadata(OP).instr_format)")
self.out.emit("#define SAME_OPCODE_METADATA(OP1, OP2) \\")
self.out.emit(" (OPCODE_METADATA_FMT(OP1) == OPCODE_METADATA_FMT(OP2))")
self.out.emit("")
Expand Down
0