_opcode.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "Python.h"
  2. #include "opcode.h"
  3. #include "internal/pycore_code.h"
  4. /*[clinic input]
  5. module _opcode
  6. [clinic start generated code]*/
  7. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/
  8. #include "clinic/_opcode.c.h"
  9. /*[clinic input]
  10. _opcode.stack_effect -> int
  11. opcode: int
  12. oparg: object = None
  13. /
  14. *
  15. jump: object = None
  16. Compute the stack effect of the opcode.
  17. [clinic start generated code]*/
  18. static int
  19. _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
  20. PyObject *jump)
  21. /*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
  22. {
  23. int effect;
  24. int oparg_int = 0;
  25. int jump_int;
  26. if (HAS_ARG(opcode)) {
  27. if (oparg == Py_None) {
  28. PyErr_SetString(PyExc_ValueError,
  29. "stack_effect: opcode requires oparg but oparg was not specified");
  30. return -1;
  31. }
  32. oparg_int = (int)PyLong_AsLong(oparg);
  33. if ((oparg_int == -1) && PyErr_Occurred()) {
  34. return -1;
  35. }
  36. }
  37. else if (oparg != Py_None) {
  38. PyErr_SetString(PyExc_ValueError,
  39. "stack_effect: opcode does not permit oparg but oparg was specified");
  40. return -1;
  41. }
  42. if (jump == Py_None) {
  43. jump_int = -1;
  44. }
  45. else if (jump == Py_True) {
  46. jump_int = 1;
  47. }
  48. else if (jump == Py_False) {
  49. jump_int = 0;
  50. }
  51. else {
  52. PyErr_SetString(PyExc_ValueError,
  53. "stack_effect: jump must be False, True or None");
  54. return -1;
  55. }
  56. effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
  57. if (effect == PY_INVALID_STACK_EFFECT) {
  58. PyErr_SetString(PyExc_ValueError,
  59. "invalid opcode or oparg");
  60. return -1;
  61. }
  62. return effect;
  63. }
  64. /*[clinic input]
  65. _opcode.get_specialization_stats
  66. Return the specialization stats
  67. [clinic start generated code]*/
  68. static PyObject *
  69. _opcode_get_specialization_stats_impl(PyObject *module)
  70. /*[clinic end generated code: output=fcbc32fdfbec5c17 input=e1f60db68d8ce5f6]*/
  71. {
  72. #ifdef Py_STATS
  73. return _Py_GetSpecializationStats();
  74. #else
  75. Py_RETURN_NONE;
  76. #endif
  77. }
  78. static PyMethodDef
  79. opcode_functions[] = {
  80. _OPCODE_STACK_EFFECT_METHODDEF
  81. _OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
  82. {NULL, NULL, 0, NULL}
  83. };
  84. static PyModuleDef_Slot module_slots[] = {
  85. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  86. {0, NULL}
  87. };
  88. static struct PyModuleDef opcodemodule = {
  89. PyModuleDef_HEAD_INIT,
  90. .m_name = "_opcode",
  91. .m_doc = "Opcode support module.",
  92. .m_size = 0,
  93. .m_methods = opcode_functions,
  94. .m_slots = module_slots,
  95. };
  96. PyMODINIT_FUNC
  97. PyInit__opcode(void)
  98. {
  99. return PyModuleDef_Init(&opcodemodule);
  100. }