raz / commit e224225d

commit
e224225dfc724f01aa79607936274f2378506950
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
62a9b275
Slices in registers

A slice whose address isn't taken may have two of the variables'
registers, its pointer's and its length's (reg2), chosen by the
read-ahead as a single value is, its count halved for each register:
counted whole, a slice took registers from busier single values, and
rat ran 3% slower. Its item names both registers: PushVar reads either
word, IndexSlice and Slice use them as they are, a failed check takes
the length from its register, CopyTo loads both, and what wants its
memory has its words stored in a temporary.

rat 583 -> 557 ms (-4.5%), text -0.1%; heap sort -5%, search -4%,
sum within noise; make check compiles test/bench.

patch · log

13 files changed, +273 -43

PLAN.md (history) modified +1 -1

@@ -19,7 +19,7 @@
1919 Each step is measured as v0.7.0's were, on rat and on its kernel, and
2020 one that buys less than 2% on both is reverted and noted here.
2121
22-1. **Slices in registers.** A slice parameter or variable whose address
22+1. Done: **Slices in registers.** A slice parameter or variable whose address
2323 isn't taken lives in two of the variables' registers, its pointer and
2424 its length, chosen by the read-ahead as a single value is, and
2525 counted as two. FPC's search keeps its slice in `rdi` and `rsi`;

doc/compiler.md (history) modified +48 -3

