Debian Codegraph @

pypy/interpreter/astcompiler/test/test_assemble.py

raw symbols SCIP
1 from hypothesis import given, settings
2 import hypothesis.strategies as st
3
4 from pypy.interpreter.astcompiler.assemble import (
5 Instruction, _remove_redundant_nops, Block, _encode_varint,
6 _apply_static_swaps, _SETUP_FINALLY)
7 from pypy.interpreter.pycode import _decode_varint
8 from pypy.tool import stdlib_opcode as ops
9
10
11 # unit tests for remove_redundant_nops
12
13 def create(*args):
14 assert len(args) % 2 == 0
15 res = []
16 for i in range(0, len(args), 2):
17 res.append(Instruction(args[i], position_info=(args[i+1], -1, -1, -1)))
18 block = Block()
19 block.instructions = res
20 block.next_block = Block()
21 return block
22
23 def check(block, *args):
24 _remove_redundant_nops(block)
25 assert len(args) % 2 == 0
26 got = []
27 for op in block.instructions:
28 got.append(op.opcode)
29 got.append(op.position_info[0])
30 assert got == list(args)
31
32
33
34 def test_remove_redundant_nops():
35 # chains of nops removed to one
36 block = create(
37 ops.NOP, 1,
38 ops.NOP, 1,
39 ops.NOP, 1,
40 ops.NOP, 1,
41 ops.NOP, 1,
42 ops.NOP, 2,
43 ops.NOP, 2,
44 ops.NOP, 2,
45 )
46 check(block, ops.NOP, 1, ops.NOP, 2)
47
48 # nops without position get removed
49 block = create(
50 ops.NOP, 1,
51 ops.NOP, 1,
52 ops.NOP, -1,
53 ops.NOP, 1,
54 ops.NOP, 1,
55 ops.NOP, -1,
56 ops.NOP, 2,
57 ops.NOP, -1,
58 ops.NOP, 2,
59 ops.NOP, -1,
60 ops.NOP, 2,
61 ops.NOP, -1,
62 )
63 check(block, ops.NOP, 1, ops.NOP, 2)
64
65 # nops with the same line as a non-nop op get removed
66 block = create(
67 ops.POP_TOP, 1,
68 ops.NOP, 1,
69 )
70 check(block, ops.POP_TOP, 1)
71 block = create(
72 ops.NOP, 1,
73 ops.POP_TOP, 1,
74 )
75 check(block, ops.POP_TOP, 1)
76
77
78 # unit tests for apply_static_swaps
79
80 def make_block(*opcodes_and_args):
81 """Build a single Block from (opcode, arg, lineno) triples."""
82 block = Block()
83 for opcode, arg, lineno in opcodes_and_args:
84 instr = Instruction(opcode, arg, position_info=(lineno, -1, -1, -1))
85 block.instructions.append(instr)
86 return block
87
88 def opcodes(block):
89 return [instr.opcode for instr in block.instructions]
90
91 def run_static_swaps(block):
92 instructions = block.instructions
93 for i in range(len(instructions)):
94 if instructions[i].opcode == ops.SWAP:
95 _apply_static_swaps(instructions, i)
96
97
98 def test_static_swaps_no_swap():
99 # no SWAP present: instructions unchanged
100 block = make_block(
101 (ops.STORE_FAST, 0, 1),
102 (ops.STORE_FAST, 1, 1),
103 )
104 run_static_swaps(block)
105 assert opcodes(block) == [ops.STORE_FAST, ops.STORE_FAST]
106
107 def test_static_swaps_swap2_two_store_fast():
108 # SWAP(2) + STORE_FAST a + STORE_FAST b -> NOP + STORE_FAST b + STORE_FAST a
109 block = make_block(
110 (ops.SWAP, 2, 1),
111 (ops.STORE_FAST, 0, 1),
112 (ops.STORE_FAST, 1, 1),
113 )
114 run_static_swaps(block)
115 instrs = block.instructions
116 assert instrs[0].opcode == ops.NOP
117 assert instrs[1].opcode == ops.STORE_FAST and instrs[1].arg == 1
118 assert instrs[2].opcode == ops.STORE_FAST and instrs[2].arg == 0
119
120 def test_static_swaps_swap2_pop_top_store_fast():
121 # SWAP(2) + POP_TOP + STORE_FAST -> NOP + STORE_FAST + POP_TOP
122 block = make_block(
123 (ops.SWAP, 2, 1),
124 (ops.POP_TOP, 0, 1),
125 (ops.STORE_FAST, 5, 1),
126 )
127 run_static_swaps(block)
128 instrs = block.instructions
129 assert instrs[0].opcode == ops.NOP
130 assert instrs[1].opcode == ops.STORE_FAST and instrs[1].arg == 5
131 assert instrs[2].opcode == ops.POP_TOP
132
133 def test_static_swaps_swap2_two_pop_top():
134 # SWAP(2) + POP_TOP + POP_TOP -> NOP + POP_TOP + POP_TOP (order unchanged, both POP_TOP)
135 block = make_block(
136 (ops.SWAP, 2, 1),
137 (ops.POP_TOP, 0, 1),
138 (ops.POP_TOP, 0, 1),
139 )
140 run_static_swaps(block)
141 instrs = block.instructions
142 assert instrs[0].opcode == ops.NOP
143 assert instrs[1].opcode == ops.POP_TOP
144 assert instrs[2].opcode == ops.POP_TOP
145
146 def test_static_swaps_swap_n_greater_than_2():
147 # SWAP(3): j=first swappable, k=third swappable; swap them
148 block = make_block(
149 (ops.SWAP, 3, 1),
150 (ops.STORE_FAST, 0, 1),
151 (ops.STORE_FAST, 1, 1),
152 (ops.STORE_FAST, 2, 1),
153 )
154 run_static_swaps(block)
155 instrs = block.instructions
156 assert instrs[0].opcode == ops.NOP
157 assert instrs[1].arg == 2 # swapped with k
158 assert instrs[2].arg == 1 # unchanged
159 assert instrs[3].arg == 0 # swapped with j
160
161 def test_static_swaps_no_followers():
162 # SWAP at end of block: j not found, no change
163 block = make_block(
164 (ops.SWAP, 2, 1),
165 )
166 run_static_swaps(block)
167 assert opcodes(block) == [ops.SWAP]
168
169 def test_static_swaps_follower_not_swappable():
170 # SWAP(2) followed by a non-swappable: no change
171 block = make_block(
172 (ops.SWAP, 2, 1),
173 (ops.LOAD_FAST, 0, 1),
174 (ops.STORE_FAST, 1, 1),
175 )
176 run_static_swaps(block)
177 assert opcodes(block) == [ops.SWAP, ops.LOAD_FAST, ops.STORE_FAST]
178
179 def test_static_swaps_not_enough_swappables():
180 # SWAP(3) but only one swappable follower: k not found, no change
181 block = make_block(
182 (ops.SWAP, 3, 1),
183 (ops.STORE_FAST, 0, 1),
184 (ops.LOAD_FAST, 1, 1),
185 (ops.STORE_FAST, 2, 1),
186 )
187 run_static_swaps(block)
188 assert opcodes(block) == [ops.SWAP, ops.STORE_FAST, ops.LOAD_FAST, ops.STORE_FAST]
189
190 def test_static_swaps_conflict_same_var():
191 # j and k both STORE_FAST to the same variable: no change
192 block = make_block(
193 (ops.SWAP, 2, 1),
194 (ops.STORE_FAST, 7, 1),
195 (ops.STORE_FAST, 7, 1),
196 )
197 run_static_swaps(block)
198 assert opcodes(block) == [ops.SWAP, ops.STORE_FAST, ops.STORE_FAST]
199
200 def test_static_swaps_conflict_intermediate_store():
201 # SWAP(3): j stores to var 0, k stores to var 2, but instructions[j+1]
202 # also stores to var 0 -> conflict, no change
203 block = make_block(
204 (ops.SWAP, 3, 1),
205 (ops.STORE_FAST, 0, 1),
206 (ops.STORE_FAST, 0, 1),
207 (ops.STORE_FAST, 2, 1),
208 )
209 run_static_swaps(block)
210 assert opcodes(block) == [ops.SWAP, ops.STORE_FAST, ops.STORE_FAST, ops.STORE_FAST]
211
212 def test_static_swaps_nop_skipped():
213 # NOP between SWAP and swappables is transparent
214 block = make_block(
215 (ops.SWAP, 2, 1),
216 (ops.NOP, 0, 1),
217 (ops.STORE_FAST, 0, 1),
218 (ops.STORE_FAST, 1, 1),
219 )
220 run_static_swaps(block)
221 instrs = block.instructions
222 assert instrs[0].opcode == ops.NOP # was SWAP
223 assert instrs[1].opcode == ops.NOP # original NOP
224 assert instrs[2].arg == 1 # swapped
225 assert instrs[3].arg == 0 # swapped
226
227 def test_static_swaps_pseudo_op_skipped():
228 # pseudo-op between SWAP and swappables is transparent
229 block = make_block(
230 (ops.SWAP, 2, 1),
231 (_SETUP_FINALLY, 0, 1),
232 (ops.STORE_FAST, 0, 1),
233 (ops.STORE_FAST, 1, 1),
234 )
235 run_static_swaps(block)
236 instrs = block.instructions
237 assert instrs[0].opcode == ops.NOP
238 assert instrs[2].arg == 1
239 assert instrs[3].arg == 0
240
241 def test_static_swaps_lineno_mismatch_stops_k_search():
242 # SWAP(3): j is on line 1, but the next candidate for k is on line 2 -> no change
243 block = make_block(
244 (ops.SWAP, 3, 1),
245 (ops.STORE_FAST, 0, 1),
246 (ops.STORE_FAST, 1, 2),
247 (ops.STORE_FAST, 2, 1),
248 )
249 run_static_swaps(block)
250 assert opcodes(block) == [ops.SWAP, ops.STORE_FAST, ops.STORE_FAST, ops.STORE_FAST]
251
252 def test_static_swaps_backwards_scan_through_swappable():
253 # When scanning backwards, a STORE_FAST before the SWAP is allowed (scan continues).
254 # STORE_FAST(a), SWAP(2), STORE_FAST(b), STORE_FAST(c):
255 # Called at i=1 (SWAP): process SWAP -> NOP, swap b/c.
256 # i decremented to 0: STORE_FAST is swappable -> continue.
257 # i decremented to -1: loop ends. Result: STORE_FAST(a), NOP, STORE_FAST(c), STORE_FAST(b).
258 block = make_block(
259 (ops.STORE_FAST, 0, 1),
260 (ops.SWAP, 2, 1),
261 (ops.STORE_FAST, 1, 1),
262 (ops.STORE_FAST, 2, 1),
263 )
264 run_static_swaps(block)
265 instrs = block.instructions
266 assert instrs[0].opcode == ops.STORE_FAST and instrs[0].arg == 0
267 assert instrs[1].opcode == ops.NOP
268 assert instrs[2].arg == 2
269 assert instrs[3].arg == 1
270
271 def test_static_swaps_backwards_scan_stops_at_non_swappable():
272 # A non-swappable, non-SWAP before the SWAP stops the backwards scan early.
273 # LOAD_FAST, SWAP(2), STORE_FAST(a), STORE_FAST(b):
274 # Called at i=1: process SWAP -> NOP, swap a/b.
275 # i=0: LOAD_FAST is not swappable -> return.
276 block = make_block(
277 (ops.LOAD_FAST, 9, 1),
278 (ops.SWAP, 2, 1),
279 (ops.STORE_FAST, 0, 1),
280 (ops.STORE_FAST, 1, 1),
281 )
282 run_static_swaps(block)
283 instrs = block.instructions
284 assert instrs[0].opcode == ops.LOAD_FAST
285 assert instrs[1].opcode == ops.NOP
286 assert instrs[2].arg == 1
287 assert instrs[3].arg == 0
288
289
290 # varint encode/decode round-trip tests
291
292 @given(st.integers(min_value=0, max_value=2**30))
293 def test_varint_roundtrip(value):
294 result = []
295 _encode_varint(result, value)
296 decoded, new_i = _decode_varint(''.join(result), 0)
297 assert decoded == value
298 assert new_i == len(result)
299
300
301 @given(st.lists(st.integers(min_value=0, max_value=2**30), min_size=1, max_size=8))
302 def test_varint_sequence_roundtrip(values):
303 # Encode a sequence of varints and decode them back in order.
304 result = []
305 for v in values:
306 _encode_varint(result, v)
307 table = ''.join(result)
308 i = 0
309 for v in values:
310 decoded, i = _decode_varint(table, i)
311 assert decoded == v
312 assert i == len(table)
313
314 @given(st.integers(min_value=0, max_value=2**30))
315 def test_varint_roundtrip_msb(value):
316 result = []
317 _encode_varint(result, value, msb=0x80)
318 decoded, new_i = _decode_varint(''.join(result), 0)
319 assert decoded == value
320 assert new_i == len(result)
321
322
323 @given(st.lists(st.integers(min_value=0, max_value=2**30), min_size=1, max_size=8))
324 def test_varint_sequence_roundtrip_msb(values):
325 # Encode a sequence of varints and decode them back in order.
326 result = []
327 for v in values:
328 _encode_varint(result, v, msb=0x80)
329 table = ''.join(result)
330 i = 0
331 for v in values:
332 decoded, i = _decode_varint(table, i)
333 assert decoded == v
334 assert i == len(table)
335
336
337 # tests that do precise checks on the shape of generated assembly
338
339 class TestInstructionDetails(object):
340 def compile_ast_to_blocks(self, src, mode="exec"):
341 from pypy.interpreter.pyparser import pyparse
342 from pypy.interpreter.astcompiler import ast, assemble, symtable, optimize, codegen
343 space = self.space
344 p = pyparse.PegParser(space)
345 info = pyparse.CompileInfo("<test>", mode)
346 ast = p.parse_source(src, info)
347 module = optimize.optimize_ast(space, ast, info)
348 symbols = symtable.SymtableBuilder(space, module, info)
349 generator = codegen.TopLevelCodeGenerator(space, module, symbols, info)
350 pycode = generator.assemble()
351 return pycode, generator._final_blocks
352
353 def extract_opnames(self, blocks):
354 res = []
355 for block in blocks:
356 for instr in block.instructions:
357 res.append(ops.opname[instr.opcode])
358 return res
359
360 def test_match_uses_accept_jump_if(self):
361 code, blocks = self.compile_ast_to_blocks("""match x:
362 case [a] if not a:
363 f()
364 case _:
365 g()
366 """)
367 opnames = self.extract_opnames(blocks)
368 assert "UNARY_NOT" not in opnames
369
370 def test_copy_emitted_in_except_as(self):
371 code, blocks = self.compile_ast_to_blocks("""
372 try:
373 x = 1
374 except ValueError as e:
375 pass
376 """)
377 opnames = self.extract_opnames(blocks)
378 assert "COPY" in opnames
379
380 def test_copy_encoding(self):
381 code, blocks = self.compile_ast_to_blocks("""
382 try:
383 x = 1
384 except ValueError as e:
385 pass
386 """)
387 co_code = bytearray(code.co_code)
388 found = False
389 for i in range(0, len(co_code), 2):
390 if co_code[i] == ops.COPY and co_code[i + 1] == 3:
391 found = True
392 break
393 assert found, "COPY 3 not found in co_code"