From 87e3c487421b8db4e2fca12ce1d6541f17e9235c Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:52:22 +0100 Subject: [PATCH] An element of a var or const array parameter, of 1, 2, 4 or 8 bytes, is the operand [pointer + index * size], as one in the frame is: indexArray takes bPtr's register as the base (a sum over const s: [N]INT 145 ms to 82, a slice's speed; ChaCha's quarter on var x: [16]U32 560 ms to 283) --- diff --git a/doc/compiler.md b/doc/compiler.md index d063e49..19a958f 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -21,7 +21,7 @@ | `src/Sym.raz` | 723 | 581 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,797 | 1,961 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,592 | 3,942 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | @@ -884,6 +884,36 @@ ahead. The compiler, built by itself, runs 0.9% fewer instructions on the 348,000-line program, in the same time. +## Array parameters, measured + +Two gaps in what the earlier sections built, found by timing loops +over arrays passed as parameters, which the library's `const` +convention (report 2026-10.33) made common. + +1. **An array parameter's element as an operand.** `indexArray` noted + an element as `[base + index * size]` (`lIdx`) for an array in the + frame or a chunk, but an array reached through a pointer (`bPtr`, + a `var` or `const` parameter whose address is in a register) was + added up, though the pointer was in a register already. A sum over + `const s: [N]INT`: + + ``` + mov r9, r12 + shl r9, 3 + lea r10, [rdi] now: add rbx, [rdi + r12 * 8] + add r9, r10 + mov r9, [r9] + add rbx, r9 + ``` + + The pointer's register is the base, as `rbp` is for the frame. It + is a variable's register, not one of the stack's, so the value + owns only its index's, as for the frame. One in a frame slot + (`bSlot`) is added up still: the pointer would need a register of + its own. The sum over a `const` array 145 ms to 82, the speed of + the sum over a slice; ChaCha's `quarter(var x: [16]U32; ...)` 560 + ms to 283 (2.0 times), in 58% fewer instructions. + ## Diag.raz: messages Every message the compiler prints is made here. A position, `Pos`, is a @@ -1097,8 +1127,8 @@ `and` and `or`, `fix` where the label is, and `joinJumps`; - checks: `checkIndex`, `checkView`, `checkEnum`, and `indexSlice`, `slice` and `slicePtr`, which check as they make their address or slice; and - `indexArray`, an element's address in an array in the frame or a - chunk, the index checked before; + `indexArray`, an element's address in an array in the frame, a + chunk or behind a parameter's pointer, the index checked before; - calls (`call`), frames (`beginProc`, `enterBody`, `returnJump`, `endProc`, the temporaries of `allocTemp`), deferred statements (`deferBegin`, `deferEnd`, `runDefer`), the built-ins that are diff --git a/src/Gen.raz b/src/Gen.raz index 00f2fa4..d2cac41 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -1785,18 +1785,21 @@ (* Pops an index, checked, and makes the address of the element it picks of the array at a. An element of 1, 2, 4 or 8 bytes is only noted, as the operand [base + index * size] - (lIdx): an array in the frame has rbp as its base, and one in a - chunk its address as the displacement (X.memAbs), with no lea. Any - other is added up. *) + (lIdx): an array in the frame has rbp as its base, one reached + through a pointer in a register (a var or const parameter) that + register, and one in a chunk its address as the displacement + (X.memAbs), with no lea. Any other is added up. *) proc indexArray*(a: Adr; size: I64); var r: INT; o: I64; begin - if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and ((a.base = bFrame) or (a.base = bGlobal)) then + if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base in {bFrame, bGlobal, bPtr}) then o := indexOff(size, a.off); r := peek(depth - 1); drop(); if a.base = bFrame then newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); + elsif a.base = bPtr then + newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false); else newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false); end; diff --git a/test/run/IndexParam.expect b/test/run/IndexParam.expect new file mode 100644 index 0000000..56dd024 --- /dev/null +++ b/test/run/IndexParam.expect @@ -0,0 +1,10 @@ +3246 26250000 2485999775 +3246 26250000 2485999775 +1 -17 -50 -111 -226 -447 -878 -1727 +1 -17 -50 -111 -226 -447 -878 -1727 +-17 -50 -111 -226 -447 -878 -1727 +8 227 190 153 116 79 42 5 +5 42 79 116 153 190 227 8 +148 +49 +3500 diff --git a/test/run/IndexParam.raz b/test/run/IndexParam.raz new file mode 100644 index 0000000..09e7236 --- /dev/null +++ b/test/run/IndexParam.raz @@ -0,0 +1,154 @@ +(* 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. diff --git a/test/run/IndexParam.status b/test/run/IndexParam.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/IndexParam.status @@ -0,0 +1 @@ +2 diff --git a/test/run/IndexParam.stderr b/test/run/IndexParam.stderr new file mode 100644 index 0000000..710fea2 --- /dev/null +++ b/test/run/IndexParam.stderr @@ -0,0 +1,3 @@ +test/run/IndexParam.raz:106:14: index 8 is out of range for a length of 8 + in IndexParam.at at test/run/IndexParam.raz:106:14: k = 7 + in IndexParam.main at test/run/IndexParam.raz:151:5: i = 7 diff --git a/test/run/mods/IndexParamOff.raz b/test/run/mods/IndexParamOff.raz new file mode 100644 index 0000000..7dc18a3 --- /dev/null +++ b/test/run/mods/IndexParamOff.raz @@ -0,0 +1,59 @@ +(* IndexParam's procedures again, with no checks: an element of an array + parameter of 1, 2, 4 or 8 bytes is read and written as the operand + [pointer + index * size], where the index comes with no check. *) +module IndexParamOff; +check off; + +const N = 8; + +type + Bytes* = [N]U8; + Shorts* = [N]I16; + Words* = [N]U32; + Longs* = [N]I64; + +(* a sum of a's elements, picked by indices worked out several ways *) +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; + +(* each element of a made twice the one before, plus d[i] *) +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; + +(* the bytes of a turned round, two at a time from either 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; + +end IndexParamOff.