aboutsummaryrefslogtreecommitdiff
path: root/docs/history/13-gap-fixes/code/src/rt.zc
blob: 55b897642c2468ca3905fdb28f1cb728761d4cd5 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// ============================================================================
// rt.zc: the runtime half of the genome. memory map, i/o, arithmetic helpers,
// the heap (size-class free lists) and the mark-sweep collector.
// ============================================================================

// ---- memory map (words) ----
const MW = 0x100000;		// 1m words of memory
const HEAP_LO = 0x40000;	// heap: 0x40000 .. 0xA0000
const HEAP_HI = 0xA0000;
const ZFP = 0xA0000;		// frame pointer word, used by every prologue
const GORG = 0xC0000;		// the genome (this code) lives here
const FBOT = 0xE0000;		// frame stack: FBOT .. FTOP, grows down
const FTOP = 0xF0000;
const RS0 = 0xF0000;		// return stack grows up from here
const MARK0 = 0xF8000;		// mark stack for the collector
const MARKN = 0x4000;
const DSLO = 0xFC000;		// data stack: DSLO .. MW-16, grows down
const DSHI = 0xFFFF0;
const MAGIC = 0x2B7E1516;

// ---- virtual enhancements ----
// install(desc) wires a custom opcode into the cpu (the % opcode). these three
// give the language native division and left shift (zdiv, zmod and zshl
// below are the same thing done in software). they come first, since the
// compiler resolves / % and << to them by name.
fn install(desc) { return asm { desc @ % }; }
enhance idiv(a, b) micro "a = sdiv a b";
enhance imod(a, b) micro "a = srem a b";
enhance ishl(a, b) micro "a = shl a b";

// ---- i/o ----
fn putc(ch) { asm { ch @ . } return 0; }
fn getc() { return asm { , }; }
fn eof() { return asm { ? }; }
fn halt() { asm { q } return 0; }
fn puts(s) { let i = 0; while (s[i]) { putc(s[i]); i++; } return 0; }
fn putn(n)
{
	let b[12], i = 0;
	if (n < 0) { putc('-'); n = 0 - n; }
	if (n < 0) { puts("2147483648"); return 0; }
	while (1) { b[i++] = '0' + n % 10; n = n / 10; if (!n) break; }
	while (i) putc(b[--i]);
	return 0;
}
fn strlen(s) { let n = 0; while (s[n]) n++; return n; }
fn streq(a, b) { let i = 0; while (a[i] && a[i] == b[i]) i++; return a[i] == b[i]; }
fn wcopy(dst, src, n) { let i = 0; while (i < n) { dst[i] = src[i]; i++; } return 0; }
fn wfill(dst, v, n) { let i = 0; while (i < n) { dst[i] = v; i++; } return 0; }

// ---- arithmetic the hardware doesn't have ----
let pow2[32];
let zrem;
fn zudiv(n, dv)
{
	let q = 0, r = 0, i = 30;
	if (n < dv) { zrem = n; return 0; }
	while (i >= 0) {
		r = r + r + ((n >> i) & 1);
		q = q + q;
		if (r >= dv) { r = r - dv; q++; }
		i--;
	}
	zrem = r;
	return q;
}
fn zdiv(a, b)
{
	let s = 0, q;
	if (b == 0) { zrem = a; return 0; }
	if (a < 0) { a = 0 - a; s = 1; }
	if (b < 0) { b = 0 - b; s = s ^ 1; }
	q = zudiv(a, b);
	if (s) return 0 - q;
	return q;
}
fn zmod(a, b) { let neg = a < 0; if (b == 0) return a; zdiv(a, b); if (neg) return 0 - zrem; return zrem; }
fn zshl(a, b) { return a * pow2[b & 31]; }

// ---- heap ----
// a buddy allocator. the heap is cut into aligned power-of-two chunks, every
// block is a power of two words, and a freed block merges with its buddy
// (the other half of the block they were split from) whenever that one is
// free too, so freed space can be reused at any size. every block has a 6
// word header before the pointer the program sees:
//   p[-6] block size   p[-5] size class   p[-4] flags (0 free, 1 live, 3 marked)
//   p[-3] p ^ MAGIC    p[-2] owner (the container that owns it, or 0)
//   p[-1] its type (a type record from new(T), or 0 from new(n))
// free blocks sit on a doubly linked list per class, linked through p[0], p[1].
const HDR = 6;
let hbump, freel[32], gc_runs, gc_freed, gc_auto = 1;