@@ -262,6 +262,48 @@
262262 though this path uses one of them. Each could go, at the cost of lines
263263 a reader would have to follow.
264264
265+## Better code again, in v0.8.0
266+
267+Each change below was measured on rat, a terminal emulator written in
268+raz, running its 26 MB workload, and on the kernels of `test/bench`,
269+against their twins built by FPC `-O2` (`make kernels`); one that
270+bought less than 2% on both was taken out again.
271+
272+1. **Slices in registers.** `sum(s: []INT)` kept its slice in the
273+ frame, and read its pointer from there for each element:
274+
275+ ```
276+ mov [rbp - 16], rdi ; the prologue: s, to its slots
277+ mov [rbp - 8], rsi
278+ ...
279+ mov r9, r14 ; s[i], each time round
280+ shl r9, 3
281+ add r9, [rbp - 16]
282+ ```
283+
284+ Now a slice whose address isn't taken can have two of the
285+ variables' registers, its pointer's and its length's
286+ (`o^.reg2`), chosen as a single value's is but for half its weight
287+ in each:
288+
289+ ```
290+ mov r12, rdi
291+ mov r13, rsi
292+ ...
293+ mov r9, r14
294+ shl r9, 3
295+ add r9, r12
296+ ```
297+
298+ Its item is at `bReg`, and its `TAdr` names both registers (`hi`
299+ for the length's): `PushVar` reads either word (`RegAt`),
300+ `IndexSlice` and `Slice` compare with the length's and add the
301+ pointer's, a failed check takes the length from its register
302+ (`InRegister`), and `CopyTo` loads both. What wants a slice's
303+ memory, a comparison or `COPY`, has its words stored in a
304+ temporary first (`CopyToTemp`). rat 5% faster, the heap sort 5%,
305+ the search 4%.
306+
265307 ## diag.pas: messages
266308
267309 Every message the compiler prints is made here. A position, `TPos`, is
@@ -532,7 +574,8 @@
532574 first. The
533575 prologue saves them, zeroes them, and moves a parameter into its
534576 register from the register or stack slot it arrived in; the epilogue
535-restores them. A variable in one is an
577+restores them. A slice may have two, its pointer's and its length's
578+(see "Better code again" above). A variable in one is an
536579 item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it,
537580 `Peek` reads it in place where the value is only read, so `i < n` is
538581 `cmp rbx, r12`, and `PopVar` and `IncDecReg` write it. Rt finds the
@@ -726,7 +769,8 @@
726769 the body ahead, as `InstancesFor` does, and counts each variable's
727770 uses: 1 for each, 4 in a loop, 16 in two, at most 64 (loops nested three
728771 deep or more); one `ADR` takes is out. The
729-five busiest used at least three times get registers (`o^.reg`). An
772+busiest used at least three times get the five registers (`o^.reg`),
773+a slice two (`o^.reg2` too), its count halved for each. An
730774 address wanted after all, by a `var` parameter, say, comes to
731775 `Address` (`item.pas`), which puts the variable back in memory and
732776 sets `demoted`: `Body` then throws the procedure's code away
@@ -862,7 +906,8 @@
862906 `_ := f(x)` drops one on purpose. `-Werror` makes each an error.
863907 - **Registers for variables.** Up to five of a procedure's single
864908 values, not reals, whose address is never taken, live in `rbx` and
865- `r12` to `r15`; the trap report shows them as it shows the others.
909+ `r12` to `r15`, a slice taking two of them; the trap report shows
910+ them as it shows the others, but for slices, which it never shows.
866911 - **Several results** that are two words, and a slice, come back in
867912 rax and rdx, as from a code procedure; others through a hidden
868913 pointer to the caller's slots.

src/gen.pas modified +69 -22

@@ -70,7 +70,9 @@
7070 TAdr = record
7171 base: TBase;
7272 chunk: longint; { bGlobal: the chunk; bSlot: the frame slot; bReg: the register }
73- off: int64; { added to the address }
73+ off: int64; { added to the address; for bReg, which word, 0 or 8 }
74+ hi: longint; { bReg, a slice: the register of its length, the
75+ second word; chunk is that of its pointer }
7476 end;
7577
7678 { The code made so far, to go back to for a second try at a body }
@@ -411,6 +413,13 @@
411413 Fits32 := (v >= low(longint)) and (v <= high(longint));
412414 end;
413415
416+{ The register of the word at a, a variable in registers: the variable's
417+ own, or for a slice its pointer's (off 0) or its length's (off 8) }
418+function RegAt(const a: TAdr): longint;
419+begin
420+ if a.off = 0 then RegAt := a.chunk else RegAt := a.hi;
421+end;
422+
414423 { The memory a names. An address on the stack is popped, and is in its
415424 register still; one in a slot is loaded into r. }
416425 function Place(const a: TAdr; r: longint): TMem;
@@ -458,7 +467,7 @@
458467 var m: TMem; r: longint;
459468 begin
460469 if a.base = bReg then begin
461- NewEntry(k, lHeld, a.chunk);
470+ NewEntry(k, lHeld, RegAt(a));
462471 exit;
463472 end;
464473 { in the order PushAdr explains }
@@ -483,7 +492,7 @@
483492 var r: longint; v: int64;
484493 begin
485494 if a.base = bReg then begin
486- if TopConst(v) then MovRI(a.chunk, v) else MovRR(a.chunk, Peek(depth - 1));
495+ if TopConst(v) then MovRI(RegAt(a), v) else MovRR(RegAt(a), Peek(depth - 1));
487496 Drop;
488497 exit;
489498 end;
@@ -553,10 +562,16 @@
553562 CopyBytes(size);
554563 end;
555564
556-{ Pops a source's address, and copies size bytes to a. }
565+{ Pops a source's address, and copies size bytes to a: for a slice in
566+ registers, its two words into them. }
557567 procedure CopyTo(const a: TAdr; size: int64);
558568 begin
559569 PopTo(RSI);
570+ if a.base = bReg then begin
571+ Load(a.chunk, Mem(RSI, 0), 8, false);
572+ Load(a.hi, Mem(RSI, 8), 8, false);
573+ exit;
574+ end;
560575 Lea(RDI, Place(a, RDI));
561576 CopyBytes(size);
562577 end;
@@ -1069,6 +1084,7 @@
10691084
10701085 const
10711086 NoBase = -2; { a TMem that is no memory }
1087+ ARegister = -3; { a TMem that is no memory but a register, its index }
10721088
10731089 { The program goes on if the condition cc holds, and otherwise stops
10741090 in Rt.fail(what, a, b, len, at, name), which says what failed at p,
@@ -1110,7 +1126,9 @@
11101126 LineCol(at, line, col);
11111127 NoteLine(line, col);
11121128 if ra <> RSI then MovRR(RSI, ra);
1113- if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false);
1129+ if m.base = NoBase then MovRI(RCX, n)
1130+ else if m.base = ARegister then MovRR(RCX, m.index)
1131+ else Load(RCX, m, 8, false);
11141132 if name >= 0 then Lea(R9, MemRip(name, 0));
11151133 if failStubs[what] < 0 then failStubs[what] := NewChunk(secText, Intern('Rt', 2), Intern(@StubNames[what][1], 6));
11161134 CallChunk(failStubs[what]);
@@ -1122,6 +1140,13 @@
11221140 function NoMem: TMem;
11231141 begin
11241142 NoMem := Mem(NoBase, 0);
1143+end;
1144+
1145+{ For Check: a length in register r, a slice's in registers }
1146+function InRegister(r: longint): TMem;
1147+begin
1148+ InRegister := Mem(ARegister, 0);
1149+ InRegister.index := r;
11251150 end;
11261151
11271152 { The index on top is below n, unsigned }
@@ -1160,21 +1185,31 @@
11601185 It is worked out in the index's own register, which then holds the
11611186 address; but if the slice's address is on the stack, under the
11621187 index, the element's register is the one that holds that, still
1163- needed, so the work is done in rax. }
1188+ needed, so the work is done in rax. A slice in registers is
1189+ compared with and added to them. }
11641190 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
11651191 var m, ml: TMem; r: longint; inRax: boolean;
11661192 begin
11671193 inRax := a.base = bStack;
11681194 if inRax then begin r := RAX; PopTo(RAX) end
11691195 else begin r := Top; Drop end;
1170- m := Place(a, RCX);
1171- ml := m; ml.disp := ml.disp + 8;
1172- if checked then begin
1173- AluM(aluCmp, r, ml);
1174- Check(ccB, 0, p, r, ml, 0, -1);
1196+ if a.base = bReg then begin
1197+ if checked then begin
1198+ Alu(aluCmp, r, a.hi);
1199+ Check(ccB, 0, p, r, InRegister(a.hi), 0, -1);
1200+ end;
1201+ MulI(r, size);
1202+ Alu(aluAdd, r, a.chunk);
1203+ end else begin
1204+ m := Place(a, RCX);
1205+ ml := m; ml.disp := ml.disp + 8;
1206+ if checked then begin
1207+ AluM(aluCmp, r, ml);
1208+ Check(ccB, 0, p, r, ml, 0, -1);
1209+ end;
1210+ MulI(r, size);
1211+ AluM(aluAdd, r, m);
11751212 end;
1176- MulI(r, size);
1177- AluM(aluAdd, r, m);
11781213 if inRax then MovRR(Target, RAX)
11791214 else Claim(depth); { the index's register again, now for the address }
11801215 Computed(kWord);
@@ -1188,12 +1223,16 @@
11881223 begin
11891224 PopTo(RDX);
11901225 PopTo(RCX);
1191- m := Place(a, RDI);
1192- ml := m; ml.disp := ml.disp + 8;
1226+ if a.base = bReg then ml := InRegister(a.hi) { a slice in registers }
1227+ else begin
1228+ m := Place(a, RDI);
1229+ ml := m; ml.disp := ml.disp + 8;
1230+ end;
11931231 if checked then begin
11941232 { two checks, both unsigned, so that a negative bound fails too:
11951233 hi against the length, then lo against hi }
11961234 if isArray then begin CmpI(RDX, n); ml := NoMem end
1235+ else if a.base = bReg then Alu(aluCmp, RDX, a.hi)
11971236 else AluM(aluCmp, RDX, ml);
11981237 Check(ccBE, 1, p, RCX, ml, n, -1);
11991238 Alu(aluCmp, RCX, RDX);
@@ -1203,7 +1242,9 @@
12031242 Alu(aluSub, RAX, RCX);
12041243 Store(Mem(RBP, tmp + 8), RAX, 8);
12051244 MulI(RCX, size);
1206- if isArray then Lea(RAX, m) else Load(RAX, m, 8, false);
1245+ if isArray then Lea(RAX, m)
1246+ else if a.base = bReg then MovRR(RAX, a.chunk)
1247+ else Load(RAX, m, 8, false);
12071248 Alu(aluAdd, RAX, RCX);
12081249 Store(Mem(RBP, tmp), RAX, 8);
12091250 end;
@@ -1441,7 +1482,7 @@
14411482 p^.val := AllocPerm(16, 8);
14421483 for k := 0 to 1 do begin
14431484 inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + 8 * k;
1444- inSize[numIn] := 8; inObj[numIn] := nil;
1485+ inSize[numIn] := 8; inObj[numIn] := p;
14451486 inc(numIn); inc(ni);
14461487 end;
14471488 end else begin
@@ -1513,7 +1554,7 @@
15131554 registers of variables are zeroed in place, their save slots are
15141555 written before they are read, and so is every temporary. }
15151556 procedure EnterBody(c, line: longint);
1516-var i, top: longint; n: int64; p: PObj;
1557+var i, top, r: longint; n: int64; p: PObj;
15171558 begin
15181559 BeginChunk(c);
15191560 codeChunk := c;
@@ -1550,12 +1591,15 @@
15501591 inc(n);
15511592 end;
15521593 { the parameters that came in registers, to their slots or, for a
1553- register variable, to its register }
1594+ register variable, to its register: a slice's second word, the one
1595+ for the slot 8 bytes into its own, to its length's }
15541596 for i := 0 to numIn - 1 do
15551597 if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i])
15561598 else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin
1557- MovRR(inObj[i]^.reg, inReg[i]);
1558- if inSize[i] < 8 then Extend(inObj[i]^.reg, inSize[i], inObj[i]^.typ^.signed);
1599+ r := inObj[i]^.reg;
1600+ if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2;
1601+ MovRR(r, inReg[i]);
1602+ if inSize[i] < 8 then Extend(r, inSize[i], inObj[i]^.typ^.signed);
15591603 end else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]);
15601604 { a register variable's parameter that came on the stack, above the
15611605 frame (val > 0), is loaded from there }
@@ -1564,7 +1608,10 @@
15641608 if (p^.reg <> 0) and (p^.val > 0) then
15651609 if p^.typ^.form in [fInt, fBool, fEnum, fSet] then
15661610 Load(p^.reg, Mem(RBP, p^.val), p^.typ^.size, p^.typ^.signed)
1567- else Load(p^.reg, Mem(RBP, p^.val), 8, false);
1611+ else begin
1612+ Load(p^.reg, Mem(RBP, p^.val), 8, false);
1613+ if p^.typ^.form = fSlice then Load(p^.reg2, Mem(RBP, p^.val + 8), 8, false);
1614+ end;
15681615 p := p^.next;
15691616 end;
15701617 end;

