(* An element of a var or const array parameter, of 1, 2, 4 or 8 bytes, is read and written as the operand [pointer + index * size], the index checked first; here in a leaf, which keeps the pointer in rdi or rsi, and in a procedure that calls, which keeps it in a callee-saved register. IndexParamOff has the same with no checks. An element of another size is added up. The last index is out of range. *) module IndexParam; import Out, IndexParamOff; const N = 8; type Bytes = IndexParamOff.Bytes; Shorts = IndexParamOff.Shorts; Words = IndexParamOff.Words; Longs = IndexParamOff.Longs; Odd = record a, b, c: U8; end; (* 3 bytes *) Pair = record tag: U8; v: [N]I32; end; (* v at an offset *) var bs: Bytes; ss: Shorts; ws: Words; ls, ds: Longs; os: [N]Odd; pr: Pair; proc sumBytes(const a: Bytes): INT; var i, t: INT; begin t := 0; for i := 0 to N - 1 do t := t + a[i] + INT(a[N - 1 - i]) * 2; end; for i := 0 to N div 2 - 1 do t := t + a[2 * i + 1] + a[a[i] mod N]; end; return t; end; proc sumShorts(const a: Shorts): INT; var i, t: INT; begin t := 0; for i := 1 to N - 1 do t := t + INT(a[i]) * a[i - 1]; end; return t; end; proc sumWords(const a: Words): U32; var i: INT; t: U32; begin t := 0; for i := 0 to N - 2 do t := t + a[i + 1] xor a[i]; end; return t; end; proc double(var a: Longs; const d: Longs); var i: INT; begin for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; end; end; (* as double, but calling between: the pointers are kept past calls *) proc doubleSay(var a: Longs; const d: Longs); var i: INT; begin for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; Out.int(a[i]); Out.char(' '); end; Out.ln; end; proc swap(var a: Bytes); var i: INT; c: U8; begin for i := 0 to N div 2 - 1 do c := a[i]; a[i] := a[N - 1 - i]; a[N - 1 - i] := c; end; end; (* the elements of 3 bytes, added up as before *) proc sumOdd(var a: [N]Odd): INT; var i, t: INT; begin t := 0; for i := 0 to N - 1 do a[i].c := U8(a[N - 1 - i].a + 1); t := t + a[i].a + INT(a[i].b) * a[i].c; end; return t; end; (* an array at an offset in a record parameter, read and written *) proc pairSum(var p: Pair): INT; var i, t: INT; begin t := 0; for i := 0 to N - 1 do p.v[i] := I32(i - 3) * I32(p.tag); end; for i := 1 to N - 1 do t := t + INT(p.v[i - 1]) * 10 + INT(p.v[i]); end; return t; end; (* a's element at k, which may be out of range *) proc at(const a: Shorts; k: INT): INT; begin return a[k + 1]; end; proc reset; var i: INT; begin for i := 0 to N - 1 do bs[i] := U8(i * 37 + 5); ss[i] := I16(i * 1000 - 3500); ws[i] := U32(i) * 9E3779B9H; ls[i] := I64(i + 1); ds[i] := I64(i * i) - 20; os[i].a := U8(i); os[i].b := U8(i + 1); os[i].c := 0; end; end; proc main*; var i: INT; begin reset(); Out.int(sumBytes(bs)); Out.char(' '); Out.int(sumShorts(ss)); Out.char(' '); Out.int(I64(sumWords(ws))); Out.ln; Out.int(IndexParamOff.sumBytes(bs)); Out.char(' '); Out.int(IndexParamOff.sumShorts(ss)); Out.char(' '); Out.int(I64(IndexParamOff.sumWords(ws))); Out.ln; double(ls, ds); for i := 0 to N - 1 do Out.int(ls[i]); Out.char(' '); end; Out.ln; reset(); IndexParamOff.double(ls, ds); for i := 0 to N - 1 do Out.int(ls[i]); Out.char(' '); end; Out.ln; reset(); doubleSay(ls, ds); swap(bs); for i := 0 to N - 1 do Out.int(I64(bs[i])); Out.char(' '); end; Out.ln; IndexParamOff.swap(bs); for i := 0 to N - 1 do Out.int(I64(bs[i])); Out.char(' '); end; Out.ln; Out.int(sumOdd(os)); Out.ln; pr.tag := 7; Out.int(pairSum(pr)); Out.ln; Out.int(at(ss, 6)); Out.ln; Out.int(at(ss, 7)); Out.ln; end; end IndexParam.