From e19ed1d818a35ae4531ebb1bab5433901172a76b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:21:28 +0100 Subject: [PATCH] FILL(d, x): every element of an array or slice set to x, as report 2026-10.36 has it: rep stos of the element's width, a loop of stores for a short slice, a store each for a short array, and a record copied forward by rep movsb (10M fills of 10 U32s 52 ms to 37 against the loop, of 1,000 2,500 to 490) seqSlice copies no slice that is a temporary already, which a slice just cut is (FILL of 10 U32s 65 ms to 37). A const parameter's or another module's array, as COPY's or FILL's destination, says why it can't be changed. X.repStos(n) replaces repStosb. --- diff --git a/CLAUDE.md b/CLAUDE.md index 63f1109..d64846d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -372,3 +372,16 @@ is no variable. `T` is refused by the view's `notPlain`. The result keeps `x`'s `frame`, so the bytes of a local are caught by `notOfFrame` as far as a slice of it is. +- `FILL(d, x)` (report 2026-10.36) takes `d` as `COPY` does + (`destSlice`), then `x` as `:=` would, with `:=`'s messages, read + once before any element is written. A scalar element of 1, 2, 4 or 8 + bytes is stored from `rax`, a real's bits as an integer's: by a store + each into an array of 16 elements or fewer, else by a loop for fewer + than 16 at run time and `rep stos` of its width from 16 up. A + constant zero into an array is `zeroBytes`, as `NEW`'s zeroing is; + into a slice it is stored as any scalar, which is as fast. Another + element is copied into `d[0]` and forward from there by `rep movsb`. + A record literal of zeros isn't seen as a zero. A const parameter's + or another module's array, as `d`, says why it can't be changed, for + `COPY` too. `seqSlice` copies no slice that is a temporary already + (a slice just cut): nothing `s` or `x` does can change it. diff --git a/conform/err/FillInto.raz b/conform/err/FillInto.raz new file mode 100644 index 0000000..86dda6f --- /dev/null +++ b/conform/err/FillInto.raz @@ -0,0 +1,20 @@ +module FillInto; +import Owner; +const A: [3]I64 = [1, 2, 3]; +proc p(const a: [3]I64); +begin + FILL(a, 0); +end; +proc main*; +var a: [3]I64; x: I64; s: []U8; +begin + FILL("abc", 0); + FILL(x, 0); + FILL(A, 0); + FILL(Owner.a, 0); + FILL(a, "abc"); + FILL(a, 1.5); + FILL(s, 256); + FILL(a, U8(1)); +end; +end FillInto. diff --git a/conform/run/Fill.expect b/conform/run/Fill.expect new file mode 100644 index 0000000..fb59c6d --- /dev/null +++ b/conform/run/Fill.expect @@ -0,0 +1,16 @@ +U8 254 0 +U16 254 0 +U32 254 0 +U64 254 0 +I8 254 0 +BOOL 254 0 +pointer 254 0 +set 254 0 +enumeration 254 0 +3-byte record 254 0 +24-byte record 254 0 +2 66 +3 0 spanspan +900 +45 +0 2 45 zero diff --git a/conform/run/Fill.raz b/conform/run/Fill.raz new file mode 100644 index 0000000..ce03928 --- /dev/null +++ b/conform/run/Fill.raz @@ -0,0 +1,89 @@ +module Fill; +import Out, FillKinds, U8s := Filled(U8), U16s := Filled(U16), U32s := Filled(U32), + U64s := Filled(U64), I8s := Filled(I8), Bools := Filled(BOOL), Ptrs := Filled(^I64), + Sets := Filled(FillKinds.Colours), Colours := Filled(FillKinds.Colour), + Rgbs := Filled(FillKinds.Rgb), Spans := Filled(FillKinds.Span); + +var + calls: INT; (* how many times next has been called *) + a: [9]U32; big: [100]U64; s: []U32; ps: [3]^I64; spans: [5]FillKinds.Span; + x, y: I64; + +(* 7, counting the call *) +proc next(): U32; +begin + INC(calls); + return 7; +end; + +(* A span of n, counting the call *) +proc span(n: I64): FillKinds.Span; +begin + INC(calls); + return {lo: n, hi: -n, tag: "spanspan"}; +end; + +(* The sum of the elements of s *) +proc sum(s: []U32): I64; +var i: INT; t: I64; +begin + t := 0; + for i := 0 to LEN(s) - 1 do INC(t, I64(s[i])); end; + return t; +end; + +(* An array a var parameter gives *) +proc fives(var b: [9]U32); +begin + FILL(b, 5); +end; + +proc main*; +var i: INT; t: I64; p: ^[100]U64; +begin + U8s.check("U8", 0A5H, 1); + U16s.check("U16", 0A55AH, 1); + U32s.check("U32", 0DEADBEEFH, 1); + U64s.check("U64", 0123456789ABCDEFH, 1); + I8s.check("I8", -2, 3); + Bools.check("BOOL", true, false); + Ptrs.check("pointer", ADR(x), ADR(y)); + Sets.check("set", {FillKinds.red, FillKinds.blue}, {FillKinds.green}); + Colours.check("enumeration", FillKinds.blue, FillKinds.green); + Rgbs.check("3-byte record", {r: 1, g: 2, b: 3}, {r: 9, g: 9, b: 9}); + Spans.check("24-byte record", {lo: 1, hi: 2, tag: "abcdefgh"}, {lo: -1, hi: -2, tag: "zzzzzzzz"}); + + (* x is evaluated once, before any element is written *) + FILL(a, next()); + s := a; + FILL(s[2..5], next() + 1); + Out.int(calls); Out.char(' '); Out.int(sum(a)); Out.ln; + FILL(spans, span(4)); + Out.int(calls); Out.char(' '); Out.int(spans[0].lo + spans[4].hi); Out.char(' '); + Out.str(spans[4].tag[0..8]); Out.ln; + a[3] := 100; + FILL(a, a[3]); (* read once, though FILL writes a[3] *) + Out.int(sum(a)); Out.ln; + fives(a); + Out.int(sum(a)); Out.ln; + + (* zeros *) + FILL(a, 0); + Out.int(sum(a)); Out.char(' '); + FILL(a, 1); + FILL(s[1..8], 0); + Out.int(sum(a)); Out.char(' '); + for i := 0 to 99 do big[i] := U64(i); end; + p := ADR(big); + FILL(p^[10..100], 0); + t := 0; + for i := 0 to 99 do INC(t, I64(big[i])); end; + Out.int(t); Out.char(' '); + FILL(p^, 0); + FILL(ps, ADR(x)); + FILL(ps, nil); + if (big[1] = 0) and (ps[2] = nil) then Out.str("zero"); end; + Out.ln; +end; + +end Fill. diff --git a/conform/run/FillReals.expect b/conform/run/FillReals.expect new file mode 100644 index 0000000..b537790 --- /dev/null +++ b/conform/run/FillReals.expect @@ -0,0 +1,3 @@ +F32 254 0 +F64 254 0 +0 2 -0 diff --git a/conform/run/FillReals.raz b/conform/run/FillReals.raz new file mode 100644 index 0000000..8995068 --- /dev/null +++ b/conform/run/FillReals.raz @@ -0,0 +1,25 @@ +module FillReals; +import Out, F32s := Filled(F32), F64s := Filled(F64); + +var window: [576]F32; w: []F64; ws: [3]F64; + +proc main*; +var i: INT; t: F64; +begin + F32s.check("F32", 1.5, -2.25); + F64s.check("F64", -1.0E300, 3.5); + + for i := 0 to 575 do window[i] := F32(i); end; + FILL(window, 0.0); + t := 0.0; + for i := 0 to 575 do t := t + F64(window[i]); end; + Out.int(I64(t)); Out.char(' '); + w := ws; + FILL(w, 0.25); + FILL(w[1..2], -0.0); (* not zero bits *) + Out.int(I64(4.0 * (ws[0] + ws[2]))); Out.char(' '); + if (ws[1] = 0.0) and (1.0 / ws[1] < 0.0) then Out.str("-0"); end; + Out.ln; +end; + +end FillReals. diff --git a/conform/run/FillReals.reals b/conform/run/FillReals.reals new file mode 100644 index 0000000..e69de29 diff --git a/conform/run/mods/FillKinds.raz b/conform/run/mods/FillKinds.raz new file mode 100644 index 0000000..4db9b6f --- /dev/null +++ b/conform/run/mods/FillKinds.raz @@ -0,0 +1,10 @@ +(* Types for the test Fill, which imports Filled with each *) +module FillKinds; + +type + Colour* = (red, green, blue); + Colours* = set of Colour; + Rgb* = record r*, g*, b*: U8; end; (* 3 bytes *) + Span* = record lo*, hi*: I64; tag*: [8]U8; end; (* 24 bytes *) + +end FillKinds. diff --git a/conform/run/mods/Filled.raz b/conform/run/mods/Filled.raz new file mode 100644 index 0000000..1e06894 --- /dev/null +++ b/conform/run/mods/Filled.raz @@ -0,0 +1,93 @@ +(* A generic module for the test Fill: FILL of slices of T, and of + arrays of T, of 0 to 100 elements, each with elements around it that + must keep what they held. Elements are compared by their bytes, so T + may be a record, which = doesn't compare. *) +module Filled(T); +import Out; + +(* The lengths of the slices filled *) +const Lens: [7]INT = [0, 1, 2, 7, 8, 9, 100]; + +(* An array of n elements of T, with one before it and one after it *) +type + G1 = record pre: T; a: [1]T; post: T; end; + G2 = record pre: T; a: [2]T; post: T; end; + G7 = record pre: T; a: [7]T; post: T; end; + G8 = record pre: T; a: [8]T; post: T; end; + G9 = record pre: T; a: [9]T; post: T; end; + G100 = record pre: T; a: [100]T; post: T; end; + +var + buf: [102]T; (* slices are cut from it, from its second element *) + g1: G1; g2: G2; g7: G7; g8: G8; g9: G9; g100: G100; + filled: INT; (* elements filled, and checked *) + wrong: INT; (* elements that hold what they shouldn't *) + +(* The bytes of the T at p: BYTES takes no BOOL, set or enumeration *) +proc bytesAt(p: ^T): []U8; +var q: ^U8; +begin + q := ^U8(p); + return q[0..SIZE(T)]; +end; + +(* Counts the elements of a that aren't v *) +proc count(a: []T; v: T); +var i: INT; +begin + for i := 0 to LEN(a) - 1 do + if bytesAt(ADR(a[i])) # bytesAt(ADR(v)) then INC(wrong); end; + end; +end; + +(* Sets every element of a to s, by a loop *) +proc reset(a: []T; s: T); +var i: INT; +begin + for i := 0 to LEN(a) - 1 do a[i] := s; end; +end; + +(* Fills slices of buf with v, the rest s *) +proc slices(v, s: T); +var k, n: INT; +begin + for k := 0 to LEN(Lens) - 1 do + n := Lens[k]; + reset(buf, s); + FILL(buf[1..1 + n], v); + count(buf[0..1], s); + count(buf[1..1 + n], v); + count(buf[1 + n..102], s); + INC(filled, n); + end; +end; + +(* Fills each array with v, its guards and what it held before s *) +proc arrays(v, s: T); +begin + g1.pre := s; g1.post := s; reset(g1.a, s); FILL(g1.a, v); + count(g1.a, v); count([g1.pre, g1.post], s); INC(filled, LEN(g1.a)); + g2.pre := s; g2.post := s; reset(g2.a, s); FILL(g2.a, v); + count(g2.a, v); count([g2.pre, g2.post], s); INC(filled, LEN(g2.a)); + g7.pre := s; g7.post := s; reset(g7.a, s); FILL(g7.a, v); + count(g7.a, v); count([g7.pre, g7.post], s); INC(filled, LEN(g7.a)); + g8.pre := s; g8.post := s; reset(g8.a, s); FILL(g8.a, v); + count(g8.a, v); count([g8.pre, g8.post], s); INC(filled, LEN(g8.a)); + g9.pre := s; g9.post := s; reset(g9.a, s); FILL(g9.a, v); + count(g9.a, v); count([g9.pre, g9.post], s); INC(filled, LEN(g9.a)); + g100.pre := s; g100.post := s; reset(g100.a, s); FILL(g100.a, v); + count(g100.a, v); count([g100.pre, g100.post], s); INC(filled, LEN(g100.a)); +end; + +(* Fills slices and arrays with v, around s, and says how many elements + were filled and how many are wrong, after name *) +proc check*(name: []U8; v, s: T); +begin + filled := 0; + wrong := 0; + slices(v, s); + arrays(v, s); + Out.str(name); Out.char(' '); Out.int(filled); Out.char(' '); Out.int(wrong); Out.ln; +end; + +end Filled. diff --git a/doc/compiler.md b/doc/compiler.md index cc3197c..112ccf6 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of raz, about 11,500 lines of code (the +The compiler is eleven files of raz, about 12,200 lines of code (the budget is 14,000). It is its own compiler: a release of it, the one `BOOT` names, builds `src/`, and `make fixpoint` checks that what it makes makes itself again, byte for byte. Each release was built by the @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout | +| `src/Sym.raz` | 725 | 583 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | -| `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,381 | 1,912 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,518 | 3,890 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `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/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,528 | 3,900 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -314,7 +314,7 @@ `indexSlice` and `slice` compare with the length's and add the pointer's, a failed check takes the length from its register (`inRegister`), and `copyTo` loads both. What wants a slice's - memory, a comparison or `COPY`, has its words stored in a + memory, a comparison, `COPY` or `FILL`, has its words stored in a temporary first (`copyToTemp`). rat 5% faster, the heap sort 5%, the search 4%. @@ -330,7 +330,7 @@ ``` In a procedure that names no procedure, a procedure variable, `NEW`, - `SYSCALL` or `COPY`, as `chooseRegs` sees reading ahead, the + `SYSCALL`, `COPY` or `FILL`, as `chooseRegs` sees reading ahead, the parameters that arrive in `rdi` and `rsi` stay there, which only a call or a copy needs: nothing is saved or moved, and `s[i]` is read through `rdi` itself (`keepArrived`). Where Gen needs them after @@ -1091,6 +1091,7 @@ `copyBlock`, `copyTo`, `zeroVar`, and `copySlice` for `COPY`, a `rep movsb`, or a loop going down (`copyDown`) when the destination starts inside the source; the small ones by moves (`copyBytes`, `zeroBytes`); + and `fillSlice` for `FILL` (`fillScalar`, `fillCopy`); - jumps: `jump`, `jumpIf` (on a `BOOL`), `jumpCmp` (on two integers), `jumpOut` (`jumpCmp` from within an expression) and `condValue` for `and` and `or`, `fix` where the label is, and `joinJumps`; @@ -1148,6 +1149,78 @@ 576 ms to 71 at 10 elements, 10,900 to 410 at 1,000, as fast as downward; and one a single element up inside itself, 539 to 57 and 10,800 to 1,600. + +`FILL(d, x)` makes `d` a slice in the frame, as `COPY` does +(`Item.fillDest`), then `x`, of `d`'s element type, and stores it in +each element (`fillSlice`). An element of 1, 2, 4 or 8 bytes, a scalar, +goes into `rax`, a real's bits as an integer's, and is stored with +`rep stos` of its width. That takes some thirty cycles to start, twice +the time of ten stores, so a short slice is filled by a loop instead, +and an array of 16 elements or fewer by a store each (`fillScalar`): + +``` +mov rax, r8 ; x +mov rdi, [rbp - 48] ; d's pointer and length +mov rcx, [rbp - 40] +cmp rcx, 16 +jae many +test rcx, rcx +jz out +top: mov [rdi], eax +add rdi, 4 +sub rcx, 1 +jne top +jmp out +many: rep stosd +out: +``` + +A constant zero into an array is its bytes zeroed, by `zeroBytes`, as +a variable's are. Any other element, a record or an array, is copied +into `d[0]` from `x`'s address, then `d[0..n - 1]`'s bytes into +`d[1..n]`'s by `rep movsb`, forward (`fillCopy`): each byte it reads it +wrote `SIZE(T)` bytes before, so `d[0]` repeats through the rest. + +``` +mov rdx, [rbp - 40] ; the length +test rdx, rdx +jz out +... ; copyBytes: d[0] := x, from [rsi] +mov rsi, [rbp - 48] ; from d[0] +lea rdi, [rsi + 24] ; to d[1], for a 24-byte record +lea rcx, [rdx - 1] +imul rcx, 24 +rep movsb +out: +``` + +`x` is read once, before any element is written, so `FILL(a, a[3])` +fills `a` with what `a[3]` held. Ten million fills of a row of +`U32`s at one of 64 places in a `[4096]U32`, as rat paints a cell's +background, against the loop in a procedure of its own: 52 ms to 37 at 10 +elements, 2,500 to 490 at 1,000. + +At 10 it was 65 ms, slower than the loop, until `seqSlice`, which +gives `COPY`, `FILL` and a comparison their slices in the frame, +stopped copying a slice that is already a temporary of its own, as one +just cut is. It had copied every slice, lest `s` or `x` change a +variable it read, and so the pointer and length went through memory +twice before `rdi` and `rcx` had them: + +``` +mov [rbp - 48], rax ; band[at..at + n], cut +lea rsi, [rbp - 48] +lea rdi, [rbp - 64] +mov rcx, [rsi] ; copied +mov [rdi], rcx +mov rcx, [rsi + 8] +mov [rdi + 8], rcx +... +mov rdi, [rbp - 64] +mov rcx, [rbp - 56] +``` + +Now `rdi` and `rcx` are loaded from `[rbp - 48]` and `[rbp - 40]`. A variable's place is an `Adr`: relative to the frame (`bFrame`), to a chunk (`bGlobal`), to an address on the stack (`bStack`), or to one diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index 32aa3ae..056edaa 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -33,7 +33,7 @@ (defconst raz-builtins '("SIZE" "OFFSET" "ADR" "LEN" "PTR" "BYTES" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" - "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX" + "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX" "CLZ" "CTZ" "POPCOUNT")) (defvar raz-mode-syntax-table diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index d27adeb..c9608ce 100644 --- a/editor/vim/syntax/raz.vim +++ b/editor/vim/syntax/raz.vim @@ -15,7 +15,7 @@ syntax keyword razKeyword var variant VOLATILE while xor syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR syntax keyword razBuiltin SIZE OFFSET ADR LEN PTR BYTES NEW MSG SYSCALL FENCE CAS XCHG XADD -syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX +syntax keyword razBuiltin ARGC ARGV INC DEC COPY FILL ROL ROR MIN MAX syntax keyword razBuiltin CLZ CTZ POPCOUNT syntax keyword razConstant true false nil diff --git a/src/Gen.raz b/src/Gen.raz index 55ffecb..00f2fa4 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -796,7 +796,7 @@ if size > MaxMoves then X.movRI(RAX, 0); X.movRI(RCX, size); - X.repStosb(); + X.repStos(1); return; end; o := 0; @@ -1351,7 +1351,7 @@ X.movRR(RDI, r); X.movRI(RCX, size); X.movRI(RAX, 0); - X.repStosb(); + X.repStos(1); X.fixLink(L); end; @@ -1900,7 +1900,116 @@ X.repMovsb(); X.fixLink(E); end; + +(* The most elements FILL stores one by one: as for MaxMoves, a store + each costs less than rep stos takes to start, which at 10 elements + is twice the time. *) +const FewFills = 16; + +(* rax, an element of size bytes, 1, 2, 4 or 8, into each of the + elements of the slice in the frame at dOff, of which there are n if + n isn't -1. Few, as of a short array, are a store each; else, at run + time, a loop of stores for fewer than FewFills, or rep stos of the + element's width: + mov rdi, [rbp + dOff] + mov rcx, [rbp + dOff + 8] + cmp rcx, 16 + jae many + test rcx, rcx + jz out + top: mov [rdi], eax ; for a U32 + add rdi, 4 + sub rcx, 1 + jne top + jmp out + many: rep stosd + out: *) +proc fillScalar(dOff, size, n: I64); +var i: I64; many, out, top: INT; +begin + X.load(RDI, X.mem(RBP, dOff), 8, false); + if (n >= 0) and (n <= FewFills) then + for i := 0 to n - 1 do X.store(X.mem(RDI, i * size), RAX, INT(size)); end; + return; + end; + X.load(RCX, X.mem(RBP, dOff + 8), 8, false); + many := NoLabel; + out := NoLabel; + X.aluI(X.aluCmp, RCX, FewFills); + X.jcc(X.ccAE, many); + X.testRR(RCX, RCX); + X.jcc(X.ccE, out); + top := Chunk.pc; + X.store(X.mem(RDI, 0), RAX, INT(size)); + X.aluI(X.aluAdd, RDI, INT(size)); + X.aluI(X.aluSub, RCX, 1); + X.jccBack(X.ccNE, top); + X.jmp(out); + X.fixLink(many); + X.repStos(INT(size)); + X.fixLink(out); +end; + +(* The size bytes at rsi into each element of the slice in the frame at + dOff: into the first by copyBytes, and from there forward by rep + movsb, from d[0] on to d[1] on. A forward copy reads each byte after it has written it, size + bytes before, so the first element repeats through the rest: + + mov rdi, [rbp + dOff] + mov rdx, [rbp + dOff + 8] + test rdx, rdx + jz out ; nothing to fill + ... ; copyBytes(size): d[0] := x + mov rsi, [rbp + dOff] + lea rdi, [rsi + size] + lea rcx, [rdx - 1] + imul rcx, size ; the bytes of d[1..n] + rep movsb + out: *) +proc fillCopy(dOff, size: I64); +var out: INT; +begin + X.load(RDI, X.mem(RBP, dOff), 8, false); + X.load(RDX, X.mem(RBP, dOff + 8), 8, false); + X.testRR(RDX, RDX); + out := NoLabel; + X.jcc(X.ccE, out); + copyBytes(size); + X.load(RSI, X.mem(RBP, dOff), 8, false); + X.lea(RDI, X.mem(RSI, size)); + X.lea(RCX, X.mem(RDX, -1)); + mulI(RCX, size); + X.repMovsb(); + X.fixLink(out); +end; + +(* FILL: every element of the slice in the frame at dOff, of size bytes + each, set to the value on top of the stack if the element is a + scalar, or else to the size bytes at the address on top. n is how + many elements the slice has if that is known, as for an array, else + -1. A scalar goes into rax and is stored as it is, a real's bits as + an integer's (fillScalar); but a constant zero into an array of n + elements is n * size zero bytes, as NEW's zeroing is: by moves, or + rep stosb (zeroBytes). Any other element is copied (fillCopy). *) +proc fillSlice*(dOff, size, n: I64; scalar: BOOL); +var isConst: BOOL; v: I64; +begin + clobberArgs(); + isConst, v := topConst(); + if scalar and isConst and (v = 0) and (n >= 0) then + drop(); + X.load(RDI, X.mem(RBP, dOff), 8, false); + zeroBytes(n * size); + elsif scalar then + popTo(RAX); + fillScalar(dOff, size, n); + else + popTo(RSI); + fillCopy(dOff, size); + end; +end; + (* Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its bounds can't be known, but it can't run backwards. *) proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos); @@ -2472,7 +2581,7 @@ X.load(RDI, X.mem(RBP, retSlot), 8, false); X.movRI(RCX, resType^.size); X.movRI(RAX, 0); - X.repStosb(); + X.repStos(1); X.load(RAX, X.mem(RBP, retSlot), 8, false); else X.movRI(RAX, 0); diff --git a/src/Item.raz b/src/Item.raz index 8b773c6..db19a99 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1158,7 +1158,9 @@ t := Sym.newType(Sym.fSlice, 16, 8); t^.base := elemOf(x.typ); if x.typ^.form = Sym.fSlice then - x.typ := t; try copyToTemp(x); + (* a temporary, as a slice just cut is, nothing else can change *) + if (x.mode # mVar) or (x.base # Gen.bFrame) or x.isVar or (x.obj # nil) then try copyToTemp(x); end; + x.typ := t; elsif x.mode = mStr then try address(x); sliceOf(x, t, x.val); else @@ -2326,15 +2328,46 @@ return nil; end; -(* COPY(d, s): d, an array variable or a slice, as a slice in the frame, - before s is read *) -proc copyDest*(var d: Item): ERROR; +(* COPY(d, s) or FILL(d, x): d, an array variable or a slice, as a slice + in the frame, before s or x is read; text says what the built-in + writes into, for an error *) +proc destSlice(var d: Item; text: []U8): ERROR; begin + if (d.typ^.form = Sym.fArray) and (d.mode = mVar) then try notReadOnly(d); end; if not isSeq(d.typ) or (d.mode = mStr) or ((d.typ^.form = Sym.fArray) and ((d.mode # mVar) or not d.isVar)) then - return Scan.markAt(d.pos, say1("COPY copies into an array variable or a slice, found {}", what(d))); + return Scan.markAt(d.pos, say1(text, what(d))); end; - if d.typ^.form = Sym.fArray then try notReadOnly(d); end; return seqSlice(d); +end; + +proc copyDest*(var d: Item): ERROR; +begin + return destSlice(d, "COPY copies into an array variable or a slice, found {}"); +end; + +proc fillDest*(var d: Item): ERROR; +begin + return destSlice(d, "FILL fills an array variable or a slice, found {}"); +end; + +(* ... and each of its elements set to x, as by :=; t is d's type as it + was written, so that an array's length is known *) +proc fillWith*(var d, x: Item; t: ^Sym.Type): ERROR; +var e: ^Sym.Type; n: I64; +begin + e := d.typ^.base; + try coerce(x, e); + if Sym.isScalar(e) then + try value(x); + else + if x.vol then return Scan.markAt(x.pos, "can't copy through a volatile pointer"); end; + if (x.mode = mVar) and (x.base = Gen.bReg) then try copyToTemp(x); end; (* a slice in registers *) + try address(x); + end; + n := -1; + if t^.form = Sym.fArray then n := t^.len; end; + Gen.fillSlice(d.off, e^.size, n, Sym.isScalar(e)); + return nil; end; (* ... and the elements of s copied to its start *) diff --git a/src/Parse.raz b/src/Parse.raz index 24bdb9a..a087b80 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1173,6 +1173,15 @@ try Item.copyFrom(x, y); Item.makeConst(x, Sym.tNone, 0); x.mode := Item.mNone; + | Sym.bFill: + try expression(x, nil); + t := x.typ; + try Item.fillDest(x); + try expect(Scan.sComma); + try expression(y, x.typ^.base); + try Item.fillWith(x, y, t); + Item.makeConst(x, Sym.tNone, 0); + x.mode := Item.mNone; | else: end; return expect(Scan.sRparen); @@ -2873,12 +2882,13 @@ end; (* Whether naming o may make a call: a procedure, a procedure variable, - or NEW, SYSCALL or COPY, which use the registers a call would *) + or NEW, SYSCALL, COPY or FILL, which use the registers a call would *) proc calls(o: PObj): BOOL; begin if o = nil then return false; end; return ((o^.cls = Sym.oProc) and (o^.inl = 0)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) - or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy))); + or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy) + or (o^.val = Sym.bFill))); end; (* Puts the busiest variables of the procedure, whose parameters are diff --git a/src/Sym.raz b/src/Sym.raz index 9ed3ad5..274df57 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -133,7 +133,7 @@ bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22; - bOffset* = 23; bBytes* = 24; + bOffset* = 23; bBytes* = 24; bFill* = 25; MaxDepth* = 64; (* scopes open at once *) @@ -714,6 +714,7 @@ enter("INC", oBuiltin, nil, bInc); enter("DEC", oBuiltin, nil, bDec); enter("COPY", oBuiltin, nil, bCopy); + enter("FILL", oBuiltin, nil, bFill); enter("ROL", oBuiltin, nil, bRol); enter("ROR", oBuiltin, nil, bRor); enter("MIN", oBuiltin, nil, bMin); diff --git a/src/X64.raz b/src/X64.raz index 477972d..524770a 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -692,9 +692,20 @@ say("rep movsb"); Chunk.put1(0F3H); Chunk.put1(0A4H); end; -proc repStosb*; +(* rep stos of an element of n bytes, 1, 2, 4 or 8: al, ax, eax or rax + into rcx elements from [rdi] up. The width is that of stosb, with 66H + for a word, or with REX.W for a quadword. *) +proc repStos*(n: INT); begin - say("rep stosb"); Chunk.put1(0F3H); Chunk.put1(0AAH); + match n of + | 1: say("rep stosb"); + | 2: say("rep stosw"); Chunk.put1(66H); + | 4: say("rep stosd"); + | else: say("rep stosq"); + end; + Chunk.put1(0F3H); + if n = 8 then Chunk.put1(48H); end; + if n = 1 then Chunk.put1(0AAH); else Chunk.put1(0ABH); end; end; proc repeCmpsb*; diff --git a/test/msg/FillInto.err b/test/msg/FillInto.err new file mode 100644 index 0000000..167ce97 --- /dev/null +++ b/test/msg/FillInto.err @@ -0,0 +1,30 @@ +conform/err/FillInto.raz:6:10: error: a is only read here, and can't be changed + 6 | FILL(a, 0); + | ^ +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it +conform/err/FillInto.raz:11:10: error: FILL fills an array variable or a slice, found a string + 11 | FILL("abc", 0); + | ^^^^^ +conform/err/FillInto.raz:12:10: error: FILL fills an array variable or a slice, found I64 + 12 | FILL(x, 0); + | ^ +conform/err/FillInto.raz:13:10: error: FILL fills an array variable or a slice, found [3]I64 + 13 | FILL(A, 0); + | ^ +conform/err/FillInto.raz:14:10: error: Owner.a is Owner's, and can't be changed outside it + 14 | FILL(Owner.a, 0); + | ^^^^^^^ +conform/err/FillInto.raz:15:13: error: can't use a string as I64 + 15 | FILL(a, "abc"); + | ^^^^^ +conform/err/FillInto.raz:16:13: error: can't use a real constant as I64 + 16 | FILL(a, 1.5); + | ^^^ +conform/err/FillInto.raz:17:13: error: 256 doesn't fit U8 + 17 | FILL(s, 256); + | ^^^ +conform/err/FillInto.raz:18:13: error: can't use U8 as I64 + 18 | FILL(a, U8(1)); + | ^^^^^ +help: U8 widens to I64 only as written, I64(U8(1)) +raz: 9 errors