opcode.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """
  2. opcode module - potentially shared between dis and other modules which
  3. operate on bytecodes (e.g. peephole optimizers).
  4. """
  5. __all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
  6. "haslocal", "hascompare", "hasfree", "opname", "opmap",
  7. "HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"]
  8. # It's a chicken-and-egg I'm afraid:
  9. # We're imported before _opcode's made.
  10. # With exception unheeded
  11. # (stack_effect is not needed)
  12. # Both our chickens and eggs are allayed.
  13. # --Larry Hastings, 2013/11/23
  14. try:
  15. from _opcode import stack_effect
  16. __all__.append('stack_effect')
  17. except ImportError:
  18. pass
  19. cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
  20. 'is not', 'exception match', 'BAD')
  21. hasconst = []
  22. hasname = []
  23. hasjrel = []
  24. hasjabs = []
  25. haslocal = []
  26. hascompare = []
  27. hasfree = []
  28. hasnargs = [] # unused
  29. opmap = {}
  30. opname = ['<%r>' % (op,) for op in range(256)]
  31. def def_op(name, op):
  32. opname[op] = name
  33. opmap[name] = op
  34. def name_op(name, op):
  35. def_op(name, op)
  36. hasname.append(op)
  37. def jrel_op(name, op):
  38. def_op(name, op)
  39. hasjrel.append(op)
  40. def jabs_op(name, op):
  41. def_op(name, op)
  42. hasjabs.append(op)
  43. # Instruction opcodes for compiled code
  44. # Blank lines correspond to available opcodes
  45. def_op('POP_TOP', 1)
  46. def_op('ROT_TWO', 2)
  47. def_op('ROT_THREE', 3)
  48. def_op('DUP_TOP', 4)
  49. def_op('DUP_TOP_TWO', 5)
  50. def_op('ROT_FOUR', 6)
  51. def_op('NOP', 9)
  52. def_op('UNARY_POSITIVE', 10)
  53. def_op('UNARY_NEGATIVE', 11)
  54. def_op('UNARY_NOT', 12)
  55. def_op('UNARY_INVERT', 15)
  56. def_op('BINARY_MATRIX_MULTIPLY', 16)
  57. def_op('INPLACE_MATRIX_MULTIPLY', 17)
  58. def_op('BINARY_POWER', 19)
  59. def_op('BINARY_MULTIPLY', 20)
  60. def_op('BINARY_MODULO', 22)
  61. def_op('BINARY_ADD', 23)
  62. def_op('BINARY_SUBTRACT', 24)
  63. def_op('BINARY_SUBSCR', 25)
  64. def_op('BINARY_FLOOR_DIVIDE', 26)
  65. def_op('BINARY_TRUE_DIVIDE', 27)
  66. def_op('INPLACE_FLOOR_DIVIDE', 28)
  67. def_op('INPLACE_TRUE_DIVIDE', 29)
  68. def_op('GET_AITER', 50)
  69. def_op('GET_ANEXT', 51)
  70. def_op('BEFORE_ASYNC_WITH', 52)
  71. def_op('BEGIN_FINALLY', 53)
  72. def_op('END_ASYNC_FOR', 54)
  73. def_op('INPLACE_ADD', 55)
  74. def_op('INPLACE_SUBTRACT', 56)
  75. def_op('INPLACE_MULTIPLY', 57)
  76. def_op('INPLACE_MODULO', 59)
  77. def_op('STORE_SUBSCR', 60)
  78. def_op('DELETE_SUBSCR', 61)
  79. def_op('BINARY_LSHIFT', 62)
  80. def_op('BINARY_RSHIFT', 63)
  81. def_op('BINARY_AND', 64)
  82. def_op('BINARY_XOR', 65)
  83. def_op('BINARY_OR', 66)
  84. def_op('INPLACE_POWER', 67)
  85. def_op('GET_ITER', 68)
  86. def_op('GET_YIELD_FROM_ITER', 69)
  87. def_op('PRINT_EXPR', 70)
  88. def_op('LOAD_BUILD_CLASS', 71)
  89. def_op('YIELD_FROM', 72)
  90. def_op('GET_AWAITABLE', 73)
  91. def_op('INPLACE_LSHIFT', 75)
  92. def_op('INPLACE_RSHIFT', 76)
  93. def_op('INPLACE_AND', 77)
  94. def_op('INPLACE_XOR', 78)
  95. def_op('INPLACE_OR', 79)
  96. def_op('WITH_CLEANUP_START', 81)
  97. def_op('WITH_CLEANUP_FINISH', 82)
  98. def_op('RETURN_VALUE', 83)
  99. def_op('IMPORT_STAR', 84)
  100. def_op('SETUP_ANNOTATIONS', 85)
  101. def_op('YIELD_VALUE', 86)
  102. def_op('POP_BLOCK', 87)
  103. def_op('END_FINALLY', 88)
  104. def_op('POP_EXCEPT', 89)
  105. HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
  106. name_op('STORE_NAME', 90) # Index in name list
  107. name_op('DELETE_NAME', 91) # ""
  108. def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
  109. jrel_op('FOR_ITER', 93)
  110. def_op('UNPACK_EX', 94)
  111. name_op('STORE_ATTR', 95) # Index in name list
  112. name_op('DELETE_ATTR', 96) # ""
  113. name_op('STORE_GLOBAL', 97) # ""
  114. name_op('DELETE_GLOBAL', 98) # ""
  115. def_op('LOAD_CONST', 100) # Index in const list
  116. hasconst.append(100)
  117. name_op('LOAD_NAME', 101) # Index in name list
  118. def_op('BUILD_TUPLE', 102) # Number of tuple items
  119. def_op('BUILD_LIST', 103) # Number of list items
  120. def_op('BUILD_SET', 104) # Number of set items
  121. def_op('BUILD_MAP', 105) # Number of dict entries
  122. name_op('LOAD_ATTR', 106) # Index in name list
  123. def_op('COMPARE_OP', 107) # Comparison operator
  124. hascompare.append(107)
  125. name_op('IMPORT_NAME', 108) # Index in name list
  126. name_op('IMPORT_FROM', 109) # Index in name list
  127. jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
  128. jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
  129. jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
  130. jabs_op('JUMP_ABSOLUTE', 113) # ""
  131. jabs_op('POP_JUMP_IF_FALSE', 114) # ""
  132. jabs_op('POP_JUMP_IF_TRUE', 115) # ""
  133. name_op('LOAD_GLOBAL', 116) # Index in name list
  134. jrel_op('SETUP_FINALLY', 122) # Distance to target address
  135. def_op('LOAD_FAST', 124) # Local variable number
  136. haslocal.append(124)
  137. def_op('STORE_FAST', 125) # Local variable number
  138. haslocal.append(125)
  139. def_op('DELETE_FAST', 126) # Local variable number
  140. haslocal.append(126)
  141. def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
  142. def_op('CALL_FUNCTION', 131) # #args
  143. def_op('MAKE_FUNCTION', 132) # Flags
  144. def_op('BUILD_SLICE', 133) # Number of items
  145. def_op('LOAD_CLOSURE', 135)
  146. hasfree.append(135)
  147. def_op('LOAD_DEREF', 136)
  148. hasfree.append(136)
  149. def_op('STORE_DEREF', 137)
  150. hasfree.append(137)
  151. def_op('DELETE_DEREF', 138)
  152. hasfree.append(138)
  153. def_op('CALL_FUNCTION_KW', 141) # #args + #kwargs
  154. def_op('CALL_FUNCTION_EX', 142) # Flags
  155. jrel_op('SETUP_WITH', 143)
  156. def_op('LIST_APPEND', 145)
  157. def_op('SET_ADD', 146)
  158. def_op('MAP_ADD', 147)
  159. def_op('LOAD_CLASSDEREF', 148)
  160. hasfree.append(148)
  161. def_op('EXTENDED_ARG', 144)
  162. EXTENDED_ARG = 144
  163. def_op('BUILD_LIST_UNPACK', 149)
  164. def_op('BUILD_MAP_UNPACK', 150)
  165. def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
  166. def_op('BUILD_TUPLE_UNPACK', 152)
  167. def_op('BUILD_SET_UNPACK', 153)
  168. jrel_op('SETUP_ASYNC_WITH', 154)
  169. def_op('FORMAT_VALUE', 155)
  170. def_op('BUILD_CONST_KEY_MAP', 156)
  171. def_op('BUILD_STRING', 157)
  172. def_op('BUILD_TUPLE_UNPACK_WITH_CALL', 158)
  173. name_op('LOAD_METHOD', 160)
  174. def_op('CALL_METHOD', 161)
  175. jrel_op('CALL_FINALLY', 162)
  176. def_op('POP_FINALLY', 163)
  177. del def_op, name_op, jrel_op, jabs_op