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.
| @@ -372,3 +372,16 @@ | ||
| 372 | 372 | is no variable. `T` is refused by the view's `notPlain`. The result |
| 373 | 373 | keeps `x`'s `frame`, so the bytes of a local are caught by |
| 374 | 374 | `notOfFrame` as far as a slice of it is. |
| 375 | +- `FILL(d, x)` (report 2026-10.36) takes `d` as `COPY` does | |
| 376 | + (`destSlice`), then `x` as `:=` would, with `:=`'s messages, read | |
| 377 | + once before any element is written. A scalar element of 1, 2, 4 or 8 | |
| 378 | + bytes is stored from `rax`, a real's bits as an integer's: by a store | |
| 379 | + each into an array of 16 elements or fewer, else by a loop for fewer | |
| 380 | + than 16 at run time and `rep stos` of its width from 16 up. A | |
| 381 | + constant zero into an array is `zeroBytes`, as `NEW`'s zeroing is; | |
| 382 | + into a slice it is stored as any scalar, which is as fast. Another | |
| 383 | + element is copied into `d[0]` and forward from there by `rep movsb`. | |
| 384 | + A record literal of zeros isn't seen as a zero. A const parameter's | |
| 385 | + or another module's array, as `d`, says why it can't be changed, for | |
| 386 | + `COPY` too. `seqSlice` copies no slice that is a temporary already | |
| 387 | + (a slice just cut): nothing `s` or `x` does can change it. |
| @@ -0,0 +1,20 @@ | ||
| 1 | +module FillInto; | |
| 2 | +import Owner; | |
| 3 | +const A: [3]I64 = [1, 2, 3]; | |
| 4 | +proc p(const a: [3]I64); | |
| 5 | +begin | |
| 6 | + FILL(a, 0); | |
| 7 | +end; | |
| 8 | +proc main*; | |
| 9 | +var a: [3]I64; x: I64; s: []U8; | |
| 10 | +begin | |
| 11 | + FILL("abc", 0); | |
| 12 | + FILL(x, 0); | |
| 13 | + FILL(A, 0); | |
| 14 | + FILL(Owner.a, 0); | |
| 15 | + FILL(a, "abc"); | |
| 16 | + FILL(a, 1.5); | |
| 17 | + FILL(s, 256); | |
| 18 | + FILL(a, U8(1)); | |
| 19 | +end; | |
| 20 | +end FillInto. |
| @@ -0,0 +1,16 @@ | ||
| 1 | +U8 254 0 | |
| 2 | +U16 254 0 | |
| 3 | +U32 254 0 | |
| 4 | +U64 254 0 | |
| 5 | +I8 254 0 | |
| 6 | +BOOL 254 0 | |
| 7 | +pointer 254 0 | |
| 8 | +set 254 0 | |
| 9 | +enumeration 254 0 | |
| 10 | +3-byte record 254 0 | |
| 11 | +24-byte record 254 0 | |
| 12 | +2 66 | |
| 13 | +3 0 spanspan | |
| 14 | +900 | |
| 15 | +45 | |
| 16 | +0 2 45 zero |
| @@ -0,0 +1,89 @@ | ||
| 1 | +module Fill; | |
| 2 | +import Out, FillKinds, U8s := Filled(U8), U16s := Filled(U16), U32s := Filled(U32), | |
| 3 | + U64s := Filled(U64), I8s := Filled(I8), Bools := Filled(BOOL), Ptrs := Filled(^I64), | |
| 4 | + Sets := Filled(FillKinds.Colours), Colours := Filled(FillKinds.Colour), | |
| 5 | + Rgbs := Filled(FillKinds.Rgb), Spans := Filled(FillKinds.Span); | |
| 6 | + | |
| 7 | +var | |
| 8 | + calls: INT; (* how many times next has been called *) | |
| 9 | + a: [9]U32; big: [100]U64; s: []U32; ps: [3]^I64; spans: [5]FillKinds.Span; | |
| 10 | + x, y: I64; | |
| 11 | + | |
| 12 | +(* 7, counting the call *) | |
| 13 | +proc next(): U32; | |
| 14 | +begin | |
| 15 | + INC(calls); | |
| 16 | + return 7; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +(* A span of n, counting the call *) | |
| 20 | +proc span(n: I64): FillKinds.Span; | |
| 21 | +begin | |
| 22 | + INC(calls); | |
| 23 | + return {lo: n, hi: -n, tag: "spanspan"}; | |
| 24 | +end; | |
| 25 | + | |
| 26 | +(* The sum of the elements of s *) | |
| 27 | +proc sum(s: []U32): I64; | |
| 28 | +var i: INT; t: I64; | |
| 29 | +begin | |
| 30 | + t := 0; | |
| 31 | + for i := 0 to LEN(s) - 1 do INC(t, I64(s[i])); end; | |
| 32 | + return t; | |
| 33 | +end; | |
| 34 | + | |
| 35 | +(* An array a var parameter gives *) | |
| 36 | +proc fives(var b: [9]U32); | |
| 37 | +begin | |
| 38 | + FILL(b, 5); | |
| 39 | +end; | |
| 40 | + | |
| 41 | +proc main*; | |
| 42 | +var i: INT; t: I64; p: ^[100]U64; | |
| 43 | +begin | |
| 44 | + U8s.check("U8", 0A5H, 1); | |
| 45 | + U16s.check("U16", 0A55AH, 1); | |
| 46 | + U32s.check("U32", 0DEADBEEFH, 1); | |
| 47 | + U64s.check("U64", 0123456789ABCDEFH, 1); | |
| 48 | + I8s.check("I8", -2, 3); | |
| 49 | + Bools.check("BOOL", true, false); | |
| 50 | + Ptrs.check("pointer", ADR(x), ADR(y)); | |
| 51 | + Sets.check("set", {FillKinds.red, FillKinds.blue}, {FillKinds.green}); | |
| 52 | + Colours.check("enumeration", FillKinds.blue, FillKinds.green); | |
| 53 | + Rgbs.check("3-byte record", {r: 1, g: 2, b: 3}, {r: 9, g: 9, b: 9}); | |
| 54 | + Spans.check("24-byte record", {lo: 1, hi: 2, tag: "abcdefgh"}, {lo: -1, hi: -2, tag: "zzzzzzzz"}); | |
| 55 | + | |
| 56 | + (* x is evaluated once, before any element is written *) | |
| 57 | + FILL(a, next()); | |
| 58 | + s := a; | |
| 59 | + FILL(s[2..5], next() + 1); | |
| 60 | + Out.int(calls); Out.char(' '); Out.int(sum(a)); Out.ln; | |
| 61 | + FILL(spans, span(4)); | |
| 62 | + Out.int(calls); Out.char(' '); Out.int(spans[0].lo + spans[4].hi); Out.char(' '); | |
| 63 | + Out.str(spans[4].tag[0..8]); Out.ln; | |
| 64 | + a[3] := 100; | |
| 65 | + FILL(a, a[3]); (* read once, though FILL writes a[3] *) | |
| 66 | + Out.int(sum(a)); Out.ln; | |
| 67 | + fives(a); | |
| 68 | + Out.int(sum(a)); Out.ln; | |
| 69 | + | |
| 70 | + (* zeros *) | |
| 71 | + FILL(a, 0); | |
| 72 | + Out.int(sum(a)); Out.char(' '); | |
| 73 | + FILL(a, 1); | |
| 74 | + FILL(s[1..8], 0); | |
| 75 | + Out.int(sum(a)); Out.char(' '); | |
| 76 | + for i := 0 to 99 do big[i] := U64(i); end; | |
| 77 | + p := ADR(big); | |
| 78 | + FILL(p^[10..100], 0); | |
| 79 | + t := 0; | |
| 80 | + for i := 0 to 99 do INC(t, I64(big[i])); end; | |
| 81 | + Out.int(t); Out.char(' '); | |
| 82 | + FILL(p^, 0); | |
| 83 | + FILL(ps, ADR(x)); | |
| 84 | + FILL(ps, nil); | |
| 85 | + if (big[1] = 0) and (ps[2] = nil) then Out.str("zero"); end; | |
| 86 | + Out.ln; | |
| 87 | +end; | |
| 88 | + | |
| 89 | +end Fill. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +F32 254 0 | |
| 2 | +F64 254 0 | |
| 3 | +0 2 -0 |
| @@ -0,0 +1,25 @@ | ||
| 1 | +module FillReals; | |
| 2 | +import Out, F32s := Filled(F32), F64s := Filled(F64); | |
| 3 | + | |
| 4 | +var window: [576]F32; w: []F64; ws: [3]F64; | |
| 5 | + | |
| 6 | +proc main*; | |
| 7 | +var i: INT; t: F64; | |
| 8 | +begin | |
| 9 | + F32s.check("F32", 1.5, -2.25); | |
| 10 | + F64s.check("F64", -1.0E300, 3.5); | |
| 11 | + | |
| 12 | + for i := 0 to 575 do window[i] := F32(i); end; | |
| 13 | + FILL(window, 0.0); | |
| 14 | + t := 0.0; | |
| 15 | + for i := 0 to 575 do t := t + F64(window[i]); end; | |
| 16 | + Out.int(I64(t)); Out.char(' '); | |
| 17 | + w := ws; | |
| 18 | + FILL(w, 0.25); | |
| 19 | + FILL(w[1..2], -0.0); (* not zero bits *) | |
| 20 | + Out.int(I64(4.0 * (ws[0] + ws[2]))); Out.char(' '); | |
| 21 | + if (ws[1] = 0.0) and (1.0 / ws[1] < 0.0) then Out.str("-0"); end; | |
| 22 | + Out.ln; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +end FillReals. |
An empty file.
| @@ -0,0 +1,10 @@ | ||
| 1 | +(* Types for the test Fill, which imports Filled with each *) | |
| 2 | +module FillKinds; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Colour* = (red, green, blue); | |
| 6 | + Colours* = set of Colour; | |
| 7 | + Rgb* = record r*, g*, b*: U8; end; (* 3 bytes *) | |
| 8 | + Span* = record lo*, hi*: I64; tag*: [8]U8; end; (* 24 bytes *) | |
| 9 | + | |
| 10 | +end FillKinds. |
| @@ -0,0 +1,93 @@ | ||
| 1 | +(* A generic module for the test Fill: FILL of slices of T, and of | |
| 2 | + arrays of T, of 0 to 100 elements, each with elements around it that | |
| 3 | + must keep what they held. Elements are compared by their bytes, so T | |
| 4 | + may be a record, which = doesn't compare. *) | |
| 5 | +module Filled(T); | |
| 6 | +import Out; | |
| 7 | + | |
| 8 | +(* The lengths of the slices filled *) | |
| 9 | +const Lens: [7]INT = [0, 1, 2, 7, 8, 9, 100]; | |
| 10 | + | |
| 11 | +(* An array of n elements of T, with one before it and one after it *) | |
| 12 | +type | |
| 13 | + G1 = record pre: T; a: [1]T; post: T; end; | |
| 14 | + G2 = record pre: T; a: [2]T; post: T; end; | |
| 15 | + G7 = record pre: T; a: [7]T; post: T; end; | |
| 16 | + G8 = record pre: T; a: [8]T; post: T; end; | |
| 17 | + G9 = record pre: T; a: [9]T; post: T; end; | |
| 18 | + G100 = record pre: T; a: [100]T; post: T; end; | |
| 19 | + | |
| 20 | +var | |
| 21 | + buf: [102]T; (* slices are cut from it, from its second element *) | |
| 22 | + g1: G1; g2: G2; g7: G7; g8: G8; g9: G9; g100: G100; | |
| 23 | + filled: INT; (* elements filled, and checked *) | |
| 24 | + wrong: INT; (* elements that hold what they shouldn't *) | |
| 25 | + | |
| 26 | +(* The bytes of the T at p: BYTES takes no BOOL, set or enumeration *) | |
| 27 | +proc bytesAt(p: ^T): []U8; | |
| 28 | +var q: ^U8; | |
| 29 | +begin | |
| 30 | + q := ^U8(p); | |
| 31 | + return q[0..SIZE(T)]; | |
| 32 | +end; | |
| 33 | + | |
| 34 | +(* Counts the elements of a that aren't v *) | |
| 35 | +proc count(a: []T; v: T); | |
| 36 | +var i: INT; | |
| 37 | +begin | |
| 38 | + for i := 0 to LEN(a) - 1 do | |
| 39 | + if bytesAt(ADR(a[i])) # bytesAt(ADR(v)) then INC(wrong); end; | |
| 40 | + end; | |
| 41 | +end; | |
| 42 | + | |
| 43 | +(* Sets every element of a to s, by a loop *) | |
| 44 | +proc reset(a: []T; s: T); | |
| 45 | +var i: INT; | |
| 46 | +begin | |
| 47 | + for i := 0 to LEN(a) - 1 do a[i] := s; end; | |
| 48 | +end; | |
| 49 | + | |
| 50 | +(* Fills slices of buf with v, the rest s *) | |
| 51 | +proc slices(v, s: T); | |
| 52 | +var k, n: INT; | |
| 53 | +begin | |
| 54 | + for k := 0 to LEN(Lens) - 1 do | |
| 55 | + n := Lens[k]; | |
| 56 | + reset(buf, s); | |
| 57 | + FILL(buf[1..1 + n], v); | |
| 58 | + count(buf[0..1], s); | |
| 59 | + count(buf[1..1 + n], v); | |
| 60 | + count(buf[1 + n..102], s); | |
| 61 | + INC(filled, n); | |
| 62 | + end; | |
| 63 | +end; | |
| 64 | + | |
| 65 | +(* Fills each array with v, its guards and what it held before s *) | |
| 66 | +proc arrays(v, s: T); | |
| 67 | +begin | |
| 68 | + g1.pre := s; g1.post := s; reset(g1.a, s); FILL(g1.a, v); | |
| 69 | + count(g1.a, v); count([g1.pre, g1.post], s); INC(filled, LEN(g1.a)); | |
| 70 | + g2.pre := s; g2.post := s; reset(g2.a, s); FILL(g2.a, v); | |
| 71 | + count(g2.a, v); count([g2.pre, g2.post], s); INC(filled, LEN(g2.a)); | |
| 72 | + g7.pre := s; g7.post := s; reset(g7.a, s); FILL(g7.a, v); | |
| 73 | + count(g7.a, v); count([g7.pre, g7.post], s); INC(filled, LEN(g7.a)); | |
| 74 | + g8.pre := s; g8.post := s; reset(g8.a, s); FILL(g8.a, v); | |
| 75 | + count(g8.a, v); count([g8.pre, g8.post], s); INC(filled, LEN(g8.a)); | |
| 76 | + g9.pre := s; g9.post := s; reset(g9.a, s); FILL(g9.a, v); | |
| 77 | + count(g9.a, v); count([g9.pre, g9.post], s); INC(filled, LEN(g9.a)); | |
| 78 | + g100.pre := s; g100.post := s; reset(g100.a, s); FILL(g100.a, v); | |
| 79 | + count(g100.a, v); count([g100.pre, g100.post], s); INC(filled, LEN(g100.a)); | |
| 80 | +end; | |
| 81 | + | |
| 82 | +(* Fills slices and arrays with v, around s, and says how many elements | |
| 83 | + were filled and how many are wrong, after name *) | |
| 84 | +proc check*(name: []U8; v, s: T); | |
| 85 | +begin | |
| 86 | + filled := 0; | |
| 87 | + wrong := 0; | |
| 88 | + slices(v, s); | |
| 89 | + arrays(v, s); | |
| 90 | + Out.str(name); Out.char(' '); Out.int(filled); Out.char(' '); Out.int(wrong); Out.ln; | |
| 91 | +end; | |
| 92 | + | |
| 93 | +end Filled. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is eleven files of raz, about 11,500 lines of code (the | |
| 3 | +The compiler is eleven files of raz, about 12,200 lines of code (the | |
| 4 | 4 | budget is 14,000). It is its own compiler: a release of it, the one |
| 5 | 5 | `BOOT` names, builds `src/`, and `make fixpoint` checks that what it |
| 6 | 6 | makes makes itself again, byte for byte. Each release was built by the |
| @@ -18,12 +18,12 @@ | ||
| 18 | 18 | |---|---|---|---| |
| 19 | 19 | | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | |
| 20 | 20 | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | |
| 21 | -| `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout | | |
| 21 | +| `src/Sym.raz` | 725 | 583 | Types, objects, scopes, layout | | |
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | -| `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | | |
| 24 | -| `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 25 | -| `src/Item.raz` | 2,381 | 1,912 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | -| `src/Parse.raz` | 4,518 | 3,890 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 23 | +| `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | | |
| 24 | +| `src/Gen.raz` | 2,797 | 1,961 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 25 | +| `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | +| `src/Parse.raz` | 4,528 | 3,900 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -314,7 +314,7 @@ | ||
| 314 | 314 | `indexSlice` and `slice` compare with the length's and add the |
| 315 | 315 | pointer's, a failed check takes the length from its register |
| 316 | 316 | (`inRegister`), and `copyTo` loads both. What wants a slice's |
| 317 | - memory, a comparison or `COPY`, has its words stored in a | |
| 317 | + memory, a comparison, `COPY` or `FILL`, has its words stored in a | |
| 318 | 318 | temporary first (`copyToTemp`). rat 5% faster, the heap sort 5%, |
| 319 | 319 | the search 4%. |
| 320 | 320 | |
| @@ -330,7 +330,7 @@ | ||
| 330 | 330 | ``` |
| 331 | 331 | |
| 332 | 332 | In a procedure that names no procedure, a procedure variable, `NEW`, |
| 333 | - `SYSCALL` or `COPY`, as `chooseRegs` sees reading ahead, the | |
| 333 | + `SYSCALL`, `COPY` or `FILL`, as `chooseRegs` sees reading ahead, the | |
| 334 | 334 | parameters that arrive in `rdi` and `rsi` stay there, which only a |
| 335 | 335 | call or a copy needs: nothing is saved or moved, and `s[i]` is read |
| 336 | 336 | through `rdi` itself (`keepArrived`). Where Gen needs them after |
| @@ -1091,6 +1091,7 @@ | ||
| 1091 | 1091 | `copyBlock`, `copyTo`, `zeroVar`, and `copySlice` for `COPY`, a `rep |
| 1092 | 1092 | movsb`, or a loop going down (`copyDown`) when the destination starts |
| 1093 | 1093 | inside the source; the small ones by moves (`copyBytes`, `zeroBytes`); |
| 1094 | + and `fillSlice` for `FILL` (`fillScalar`, `fillCopy`); | |
| 1094 | 1095 | - jumps: `jump`, `jumpIf` (on a `BOOL`), `jumpCmp` (on two integers), |
| 1095 | 1096 | `jumpOut` (`jumpCmp` from within an expression) and `condValue` for |
| 1096 | 1097 | `and` and `or`, `fix` where the label is, and `joinJumps`; |
| @@ -1148,6 +1149,78 @@ | ||
| 1148 | 1149 | 576 ms to 71 at 10 elements, 10,900 to 410 at 1,000, as fast as |
| 1149 | 1150 | downward; and one a single element up inside itself, 539 to 57 and |
| 1150 | 1151 | 10,800 to 1,600. |
| 1152 | + | |
| 1153 | +`FILL(d, x)` makes `d` a slice in the frame, as `COPY` does | |
| 1154 | +(`Item.fillDest`), then `x`, of `d`'s element type, and stores it in | |
| 1155 | +each element (`fillSlice`). An element of 1, 2, 4 or 8 bytes, a scalar, | |
| 1156 | +goes into `rax`, a real's bits as an integer's, and is stored with | |
| 1157 | +`rep stos` of its width. That takes some thirty cycles to start, twice | |
| 1158 | +the time of ten stores, so a short slice is filled by a loop instead, | |
| 1159 | +and an array of 16 elements or fewer by a store each (`fillScalar`): | |
| 1160 | + | |
| 1161 | +``` | |
| 1162 | +mov rax, r8 ; x | |
| 1163 | +mov rdi, [rbp - 48] ; d's pointer and length | |
| 1164 | +mov rcx, [rbp - 40] | |
| 1165 | +cmp rcx, 16 | |
| 1166 | +jae many | |
| 1167 | +test rcx, rcx | |
| 1168 | +jz out | |
| 1169 | +top: mov [rdi], eax | |
| 1170 | +add rdi, 4 | |
| 1171 | +sub rcx, 1 | |
| 1172 | +jne top | |
| 1173 | +jmp out | |
| 1174 | +many: rep stosd | |
| 1175 | +out: | |
| 1176 | +``` | |
| 1177 | + | |
| 1178 | +A constant zero into an array is its bytes zeroed, by `zeroBytes`, as | |
| 1179 | +a variable's are. Any other element, a record or an array, is copied | |
| 1180 | +into `d[0]` from `x`'s address, then `d[0..n - 1]`'s bytes into | |
| 1181 | +`d[1..n]`'s by `rep movsb`, forward (`fillCopy`): each byte it reads it | |
| 1182 | +wrote `SIZE(T)` bytes before, so `d[0]` repeats through the rest. | |
| 1183 | + | |
| 1184 | +``` | |
| 1185 | +mov rdx, [rbp - 40] ; the length | |
| 1186 | +test rdx, rdx | |
| 1187 | +jz out | |
| 1188 | +... ; copyBytes: d[0] := x, from [rsi] | |
| 1189 | +mov rsi, [rbp - 48] ; from d[0] | |
| 1190 | +lea rdi, [rsi + 24] ; to d[1], for a 24-byte record | |
| 1191 | +lea rcx, [rdx - 1] | |
| 1192 | +imul rcx, 24 | |
| 1193 | +rep movsb | |
| 1194 | +out: | |
| 1195 | +``` | |
| 1196 | + | |
| 1197 | +`x` is read once, before any element is written, so `FILL(a, a[3])` | |
| 1198 | +fills `a` with what `a[3]` held. Ten million fills of a row of | |
| 1199 | +`U32`s at one of 64 places in a `[4096]U32`, as rat paints a cell's | |
| 1200 | +background, against the loop in a procedure of its own: 52 ms to 37 at 10 | |
| 1201 | +elements, 2,500 to 490 at 1,000. | |
| 1202 | + | |
| 1203 | +At 10 it was 65 ms, slower than the loop, until `seqSlice`, which | |
| 1204 | +gives `COPY`, `FILL` and a comparison their slices in the frame, | |
| 1205 | +stopped copying a slice that is already a temporary of its own, as one | |
| 1206 | +just cut is. It had copied every slice, lest `s` or `x` change a | |
| 1207 | +variable it read, and so the pointer and length went through memory | |
| 1208 | +twice before `rdi` and `rcx` had them: | |
| 1209 | + | |
| 1210 | +``` | |
| 1211 | +mov [rbp - 48], rax ; band[at..at + n], cut | |
| 1212 | +lea rsi, [rbp - 48] | |
| 1213 | +lea rdi, [rbp - 64] | |
| 1214 | +mov rcx, [rsi] ; copied | |
| 1215 | +mov [rdi], rcx | |
| 1216 | +mov rcx, [rsi + 8] | |
| 1217 | +mov [rdi + 8], rcx | |
| 1218 | +... | |
| 1219 | +mov rdi, [rbp - 64] | |
| 1220 | +mov rcx, [rbp - 56] | |
| 1221 | +``` | |
| 1222 | + | |
| 1223 | +Now `rdi` and `rcx` are loaded from `[rbp - 48]` and `[rbp - 40]`. | |
| 1151 | 1224 | |
| 1152 | 1225 | A variable's place is an `Adr`: relative to the frame (`bFrame`), to a |
| 1153 | 1226 | chunk (`bGlobal`), to an address on the stack (`bStack`), or to one |
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | |
| 34 | 34 | (defconst raz-builtins |
| 35 | 35 | '("SIZE" "OFFSET" "ADR" "LEN" "PTR" "BYTES" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" |
| 36 | - "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX" | |
| 36 | + "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX" | |
| 37 | 37 | "CLZ" "CTZ" "POPCOUNT")) |
| 38 | 38 | |
| 39 | 39 | (defvar raz-mode-syntax-table |
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | syntax keyword razKeyword var variant VOLATILE while xor |
| 16 | 16 | syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR |
| 17 | 17 | syntax keyword razBuiltin SIZE OFFSET ADR LEN PTR BYTES NEW MSG SYSCALL FENCE CAS XCHG XADD |
| 18 | -syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX | |
| 18 | +syntax keyword razBuiltin ARGC ARGV INC DEC COPY FILL ROL ROR MIN MAX | |
| 19 | 19 | syntax keyword razBuiltin CLZ CTZ POPCOUNT |
| 20 | 20 | syntax keyword razConstant true false nil |
| 21 | 21 |
| @@ -796,7 +796,7 @@ | ||
| 796 | 796 | if size > MaxMoves then |
| 797 | 797 | X.movRI(RAX, 0); |
| 798 | 798 | X.movRI(RCX, size); |
| 799 | - X.repStosb(); | |
| 799 | + X.repStos(1); | |
| 800 | 800 | return; |
| 801 | 801 | end; |
| 802 | 802 | o := 0; |
| @@ -1351,7 +1351,7 @@ | ||
| 1351 | 1351 | X.movRR(RDI, r); |
| 1352 | 1352 | X.movRI(RCX, size); |
| 1353 | 1353 | X.movRI(RAX, 0); |
| 1354 | - X.repStosb(); | |
| 1354 | + X.repStos(1); | |
| 1355 | 1355 | X.fixLink(L); |
| 1356 | 1356 | end; |
| 1357 | 1357 | |
| @@ -1900,7 +1900,116 @@ | ||
| 1900 | 1900 | X.repMovsb(); |
| 1901 | 1901 | X.fixLink(E); |
| 1902 | 1902 | end; |
| 1903 | + | |
| 1904 | +(* The most elements FILL stores one by one: as for MaxMoves, a store | |
| 1905 | + each costs less than rep stos takes to start, which at 10 elements | |
| 1906 | + is twice the time. *) | |
| 1907 | +const FewFills = 16; | |
| 1908 | + | |
| 1909 | +(* rax, an element of size bytes, 1, 2, 4 or 8, into each of the | |
| 1910 | + elements of the slice in the frame at dOff, of which there are n if | |
| 1911 | + n isn't -1. Few, as of a short array, are a store each; else, at run | |
| 1912 | + time, a loop of stores for fewer than FewFills, or rep stos of the | |
| 1913 | + element's width: | |
| 1903 | 1914 | |
| 1915 | + mov rdi, [rbp + dOff] | |
| 1916 | + mov rcx, [rbp + dOff + 8] | |
| 1917 | + cmp rcx, 16 | |
| 1918 | + jae many | |
| 1919 | + test rcx, rcx | |
| 1920 | + jz out | |
| 1921 | + top: mov [rdi], eax ; for a U32 | |
| 1922 | + add rdi, 4 | |
| 1923 | + sub rcx, 1 | |
| 1924 | + jne top | |
| 1925 | + jmp out | |
| 1926 | + many: rep stosd | |
| 1927 | + out: *) | |
| 1928 | +proc fillScalar(dOff, size, n: I64); | |
| 1929 | +var i: I64; many, out, top: INT; | |
| 1930 | +begin | |
| 1931 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 1932 | + if (n >= 0) and (n <= FewFills) then | |
| 1933 | + for i := 0 to n - 1 do X.store(X.mem(RDI, i * size), RAX, INT(size)); end; | |
| 1934 | + return; | |
| 1935 | + end; | |
| 1936 | + X.load(RCX, X.mem(RBP, dOff + 8), 8, false); | |
| 1937 | + many := NoLabel; | |
| 1938 | + out := NoLabel; | |
| 1939 | + X.aluI(X.aluCmp, RCX, FewFills); | |
| 1940 | + X.jcc(X.ccAE, many); | |
| 1941 | + X.testRR(RCX, RCX); | |
| 1942 | + X.jcc(X.ccE, out); | |
| 1943 | + top := Chunk.pc; | |
| 1944 | + X.store(X.mem(RDI, 0), RAX, INT(size)); | |
| 1945 | + X.aluI(X.aluAdd, RDI, INT(size)); | |
| 1946 | + X.aluI(X.aluSub, RCX, 1); | |
| 1947 | + X.jccBack(X.ccNE, top); | |
| 1948 | + X.jmp(out); | |
| 1949 | + X.fixLink(many); | |
| 1950 | + X.repStos(INT(size)); | |
| 1951 | + X.fixLink(out); | |
| 1952 | +end; | |
| 1953 | + | |
| 1954 | +(* The size bytes at rsi into each element of the slice in the frame at | |
| 1955 | + dOff: into the first by copyBytes, and from there forward by rep | |
| 1956 | + movsb, from d[0] on to d[1] on. A forward copy reads each byte after it has written it, size | |
| 1957 | + bytes before, so the first element repeats through the rest: | |
| 1958 | + | |
| 1959 | + mov rdi, [rbp + dOff] | |
| 1960 | + mov rdx, [rbp + dOff + 8] | |
| 1961 | + test rdx, rdx | |
| 1962 | + jz out ; nothing to fill | |
| 1963 | + ... ; copyBytes(size): d[0] := x | |
| 1964 | + mov rsi, [rbp + dOff] | |
| 1965 | + lea rdi, [rsi + size] | |
| 1966 | + lea rcx, [rdx - 1] | |
| 1967 | + imul rcx, size ; the bytes of d[1..n] | |
| 1968 | + rep movsb | |
| 1969 | + out: *) | |
| 1970 | +proc fillCopy(dOff, size: I64); | |
| 1971 | +var out: INT; | |
| 1972 | +begin | |
| 1973 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 1974 | + X.load(RDX, X.mem(RBP, dOff + 8), 8, false); | |
| 1975 | + X.testRR(RDX, RDX); | |
| 1976 | + out := NoLabel; | |
| 1977 | + X.jcc(X.ccE, out); | |
| 1978 | + copyBytes(size); | |
| 1979 | + X.load(RSI, X.mem(RBP, dOff), 8, false); | |
| 1980 | + X.lea(RDI, X.mem(RSI, size)); | |
| 1981 | + X.lea(RCX, X.mem(RDX, -1)); | |
| 1982 | + mulI(RCX, size); | |
| 1983 | + X.repMovsb(); | |
| 1984 | + X.fixLink(out); | |
| 1985 | +end; | |
| 1986 | + | |
| 1987 | +(* FILL: every element of the slice in the frame at dOff, of size bytes | |
| 1988 | + each, set to the value on top of the stack if the element is a | |
| 1989 | + scalar, or else to the size bytes at the address on top. n is how | |
| 1990 | + many elements the slice has if that is known, as for an array, else | |
| 1991 | + -1. A scalar goes into rax and is stored as it is, a real's bits as | |
| 1992 | + an integer's (fillScalar); but a constant zero into an array of n | |
| 1993 | + elements is n * size zero bytes, as NEW's zeroing is: by moves, or | |
| 1994 | + rep stosb (zeroBytes). Any other element is copied (fillCopy). *) | |
| 1995 | +proc fillSlice*(dOff, size, n: I64; scalar: BOOL); | |
| 1996 | +var isConst: BOOL; v: I64; | |
| 1997 | +begin | |
| 1998 | + clobberArgs(); | |
| 1999 | + isConst, v := topConst(); | |
| 2000 | + if scalar and isConst and (v = 0) and (n >= 0) then | |
| 2001 | + drop(); | |
| 2002 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 2003 | + zeroBytes(n * size); | |
| 2004 | + elsif scalar then | |
| 2005 | + popTo(RAX); | |
| 2006 | + fillScalar(dOff, size, n); | |
| 2007 | + else | |
| 2008 | + popTo(RSI); | |
| 2009 | + fillCopy(dOff, size); | |
| 2010 | + end; | |
| 2011 | +end; | |
| 2012 | + | |
| 1904 | 2013 | (* Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its |
| 1905 | 2014 | bounds can't be known, but it can't run backwards. *) |
| 1906 | 2015 | proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos); |
| @@ -2472,7 +2581,7 @@ | ||
| 2472 | 2581 | X.load(RDI, X.mem(RBP, retSlot), 8, false); |
| 2473 | 2582 | X.movRI(RCX, resType^.size); |
| 2474 | 2583 | X.movRI(RAX, 0); |
| 2475 | - X.repStosb(); | |
| 2584 | + X.repStos(1); | |
| 2476 | 2585 | X.load(RAX, X.mem(RBP, retSlot), 8, false); |
| 2477 | 2586 | else |
| 2478 | 2587 | X.movRI(RAX, 0); |
| @@ -1158,7 +1158,9 @@ | ||
| 1158 | 1158 | t := Sym.newType(Sym.fSlice, 16, 8); |
| 1159 | 1159 | t^.base := elemOf(x.typ); |
| 1160 | 1160 | if x.typ^.form = Sym.fSlice then |
| 1161 | - x.typ := t; try copyToTemp(x); | |
| 1161 | + (* a temporary, as a slice just cut is, nothing else can change *) | |
| 1162 | + if (x.mode # mVar) or (x.base # Gen.bFrame) or x.isVar or (x.obj # nil) then try copyToTemp(x); end; | |
| 1163 | + x.typ := t; | |
| 1162 | 1164 | elsif x.mode = mStr then |
| 1163 | 1165 | try address(x); sliceOf(x, t, x.val); |
| 1164 | 1166 | else |
| @@ -2326,15 +2328,46 @@ | ||
| 2326 | 2328 | return nil; |
| 2327 | 2329 | end; |
| 2328 | 2330 | |
| 2329 | -(* COPY(d, s): d, an array variable or a slice, as a slice in the frame, | |
| 2330 | - before s is read *) | |
| 2331 | -proc copyDest*(var d: Item): ERROR; | |
| 2331 | +(* COPY(d, s) or FILL(d, x): d, an array variable or a slice, as a slice | |
| 2332 | + in the frame, before s or x is read; text says what the built-in | |
| 2333 | + writes into, for an error *) | |
| 2334 | +proc destSlice(var d: Item; text: []U8): ERROR; | |
| 2332 | 2335 | begin |
| 2336 | + if (d.typ^.form = Sym.fArray) and (d.mode = mVar) then try notReadOnly(d); end; | |
| 2333 | 2337 | if not isSeq(d.typ) or (d.mode = mStr) or ((d.typ^.form = Sym.fArray) and ((d.mode # mVar) or not d.isVar)) then |
| 2334 | - return Scan.markAt(d.pos, say1("COPY copies into an array variable or a slice, found {}", what(d))); | |
| 2338 | + return Scan.markAt(d.pos, say1(text, what(d))); | |
| 2335 | 2339 | end; |
| 2336 | - if d.typ^.form = Sym.fArray then try notReadOnly(d); end; | |
| 2337 | 2340 | return seqSlice(d); |
| 2341 | +end; | |
| 2342 | + | |
| 2343 | +proc copyDest*(var d: Item): ERROR; | |
| 2344 | +begin | |
| 2345 | + return destSlice(d, "COPY copies into an array variable or a slice, found {}"); | |
| 2346 | +end; | |
| 2347 | + | |
| 2348 | +proc fillDest*(var d: Item): ERROR; | |
| 2349 | +begin | |
| 2350 | + return destSlice(d, "FILL fills an array variable or a slice, found {}"); | |
| 2351 | +end; | |
| 2352 | + | |
| 2353 | +(* ... and each of its elements set to x, as by :=; t is d's type as it | |
| 2354 | + was written, so that an array's length is known *) | |
| 2355 | +proc fillWith*(var d, x: Item; t: ^Sym.Type): ERROR; | |
| 2356 | +var e: ^Sym.Type; n: I64; | |
| 2357 | +begin | |
| 2358 | + e := d.typ^.base; | |
| 2359 | + try coerce(x, e); | |
| 2360 | + if Sym.isScalar(e) then | |
| 2361 | + try value(x); | |
| 2362 | + else | |
| 2363 | + if x.vol then return Scan.markAt(x.pos, "can't copy through a volatile pointer"); end; | |
| 2364 | + if (x.mode = mVar) and (x.base = Gen.bReg) then try copyToTemp(x); end; (* a slice in registers *) | |
| 2365 | + try address(x); | |
| 2366 | + end; | |
| 2367 | + n := -1; | |
| 2368 | + if t^.form = Sym.fArray then n := t^.len; end; | |
| 2369 | + Gen.fillSlice(d.off, e^.size, n, Sym.isScalar(e)); | |
| 2370 | + return nil; | |
| 2338 | 2371 | end; |
| 2339 | 2372 | |
| 2340 | 2373 | (* ... and the elements of s copied to its start *) |
| @@ -1173,6 +1173,15 @@ | ||
| 1173 | 1173 | try Item.copyFrom(x, y); |
| 1174 | 1174 | Item.makeConst(x, Sym.tNone, 0); |
| 1175 | 1175 | x.mode := Item.mNone; |
| 1176 | + | Sym.bFill: | |
| 1177 | + try expression(x, nil); | |
| 1178 | + t := x.typ; | |
| 1179 | + try Item.fillDest(x); | |
| 1180 | + try expect(Scan.sComma); | |
| 1181 | + try expression(y, x.typ^.base); | |
| 1182 | + try Item.fillWith(x, y, t); | |
| 1183 | + Item.makeConst(x, Sym.tNone, 0); | |
| 1184 | + x.mode := Item.mNone; | |
| 1176 | 1185 | | else: |
| 1177 | 1186 | end; |
| 1178 | 1187 | return expect(Scan.sRparen); |
| @@ -2873,12 +2882,13 @@ | ||
| 2873 | 2882 | end; |
| 2874 | 2883 | |
| 2875 | 2884 | (* Whether naming o may make a call: a procedure, a procedure variable, |
| 2876 | - or NEW, SYSCALL or COPY, which use the registers a call would *) | |
| 2885 | + or NEW, SYSCALL, COPY or FILL, which use the registers a call would *) | |
| 2877 | 2886 | proc calls(o: PObj): BOOL; |
| 2878 | 2887 | begin |
| 2879 | 2888 | if o = nil then return false; end; |
| 2880 | 2889 | return ((o^.cls = Sym.oProc) and (o^.inl = 0)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) |
| 2881 | - or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy))); | |
| 2890 | + or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy) | |
| 2891 | + or (o^.val = Sym.bFill))); | |
| 2882 | 2892 | end; |
| 2883 | 2893 | |
| 2884 | 2894 | (* Puts the busiest variables of the procedure, whose parameters are |
| @@ -133,7 +133,7 @@ | ||
| 133 | 133 | bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; |
| 134 | 134 | bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; |
| 135 | 135 | bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22; |
| 136 | - bOffset* = 23; bBytes* = 24; | |
| 136 | + bOffset* = 23; bBytes* = 24; bFill* = 25; | |
| 137 | 137 | |
| 138 | 138 | MaxDepth* = 64; (* scopes open at once *) |
| 139 | 139 | |
| @@ -714,6 +714,7 @@ | ||
| 714 | 714 | enter("INC", oBuiltin, nil, bInc); |
| 715 | 715 | enter("DEC", oBuiltin, nil, bDec); |
| 716 | 716 | enter("COPY", oBuiltin, nil, bCopy); |
| 717 | + enter("FILL", oBuiltin, nil, bFill); | |
| 717 | 718 | enter("ROL", oBuiltin, nil, bRol); |
| 718 | 719 | enter("ROR", oBuiltin, nil, bRor); |
| 719 | 720 | enter("MIN", oBuiltin, nil, bMin); |
| @@ -692,9 +692,20 @@ | ||
| 692 | 692 | say("rep movsb"); Chunk.put1(0F3H); Chunk.put1(0A4H); |
| 693 | 693 | end; |
| 694 | 694 | |
| 695 | -proc repStosb*; | |
| 695 | +(* rep stos of an element of n bytes, 1, 2, 4 or 8: al, ax, eax or rax | |
| 696 | + into rcx elements from [rdi] up. The width is that of stosb, with 66H | |
| 697 | + for a word, or with REX.W for a quadword. *) | |
| 698 | +proc repStos*(n: INT); | |
| 696 | 699 | begin |
| 697 | - say("rep stosb"); Chunk.put1(0F3H); Chunk.put1(0AAH); | |
| 700 | + match n of | |
| 701 | + | 1: say("rep stosb"); | |
| 702 | + | 2: say("rep stosw"); Chunk.put1(66H); | |
| 703 | + | 4: say("rep stosd"); | |
| 704 | + | else: say("rep stosq"); | |
| 705 | + end; | |
| 706 | + Chunk.put1(0F3H); | |
| 707 | + if n = 8 then Chunk.put1(48H); end; | |
| 708 | + if n = 1 then Chunk.put1(0AAH); else Chunk.put1(0ABH); end; | |
| 698 | 709 | end; |
| 699 | 710 | |
| 700 | 711 | proc repeCmpsb*; |
| @@ -0,0 +1,30 @@ | ||
| 1 | +conform/err/FillInto.raz:6:10: error: a is only read here, and can't be changed | |
| 2 | + 6 | FILL(a, 0); | |
| 3 | + | ^ | |
| 4 | +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it | |
| 5 | +conform/err/FillInto.raz:11:10: error: FILL fills an array variable or a slice, found a string | |
| 6 | + 11 | FILL("abc", 0); | |
| 7 | + | ^^^^^ | |
| 8 | +conform/err/FillInto.raz:12:10: error: FILL fills an array variable or a slice, found I64 | |
| 9 | + 12 | FILL(x, 0); | |
| 10 | + | ^ | |
| 11 | +conform/err/FillInto.raz:13:10: error: FILL fills an array variable or a slice, found [3]I64 | |
| 12 | + 13 | FILL(A, 0); | |
| 13 | + | ^ | |
| 14 | +conform/err/FillInto.raz:14:10: error: Owner.a is Owner's, and can't be changed outside it | |
| 15 | + 14 | FILL(Owner.a, 0); | |
| 16 | + | ^^^^^^^ | |
| 17 | +conform/err/FillInto.raz:15:13: error: can't use a string as I64 | |
| 18 | + 15 | FILL(a, "abc"); | |
| 19 | + | ^^^^^ | |
| 20 | +conform/err/FillInto.raz:16:13: error: can't use a real constant as I64 | |
| 21 | + 16 | FILL(a, 1.5); | |
| 22 | + | ^^^ | |
| 23 | +conform/err/FillInto.raz:17:13: error: 256 doesn't fit U8 | |
| 24 | + 17 | FILL(s, 256); | |
| 25 | + | ^^^ | |
| 26 | +conform/err/FillInto.raz:18:13: error: can't use U8 as I64 | |
| 27 | + 18 | FILL(a, U8(1)); | |
| 28 | + | ^^^^^ | |
| 29 | +help: U8 widens to I64 only as written, I64(U8(1)) | |
| 30 | +raz: 9 errors |