aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/run.zc
blob: a8f82e2e5bffb07e85af27bf0bfa9333bd999ea5 (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
namespace run;
include rt;

// run-only image: the runtime, a collector root set, and start. there is no
// resident compiler and none of its tables. put this between rt.zc and a
// program that defines main. zc0 accepts the bootstrap subset; build.zc uses
// the self-hosted compiler for typed programs and library receiver methods.
//
//   cat build/run/rt.zc src/runtime/run.zc examples/hello.zc | ./zc0 >build/run/genome.h

// the program's entry point, written by the program (see start).
namespace;
int main();
namespace run;
include rt;

// the collector's roots: the code and globals, the program's frames and the
// operand stack. the collector marks from these before it frees anything.
int gc_roots() {
	rt.markrange(0, rt.CODE_END);
	rt.markrange(rt.GORG, rt.GORG[6]);
	rt.gc_frames();
	rt.markrange(rt.dsp(), rt.data_top);
	return 0;
}

// install enhancement descriptors from the image's symbol table before any
// runtime arithmetic uses their opcodes. kind 6 is an enhancement record.
int install_enhancements() {
	word p = rt.GORG[5], name;
	while (*p) {
		if (p[0] == 6 && rt.install(p[2] >> 8) < 0) {
			rt.put_s("run: cannot install image enhancement\n");
			rt.syscall(rt.SYS_EXIT, 2, 0, 0);
		}
		name = p + 3;
		while (*name) name++;
		p = name + 1;
	}
	return 0;
}

// the first code a run-only image executes: initialize the runtime, run main and halt.
int start() {
	install_enhancements();
	rt.heap_init();
	main();
	rt.halt();
	return 0;
}