blob: 712e6eb54b4626e6ee3fdd5a07c9b7a82b2581be [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]*/
Larry Hastings581ee362014-01-28 05:00:08 -08007/*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/
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
Larry Hastings7726ac92014-01-31 22:03:12 -080014 oparg: object = None
Larry Hastings3a907972013-11-23 14:49:22 -080015 /
16
17Compute the stack effect of the opcode.
Larry Hastings61272b72014-01-07 12:41:53 -080018[clinic start generated code]*/
Larry Hastings3a907972013-11-23 14:49:22 -080019
20PyDoc_STRVAR(_opcode_stack_effect__doc__,
Larry Hastings7726ac92014-01-31 22:03:12 -080021"sig=($module, opcode, oparg=None)\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -080022"Compute the stack effect of the opcode.");
Larry Hastings3a907972013-11-23 14:49:22 -080023
24#define _OPCODE_STACK_EFFECT_METHODDEF \
25 {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__},
26
27static int
Larry Hastings7726ac92014-01-31 22:03:12 -080028_opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg);
Larry Hastings3a907972013-11-23 14:49:22 -080029
30static PyObject *
Larry Hastings44e2eaa2013-11-23 15:37:55 -080031_opcode_stack_effect(PyModuleDef *module, PyObject *args)
Larry Hastings3a907972013-11-23 14:49:22 -080032{
33 PyObject *return_value = NULL;
34 int opcode;
Larry Hastings7726ac92014-01-31 22:03:12 -080035 PyObject *oparg = Py_None;
Larry Hastings3a907972013-11-23 14:49:22 -080036 int _return_value;
37
Larry Hastings7726ac92014-01-31 22:03:12 -080038 if (!PyArg_ParseTuple(args,
39 "i|O:stack_effect",
40 &opcode, &oparg))
41 goto exit;
42 _return_value = _opcode_stack_effect_impl(module, opcode, oparg);
Larry Hastings3a907972013-11-23 14:49:22 -080043 if ((_return_value == -1) && PyErr_Occurred())
44 goto exit;
45 return_value = PyLong_FromLong((long)_return_value);
46
47exit:
48 return return_value;
49}
50
51static int
Larry Hastings7726ac92014-01-31 22:03:12 -080052_opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg)
53/*[clinic end generated code: output=4fe636f5db87c0a9 input=2d0a9ee53c0418f5]*/
Larry Hastings3a907972013-11-23 14:49:22 -080054{
55 int effect;
Larry Hastings7726ac92014-01-31 22:03:12 -080056 int oparg_int = 0;
Larry Hastings61272b72014-01-07 12:41:53 -080057 if (HAS_ARG(opcode)) {
Larry Hastings7726ac92014-01-31 22:03:12 -080058 if (oparg == Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080059 PyErr_SetString(PyExc_ValueError,
60 "stack_effect: opcode requires oparg but oparg was not specified");
61 return -1;
62 }
Larry Hastings7726ac92014-01-31 22:03:12 -080063 oparg_int = (int)PyLong_AsLong(oparg);
64 if ((oparg_int == -1) && PyErr_Occurred())
65 return -1;
Larry Hastings3a907972013-11-23 14:49:22 -080066 }
Larry Hastings7726ac92014-01-31 22:03:12 -080067 else if (oparg != Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080068 PyErr_SetString(PyExc_ValueError,
69 "stack_effect: opcode does not permit oparg but oparg was specified");
70 return -1;
71 }
Larry Hastings7726ac92014-01-31 22:03:12 -080072 effect = PyCompile_OpcodeStackEffect(opcode, oparg_int);
Larry Hastings3a907972013-11-23 14:49:22 -080073 if (effect == PY_INVALID_STACK_EFFECT) {
74 PyErr_SetString(PyExc_ValueError,
75 "invalid opcode or oparg");
76 return -1;
77 }
78 return effect;
79}
80
81
82
83
84static PyMethodDef
85opcode_functions[] = {
86 _OPCODE_STACK_EFFECT_METHODDEF
87 {NULL, NULL, 0, NULL}
88};
89
90
91static struct PyModuleDef opcodemodule = {
92 PyModuleDef_HEAD_INIT,
93 "_opcode",
94 "Opcode support module.",
95 -1,
96 opcode_functions,
97 NULL,
98 NULL,
99 NULL,
100 NULL
101};
102
103PyMODINIT_FUNC
104PyInit__opcode(void)
105{
106 return PyModule_Create(&opcodemodule);
107}