Code procedures take and return slices (report 2026-10.18) CodeBody lets a value parameter be a slice, counting as two of the six integer registers, and the single result a slice, in rax and rdx; the call side already passed them so. Simd's index takes a slice; Asm gains tail and pick; the compiler follows 2026-10.18.
| @@ -99,13 +99,14 @@ | ||
| 99 | 99 | defect, and the library grows only by what breaks no program written |
| 100 | 100 | for it. A wish for more, the owner's or yours, goes on the list in |
| 101 | 101 | `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, |
| 102 | -by the owner's word: report 2026-10.12; the compiler follows 2026-10.17, | |
| 102 | +by the owner's word: report 2026-10.12; the compiler follows 2026-10.18, | |
| 103 | 103 | whose `Sys` writes without waiting, for rat's pastes (10.13), and |
| 104 | 104 | whose blank, `_ := f(x)`, drops a result that is otherwise warned of |
| 105 | 105 | (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, |
| 106 | 106 | rounding and the inverse functions, and `Sys` `space` (10.15), and |
| 107 | 107 | whose constants may be records and tables of them, with `Str.copy` |
| 108 | -(10.16), and `COPY` (10.17), also by the owner's word. Its parser | |
| 108 | +(10.16), and `COPY` (10.17), also by the owner's word, and whose code | |
| 109 | +procedures take and return slices (10.18). Its parser | |
| 109 | 110 | reaches the machine only through `gen.pas`. Keep |
| 110 | 111 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 111 | 112 | compiler changes. |
| @@ -832,7 +832,9 @@ | ||
| 832 | 832 | Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` |
| 833 | 833 | and `extern`, each one through `Declaration`, a recovery point, which |
| 834 | 834 | calls `ConstDecl`, `TypeDecl`, `VarDecl`, `ProcDecl` or `ExternDecl`. `ProcDecl` handles forward declarations, code procedures |
| 835 | -(`CodeBody`, which checks the signature and has gen's `CodeProc` run | |
| 835 | +(`CodeBody`, which checks the signature, a slice parameter counting as | |
| 836 | +two of the six integer registers and a slice result going in rax and | |
| 837 | +rdx, and has gen's `CodeProc` run | |
| 836 | 838 | the target's assembler) and bodies (`Body`). |
| 837 | 839 | `ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`, |
| 838 | 840 | `EnumType`) build `TType`s. |
| @@ -2141,9 +2141,12 @@ | ||
| 2141 | 2141 | ni := 0; nf := 0; |
| 2142 | 2142 | q := sig^.fields; |
| 2143 | 2143 | while q <> nil do begin |
| 2144 | - if not q^.isVar and not IsScalar(q^.typ) then | |
| 2145 | - MarkAt(at, 'a code procedure takes integers, BOOL, reals, pointers and procedures'); | |
| 2146 | - if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); | |
| 2144 | + if not q^.isVar and (q^.typ^.form = fSlice) then inc(ni, 2) { address, then length } | |
| 2145 | + else begin | |
| 2146 | + if not q^.isVar and not IsScalar(q^.typ) then | |
| 2147 | + MarkAt(at, 'a code procedure takes integers, BOOL, reals, pointers, procedures and slices'); | |
| 2148 | + if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); | |
| 2149 | + end; | |
| 2147 | 2150 | q := q^.next; |
| 2148 | 2151 | end; |
| 2149 | 2152 | if (ni > 6) or (nf > 8) or sig^.variadic then MarkAt(at, 'too many parameters for a code procedure'); |
| @@ -2154,7 +2157,8 @@ | ||
| 2154 | 2157 | or not IsScalar(q^.typ) or not IsScalar(q^.next^.typ) |
| 2155 | 2158 | or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then |
| 2156 | 2159 | MarkAt(at, 'a code procedure returns two results only in rax and rdx'); |
| 2157 | - end else if not IsScalar(sig^.base) then MarkAt(at, 'a code procedure returns a single value'); | |
| 2160 | + end else if not IsScalar(sig^.base) and (sig^.base^.form <> fSlice) then | |
| 2161 | + MarkAt(at, 'a code procedure returns a single value'); | |
| 2158 | 2162 | CodeProc(p^.chunk, @AsmConst); |
| 2159 | 2163 | Expect(sEnd); |
| 2160 | 2164 | end; |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 35 | 35 | 0.3.0-5-g38d29a8 five commits past it } |
| 36 | 36 | Version = {$I version.inc}; |
| 37 | - Report = '2026-10.17'; { the version of doc/raz.md it follows } | |
| 37 | + Report = '2026-10.18'; { the version of doc/raz.md it follows } | |
| 38 | 38 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 39 | 39 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 40 | 40 |
| @@ -0,0 +1,4 @@ | ||
| 1 | +test/err/StmtCodeSlices.raz:2:6: error: too many parameters for a code procedure | |
| 2 | + 2 | proc f(a, b, c, d, e: I64; s: []U8); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module StmtCodeSlices; | |
| 2 | +proc f(a, b, c, d, e: I64; s: []U8); | |
| 3 | +code ret; end; | |
| 4 | +end StmtCodeSlices. |
| @@ -1,4 +1,4 @@ | ||
| 1 | -test/err/StmtCodeTakes.raz:3:6: error: a code procedure takes integers, BOOL, reals, pointers and procedures | |
| 1 | +test/err/StmtCodeTakes.raz:3:6: error: a code procedure takes integers, BOOL, reals, pointers, procedures and slices | |
| 2 | 2 | 3 | proc f(r: R); |
| 3 | 3 | | ^ |
| 4 | 4 | raz: 1 error |
| @@ -7,3 +7,5 @@ | ||
| 7 | 7 | 13 |
| 8 | 8 | 40 42 |
| 9 | 9 | 42 42 |
| 10 | +cdef 0 | |
| 11 | +100 -1 |
| @@ -120,8 +120,31 @@ | ||
| 120 | 120 | db 0C3H; |
| 121 | 121 | end answer; |
| 122 | 122 | |
| 123 | +(* s without its first k bytes: s in rdi and rsi, k in rdx, the slice back in rax and rdx *) | |
| 124 | +proc tail(s: []U8; k: INT): []U8; | |
| 125 | +code | |
| 126 | + add rdi, rdx; | |
| 127 | + sub rsi, rdx; | |
| 128 | + mov rax, rdi; | |
| 129 | + mov rdx, rsi; | |
| 130 | + ret; | |
| 131 | +end tail; | |
| 132 | + | |
| 133 | +(* the byte of s at x, or -1; the slice is not the first parameter: | |
| 134 | + x in rdi, the address of s in rsi, its length in rdx *) | |
| 135 | +proc pick(x: INT; s: []U8): INT; | |
| 136 | +code | |
| 137 | + cmp rdi, rdx; | |
| 138 | + jae none; | |
| 139 | + movzx eax, byte [rsi + rdi]; | |
| 140 | + ret; | |
| 141 | + none: | |
| 142 | + mov rax, -1; | |
| 143 | + ret; | |
| 144 | +end pick; | |
| 145 | + | |
| 123 | 146 | proc main*; |
| 124 | -var a: [4]I64; s: [3]U8; q, r: U64; c: INT; | |
| 147 | +var a: [4]I64; s: [3]U8; t: [6]U8; q, r: U64; c: INT; | |
| 125 | 148 | begin |
| 126 | 149 | Out.int(tens(4, 2)); Out.ln; |
| 127 | 150 | a := [1, 20, 300, 4000]; |
| @@ -139,6 +162,9 @@ | ||
| 139 | 162 | c := 40; |
| 140 | 163 | Out.int(fetchAdd(ADR(c), 2)); Out.str(" "); Out.int(c); Out.ln; |
| 141 | 164 | Out.int(I64(two() * 21.0)); Out.str(" "); Out.int(answer()); Out.ln; |
| 165 | + t := "abcdef"; | |
| 166 | + Out.str(tail(t[0..6], 2)); Out.str(" "); Out.int(LEN(tail(t[0..6], 6))); Out.ln; | |
| 167 | + Out.int(pick(3, t[0..6])); Out.str(" "); Out.int(pick(9, t[0..6])); Out.ln; | |
| 142 | 168 | end; |
| 143 | 169 | |
| 144 | 170 | end Asm. |
| @@ -3,8 +3,8 @@ | ||
| 3 | 3 | at a time, and the counts of bits *) |
| 4 | 4 | import Out; |
| 5 | 5 | |
| 6 | -(* the first place of b in the n bytes at p, or -1: p in rdi, n in rsi, b in dl *) | |
| 7 | -proc index(p: ^U8; n: INT; b: U8): INT; | |
| 6 | +(* the first place of b in s, or -1: its address in rdi, its length in rsi, b in dl *) | |
| 7 | +proc index(s: []U8; b: U8): INT; | |
| 8 | 8 | code |
| 9 | 9 | movd xmm1, edx; |
| 10 | 10 | punpcklbw xmm1, xmm1; (* b in the low two bytes... *) |
| @@ -63,12 +63,12 @@ | ||
| 63 | 63 | begin |
| 64 | 64 | for i := 0 to 39 do buf[i] := 'a'; end; |
| 65 | 65 | buf[37] := 'x'; buf[20] := 'y'; buf[5] := 'z'; |
| 66 | - Out.int(index(ADR(buf[0]), 40, 'z')); Out.str(" "); | |
| 67 | - Out.int(index(ADR(buf[0]), 40, 'y')); Out.str(" "); | |
| 68 | - Out.int(index(ADR(buf[0]), 40, 'x')); Out.str(" "); | |
| 69 | - Out.int(index(ADR(buf[0]), 40, 'q')); Out.str(" "); | |
| 70 | - Out.int(index(ADR(buf[0]), 0, 'a')); Out.str(" "); | |
| 71 | - Out.int(index(ADR(buf[6]), 15, 'y')); Out.ln; | |
| 66 | + Out.int(index(buf[0..40], 'z')); Out.str(" "); | |
| 67 | + Out.int(index(buf[0..40], 'y')); Out.str(" "); | |
| 68 | + Out.int(index(buf[0..40], 'x')); Out.str(" "); | |
| 69 | + Out.int(index(buf[0..40], 'q')); Out.str(" "); | |
| 70 | + Out.int(index(buf[0..0], 'a')); Out.str(" "); | |
| 71 | + Out.int(index(buf[6..21], 'y')); Out.ln; | |
| 72 | 72 | Out.int(bits(0B0A0H)); Out.ln; |
| 73 | 73 | end; |
| 74 | 74 |