Skip to content

Commit 8da9d1b

Browse files
authored
gh-105540: Fix code generator tests (#105707)
This involves expanding PEEK, POKE and JUMPBY macros, and removing super and register tests (those features no longer exist).
1 parent ca3cc4b commit 8da9d1b

File tree

1 file changed

+47
-128
lines changed

1 file changed

+47
-128
lines changed

Tools/cases_generator/test_generator.py

+47-128
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def run_cases_test(input: str, expected: str):
4343
temp_input.write(generate_cases.END_MARKER)
4444
temp_input.flush()
4545
temp_output = tempfile.NamedTemporaryFile("w+")
46-
a = generate_cases.Analyzer(temp_input.name, temp_output.name)
46+
temp_metadata = tempfile.NamedTemporaryFile("w+")
47+
a = generate_cases.Analyzer([temp_input.name], temp_output.name, temp_metadata.name)
4748
a.parse()
4849
a.analyze()
4950
if a.errors:
@@ -84,7 +85,7 @@ def test_inst_one_pop():
8485
"""
8586
output = """
8687
TARGET(OP) {
87-
PyObject *value = PEEK(1);
88+
PyObject *value = stack_pointer[-1];
8889
spam();
8990
STACK_SHRINK(1);
9091
DISPATCH();
@@ -103,7 +104,7 @@ def test_inst_one_push():
103104
PyObject *res;
104105
spam();
105106
STACK_GROW(1);
106-
POKE(1, res);
107+
stack_pointer[-1] = res;
107108
DISPATCH();
108109
}
109110
"""
@@ -117,10 +118,10 @@ def test_inst_one_push_one_pop():
117118
"""
118119
output = """
119120
TARGET(OP) {
120-
PyObject *value = PEEK(1);
121+
PyObject *value = stack_pointer[-1];
121122
PyObject *res;
122123
spam();
123-
POKE(1, res);
124+
stack_pointer[-1] = res;
124125
DISPATCH();
125126
}
126127
"""
@@ -134,12 +135,12 @@ def test_binary_op():
134135
"""
135136
output = """
136137
TARGET(OP) {
137-
PyObject *right = PEEK(1);
138-
PyObject *left = PEEK(2);
138+
PyObject *right = stack_pointer[-1];
139+
PyObject *left = stack_pointer[-2];
139140
PyObject *res;
140141
spam();
141142
STACK_SHRINK(1);
142-
POKE(1, res);
143+
stack_pointer[-1] = res;
143144
DISPATCH();
144145
}
145146
"""
@@ -153,11 +154,11 @@ def test_overlap():
153154
"""
154155
output = """
155156
TARGET(OP) {
156-
PyObject *right = PEEK(1);
157-
PyObject *left = PEEK(2);
157+
PyObject *right = stack_pointer[-1];
158+
PyObject *left = stack_pointer[-2];
158159
PyObject *result;
159160
spam();
160-
POKE(1, result);
161+
stack_pointer[-1] = result;
161162
DISPATCH();
162163
}
163164
"""
@@ -167,11 +168,8 @@ def test_predictions_and_eval_breaker():
167168
input = """
168169
inst(OP1, (--)) {
169170
}
170-
inst(OP2, (--)) {
171-
}
172171
inst(OP3, (arg -- res)) {
173172
DEOPT_IF(xxx, OP1);
174-
PREDICT(OP2);
175173
CHECK_EVAL_BREAKER();
176174
}
177175
"""
@@ -181,17 +179,11 @@ def test_predictions_and_eval_breaker():
181179
DISPATCH();
182180
}
183181
184-
TARGET(OP2) {
185-
PREDICTED(OP2);
186-
DISPATCH();
187-
}
188-
189182
TARGET(OP3) {
190-
PyObject *arg = PEEK(1);
183+
PyObject *arg = stack_pointer[-1];
191184
PyObject *res;
192185
DEOPT_IF(xxx, OP1);
193-
POKE(1, res);
194-
PREDICT(OP2);
186+
stack_pointer[-1] = res;
195187
CHECK_EVAL_BREAKER();
196188
DISPATCH();
197189
}
@@ -234,12 +226,12 @@ def test_error_if_pop():
234226
"""
235227
output = """
236228
TARGET(OP) {
237-
PyObject *right = PEEK(1);
238-
PyObject *left = PEEK(2);
229+
PyObject *right = stack_pointer[-1];
230+
PyObject *left = stack_pointer[-2];
239231
PyObject *res;
240232
if (cond) goto pop_2_label;
241233
STACK_SHRINK(1);
242-
POKE(1, res);
234+
stack_pointer[-1] = res;
243235
DISPATCH();
244236
}
245237
"""
@@ -252,11 +244,11 @@ def test_cache_effect():
252244
"""
253245
output = """
254246
TARGET(OP) {
255-
PyObject *value = PEEK(1);
247+
PyObject *value = stack_pointer[-1];
256248
uint16_t counter = read_u16(&next_instr[0].cache);
257249
uint32_t extra = read_u32(&next_instr[1].cache);
258250
STACK_SHRINK(1);
259-
JUMPBY(3);
251+
next_instr += 3;
260252
DISPATCH();
261253
}
262254
"""
@@ -275,59 +267,6 @@ def test_suppress_dispatch():
275267
"""
276268
run_cases_test(input, output)
277269

278-
def test_super_instruction():
279-
# TODO: Test cache effect
280-
input = """
281-
inst(OP1, (counter/1, arg --)) {
282-
op1();
283-
}
284-
inst(OP2, (extra/2, arg --)) {
285-
op2();
286-
}
287-
super(OP) = OP1 + OP2;
288-
"""
289-
output = """
290-
TARGET(OP1) {
291-
PyObject *arg = PEEK(1);
292-
uint16_t counter = read_u16(&next_instr[0].cache);
293-
op1();
294-
STACK_SHRINK(1);
295-
JUMPBY(1);
296-
DISPATCH();
297-
}
298-
299-
TARGET(OP2) {
300-
PyObject *arg = PEEK(1);
301-
uint32_t extra = read_u32(&next_instr[0].cache);
302-
op2();
303-
STACK_SHRINK(1);
304-
JUMPBY(2);
305-
DISPATCH();
306-
}
307-
308-
TARGET(OP) {
309-
PyObject *_tmp_1 = PEEK(1);
310-
PyObject *_tmp_2 = PEEK(2);
311-
{
312-
PyObject *arg = _tmp_1;
313-
uint16_t counter = read_u16(&next_instr[0].cache);
314-
op1();
315-
}
316-
JUMPBY(1);
317-
NEXTOPARG();
318-
JUMPBY(1);
319-
{
320-
PyObject *arg = _tmp_2;
321-
uint32_t extra = read_u32(&next_instr[0].cache);
322-
op2();
323-
}
324-
JUMPBY(2);
325-
STACK_SHRINK(2);
326-
DISPATCH();
327-
}
328-
"""
329-
run_cases_test(input, output)
330-
331270
def test_macro_instruction():
332271
input = """
333272
inst(OP1, (counter/1, left, right -- left, right)) {
@@ -344,18 +283,18 @@ def test_macro_instruction():
344283
"""
345284
output = """
346285
TARGET(OP1) {
347-
PyObject *right = PEEK(1);
348-
PyObject *left = PEEK(2);
286+
PyObject *right = stack_pointer[-1];
287+
PyObject *left = stack_pointer[-2];
349288
uint16_t counter = read_u16(&next_instr[0].cache);
350289
op1(left, right);
351-
JUMPBY(1);
290+
next_instr += 1;
352291
DISPATCH();
353292
}
354293
355294
TARGET(OP) {
356-
PyObject *_tmp_1 = PEEK(1);
357-
PyObject *_tmp_2 = PEEK(2);
358-
PyObject *_tmp_3 = PEEK(3);
295+
PyObject *_tmp_1 = stack_pointer[-1];
296+
PyObject *_tmp_2 = stack_pointer[-2];
297+
PyObject *_tmp_3 = stack_pointer[-3];
359298
{
360299
PyObject *right = _tmp_1;
361300
PyObject *left = _tmp_2;
@@ -373,22 +312,22 @@ def test_macro_instruction():
373312
res = op2(arg2, left, right);
374313
_tmp_3 = res;
375314
}
376-
JUMPBY(5);
315+
next_instr += 5;
377316
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
378317
STACK_SHRINK(2);
379-
POKE(1, _tmp_3);
318+
stack_pointer[-1] = _tmp_3;
380319
DISPATCH();
381320
}
382321
383322
TARGET(OP3) {
384-
PyObject *right = PEEK(1);
385-
PyObject *left = PEEK(2);
386-
PyObject *arg2 = PEEK(3);
323+
PyObject *right = stack_pointer[-1];
324+
PyObject *left = stack_pointer[-2];
325+
PyObject *arg2 = stack_pointer[-3];
387326
PyObject *res;
388327
res = op3(arg2, left, right);
389328
STACK_SHRINK(2);
390-
POKE(1, res);
391-
JUMPBY(5);
329+
stack_pointer[-1] = res;
330+
next_instr += 5;
392331
DISPATCH();
393332
}
394333
"""
@@ -402,9 +341,9 @@ def test_array_input():
402341
"""
403342
output = """
404343
TARGET(OP) {
405-
PyObject *above = PEEK(1);
406-
PyObject **values = &PEEK(1 + oparg*2);
407-
PyObject *below = PEEK(2 + oparg*2);
344+
PyObject *above = stack_pointer[-1];
345+
PyObject **values = (stack_pointer - (1 + oparg*2));
346+
PyObject *below = stack_pointer[-(2 + oparg*2)];
408347
spam();
409348
STACK_SHRINK(oparg*2);
410349
STACK_SHRINK(2);
@@ -426,8 +365,8 @@ def test_array_output():
426365
PyObject *above;
427366
spam(values, oparg);
428367
STACK_GROW(oparg*3);
429-
POKE(1, above);
430-
POKE(2 + oparg*3, below);
368+
stack_pointer[-1] = above;
369+
stack_pointer[-(2 + oparg*3)] = below;
431370
DISPATCH();
432371
}
433372
"""
@@ -441,11 +380,11 @@ def test_array_input_output():
441380
"""
442381
output = """
443382
TARGET(OP) {
444-
PyObject **values = &PEEK(oparg);
383+
PyObject **values = (stack_pointer - oparg);
445384
PyObject *above;
446385
spam(values, oparg);
447386
STACK_GROW(1);
448-
POKE(1, above);
387+
stack_pointer[-1] = above;
449388
DISPATCH();
450389
}
451390
"""
@@ -459,8 +398,8 @@ def test_array_error_if():
459398
"""
460399
output = """
461400
TARGET(OP) {
462-
PyObject **values = &PEEK(oparg);
463-
PyObject *extra = PEEK(1 + oparg);
401+
PyObject **values = (stack_pointer - oparg);
402+
PyObject *extra = stack_pointer[-(1 + oparg)];
464403
if (oparg == 0) { STACK_SHRINK(oparg); goto pop_1_somewhere; }
465404
STACK_SHRINK(oparg);
466405
STACK_SHRINK(1);
@@ -469,26 +408,6 @@ def test_array_error_if():
469408
"""
470409
run_cases_test(input, output)
471410

472-
def test_register():
473-
input = """
474-
register inst(OP, (counter/1, left, right -- result)) {
475-
result = op(left, right);
476-
}
477-
"""
478-
output = """
479-
TARGET(OP) {
480-
PyObject *left = REG(oparg1);
481-
PyObject *right = REG(oparg2);
482-
PyObject *result;
483-
uint16_t counter = read_u16(&next_instr[0].cache);
484-
result = op(left, right);
485-
Py_XSETREF(REG(oparg3), result);
486-
JUMPBY(1);
487-
DISPATCH();
488-
}
489-
"""
490-
run_cases_test(input, output)
491-
492411
def test_cond_effect():
493412
input = """
494413
inst(OP, (aa, input if ((oparg & 1) == 1), cc -- xx, output if (oparg & 2), zz)) {
@@ -497,18 +416,18 @@ def test_cond_effect():
497416
"""
498417
output = """
499418
TARGET(OP) {
500-
PyObject *cc = PEEK(1);
501-
PyObject *input = ((oparg & 1) == 1) ? PEEK(1 + (((oparg & 1) == 1) ? 1 : 0)) : NULL;
502-
PyObject *aa = PEEK(2 + (((oparg & 1) == 1) ? 1 : 0));
419+
PyObject *cc = stack_pointer[-1];
420+
PyObject *input = ((oparg & 1) == 1) ? stack_pointer[-(1 + (((oparg & 1) == 1) ? 1 : 0))] : NULL;
421+
PyObject *aa = stack_pointer[-(2 + (((oparg & 1) == 1) ? 1 : 0))];
503422
PyObject *xx;
504423
PyObject *output = NULL;
505424
PyObject *zz;
506425
output = spam(oparg, input);
507426
STACK_SHRINK((((oparg & 1) == 1) ? 1 : 0));
508427
STACK_GROW(((oparg & 2) ? 1 : 0));
509-
POKE(1, zz);
510-
if (oparg & 2) { POKE(1 + ((oparg & 2) ? 1 : 0), output); }
511-
POKE(2 + ((oparg & 2) ? 1 : 0), xx);
428+
stack_pointer[-1] = zz;
429+
if (oparg & 2) { stack_pointer[-(1 + ((oparg & 2) ? 1 : 0))] = output; }
430+
stack_pointer[-(2 + ((oparg & 2) ? 1 : 0))] = xx;
512431
DISPATCH();
513432
}
514433
"""

0 commit comments

Comments
 (0)