From 95cc648a36e38cd36808d456848929da0dfcbb28 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:35:21 +0100 Subject: [PATCH] = 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index c4e4d9f..e30b835 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -99,8 +99,8 @@ - Several results come back through a hidden pointer; a code procedure returns two in rax and rdx. - `NEW(p, a)` requires `a: ^Mem.Allocator` and calls its `alloc` field: - the one library type the compiler knows by name. `Rt.fail` and - `Rt.eq` are found by name too. + the one library type the compiler knows by name. `Rt.fail` is + found by name too. - A set literal takes its type from its first name when nothing else gives it; `{}` needs a known type. String constants can be indexed and sliced. diff --git a/PLAN.md b/PLAN.md index e8bab83..dd97738 100644 --- a/PLAN.md +++ b/PLAN.md @@ -430,7 +430,7 @@ results must be the procedure's, type for type; `test/err` for a mismatch, `test/run` for one, two and three results, through two levels of calls. -- [ ] 44. `=` and `#` inline: the lengths compared, then, if equal, +- [x] 44. `=` and `#` inline: the lengths compared, then, if equal, the bytes, `LEN * SIZE(T)` of them, with `repe cmpsb`; for strings and for arrays and slices of integers, `BOOL`s, enumerations, sets and pointers, any mix of `[n]T` and `[]T`. diff --git a/doc/compiler.md b/doc/compiler.md index 192cb16..2317ce7 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -11,9 +11,9 @@ | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions | | `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout | -| `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,547 | 1,339 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,293 | 2,072 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/x64.pas` | 693 | 556 | Code buffer, chunks, fixups, places, instruction encoder | +| `src/gen.pas` | 1,570 | 1,358 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,290 | 2,069 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 227 | 178 | The command line, the entry points | @@ -268,6 +268,9 @@ on the right of `*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow. Narrow integers are brought back to their width after each operation (`Norm`), which is how `U8` arithmetic wraps. +`=` and `#` on arrays, slices and strings make a slice of each side +(`SeqSlice`), compare the lengths, and then the bytes with `repe +cmpsb`, right where the comparison stands: no call. Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a variable into an item for part of it, mostly by adding to `off`. @@ -333,8 +336,9 @@ and compiles it if it isn't yet. `CheckOneModule` first makes sure no later directory has the module too, unless the file is a link to one of its files there; `CheckSiblings` then reads the files for other -targets beside it, declarations only, and `CompareIface` holds them to the same exports by their fingerprints. A generic module -is compiled once per distinct list of arguments: `ParseArgs` reads +targets beside it, declarations only, and `CompareIface` holds them +to the same exports by their fingerprints. A generic module is +compiled once per distinct list of arguments: `ParseArgs` reads them at the import, `Params` binds them to the module's parameters, and `IsModule` decides whether an instance already exists. diff --git a/lib/Rt.raz b/lib/Rt.raz index ea523e5..fd2e1bb 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -1,8 +1,8 @@ (* The run time: ending the program, and what the compiler's code - calls: fail when a check fails, eq when = or # compares two strings; - stop, for the library's own failures; and the handlers that say - where the program was when a signal stops it. - The compiler finds fail, eq, exit, cExit and places by these names. + calls, fail when a check fails; stop, for the library's own + failures; and the handlers that say where the program was when a + signal stops it. + The compiler finds fail, exit, cExit and places by these names. The handlers are Linux's on x86-64, set by system calls: what the kernel hands them is laid out as it is there. *) module Rt; @@ -205,16 +205,6 @@ put("stopped by "); put(why); say; -end; - -proc eq*(a, b: []U8): BOOL; -var i: I64; -begin - if LEN(a) # LEN(b) then return false; end; - for i := 0 to LEN(a) - 1 do - if a[i] # b[i] then return false; end; - end; - return true; end; begin diff --git a/src/gen.pas b/src/gen.pas index 5c8de08..aa69428 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -61,7 +61,7 @@ end; var - rtFail, rtEq, argcChunk, argvChunk: longint; + rtFail, argcChunk, argvChunk: longint; depth: longint; { values pushed and not yet popped } procedure MakeConst(var x: TItem; t: PType; v: int64); @@ -721,19 +721,25 @@ if x.mode = mConst then x.val := Cut(not x.val, x.typ) else begin Value(x); Unary(2, RAX); Norm(x.typ) end; end else MarkAt(x.pos, '"not" needs a BOOL or an integer, found ' + What(x)); +end; + +{ Arrays, slices and strings, which = and # compare element by element } +function IsSeq(t: PType): boolean; +begin + IsSeq := t^.form in [fStr, fArray, fSlice]; end; -function IsStrLike(t: PType): boolean; +function ElemOf(t: PType): PType; begin - IsStrLike := (t^.form = fStr) or ((t^.form in [fArray, fSlice]) and (t^.base = tU8)); + if t^.form = fStr then ElemOf := tU8 else ElemOf := t^.base; end; -{ A []U8, [n]U8 or string as a slice in a temporary, for comparing. } -procedure StrSlice(var x: TItem); +{ An array, slice or string as a slice in a temporary, for comparing. } +procedure SeqSlice(var x: TItem); var t: PType; begin t := NewType(fSlice, 16, 8); - t^.base := tU8; + t^.base := ElemOf(x.typ); if x.typ^.form = fSlice then begin x.typ := t; CopyToTemp(x) end else begin @@ -755,8 +761,8 @@ if x.mode <> mConst then x := tmp; TestRR(RAX, RAX); if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L); - end else if (op in [sEql, sNeq]) and IsStrLike(x.typ) then - StrSlice(x) + end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then + SeqSlice(x) else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then NotValue(x) else if x.mode <> mConst then begin @@ -853,7 +859,7 @@ CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); var - xt, yt, t: PType; r: TItem; scale, sh, k: longint; isPtr, unsign, f: boolean; a, b: int64; + xt, yt, t, e: PType; r: TItem; scale, sh, k, differ: longint; isPtr, unsign, f: boolean; a, b: int64; begin xt := x.typ; yt := y.typ; if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); @@ -871,16 +877,33 @@ exit; end; - if (op in [sEql, sNeq]) and IsStrLike(xt) then begin - if not IsStrLike(yt) then MarkAt(y.pos, 'can''t compare a string with ' + TypeName(yt)); - StrSlice(y); + { the lengths, then the bytes, LEN * SIZE(T) of them: the elements + are of types whose bytes say all there is to their values } + if (op in [sEql, sNeq]) and IsSeq(xt) then begin + e := ElemOf(xt); + if not IsSeq(yt) then + MarkAt(Join(x.pos, y.pos), 'can''t compare an array with ' + TypeName(yt)) + else if not Same(e, ElemOf(yt)) then + MarkAt(Join(x.pos, y.pos), 'can''t compare an array of ' + TypeName(e) + ' with one of ' + TypeName(ElemOf(yt))); + if e^.form = fReal then + MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of reals with ' + SymText(op), + '-0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop') + else if not (e^.form in [fInt, fBool, fEnum, fSet, fPtr]) then + MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of ' + TypeName(e) + ' with ' + SymText(op), 'write a loop'); + SeqSlice(y); Address(y, RSI); Address(x, RDI); - if odd(depth) then AluI(aluSub, RSP, 8); - CallChunk(rtEq); - if odd(depth) then AluI(aluAdd, RSP, 8); - Extend(RAX, 1, false); - if op = sNeq then AluI(aluXor, RAX, 1); + Load(RCX, Mem(RSI, 8), 8, false); + AluM(aluCmp, RCX, Mem(RDI, 8)); + differ := NoLabel; + Jcc(ccNE, differ); + Load(RSI, Mem(RSI, 0), 8, false); + Load(RDI, Mem(RDI, 0), 8, false); + if e^.size > 1 then ShiftI(shShl, RCX, Log2(e^.size)); + Alu(aluCmp, RCX, RCX); { equal, if there are no bytes } + RepeCmpsb; + FixLink(differ); + if op = sEql then SetCC(ccE, RAX) else SetCC(ccNE, RAX); MakeConst(x, tBool, 0); x.mode := mReg; exit; diff --git a/src/parse.pas b/src/parse.pas index 34ad4ef..0d5b90f 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -67,7 +67,7 @@ curResult: PType; { the result type of the procedure being compiled } inExtern: boolean; { reading an extern's signature: ... allowed } tArgv, tBodySig: PType; - nameRt, nameFail, nameEq, nameMain, nameBody, nameStatic: longint; + nameRt, nameFail, nameMain, nameBody, nameStatic: longint; declaring: array[0..63] of longint; { the names the declaration being read declares } numDeclaring: longint; skipped: boolean; { the last statement had an error and was skipped } @@ -1629,7 +1629,6 @@ p^.pos := at; p^.exported := exp; if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail - else if (curModule^.name = nameRt) and not checkMode and (n = nameEq) then p^.chunk := rtEq else p^.chunk := NewChunk(secText, curModule^.sname, n); Declare(p); end; @@ -2274,12 +2273,10 @@ initialization nameRt := Name('Rt'); nameFail := Name('fail'); - nameEq := Name('eq'); nameMain := Name('main'); nameBody := Name('body'); nameStatic := Name('static'); rtFail := NewChunk(secText, nameRt, nameFail); - rtEq := NewChunk(secText, nameRt, nameEq); argcChunk := NewChunk(secBss, -1, Name('argc')); SetBss(argcChunk, 8, 8); argvChunk := NewChunk(secBss, -1, Name('argv')); diff --git a/src/x64.pas b/src/x64.pas index cd2c75f..0eba27a 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -143,6 +143,7 @@ procedure Ud2; procedure RepMovsb; procedure RepStosb; +procedure RepeCmpsb; procedure LockCmpxchg(const m: TMem; r: longint); procedure Xchg(const m: TMem; r: longint); procedure LockXadd(const m: TMem; r: longint); @@ -559,6 +560,7 @@ procedure Ud2; begin Put1($0F); Put1($0B) end; procedure RepMovsb; begin Put1($F3); Put1($A4) end; procedure RepStosb; begin Put1($F3); Put1($AA) end; +procedure RepeCmpsb; begin Put1($F3); Put1($A6) end; procedure LockCmpxchg(const m: TMem; r: longint); begin diff --git a/test/err/ArrayEqReal.err b/test/err/ArrayEqReal.err new file mode 100644 index 0000000..6d60572 --- /dev/null +++ b/test/err/ArrayEqReal.err @@ -0,0 +1,5 @@ +test/err/ArrayEqReal.raz:6:8: error: can't compare arrays of reals with "=" + 6 | if a = b then end; + | ^^^^^ +help: -0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop +raz: 1 error diff --git a/test/err/ArrayEqReal.raz b/test/err/ArrayEqReal.raz new file mode 100644 index 0000000..fa57470 --- /dev/null +++ b/test/err/ArrayEqReal.raz @@ -0,0 +1,9 @@ +module ArrayEqReal; + +proc main*; +var a, b: [2]F64; +begin + if a = b then end; +end; + +end ArrayEqReal. diff --git a/test/err/ArrayEqRecord.err b/test/err/ArrayEqRecord.err new file mode 100644 index 0000000..0d2a772 --- /dev/null +++ b/test/err/ArrayEqRecord.err @@ -0,0 +1,5 @@ +test/err/ArrayEqRecord.raz:6:8: error: can't compare arrays of record with "=" + 6 | if a = b then end; + | ^^^^^ +help: write a loop +raz: 1 error diff --git a/test/err/ArrayEqRecord.raz b/test/err/ArrayEqRecord.raz new file mode 100644 index 0000000..93eea39 --- /dev/null +++ b/test/err/ArrayEqRecord.raz @@ -0,0 +1,9 @@ +module ArrayEqRecord; + +proc main*; +var a, b: [2]record x: I64; end; +begin + if a = b then end; +end; + +end ArrayEqRecord. diff --git a/test/err/ArrayEqTypes.err b/test/err/ArrayEqTypes.err new file mode 100644 index 0000000..12ad393 --- /dev/null +++ b/test/err/ArrayEqTypes.err @@ -0,0 +1,4 @@ +test/err/ArrayEqTypes.raz:6:8: error: can't compare an array of I64 with one of I32 + 6 | if a # b then end; + | ^^^^^ +raz: 1 error diff --git a/test/err/ArrayEqTypes.raz b/test/err/ArrayEqTypes.raz new file mode 100644 index 0000000..c76355a --- /dev/null +++ b/test/err/ArrayEqTypes.raz @@ -0,0 +1,9 @@ +module ArrayEqTypes; + +proc main*; +var a: [2]I64; b: [2]I32; +begin + if a # b then end; +end; + +end ArrayEqTypes. diff --git a/test/err/AtStrCmp.err b/test/err/AtStrCmp.err index 294ffec..f079cb9 100644 --- a/test/err/AtStrCmp.err +++ b/test/err/AtStrCmp.err @@ -1,4 +1,4 @@ -test/err/AtStrCmp.raz:5:12: error: can't compare a string with I64 +test/err/AtStrCmp.raz:5:8: error: can't compare an array with I64 5 | if s = x + 1 then end; - | ^^^^^ + | ^^^^^^^^^ raz: 1 error diff --git a/test/run/ArrayEq.expect b/test/run/ArrayEq.expect new file mode 100644 index 0000000..30a6823 --- /dev/null +++ b/test/run/ArrayEq.expect @@ -0,0 +1 @@ +TFFTTFTTTTFTTFTFTFTTTTT diff --git a/test/run/ArrayEq.raz b/test/run/ArrayEq.raz new file mode 100644 index 0000000..3371d23 --- /dev/null +++ b/test/run/ArrayEq.raz @@ -0,0 +1,41 @@ +module ArrayEq; +import Out; + +type Colour = (red, green, blue); + +proc say(b: BOOL); +begin + if b then Out.str("T"); else Out.str("F"); end; +end; + +proc main*; +var + a, b: [4]I64; s: []I64; e: []I64; + w: [3]U16; c, d: [2]Colour; f: [3]BOOL; + m, n: [2]set of Colour; p, q: [2]^I64; x: I64; + t: [5]U8; +begin + a := [1, 2, 3, 4]; b := [1, 2, 3, 4]; + say(a = b); say(a # b); + b[3] := 5; + say(a = b); say(a # b); + s := a[0..3]; + say(s = a[0..3]); say(s = a); (* of different lengths *) + say(a[1..1] = e); say(e = s[0..0]); (* empty *) + w := [1, 256, 65535]; + say(w = [U16(1), 256, 65535]); say(w[2..3] = [U16(65535)]); + c := [red, blue]; d := [red, green]; + say(c = d); d[1] := blue; say(c = d); + f := [true, false, true]; + say(f[0..1] = f[2..3]); say(f[0..1] = [true, false]); + m := [{red}, {green, blue}]; n := m; + say(m = n); n[1] := {blue}; say(m = n); + p := [ADR(x), nil]; q := p; + say(p = q); q[1] := ADR(x); say(p = q); + t := [104, 101, 108, 108, 111]; + say(t = "hello"); say("hello" = t[0..5]); say("hell" = t[0..4]); + say("help" # t[0..4]); say("" = t[0..0]); + Out.ln; +end; + +end ArrayEq.