raz / test / mac / Calls.raz

76 lines · 1.8 KiB · raw · history · last change 6d4487fb

1(* Calls past the registers (PLAN.md 5, 5b): arguments past x7 on the
2 machine's stack, a slice among them; a record and several results
3 through x8; two results in x0 and x1; a procedure value; C's printf,
4 whose arguments past its own go on the stack; and copies past 256
5 bytes. *)
6module Calls;
7import Out;
8import SYSTEM;
9
10extern proc printf(fmt: ^U8; ...): I32;
11extern proc fflush(f: ^U8): I32;
12
13type
14 Big = record a: [40]INT; n: INT; end;
15
16proc ten(a, b, c, d, e, f, g, h, i, j: INT): INT;
17begin
18 return a + 2 * b + 3 * c + 4 * d + 5 * e + 6 * f + 7 * g + 8 * h + 9 * i + 10 * j;
19end;
20
21(* the slice takes x7 and the stack's first word, or both on the stack *)
22proc late(a, b, c, d, e, f, g: INT; s: []const U8; n: I8): INT;
23begin
24 return a + b + c + d + e + f + g + LEN(s) * 100 + INT(s[1]) * 1000 + INT(n) * 100000;
25end;
26
27proc big(n: INT): Big;
28var r: Big; i: INT;
29begin
30 for i := 0 to 39 do r.a[i] := i * n; end;
31 r.n := n;
32 return r;
33end;
34
35proc three(x: INT): (INT, INT, INT);
36begin
37 return x, x * 2, x * 3;
38end;
39
40proc passOn(x: INT): (INT, INT, INT);
41begin
42 return three(x + 1);
43end;
44
45proc two(x: INT): (INT, INT);
46begin
47 return x + 1, x - 1;
48end;
49
50proc twice(x: INT): INT;
51begin
52 return 2 * x;
53end;
54
55proc main*;
56var b, c: Big; p, q, r: INT; f: proc(x: INT): INT;
57begin
58 Out.int(ten(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); Out.ln();
59 Out.int(late(1, 2, 3, 4, 5, 6, 7, "xyz", -3)); Out.ln();
60 b := big(3);
61 c := b;
62 Out.int(c.a[39] + c.n); Out.ln();
63 p, q, r := three(5);
64 Out.int(p + q * 10 + r * 100); Out.ln();
65 p, q, r := passOn(5);
66 Out.int(p + q * 10 + r * 100); Out.ln();
67 p, q := two(7);
68 Out.int(p * 10 + q); Out.ln();
69 f := twice;
70 Out.int(f(21)); Out.ln();
71 Out.flush();
72 _ := printf("%d %s %ld %d\n", 42, "and", 1234567890123, -5);
73 _ := fflush(nil);
74end;
75
76end Calls.