raz / commit d8137081

commit
d8137081de1e4f8571ea3616337787104888b5ee
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
60173e50
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.

patch · log

18 files changed, +145 -28

CLAUDE.md modified +2 -2

@@ -87,13 +87,13 @@
8787 defect, and the library grows only by what breaks no program written
8888 for it. A wish for more, the owner's or yours, goes on the list in
8989 `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,
9191 whose `Sys` writes without waiting, for rat's pastes (10.13), and
9292 whose blank, `_ := f(x)`, drops a result that is otherwise warned of
9393 (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10,
9494 rounding and the inverse functions, and `Sys` `space` (10.15), and
9595 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
9797 reaches the machine only through `gen.pas`; its steps 5 to 10 await
9898 the owner's review, and git has them. Keep
9999 `doc/compiler.md`'s table of lines and its routine names true when the

conform/err/CopyInto.raz (history) added +8 -0

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

conform/run/Copy.expect (history) added +5 -0

@@ -0,0 +1,5 @@
1+ababcdehij
2+defghijhij
3+defghijhXY
4+5
5+98

conform/run/Copy.raz (history) added +29 -0

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

conform/run/Copy.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

doc/compiler.md (history) modified +8 -6

@@ -10,12 +10,12 @@
1010 |---|---|---|---|
1111 | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts |
1212 | `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 |
1414 | `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 |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 197 | 147 | The command line |
@@ -274,7 +274,9 @@
274274 - operations on the values on top: `Op`, `Compare`, `Neg`,
275275 `Complement`, `NotBool`, `Narrow` (to a narrow integer's width),
276276 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;
278280 - jumps: `Jump`, `JumpIf` (on a `BOOL`), `JumpCmp` (on two integers),
279281 `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for
280282 `and` and `or`, `Fix` where the label is, and `JoinJumps`;

lib/Buf.raz (history) modified +3 -4

@@ -17,7 +17,7 @@
1717
1818 (* Room for n more bytes; false if the allocator has none. *)
1919 proc room*(b: ^Buf; n: INT): BOOL;
20-var cap, i: INT; p: ^U8;
20+var cap: INT; p: ^U8;
2121 begin
2222 if b^.len + n <= b^.cap then return true; end;
2323 cap := b^.cap * 2;
@@ -25,7 +25,7 @@
2525 while cap < b^.len + n do cap := cap * 2; end;
2626 p := Mem.alloc(b^.a, cap, 1);
2727 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;
2929 if b^.data # nil then Mem.free(b^.a, b^.data); end;
3030 b^.data := p;
3131 b^.cap := cap;
@@ -45,10 +45,9 @@
4545 (* Adds the bytes of s at the end; if room for them can't be had, none
4646 is added. *)
4747 proc str*(b: ^Buf; s: []U8);
48-var i: INT;
4948 begin
5049 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);
5251 INC(b^.len, LEN(s));
5352 end;
5453 end;

lib/Str.raz (history) modified +2 -2

@@ -9,12 +9,12 @@
99 (* s's bytes, copied into memory from a, to keep after s's own are
1010 gone; "" and false if a has no room. *)
1111 proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL);
12-var p: ^U8; i: INT;
12+var p: ^U8;
1313 begin
1414 if LEN(s) = 0 then return "", true; end;
1515 p := Mem.alloc(a, LEN(s), 1);
1616 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);
1818 return p[0..LEN(s)], true;
1919 end;
2020

lib/Sys.linux-x86_64.raz (history) modified +3 -6

@@ -99,10 +99,9 @@
9999
100100 (* path, a C string in buf, or false if it doesn't fit. *)
101101 proc cstr(path, buf: []U8): BOOL;
102-var i: INT;
103102 begin
104103 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);
106105 buf[LEN(path)] := 0;
107106 return true;
108107 end;
@@ -421,11 +420,10 @@
421420
422421 (* path as a Unix socket's address in a, or false if it is too long. *)
423422 proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL;
424-var i: INT;
425423 begin
426424 if LEN(path) >= LEN(a^.path) then return false; end;
427425 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);
429427 return true;
430428 end;
431429
@@ -699,9 +697,8 @@
699697
700698 (* Copies s and a zero to at: the byte after them. *)
701699 proc put(s: []U8; at: ^U8): ^U8;
702-var i: INT;
703700 begin
704- for i := 0 to LEN(s) - 1 do at[i] := s[i]; end;
701+ COPY(at[0..LEN(s)], s);
705702 at[LEN(s)] := 0;
706703 return at + (LEN(s) + 1);
707704 end;

