opcode.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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"]
  8. cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
  9. 'is not', 'exception match', 'BAD')
  10. hasconst = []
  11. hasname = []
  12. hasjrel = []
  13. hasjabs = []
  14. haslocal = []
  15. hascompare = []
  16. hasfree = []
  17. opmap = {}
  18. opname = [''] * 256
  19. for op in range(256): opname[op] = '<%r>' % (op,)
  20. del op
  21. def def_op(name, op):
  22. opname[op] = name
  23. opmap[name] = op
  24. def name_op(name, op):
  25. def_op(name, op)
  26. hasname.append(op)
  27. def jrel_op(name, op):
  28. def_op(name, op)
  29. hasjrel.append(op)
  30. def jabs_op(name, op):
  31. def_op(name, op)
  32. hasjabs.append(op)
  33. # Instruction opcodes for compiled code
  34. # Blank lines correspond to available opcodes
  35. def_op('STOP_CODE', 0)
  36. def_op('POP_TOP', 1)
  37. def_op('ROT_TWO', 2)
  38. def_op('ROT_THREE', 3)
  39. def_op('DUP_TOP', 4)
  40. def_op('ROT_FOUR', 5)
  41. def_op('NOP', 9)
  42. def_op('UNARY_POSITIVE', 10)
  43. def_op('UNARY_NEGATIVE', 11)
  44. def_op('UNARY_NOT', 12)
  45. def_op('UNARY_CONVERT', 13)
  46. def_op('UNARY_INVERT', 15)
  47. def_op('BINARY_POWER', 19)
  48. def_op('BINARY_MULTIPLY', 20)
  49. def_op('BINARY_DIVIDE', 21)
  50. def_op('BINARY_MODULO', 22)
  51. def_op('BINARY_ADD', 23)
  52. def_op('BINARY_SUBTRACT', 24)
  53. def_op('BINARY_SUBSCR', 25)
  54. def_op('BINARY_FLOOR_DIVIDE', 26)
  55. def_op('BINARY_TRUE_DIVIDE', 27)
  56. def_op('INPLACE_FLOOR_DIVIDE', 28)
  57. def_op('INPLACE_TRUE_DIVIDE', 29)
  58. def_op('SLICE+0', 30)
  59. def_op('SLICE+1', 31)
  60. def_op('SLICE+2', 32)
  61. def_op('SLICE+3', 33)
  62. def_op('STORE_SLICE+0', 40)
  63. def_op('STORE_SLICE+1', 41)
  64. def_op('STORE_SLICE+2', 42)
  65. def_op('STORE_SLICE+3', 43)
  66. def_op('DELETE_SLICE+0', 50)
  67. def_op('DELETE_SLICE+1', 51)
  68. def_op('DELETE_SLICE+2', 52)
  69. def_op('DELETE_SLICE+3', 53)
  70. def_op('STORE_MAP', 54)
  71. def_op('INPLACE_ADD', 55)
  72. def_op('INPLACE_SUBTRACT', 56)
  73. def_op('INPLACE_MULTIPLY', 57)
  74. def_op('INPLACE_DIVIDE', 58)
  75. def_op('INPLACE_MODULO', 59)
  76. def_op('STORE_SUBSCR', 60)
  77. def_op('DELETE_SUBSCR', 61)
  78. def_op('BINARY_LSHIFT', 62)
  79. def_op('BINARY_RSHIFT', 63)
  80. def_op('BINARY_AND', 64)
  81. def_op('BINARY_XOR', 65)
  82. def_op('BINARY_OR', 66)
  83. def_op('INPLACE_POWER', 67)
  84. def_op('GET_ITER', 68)
  85. def_op('PRINT_EXPR', 70)
  86. def_op('PRINT_ITEM', 71)
  87. def_op('PRINT_NEWLINE', 72)
  88. def_op('PRINT_ITEM_TO', 73)
  89. def_op('PRINT_NEWLINE_TO', 74)
  90. def_op('INPLACE_LSHIFT', 75)
  91. def_op('INPLACE_RSHIFT', 76)
  92. def_op('INPLACE_AND', 77)
  93. def_op('INPLACE_XOR', 78)
  94. def_op('INPLACE_OR', 79)
  95. def_op('BREAK_LOOP', 80)
  96. def_op('WITH_CLEANUP', 81)
  97. def_op('LOAD_LOCALS', 82)
  98. def_op('RETURN_VALUE', 83)
  99. def_op('IMPORT_STAR', 84)
  100. def_op('EXEC_STMT', 85)
  101. def_op('YIELD_VALUE', 86)
  102. def_op('POP_BLOCK', 87)
  103. def_op('END_FINALLY', 88)
  104. def_op('BUILD_CLASS', 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('LIST_APPEND', 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('DUP_TOPX', 99) # number of items to duplicate
  116. def_op('LOAD_CONST', 100) # Index in const list
  117. hasconst.append(100)
  118. name_op('LOAD_NAME', 101) # Index in name list
  119. def_op('BUILD_TUPLE', 102) # Number of tuple items
  120. def_op('BUILD_LIST', 103) # Number of list items
  121. def_op('BUILD_SET', 104) # Number of set items
  122. def_op('BUILD_MAP', 105) # Number of dict entries (upto 255)
  123. name_op('LOAD_ATTR', 106) # Index in name list
  124. def_op('COMPARE_OP', 107) # Comparison operator
  125. hascompare.append(107)
  126. name_op('IMPORT_NAME', 108) # Index in name list
  127. name_op('IMPORT_FROM', 109) # Index in name list
  128. jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
  129. jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
  130. jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
  131. jabs_op('JUMP_ABSOLUTE', 113) # ""
  132. jabs_op('POP_JUMP_IF_FALSE', 114) # ""
  133. jabs_op('POP_JUMP_IF_TRUE', 115) # ""
  134. name_op('LOAD_GLOBAL', 116) # Index in name list
  135. jabs_op('CONTINUE_LOOP', 119) # Target address
  136. jrel_op('SETUP_LOOP', 120) # Distance to target address
  137. jrel_op('SETUP_EXCEPT', 121) # ""
  138. jrel_op('SETUP_FINALLY', 122) # ""
  139. def_op('LOAD_FAST', 124) # Local variable number
  140. haslocal.append(124)
  141. def_op('STORE_FAST', 125) # Local variable number
  142. haslocal.append(125)
  143. def_op('DELETE_FAST', 126) # Local variable number
  144. haslocal.append(126)
  145. def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
  146. def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
  147. def_op('MAKE_FUNCTION', 132) # Number of args with default values
  148. def_op('BUILD_SLICE', 133) # Number of items
  149. def_op('MAKE_CLOSURE', 134)
  150. def_op('LOAD_CLOSURE', 135)
  151. hasfree.append(135)
  152. def_op('LOAD_DEREF', 136)
  153. hasfree.append(136)
  154. def_op('STORE_DEREF', 137)
  155. hasfree.append(137)
  156. def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
  157. def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
  158. def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
  159. jrel_op('SETUP_WITH', 143)
  160. def_op('EXTENDED_ARG', 145)
  161. EXTENDED_ARG = 145
  162. def_op('SET_ADD', 146)
  163. def_op('MAP_ADD', 147)
  164. del def_op, name_op, jrel_op, jabs_op