opcode.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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", "hasarg", "hasconst", "hasname", "hasjrel", "hasjabs",
  6. "haslocal", "hascompare", "hasfree", "hasexc", "opname", "opmap",
  7. "HAVE_ARGUMENT", "EXTENDED_ARG"]
  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 = ('<', '<=', '==', '!=', '>', '>=')
  20. hasarg = []
  21. hasconst = []
  22. hasname = []
  23. hasjrel = []
  24. hasjabs = []
  25. haslocal = []
  26. hascompare = []
  27. hasfree = []
  28. hasexc = []
  29. ENABLE_SPECIALIZATION = True
  30. def is_pseudo(op):
  31. return op >= MIN_PSEUDO_OPCODE and op <= MAX_PSEUDO_OPCODE
  32. oplists = [hasarg, hasconst, hasname, hasjrel, hasjabs,
  33. haslocal, hascompare, hasfree, hasexc]
  34. opmap = {}
  35. ## pseudo opcodes (used in the compiler) mapped to the values
  36. ## they can become in the actual code.
  37. _pseudo_ops = {}
  38. def def_op(name, op):
  39. opmap[name] = op
  40. def name_op(name, op):
  41. def_op(name, op)
  42. hasname.append(op)
  43. def jrel_op(name, op):
  44. def_op(name, op)
  45. hasjrel.append(op)
  46. def jabs_op(name, op):
  47. def_op(name, op)
  48. hasjabs.append(op)
  49. def pseudo_op(name, op, real_ops):
  50. def_op(name, op)
  51. _pseudo_ops[name] = real_ops
  52. # add the pseudo opcode to the lists its targets are in
  53. for oplist in oplists:
  54. res = [opmap[rop] in oplist for rop in real_ops]
  55. if any(res):
  56. assert all(res)
  57. oplist.append(op)
  58. # Instruction opcodes for compiled code
  59. # Blank lines correspond to available opcodes
  60. def_op('CACHE', 0)
  61. def_op('POP_TOP', 1)
  62. def_op('PUSH_NULL', 2)
  63. def_op('INTERPRETER_EXIT', 3)
  64. def_op('END_FOR', 4)
  65. def_op('END_SEND', 5)
  66. def_op('NOP', 9)
  67. def_op('UNARY_NEGATIVE', 11)
  68. def_op('UNARY_NOT', 12)
  69. def_op('UNARY_INVERT', 15)
  70. # We reserve 17 as it is the initial value for the specializing counter
  71. # This helps us catch cases where we attempt to execute a cache.
  72. def_op('RESERVED', 17)
  73. def_op('BINARY_SUBSCR', 25)
  74. def_op('BINARY_SLICE', 26)
  75. def_op('STORE_SLICE', 27)
  76. def_op('GET_LEN', 30)
  77. def_op('MATCH_MAPPING', 31)
  78. def_op('MATCH_SEQUENCE', 32)
  79. def_op('MATCH_KEYS', 33)
  80. def_op('PUSH_EXC_INFO', 35)
  81. def_op('CHECK_EXC_MATCH', 36)
  82. def_op('CHECK_EG_MATCH', 37)
  83. def_op('WITH_EXCEPT_START', 49)
  84. def_op('GET_AITER', 50)
  85. def_op('GET_ANEXT', 51)
  86. def_op('BEFORE_ASYNC_WITH', 52)
  87. def_op('BEFORE_WITH', 53)
  88. def_op('END_ASYNC_FOR', 54)
  89. def_op('CLEANUP_THROW', 55)
  90. def_op('STORE_SUBSCR', 60)
  91. def_op('DELETE_SUBSCR', 61)
  92. def_op('GET_ITER', 68)
  93. def_op('GET_YIELD_FROM_ITER', 69)
  94. def_op('LOAD_BUILD_CLASS', 71)
  95. def_op('LOAD_ASSERTION_ERROR', 74)
  96. def_op('RETURN_GENERATOR', 75)
  97. def_op('RETURN_VALUE', 83)
  98. def_op('SETUP_ANNOTATIONS', 85)
  99. def_op('LOAD_LOCALS', 87)
  100. def_op('POP_EXCEPT', 89)
  101. HAVE_ARGUMENT = 90 # real opcodes from here have an argument:
  102. name_op('STORE_NAME', 90) # Index in name list
  103. name_op('DELETE_NAME', 91) # ""
  104. def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
  105. jrel_op('FOR_ITER', 93)
  106. def_op('UNPACK_EX', 94)
  107. name_op('STORE_ATTR', 95) # Index in name list
  108. name_op('DELETE_ATTR', 96) # ""
  109. name_op('STORE_GLOBAL', 97) # ""
  110. name_op('DELETE_GLOBAL', 98) # ""
  111. def_op('SWAP', 99)
  112. def_op('LOAD_CONST', 100) # Index in const list
  113. hasconst.append(100)
  114. name_op('LOAD_NAME', 101) # Index in name list
  115. def_op('BUILD_TUPLE', 102) # Number of tuple items
  116. def_op('BUILD_LIST', 103) # Number of list items
  117. def_op('BUILD_SET', 104) # Number of set items
  118. def_op('BUILD_MAP', 105) # Number of dict entries
  119. name_op('LOAD_ATTR', 106) # Index in name list
  120. def_op('COMPARE_OP', 107) # Comparison operator
  121. hascompare.append(107)
  122. name_op('IMPORT_NAME', 108) # Index in name list
  123. name_op('IMPORT_FROM', 109) # Index in name list
  124. jrel_op('JUMP_FORWARD', 110) # Number of words to skip
  125. jrel_op('POP_JUMP_IF_FALSE', 114)
  126. jrel_op('POP_JUMP_IF_TRUE', 115)
  127. name_op('LOAD_GLOBAL', 116) # Index in name list
  128. def_op('IS_OP', 117)
  129. def_op('CONTAINS_OP', 118)
  130. def_op('RERAISE', 119)
  131. def_op('COPY', 120)
  132. def_op('RETURN_CONST', 121)
  133. hasconst.append(121)
  134. def_op('BINARY_OP', 122)
  135. jrel_op('SEND', 123) # Number of words to skip
  136. def_op('LOAD_FAST', 124) # Local variable number, no null check
  137. haslocal.append(124)
  138. def_op('STORE_FAST', 125) # Local variable number
  139. haslocal.append(125)
  140. def_op('DELETE_FAST', 126) # Local variable number
  141. haslocal.append(126)
  142. def_op('LOAD_FAST_CHECK', 127) # Local variable number
  143. haslocal.append(127)
  144. jrel_op('POP_JUMP_IF_NOT_NONE', 128)
  145. jrel_op('POP_JUMP_IF_NONE', 129)
  146. def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
  147. def_op('GET_AWAITABLE', 131)
  148. def_op('MAKE_FUNCTION', 132) # Flags
  149. def_op('BUILD_SLICE', 133) # Number of items
  150. jrel_op('JUMP_BACKWARD_NO_INTERRUPT', 134) # Number of words to skip (backwards)
  151. def_op('MAKE_CELL', 135)
  152. hasfree.append(135)
  153. def_op('LOAD_CLOSURE', 136)
  154. hasfree.append(136)
  155. def_op('LOAD_DEREF', 137)
  156. hasfree.append(137)
  157. def_op('STORE_DEREF', 138)
  158. hasfree.append(138)
  159. def_op('DELETE_DEREF', 139)
  160. hasfree.append(139)
  161. jrel_op('JUMP_BACKWARD', 140) # Number of words to skip (backwards)
  162. name_op('LOAD_SUPER_ATTR', 141)
  163. def_op('CALL_FUNCTION_EX', 142) # Flags
  164. def_op('LOAD_FAST_AND_CLEAR', 143) # Local variable number
  165. haslocal.append(143)
  166. def_op('EXTENDED_ARG', 144)
  167. EXTENDED_ARG = 144
  168. def_op('LIST_APPEND', 145)
  169. def_op('SET_ADD', 146)
  170. def_op('MAP_ADD', 147)
  171. hasfree.append(148)
  172. def_op('COPY_FREE_VARS', 149)
  173. def_op('YIELD_VALUE', 150)
  174. def_op('RESUME', 151) # This must be kept in sync with deepfreeze.py
  175. def_op('MATCH_CLASS', 152)
  176. def_op('FORMAT_VALUE', 155)
  177. def_op('BUILD_CONST_KEY_MAP', 156)
  178. def_op('BUILD_STRING', 157)
  179. def_op('LIST_EXTEND', 162)
  180. def_op('SET_UPDATE', 163)
  181. def_op('DICT_MERGE', 164)
  182. def_op('DICT_UPDATE', 165)
  183. def_op('CALL', 171)
  184. def_op('KW_NAMES', 172)
  185. hasconst.append(172)
  186. def_op('CALL_INTRINSIC_1', 173)
  187. def_op('CALL_INTRINSIC_2', 174)
  188. name_op('LOAD_FROM_DICT_OR_GLOBALS', 175)
  189. def_op('LOAD_FROM_DICT_OR_DEREF', 176)
  190. hasfree.append(176)
  191. # Instrumented instructions
  192. MIN_INSTRUMENTED_OPCODE = 237
  193. def_op('INSTRUMENTED_LOAD_SUPER_ATTR', 237)
  194. def_op('INSTRUMENTED_POP_JUMP_IF_NONE', 238)
  195. def_op('INSTRUMENTED_POP_JUMP_IF_NOT_NONE', 239)
  196. def_op('INSTRUMENTED_RESUME', 240)
  197. def_op('INSTRUMENTED_CALL', 241)
  198. def_op('INSTRUMENTED_RETURN_VALUE', 242)
  199. def_op('INSTRUMENTED_YIELD_VALUE', 243)
  200. def_op('INSTRUMENTED_CALL_FUNCTION_EX', 244)
  201. def_op('INSTRUMENTED_JUMP_FORWARD', 245)
  202. def_op('INSTRUMENTED_JUMP_BACKWARD', 246)
  203. def_op('INSTRUMENTED_RETURN_CONST', 247)
  204. def_op('INSTRUMENTED_FOR_ITER', 248)
  205. def_op('INSTRUMENTED_POP_JUMP_IF_FALSE', 249)
  206. def_op('INSTRUMENTED_POP_JUMP_IF_TRUE', 250)
  207. def_op('INSTRUMENTED_END_FOR', 251)
  208. def_op('INSTRUMENTED_END_SEND', 252)
  209. def_op('INSTRUMENTED_INSTRUCTION', 253)
  210. def_op('INSTRUMENTED_LINE', 254)
  211. # 255 is reserved
  212. hasarg.extend([op for op in opmap.values() if op >= HAVE_ARGUMENT])
  213. MIN_PSEUDO_OPCODE = 256
  214. pseudo_op('SETUP_FINALLY', 256, ['NOP'])
  215. hasexc.append(256)
  216. pseudo_op('SETUP_CLEANUP', 257, ['NOP'])
  217. hasexc.append(257)
  218. pseudo_op('SETUP_WITH', 258, ['NOP'])
  219. hasexc.append(258)
  220. pseudo_op('POP_BLOCK', 259, ['NOP'])
  221. pseudo_op('JUMP', 260, ['JUMP_FORWARD', 'JUMP_BACKWARD'])
  222. pseudo_op('JUMP_NO_INTERRUPT', 261, ['JUMP_FORWARD', 'JUMP_BACKWARD_NO_INTERRUPT'])
  223. pseudo_op('LOAD_METHOD', 262, ['LOAD_ATTR'])
  224. pseudo_op('LOAD_SUPER_METHOD', 263, ['LOAD_SUPER_ATTR'])
  225. pseudo_op('LOAD_ZERO_SUPER_METHOD', 264, ['LOAD_SUPER_ATTR'])
  226. pseudo_op('LOAD_ZERO_SUPER_ATTR', 265, ['LOAD_SUPER_ATTR'])
  227. pseudo_op('STORE_FAST_MAYBE_NULL', 266, ['STORE_FAST'])
  228. MAX_PSEUDO_OPCODE = MIN_PSEUDO_OPCODE + len(_pseudo_ops) - 1
  229. del def_op, name_op, jrel_op, jabs_op, pseudo_op
  230. opname = ['<%r>' % (op,) for op in range(MAX_PSEUDO_OPCODE + 1)]
  231. for op, i in opmap.items():
  232. opname[i] = op
  233. _nb_ops = [
  234. ("NB_ADD", "+"),
  235. ("NB_AND", "&"),
  236. ("NB_FLOOR_DIVIDE", "//"),
  237. ("NB_LSHIFT", "<<"),
  238. ("NB_MATRIX_MULTIPLY", "@"),
  239. ("NB_MULTIPLY", "*"),
  240. ("NB_REMAINDER", "%"),
  241. ("NB_OR", "|"),
  242. ("NB_POWER", "**"),
  243. ("NB_RSHIFT", ">>"),
  244. ("NB_SUBTRACT", "-"),
  245. ("NB_TRUE_DIVIDE", "/"),
  246. ("NB_XOR", "^"),
  247. ("NB_INPLACE_ADD", "+="),
  248. ("NB_INPLACE_AND", "&="),
  249. ("NB_INPLACE_FLOOR_DIVIDE", "//="),
  250. ("NB_INPLACE_LSHIFT", "<<="),
  251. ("NB_INPLACE_MATRIX_MULTIPLY", "@="),
  252. ("NB_INPLACE_MULTIPLY", "*="),
  253. ("NB_INPLACE_REMAINDER", "%="),
  254. ("NB_INPLACE_OR", "|="),
  255. ("NB_INPLACE_POWER", "**="),
  256. ("NB_INPLACE_RSHIFT", ">>="),
  257. ("NB_INPLACE_SUBTRACT", "-="),
  258. ("NB_INPLACE_TRUE_DIVIDE", "/="),
  259. ("NB_INPLACE_XOR", "^="),
  260. ]
  261. _intrinsic_1_descs = [
  262. "INTRINSIC_1_INVALID",
  263. "INTRINSIC_PRINT",
  264. "INTRINSIC_IMPORT_STAR",
  265. "INTRINSIC_STOPITERATION_ERROR",
  266. "INTRINSIC_ASYNC_GEN_WRAP",
  267. "INTRINSIC_UNARY_POSITIVE",
  268. "INTRINSIC_LIST_TO_TUPLE",
  269. "INTRINSIC_TYPEVAR",
  270. "INTRINSIC_PARAMSPEC",
  271. "INTRINSIC_TYPEVARTUPLE",
  272. "INTRINSIC_SUBSCRIPT_GENERIC",
  273. "INTRINSIC_TYPEALIAS",
  274. ]
  275. _intrinsic_2_descs = [
  276. "INTRINSIC_2_INVALID",
  277. "INTRINSIC_PREP_RERAISE_STAR",
  278. "INTRINSIC_TYPEVAR_WITH_BOUND",
  279. "INTRINSIC_TYPEVAR_WITH_CONSTRAINTS",
  280. "INTRINSIC_SET_FUNCTION_TYPE_PARAMS",
  281. ]
  282. _specializations = {
  283. "BINARY_OP": [
  284. "BINARY_OP_ADD_FLOAT",
  285. "BINARY_OP_ADD_INT",
  286. "BINARY_OP_ADD_UNICODE",
  287. "BINARY_OP_INPLACE_ADD_UNICODE",
  288. "BINARY_OP_MULTIPLY_FLOAT",
  289. "BINARY_OP_MULTIPLY_INT",
  290. "BINARY_OP_SUBTRACT_FLOAT",
  291. "BINARY_OP_SUBTRACT_INT",
  292. ],
  293. "BINARY_SUBSCR": [
  294. "BINARY_SUBSCR_DICT",
  295. "BINARY_SUBSCR_GETITEM",
  296. "BINARY_SUBSCR_LIST_INT",
  297. "BINARY_SUBSCR_TUPLE_INT",
  298. ],
  299. "CALL": [
  300. "CALL_PY_EXACT_ARGS",
  301. "CALL_PY_WITH_DEFAULTS",
  302. "CALL_BOUND_METHOD_EXACT_ARGS",
  303. "CALL_BUILTIN_CLASS",
  304. "CALL_BUILTIN_FAST_WITH_KEYWORDS",
  305. "CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS",
  306. "CALL_NO_KW_BUILTIN_FAST",
  307. "CALL_NO_KW_BUILTIN_O",
  308. "CALL_NO_KW_ISINSTANCE",
  309. "CALL_NO_KW_LEN",
  310. "CALL_NO_KW_LIST_APPEND",
  311. "CALL_NO_KW_METHOD_DESCRIPTOR_FAST",
  312. "CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS",
  313. "CALL_NO_KW_METHOD_DESCRIPTOR_O",
  314. "CALL_NO_KW_STR_1",
  315. "CALL_NO_KW_TUPLE_1",
  316. "CALL_NO_KW_TYPE_1",
  317. ],
  318. "COMPARE_OP": [
  319. "COMPARE_OP_FLOAT",
  320. "COMPARE_OP_INT",
  321. "COMPARE_OP_STR",
  322. ],
  323. "FOR_ITER": [
  324. "FOR_ITER_LIST",
  325. "FOR_ITER_TUPLE",
  326. "FOR_ITER_RANGE",
  327. "FOR_ITER_GEN",
  328. ],
  329. "LOAD_SUPER_ATTR": [
  330. "LOAD_SUPER_ATTR_ATTR",
  331. "LOAD_SUPER_ATTR_METHOD",
  332. ],
  333. "LOAD_ATTR": [
  334. # These potentially push [NULL, bound method] onto the stack.
  335. "LOAD_ATTR_CLASS",
  336. "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN",
  337. "LOAD_ATTR_INSTANCE_VALUE",
  338. "LOAD_ATTR_MODULE",
  339. "LOAD_ATTR_PROPERTY",
  340. "LOAD_ATTR_SLOT",
  341. "LOAD_ATTR_WITH_HINT",
  342. # These will always push [unbound method, self] onto the stack.
  343. "LOAD_ATTR_METHOD_LAZY_DICT",
  344. "LOAD_ATTR_METHOD_NO_DICT",
  345. "LOAD_ATTR_METHOD_WITH_VALUES",
  346. ],
  347. "LOAD_CONST": [
  348. "LOAD_CONST__LOAD_FAST",
  349. ],
  350. "LOAD_FAST": [
  351. "LOAD_FAST__LOAD_CONST",
  352. "LOAD_FAST__LOAD_FAST",
  353. ],
  354. "LOAD_GLOBAL": [
  355. "LOAD_GLOBAL_BUILTIN",
  356. "LOAD_GLOBAL_MODULE",
  357. ],
  358. "STORE_ATTR": [
  359. "STORE_ATTR_INSTANCE_VALUE",
  360. "STORE_ATTR_SLOT",
  361. "STORE_ATTR_WITH_HINT",
  362. ],
  363. "STORE_FAST": [
  364. "STORE_FAST__LOAD_FAST",
  365. "STORE_FAST__STORE_FAST",
  366. ],
  367. "STORE_SUBSCR": [
  368. "STORE_SUBSCR_DICT",
  369. "STORE_SUBSCR_LIST_INT",
  370. ],
  371. "UNPACK_SEQUENCE": [
  372. "UNPACK_SEQUENCE_LIST",
  373. "UNPACK_SEQUENCE_TUPLE",
  374. "UNPACK_SEQUENCE_TWO_TUPLE",
  375. ],
  376. "SEND": [
  377. "SEND_GEN",
  378. ],
  379. }
  380. _specialized_instructions = [
  381. opcode for family in _specializations.values() for opcode in family
  382. ]
  383. _cache_format = {
  384. "LOAD_GLOBAL": {
  385. "counter": 1,
  386. "index": 1,
  387. "module_keys_version": 1,
  388. "builtin_keys_version": 1,
  389. },
  390. "BINARY_OP": {
  391. "counter": 1,
  392. },
  393. "UNPACK_SEQUENCE": {
  394. "counter": 1,
  395. },
  396. "COMPARE_OP": {
  397. "counter": 1,
  398. },
  399. "BINARY_SUBSCR": {
  400. "counter": 1,
  401. },
  402. "FOR_ITER": {
  403. "counter": 1,
  404. },
  405. "LOAD_SUPER_ATTR": {
  406. "counter": 1,
  407. },
  408. "LOAD_ATTR": {
  409. "counter": 1,
  410. "version": 2,
  411. "keys_version": 2,
  412. "descr": 4,
  413. },
  414. "STORE_ATTR": {
  415. "counter": 1,
  416. "version": 2,
  417. "index": 1,
  418. },
  419. "CALL": {
  420. "counter": 1,
  421. "func_version": 2,
  422. },
  423. "STORE_SUBSCR": {
  424. "counter": 1,
  425. },
  426. "SEND": {
  427. "counter": 1,
  428. },
  429. }
  430. _inline_cache_entries = [
  431. sum(_cache_format.get(opname[opcode], {}).values()) for opcode in range(256)
  432. ]