raz / commit 95cc648a

commit
95cc648a36e38cd36808d456848929da0dfcbb28
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
71fdfd58
= and # on arrays inline, with repe cmpsb; Rt.eq goes

Report 2026-10.3, 6.5. Arrays, slices and strings of integers, BOOLs,
enumerations, sets and pointers compare where they stand: the lengths,
then LEN * SIZE(T) bytes with repe cmpsb, any mix of [n]T and []T.
Arrays of reals, of records and of two element types are errors, each
saying why. Rt.eq and the compiler's finding it by name go.
test/run/ArrayEq; test/err/ArrayEqReal, ArrayEqRecord, ArrayEqTypes.

patch · log

16 files changed, +144 -45

CLAUDE.md modified +2 -2

@@ -99,8 +99,8 @@
9999 - Several results come back through a hidden pointer; a code procedure
100100 returns two in rax and rdx.
101101 - `NEW(p, a)` requires `a: ^Mem.Allocator` and calls its `alloc` field:
102- the one library type the compiler knows by name. `Rt.fail` and
103- `Rt.eq` are found by name too.
102+ the one library type the compiler knows by name. `Rt.fail` is
103+ found by name too.
104104 - A set literal takes its type from its first name when nothing else
105105 gives it; `{}` needs a known type. String constants can be indexed
106106 and sliced.

PLAN.md (history) modified +1 -1

@@ -430,7 +430,7 @@
430430 results must be the procedure's, type for type; `test/err` for
431431 a mismatch, `test/run` for one, two and three results, through
432432 two levels of calls.
433-- [ ] 44. `=` and `#` inline: the lengths compared, then, if equal,
433+- [x] 44. `=` and `#` inline: the lengths compared, then, if equal,
434434 the bytes, `LEN * SIZE(T)` of them, with `repe cmpsb`; for
435435 strings and for arrays and slices of integers, `BOOL`s,
436436 enumerations, sets and pointers, any mix of `[n]T` and `[]T`.

doc/compiler.md (history) modified +9 -5

@@ -11,9 +11,9 @@
1111 | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout |
14-| `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder |
15-| `src/gen.pas` | 1,547 | 1,339 | Items: loading, storing, copying, frames, calls |
16-| `src/parse.pas` | 2,293 | 2,072 | Declarations, statements, expressions, built-ins, recovery, warnings |
14+| `src/x64.pas` | 693 | 556 | Code buffer, chunks, fixups, places, instruction encoder |
15+| `src/gen.pas` | 1,570 | 1,358 | Items: loading, storing, copying, frames, calls |
16+| `src/parse.pas` | 2,290 | 2,069 | Declarations, statements, expressions, built-ins, recovery, warnings |
1717 | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
1919 | `src/raz.pas` | 227 | 178 | The command line, the entry points |
@@ -268,6 +268,9 @@
268268 on the right of `*`, `div` or `mod` becomes shifts (`DivPow2`), since
269269 `idiv` is slow. Narrow integers are brought back to their width
270270 after each operation (`Norm`), which is how `U8` arithmetic wraps.
271+`=` and `#` on arrays, slices and strings make a slice of each side
272+(`SeqSlice`), compare the lengths, and then the bytes with `repe
273+cmpsb`, right where the comparison stands: no call.
271274
272275 Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a
273276 variable into an item for part of it, mostly by adding to `off`.
@@ -333,8 +336,9 @@
333336 and compiles it if it isn't yet. `CheckOneModule` first makes sure no
334337 later directory has the module too, unless the file is a link to one
335338 of its files there; `CheckSiblings` then reads the files for other
336-targets beside it, declarations only, and `CompareIface` holds them to the same exports by their fingerprints. A generic module
337-is compiled once per distinct list of arguments: `ParseArgs` reads
339+targets beside it, declarations only, and `CompareIface` holds them
340+to the same exports by their fingerprints. A generic module is
341+compiled once per distinct list of arguments: `ParseArgs` reads
338342 them at the import, `Params` binds them to the module's parameters, and
339343 `IsModule` decides whether an instance already exists.
340344

lib/Rt.raz (history) modified +4 -14

