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.
| @@ -87,13 +87,13 @@ | ||
| 87 | 87 | defect, and the library grows only by what breaks no program written |
| 88 | 88 | for it. A wish for more, the owner's or yours, goes on the list in |
| 89 | 89 | `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, |
| 90 | -by the owner's word: report 2026-10.12; the compiler follows 2026-10.16, | |
| 90 | +by the owner's word: report 2026-10.12; the compiler follows 2026-10.17, | |
| 91 | 91 | whose `Sys` writes without waiting, for rat's pastes (10.13), and |
| 92 | 92 | whose blank, `_ := f(x)`, drops a result that is otherwise warned of |
| 93 | 93 | (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, |
| 94 | 94 | rounding and the inverse functions, and `Sys` `space` (10.15), and |
| 95 | 95 | whose constants may be records and tables of them, with `Str.copy` |
| 96 | -(10.16), also by the owner's word. Its parser | |
| 96 | +(10.16), and `COPY` (10.17), also by the owner's word. Its parser | |
| 97 | 97 | reaches the machine only through `gen.pas`; its steps 5 to 10 await |
| 98 | 98 | the owner's review, and git has them. Keep |
| 99 | 99 | `doc/compiler.md`'s table of lines and its routine names true when the |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module CopyInto; | |
| 2 | +proc main*; | |
| 3 | +var a: [3]I64; | |
| 4 | +begin | |
| 5 | + COPY("abc", "x"); | |
| 6 | + COPY(a, "abc"); | |
| 7 | +end; | |
| 8 | +end CopyInto. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +ababcdehij | |
| 2 | +defghijhij | |
| 3 | +defghijhXY | |
| 4 | +5 | |
| 5 | +98 |
| @@ -0,0 +1,29 @@ | ||
| 1 | +module Copy; | |
| 2 | +import Out; | |
| 3 | +type P = record x, y: I32; end; | |
| 4 | +var a: [10]U8; b: []U8; ps: [4]P; qs: [2]P; big: [100]I64; i: INT; | |
| 5 | +proc show(s: []U8); | |
| 6 | +begin | |
| 7 | + Out.str(s); Out.ln; | |
| 8 | +end; | |
| 9 | +proc main*; | |
| 10 | +begin | |
| 11 | + a := "abcdefghij"; | |
| 12 | + COPY(a[2..10], a[0..5]); (* overlap, up *) | |
| 13 | + show(a[0..10]); | |
| 14 | + a := "abcdefghij"; | |
| 15 | + COPY(a, a[3..10]); (* overlap, down *) | |
| 16 | + show(a[0..10]); | |
| 17 | + b := a[0..10]; | |
| 18 | + COPY(b[8..10], "XY"); | |
| 19 | + show(a[0..10]); | |
| 20 | + COPY(b, ""); | |
| 21 | + qs := [{x: 1, y: 2}, {x: 3, y: 4}]; | |
| 22 | + COPY(ps[1..4], qs); | |
| 23 | + Out.int(I64(ps[1].x + ps[2].y)); Out.ln; | |
| 24 | + for i := 0 to 99 do big[i] := i; end; | |
| 25 | + COPY(big[1..100], big[0..99]); | |
| 26 | + Out.int(big[99] + big[1] + big[0]); Out.ln; | |
| 27 | + COPY(a[0..2], "abc"); | |
| 28 | +end; | |
| 29 | +end Copy. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -10,12 +10,12 @@ | ||
| 10 | 10 | |---|---|---|---| |
| 11 | 11 | | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | |
| 13 | -| `src/sym.pas` | 616 | 511 | Types, objects, scopes, layout | | |
| 13 | +| `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | | |
| 14 | 14 | | `src/chunk.pas` | 300 | 234 | The compiled program: chunks, fixups, line notes | |
| 15 | -| `src/x64.pas` | 1,108 | 926 | The instruction encoder, jumps within a procedure, the assembler | | |
| 16 | -| `src/gen.pas` | 1,536 | 1,203 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | -| `src/item.pas` | 1,412 | 1,178 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | -| `src/parse.pas` | 2,895 | 2,566 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 15 | +| `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | | |
| 16 | +| `src/gen.pas` | 1,570 | 1,232 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | +| `src/item.pas` | 1,433 | 1,194 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | +| `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | 19 | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | |
| 20 | 20 | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 21 | 21 | | `src/raz.pas` | 197 | 147 | The command line | |
| @@ -274,7 +274,9 @@ | ||
| 274 | 274 | - operations on the values on top: `Op`, `Compare`, `Neg`, |
| 275 | 275 | `Complement`, `NotBool`, `Narrow` (to a narrow integer's width), |
| 276 | 276 | the conversions between integers and reals, `CompareSeq` (of two |
| 277 | - slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`; | |
| 277 | + slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`, and | |
| 278 | + `CopySlice` for `COPY`, a `rep movsb` that goes down when the | |
| 279 | + destination is above the source; | |
| 278 | 280 | - jumps: `Jump`, `JumpIf` (on a `BOOL`), `JumpCmp` (on two integers), |
| 279 | 281 | `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for |
| 280 | 282 | `and` and `or`, `Fix` where the label is, and `JoinJumps`; |
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | (* Room for n more bytes; false if the allocator has none. *) |
| 19 | 19 | proc room*(b: ^Buf; n: INT): BOOL; |
| 20 | -var cap, i: INT; p: ^U8; | |
| 20 | +var cap: INT; p: ^U8; | |
| 21 | 21 | begin |
| 22 | 22 | if b^.len + n <= b^.cap then return true; end; |
| 23 | 23 | cap := b^.cap * 2; |
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | while cap < b^.len + n do cap := cap * 2; end; |
| 26 | 26 | p := Mem.alloc(b^.a, cap, 1); |
| 27 | 27 | if p = nil then return false; end; |
| 28 | - for i := 0 to b^.len - 1 do p[i] := b^.data[i]; end; | |
| 28 | + if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; | |
| 29 | 29 | if b^.data # nil then Mem.free(b^.a, b^.data); end; |
| 30 | 30 | b^.data := p; |
| 31 | 31 | b^.cap := cap; |
| @@ -45,10 +45,9 @@ | ||
| 45 | 45 | (* Adds the bytes of s at the end; if room for them can't be had, none |
| 46 | 46 | is added. *) |
| 47 | 47 | proc str*(b: ^Buf; s: []U8); |
| 48 | -var i: INT; | |
| 49 | 48 | begin |
| 50 | 49 | if room(b, LEN(s)) then |
| 51 | - for i := 0 to LEN(s) - 1 do b^.data[b^.len + i] := s[i]; end; | |
| 50 | + COPY(b^.data[b^.len..b^.len + LEN(s)], s); | |
| 52 | 51 | INC(b^.len, LEN(s)); |
| 53 | 52 | end; |
| 54 | 53 | end; |
| @@ -9,12 +9,12 @@ | ||
| 9 | 9 | (* s's bytes, copied into memory from a, to keep after s's own are |
| 10 | 10 | gone; "" and false if a has no room. *) |
| 11 | 11 | proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); |
| 12 | -var p: ^U8; i: INT; | |
| 12 | +var p: ^U8; | |
| 13 | 13 | begin |
| 14 | 14 | if LEN(s) = 0 then return "", true; end; |
| 15 | 15 | p := Mem.alloc(a, LEN(s), 1); |
| 16 | 16 | if p = nil then return "", false; end; |
| 17 | - for i := 0 to LEN(s) - 1 do p[i] := s[i]; end; | |
| 17 | + COPY(p[0..LEN(s)], s); | |
| 18 | 18 | return p[0..LEN(s)], true; |
| 19 | 19 | end; |
| 20 | 20 |
| @@ -99,10 +99,9 @@ | ||
| 99 | 99 | |
| 100 | 100 | (* path, a C string in buf, or false if it doesn't fit. *) |
| 101 | 101 | proc cstr(path, buf: []U8): BOOL; |
| 102 | -var i: INT; | |
| 103 | 102 | begin |
| 104 | 103 | if LEN(path) >= LEN(buf) then return false; end; |
| 105 | - for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; | |
| 104 | + COPY(buf, path); | |
| 106 | 105 | buf[LEN(path)] := 0; |
| 107 | 106 | return true; |
| 108 | 107 | end; |
| @@ -421,11 +420,10 @@ | ||
| 421 | 420 | |
| 422 | 421 | (* path as a Unix socket's address in a, or false if it is too long. *) |
| 423 | 422 | proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; |
| 424 | -var i: INT; | |
| 425 | 423 | begin |
| 426 | 424 | if LEN(path) >= LEN(a^.path) then return false; end; |
| 427 | 425 | a^.family := 1; (* AF_UNIX *) |
| 428 | - for i := 0 to LEN(path) - 1 do a^.path[i] := path[i]; end; | |
| 426 | + COPY(a^.path, path); | |
| 429 | 427 | return true; |
| 430 | 428 | end; |
| 431 | 429 | |
| @@ -699,9 +697,8 @@ | ||
| 699 | 697 | |
| 700 | 698 | (* Copies s and a zero to at: the byte after them. *) |
| 701 | 699 | proc put(s: []U8; at: ^U8): ^U8; |
| 702 | -var i: INT; | |
| 703 | 700 | begin |
| 704 | - for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; | |
| 701 | + COPY(at[0..LEN(s)], s); | |
| 705 | 702 | at[LEN(s)] := 0; |
| 706 | 703 | return at + (LEN(s) + 1); |
| 707 | 704 | end; |
| @@ -127,10 +127,9 @@ | ||
| 127 | 127 | |
| 128 | 128 | (* path, a C string in buf, or false if it doesn't fit. *) |
| 129 | 129 | proc cstr(path, buf: []U8): BOOL; |
| 130 | -var i: INT; | |
| 131 | 130 | begin |
| 132 | 131 | if LEN(path) >= LEN(buf) then return false; end; |
| 133 | - for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; | |
| 132 | + COPY(buf, path); | |
| 134 | 133 | buf[LEN(path)] := 0; |
| 135 | 134 | return true; |
| 136 | 135 | end; |
| @@ -441,11 +440,10 @@ | ||
| 441 | 440 | |
| 442 | 441 | (* path as a Unix socket's address in a, or false if it is too long. *) |
| 443 | 442 | proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; |
| 444 | -var i: INT; | |
| 445 | 443 | begin |
| 446 | 444 | if LEN(path) >= LEN(a^.path) then return false; end; |
| 447 | 445 | a^.family := 1; (* AF_UNIX *) |
| 448 | - for i := 0 to LEN(path) - 1 do a^.path[i] := path[i]; end; | |
| 446 | + COPY(a^.path, path); | |
| 449 | 447 | return true; |
| 450 | 448 | end; |
| 451 | 449 | |
| @@ -642,9 +640,8 @@ | ||
| 642 | 640 | |
| 643 | 641 | (* Copies s and a zero to at: the byte after them. *) |
| 644 | 642 | proc put(s: []U8; at: ^U8): ^U8; |
| 645 | -var i: INT; | |
| 646 | 643 | begin |
| 647 | - for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; | |
| 644 | + COPY(at[0..LEN(s)], s); | |
| 648 | 645 | at[LEN(s)] := 0; |
| 649 | 646 | return at + (LEN(s) + 1); |
| 650 | 647 | end; |
| @@ -147,6 +147,7 @@ | ||
| 147 | 147 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 148 | 148 | procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); |
| 149 | 149 | procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); |
| 150 | +procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); | |
| 150 | 151 | |
| 151 | 152 | procedure PopArg(var c: TCall); |
| 152 | 153 | procedure Call(var c: TCall); |
| @@ -1083,6 +1084,39 @@ | ||
| 1083 | 1084 | if isArray then Lea(RAX, m) else Load(RAX, m, 8, false); |
| 1084 | 1085 | Alu(aluAdd, RAX, RCX); |
| 1085 | 1086 | Store(Mem(RBP, tmp), RAX, 8); |
| 1087 | +end; | |
| 1088 | + | |
| 1089 | +{ COPY: the elements of the slice in the frame at sOff, of size bytes | |
| 1090 | + each, to the start of the one at dOff, which must be as long, as one | |
| 1091 | + block; backwards when the destination is above the source, so that | |
| 1092 | + an overlap comes out right. } | |
| 1093 | +procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); | |
| 1094 | +var ml: TMem; L, E: longint; | |
| 1095 | +begin | |
| 1096 | + Load(RDX, Mem(RBP, sOff + 8), 8, false); | |
| 1097 | + if checked then begin | |
| 1098 | + ml := Mem(RBP, dOff + 8); | |
| 1099 | + MovRI(RAX, 0); | |
| 1100 | + AluM(aluCmp, RDX, ml); | |
| 1101 | + Check(ccBE, 1, p, RAX, ml, 0, -1); | |
| 1102 | + end; | |
| 1103 | + MovRR(RCX, RDX); | |
| 1104 | + if size <> 1 then ImulI(RCX, size); | |
| 1105 | + Load(RSI, Mem(RBP, sOff), 8, false); | |
| 1106 | + Load(RDI, Mem(RBP, dOff), 8, false); | |
| 1107 | + Alu(aluCmp, RDI, RSI); | |
| 1108 | + L := NoLabel; | |
| 1109 | + Jcc(ccBE, L); | |
| 1110 | + Lea(RSI, MemIdx(RSI, RCX, 1, -1)); | |
| 1111 | + Lea(RDI, MemIdx(RDI, RCX, 1, -1)); | |
| 1112 | + Std; | |
| 1113 | + RepMovsb; | |
| 1114 | + Cld; | |
| 1115 | + E := NoLabel; | |
| 1116 | + Jmp(E); | |
| 1117 | + FixLink(L); | |
| 1118 | + RepMovsb; | |
| 1119 | + FixLink(E); | |
| 1086 | 1120 | end; |
| 1087 | 1121 | |
| 1088 | 1122 | { Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its |
| @@ -112,6 +112,8 @@ | ||
| 112 | 112 | procedure ReturnNone; |
| 113 | 113 | procedure Pin(var x: TItem); |
| 114 | 114 | procedure IncDec(var v, k: TItem; dec: boolean); |
| 115 | +procedure CopyDest(var d: TItem); | |
| 116 | +procedure CopyFrom(var d, s: TItem); | |
| 115 | 117 | procedure Msg(var x: TItem); |
| 116 | 118 | function ErrorChunk(var x: TItem; module, name: longint): longint; |
| 117 | 119 | |
| @@ -1383,6 +1385,25 @@ | ||
| 1383 | 1385 | Value(k); |
| 1384 | 1386 | if v.base = bReg then IncDecReg(v.chunk, ScalarSize(v.typ), v.typ^.signed, dec) |
| 1385 | 1387 | else gen.IncDec(ScalarSize(v.typ), v.typ^.signed, dec); |
| 1388 | +end; | |
| 1389 | + | |
| 1390 | +{ COPY(d, s): d, an array variable or a slice, as a slice in the frame, | |
| 1391 | + before s is read } | |
| 1392 | +procedure CopyDest(var d: TItem); | |
| 1393 | +begin | |
| 1394 | + if not IsSeq(d.typ) or (d.mode = mStr) or (d.typ^.form = fArray) and ((d.mode <> mVar) or not d.isVar) then | |
| 1395 | + MarkAt(d.pos, 'COPY copies into an array variable or a slice, found ' + What(d)); | |
| 1396 | + if d.typ^.form = fArray then NotReadOnly(d); | |
| 1397 | + SeqSlice(d); | |
| 1398 | +end; | |
| 1399 | + | |
| 1400 | +{ ... and the elements of s copied to its start } | |
| 1401 | +procedure CopyFrom(var d, s: TItem); | |
| 1402 | +begin | |
| 1403 | + if not IsSeq(s.typ) or not Same(ElemOf(s.typ), d.typ^.base) then | |
| 1404 | + MarkAt(s.pos, 'COPY copies elements of ' + TypeName(d.typ^.base) + ', found ' + What(s)); | |
| 1405 | + SeqSlice(s); | |
| 1406 | + CopySlice(d.off, s.off, d.typ^.base^.size, checks, s.pos); | |
| 1386 | 1407 | end; |
| 1387 | 1408 | |
| 1388 | 1409 | { An error is the address of its length, followed by its message. } |
| @@ -840,6 +840,15 @@ | ||
| 840 | 840 | x.mode := mNone; |
| 841 | 841 | end; |
| 842 | 842 | bNew: New_(x); |
| 843 | + bCopy: begin | |
| 844 | + Expression(x, nil); | |
| 845 | + CopyDest(x); | |
| 846 | + Expect(sComma); | |
| 847 | + Expression(y, nil); | |
| 848 | + CopyFrom(x, y); | |
| 849 | + MakeConst(x, tNone, 0); | |
| 850 | + x.mode := mNone; | |
| 851 | + end; | |
| 843 | 852 | end; |
| 844 | 853 | Expect(sRparen); |
| 845 | 854 | end; |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 35 | 35 | 0.3.0-5-g38d29a8 five commits past it } |
| 36 | 36 | Version = {$I version.inc}; |
| 37 | - Report = '2026-10.16'; { the version of doc/raz.md it follows } | |
| 37 | + Report = '2026-10.17'; { the version of doc/raz.md it follows } | |
| 38 | 38 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 39 | 39 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 40 | 40 |
| @@ -125,7 +125,7 @@ | ||
| 125 | 125 | { built-in procedures } |
| 126 | 126 | bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5; |
| 127 | 127 | bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11; |
| 128 | - bInc = 12; bDec = 13; | |
| 128 | + bInc = 12; bDec = 13; bCopy = 14; | |
| 129 | 129 | |
| 130 | 130 | MaxDepth = 64; { scopes open at once } |
| 131 | 131 | |
| @@ -613,4 +613,5 @@ | ||
| 613 | 613 | Enter('ARGV', oBuiltin, nil, bArgv); |
| 614 | 614 | Enter('INC', oBuiltin, nil, bInc); |
| 615 | 615 | Enter('DEC', oBuiltin, nil, bDec); |
| 616 | + Enter('COPY', oBuiltin, nil, bCopy); | |
| 616 | 617 | end. |
| @@ -83,6 +83,8 @@ | ||
| 83 | 83 | procedure Mfence; |
| 84 | 84 | procedure Ud2; |
| 85 | 85 | procedure RepMovsb; |
| 86 | +procedure Std; | |
| 87 | +procedure Cld; | |
| 86 | 88 | procedure RepStosb; |
| 87 | 89 | procedure RepeCmpsb; |
| 88 | 90 | procedure LockCmpxchg(const m: TMem; r: longint); |
| @@ -561,6 +563,9 @@ | ||
| 561 | 563 | procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end; |
| 562 | 564 | procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end; |
| 563 | 565 | procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end; |
| 566 | +{ the direction flag, which has rep movsb go down, and back up } | |
| 567 | +procedure Std; begin Say('std'); Put1($FD) end; | |
| 568 | +procedure Cld; begin Say('cld'); Put1($FC) end; | |
| 564 | 569 | procedure RepStosb; begin Say('rep stosb'); Put1($F3); Put1($AA) end; |
| 565 | 570 | procedure RepeCmpsb; begin Say('repe cmpsb'); Put1($F3); Put1($A6) end; |
| 566 | 571 |
| @@ -0,0 +1,2 @@ | ||
| 1 | +conform/run/Copy.raz:27:19: slice 0..3 is out of range for a length of 2 | |
| 2 | + in Copy.main at conform/run/Copy.raz:27:19 |
| @@ -0,0 +1,7 @@ | ||
| 1 | +conform/err/CopyInto.raz:5:10: error: COPY copies into an array variable or a slice, found a string | |
| 2 | + 5 | COPY("abc", "x"); | |
| 3 | + | ^^^^^ | |
| 4 | +conform/err/CopyInto.raz:6:13: error: COPY copies elements of I64, found a string | |
| 5 | + 6 | COPY(a, "abc"); | |
| 6 | + | ^^^^^ | |
| 7 | +raz: 2 errors |