blob: 42a8732694afef6e7dbfcbfc4f5d56d4ab87a933 [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 /
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +030018 *
19 jump: object = None
Larry Hastings3a907972013-11-23 14:49:22 -080020
21Compute the stack effect of the opcode.
Larry Hastings61272b72014-01-07 12:41:53 -080022[clinic start generated code]*/
Larry Hastings3a907972013-11-23 14:49:22 -080023
Larry Hastings3a907972013-11-23 14:49:22 -080024static int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +030025_opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
26 PyObject *jump)
27/*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
Larry Hastings3a907972013-11-23 14:49:22 -080028{
29 int effect;
Larry Hastings7726ac92014-01-31 22:03:12 -080030 int oparg_int = 0;
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +030031 int jump_int;
Larry Hastings61272b72014-01-07 12:41:53 -080032 if (HAS_ARG(opcode)) {
Larry Hastings7726ac92014-01-31 22:03:12 -080033 if (oparg == Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080034 PyErr_SetString(PyExc_ValueError,
35 "stack_effect: opcode requires oparg but oparg was not specified");
36 return -1;
37 }
Larry Hastings7726ac92014-01-31 22:03:12 -080038 oparg_int = (int)PyLong_AsLong(oparg);
39 if ((oparg_int == -1) && PyErr_Occurred())
40 return -1;
Larry Hastings3a907972013-11-23 14:49:22 -080041 }
Larry Hastings7726ac92014-01-31 22:03:12 -080042 else if (oparg != Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080043 PyErr_SetString(PyExc_ValueError,
44 "stack_effect: opcode does not permit oparg but oparg was specified");
45 return -1;
46 }
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +030047 if (jump == Py_None) {
48 jump_int = -1;
49 }
50 else if (jump == Py_True) {
51 jump_int = 1;
52 }
53 else if (jump == Py_False) {
54 jump_int = 0;
55 }
56 else {
57 PyErr_SetString(PyExc_ValueError,
58 "stack_effect: jump must be False, True or None");
59 return -1;
60 }
61 effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
Larry Hastings3a907972013-11-23 14:49:22 -080062 if (effect == PY_INVALID_STACK_EFFECT) {
63 PyErr_SetString(PyExc_ValueError,
64 "invalid opcode or oparg");
65 return -1;
66 }
67 return effect;
68}
69
70
71
72
73static PyMethodDef
74opcode_functions[] = {
75 _OPCODE_STACK_EFFECT_METHODDEF
76 {NULL, NULL, 0, NULL}
77};
78
79
80static struct PyModuleDef opcodemodule = {
81 PyModuleDef_HEAD_INIT,
82 "_opcode",
83 "Opcode support module.",
84 -1,
85 opcode_functions,
86 NULL,
87 NULL,
88 NULL,
89 NULL
90};
91
92PyMODINIT_FUNC
93PyInit__opcode(void)
94{
95 return PyModule_Create(&opcodemodule);
96}