raz / test / run / Deep.raz

58 lines · 1.7 KiB · raw · history · last change 264716cb

1module Deep;
2import Out;
3import SYSTEM;
4
5(* Expressions deeper than the registers of the stack, with calls,
6 and, or, constants on the left and system calls among them. *)
7
8var g: I64;
9
10proc f(x: I64): I64;
11begin
12 g := g + 1;
13 return x * 3;
14end;
15
16proc b(x: BOOL): BOOL;
17begin
18 g := g + 10;
19 return x;
20end;
21
22proc n(x: BOOL): I64;
23begin
24 if x then return 1; end;
25 return 0;
26end;
27
28proc main*;
29var a, c, d, e: I64; s: [8]I64; t: BOOL; k: I32; u: U8; p: ^I64;
30begin
31 a := 1; c := 2; d := 3; e := 4;
32 (* nine values deep, right-nested *)
33 Out.int(a - (c - (d - (e - (a - (c - (d - (e - (a - 7))))))))); Out.ln;
34 (* calls under values held in registers *)
35 Out.int(a + (c * (d + f(e + f(a) * (c - f(d)))))); Out.ln;
36 Out.int(g); Out.ln;
37 (* constants on the left of operators that don't commute *)
38 Out.int(100 - a); Out.str(" "); Out.int(100 div c); Out.str(" ");
39 Out.int(100 mod (d + e)); Out.str(" "); Out.int(1 shl e); Out.str(" ");
40 Out.int(n(-1000000000000 < a)); Out.ln;
41 (* and, or over values held below them *)
42 t := (a + c > d) and b(c < d) or b(false);
43 Out.int(a + c * (d + n(t or b(true)))); Out.ln;
44 Out.int(g); Out.ln;
45 (* a constant too big for an instruction's operand *)
46 Out.int(a + 123456789012 * c - 9876543210); Out.ln;
47 (* narrow integers, division of their smallest by -1 aside *)
48 k := 2147483647; k := k + I32(a); Out.int(I64(k)); Out.str(" ");
49 u := 250; u := u + U8(c * 5) + U8(d); Out.int(I64(u)); Out.ln;
50 (* array elements and pointers among deep values *)
51 for d := 0 to 7 do s[d] := d * d; end;
52 p := SYSTEM.ADR(s[3]);
53 Out.int(s[a] + (s[c] * (s[e] - (p^ + s[s[2]] * (s[7] - s[6]))))); Out.ln;
54 (* a system call under values *)
55 Out.int(a + (c * n(SYSTEM.SYSCALL(39) > 0) + d)); Out.ln;
56end;
57
58end Deep.