1 #include "Python.h"
2 #include "opcode.h"
3
4 /*[clinic input]
5 module _opcode
6 [clinic start generated code]*/
7 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/
8
9 #include "clinic/_opcode.c.h"
10
11 /*[clinic input]
12
13 _opcode.stack_effect -> int
14
15 opcode: int
16 oparg: object = None
17 /
18 *
19 jump: object = None
20
21 Compute the stack effect of the opcode.
22 [clinic start generated code]*/
23
24 static int
_opcode_stack_effect_impl(PyObject * module,int opcode,PyObject * oparg,PyObject * jump)25 _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
26 PyObject *jump)
27 /*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
28 {
29 int effect;
30 int oparg_int = 0;
31 int jump_int;
32 if (HAS_ARG(opcode)) {
33 if (oparg == Py_None) {
34 PyErr_SetString(PyExc_ValueError,
35 "stack_effect: opcode requires oparg but oparg was not specified");
36 return -1;
37 }
38 oparg_int = (int)PyLong_AsLong(oparg);
39 if ((oparg_int == -1) && PyErr_Occurred())
40 return -1;
41 }
42 else if (oparg != Py_None) {
43 PyErr_SetString(PyExc_ValueError,
44 "stack_effect: opcode does not permit oparg but oparg was specified");
45 return -1;
46 }
47 if (jump == Py_None) {
48 jump_int = -1;
49 }
50 else if (jump == Py_True) {
51 jump_int = 1;
52 }
53 else if (jump == Py_False) {
54 jump_int = 0;
55 }
56 else {
57 PyErr_SetString(PyExc_ValueError,
58 "stack_effect: jump must be False, True or None");
59 return -1;
60 }
61 effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
62 if (effect == PY_INVALID_STACK_EFFECT) {
63 PyErr_SetString(PyExc_ValueError,
64 "invalid opcode or oparg");
65 return -1;
66 }
67 return effect;
68 }
69
70
71
72
73 static PyMethodDef
74 opcode_functions[] = {
75 _OPCODE_STACK_EFFECT_METHODDEF
76 {NULL, NULL, 0, NULL}
77 };
78
79
80 static struct PyModuleDef opcodemodule = {
81 PyModuleDef_HEAD_INIT,
82 "_opcode",
83 "Opcode support module.",
84 -1,
85 opcode_functions,
86 NULL,
87 NULL,
88 NULL,
89 NULL
90 };
91
92 PyMODINIT_FUNC
PyInit__opcode(void)93 PyInit__opcode(void)
94 {
95 return PyModule_Create(&opcodemodule);
96 }
97