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
don't expose opcode_metadata
  • Loading branch information
iritkatriel committed Jul 14, 2023
commit 63fabee66318a97b684b1809d12d038ecd96f3b0
7 changes: 6 additions & 1 deletion Include/cpython/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ 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);

PyAPI_FUNC(int) PyUnstable_OpcodeIsValid(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasArg(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasConst(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasName(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasJump(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.

16 changes: 10 additions & 6 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static int
_opcode_is_valid_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=b0d918ea1d073f65 input=fe23e0aa194ddae0]*/
{
return IS_VALID_OPCODE(opcode);
return PyUnstable_OpcodeIsValid(opcode);
}

/*[clinic input]
Expand All @@ -90,7 +90,8 @@ static int
_opcode_has_arg_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=7a062d3b2dcc0815 input=93d878ba6361db5f]*/
{
return IS_VALID_OPCODE(opcode) && OPCODE_HAS_ARG(opcode);
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasArg(opcode);
}

/*[clinic input]
Expand All @@ -106,7 +107,8 @@ static int
_opcode_has_const_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=c646d5027c634120 input=a6999e4cf13f9410]*/
{
return IS_VALID_OPCODE(opcode) && OPCODE_HAS_CONST(opcode);
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasConst(opcode);
}

/*[clinic input]
Expand All @@ -122,7 +124,8 @@ static int
_opcode_has_name_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=b49a83555c2fa517 input=448aa5e4bcc947ba]*/
{
return IS_VALID_OPCODE(opcode) && OPCODE_HAS_NAME(opcode);
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasName(opcode);
}

/*[clinic input]
Expand All @@ -138,9 +141,10 @@ static int
_opcode_has_jump_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=e9c583c669f1c46a input=35f711274357a0c3]*/
{
return IS_VALID_OPCODE(opcode) && OPCODE_HAS_JUMP(opcode);
}
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasJump(opcode);

}

/*[clinic input]

Expand Down
30 changes: 27 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,10 +866,34 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
return stack_effect(opcode, oparg, -1);
}

struct opcode_metadata
PyUnstable_OpcodeMetadata(int opcode)
int
PyUnstable_OpcodeIsValid(int opcode)
{
return IS_VALID_OPCODE(opcode);
}

int
PyUnstable_OpcodeHasArg(int opcode)
{
return OPCODE_HAS_ARG(opcode);
}

int
PyUnstable_OpcodeHasConst(int opcode)
{
return OPCODE_HAS_CONST(opcode);
}

int
PyUnstable_OpcodeHasName(int opcode)
{
return OPCODE_HAS_NAME(opcode);
}

int
PyUnstable_OpcodeHasJump(int opcode)
{
return _PyOpcode_opcode_metadata[opcode];
return OPCODE_HAS_JUMP(opcode);
}

static int
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"(PyUnstable_OpcodeMetadata(OP).flags & ({name}))")
f"(_PyOpcode_opcode_metadata[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"
" (PyUnstable_OpcodeMetadata(OP).valid_entry))")
" (_PyOpcode_opcode_metadata[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) "
"(PyUnstable_OpcodeMetadata(OP).instr_format)")
"(_PyOpcode_opcode_metadata[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