opcode_35.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # (C) Copyright 2016-2017, 2020-2021, 2023 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.5 bytecode opcodes
  18. This is like Python 3.5's opcode.py with some classification
  19. of stack usage and information for formatting instructions.
  20. of stack usage.
  21. """
  22. from typing import Optional, Tuple
  23. import xdis.opcodes.opcode_34 as opcode_34
  24. from xdis.opcodes.base import (
  25. def_op,
  26. finalize_opcodes,
  27. init_opdata,
  28. jrel_op,
  29. rm_op,
  30. update_pj3,
  31. varargs_op,
  32. )
  33. from xdis.opcodes.format.extended import extended_format_binary_op, get_arglist
  34. from xdis.opcodes.opcode_34 import opcode_arg_fmt34, opcode_extended_fmt34
  35. version_tuple = (3, 5)
  36. python_implementation = "CPython"
  37. loc = locals()
  38. init_opdata(loc, opcode_34, version_tuple)
  39. # fmt: off
  40. # These are removed since Python 3.5.
  41. # Removals happen before adds since
  42. # some opcodes are reused
  43. rm_op(loc, "STORE_MAP", 54)
  44. rm_op(loc, "WITH_CLEANUP", 81)
  45. # Stack effects are change from 3.4
  46. varargs_op(loc, "BUILD_MAP", 105, -1, -1) # arg is count of kwarg items
  47. # These are new since Python 3.5
  48. # OP NAME OPCODE POP PUSH
  49. #---------------------------------------------------
  50. def_op(loc, "BINARY_MATRIX_MULTIPLY", 16, 2, 1)
  51. def_op(loc, "INPLACE_MATRIX_MULTIPLY", 17, 2, 1)
  52. def_op(loc, "GET_AITER", 50, 1, 1)
  53. def_op(loc, "GET_ANEXT", 51, 0, 1)
  54. def_op(loc, "BEFORE_ASYNC_WITH", 52, 0, 1)
  55. def_op(loc, "GET_YIELD_FROM_ITER", 69, 1, 1)
  56. def_op(loc, "GET_AWAITABLE", 73, 0, 0)
  57. def_op(loc, "WITH_CLEANUP_START", 81, 0, 1)
  58. def_op(loc, "WITH_CLEANUP_FINISH", 82, 1, 0)
  59. varargs_op(loc, "BUILD_LIST_UNPACK", 149, -1, 1)
  60. varargs_op(loc, "BUILD_MAP_UNPACK", 150, -1, 1)
  61. varargs_op(loc, "BUILD_MAP_UNPACK_WITH_CALL", 151, -1, 1)
  62. varargs_op(loc, "BUILD_TUPLE_UNPACK", 152, -1, 1)
  63. varargs_op(loc, "BUILD_SET_UNPACK", 153, -1, 1)
  64. jrel_op(loc, "SETUP_ASYNC_WITH", 154, 0, 6)
  65. # fmt: on
  66. def extended_format_BINARY_MATRIX_MULTIPLY(opc, instructions) -> tuple[str, int | None]:
  67. return extended_format_binary_op(opc, instructions, "%s @ %s")
  68. def extended_format_INPLACE_MATRIX_MULTIPLY(opc, instructions) -> tuple[str, int | None]:
  69. return extended_format_binary_op(opc, instructions, "%s @= %s")
  70. def format_BUILD_MAP_UNPACK_WITH_CALL(oparg) -> str:
  71. """The lowest byte of oparg is the count of mappings, the relative
  72. position of the corresponding callable f is encoded in the second byte
  73. of oparg."""
  74. rel_func_pos, count = divmod(oparg, 256)
  75. return "%d mappings, function at %d" % (count, count + rel_func_pos)
  76. def extended_format_BUILD_MAP_35(opc, instructions: list) -> Tuple[str, Optional[int]]:
  77. arg_count = instructions[0].argval
  78. if arg_count == 0:
  79. # Note: caller generally handles this when the below isn't right.
  80. return "{}", instructions[0].offset
  81. arglist, _, i = get_arglist(instructions, 0, 2 * arg_count)
  82. if arglist is not None:
  83. assert isinstance(i, int)
  84. arg_pairs = [
  85. f"{arglist[i]}:{arglist[i+1]}" for i in range(0, len(arglist) - 1, 2)
  86. ]
  87. args_str = ", ".join(arg_pairs)
  88. return "{" + args_str + "}", instructions[i].start_offset
  89. return "", None
  90. opcode_arg_fmt = opcode_arg_fmt35 = {
  91. **opcode_arg_fmt34,
  92. **{
  93. "BUILD_MAP_UNPACK_WITH_CALL": format_BUILD_MAP_UNPACK_WITH_CALL,
  94. },
  95. }
  96. opcode_extended_fmt = opcode_extended_fmt35 = {
  97. **opcode_extended_fmt34,
  98. **{
  99. "BINARY_MATRIX_MULTIPLY": extended_format_BINARY_MATRIX_MULTIPLY,
  100. "BUILD_MAP": extended_format_BUILD_MAP_35,
  101. "INPLACE_MATRIX_MULTIPLY": extended_format_INPLACE_MATRIX_MULTIPLY,
  102. },
  103. }
  104. update_pj3(globals(), loc)
  105. finalize_opcodes(loc)