From d8137081de1e4f8571ea3616337787104888b5ee Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:12:05 +0100 Subject: [PATCH] COPY Report 2026-10.17. COPY(d, s) makes each side a slice in the frame (CopyDest before s is read, CopyFrom after), checks LEN(s) against LEN(d) as a slice is checked, and copies the bytes with rep movsb, going down (std) when the destination is above the source, so an overlap is right. The library's copying loops are COPYs now: Buf's growing and adding, Str.copy, Sys's paths. conform/run/Copy, conform/err/CopyInto. rat on 26 MB: 731 ms to 674, Buf gone from its profile. --- diff --git a/CLAUDE.md b/CLAUDE.md index afb341b..fedbec0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -87,13 +87,13 @@ 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.16, +by the owner's word: report 2026-10.12; the compiler follows 2026-10.17, 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), also by the owner's word. Its parser +(10.16), and `COPY` (10.17), also by the owner's word. Its parser reaches the machine only through `gen.pas`; its steps 5 to 10 await the owner's review, and git has them. Keep `doc/compiler.md`'s table of lines and its routine names true when the diff --git a/conform/err/CopyInto.raz b/conform/err/CopyInto.raz new file mode 100644 index 0000000..fb70e44 --- /dev/null +++ b/conform/err/CopyInto.raz @@ -0,0 +1,8 @@ +module CopyInto; +proc main*; +var a: [3]I64; +begin + COPY("abc", "x"); + COPY(a, "abc"); +end; +end CopyInto. diff --git a/conform/run/Copy.expect b/conform/run/Copy.expect new file mode 100644 index 0000000..eb7e952 --- /dev/null +++ b/conform/run/Copy.expect @@ -0,0 +1,5 @@ +ababcdehij +defghijhij +defghijhXY +5 +98 diff --git a/conform/run/Copy.raz b/conform/run/Copy.raz new file mode 100644 index 0000000..eed4f80 --- /dev/null +++ b/conform/run/Copy.raz @@ -0,0 +1,29 @@ +module Copy; +import Out; +type P = record x, y: I32; end; +var a: [10]U8; b: []U8; ps: [4]P; qs: [2]P; big: [100]I64; i: INT; +proc show(s: []U8); +begin + Out.str(s); Out.ln; +end; +proc main*; +begin + a := "abcdefghij"; + COPY(a[2..10], a[0..5]); (* overlap, up *) + show(a[0..10]); + a := "abcdefghij"; + COPY(a, a[3..10]); (* overlap, down *) + show(a[0..10]); + b := a[0..10]; + COPY(b[8..10], "XY"); + show(a[0..10]); + COPY(b, ""); + qs := [{x: 1, y: 2}, {x: 3, y: 4}]; + COPY(ps[1..4], qs); + Out.int(I64(ps[1].x + ps[2].y)); Out.ln; + for i := 0 to 99 do big[i] := i; end; + COPY(big[1..100], big[0..99]); + Out.int(big[99] + big[1] + big[0]); Out.ln; + COPY(a[0..2], "abc"); +end; +end Copy. diff --git a/conform/run/Copy.status b/conform/run/Copy.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/Copy.status @@ -0,0 +1 @@ +2 diff --git a/doc/compiler.md b/doc/compiler.md index ae77bd6..1e9dcae 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -10,12 +10,12 @@ |---|---|---|---| | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | -| `src/sym.pas` | 616 | 511 | Types, objects, scopes, layout | +| `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | | `src/chunk.pas` | 300 | 234 | The compiled program: chunks, fixups, line notes | -| `src/x64.pas` | 1,108 | 926 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,536 | 1,203 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,412 | 1,178 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,895 | 2,566 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | +| `src/gen.pas` | 1,570 | 1,232 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/item.pas` | 1,433 | 1,194 | Items: types, constants, variables, operators, conversions, calls | +| `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 197 | 147 | The command line | @@ -274,7 +274,9 @@ - operations on the values on top: `Op`, `Compare`, `Neg`, `Complement`, `NotBool`, `Narrow` (to a narrow integer's width), the conversions between integers and reals, `CompareSeq` (of two - slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`; + slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`, and + `CopySlice` for `COPY`, a `rep movsb` that goes down when the + destination is above the source; - jumps: `Jump`, `JumpIf` (on a `BOOL`), `JumpCmp` (on two integers), `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for `and` and `or`, `Fix` where the label is, and `JoinJumps`; diff --git a/lib/Buf.raz b/lib/Buf.raz index bceb370..590ffb1 100644 --- a/lib/Buf.raz +++ b/lib/Buf.raz @@ -17,7 +17,7 @@ (* Room for n more bytes; false if the allocator has none. *) proc room*(b: ^Buf; n: INT): BOOL; -var cap, i: INT; p: ^U8; +var cap: INT; p: ^U8; begin if b^.len + n <= b^.cap then return true; end; cap := b^.cap * 2; @@ -25,7 +25,7 @@ while cap < b^.len + n do cap := cap * 2; end; p := Mem.alloc(b^.a, cap, 1); if p = nil then return false; end; - for i := 0 to b^.len - 1 do p[i] := b^.data[i]; end; + if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; if b^.data # nil then Mem.free(b^.a, b^.data); end; b^.data := p; b^.cap := cap; @@ -45,10 +45,9 @@ (* Adds the bytes of s at the end; if room for them can't be had, none is added. *) proc str*(b: ^Buf; s: []U8); -var i: INT; begin if room(b, LEN(s)) then - for i := 0 to LEN(s) - 1 do b^.data[b^.len + i] := s[i]; end; + COPY(b^.data[b^.len..b^.len + LEN(s)], s); INC(b^.len, LEN(s)); end; end; diff --git a/lib/Str.raz b/lib/Str.raz index 2804abd..1365b75 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -9,12 +9,12 @@ (* s's bytes, copied into memory from a, to keep after s's own are gone; "" and false if a has no room. *) proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); -var p: ^U8; i: INT; +var p: ^U8; begin if LEN(s) = 0 then return "", true; end; p := Mem.alloc(a, LEN(s), 1); if p = nil then return "", false; end; - for i := 0 to LEN(s) - 1 do p[i] := s[i]; end; + COPY(p[0..LEN(s)], s); return p[0..LEN(s)], true; end; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 932353a..21ca79f 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -99,10 +99,9 @@ (* path, a C string in buf, or false if it doesn't fit. *) proc cstr(path, buf: []U8): BOOL; -var i: INT; begin if LEN(path) >= LEN(buf) then return false; end; - for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; + COPY(buf, path); buf[LEN(path)] := 0; return true; end; @@ -421,11 +420,10 @@ (* path as a Unix socket's address in a, or false if it is too long. *) proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; -var i: INT; begin if LEN(path) >= LEN(a^.path) then return false; end; a^.family := 1; (* AF_UNIX *) - for i := 0 to LEN(path) - 1 do a^.path[i] := path[i]; end; + COPY(a^.path, path); return true; end; @@ -699,9 +697,8 @@ (* Copies s and a zero to at: the byte after them. *) proc put(s: []U8; at: ^U8): ^U8; -var i: INT; begin - for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; + COPY(at[0..LEN(s)], s); at[LEN(s)] := 0; return at + (LEN(s) + 1); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index a144a53..7703997 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -127,10 +127,9 @@ (* path, a C string in buf, or false if it doesn't fit. *) proc cstr(path, buf: []U8): BOOL; -var i: INT; begin if LEN(path) >= LEN(buf) then return false; end; - for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; + COPY(buf, path); buf[LEN(path)] := 0; return true; end; @@ -441,11 +440,10 @@ (* path as a Unix socket's address in a, or false if it is too long. *) proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; -var i: INT; begin if LEN(path) >= LEN(a^.path) then return false; end; a^.family := 1; (* AF_UNIX *) - for i := 0 to LEN(path) - 1 do a^.path[i] := path[i]; end; + COPY(a^.path, path); return true; end; @@ -642,9 +640,8 @@ (* Copies s and a zero to at: the byte after them. *) proc put(s: []U8; at: ^U8): ^U8; -var i: INT; begin - for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; + COPY(at[0..LEN(s)], s); at[LEN(s)] := 0; return at + (LEN(s) + 1); end; diff --git a/src/gen.pas b/src/gen.pas index 28fedea..b2866bd 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -147,6 +147,7 @@ procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); +procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); procedure PopArg(var c: TCall); procedure Call(var c: TCall); @@ -1083,6 +1084,39 @@ if isArray then Lea(RAX, m) else Load(RAX, m, 8, false); Alu(aluAdd, RAX, RCX); Store(Mem(RBP, tmp), RAX, 8); +end; + +{ COPY: the elements of the slice in the frame at sOff, of size bytes + each, to the start of the one at dOff, which must be as long, as one + block; backwards when the destination is above the source, so that + an overlap comes out right. } +procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); +var ml: TMem; L, E: longint; +begin + Load(RDX, Mem(RBP, sOff + 8), 8, false); + if checked then begin + ml := Mem(RBP, dOff + 8); + MovRI(RAX, 0); + AluM(aluCmp, RDX, ml); + Check(ccBE, 1, p, RAX, ml, 0, -1); + end; + MovRR(RCX, RDX); + if size <> 1 then ImulI(RCX, size); + Load(RSI, Mem(RBP, sOff), 8, false); + Load(RDI, Mem(RBP, dOff), 8, false); + Alu(aluCmp, RDI, RSI); + L := NoLabel; + Jcc(ccBE, L); + Lea(RSI, MemIdx(RSI, RCX, 1, -1)); + Lea(RDI, MemIdx(RDI, RCX, 1, -1)); + Std; + RepMovsb; + Cld; + E := NoLabel; + Jmp(E); + FixLink(L); + RepMovsb; + FixLink(E); end; { Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its diff --git a/src/item.pas b/src/item.pas index a4cb205..03691dc 100644 --- a/src/item.pas +++ b/src/item.pas @@ -112,6 +112,8 @@ procedure ReturnNone; procedure Pin(var x: TItem); procedure IncDec(var v, k: TItem; dec: boolean); +procedure CopyDest(var d: TItem); +procedure CopyFrom(var d, s: TItem); procedure Msg(var x: TItem); function ErrorChunk(var x: TItem; module, name: longint): longint; @@ -1383,6 +1385,25 @@ Value(k); if v.base = bReg then IncDecReg(v.chunk, ScalarSize(v.typ), v.typ^.signed, dec) else gen.IncDec(ScalarSize(v.typ), v.typ^.signed, dec); +end; + +{ COPY(d, s): d, an array variable or a slice, as a slice in the frame, + before s is read } +procedure CopyDest(var d: TItem); +begin + if not IsSeq(d.typ) or (d.mode = mStr) or (d.typ^.form = fArray) and ((d.mode <> mVar) or not d.isVar) then + MarkAt(d.pos, 'COPY copies into an array variable or a slice, found ' + What(d)); + if d.typ^.form = fArray then NotReadOnly(d); + SeqSlice(d); +end; + +{ ... and the elements of s copied to its start } +procedure CopyFrom(var d, s: TItem); +begin + if not IsSeq(s.typ) or not Same(ElemOf(s.typ), d.typ^.base) then + MarkAt(s.pos, 'COPY copies elements of ' + TypeName(d.typ^.base) + ', found ' + What(s)); + SeqSlice(s); + CopySlice(d.off, s.off, d.typ^.base^.size, checks, s.pos); end; { An error is the address of its length, followed by its message. } diff --git a/src/parse.pas b/src/parse.pas index e8ec1f4..571a7fe 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -840,6 +840,15 @@ x.mode := mNone; end; bNew: New_(x); + bCopy: begin + Expression(x, nil); + CopyDest(x); + Expect(sComma); + Expression(y, nil); + CopyFrom(x, y); + MakeConst(x, tNone, 0); + x.mode := mNone; + end; end; Expect(sRparen); end; diff --git a/src/raz.pas b/src/raz.pas index c13126e..7cbcb4b 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.16'; { the version of doc/raz.md it follows } + Report = '2026-10.17'; { 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/src/sym.pas b/src/sym.pas index b43285a..ec8b27a 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -125,7 +125,7 @@ { built-in procedures } bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5; bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11; - bInc = 12; bDec = 13; + bInc = 12; bDec = 13; bCopy = 14; MaxDepth = 64; { scopes open at once } @@ -613,4 +613,5 @@ Enter('ARGV', oBuiltin, nil, bArgv); Enter('INC', oBuiltin, nil, bInc); Enter('DEC', oBuiltin, nil, bDec); + Enter('COPY', oBuiltin, nil, bCopy); end. diff --git a/src/x64.pas b/src/x64.pas index 01a87c1..05d8beb 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -83,6 +83,8 @@ procedure Mfence; procedure Ud2; procedure RepMovsb; +procedure Std; +procedure Cld; procedure RepStosb; procedure RepeCmpsb; procedure LockCmpxchg(const m: TMem; r: longint); @@ -561,6 +563,9 @@ procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end; procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end; procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end; +{ the direction flag, which has rep movsb go down, and back up } +procedure Std; begin Say('std'); Put1($FD) end; +procedure Cld; begin Say('cld'); Put1($FC) end; procedure RepStosb; begin Say('rep stosb'); Put1($F3); Put1($AA) end; procedure RepeCmpsb; begin Say('repe cmpsb'); Put1($F3); Put1($A6) end; diff --git a/test/msg/Copy.stderr b/test/msg/Copy.stderr new file mode 100644 index 0000000..c8b0e64 --- /dev/null +++ b/test/msg/Copy.stderr @@ -0,0 +1,2 @@ +conform/run/Copy.raz:27:19: slice 0..3 is out of range for a length of 2 + in Copy.main at conform/run/Copy.raz:27:19 diff --git a/test/msg/CopyInto.err b/test/msg/CopyInto.err new file mode 100644 index 0000000..2b723a0 --- /dev/null +++ b/test/msg/CopyInto.err @@ -0,0 +1,7 @@ +conform/err/CopyInto.raz:5:10: error: COPY copies into an array variable or a slice, found a string + 5 | COPY("abc", "x"); + | ^^^^^ +conform/err/CopyInto.raz:6:13: error: COPY copies elements of I64, found a string + 6 | COPY(a, "abc"); + | ^^^^^ +raz: 2 errors