aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/compiler_growth_test.py
blob: e56bf4304aa3570e1932c7c8d8201d172e30a191 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# focused growth checks for compiler columns, stable names, types, and scratch stacks.
import argparse
import subprocess
import sys

parser = argparse.ArgumentParser()
parser.add_argument('server')
parser.add_argument('--names', type=int, default=18000)
options = parser.parse_args()
server = options.server
assert options.names > 0


def run(source, expected):
    result = subprocess.run([server], input=source, text=True, capture_output=True, timeout=30)
    assert result.returncode == 0, result.stderr
    assert result.stdout == expected, result.stdout[:1000]


locals_source = 'include rt; fn main() {\n'
locals_source += ''.join(f'int local_{i} = {i};\n' for i in range(900))
locals_source += 'rt.put_n(local_899); rt.put_c(10); return 0; }\n'
run(locals_source, '899\n')

types_source = ''.join(f'type array_{i} = int[{i + 1}];\n' for i in range(1500))
types_source = 'include rt, run;\n' + types_source
types_source += 'fn main() { rt.put_n(run.ntl > 1500); rt.put_c(10); rt.gc(); rt.put_n(run.tsz(run.tarr(run.tint, 1500))); rt.put_c(10); return 0; }\n'
run(types_source, '1\n1500\n')

names_source = 'include rt, run;\n' + ''.join(f'const int repeated_long_identifier_for_pool_and_hash_growth_{i} = {i};\n' for i in range(options.names))
names_source += f'''fn main() {{
 word saved = run.intern("a name that must survive growth");
 run.reserve_names(run.hashcap * 4); run.reserve_symbols(run.symcap * 2); run.reserve_locals(run.loccap * 2);
 run.reserve_fixes(run.fixcap * 2); run.reserve_R_SRC(4096); run.reserve_R_CTX(4096);
 run.reserve_R_LOOP(4096); run.reserve_R_SNAP(32768); run.reserve_R_QBUF(32768); run.reserve_R_EXIT(8192);
 rt.gc();
 rt.put_n(saved == run.intern("a name that must survive growth")); rt.put_c(10);
 rt.put_n(repeated_long_identifier_for_pool_and_hash_growth_{options.names - 1}); rt.put_c(10);
 rt.put_n(run.poolblocks != 0 && run.symcap > run.NS && run.hashcap > run.IHN); rt.put_c(10);
 return 0;
}}
'''
run(names_source, f'1\n{options.names - 1}\n1\n')

long_name = 'identifier_' + 'x' * 1500
run(f'include rt; const int {long_name} = 7; fn main() {{ rt.put_n({long_name}); rt.put_c(10); return 0; }}\n', '7\n')

mark_source = '''include rt; let roots[60000];
unchecked fn main() {
 int i = 0, before = rt.live_objects();
 while (i < 60000) { roots[i] = rt.new(1); i++; }
 rt.gc(); rt.put_n(rt.markcap > rt.MARKN); rt.put_c(32); rt.put_n(rt.live_objects() - before == 60000); rt.put_c(32);
 // explicitly release survivors: conservative instruction roots may resemble heap addresses.
 i = 0; while (i < 60000) { rt.free(roots[i]); i++; }
 rt.fill_w(roots, 0, 60000); rt.scrub(); rt.gc();
 rt.put_n(rt.live_objects() == before); rt.put_c(10); return 0;
}
'''
run(mark_source, '1 1 1\n')

generics_source = ''.join(f'int generic_{i}<T>() {{ return sizeof(T); }}\n' for i in range(90))
generics_source += 'int size_of<T>() { return sizeof(T); }\n'
generics_source = 'include rt;\n' + generics_source
generics_source += 'fn main() { int total = 0;\n'
generics_source += ''.join(f'total += generic_{i}<int>();\n' for i in range(90))
generics_source += ''.join(f'total += size_of<int[{i + 1}]>();\n' for i in range(150))
generics_source += 'rt.gc(); rt.put_n(total); rt.put_c(10); return 0; }\n'
run(generics_source, '11415\n')

captures_source = 'include rt; fn main() {\n' + ''.join(f'int captured_{i} = {i};\n' for i in range(70))
captures_source += 'let f = fn() { return ' + ' + '.join(f'captured_{i}' for i in range(70)) + '; };\n'
captures_source += 'rt.gc(); rt.put_n(f()); rt.put_c(10); return 0; }\n'
run(captures_source, '2415\n')

nested_source = 'include rt; fn main() { int captured = 7;\n'
nested_source += ''.join(f'let nested_{i} = fn() {{\n' for i in range(12))
nested_source += 'return captured;\n'
nested_source += ''.join(f'}}; return nested_{i}();\n' for i in reversed(range(1, 12)))
nested_source += '}; rt.gc(); rt.put_n(nested_0()); rt.put_c(10); return 0; }\n'
run(nested_source, '7\n')
assembly_source = 'include rt; fn main() { int value = asm { ' + 'future _ ' * 300
assembly_source += ''.join(f':label_{i} ' for i in range(200)) + ':future 7 }; rt.gc(); rt.put_n(value); rt.put_c(10); return 0; }\n'
run(assembly_source, '7\n')
print('ok   compiler tables, long identifiers, and mark work grow without losing references')