src/item.pas modified +20 -3

@@ -191,6 +191,7 @@
191191 FrameAdr.base := bFrame;
192192 FrameAdr.chunk := -1;
193193 FrameAdr.off := off;
194+ FrameAdr.hi := 0;
194195 end;
195196
196197 function GlobalAdr(c: longint): TAdr;
@@ -198,14 +199,17 @@
198199 GlobalAdr.base := bGlobal;
199200 GlobalAdr.chunk := c;
200201 GlobalAdr.off := 0;
202+ GlobalAdr.hi := 0;
201203 end;
202204
203-{ Where the variable x is, for gen }
205+{ Where the variable x is, for gen; a slice in registers has two }
204206 function AdrOf(const x: TItem): TAdr;
205207 begin
206208 AdrOf.base := x.base;
207209 AdrOf.chunk := x.chunk;
208210 AdrOf.off := x.off;
211+ AdrOf.hi := 0;
212+ if x.base = bReg then AdrOf.hi := x.obj^.reg2;
209213 end;
210214
211215 { An item for "found ...": a constant without a type of its own as it
@@ -373,6 +377,7 @@
373377 { its address is wanted after all: the procedure is compiled again
374378 with the variable in memory; till then, this code is thrown away }
375379 x.obj^.reg := 0;
380+ x.obj^.reg2 := 0;
376381 demoted := true;
377382 x.base := bFrame;
378383 x.off := x.obj^.val;
@@ -417,10 +422,21 @@
417422
418423 { x, copied to a temporary }
419424 procedure CopyToTemp(var x: TItem);
420-var t: int64; f, g: PObj;
425+var t: int64; f, g: PObj; a: TAdr;
421426 begin
422427 if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer');
423428 t := AllocTemp(x.typ^.size, x.typ^.align);
429+ if (x.mode = mVar) and (x.base = bReg) then begin
430+ { a slice in registers, which has no address: its words stored }
431+ a := AdrOf(x);
432+ PushVar(a, 8, false, kWord);
433+ PopVar(FrameAdr(t), 8);
434+ a.off := 8;
435+ PushVar(a, 8, false, kWord);
436+ PopVar(FrameAdr(t + 8), 8);
437+ TempItem(x, x.typ, t);
438+ exit;
439+ end;
424440 if x.mode = mWords then begin
425441 { the second result is on top, so it is popped first }
426442 f := x.typ^.fields; g := f^.next;
@@ -542,6 +558,7 @@
542558 PopVar(AdrOf(dst), ScalarSize(dst.typ));
543559 end else begin
544560 if dst.vol or src.vol then MarkAt(src.pos, 'can''t copy through a volatile pointer');
561+ if (src.mode = mVar) and (src.base = bReg) then CopyToTemp(src); { a slice in registers }
545562 Address(src);
546563 if dst.base = bStack then CopyBlock(dst.typ^.size, dst.off)
547564 else CopyTo(AdrOf(dst), dst.typ^.size);
@@ -1164,7 +1181,7 @@
11641181 if x.mode = mWords then exit; { there already }
11651182 { copied to a temporary unless its two words are in memory at a place
11661183 that a later argument can't change }
1167- if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x);
1184+ if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot, bReg]) then CopyToTemp(x);
11681185 adr := AdrOf(x);
11691186 if x.typ^.form = fSlice then begin
11701187 PushVar(adr, 8, false, kWord);

