| 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. *) |
| 6 | module Calls; |
| 7 | import Out; |
| 8 | import SYSTEM; |
| 9 | |
| 10 | extern proc printf(fmt: ^U8; ...): I32; |
| 11 | extern proc fflush(f: ^U8): I32; |
| 12 | |
| 13 | type |
| 14 | Big = record a: [40]INT; n: INT; end; |
| 15 | |
| 16 | proc ten(a, b, c, d, e, f, g, h, i, j: INT): INT; |
| 17 | begin |
| 18 | return a + 2 * b + 3 * c + 4 * d + 5 * e + 6 * f + 7 * g + 8 * h + 9 * i + 10 * j; |
| 19 | end; |
| 20 | |
| 21 | (* the slice takes x7 and the stack's first word, or both on the stack *) |
| 22 | proc late(a, b, c, d, e, f, g: INT; s: []const U8; n: I8): INT; |
| 23 | begin |
| 24 | return a + b + c + d + e + f + g + LEN(s) * 100 + INT(s[1]) * 1000 + INT(n) * 100000; |
| 25 | end; |
| 26 | |
| 27 | proc big(n: INT): Big; |
| 28 | var r: Big; i: INT; |
| 29 | begin |
| 30 | for i := 0 to 39 do r.a[i] := i * n; end; |
| 31 | r.n := n; |
| 32 | return r; |
| 33 | end; |
| 34 | |
| 35 | proc three(x: INT): (INT, INT, INT); |
| 36 | begin |
| 37 | return x, x * 2, x * 3; |
| 38 | end; |
| 39 | |
| 40 | proc passOn(x: INT): (INT, INT, INT); |
| 41 | begin |
| 42 | return three(x + 1); |
| 43 | end; |
| 44 | |
| 45 | proc two(x: INT): (INT, INT); |
| 46 | begin |
| 47 | return x + 1, x - 1; |
| 48 | end; |
| 49 | |
| 50 | proc twice(x: INT): INT; |
| 51 | begin |
| 52 | return 2 * x; |
| 53 | end; |
| 54 | |
| 55 | proc main*; |
| 56 | var b, c: Big; p, q, r: INT; f: proc(x: INT): INT; |
| 57 | begin |
| 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); |
| 74 | end; |
| 75 | |
| 76 | end Calls. |