aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/exactsize.zc
blob: c3b22cce6c871d4a13226372e2cb8bfdc3c91555 (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
include rt;
// exact-size bounds: a heap block knows the n words new asked for (bits 32..62 of
// p[-4]), not just its power-of-two block. rt.bnd(p, i, 1, 1) is 1 when p[i] is
// outside: the same recipe every backend inlines in front of p[i].
struct P { int a; int b; int c; }
struct Q { int a; }
// n-1 inside, n outside, osize is n, and p[n-1] is usable
int edge(int n) {
	word* p = rt.new(n);
	p[n - 1] = n;
	return rt.bnd(p, n - 1, 1, 1) * 100 + rt.bnd(p, n, 1, 1) * 10 + (rt.osize(p) == n) + (p[n - 1] == n) - 1;
}
int zero() { word* p = rt.new(0); return rt.bnd(p, 0, 1, 1) * 10 + rt.osize(p); }
int bytes() { word* p = rt.new(3); return rt.bnd(p, 23, 1, 8) * 10 + rt.bnd(p, 24, 1, 8); }	// 3 words are 24 bytes
int where;
int leaf() { P* a = rt.new(P); a.c = 3; where = (&a.a) as int; return a.c; }	// a is recycled at the function's end
// recycle: the next 1-word object reuses the 3-word block, with n = 1
int reuse() {
	Q* q = rt.new(Q); int same = where == ((&q.a) as int);
	return same * 1000 + rt.osize(q) * 100 + rt.bnd(q, 0, 1, 1) * 10 + rt.bnd(q, 1, 1, 1);
}
// grow: the items block is replaced by one with its own exact n (16 words for 9 ints)
int grow() {
	int[?] d = rt.new(int[?], 0); int i = 0; word* h; word* p;
	while (i < 9) { d.push(i); i++; }
	h = &d[0]; p = h;
	return rt.osize(p) * 100 + rt.bnd(p, 15, 1, 1) * 10 + rt.bnd(p, 16, 1, 1);
}
// deep enough to grow the stacks: their segments are heap blocks the core allocates
// (src/core/stack.h, src/js/tinyvm.ts) with n set too. after they go back, blocks reused
// from them carry their own n.
int deep(int k) { if (k == 0) return 0; return deep(k - 1) + 1; }
fn main() {
	rt.put_n(edge(1)); rt.put_c(32); rt.put_n(edge(3)); rt.put_c(32); rt.put_n(edge(5)); rt.put_c(32);
	rt.put_n(edge(6)); rt.put_c(32); rt.put_n(edge(7)); rt.put_c(32); rt.put_n(edge(11)); rt.put_c(32);
	rt.put_n(edge(13)); rt.put_c(32); rt.put_n(edge(100)); rt.put_c(10);
	rt.put_n(zero()); rt.put_c(32); rt.put_n(bytes()); rt.put_c(10);
	leaf(); rt.put_n(reuse()); rt.put_c(10);
	rt.put_n(grow()); rt.put_c(10);
	rt.put_n(deep(200000)); rt.put_c(32); rt.put_n(edge(9)); rt.put_c(32); rt.put_n(edge(21)); rt.put_c(10);
	return 0;
}