src/parse.pas modified +21 -12

@@ -1818,12 +1818,12 @@
18181818 if o^.reg = VarRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512;
18191819 end;
18201820
1821-{ Whether variable o could live in a register: a single value, not
1822- a real, that isn't C's }
1821+{ Whether variable o could live in registers: a single value, not a
1822+ real, or a slice, in two, that isn't C's }
18231823 function RegFit(o: PObj): boolean;
18241824 begin
18251825 RegFit := (o^.cls = oVar) and not o^.ind and not o^.ext
1826- and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError])
1826+ and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice])
18271827 or (o^.typ^.form = fSet) and (o^.typ^.size <= 8));
18281828 end;
18291829
@@ -1840,12 +1840,12 @@
18401840 its "begin" to its end, a use counts 1, in a loop 4, in two 16, in
18411841 three or more 64, and a variable ADR is taken of can't have one. A register costs a save
18421842 and a restore, so a variable used less than three times keeps its
1843- place in memory. }
1843+ place in memory. A slice takes two, its pointer's and its length's. }
18441844 procedure ChooseRegs(params, before: PObj);
18451845 var
18461846 cand: array[0..63] of PObj; weight: array[0..63] of longint;
18471847 isLoop: array[0..255] of boolean;
1848- n, i, k, d, loops, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean;
1848+ n, i, k, d, loops, best, need: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean;
18491849
18501850 function Candidate(name: longint): longint;
18511851 var j: longint;
@@ -1899,15 +1899,24 @@
18991899 until (d = 0) or (tok = sEof);
19001900 RestoreScan(st);
19011901 skipping := wasSkipping;
1902- { the five heaviest, each used at least three times, take the
1903- registers in turn }
1904- for k := 0 to 4 do begin
1902+ { a slice's weight is shared by its two registers }
1903+ for i := 0 to n - 1 do
1904+ if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2;
1905+ { the heaviest, each used at least three times a register, take the
1906+ five registers in turn, while there are enough for each }
1907+ k := 0;
1908+ repeat
19051909 best := -1;
19061910 for i := 0 to n - 1 do
19071911 if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i;
19081912 if best < 0 then break;
1909- cand[best]^.reg := VarRegs[k];
1910- end;
1913+ need := 1 + ord(cand[best]^.typ^.form = fSlice);
1914+ if k + need <= 5 then begin
1915+ cand[best]^.reg := VarRegs[k];
1916+ if need = 2 then cand[best]^.reg2 := VarRegs[k + 1];
1917+ inc(k, need);
1918+ end else weight[best] := 0; { too big for what is left }
1919+ until k = 5;
19111920 end;
19121921 end;
19131922
@@ -1919,13 +1928,13 @@
19191928 m := 0;
19201929 o := params;
19211930 while o <> nil do begin
1922- for k := 0 to 4 do if o^.reg = VarRegs[k] then m := m or (1 shl k);
1931+ for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k);
19231932 o := o^.next;
19241933 end;
19251934 o := FirstLocal(before);
19261935 while o <> nil do begin
19271936 if o^.cls = oVar then
1928- for k := 0 to 4 do if o^.reg = VarRegs[k] then m := m or (1 shl k);
1937+ for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k);
19291938 o := o^.link;
19301939 end;
19311940 RegsOf := m;

