Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Modules/_opcode.c
12 views
1
#include "Python.h"
2
#include "opcode.h"
3
#include "internal/pycore_code.h"
4
5
/*[clinic input]
6
module _opcode
7
[clinic start generated code]*/
8
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/
9
10
#include "clinic/_opcode.c.h"
11
12
/*[clinic input]
13
14
_opcode.stack_effect -> int
15
16
opcode: int
17
oparg: object = None
18
/
19
*
20
jump: object = None
21
22
Compute the stack effect of the opcode.
23
[clinic start generated code]*/
24
25
static int
26
_opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
27
PyObject *jump)
28
/*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
29
{
30
int oparg_int = 0;
31
int jump_int;
32
33
if (oparg != Py_None) {
34
oparg_int = (int)PyLong_AsLong(oparg);
35
if ((oparg_int == -1) && PyErr_Occurred()) {
36
return -1;
37
}
38
}
39
40
if (jump == Py_None) {
41
jump_int = -1;
42
}
43
else if (jump == Py_True) {
44
jump_int = 1;
45
}
46
else if (jump == Py_False) {
47
jump_int = 0;
48
}
49
else {
50
PyErr_SetString(PyExc_ValueError,
51
"stack_effect: jump must be False, True or None");
52
return -1;
53
}
54
int effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
55
if (effect == PY_INVALID_STACK_EFFECT) {
56
PyErr_SetString(PyExc_ValueError, "invalid opcode or oparg");
57
return -1;
58
}
59
return effect;
60
}
61
62
/*[clinic input]
63
64
_opcode.get_specialization_stats
65
66
Return the specialization stats
67
[clinic start generated code]*/
68
69
static PyObject *
70
_opcode_get_specialization_stats_impl(PyObject *module)
71
/*[clinic end generated code: output=fcbc32fdfbec5c17 input=e1f60db68d8ce5f6]*/
72
{
73
#ifdef Py_STATS
74
return _Py_GetSpecializationStats();
75
#else
76
Py_RETURN_NONE;
77
#endif
78
}
79
80
static PyMethodDef
81
opcode_functions[] = {
82
_OPCODE_STACK_EFFECT_METHODDEF
83
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
84
{NULL, NULL, 0, NULL}
85
};
86
87
static PyModuleDef_Slot module_slots[] = {
88
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
89
{0, NULL}
90
};
91
92
static struct PyModuleDef opcodemodule = {
93
PyModuleDef_HEAD_INIT,
94
.m_name = "_opcode",
95
.m_doc = "Opcode support module.",
96
.m_size = 0,
97
.m_methods = opcode_functions,
98
.m_slots = module_slots,
99
};
100
101
PyMODINIT_FUNC
102
PyInit__opcode(void)
103
{
104
return PyModuleDef_Init(&opcodemodule);
105
}
106
107