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

This commit is contained in:
Irit Katriel
2023-07-14 18:41:52 +01:00
committed by GitHub
parent 243fdcb40e
commit 6a70edf24c
9 changed files with 558 additions and 21 deletions

View File

@@ -1,4 +1,5 @@
#include "Python.h"
#include "compile.h"
#include "opcode.h"
#include "internal/pycore_code.h"
@@ -61,6 +62,91 @@ _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
/*[clinic input]
_opcode.is_valid -> bool
opcode: int
Return True if opcode is valid, False otherwise.
[clinic start generated code]*/
static int
_opcode_is_valid_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=b0d918ea1d073f65 input=fe23e0aa194ddae0]*/
{
return PyUnstable_OpcodeIsValid(opcode);
}
/*[clinic input]
_opcode.has_arg -> bool
opcode: int
Return True if the opcode uses its oparg, False otherwise.
[clinic start generated code]*/
static int
_opcode_has_arg_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=7a062d3b2dcc0815 input=93d878ba6361db5f]*/
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasArg(opcode);
}
/*[clinic input]
_opcode.has_const -> bool
opcode: int
Return True if the opcode accesses a constant, False otherwise.
[clinic start generated code]*/
static int
_opcode_has_const_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=c646d5027c634120 input=a6999e4cf13f9410]*/
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasConst(opcode);
}
/*[clinic input]
_opcode.has_name -> bool
opcode: int
Return True if the opcode accesses an attribute by name, False otherwise.
[clinic start generated code]*/
static int
_opcode_has_name_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=b49a83555c2fa517 input=448aa5e4bcc947ba]*/
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasName(opcode);
}
/*[clinic input]
_opcode.has_jump -> bool
opcode: int
Return True if the opcode has a jump target, False otherwise.
[clinic start generated code]*/
static int
_opcode_has_jump_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=e9c583c669f1c46a input=35f711274357a0c3]*/
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasJump(opcode);
}
/*[clinic input]
_opcode.get_specialization_stats
Return the specialization stats
@@ -80,6 +166,11 @@ _opcode_get_specialization_stats_impl(PyObject *module)
static PyMethodDef
opcode_functions[] = {
_OPCODE_STACK_EFFECT_METHODDEF
_OPCODE_IS_VALID_METHODDEF
_OPCODE_HAS_ARG_METHODDEF
_OPCODE_HAS_CONST_METHODDEF
_OPCODE_HAS_NAME_METHODDEF
_OPCODE_HAS_JUMP_METHODDEF
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
{NULL, NULL, 0, NULL}
};