| 
									
										
										
										
											2003-02-27 21:27:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | opcode module - potentially shared between dis and other modules which | 
					
						
							|  |  |  | operate on bytecodes (e.g. peephole optimizers). | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 10:28:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 18:39:00 +01:00
										 |  |  | __all__ = ["cmp_op", "stack_effect", "hascompare", "opname", "opmap", | 
					
						
							|  |  |  |            "HAVE_ARGUMENT", "EXTENDED_ARG", "hasarg", "hasconst", "hasname", | 
					
						
							|  |  |  |            "hasjump", "hasjrel", "hasjabs", "hasfree", "haslocal", "hasexc"] | 
					
						
							| 
									
										
										
										
											2023-07-18 19:42:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-28 10:35:20 +00:00
										 |  |  | import builtins | 
					
						
							| 
									
										
										
										
											2023-07-18 19:42:44 +01:00
										 |  |  | import _opcode | 
					
						
							| 
									
										
										
										
											2023-07-17 10:28:33 +01:00
										 |  |  | from _opcode import stack_effect | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 16:14:50 +02:00
										 |  |  | from _opcode_metadata import (_specializations, _specialized_opmap, opmap,  # noqa: F401 | 
					
						
							|  |  |  |                               HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE)  # noqa: F401 | 
					
						
							| 
									
										
										
										
											2023-08-23 18:39:00 +01:00
										 |  |  | EXTENDED_ARG = opmap['EXTENDED_ARG'] | 
					
						
							| 
									
										
										
										
											2023-06-19 23:47:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 18:39:00 +01:00
										 |  |  | opname = ['<%r>' % (op,) for op in range(max(opmap.values()) + 1)] | 
					
						
							| 
									
										
										
										
											2025-01-15 21:02:32 +00:00
										 |  |  | for m in (opmap, _specialized_opmap): | 
					
						
							|  |  |  |     for op, i in m.items(): | 
					
						
							|  |  |  |         opname[i] = op | 
					
						
							| 
									
										
										
										
											2023-06-29 13:49:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-16 23:25:18 +01:00
										 |  |  | cmp_op = ('<', '<=', '==', '!=', '>', '>=') | 
					
						
							| 
									
										
										
										
											2022-02-28 12:56:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 18:39:00 +01:00
										 |  |  | # These lists are documented as part of the dis module's API | 
					
						
							|  |  |  | hasarg = [op for op in opmap.values() if _opcode.has_arg(op)] | 
					
						
							|  |  |  | hasconst = [op for op in opmap.values() if _opcode.has_const(op)] | 
					
						
							|  |  |  | hasname = [op for op in opmap.values() if _opcode.has_name(op)] | 
					
						
							|  |  |  | hasjump = [op for op in opmap.values() if _opcode.has_jump(op)] | 
					
						
							|  |  |  | hasjrel = hasjump  # for backward compatibility | 
					
						
							|  |  |  | hasjabs = [] | 
					
						
							|  |  |  | hasfree = [op for op in opmap.values() if _opcode.has_free(op)] | 
					
						
							|  |  |  | haslocal = [op for op in opmap.values() if _opcode.has_local(op)] | 
					
						
							|  |  |  | hasexc = [op for op in opmap.values() if _opcode.has_exc(op)] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _intrinsic_1_descs = _opcode.get_intrinsic1_descs() | 
					
						
							|  |  |  | _intrinsic_2_descs = _opcode.get_intrinsic2_descs() | 
					
						
							| 
									
										
										
										
											2024-06-20 07:07:24 -07:00
										 |  |  | _special_method_names = _opcode.get_special_method_names() | 
					
						
							| 
									
										
										
										
											2025-03-28 10:35:20 +00:00
										 |  |  | _common_constants = [builtins.AssertionError, builtins.NotImplementedError, | 
					
						
							|  |  |  |                      builtins.tuple, builtins.all, builtins.any] | 
					
						
							| 
									
										
										
										
											2023-08-23 18:39:00 +01:00
										 |  |  | _nb_ops = _opcode.get_nb_ops() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | hascompare = [opmap["COMPARE_OP"]] | 
					
						
							| 
									
										
										
										
											2021-06-10 08:46:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  | _cache_format = { | 
					
						
							|  |  |  |     "LOAD_GLOBAL": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |         "index": 1, | 
					
						
							| 
									
										
										
										
											2023-03-10 17:01:16 -08:00
										 |  |  |         "module_keys_version": 1, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  |         "builtin_keys_version": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "BINARY_OP": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							| 
									
										
										
										
											2025-01-16 15:22:13 +00:00
										 |  |  |         "descr": 4, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  |     }, | 
					
						
							|  |  |  |     "UNPACK_SEQUENCE": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "COMPARE_OP": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-03-07 03:30:11 +08:00
										 |  |  |     "CONTAINS_OP": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-06-21 06:19:26 -04:00
										 |  |  |     "FOR_ITER": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-04-25 11:45:51 -06:00
										 |  |  |     "LOAD_SUPER_ATTR": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  |     "LOAD_ATTR": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |         "version": 2, | 
					
						
							| 
									
										
										
										
											2022-06-14 18:36:22 +08:00
										 |  |  |         "keys_version": 2, | 
					
						
							|  |  |  |         "descr": 4, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  |     }, | 
					
						
							|  |  |  |     "STORE_ATTR": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |         "version": 2, | 
					
						
							|  |  |  |         "index": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "CALL": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |         "func_version": 2, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-08-16 17:11:24 +01:00
										 |  |  |     "CALL_KW": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |         "func_version": 2, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  |     "STORE_SUBSCR": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-02-13 11:24:55 +00:00
										 |  |  |     "SEND": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-06-02 11:46:18 +01:00
										 |  |  |     "JUMP_BACKWARD": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-06-29 13:49:54 -07:00
										 |  |  |     "TO_BOOL": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |         "version": 2, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-09-11 11:20:24 -07:00
										 |  |  |     "POP_JUMP_IF_TRUE": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "POP_JUMP_IF_FALSE": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "POP_JUMP_IF_NONE": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "POP_JUMP_IF_NOT_NONE": { | 
					
						
							|  |  |  |         "counter": 1, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-05-06 07:18:09 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 14:15:25 +01:00
										 |  |  | _inline_cache_entries = { | 
					
						
							|  |  |  |     name : sum(value.values()) for (name, value) in _cache_format.items() | 
					
						
							|  |  |  | } |