| 1 | (* A procedure that calls nothing keeps its first parameters in rdi and |
| 2 | rsi; when it copies an array, which needs them, it is compiled again |
| 3 | with those parameters in callee-saved registers where some are free |
| 4 | and they are used enough, or in memory. The copy and the uses after |
| 5 | it must agree either way, and a failed check still says their |
| 6 | values. *) |
| 7 | module KeptCopy; |
| 8 | import Out; |
| 9 | |
| 10 | const N = 8; |
| 11 | |
| 12 | type Row = [N]I64; |
| 13 | |
| 14 | var r1, r2: Row; total: I64; |
| 15 | |
| 16 | (* out a copy of st, then each element worked on through both pointers *) |
| 17 | proc mix(const st: Row; var out: Row; k: INT); |
| 18 | var i: INT; |
| 19 | begin |
| 20 | out := st; |
| 21 | for i := 0 to N - 1 do out[i] := out[i] * 3 + st[N - 1 - i] + k; end; |
| 22 | for i := 1 to N - 1 do out[i] := out[i] + out[i - 1]; end; |
| 23 | end; |
| 24 | |
| 25 | (* a slice, kept in both registers, then a copy, then the slice read: |
| 26 | one callee-saved register is left, too few for it, so it goes to |
| 27 | memory *) |
| 28 | proc sumAfter(s: []I64; var a: Row; const b: Row): I64; |
| 29 | var i: INT; t: I64; |
| 30 | begin |
| 31 | a := b; |
| 32 | t := 0; |
| 33 | for i := 0 to LEN(s) - 1 do t := t + s[i] * a[i mod N]; end; |
| 34 | return t; |
| 35 | end; |
| 36 | |
| 37 | (* the same, with registers enough left for the slice's two *) |
| 38 | proc sumThen(s: []I64; var a: Row): I64; |
| 39 | var i: INT; t: I64; |
| 40 | begin |
| 41 | a := r1; |
| 42 | t := 0; |
| 43 | for i := 0 to LEN(s) - 1 do t := t + s[i] * s[LEN(s) - 1 - i]; end; |
| 44 | return t + a[0]; |
| 45 | end; |
| 46 | |
| 47 | (* five busy locals take every callee-saved register: the pointers go |
| 48 | to memory *) |
| 49 | proc busy(var out: Row; const st: Row): I64; |
| 50 | var a, b, c, d, e, i: INT; |
| 51 | begin |
| 52 | out := st; |
| 53 | a := 1; b := 2; c := 3; d := 4; e := 5; |
| 54 | for i := 0 to N - 1 do |
| 55 | a := a + b; b := b + c; c := c + d; d := d + e; e := e + a; |
| 56 | out[i] := out[i] + a + b + c + d + e + st[i]; |
| 57 | end; |
| 58 | return out[N - 1]; |
| 59 | end; |
| 60 | |
| 61 | (* a pointer used once only goes to memory *) |
| 62 | proc once(var out: Row; const st: Row); |
| 63 | begin |
| 64 | out := st; |
| 65 | out[0] := 99; |
| 66 | end; |
| 67 | |
| 68 | (* the element at k of a copy, which may be out of range *) |
| 69 | proc pick(const st: Row; var out: Row; k: INT): I64; |
| 70 | var i: INT; |
| 71 | begin |
| 72 | out := st; |
| 73 | for i := 0 to N - 1 do out[i] := out[i] + st[i]; end; |
| 74 | return out[k] + st[0]; |
| 75 | end; |
| 76 | |
| 77 | proc say(const r: Row); |
| 78 | var i: INT; |
| 79 | begin |
| 80 | for i := 0 to N - 1 do Out.int(r[i]); Out.char(' '); end; |
| 81 | Out.ln; |
| 82 | end; |
| 83 | |
| 84 | proc main*; |
| 85 | var i: INT; |
| 86 | begin |
| 87 | for i := 0 to N - 1 do r1[i] := I64(i * i - 10); end; |
| 88 | mix(r1, r2, 7); |
| 89 | say(r2); |
| 90 | total := sumAfter(r1[1..N], r2, r1); |
| 91 | Out.int(total); Out.ln; |
| 92 | say(r2); |
| 93 | Out.int(sumThen(r1[2..N], r2)); Out.ln; |
| 94 | Out.int(busy(r2, r1)); Out.ln; |
| 95 | say(r2); |
| 96 | once(r2, r1); |
| 97 | say(r2); |
| 98 | Out.int(pick(r1, r2, 3)); Out.ln; |
| 99 | Out.int(pick(r1, r2, N + 1)); Out.ln; |
| 100 | end; |
| 101 | |
| 102 | end KeptCopy. |