Skip to content

Commit 634cb61

Browse files
authored
gh-87092: refactor assemble() to a number of separate functions, which do not need the compiler struct (#102562)
1 parent ca01cae commit 634cb61

File tree

3 files changed

+318
-261
lines changed

3 files changed

+318
-261
lines changed

Lib/test/support/bytecode_helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def complete_insts_info(self, insts):
125125
assert isinstance(item, tuple)
126126
inst = list(reversed(item))
127127
opcode = dis.opmap[inst.pop()]
128-
oparg = inst.pop() if opcode in self.HAS_ARG_OR_TARGET else 0
128+
oparg = inst.pop()
129129
loc = inst + [-1] * (4 - len(inst))
130130
res.append((opcode, oparg, *loc))
131131
return res

Lib/test/test_peepholer.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -995,15 +995,19 @@ def test_conditional_jump_forward_non_const_condition(self):
995995
('LOAD_CONST', 2, 13),
996996
lbl,
997997
('LOAD_CONST', 3, 14),
998+
('RETURN_VALUE', 14),
998999
]
999-
expected = [
1000+
expected_insts = [
10001001
('LOAD_NAME', 1, 11),
10011002
('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
1002-
('LOAD_CONST', 2, 13),
1003+
('LOAD_CONST', 1, 13),
10031004
lbl,
1004-
('LOAD_CONST', 3, 14)
1005+
('RETURN_CONST', 2, 14),
10051006
]
1006-
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
1007+
self.cfg_optimization_test(insts,
1008+
expected_insts,
1009+
consts=[0, 1, 2, 3, 4],
1010+
expected_consts=[0, 2, 3])
10071011

10081012
def test_conditional_jump_forward_const_condition(self):
10091013
# The unreachable branch of the jump is removed, the jump
@@ -1015,43 +1019,50 @@ def test_conditional_jump_forward_const_condition(self):
10151019
('LOAD_CONST', 2, 13),
10161020
lbl,
10171021
('LOAD_CONST', 3, 14),
1022+
('RETURN_VALUE', 14),
10181023
]
1019-
expected = [
1020-
('NOP', None, 11),
1021-
('NOP', None, 12),
1022-
('LOAD_CONST', 3, 14)
1024+
expected_insts = [
1025+
('NOP', 11),
1026+
('NOP', 12),
1027+
('RETURN_CONST', 1, 14),
10231028
]
1024-
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
1029+
self.cfg_optimization_test(insts,
1030+
expected_insts,
1031+
consts=[0, 1, 2, 3, 4],
1032+
expected_consts=[0, 3])
10251033

10261034
def test_conditional_jump_backward_non_const_condition(self):
10271035
insts = [
10281036
lbl1 := self.Label(),
10291037
('LOAD_NAME', 1, 11),
10301038
('POP_JUMP_IF_TRUE', lbl1, 12),
1031-
('LOAD_CONST', 2, 13),
1039+
('LOAD_NAME', 2, 13),
1040+
('RETURN_VALUE', 13),
10321041
]
10331042
expected = [
10341043
lbl := self.Label(),
10351044
('LOAD_NAME', 1, 11),
10361045
('POP_JUMP_IF_TRUE', lbl, 12),
1037-
('LOAD_CONST', 2, 13)
1046+
('LOAD_NAME', 2, 13),
1047+
('RETURN_VALUE', 13),
10381048
]
10391049
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
10401050

10411051
def test_conditional_jump_backward_const_condition(self):
10421052
# The unreachable branch of the jump is removed
10431053
insts = [
10441054
lbl1 := self.Label(),
1045-
('LOAD_CONST', 1, 11),
1055+
('LOAD_CONST', 3, 11),
10461056
('POP_JUMP_IF_TRUE', lbl1, 12),
10471057
('LOAD_CONST', 2, 13),
1058+
('RETURN_VALUE', 13),
10481059
]
1049-
expected = [
1060+
expected_insts = [
10501061
lbl := self.Label(),
1051-
('NOP', None, 11),
1052-
('JUMP', lbl, 12)
1062+
('NOP', 11),
1063+
('JUMP', lbl, 12),
10531064
]
1054-
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
1065+
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
10551066

10561067

10571068
if __name__ == "__main__":

0 commit comments

Comments
 (0)