// the size of the top-level chunk holding heap offset off
fn topsize(off)
{
	let h = HEAP_HI - HEAP_LO, base = 0, sz;
	while (h) {
		sz = 1;
		while (sz + sz <= h) sz = sz + sz;
		if (off < base + sz) return sz;
		base = base + sz; h = h - sz;
	}
	return 0;
}
fn fpush(b, k) { b[2] = 0; b[6] = freel[k]; b[7] = 0; if (freel[k]) freel[k][7] = b; freel[k] = b; return 0; }
fn funlink(b, k)
{
	if (b[7]) b[7][6] = b[6]; else freel[k] = b[6];
	if (b[6]) b[6][7] = b[7];
	return 0;
}
fn mkblock(b, k) { b[0] = pow2[k]; b[1] = k; b[2] = 0; b[3] = 0; b[4] = 0; b[5] = 0; return b; }
fn heap_init()
{
	let i = 1, b = HEAP_LO, sz, k;
	pow2[0] = 1;
	while (i < 32) { pow2[i] = pow2[i - 1] + pow2[i - 1]; i++; }
	hbump = HEAP_HI;
	wfill(freel, 0, 32);
	while (b < HEAP_HI) {
		sz = topsize(b - HEAP_LO); k = 0;
		while (pow2[k] < sz) k++;
		fpush(mkblock(b, k), k);
		b = b + sz;
	}
	return 0;
}
// a free block of class k, split down from a bigger one if needed; 0 if none
fn balloc(k)
{
	let j = k, b, h;
	while (j < 32 && !freel[j]) j++;
	if (j >= 32) return 0;
	b = freel[j];
	funlink(b, j);
	while (j > k) { j--; h = b + pow2[j]; fpush(mkblock(h, j), j); }
	mkblock(b, k);
	return b;
}
let fend;		// end of the block bfree last made, for the sweep
// put block b back, merging it with its buddy as long as it can
fn bfree(b)
{
	let k = b[1], sz = pow2[k], off, bud;
	while (1) {
		off = b - HEAP_LO;
		if (sz >= topsize(off)) break;
		bud = HEAP_LO + (off ^ sz);
		if (bud[2] != 0 || bud[1] != k || bud[0] != sz) break;
		funlink(bud, k);
		if (bud < b) { b[0] = 0; b[3] = 0; b = bud; }	// the upper header is inside the merged block now
		else { bud[0] = 0; bud[3] = 0; }
		k++; sz = sz + sz;
		mkblock(b, k);
	}
	fpush(mkblock(b, k), k);
	fend = b + sz;
	return 0;
}
fn isobj(x) { return x >= HEAP_LO + HDR && x < hbump && x[-3] == (x ^ MAGIC) && (x[-4] & 1); }

// every prologue checks the frame stack; on overflow it resets the frame
// pointer (so this can run at all) and stops
fn frame_overflow() { puts("zy: frame stack overflow\n"); halt(); return 0; }
fn oom() { puts("zc: out of heap memory\n"); halt(); return 0; }
// n words typed t
unchecked fn new_t(n, t) -> .u
{
	let need = n + HDR, k = 3, b, p;
	if (n < 0) need = HDR;
	while (pow2[k] < need) k++;
	b = balloc(k);
	if (!b && gc_auto) { gc(); b = balloc(k); }
	if (!b) oom();
	p = b + HDR;
	b[2] = 1; b[3] = p ^ MAGIC; b[4] = 0; b[5] = t;
	wfill(p, 0, pow2[k] - HDR);
	return p;
}
unchecked fn new(n) -> .u { return new_t(n, 0); }
fn osize(p) { if (!isobj(p)) return 0; return p[-6] - HDR; }
fn typeof_obj(p) { if (!isobj(p)) return 0; return p[-1]; }

// frees p and, recursively, every object p owns
fn release(p)
{
	let b = p - HDR, n = b[0] - HDR, i = 0, w;
	b[2] = 0;
	while (i < n) { w = p[i]; if (isobj(w) && w[-2] == p) release(w); i++; }
	bfree(b);
	return 0;
}
unchecked fn free(.d p) { if (isobj(p)) release(p); return 0; }
fn own(c, p) { if (isobj(c)) c[-2] = p; return c; }

// ---- mark-sweep ----
// conservative and non-moving. roots: the program area (globals live
// between functions), the genome's globals, the compiler's input stack,
// the live part of the frame stack and the whole data stack region.
let ms_top, ms_over, gc_lo, gc_hi;
fn mark(w)
{
	if (!isobj(w) || (w[-4] & 2)) return 0;
	w[-4] = w[-4] | 2;
	if (ms_top < MARKN) { MARK0[ms_top] = w; ms_top++; } else ms_over = 1;
	return 0;
}
fn markrange(a, b) { while (a < b) { mark(*a); a++; } return 0; }
fn drain()
{
	let p;
	while (ms_top) { ms_top--; p = MARK0[ms_top]; markrange(p, p + p[-6] - HDR); }
	return 0;
}
fn gc_roots();
unchecked fn gc()
{
	let b, sz, n = 0;
	ms_top = 0; ms_over = 0;
	gc_roots();
	drain();
	while (ms_over) {	// the mark stack overflowed: rescan marked objects
		ms_over = 0;
		b = HEAP_LO;
		while (b < HEAP_HI) { if (b[2] == 3) markrange(b + HDR, b + b[0]); b = b + b[0]; drain(); }
	}
	b = HEAP_LO;
	while (b < HEAP_HI) {
		sz = b[0];
		if (b[2] == 1) { bfree(b); n++; b = fend; }	// merging may swallow what follows
		else { if (b[2] == 3) b[2] = 1; b = b + sz; }
	}
	gc_runs++;
	gc_freed = gc_freed + n;
	return n;
}
// number of live objects, for tests
fn live_objects() { let b = HEAP_LO, n = 0; while (b < HEAP_HI) { if (b[2] & 1) n++; b = b + b[0]; } return n; }
// the biggest block that's free right now, in words
fn largest_free() { let k = 31; while (k > 0 && !freel[k]) k--; if (!freel[k]) return 0; return pow2[k]; }