blob: 55cffe1ab59f0778997e8d0e87bc0f399e2684f3 [file] [log] [blame]
Larry Hastings3a907972013-11-23 14:49:22 -08001#include "Python.h"
2#include "opcode.h"
3
Larry Hastings44e2eaa2013-11-23 15:37:55 -08004/*[clinic]
5module _opcode
6[clinic]*/
7/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastings3a907972013-11-23 14:49:22 -08008
9/*[clinic]
10
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.
21[clinic]*/
22
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
42 switch (PyTuple_Size(args)) {
43 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)
67/*[clinic checksum: e880e62dc7b0de73403026eaf4f8074aa106358b]*/
Larry Hastings3a907972013-11-23 14:49:22 -080068{
69 int effect;
70 if (HAS_ARG(opcode)) {
71 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}