opcode_39.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # (C) Copyright 2019-2021, 2023-2024 by Rocky Bernstein
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. """
  17. CPython 3.9 bytecode opcodes
  18. This is like Python 3.9's opcode.py with some classification
  19. of stack usage and information for formatting instructions.
  20. """
  21. from typing import Optional, Tuple
  22. import xdis.opcodes.opcode_38 as opcode_38
  23. from xdis.opcodes.base import (
  24. binary_op,
  25. def_op,
  26. finalize_opcodes,
  27. init_opdata,
  28. jabs_op,
  29. rm_op,
  30. update_pj3,
  31. )
  32. from xdis.opcodes.format.extended import extended_format_binary_op
  33. from xdis.opcodes.opcode_38 import opcode_arg_fmt38, opcode_extended_fmt38
  34. version_tuple = (3, 9)
  35. python_implementation = "CPython"
  36. loc = locals()
  37. init_opdata(loc, opcode_38, version_tuple)
  38. # fmt: off
  39. # These are removed since 3.8...
  40. rm_op(loc, "BEGIN_FINALLY", 53)
  41. rm_op(loc, "WITH_CLEANUP_START", 81)
  42. rm_op(loc, "WITH_CLEANUP_FINISH", 82)
  43. rm_op(loc, "END_FINALLY", 88)
  44. rm_op(loc, "BUILD_LIST_UNPACK", 149)
  45. rm_op(loc, "BUILD_MAP_UNPACK", 150)
  46. rm_op(loc, "BUILD_MAP_UNPACK_WITH_CALL", 151)
  47. rm_op(loc, "BUILD_TUPLE_UNPACK", 152)
  48. rm_op(loc, "BUILD_SET_UNPACK", 153)
  49. rm_op(loc, "BUILD_TUPLE_UNPACK_WITH_CALL", 158)
  50. rm_op(loc, "CALL_FINALLY", 162)
  51. rm_op(loc, "POP_FINALLY", 163)
  52. # These are new since Python 3.9
  53. # OP NAME OPCODE POP PUSH
  54. #------------------------------------------------
  55. def_op(loc, "RERAISE", 48, 3, 0)
  56. def_op(loc, "WITH_EXCEPT_START", 49, 0, 1)
  57. def_op(loc, "LOAD_ASSERTION_ERROR", 74, 0, 1)
  58. def_op(loc, "LIST_TO_TUPLE", 82, 1, 1)
  59. binary_op(loc, "IS_OP", 117)
  60. jabs_op(loc, "JUMP_IF_NOT_EXC_MATCH", 121, 2, 0)
  61. binary_op(loc, "CONTAINS_OP", 118, 2, 1)
  62. def_op(loc, "LIST_EXTEND", 162, 2, 1)
  63. def_op(loc, "SET_UPDATE", 163, 2, 1)
  64. def_op(loc, "DICT_MERGE", 164, 2, 1)
  65. def_op(loc, "DICT_UPDATE", 165, 2, 1)
  66. # fmt: on
  67. def extended_format_CONTAINS_OP(
  68. opc, instructions
  69. ) -> Tuple[Optional[str], Optional[int]]:
  70. instr = instructions[0]
  71. return extended_format_binary_op(
  72. opc, instructions, f"%s {format_CONTAINS_OP(instr.arg)} %s"
  73. )
  74. def extended_format_IS_OP(opc, instructions) -> Tuple[Optional[str], Optional[int]]:
  75. instr = instructions[0]
  76. return extended_format_binary_op(
  77. opc, instructions, f"%s {format_IS_OP(instr.arg)} %s"
  78. )
  79. def format_CONTAINS_OP(arg) -> str:
  80. return "in" if arg == 0 else "not in"
  81. def format_IS_OP(arg) -> str:
  82. return "is" if arg == 0 else "is not"
  83. opcode_arg_fmt = opcode_arg_fmt39 = {
  84. **opcode_arg_fmt38,
  85. **{
  86. "CONTAINS_OP": format_CONTAINS_OP,
  87. "IS_OP": format_IS_OP,
  88. },
  89. }
  90. opcode_extended_fmt = opcode_extended_fmt39 = {
  91. **opcode_extended_fmt38.copy(),
  92. **{
  93. "CONTAINS_OP": extended_format_CONTAINS_OP,
  94. "IS_OP": extended_format_IS_OP,
  95. },
  96. }
  97. update_pj3(globals(), loc)
  98. finalize_opcodes(loc)