@@ -1,8 +1,8 @@
11 (* The run time: ending the program, and what the compiler's code
2- calls: fail when a check fails, eq when = or # compares two strings;
3- stop, for the library's own failures; and the handlers that say
4- where the program was when a signal stops it.
5- The compiler finds fail, eq, exit, cExit and places by these names.
2+ calls, fail when a check fails; stop, for the library's own
3+ failures; and the handlers that say where the program was when a
4+ signal stops it.
5+ The compiler finds fail, exit, cExit and places by these names.
66 The handlers are Linux's on x86-64, set by system calls: what the
77 kernel hands them is laid out as it is there. *)
88 module Rt;
@@ -205,16 +205,6 @@
205205 put("stopped by ");
206206 put(why);
207207 say;
208-end;
209-
210-proc eq*(a, b: []U8): BOOL;
211-var i: I64;
212-begin
213- if LEN(a) # LEN(b) then return false; end;
214- for i := 0 to LEN(a) - 1 do
215- if a[i] # b[i] then return false; end;
216- end;
217- return true;
218208 end;
219209
220210 begin

src/gen.pas modified +40 -17

@@ -61,7 +61,7 @@
6161 end;
6262
6363 var
64- rtFail, rtEq, argcChunk, argvChunk: longint;
64+ rtFail, argcChunk, argvChunk: longint;
6565 depth: longint; { values pushed and not yet popped }
6666
6767 procedure MakeConst(var x: TItem; t: PType; v: int64);
@@ -721,19 +721,25 @@
721721 if x.mode = mConst then x.val := Cut(not x.val, x.typ)
722722 else begin Value(x); Unary(2, RAX); Norm(x.typ) end;
723723 end else MarkAt(x.pos, '"not" needs a BOOL or an integer, found ' + What(x));
724+end;
725+
726+{ Arrays, slices and strings, which = and # compare element by element }
727+function IsSeq(t: PType): boolean;
728+begin
729+ IsSeq := t^.form in [fStr, fArray, fSlice];
724730 end;
725731
726-function IsStrLike(t: PType): boolean;
732+function ElemOf(t: PType): PType;
727733 begin
728- IsStrLike := (t^.form = fStr) or ((t^.form in [fArray, fSlice]) and (t^.base = tU8));
734+ if t^.form = fStr then ElemOf := tU8 else ElemOf := t^.base;
729735 end;
730736
731-{ A []U8, [n]U8 or string as a slice in a temporary, for comparing. }
732-procedure StrSlice(var x: TItem);
737+{ An array, slice or string as a slice in a temporary, for comparing. }
738+procedure SeqSlice(var x: TItem);
733739 var t: PType;
734740 begin
735741 t := NewType(fSlice, 16, 8);
736- t^.base := tU8;
742+ t^.base := ElemOf(x.typ);
737743 if x.typ^.form = fSlice then begin
738744 x.typ := t; CopyToTemp(x)
739745 end else begin
@@ -755,8 +761,8 @@
755761 if x.mode <> mConst then x := tmp;
756762 TestRR(RAX, RAX);
757763 if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L);
758- end else if (op in [sEql, sNeq]) and IsStrLike(x.typ) then
759- StrSlice(x)
764+ end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then
765+ SeqSlice(x)
760766 else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then
761767 NotValue(x)
762768 else if x.mode <> mConst then begin
@@ -853,7 +859,7 @@
853859 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
854860 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
855861 var
856- xt, yt, t: PType; r: TItem; scale, sh, k: longint; isPtr, unsign, f: boolean; a, b: int64;
862+ xt, yt, t, e: PType; r: TItem; scale, sh, k, differ: longint; isPtr, unsign, f: boolean; a, b: int64;
857863 begin
858864 xt := x.typ; yt := y.typ;
859865 if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y);
@@ -871,16 +877,33 @@
871877 exit;
872878 end;
873879
874- if (op in [sEql, sNeq]) and IsStrLike(xt) then begin
875- if not IsStrLike(yt) then MarkAt(y.pos, 'can''t compare a string with ' + TypeName(yt));
876- StrSlice(y);
880+ { the lengths, then the bytes, LEN * SIZE(T) of them: the elements
881+ are of types whose bytes say all there is to their values }
882+ if (op in [sEql, sNeq]) and IsSeq(xt) then begin
883+ e := ElemOf(xt);
884+ if not IsSeq(yt) then
885+ MarkAt(Join(x.pos, y.pos), 'can''t compare an array with ' + TypeName(yt))
886+ else if not Same(e, ElemOf(yt)) then
887+ MarkAt(Join(x.pos, y.pos), 'can''t compare an array of ' + TypeName(e) + ' with one of ' + TypeName(ElemOf(yt)));
888+ if e^.form = fReal then
889+ MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of reals with ' + SymText(op),
890+ '-0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop')
891+ else if not (e^.form in [fInt, fBool, fEnum, fSet, fPtr]) then
892+ MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of ' + TypeName(e) + ' with ' + SymText(op), 'write a loop');
893+ SeqSlice(y);
877894 Address(y, RSI);
878895 Address(x, RDI);
879- if odd(depth) then AluI(aluSub, RSP, 8);
880- CallChunk(rtEq);
881- if odd(depth) then AluI(aluAdd, RSP, 8);
882- Extend(RAX, 1, false);
883- if op = sNeq then AluI(aluXor, RAX, 1);
896+ Load(RCX, Mem(RSI, 8), 8, false);
897+ AluM(aluCmp, RCX, Mem(RDI, 8));
898+ differ := NoLabel;
899+ Jcc(ccNE, differ);
900+ Load(RSI, Mem(RSI, 0), 8, false);
901+ Load(RDI, Mem(RDI, 0), 8, false);
902+ if e^.size > 1 then ShiftI(shShl, RCX, Log2(e^.size));
903+ Alu(aluCmp, RCX, RCX); { equal, if there are no bytes }
904+ RepeCmpsb;
905+ FixLink(differ);
906+ if op = sEql then SetCC(ccE, RAX) else SetCC(ccNE, RAX);
884907 MakeConst(x, tBool, 0);
885908 x.mode := mReg;
886909 exit;

