aboutsummaryrefslogtreecommitdiff
path: root/examples/opt_test.zc
blob: e310690e7331910823ee8aaff9a0271f6882dc43 (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
namespace;
include rt;

// a tour of the compiler's optimization hints. the program is valid and runs;
// the hints only show up in the editor (or the lsp cli), never in a normal build.
// each function below triggers one of them.

void sink(.d p) { rt.free(p); return; }

// a variable that is written and never read
fn dead() {
	let unused = 5;
	return 1;
}

// never reassigned, so it could be a const
fn constant() {
	let width = 40;
	return width + 2;
}

// multiplying by a power of two is a shift
fn shifty(n) { return n * 8; }

// a number with no name
fn magic(n) { return n + 1234; }

// new inside a loop allocates every pass
fn looping() {
	let i = 0;
	while (i < 3) {
		let buf = rt.new(10);
		buf[0] = i;
		sink(buf);
		i++;
	}
	return i;
}

// a small buffer that never leaves the function can live on the stack
fn scratch() {
	let buf = rt.new(8);
	buf[0] = 7;
	return buf[0];
}

// a consuming call takes the object after its allocation
void handoff() {
	let tmp = rt.new(4);
	sink(tmp);
	return;
}

void main() {
	rt.put_n(dead() + constant() + shifty(2) + magic(0) + looping() + scratch()); handoff(); rt.put_c(10);
	return;
}