Skip to content

Commit 6709e97

Browse files
author
rocky
committed
Add 3.11+ COPY formatting
1 parent 10fec20 commit 6709e97

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

xdis/opcodes/format/basic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ def format_RAISE_VARARGS_older(argc) -> str:
5858
return "exception, parameter, traceback"
5959
return ""
6060

61+
def format_ROT_FOUR(_: int) -> str:
62+
return "TOS, TOS1, TOS2, TOS3 = TOS1, TOS2, TOS3, TOS"
63+
64+
65+
def format_ROT_THREE(_: int) -> str:
66+
return "TOS, TOS1, TOS2 = TOS1, TOS2, TOS"
67+
68+
6169
def format_ROT_TWO(_: int) -> str:
6270
# We add a space at the end as a sentinal to use in get_instruction_tos_str()
6371
return "TOS, TOS1 = TOS1, TOS"
@@ -70,5 +78,7 @@ def format_ROT_TWO(_: int) -> str:
7078
"CALL_FUNCTION_VAR_KW": format_CALL_FUNCTION_pos_name_encoded,
7179
"EXTENDED_ARG": format_extended_arg,
7280
"RAISE_VARARGS": format_RAISE_VARARGS_older,
81+
"ROT_FOUR": format_ROT_FOUR,
82+
"ROT_THREE": format_ROT_THREE,
7383
"ROT_TWO": format_ROT_TWO,
7484
}

xdis/opcodes/format/extended.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ def get_instruction_arg(inst: Instruction, argval=None) -> str:
791791
def get_instruction_tos_str(inst: Instruction) -> str:
792792
if inst.tos_str is not None:
793793
argval = inst.tos_str
794-
argval_without_push = re.match(r"^push\((.+)\) ", argval)
794+
argval_without_push = re.match(r"^(?:push|copy)\((.+)\) ", argval)
795795
if argval_without_push:
796-
# remove surrounding "push(...)" string
796+
# remove surrounding "push(...) or copy(...)" string
797797
argval = argval_without_push.group(1)
798798
else:
799799
argval = inst.argrepr

xdis/opcodes/opcode_311.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
store_op,
3737
update_pj3,
3838
)
39-
from xdis.opcodes.format.extended import extended_format_binary_op
39+
from xdis.opcodes.format.extended import (
40+
NULL_EXTENDED_OP,
41+
extended_format_binary_op,
42+
extended_format_unary_op,
43+
)
4044
from xdis.opcodes.opcode_310 import opcode_arg_fmt310, opcode_extended_fmt310
4145

4246
version_tuple = (3, 11)
@@ -245,6 +249,21 @@ def extended_format_BINARY_OP(opc, instructions) -> Tuple[str, Optional[int]]:
245249
return extended_format_binary_op(opc, instructions, f"%s {opname} %s")
246250

247251

252+
def extended_format_COPY_OP(
253+
opc, instructions: List[Instruction]
254+
) -> Tuple[str, Optional[int]]:
255+
"""Try to extract TOS value and show that surrounded in a "push() ".
256+
The trailing space at the used as a sentinal for `get_instruction_tos_str()`
257+
which tries to remove the push() part when the operand value string is needed.
258+
"""
259+
260+
# We add a space at the end as a sentinal to use in get_instruction_tos_str()
261+
if instructions[1].optype not in ["jrel", "jabs"]:
262+
return extended_format_unary_op(opc, instructions, "copy(%s) ")
263+
else:
264+
return NULL_EXTENDED_OP
265+
266+
248267
def extended_format_SWAP(
249268
opc, instructions: List[Instruction]
250269
) -> Tuple[str, Optional[int]]:
@@ -263,12 +282,13 @@ def extended_format_SWAP(
263282
i = swap_instr.argval
264283
# s = ""
265284

266-
if (i is None or not (0 < i < len(instructions))):
285+
if i is None or not (0 < i < len(instructions)):
267286
return "", None
268287

269288
# To be continued
270289
return "", None
271290

291+
272292
def format_BINARY_OP(arg: int) -> str:
273293
return _nb_ops[arg][1]
274294

@@ -294,6 +314,7 @@ def format_SWAP_OP(arg: int) -> str:
294314
**opcode_extended_fmt310,
295315
**{
296316
"BINARY_OP": extended_format_BINARY_OP,
317+
"COPY": extended_format_COPY_OP,
297318
},
298319
}
299320

0 commit comments

Comments
 (0)