raz / commit 413478c6

commit
413478c6814b6fc9c01d0c6e397d0a4e3f64f581
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6ca487e7
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.

patch · log

10 files changed, +61 -18

CLAUDE.md modified +3 -2

@@ -99,13 +99,14 @@
9999 defect, and the library grows only by what breaks no program written
100100 for it. A wish for more, the owner's or yours, goes on the list in
101101 `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,
103103 whose `Sys` writes without waiting, for rat's pastes (10.13), and
104104 whose blank, `_ := f(x)`, drops a result that is otherwise warned of
105105 (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10,
106106 rounding and the inverse functions, and `Sys` `space` (10.15), and
107107 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
109110 reaches the machine only through `gen.pas`. Keep
110111 `doc/compiler.md`'s table of lines and its routine names true when the
111112 compiler changes.

doc/compiler.md (history) modified +3 -1

@@ -832,7 +832,9 @@
832832 Then declarations: `Declarations` reads `const`, `type`, `var`, `proc`
833833 and `extern`, each one through `Declaration`, a recovery point, which
834834 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
836838 the target's assembler) and bodies (`Body`).
837839 `ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`,
838840 `EnumType`) build `TType`s.

src/parse.pas modified +8 -4

@@ -2141,9 +2141,12 @@
21412141 ni := 0; nf := 0;
21422142 q := sig^.fields;
21432143 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;
21472150 q := q^.next;
21482151 end;
21492152 if (ni > 6) or (nf > 8) or sig^.variadic then MarkAt(at, 'too many parameters for a code procedure');
@@ -2154,7 +2157,8 @@
21542157 or not IsScalar(q^.typ) or not IsScalar(q^.next^.typ)
21552158 or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then
21562159 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');
21582162 CodeProc(p^.chunk, @AsmConst);
21592163 Expect(sEnd);
21602164 end;

src/raz.pas modified +1 -1

@@ -34,7 +34,7 @@
3434 { what git describe says, made by make: 0.3.0 at the tag v0.3.0,
3535 0.3.0-5-g38d29a8 five commits past it }
3636 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 }
3838 MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it }
3939 MfdCloexec = 1; { an ELF still executes: the kernel opens it first }
4040

test/err/StmtCodeSlices.err (history) added +4 -0

@@ -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

test/err/StmtCodeSlices.raz (history) added +4 -0

@@ -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.

test/err/StmtCodeTakes.err (history) modified +1 -1

@@ -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
22 3 | proc f(r: R);
33 | ^
44 raz: 1 error

test/run/Asm.expect (history) modified +2 -0

@@ -7,3 +7,5 @@
77 13
88 40 42
99 42 42
10+cdef 0
11+100 -1

test/run/Asm.raz (history) modified +27 -1

@@ -120,8 +120,31 @@
120120 db 0C3H;
121121 end answer;
122122
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+
123146 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;
125148 begin
126149 Out.int(tens(4, 2)); Out.ln;
127150 a := [1, 20, 300, 4000];
@@ -139,6 +162,9 @@
139162 c := 40;
140163 Out.int(fetchAdd(ADR(c), 2)); Out.str(" "); Out.int(c); Out.ln;
141164 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;
142168 end;
143169
144170 end Asm.

test/run/Simd.raz (history) modified +8 -8

@@ -3,8 +3,8 @@
33 at a time, and the counts of bits *)
44 import Out;
55
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;
88 code
99 movd xmm1, edx;
1010 punpcklbw xmm1, xmm1; (* b in the low two bytes... *)
@@ -63,12 +63,12 @@
6363 begin
6464 for i := 0 to 39 do buf[i] := 'a'; end;
6565 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;
7272 Out.int(bits(0B0A0H)); Out.ln;
7373 end;
7474