src/sym.pas modified +3 -1

@@ -72,7 +72,9 @@
7272 fwd: boolean; { oProc: declared forward, no body yet }
7373 isCode: boolean; { oProc: a code procedure }
7474 openEnd: boolean; { oProc: has results, and may reach its end }
75- reg: longint; { oVar: the register it lives in, or 0 if in memory }
75+ reg: longint; { oVar: the register it lives in, or 0 if in memory;
76+ for a slice, the one its pointer is in }
77+ reg2: longint; { oVar: a slice in registers: the one its length is in }
7678 ext: boolean; { oProc, oVar: C's, reached through its chunk, which
7779 holds its address }
7880 fields: PObj; { oCase: its fields }

test/check.sh (history) modified +1 -1

@@ -65,7 +65,7 @@
6565 fi
6666 }
6767
68-for f in examples/*.raz test/run/*.raz test/libc/*.raz; do
68+for f in examples/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz; do
6969 run $(dirname $f) $(basename $f .raz)
7070 done
7171

test/run/SliceRegs.expect (history) added +6 -0

@@ -0,0 +1,6 @@
1+21
2+4 3
3+0
4+1003 abcd
5+41
6+27

test/run/SliceRegs.raz (history) added +78 -0

@@ -0,0 +1,78 @@
1+(* Slices in registers: a slice parameter or variable used enough to
2+ be given its two registers, through every way a slice is used. *)
3+module SliceRegs;
4+import Out;
5+
6+proc sum(s: []INT): INT;
7+var i, t: INT;
8+begin
9+ for i := 0 to LEN(s) - 1 do t := t + s[i]; end;
10+ return t;
11+end;
12+
13+(* s sliced, assigned, passed and returned, all in registers *)
14+proc tail(s: []INT; n: INT): []INT;
15+var i: INT;
16+begin
17+ for i := 1 to n do
18+ if LEN(s) > 0 then s := s[1..LEN(s)]; end;
19+ end;
20+ return s;
21+end;
22+
23+(* two slices compared, copied and assigned to each other *)
24+proc mix(a, b: []U8): INT;
25+var i, n: INT; c: []U8;
26+begin
27+ for i := 0 to LEN(a) - 1 do
28+ if a[i] = b[i] then INC(n); end;
29+ end;
30+ if a = b then INC(n, 100); end;
31+ COPY(b, a);
32+ if a = b then INC(n, 1000); end;
33+ c := a;
34+ for i := 0 to LEN(c) - 1 do INC(n, INT(c[i]) - INT(a[i])); end;
35+ return n;
36+end;
37+
38+proc bump(var s: []INT);
39+begin
40+ s := s[1..LEN(s)];
41+end;
42+
43+(* s passed to a var parameter: demoted to memory, and still right *)
44+proc demote(s: []INT): INT;
45+var i, t: INT;
46+begin
47+ for i := 0 to LEN(s) - 1 do t := t + s[i]; end;
48+ bump(s);
49+ for i := 0 to LEN(s) - 1 do t := t + s[i]; end;
50+ return t;
51+end;
52+
53+(* seven slices: some in registers, some not *)
54+proc many(a, b, c, d, e, f, g: []INT): INT;
55+var i, t: INT;
56+begin
57+ for i := 0 to 1 do
58+ t := t + a[i] + b[i] + c[i] + d[i] + e[i] + f[i] + g[i];
59+ end;
60+ return t;
61+end;
62+
63+proc main*;
64+var x: [6]INT; i: INT; p, q: [4]U8; s: []INT;
65+begin
66+ for i := 0 to 5 do x[i] := i + 1; end;
67+ Out.int(sum(x)); Out.ln;
68+ s := tail(x, 2);
69+ Out.int(LEN(s)); Out.char(' '); Out.int(s[0]); Out.ln;
70+ Out.int(LEN(tail(x, 9))); Out.ln;
71+ p := "abcd";
72+ q := "abxd";
73+ Out.int(mix(p, q)); Out.char(' '); Out.str(q); Out.ln;
74+ Out.int(demote(x)); Out.ln;
75+ Out.int(many(x, x[1..6], x, x, x, x[2..6], x)); Out.ln;
76+end;
77+
78+end SliceRegs.

test/run/SliceRegsCheck.expect (history) added +1 -0

@@ -0,0 +1 @@
1+14

test/run/SliceRegsCheck.raz (history) added +21 -0

@@ -0,0 +1,21 @@
1+(* A failed check of an index into a slice in registers says the
2+ length, read from its register. *)
3+module SliceRegsCheck;
4+import Out;
5+
6+proc squares(s: []INT; n: INT): INT;
7+var i, t: INT;
8+begin
9+ for i := 0 to n do t := t + s[i] * s[i]; end;
10+ return t;
11+end;
12+
13+proc main*;
14+var x: [3]INT;
15+begin
16+ x := [1, 2, 3];
17+ Out.int(squares(x, 2)); Out.ln;
18+ Out.int(squares(x, 3)); Out.ln;
19+end;
20+
21+end SliceRegsCheck.

test/run/SliceRegsCheck.status (history) added +1 -0

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

test/run/SliceRegsCheck.stderr (history) added +3 -0

@@ -0,0 +1,3 @@
1+test/run/SliceRegsCheck.raz:9:35: index 3 is out of range for a length of 3
2+ in SliceRegsCheck.squares at test/run/SliceRegsCheck.raz:9:35: n = 3, i = 3, t = 14
3+ in SliceRegsCheck.main at test/run/SliceRegsCheck.raz:18:5