Skip to content

Commit

Permalink
pythongh-87092: refactor assemble() to a number of separate functions…
Browse files Browse the repository at this point in the history
…, which do not need the compiler struct (python#102562)
  • Loading branch information
iritkatriel authored and Fidget-Spinner committed Mar 27, 2023
1 parent 80fa4ed commit aaca52a
Show file tree
Hide file tree
Showing 3 changed files with 318 additions and 261 deletions.
2 changes: 1 addition & 1 deletion Lib/test/support/bytecode_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def complete_insts_info(self, insts):
assert isinstance(item, tuple)
inst = list(reversed(item))
opcode = dis.opmap[inst.pop()]
oparg = inst.pop() if opcode in self.HAS_ARG_OR_TARGET else 0
oparg = inst.pop()
loc = inst + [-1] * (4 - len(inst))
res.append((opcode, oparg, *loc))
return res
Expand Down
43 changes: 27 additions & 16 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,15 +995,19 @@ def test_conditional_jump_forward_non_const_condition(self):
('LOAD_CONST', 2, 13),
lbl,
('LOAD_CONST', 3, 14),
('RETURN_VALUE', 14),
]
expected = [
expected_insts = [
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
('LOAD_CONST', 2, 13),
('LOAD_CONST', 1, 13),
lbl,
('LOAD_CONST', 3, 14)
('RETURN_CONST', 2, 14),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
self.cfg_optimization_test(insts,
expected_insts,
consts=[0, 1, 2, 3, 4],
expected_consts=[0, 2, 3])

def test_conditional_jump_forward_const_condition(self):
# The unreachable branch of the jump is removed, the jump
Expand All @@ -1015,43 +1019,50 @@ def test_conditional_jump_forward_const_condition(self):
('LOAD_CONST', 2, 13),
lbl,
('LOAD_CONST', 3, 14),
('RETURN_VALUE', 14),
]
expected = [
('NOP', None, 11),
('NOP', None, 12),
('LOAD_CONST', 3, 14)
expected_insts = [
('NOP', 11),
('NOP', 12),
('RETURN_CONST', 1, 14),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
self.cfg_optimization_test(insts,
expected_insts,
consts=[0, 1, 2, 3, 4],
expected_consts=[0, 3])

def test_conditional_jump_backward_non_const_condition(self):
insts = [
lbl1 := self.Label(),
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl1, 12),
('LOAD_CONST', 2, 13),
('LOAD_NAME', 2, 13),
('RETURN_VALUE', 13),
]
expected = [
lbl := self.Label(),
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl, 12),
('LOAD_CONST', 2, 13)
('LOAD_NAME', 2, 13),
('RETURN_VALUE', 13),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))

def test_conditional_jump_backward_const_condition(self):
# The unreachable branch of the jump is removed
insts = [
lbl1 := self.Label(),
('LOAD_CONST', 1, 11),
('LOAD_CONST', 3, 11),
('POP_JUMP_IF_TRUE', lbl1, 12),
('LOAD_CONST', 2, 13),
('RETURN_VALUE', 13),
]
expected = [
expected_insts = [
lbl := self.Label(),
('NOP', None, 11),
('JUMP', lbl, 12)
('NOP', 11),
('JUMP', lbl, 12),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit aaca52a

Please sign in to comment.