blob: f9c1c0108d6af72bf7165c3076a81336d5e120cc [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
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03009#include "clinic/_opcode.c.h"
10
Larry Hastings61272b72014-01-07 12:41:53 -080011/*[clinic input]
Larry Hastings3a907972013-11-23 14:49:22 -080012
Larry Hastings3a907972013-11-23 14:49:22 -080013_opcode.stack_effect -> int
14
15 opcode: int
Larry Hastings7726ac92014-01-31 22:03:12 -080016 oparg: object = None
Larry Hastings3a907972013-11-23 14:49:22 -080017 /
18
19Compute the stack effect of the opcode.
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Larry Hastings3a907972013-11-23 14:49:22 -080021
Larry Hastings3a907972013-11-23 14:49:22 -080022static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030023_opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg)
24/*[clinic end generated code: output=ad39467fa3ad22ce input=2d0a9ee53c0418f5]*/
Larry Hastings3a907972013-11-23 14:49:22 -080025{
26 int effect;
Larry Hastings7726ac92014-01-31 22:03:12 -080027 int oparg_int = 0;
Larry Hastings61272b72014-01-07 12:41:53 -080028 if (HAS_ARG(opcode)) {
Larry Hastings7726ac92014-01-31 22:03:12 -080029 if (oparg == Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080030 PyErr_SetString(PyExc_ValueError,
31 "stack_effect: opcode requires oparg but oparg was not specified");
32 return -1;
33 }
Larry Hastings7726ac92014-01-31 22:03:12 -080034 oparg_int = (int)PyLong_AsLong(oparg);
35 if ((oparg_int == -1) && PyErr_Occurred())
36 return -1;
Larry Hastings3a907972013-11-23 14:49:22 -080037 }
Larry Hastings7726ac92014-01-31 22:03:12 -080038 else if (oparg != Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080039 PyErr_SetString(PyExc_ValueError,
40 "stack_effect: opcode does not permit oparg but oparg was specified");
41 return -1;
42 }
Larry Hastings7726ac92014-01-31 22:03:12 -080043 effect = PyCompile_OpcodeStackEffect(opcode, oparg_int);
Larry Hastings3a907972013-11-23 14:49:22 -080044 if (effect == PY_INVALID_STACK_EFFECT) {
45 PyErr_SetString(PyExc_ValueError,
46 "invalid opcode or oparg");
47 return -1;
48 }
49 return effect;
50}
51
52
53
54
55static PyMethodDef
56opcode_functions[] = {
57 _OPCODE_STACK_EFFECT_METHODDEF
58 {NULL, NULL, 0, NULL}
59};
60
61
62static struct PyModuleDef opcodemodule = {
63 PyModuleDef_HEAD_INIT,
64 "_opcode",
65 "Opcode support module.",
66 -1,
67 opcode_functions,
68 NULL,
69 NULL,
70 NULL,
71 NULL
72};
73
74PyMODINIT_FUNC
75PyInit__opcode(void)
76{
77 return PyModule_Create(&opcodemodule);
78}