blob: 2e8469933de3569fc945f2709557e50156341cfe [file] [log] [blame]
Larry Hastings3a907972013-11-23 14:49:22 -08001#include "Python.h"
2#include "opcode.h"
3
Larry Hastings61272b72014-01-07 12:41:53 -08004/*[clinic input]
Larry Hastings44e2eaa2013-11-23 15:37:55 -08005module _opcode
Larry Hastings61272b72014-01-07 12:41:53 -08006[clinic start generated code]*/
7/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastings3a907972013-11-23 14:49:22 -08008
Larry Hastings61272b72014-01-07 12:41:53 -08009/*[clinic input]
Larry Hastings3a907972013-11-23 14:49:22 -080010
Larry Hastings3a907972013-11-23 14:49:22 -080011_opcode.stack_effect -> int
12
13 opcode: int
14
15 [
16 oparg: int
17 ]
18 /
19
20Compute the stack effect of the opcode.
Larry Hastings61272b72014-01-07 12:41:53 -080021[clinic start generated code]*/
Larry Hastings3a907972013-11-23 14:49:22 -080022
23PyDoc_STRVAR(_opcode_stack_effect__doc__,
Larry Hastings44e2eaa2013-11-23 15:37:55 -080024"stack_effect(opcode, [oparg])\n"
25"Compute the stack effect of the opcode.");
Larry Hastings3a907972013-11-23 14:49:22 -080026
27#define _OPCODE_STACK_EFFECT_METHODDEF \
28 {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__},
29
30static int
Larry Hastings44e2eaa2013-11-23 15:37:55 -080031_opcode_stack_effect_impl(PyModuleDef *module, int opcode, int group_right_1, int oparg);
Larry Hastings3a907972013-11-23 14:49:22 -080032
33static PyObject *
Larry Hastings44e2eaa2013-11-23 15:37:55 -080034_opcode_stack_effect(PyModuleDef *module, PyObject *args)
Larry Hastings3a907972013-11-23 14:49:22 -080035{
36 PyObject *return_value = NULL;
37 int opcode;
38 int group_right_1 = 0;
39 int oparg = 0;
40 int _return_value;
41
Larry Hastings2a727912014-01-16 11:32:01 -080042 switch (PyTuple_GET_SIZE(args)) {
Larry Hastings3a907972013-11-23 14:49:22 -080043 case 1:
44 if (!PyArg_ParseTuple(args, "i:stack_effect", &opcode))
45 return NULL;
46 break;
47 case 2:
48 if (!PyArg_ParseTuple(args, "ii:stack_effect", &opcode, &oparg))
49 return NULL;
50 group_right_1 = 1;
51 break;
52 default:
53 PyErr_SetString(PyExc_TypeError, "_opcode.stack_effect requires 1 to 2 arguments");
54 return NULL;
55 }
56 _return_value = _opcode_stack_effect_impl(module, opcode, group_right_1, oparg);
57 if ((_return_value == -1) && PyErr_Occurred())
58 goto exit;
59 return_value = PyLong_FromLong((long)_return_value);
60
61exit:
62 return return_value;
63}
64
65static int
Larry Hastings44e2eaa2013-11-23 15:37:55 -080066_opcode_stack_effect_impl(PyModuleDef *module, int opcode, int group_right_1, int oparg)
Larry Hastings2a727912014-01-16 11:32:01 -080067/*[clinic end generated code: checksum=47e76ec27523da4ab192713642d32482cd743aa4]*/
Larry Hastings3a907972013-11-23 14:49:22 -080068{
69 int effect;
Larry Hastings61272b72014-01-07 12:41:53 -080070 if (HAS_ARG(opcode)) {
Larry Hastings3a907972013-11-23 14:49:22 -080071 if (!group_right_1) {
72 PyErr_SetString(PyExc_ValueError,
73 "stack_effect: opcode requires oparg but oparg was not specified");
74 return -1;
75 }
76 }
77 else if (group_right_1) {
78 PyErr_SetString(PyExc_ValueError,
79 "stack_effect: opcode does not permit oparg but oparg was specified");
80 return -1;
81 }
82 effect = PyCompile_OpcodeStackEffect(opcode, oparg);
83 if (effect == PY_INVALID_STACK_EFFECT) {
84 PyErr_SetString(PyExc_ValueError,
85 "invalid opcode or oparg");
86 return -1;
87 }
88 return effect;
89}
90
91
92
93
94static PyMethodDef
95opcode_functions[] = {
96 _OPCODE_STACK_EFFECT_METHODDEF
97 {NULL, NULL, 0, NULL}
98};
99
100
101static struct PyModuleDef opcodemodule = {
102 PyModuleDef_HEAD_INIT,
103 "_opcode",
104 "Opcode support module.",
105 -1,
106 opcode_functions,
107 NULL,
108 NULL,
109 NULL,
110 NULL
111};
112
113PyMODINIT_FUNC
114PyInit__opcode(void)
115{
116 return PyModule_Create(&opcodemodule);
117}