From 413478c6814b6fc9c01d0c6e397d0a4e3f64f581 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:20:31 +0100 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index b4cbc1a..c87baea 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -99,13 +99,14 @@ defect, and the library grows only by what breaks no program written for it. A wish for more, the owner's or yours, goes on the list in `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, -by the owner's word: report 2026-10.12; the compiler follows 2026-10.17, +by the owner's word: report 2026-10.12; the compiler follows 2026-10.18, whose `Sys` writes without waiting, for rat's pastes (10.13), and whose blank, `_ := f(x)`, drops a result that is otherwise warned of (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, rounding and the inverse functions, and `Sys` `space` (10.15), and whose constants may be records and tables of them, with `Str.copy` -(10.16), and `COPY` (10.17), also by the owner's word. Its parser +(10.16), and `COPY` (10.17), also by the owner's word, and whose code +procedures take and return slices (10.18). Its parser reaches the machine only through `gen.pas`. Keep `doc/compiler.md`'s table of lines and its routine names true when the compiler changes. diff --git a/doc/compiler.md b/doc/compiler.md index 1169d50..b9625b1 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -832,7 +832,9 @@ Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` and `extern`, each one through `Declaration`, a recovery point, which calls `ConstDecl`, `TypeDecl`, `VarDecl`, `ProcDecl` or `ExternDecl`. `ProcDecl` handles forward declarations, code procedures -(`CodeBody`, which checks the signature and has gen's `CodeProc` run +(`CodeBody`, which checks the signature, a slice parameter counting as +two of the six integer registers and a slice result going in rax and +rdx, and has gen's `CodeProc` run the target's assembler) and bodies (`Body`). `ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`, `EnumType`) build `TType`s. diff --git a/src/parse.pas b/src/parse.pas index e36f5f8..be36e4d 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -2141,9 +2141,12 @@ ni := 0; nf := 0; q := sig^.fields; while q <> nil do begin - if not q^.isVar and not IsScalar(q^.typ) then - MarkAt(at, 'a code procedure takes integers, BOOL, reals, pointers and procedures'); - if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); + if not q^.isVar and (q^.typ^.form = fSlice) then inc(ni, 2) { address, then length } + else begin + if not q^.isVar and not IsScalar(q^.typ) then + MarkAt(at, 'a code procedure takes integers, BOOL, reals, pointers, procedures and slices'); + if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); + end; q := q^.next; end; if (ni > 6) or (nf > 8) or sig^.variadic then MarkAt(at, 'too many parameters for a code procedure'); @@ -2154,7 +2157,8 @@ or not IsScalar(q^.typ) or not IsScalar(q^.next^.typ) or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then MarkAt(at, 'a code procedure returns two results only in rax and rdx'); - end else if not IsScalar(sig^.base) then MarkAt(at, 'a code procedure returns a single value'); + end else if not IsScalar(sig^.base) and (sig^.base^.form <> fSlice) then + MarkAt(at, 'a code procedure returns a single value'); CodeProc(p^.chunk, @AsmConst); Expect(sEnd); end; diff --git a/src/raz.pas b/src/raz.pas index 4290b35..fd67450 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -34,7 +34,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.17'; { the version of doc/raz.md it follows } + Report = '2026-10.18'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first } diff --git a/test/err/StmtCodeSlices.err b/test/err/StmtCodeSlices.err new file mode 100644 index 0000000..53390a7 --- /dev/null +++ b/test/err/StmtCodeSlices.err @@ -0,0 +1,4 @@ +test/err/StmtCodeSlices.raz:2:6: error: too many parameters for a code procedure + 2 | proc f(a, b, c, d, e: I64; s: []U8); + | ^ +raz: 1 error diff --git a/test/err/StmtCodeSlices.raz b/test/err/StmtCodeSlices.raz new file mode 100644 index 0000000..5ebe0fc --- /dev/null +++ b/test/err/StmtCodeSlices.raz @@ -0,0 +1,4 @@ +module StmtCodeSlices; +proc f(a, b, c, d, e: I64; s: []U8); +code ret; end; +end StmtCodeSlices. diff --git a/test/err/StmtCodeTakes.err b/test/err/StmtCodeTakes.err index 6be5361..796cf65 100644 --- a/test/err/StmtCodeTakes.err +++ b/test/err/StmtCodeTakes.err @@ -1,4 +1,4 @@ -test/err/StmtCodeTakes.raz:3:6: error: a code procedure takes integers, BOOL, reals, pointers and procedures +test/err/StmtCodeTakes.raz:3:6: error: a code procedure takes integers, BOOL, reals, pointers, procedures and slices 3 | proc f(r: R); | ^ raz: 1 error diff --git a/test/run/Asm.expect b/test/run/Asm.expect index ff13790..8db1732 100644 --- a/test/run/Asm.expect +++ b/test/run/Asm.expect @@ -7,3 +7,5 @@ 13 40 42 42 42 +cdef 0 +100 -1 diff --git a/test/run/Asm.raz b/test/run/Asm.raz index 6942bfc..e41a658 100644 --- a/test/run/Asm.raz +++ b/test/run/Asm.raz @@ -120,8 +120,31 @@ db 0C3H; end answer; +(* s without its first k bytes: s in rdi and rsi, k in rdx, the slice back in rax and rdx *) +proc tail(s: []U8; k: INT): []U8; +code + add rdi, rdx; + sub rsi, rdx; + mov rax, rdi; + mov rdx, rsi; + ret; +end tail; + +(* the byte of s at x, or -1; the slice is not the first parameter: + x in rdi, the address of s in rsi, its length in rdx *) +proc pick(x: INT; s: []U8): INT; +code + cmp rdi, rdx; + jae none; + movzx eax, byte [rsi + rdi]; + ret; + none: + mov rax, -1; + ret; +end pick; + proc main*; -var a: [4]I64; s: [3]U8; q, r: U64; c: INT; +var a: [4]I64; s: [3]U8; t: [6]U8; q, r: U64; c: INT; begin Out.int(tens(4, 2)); Out.ln; a := [1, 20, 300, 4000]; @@ -139,6 +162,9 @@ c := 40; Out.int(fetchAdd(ADR(c), 2)); Out.str(" "); Out.int(c); Out.ln; Out.int(I64(two() * 21.0)); Out.str(" "); Out.int(answer()); Out.ln; + t := "abcdef"; + Out.str(tail(t[0..6], 2)); Out.str(" "); Out.int(LEN(tail(t[0..6], 6))); Out.ln; + Out.int(pick(3, t[0..6])); Out.str(" "); Out.int(pick(9, t[0..6])); Out.ln; end; end Asm. diff --git a/test/run/Simd.raz b/test/run/Simd.raz index 06e79de..c1f661d 100644 --- a/test/run/Simd.raz +++ b/test/run/Simd.raz @@ -3,8 +3,8 @@ at a time, and the counts of bits *) import Out; -(* the first place of b in the n bytes at p, or -1: p in rdi, n in rsi, b in dl *) -proc index(p: ^U8; n: INT; b: U8): INT; +(* the first place of b in s, or -1: its address in rdi, its length in rsi, b in dl *) +proc index(s: []U8; b: U8): INT; code movd xmm1, edx; punpcklbw xmm1, xmm1; (* b in the low two bytes... *) @@ -63,12 +63,12 @@ begin for i := 0 to 39 do buf[i] := 'a'; end; buf[37] := 'x'; buf[20] := 'y'; buf[5] := 'z'; - Out.int(index(ADR(buf[0]), 40, 'z')); Out.str(" "); - Out.int(index(ADR(buf[0]), 40, 'y')); Out.str(" "); - Out.int(index(ADR(buf[0]), 40, 'x')); Out.str(" "); - Out.int(index(ADR(buf[0]), 40, 'q')); Out.str(" "); - Out.int(index(ADR(buf[0]), 0, 'a')); Out.str(" "); - Out.int(index(ADR(buf[6]), 15, 'y')); Out.ln; + Out.int(index(buf[0..40], 'z')); Out.str(" "); + Out.int(index(buf[0..40], 'y')); Out.str(" "); + Out.int(index(buf[0..40], 'x')); Out.str(" "); + Out.int(index(buf[0..40], 'q')); Out.str(" "); + Out.int(index(buf[0..0], 'a')); Out.str(" "); + Out.int(index(buf[6..21], 'y')); Out.ln; Out.int(bits(0B0A0H)); Out.ln; end;