blob: fee388f59676b074c6400a7fec857617ec1d3ae0 [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 Hastings2623c8c2014-02-08 22:15:29 -080021"stack_effect($module, opcode, oparg=None, /)\n"
22"--\n"
23"\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -080024"Compute the stack effect of the opcode.");
Larry Hastings3a907972013-11-23 14:49:22 -080025
26#define _OPCODE_STACK_EFFECT_METHODDEF \
27 {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__},
28
29static int
Larry Hastings7726ac92014-01-31 22:03:12 -080030_opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg);
Larry Hastings3a907972013-11-23 14:49:22 -080031
32static PyObject *
Larry Hastings44e2eaa2013-11-23 15:37:55 -080033_opcode_stack_effect(PyModuleDef *module, PyObject *args)
Larry Hastings3a907972013-11-23 14:49:22 -080034{
35 PyObject *return_value = NULL;
36 int opcode;
Larry Hastings7726ac92014-01-31 22:03:12 -080037 PyObject *oparg = Py_None;
Larry Hastings3a907972013-11-23 14:49:22 -080038 int _return_value;
39
Larry Hastings7726ac92014-01-31 22:03:12 -080040 if (!PyArg_ParseTuple(args,
41 "i|O:stack_effect",
42 &opcode, &oparg))
43 goto exit;
44 _return_value = _opcode_stack_effect_impl(module, opcode, oparg);
Larry Hastings3a907972013-11-23 14:49:22 -080045 if ((_return_value == -1) && PyErr_Occurred())
46 goto exit;
47 return_value = PyLong_FromLong((long)_return_value);
48
49exit:
50 return return_value;
51}
52
53static int
Larry Hastings7726ac92014-01-31 22:03:12 -080054_opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg)
Larry Hastings2623c8c2014-02-08 22:15:29 -080055/*[clinic end generated code: output=9e1133f8d587bc67 input=2d0a9ee53c0418f5]*/
Larry Hastings3a907972013-11-23 14:49:22 -080056{
57 int effect;
Larry Hastings7726ac92014-01-31 22:03:12 -080058 int oparg_int = 0;
Larry Hastings61272b72014-01-07 12:41:53 -080059 if (HAS_ARG(opcode)) {
Larry Hastings7726ac92014-01-31 22:03:12 -080060 if (oparg == Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080061 PyErr_SetString(PyExc_ValueError,
62 "stack_effect: opcode requires oparg but oparg was not specified");
63 return -1;
64 }
Larry Hastings7726ac92014-01-31 22:03:12 -080065 oparg_int = (int)PyLong_AsLong(oparg);
66 if ((oparg_int == -1) && PyErr_Occurred())
67 return -1;
Larry Hastings3a907972013-11-23 14:49:22 -080068 }
Larry Hastings7726ac92014-01-31 22:03:12 -080069 else if (oparg != Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080070 PyErr_SetString(PyExc_ValueError,
71 "stack_effect: opcode does not permit oparg but oparg was specified");
72 return -1;
73 }
Larry Hastings7726ac92014-01-31 22:03:12 -080074 effect = PyCompile_OpcodeStackEffect(opcode, oparg_int);
Larry Hastings3a907972013-11-23 14:49:22 -080075 if (effect == PY_INVALID_STACK_EFFECT) {
76 PyErr_SetString(PyExc_ValueError,
77 "invalid opcode or oparg");
78 return -1;
79 }
80 return effect;
81}
82
83
84
85
86static PyMethodDef
87opcode_functions[] = {
88 _OPCODE_STACK_EFFECT_METHODDEF
89 {NULL, NULL, 0, NULL}
90};
91
92
93static struct PyModuleDef opcodemodule = {
94 PyModuleDef_HEAD_INIT,
95 "_opcode",
96 "Opcode support module.",
97 -1,
98 opcode_functions,
99 NULL,
100 NULL,
101 NULL,
102 NULL
103};
104
105PyMODINIT_FUNC
106PyInit__opcode(void)
107{
108 return PyModule_Create(&opcodemodule);
109}