src/parse.pas modified +1 -4

@@ -67,7 +67,7 @@
6767 curResult: PType; { the result type of the procedure being compiled }
6868 inExtern: boolean; { reading an extern's signature: ... allowed }
6969 tArgv, tBodySig: PType;
70- nameRt, nameFail, nameEq, nameMain, nameBody, nameStatic: longint;
70+ nameRt, nameFail, nameMain, nameBody, nameStatic: longint;
7171 declaring: array[0..63] of longint; { the names the declaration being read declares }
7272 numDeclaring: longint;
7373 skipped: boolean; { the last statement had an error and was skipped }
@@ -1629,7 +1629,6 @@
16291629 p^.pos := at;
16301630 p^.exported := exp;
16311631 if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail
1632- else if (curModule^.name = nameRt) and not checkMode and (n = nameEq) then p^.chunk := rtEq
16331632 else p^.chunk := NewChunk(secText, curModule^.sname, n);
16341633 Declare(p);
16351634 end;
@@ -2274,12 +2273,10 @@
22742273 initialization
22752274 nameRt := Name('Rt');
22762275 nameFail := Name('fail');
2277- nameEq := Name('eq');
22782276 nameMain := Name('main');
22792277 nameBody := Name('body');
22802278 nameStatic := Name('static');
22812279 rtFail := NewChunk(secText, nameRt, nameFail);
2282- rtEq := NewChunk(secText, nameRt, nameEq);
22832280 argcChunk := NewChunk(secBss, -1, Name('argc'));
22842281 SetBss(argcChunk, 8, 8);
22852282 argvChunk := NewChunk(secBss, -1, Name('argv'));

src/x64.pas modified +2 -0

@@ -143,6 +143,7 @@
143143 procedure Ud2;
144144 procedure RepMovsb;
145145 procedure RepStosb;
146+procedure RepeCmpsb;
146147 procedure LockCmpxchg(const m: TMem; r: longint);
147148 procedure Xchg(const m: TMem; r: longint);
148149 procedure LockXadd(const m: TMem; r: longint);
@@ -559,6 +560,7 @@
559560 procedure Ud2; begin Put1($0F); Put1($0B) end;
560561 procedure RepMovsb; begin Put1($F3); Put1($A4) end;
561562 procedure RepStosb; begin Put1($F3); Put1($AA) end;
563+procedure RepeCmpsb; begin Put1($F3); Put1($A6) end;
562564
563565 procedure LockCmpxchg(const m: TMem; r: longint);
564566 begin

test/err/ArrayEqReal.err added +5 -0

@@ -0,0 +1,5 @@
1+test/err/ArrayEqReal.raz:6:8: error: can't compare arrays of reals with "="
2+ 6 | if a = b then end;
3+ | ^^^^^
4+help: -0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop
5+raz: 1 error

test/err/ArrayEqReal.raz added +9 -0

@@ -0,0 +1,9 @@
1+module ArrayEqReal;
2+
3+proc main*;
4+var a, b: [2]F64;
5+begin
6+ if a = b then end;
7+end;
8+
9+end ArrayEqReal.

test/err/ArrayEqRecord.err added +5 -0

@@ -0,0 +1,5 @@
1+test/err/ArrayEqRecord.raz:6:8: error: can't compare arrays of record with "="
2+ 6 | if a = b then end;
3+ | ^^^^^
4+help: write a loop
5+raz: 1 error

test/err/ArrayEqRecord.raz added +9 -0

@@ -0,0 +1,9 @@
1+module ArrayEqRecord;
2+
3+proc main*;
4+var a, b: [2]record x: I64; end;
5+begin
6+ if a = b then end;
7+end;
8+
9+end ArrayEqRecord.

test/err/ArrayEqTypes.err added +4 -0

@@ -0,0 +1,4 @@
1+test/err/ArrayEqTypes.raz:6:8: error: can't compare an array of I64 with one of I32
2+ 6 | if a # b then end;
3+ | ^^^^^
4+raz: 1 error

test/err/ArrayEqTypes.raz added +9 -0

@@ -0,0 +1,9 @@
1+module ArrayEqTypes;
2+
3+proc main*;
4+var a: [2]I64; b: [2]I32;
5+begin
6+ if a # b then end;
7+end;
8+
9+end ArrayEqTypes.

test/err/AtStrCmp.err modified +2 -2

@@ -1,4 +1,4 @@
1-test/err/AtStrCmp.raz:5:12: error: can't compare a string with I64
1+test/err/AtStrCmp.raz:5:8: error: can't compare an array with I64
22 5 | if s = x + 1 then end;
3- | ^^^^^
3+ | ^^^^^^^^^
44 raz: 1 error

test/run/ArrayEq.expect added +1 -0

@@ -0,0 +1 @@
1+TFFTTFTTTTFTTFTFTFTTTTT

test/run/ArrayEq.raz added +41 -0

@@ -0,0 +1,41 @@
1+module ArrayEq;
2+import Out;
3+
4+type Colour = (red, green, blue);
5+
6+proc say(b: BOOL);
7+begin
8+ if b then Out.str("T"); else Out.str("F"); end;
9+end;
10+
11+proc main*;
12+var
13+ a, b: [4]I64; s: []I64; e: []I64;
14+ w: [3]U16; c, d: [2]Colour; f: [3]BOOL;
15+ m, n: [2]set of Colour; p, q: [2]^I64; x: I64;
16+ t: [5]U8;
17+begin
18+ a := [1, 2, 3, 4]; b := [1, 2, 3, 4];
19+ say(a = b); say(a # b);
20+ b[3] := 5;
21+ say(a = b); say(a # b);
22+ s := a[0..3];
23+ say(s = a[0..3]); say(s = a); (* of different lengths *)
24+ say(a[1..1] = e); say(e = s[0..0]); (* empty *)
25+ w := [1, 256, 65535];
26+ say(w = [U16(1), 256, 65535]); say(w[2..3] = [U16(65535)]);
27+ c := [red, blue]; d := [red, green];
28+ say(c = d); d[1] := blue; say(c = d);
29+ f := [true, false, true];
30+ say(f[0..1] = f[2..3]); say(f[0..1] = [true, false]);
31+ m := [{red}, {green, blue}]; n := m;
32+ say(m = n); n[1] := {blue}; say(m = n);
33+ p := [ADR(x), nil]; q := p;
34+ say(p = q); q[1] := ADR(x); say(p = q);
35+ t := [104, 101, 108, 108, 111];
36+ say(t = "hello"); say("hello" = t[0..5]); say("hell" = t[0..4]);
37+ say("help" # t[0..4]); say("" = t[0..0]);
38+ Out.ln;
39+end;
40+
41+end ArrayEq.