2013-11-23 18:49:22 -04:00
|
|
|
#include "Python.h"
|
|
|
|
#include "opcode.h"
|
2021-07-29 13:26:53 -03:00
|
|
|
#include "internal/pycore_code.h"
|
2013-11-23 18:49:22 -04:00
|
|
|
|
2014-01-07 16:41:53 -04:00
|
|
|
/*[clinic input]
|
2013-11-23 18:49:22 -04:00
|
|
|
module _opcode
|
2014-01-07 16:41:53 -04:00
|
|
|
[clinic start generated code]*/
|
2014-01-28 09:00:08 -04:00
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/
|
2013-11-23 19:37:55 -04:00
|
|
|
|
2015-04-03 17:53:51 -03:00
|
|
|
#include "clinic/_opcode.c.h"
|
|
|
|
|
2014-01-07 16:41:53 -04:00
|
|
|
/*[clinic input]
|
2013-11-23 18:49:22 -04:00
|
|
|
|
|
|
|
_opcode.stack_effect -> int
|
|
|
|
|
|
|
|
opcode: int
|
2014-02-01 02:03:12 -04:00
|
|
|
oparg: object = None
|
2013-11-23 18:49:22 -04:00
|
|
|
/
|
2018-09-18 03:54:26 -03:00
|
|
|
*
|
|
|
|
jump: object = None
|
2013-11-23 18:49:22 -04:00
|
|
|
|
|
|
|
Compute the stack effect of the opcode.
|
2014-01-07 16:41:53 -04:00
|
|
|
[clinic start generated code]*/
|
2013-11-23 18:49:22 -04:00
|
|
|
|
|
|
|
static int
|
2018-09-18 03:54:26 -03:00
|
|
|
_opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
|
|
|
|
PyObject *jump)
|
|
|
|
/*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
|
2013-11-23 18:49:22 -04:00
|
|
|
{
|
|
|
|
int effect;
|
2014-02-01 02:03:12 -04:00
|
|
|
int oparg_int = 0;
|
2018-09-18 03:54:26 -03:00
|
|
|
int jump_int;
|
2014-01-07 16:41:53 -04:00
|
|
|
if (HAS_ARG(opcode)) {
|
2014-02-01 02:03:12 -04:00
|
|
|
if (oparg == Py_None) {
|
2013-11-23 18:49:22 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"stack_effect: opcode requires oparg but oparg was not specified");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-01 02:03:12 -04:00
|
|
|
oparg_int = (int)PyLong_AsLong(oparg);
|
2020-09-07 05:48:44 -03:00
|
|
|
if ((oparg_int == -1) && PyErr_Occurred()) {
|
2014-02-01 02:03:12 -04:00
|
|
|
return -1;
|
2020-09-07 05:48:44 -03:00
|
|
|
}
|
2013-11-23 18:49:22 -04:00
|
|
|
}
|
2014-02-01 02:03:12 -04:00
|
|
|
else if (oparg != Py_None) {
|
2013-11-23 18:49:22 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"stack_effect: opcode does not permit oparg but oparg was specified");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-18 03:54:26 -03:00
|
|
|
if (jump == Py_None) {
|
|
|
|
jump_int = -1;
|
|
|
|
}
|
|
|
|
else if (jump == Py_True) {
|
|
|
|
jump_int = 1;
|
|
|
|
}
|
|
|
|
else if (jump == Py_False) {
|
|
|
|
jump_int = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"stack_effect: jump must be False, True or None");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-05-07 11:19:19 -03:00
|
|
|
if (IS_ARTIFICIAL(opcode)) {
|
|
|
|
effect = PY_INVALID_STACK_EFFECT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
|
|
|
|
}
|
2013-11-23 18:49:22 -04:00
|
|
|
if (effect == PY_INVALID_STACK_EFFECT) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"invalid opcode or oparg");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:26:53 -03:00
|
|
|
/*[clinic input]
|
|
|
|
|
|
|
|
_opcode.get_specialization_stats
|
|
|
|
|
|
|
|
Return the specialization stats
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_opcode_get_specialization_stats_impl(PyObject *module)
|
|
|
|
/*[clinic end generated code: output=fcbc32fdfbec5c17 input=e1f60db68d8ce5f6]*/
|
|
|
|
{
|
2021-12-15 11:32:32 -04:00
|
|
|
#ifdef Py_STATS
|
2021-07-29 13:26:53 -03:00
|
|
|
return _Py_GetSpecializationStats();
|
|
|
|
#else
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-23 18:49:22 -04:00
|
|
|
static PyMethodDef
|
|
|
|
opcode_functions[] = {
|
|
|
|
_OPCODE_STACK_EFFECT_METHODDEF
|
2021-07-29 13:26:53 -03:00
|
|
|
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
|
2013-11-23 18:49:22 -04:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef opcodemodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2020-09-07 05:48:44 -03:00
|
|
|
.m_name = "_opcode",
|
|
|
|
.m_doc = "Opcode support module.",
|
|
|
|
.m_size = 0,
|
|
|
|
.m_methods = opcode_functions
|
2013-11-23 18:49:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__opcode(void)
|
|
|
|
{
|
2020-09-07 05:48:44 -03:00
|
|
|
return PyModuleDef_Init(&opcodemodule);
|
2013-11-23 18:49:22 -04:00
|
|
|
}
|