opcode_1x.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # (C) Copyright 2017, 2019-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. """CPython core set of bytecode opcodes based on version 1.5
  17. This is used in bytecode disassembly among other things. This is
  18. similar to the opcodes in Python's opcode.py library.
  19. If this file changes the other opcode files may have to be adjusted accordingly.
  20. """
  21. from xdis.opcodes.base import (
  22. binary_op,
  23. call_op,
  24. compare_op,
  25. const_op,
  26. def_op,
  27. init_opdata,
  28. jabs_op,
  29. jrel_op,
  30. local_op,
  31. name_op,
  32. store_op,
  33. ternary_op,
  34. unary_op,
  35. update_pj2,
  36. varargs_op,
  37. )
  38. from xdis.opcodes.format.basic import format_MAKE_FUNCTION_10_27, opcode_arg_fmt_base
  39. from xdis.opcodes.format.extended import opcode_extended_fmt_base
  40. from xdis.opcodes.opcode_2x import (
  41. extended_format_PRINT_ITEM,
  42. extended_format_SLICE_0,
  43. extended_format_SLICE_1,
  44. extended_format_SLICE_2,
  45. extended_format_SLICE_3,
  46. )
  47. loc = locals()
  48. init_opdata(loc, None, None)
  49. # Opcodes greater than 90 take an instruction operand or "argument"
  50. # as opcode.py likes to call it.
  51. HAVE_ARGUMENT = 90
  52. # Instruction opcodes for compiled code
  53. # Blank lines correspond to available opcodes
  54. # If the POP field is -1 and the opcode is var args operation
  55. # (hasvargs | hasnargs) operation, then
  56. # the operand holds the size.
  57. # fmt: off
  58. # OP NAME OPCODE POP PUSH
  59. #-----------------------------------------------
  60. def_op(loc, "STOP_CODE", 0, 0, 0, fallthrough=False)
  61. def_op(loc, "POP_TOP", 1)
  62. def_op(loc, "ROT_TWO", 2)
  63. def_op(loc, "ROT_THREE", 3)
  64. def_op(loc, "DUP_TOP", 4)
  65. def_op(loc, "UNARY_POSITIVE", 10)
  66. unary_op(loc, "UNARY_NEGATIVE", 11)
  67. unary_op(loc, "UNARY_NOT", 12)
  68. unary_op(loc, "UNARY_CONVERT", 13)
  69. unary_op(loc, "UNARY_INVERT", 15)
  70. binary_op(loc, "BINARY_POWER", 19)
  71. binary_op(loc, "BINARY_MULTIPLY", 20)
  72. binary_op(loc, "BINARY_DIVIDE", 21)
  73. binary_op(loc, "BINARY_MODULO", 22)
  74. binary_op(loc, "BINARY_ADD", 23)
  75. binary_op(loc, "BINARY_SUBTRACT", 24)
  76. binary_op(loc, "BINARY_SUBSCR", 25)
  77. unary_op(loc, "SLICE+0", 30)
  78. binary_op(loc, "SLICE+1", 31)
  79. binary_op(loc, "SLICE+2", 32)
  80. ternary_op(loc, "SLICE+3", 33)
  81. # OP NAME OPCODE POP PUSH
  82. #-----------------------------------------------
  83. store_op(loc, "STORE_SLICE+0", 40, 2, 0)
  84. store_op(loc, "STORE_SLICE+1", 41, 3, 0)
  85. store_op(loc, "STORE_SLICE+2", 42, 3, 0)
  86. store_op(loc, "STORE_SLICE+3", 43, 4, 0)
  87. def_op(loc, "DELETE_SLICE+0", 50, 1, 0)
  88. def_op(loc, "DELETE_SLICE+1", 51, 2, 0)
  89. def_op(loc, "DELETE_SLICE+2", 52, 2, 0)
  90. def_op(loc, "DELETE_SLICE+3", 53, 3, 0)
  91. store_op(loc, "STORE_SUBSCR", 60, 3, 0) # Implements TOS1[TOS] = TOS2.
  92. def_op(loc, "DELETE_SUBSCR", 61, 2, 0) # Implements del TOS1[TOS].
  93. binary_op(loc, "BINARY_LSHIFT", 62)
  94. binary_op(loc, "BINARY_RSHIFT", 63)
  95. binary_op(loc, "BINARY_AND", 64)
  96. binary_op(loc, "BINARY_XOR", 65)
  97. def_op(loc, "BINARY_OR", 66)
  98. def_op(loc, "PRINT_EXPR", 70, 1, 0)
  99. def_op(loc, "PRINT_ITEM", 71, 1, 0)
  100. def_op(loc, "PRINT_NEWLINE", 72, 0, 0)
  101. def_op(loc, "BREAK_LOOP", 80, 0, 0, fallthrough=False)
  102. def_op(loc, "LOAD_LOCALS", 82, 0, 1)
  103. def_op(loc, "RETURN_VALUE", 83, 1, 0, fallthrough=False)
  104. def_op(loc, "EXEC_STMT", 85, 3, 0)
  105. def_op(loc, "POP_BLOCK", 87, 0, 0)
  106. def_op(loc, "END_FINALLY", 88, 1, 0)
  107. def_op(loc, "BUILD_CLASS", 89, 3, 0)
  108. # HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
  109. # OP NAME OPCODE POP PUSH
  110. #-----------------------------------------------
  111. store_op(loc, "STORE_NAME", 90, 1, 0, is_type="name") # Operand is in name list
  112. name_op(loc, "DELETE_NAME", 91, 0, 0) # ""
  113. varargs_op(loc, "UNPACK_TUPLE", 92) # Number of tuple items
  114. def_op(loc, "UNPACK_LIST", 93) # Number of list items
  115. store_op(loc, "STORE_ATTR", 95, 2, 0, is_type="name") # Operand is in name list
  116. name_op(loc, "DELETE_ATTR", 96, 1, 0) # ""
  117. store_op(loc, "STORE_GLOBAL", 97, 1, 0, is_type="name") # ""
  118. name_op(loc, "DELETE_GLOBAL", 98, 0, 0) # ""
  119. const_op(loc, "LOAD_CONST", 100, 0, 1) # Operand is in const list
  120. loc["nullaryloadop"].add(100)
  121. name_op(loc, "LOAD_NAME", 101, 0, 1) # Operand is in name list
  122. loc["nullaryloadop"].add(101)
  123. varargs_op(loc, "BUILD_TUPLE", 102, -1, 1) # Number of tuple items
  124. varargs_op(loc, "BUILD_LIST", 103, -1, 1) # Number of list items
  125. varargs_op(loc, "BUILD_MAP", 104, -1, 1) # Always zero for now
  126. name_op(loc, "LOAD_ATTR", 105, 1, 1) # Operand is in name list
  127. compare_op(loc, "COMPARE_OP", 106, 2, 1) # Comparison operator
  128. name_op(loc, "IMPORT_NAME", 107, 2, 1) # Operand is in name list
  129. loc["nullaryloadop"].add(107)
  130. name_op(loc, "IMPORT_FROM", 108, 0, 1) # Operand is in name list
  131. jrel_op(loc, "JUMP_FORWARD", 110, 0, 0, fallthrough=False) # Number of bytes to skip
  132. jrel_op(loc, "JUMP_IF_FALSE", 111, 1, 1, True) # ""
  133. jrel_op(loc, "JUMP_IF_TRUE", 112, 1, 1, True) # ""
  134. jabs_op(loc, "JUMP_ABSOLUTE", 113, 0, 0, fallthrough=False)
  135. # Target byte offset from beginning of code
  136. def_op(loc, "FOR_LOOP", 114) # Number of bytes to skip
  137. name_op(loc, "LOAD_GLOBAL", 116, 0, 1) # Operand is in name list
  138. loc["nullaryloadop"].add(116)
  139. jrel_op(loc, "SETUP_LOOP", 120, 0, 0, conditional=True)
  140. # Distance to target address
  141. jrel_op(loc, "SETUP_EXCEPT", 121, 0, 0) # ""
  142. jrel_op(loc, "SETUP_FINALLY", 122, 0, 0) # ""
  143. local_op(loc, "LOAD_FAST", 124, 0, 1) # Local variable number
  144. loc["nullaryloadop"].add(124)
  145. store_op(loc, "STORE_FAST", 125, 1, 0, is_type="local") # Local variable number
  146. local_op(loc, "DELETE_FAST", 126) # Local variable number
  147. def_op(loc, "SET_LINENO", 127) # Current line number
  148. def_op(loc, "RAISE_VARARGS", 130, -1, 0, fallthrough=False)
  149. # Number of raise arguments (1, 2, or 3)
  150. call_op(loc, "CALL_FUNCTION", 131, -1, 1) # #args + (#kwargs << 8)
  151. def_op(loc, "MAKE_FUNCTION", 132, -1, 1) # Number of args with default values
  152. varargs_op(loc, "BUILD_SLICE", 133, -1, 1) # Number of items
  153. def_op(loc, "EXTENDED_ARG", 143)
  154. EXTENDED_ARG = 143
  155. fields2copy = """cmp_op hasjabs""".split()
  156. # fmt: on
  157. update_pj2(globals(), loc)
  158. update_arg_fmt_base1x = {
  159. **opcode_arg_fmt_base,
  160. **{
  161. "MAKE_FUNCTION": format_MAKE_FUNCTION_10_27,
  162. },
  163. }
  164. opcode_extended_fmt_base1x = {
  165. **opcode_extended_fmt_base,
  166. **{
  167. "PRINT_ITEM": extended_format_PRINT_ITEM,
  168. "SLICE+0": extended_format_SLICE_0,
  169. "SLICE+1": extended_format_SLICE_1,
  170. "SLICE+2": extended_format_SLICE_2,
  171. "SLICE+3": extended_format_SLICE_3,
  172. },
  173. }