lib/Sys.raz (history) modified +3 -6

@@ -127,10 +127,9 @@
127127
128128 (* path, a C string in buf, or false if it doesn't fit. *)
129129 proc cstr(path, buf: []U8): BOOL;
130-var i: INT;
131130 begin
132131 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);
134133 buf[LEN(path)] := 0;
135134 return true;
136135 end;
@@ -441,11 +440,10 @@
441440
442441 (* path as a Unix socket's address in a, or false if it is too long. *)
443442 proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL;
444-var i: INT;
445443 begin
446444 if LEN(path) >= LEN(a^.path) then return false; end;
447445 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);
449447 return true;
450448 end;
451449
@@ -642,9 +640,8 @@
642640
643641 (* Copies s and a zero to at: the byte after them. *)
644642 proc put(s: []U8; at: ^U8): ^U8;
645-var i: INT;
646643 begin
647- for i := 0 to LEN(s) - 1 do at[i] := s[i]; end;
644+ COPY(at[0..LEN(s)], s);
648645 at[LEN(s)] := 0;
649646 return at + (LEN(s) + 1);
650647 end;

src/gen.pas modified +34 -0

@@ -147,6 +147,7 @@
147147 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
148148 procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos);
149149 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
150+procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos);
150151
151152 procedure PopArg(var c: TCall);
152153 procedure Call(var c: TCall);
@@ -1083,6 +1084,39 @@
10831084 if isArray then Lea(RAX, m) else Load(RAX, m, 8, false);
10841085 Alu(aluAdd, RAX, RCX);
10851086 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);
10861120 end;
10871121
10881122 { Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its

src/item.pas modified +21 -0

@@ -112,6 +112,8 @@
112112 procedure ReturnNone;
113113 procedure Pin(var x: TItem);
114114 procedure IncDec(var v, k: TItem; dec: boolean);
115+procedure CopyDest(var d: TItem);
116+procedure CopyFrom(var d, s: TItem);
115117 procedure Msg(var x: TItem);
116118 function ErrorChunk(var x: TItem; module, name: longint): longint;
117119
@@ -1383,6 +1385,25 @@
13831385 Value(k);
13841386 if v.base = bReg then IncDecReg(v.chunk, ScalarSize(v.typ), v.typ^.signed, dec)
13851387 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);
13861407 end;
13871408
13881409 { An error is the address of its length, followed by its message. }

src/parse.pas modified +9 -0

@@ -840,6 +840,15 @@
840840 x.mode := mNone;
841841 end;
842842 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;
843852 end;
844853 Expect(sRparen);
845854 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.16'; { the version of doc/raz.md it follows }
37+ Report = '2026-10.17'; { 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

src/sym.pas modified +2 -1

@@ -125,7 +125,7 @@
125125 { built-in procedures }
126126 bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5;
127127 bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11;
128- bInc = 12; bDec = 13;
128+ bInc = 12; bDec = 13; bCopy = 14;
129129
130130 MaxDepth = 64; { scopes open at once }
131131
@@ -613,4 +613,5 @@
613613 Enter('ARGV', oBuiltin, nil, bArgv);
614614 Enter('INC', oBuiltin, nil, bInc);
615615 Enter('DEC', oBuiltin, nil, bDec);
616+ Enter('COPY', oBuiltin, nil, bCopy);
616617 end.

src/x64.pas modified +5 -0

@@ -83,6 +83,8 @@
8383 procedure Mfence;
8484 procedure Ud2;
8585 procedure RepMovsb;
86+procedure Std;
87+procedure Cld;
8688 procedure RepStosb;
8789 procedure RepeCmpsb;
8890 procedure LockCmpxchg(const m: TMem; r: longint);
@@ -561,6 +563,9 @@
561563 procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end;
562564 procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end;
563565 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;
564569 procedure RepStosb; begin Say('rep stosb'); Put1($F3); Put1($AA) end;
565570 procedure RepeCmpsb; begin Say('repe cmpsb'); Put1($F3); Put1($A6) end;
566571

test/msg/Copy.stderr (history) added +2 -0

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

test/msg/CopyInto.err (history) added +7 -0

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