aboutsummaryrefslogtreecommitdiff
path: root/examples/opt_test.zc
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
commitecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch)
tree70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /examples/opt_test.zc
a
Diffstat (limited to 'examples/opt_test.zc')
-rw-r--r--examples/opt_test.zc54
1 files changed, 54 insertions, 0 deletions
diff --git a/examples/opt_test.zc b/examples/opt_test.zc
new file mode 100644
index 0000000..c581283
--- /dev/null
+++ b/examples/opt_test.zc
@@ -0,0 +1,54 @@
+// 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.
+
+fn sink(.d p) { free(d*p); return 0; }
+
+// 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 = new(10);
+ buf[0] = i;
+ sink(d*buf);
+ i++;
+ }
+ return i;
+}
+
+// a small buffer that never leaves the function can live on the stack
+fn scratch() {
+ let buf = new(8);
+ buf[0] = 7;
+ return buf[0];
+}
+
+// a consuming call takes the object after its allocation
+fn handoff() {
+ let tmp = new(4);
+ sink(d*tmp);
+ return 0;
+}
+
+fn main() {
+ put_n(dead() + constant() + shifty(2) + magic(0) + looping() + scratch() + handoff()); put_c(10);
+ return 0;
+}