blob: d8de0762e765af1ee40afa7063d07323cedc9f48 [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);
Mohamed Koubaa426f2b42020-09-07 03:48:44 -050039 if ((oparg_int == -1) && PyErr_Occurred()) {
Larry Hastings7726ac92014-01-31 22:03:12 -080040 return -1;
Mohamed Koubaa426f2b42020-09-07 03:48:44 -050041 }
Larry Hastings3a907972013-11-23 14:49:22 -080042 }
Larry Hastings7726ac92014-01-31 22:03:12 -080043 else if (oparg != Py_None) {
Larry Hastings3a907972013-11-23 14:49:22 -080044 PyErr_SetString(PyExc_ValueError,
45 "stack_effect: opcode does not permit oparg but oparg was specified");
46 return -1;
47 }
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +030048 if (jump == Py_None) {
49 jump_int = -1;
50 }
51 else if (jump == Py_True) {
52 jump_int = 1;
53 }
54 else if (jump == Py_False) {
55 jump_int = 0;
56 }
57 else {
58 PyErr_SetString(PyExc_ValueError,
59 "stack_effect: jump must be False, True or None");
60 return -1;
61 }
62 effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
Larry Hastings3a907972013-11-23 14:49:22 -080063 if (effect == PY_INVALID_STACK_EFFECT) {
64 PyErr_SetString(PyExc_ValueError,
65 "invalid opcode or oparg");
66 return -1;
67 }
68 return effect;
69}
70
Larry Hastings3a907972013-11-23 14:49:22 -080071static PyMethodDef
72opcode_functions[] = {
73 _OPCODE_STACK_EFFECT_METHODDEF
74 {NULL, NULL, 0, NULL}
75};
76
Larry Hastings3a907972013-11-23 14:49:22 -080077static struct PyModuleDef opcodemodule = {
78 PyModuleDef_HEAD_INIT,
Mohamed Koubaa426f2b42020-09-07 03:48:44 -050079 .m_name = "_opcode",
80 .m_doc = "Opcode support module.",
81 .m_size = 0,
82 .m_methods = opcode_functions
Larry Hastings3a907972013-11-23 14:49:22 -080083};
84
85PyMODINIT_FUNC
86PyInit__opcode(void)
87{
Mohamed Koubaa426f2b42020-09-07 03:48:44 -050088 return PyModuleDef_Init(&opcodemodule);
Larry Hastings3a907972013-11-23 14:49:22 -080089}