raz / commit 654579f9

commit
654579f9338fd9dcac00247add685fc4d8693356
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e224225d
Parameters left where they arrive

In a procedure that names no procedure, procedure variable, NEW,
SYSCALL or COPY, as the read-ahead sees, the parameters that arrive in
rdi and rsi stay there: nothing saved, nothing moved. Gen sets lost
where it needs them after all, to copy or compare, and the procedure
is compiled again with them in memory, as for a demoted variable: 20
of rat's procedures. Rt.fail is given rdi and rsi as the failing code
had them, and the signal handler takes them from the context, so the
trap report shows the parameters still. The encoder now gives
movzx and setcc of dil and sil the REX prefix they need.

rat 545 -> 538 ms (-1.3%), text -0.7%; search -9%, heap sort -2%.

patch · log

16 files changed, +243 -46

PLAN.md (history) modified +1 -1

@@ -24,7 +24,7 @@
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`;
2626 raz's reads both from the frame on every round.
27-2. **Parameters left where they arrive.** In a procedure that calls
27+2. Done: **Parameters left where they arrive.** In a procedure that calls
2828 nothing, copies no block and divides by no variable, as the
2929 read-ahead can tell, a parameter stays in the register it came in,
3030 and no register is saved for it. Most of rat's hot procedures are

doc/compiler.md (history) modified +41 -13

@@ -110,22 +110,22 @@
110110 push rbp ; EnterBody: the frame
111111 mov rbp,rsp
112112 sub rsp,0x10 ; its size, patched in by EndProc
113-mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots
114-mov QWORD PTR [rbp-0x10],rsi
115-mov r8,QWORD PTR [rbp-0x8] ; a, at depth 0
116-mov r9,QWORD PTR [rbp-0x10] ; b, at depth 1; the 2 is only noted
117-shl r9,0x1 ; times 2 is a shift
113+mov r9,rsi ; b, at depth 1; a, at depth 0, and
114+shl r9,0x1 ; the 2 are only noted; times 2
115+mov r8,rdi ; is a shift; a is loaded for +
118116 add r8,r9
119117 mov rax,r8 ; return: the result
120118 leave ; no jump to here, as it is next
121119 ret
122120 ```
123121
124-Every line of it can be traced to a routine in `gen.pas`: the loads
125-are `PushVar`'s, the shift and the add `Op`'s, the result
126-`ReturnJump`'s, and the frame is `EnterBody`'s and `EndProc`'s. It is
127-not fast code. It is code whose every instruction you can account
128-for, which is the point.
122+`scale` calls nothing, so its parameters stay in `rdi` and `rsi`,
123+where they arrive (`KeepArrived`): they are register variables that
124+cost nothing to keep. Every line of it can be traced to a routine in
125+`gen.pas`: the moves are `Loaded`'s, the shift and the add `Op`'s, the
126+result `ReturnJump`'s, and the frame is `EnterBody`'s and `EndProc`'s.
127+It is not the fastest code. It is code whose every instruction you
128+can account for, which is the point.
129129
130130 ## Better code, in one procedure
131131
@@ -304,6 +304,30 @@
304304 temporary first (`CopyToTemp`). rat 5% faster, the heap sort 5%,
305305 the search 4%.
306306
307+2. **Parameters left where they arrive.** `find(s, sub: []U8)` stored
308+ its first slice in its frame, or in two registers it saved and
309+ restored:
310+
311+ ```
312+ mov [rbp - 40], r12 ; the caller's r12 and r13, saved
313+ mov [rbp - 32], r13
314+ mov r12, rdi ; s, into them
315+ mov r13, rsi
316+ ```
317+
318+ In a procedure that names no procedure, a procedure variable, `NEW`,
319+ `SYSCALL` or `COPY`, as `ChooseRegs` sees reading ahead, the
320+ parameters that arrive in `rdi` and `rsi` stay there, which only a
321+ call or a copy needs: nothing is saved or moved, and `s[i]` is read
322+ through `rdi` itself (`KeepArrived`). Where gen needs them after
323+ all, to copy a record or compare two slices, it sets `lost`, and
324+ `Body` compiles the procedure again with the parameters in memory,
325+ as for a variable demoted. The trap report shows a parameter kept
326+ so: `Rt.fail` is given `rdi` and `rsi` as the failing code had
327+ them, and the signal handler takes them from the context the
328+ kernel saved. The search 9% faster, rat 1.5%, its text 0.7%
329+ smaller.
330+
307331 ## diag.pas: messages
308332
309333 Every message the compiler prints is made here. A position, `TPos`, is
@@ -519,7 +543,9 @@
519543 calls a stub for its kind of failure (`failStubs`, made by
520544 `EntryPoint`), which says the kind and passes the address it was
521545 called from, by which `Rt` finds the place: the failing code has the
522-place of the offending expression (`LineCol` in `scan.pas`). A check
546+place of the offending expression (`LineCol` in `scan.pas`). It leaves
547+`rdi` and `rsi` as they were, so that the stub can pass them on, for
548+the trap report to show a parameter kept in them. A check
523549 costs a compare and a jump where it stands, and a dozen bytes out of
524550 the way. Code is aligned to 32 bytes, the windows x86 fetches it in,
525551 so that where a procedure's loops fall doesn't depend on what comes
@@ -906,8 +932,10 @@
906932 `_ := f(x)` drops one on purpose. `-Werror` makes each an error.
907933 - **Registers for variables.** Up to five of a procedure's single
908934 values, not reals, whose address is never taken, live in `rbx` and
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.
935+ `r12` to `r15`, a slice taking two of them; in a procedure that
936+ calls nothing, a parameter that arrives in `rdi` or `rsi` stays
937+ there. The trap report shows them as it shows the others, but for
938+ slices, which it never shows.
911939 - **Several results** that are two words, and a slice, come back in
912940 rax and rdx, as from a code procedure; others through a hidden
913941 pointer to the caller's slots.

lib/Rt.raz (history) modified +13 -6

@@ -72,8 +72,10 @@
7272 places: ^INT;
7373 line: [256]U8; (* the message being made *)
7474 (* rbx, r12, r13, r14 and r15 in the frame being told of, where the
75- compiler keeps a procedure's busiest variables *)
76- regs: [5]UINT;
75+ compiler keeps a procedure's busiest variables; then rdi and rsi
76+ where the program stopped, where a procedure that calls nothing
77+ keeps the parameters that arrive in them *)
78+ regs: [7]UINT;
7779 lineLen: INT;
7880 act: Action;
7981 alt: Stack;
@@ -425,10 +427,13 @@
425427 runs backwards. What a target's machine doesn't catch the code
426428 tests for, and stops at with what 4, a nil pointer; 5, a division
427429 by zero; 6, a stack overflow; these end the program by the signal
428- the machine would have given, as if it had. *)
429-proc fail*(what, a, b, len: INT; at: UINT; name: ^U8);
430+ the machine would have given, as if it had. rdi and rsi are the
431+ registers as the failing code had them. *)
432+proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT);
430433 var sig: INT;
431434 begin
435+ regs[5] := rdi;
436+ regs[6] := rsi;
432437 where(at);
433438 if what >= 4 then
434439 put("stopped by ");
@@ -481,8 +486,10 @@
481486 proc onSignal(sig: INT; info, ctx: ^U8);
482487 var adr, sp, ip, bp: UINT; q: ^UINT; why: []U8;
483488 begin
484- q := ^UINT(ctx + 120); (* uc_mcontext's rbp *)
485- bp := q^;
489+ q := ^UINT(ctx + 104); (* uc_mcontext's rdi, then rsi, then rbp *)
490+ regs[5] := q[0];
491+ regs[6] := q[1];
492+ bp := q[2];
486493 q := ^UINT(info + 16); (* si_addr *)
487494 adr := q^;
488495 q := ^UINT(ctx + 160); (* uc_mcontext's rsp, then rip *)

src/gen.pas modified +83 -6

@@ -100,12 +100,19 @@
100100 callee-saved ones, so they live through calls (a register variable); a procedure saves
101101 those it uses, and Rt finds them there for the trap report }
102102 VarRegs: array[0..4] of longint = (RBX, R12, R13, R14, R15);
103+ { the registers the trap report can show a variable in: VarRegs, then
104+ rdi and rsi, where a procedure that calls nothing keeps the
105+ parameters that arrive in them (KeptIn) }
106+ ShownRegs: array[0..6] of longint = (RBX, R12, R13, R14, R15, RDI, RSI);
103107
104108 var
105109 rtFail, argcChunk, argvChunk: longint;
106110 { the stubs that Check's code calls, one for each kind of failure,
107111 made by EntryPoint for those used }
108112 failStubs: array[0..5] of longint = (-1, -1, -1, -1, -1, -1);
113+ lost: boolean; { a parameter kept where it arrived was wanted
114+ for other work: the procedure is compiled again,
115+ the parameter in memory }
109116 depth: longint; { the values on the stack }
110117 pushed: longint; { quadwords pushed on the machine's stack }
111118 codeChunk: longint = -1; { the procedure or body being compiled, or -1 }
@@ -171,6 +178,8 @@
171178
172179 procedure BeginProc(sig: PType; params: PObj);
173180 procedure ResetLocals;
181+procedure KeepArrived(o: PObj);
182+function KeptIn(o: PObj): boolean;
174183 procedure UseRegs(mask: longint);
175184 procedure MarkCode(var m: TCodeMark);
176185 procedure Redo(const m: TCodeMark);
@@ -234,6 +243,7 @@
234243 numIn: longint;
235244 procParams: PObj; { the parameters, for EnterBody }
236245 regMask: longint; { the registers of variables: bit i is VarRegs[i] }
246+ kept: longint; { bit r: register r holds a parameter, as it arrived }
237247 saveAt: int64; { where they are saved, a quadword each }
238248 frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations }
239249 { the checks of the procedure, whose failures EndFails makes }
@@ -366,6 +376,13 @@
366376 if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1;
367377 end;
368378
379+{ rsi and rdi are about to be used for other work, as in copying: a
380+ parameter kept in either must go to memory (lost). }
381+procedure ClobberArgs;
382+begin
383+ if kept <> 0 then lost := true;
384+end;
385+
369386 { The value i into register r, from wherever it is: a constant or an
370387 address is made there, a variable's register or the value's own
371388 register copied, a spilled value loaded. Unlike Loaded, it leaves the
@@ -556,6 +573,7 @@
556573 size bytes to off bytes past the destination. }
557574 procedure CopyBlock(size, off: int64);
558575 begin
576+ ClobberArgs;
559577 PopTo(RSI);
560578 PopTo(RDI);
561579 if off <> 0 then Lea(RDI, Mem(RDI, off));
@@ -566,6 +584,7 @@
566584 registers, its two words into them. }
567585 procedure CopyTo(const a: TAdr; size: int64);
568586 begin
587+ ClobberArgs;
569588 PopTo(RSI);
570589 if a.base = bReg then begin
571590 Load(a.chunk, Mem(RSI, 0), 8, false);
@@ -578,6 +597,7 @@
578597
579598 procedure ZeroVar(const a: TAdr; size: int64);
580599 begin
600+ ClobberArgs;
581601 Lea(RDI, Place(a, RDI));
582602 MovRI(RCX, size);
583603 MovRI(RAX, 0);
@@ -871,6 +891,7 @@
871891 procedure CompareSeq(a, b: int64; shift: longint; eq: boolean);
872892 var differ, r: longint;
873893 begin
894+ ClobberArgs;
874895 r := Target;
875896 Load(RCX, Mem(RBP, b + 8), 8, false);
876897 AluM(aluCmp, RCX, Mem(RBP, a + 8));
@@ -941,6 +962,7 @@
941962 procedure ZeroNew(size: int64);
942963 var L, r: longint;
943964 begin
965+ ClobberArgs;
944966 r := Top;
945967 L := NoLabel;
946968 TestRR(r, r);
@@ -977,6 +999,7 @@
977999 const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9);
9781000 var i: longint;
9791001 begin
1002+ ClobberArgs;
9801003 SpillBelow(depth);
9811004 for i := 0 to n - 1 do
9821005 with stack[depth - n + i] do
@@ -1095,7 +1118,10 @@
10951118 division fault and a stack overflow, which this machine catches
10961119 itself, so no check here has them. The arguments go in rdi, rsi, rdx,
10971120 rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m,
1098- or n if m.base is NoBase. The failing code is out of the way, at the
1121+ or n if m.base is NoBase, or in register m.index if it is ARegister.
1122+ rdi and rsi are passed on as they were, since a parameter may be kept
1123+ in them, for the trap report to show: so a goes to the stub in rax.
1124+ The failing code is out of the way, at the
10991125 procedure's end (EndFails), so that a check that holds costs a jump
11001126 not taken; it has p for its place (at is p's offset in the source),
11011127 never returns, and may use any register once it has read what it
@@ -1125,7 +1151,7 @@
11251151 FixLink(L);
11261152 LineCol(at, line, col);
11271153 NoteLine(line, col);
1128- if ra <> RSI then MovRR(RSI, ra);
1154+ if ra <> RAX then MovRR(RAX, ra);
11291155 if m.base = NoBase then MovRI(RCX, n)
11301156 else if m.base = ARegister then MovRR(RCX, m.index)
11311157 else Load(RCX, m, 8, false);
@@ -1223,6 +1249,7 @@
12231249 begin
12241250 PopTo(RDX);
12251251 PopTo(RCX);
1252+ if a.base = bSlot then ClobberArgs; { Place loads its slot into rdi }
12261253 if a.base = bReg then ml := InRegister(a.hi) { a slice in registers }
12271254 else begin
12281255 m := Place(a, RDI);
@@ -1256,6 +1283,7 @@
12561283 procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos);
12571284 var ml: TMem; L, E: longint;
12581285 begin
1286+ ClobberArgs;
12591287 Load(RDX, Mem(RBP, sOff + 8), 8, false);
12601288 if checked then begin
12611289 ml := Mem(RBP, dOff + 8);
@@ -1288,6 +1316,7 @@
12881316 bounds can't be known, but it can't run backwards. }
12891317 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
12901318 begin
1319+ ClobberArgs;
12911320 PopTo(RDX);
12921321 PopTo(RCX);
12931322 PopTo(RDI);
@@ -1322,6 +1351,7 @@
13221351 begin
13231352 { the arguments are the c.n values on top, where computing them left
13241353 them; the registers in use under them are saved around the call }
1354+ ClobberArgs;
13251355 first := depth - c.n;
13261356 SpillBelow(first);
13271357 res := c.sig^.base;
@@ -1463,6 +1493,8 @@
14631493 ResetStack(0, 0);
14641494 exitL := NoLabel;
14651495 numIn := 0;
1496+ lost := false;
1497+ kept := 0;
14661498 resType := sig^.base;
14671499 procParams := params;
14681500 regMask := 0;
@@ -1517,16 +1549,54 @@
15171549 frameTop := paramTop; frameMax := paramTop; permTop := paramTop;
15181550 end;
15191551
1552+{ The register the word (0, or 1 for a slice's second) of parameter o
1553+ arrives in, or -1 if it comes on the stack or in an xmm register }
1554+function ArrivesIn(o: PObj; word: longint): longint;
1555+var i: longint;
1556+begin
1557+ ArrivesIn := -1;
1558+ for i := 0 to numIn - 1 do
1559+ if (inObj[i] = o) and (inSlot[i] = o^.val + 8 * word) and (inReg[i] >= 0) then ArrivesIn := inReg[i];
1560+end;
1561+
1562+{ Parameter o, of a procedure that calls nothing, stays in the register
1563+ it arrives in if that is rdi or rsi, which only a call or a copy needs:
1564+ no register is saved for it, and nothing is moved. A slice stays only
1565+ if it has both. If the procedure turns out to copy after all, lost is
1566+ set, and it is compiled again with o in memory. }
1567+procedure KeepArrived(o: PObj);
1568+var r: longint;
1569+begin
1570+ r := ArrivesIn(o, 0);
1571+ if o^.typ^.form = fSlice then begin
1572+ if (r = RDI) and (ArrivesIn(o, 1) = RSI) then begin o^.reg := RDI; o^.reg2 := RSI end;
1573+ end else if (r = RDI) or (r = RSI) then o^.reg := r;
1574+end;
1575+
1576+{ Whether parameter o is kept where it arrived }
1577+function KeptIn(o: PObj): boolean;
1578+begin
1579+ KeptIn := (o^.reg = RDI) or (o^.reg = RSI);
1580+end;
1581+
15201582 { The locals in memory have their slots: the prologue zeroes those and
15211583 nothing below. The procedure keeps variables in the registers of
15221584 mask, which are saved next, a quadword each, in slots that need no
15231585 zeroing; the frame as it then stands is where a second try at the
15241586 body starts. }
15251587 procedure UseRegs(mask: longint);
1526-var i, k: longint;
1588+var i, k: longint; p: PObj;
15271589 begin
15281590 zeroTop := permTop;
15291591 regMask := mask;
1592+ { the parameters left in rdi and rsi, as they arrived }
1593+ kept := 0;
1594+ p := procParams;
1595+ while p <> nil do begin
1596+ if p^.reg in [RDI, RSI] then kept := kept or (1 shl p^.reg);
1597+ if p^.reg2 in [RDI, RSI] then kept := kept or (1 shl p^.reg2);
1598+ p := p^.next;
1599+ end;
15301600 k := 0;
15311601 for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k);
15321602 if k > 0 then saveAt := AllocPerm(8 * k, 8);
@@ -1544,6 +1614,7 @@
15441614 begin
15451615 pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said);
15461616 numFails := 0;
1617+ lost := false;
15471618 frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2];
15481619 exitL := NoLabel;
15491620 end;
@@ -1598,7 +1669,7 @@
15981669 else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin
15991670 r := inObj[i]^.reg;
16001671 if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2;
1601- MovRR(r, inReg[i]);
1672+ if r <> inReg[i] then MovRR(r, inReg[i]); { else it is kept where it is }
16021673 if inSize[i] < 8 then Extend(r, inSize[i], inObj[i]^.typ^.signed);
16031674 end else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]);
16041675 { a register variable's parameter that came on the stack, above the
@@ -1631,6 +1702,7 @@
16311702 bytes into it. }
16321703 procedure CopyResult(off, size: int64);
16331704 begin
1705+ ClobberArgs;
16341706 PopTo(RSI);
16351707 Load(RDI, Mem(RBP, retSlot), 8, false);
16361708 if off <> 0 then Lea(RDI, Mem(RDI, off));
@@ -1788,14 +1860,19 @@
17881860 check's call is. The return address is the next instruction's, which
17891861 may belong to another line; one byte back is inside the call, so the
17901862 lookup finds the call's line. rsp is aligned for the call, as the
1791- check's code may have pushed an odd number of quadwords. }
1863+ check's code may have pushed an odd number of quadwords. rdi and rsi
1864+ as the failing code had them go on the stack, as Rt.fail's last two
1865+ arguments, and a comes in rax (EndFails). }
17921866 for i := 0 to High(failStubs) do
17931867 if failStubs[i] >= 0 then begin
17941868 BeginChunk(failStubs[i]);
1795- MovRI(RDI, i);
17961869 Load(R8, Mem(RSP, 0), 8, false);
17971870 AluI(aluSub, R8, 1);
17981871 AluI(aluAnd, RSP, -16);
1872+ Push(RSI);
1873+ Push(RDI);
1874+ MovRR(RSI, RAX);
1875+ MovRI(RDI, i);
17991876 CallChunk(rtFail);
18001877 Ud2;
18011878 EndChunk(failStubs[i]);

src/parse.pas modified +44 -6

@@ -1814,8 +1814,8 @@
18141814 else exit(0);
18151815 end;
18161816 VarKind := k + o^.typ^.size * 16 + ord(o^.ind) * 256;
1817- for i := 0 to 4 do
1818- if o^.reg = VarRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512;
1817+ for i := 0 to High(ShownRegs) do
1818+ if o^.reg = ShownRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512;
18191819 end;
18201820
18211821 { Whether variable o could live in registers: a single value, not a
@@ -1840,12 +1840,16 @@
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. A slice takes two, its pointer's and its length's. }
1843+ place in memory. A slice takes two, its pointer's and its length's.
1844+ In a procedure that names no procedure, so calls nothing, the
1845+ parameters that arrive in the registers no call needs stay there
1846+ (KeepArrived), at no cost. }
18441847 procedure ChooseRegs(params, before: PObj);
18451848 var
18461849 cand: array[0..63] of PObj; weight: array[0..63] of longint;
18471850 isLoop: array[0..255] of boolean;
1848- n, i, k, d, loops, best, need: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean;
1851+ n, i, k, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState;
1852+ afterDot, wasSkipping, leaf: boolean;
18491853
18501854 function Candidate(name: longint): longint;
18511855 var j: longint;
@@ -1859,6 +1863,14 @@
18591863 if RegFit(o) and (n <= High(cand)) then begin cand[n] := o; weight[n] := 0; inc(n) end;
18601864 end;
18611865
1866+ { Whether naming o may make a call: a procedure, a procedure variable,
1867+ or NEW, SYSCALL or COPY, which use the registers a call would }
1868+ function Calls(o: PObj): boolean;
1869+ begin
1870+ Calls := (o <> nil) and ((o^.cls = oProc) or (o^.cls = oVar) and (o^.typ^.form = fProc)
1871+ or (o^.cls = oBuiltin) and ((o^.val = bNew) or (o^.val = bSyscall) or (o^.val = bCopy)));
1872+ end;
1873+
18621874 begin
18631875 n := 0;
18641876 o := params;
@@ -1869,8 +1881,18 @@
18691881 SaveScan(st);
18701882 wasSkipping := skipping;
18711883 skipping := true; { its slips are told when it is compiled }
1872- d := 0; loops := 0; afterDot := false;
1884+ d := 0; loops := 0; afterDot := false; leaf := true; qual := nil;
18731885 repeat
1886+ { a name, or a module's name and one of its exports, that calls }
1887+ if (tok = sIdent) and not afterDot then begin
1888+ q := Lookup(id);
1889+ if Calls(q) then leaf := false;
1890+ qual := nil;
1891+ if (q <> nil) and (q^.cls = oModule) then qual := q;
1892+ end else if (tok = sIdent) and (qual <> nil) then begin
1893+ if (qual^.module = nil) or Calls(Find(qual^.module^.scope, id)) then leaf := false;
1894+ qual := nil;
1895+ end;
18741896 case tok of
18751897 sBegin, sIf, sMatch, sWhile, sFor, sRepeat:
18761898 if d < High(isLoop) then begin
@@ -1899,6 +1921,13 @@
18991921 until (d = 0) or (tok = sEof);
19001922 RestoreScan(st);
19011923 skipping := wasSkipping;
1924+ if leaf then begin
1925+ o := params;
1926+ while o <> nil do begin
1927+ if RegFit(o) and (weight[Candidate(o^.name)] >= 0) then KeepArrived(o);
1928+ o := o^.next;
1929+ end;
1930+ end;
19021931 { a slice's weight is shared by its two registers }
19031932 for i := 0 to n - 1 do
19041933 if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2;
@@ -2001,7 +2030,16 @@
20012030 p^.openEnd := (curResult <> nil) and not ends;
20022031 NoteLine(CurLine, CurCol);
20032032 Expect(sEnd);
2004- if not demoted or (errors <> errs) then break;
2033+ if not (demoted or lost) or (errors <> errs) then break;
2034+ { a parameter kept where it arrived goes to memory: a call or a copy
2035+ wanted its register }
2036+ if lost then begin
2037+ o := params;
2038+ while o <> nil do begin
2039+ if KeptIn(o) then begin o^.reg := 0; o^.reg2 := 0 end;
2040+ o := o^.next;
2041+ end;
2042+ end;
20052043 numDrops := drops0;
20062044 Redo(mark);
20072045 PlaceLocals(params, before);

src/x64.pas modified +3 -3

@@ -495,7 +495,7 @@
495495 end else if signed then Say('movsx ' + RN(r) + ', ' + RN(r, size))
496496 else Say('movzx ' + RN(r, 4) + ', ' + RN(r, size));
497497 case size of
498- 1: if signed then OpR(0, true, $BE0F, r, r) else OpR(0, false, $B60F, r, r);
498+ 1: if signed then OpR(0, true, $BE0F, r, r, true) else OpR(0, false, $B60F, r, r, true);
499499 2: if signed then OpR(0, true, $BF0F, r, r) else OpR(0, false, $B70F, r, r);
500500 4: if signed then OpR(0, true, $63, r, r) else OpR(0, false, $89, r, r);
501501 end;
@@ -505,9 +505,9 @@
505505 procedure SetCC(cc, r: longint);
506506 begin
507507 if listing then Say('set' + CondNames[cc] + ' ' + RN(r, 1));
508- OpR(0, false, $900F + cc shl 8, 0, r);
508+ OpR(0, false, $900F + cc shl 8, 0, r, true);
509509 if listing then Say('movzx ' + RN(r, 4) + ', ' + RN(r, 1));
510- OpR(0, false, $B60F, r, r);
510+ OpR(0, false, $B60F, r, r, true);
511511 end;
512512
513513 procedure TestRR(a, b: longint);

test/enctest.pas modified +2 -2

@@ -19,8 +19,8 @@
1919 AluI(aluAdd, RAX, 1); AluI(aluAnd, RAX, 1000); AluI(aluCmp, RSP, 8); AluM(aluCmp, RAX, Mem(RCX, 8)); AluM(aluAdd, RAX, Mem(RCX, 0));
2020 Imul(RAX, RCX); ImulI(RAX, 24); Shift(shShl, RAX); Shift(shSar, RAX); ShiftI(shShr, RCX, 3);
2121 Unary(2, RAX); Unary(3, RAX); Unary(6, RCX); Unary(7, RCX); Cqo;
22- Extend(RAX, 1, true); Extend(RAX, 1, false); Extend(RAX, 2, true); Extend(RAX, 2, false); Extend(RAX, 4, true); Extend(RAX, 4, false);
23- SetCC(ccL, RAX); SetCC(ccA, RCX); TestRR(RAX, RAX);
22+ Extend(RAX, 1, true); Extend(RAX, 1, false); Extend(RDI, 1, false); Extend(RSI, 1, true); Extend(RAX, 2, true); Extend(RAX, 2, false); Extend(RAX, 4, true); Extend(RAX, 4, false);
23+ SetCC(ccL, RAX); SetCC(ccA, RCX); SetCC(ccE, RDI); TestRR(RAX, RAX);
2424 Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb;
2525 LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX);
2626 MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsdXM(2, Mem(RBP, -8)); MovsdMX(Mem(RBP, -8), 7);

test/list/Listing.expect (history) modified +7 -9

@@ -8,17 +8,15 @@
88 0 push rbp
99 1 mov rbp, rsp
1010 4 sub rsp, 16
11- 11 mov [rbp - 8], rdi
12- 15 mov [rbp - 16], rsi
1311 14 return a + b * 2;
14- 19 mov r8, [rbp - 8]
15- 23 mov r9, [rbp - 16]
16- 27 shl r9, 1
17- 31 add r8, r9
18- 34 mov rax, r8
12+ 11 mov r9, rsi
13+ 14 shl r9, 1
14+ 18 mov r8, rdi
15+ 21 add r8, r9
16+ 24 mov rax, r8
1917 15 end add;
20- 37 leave
21- 38 ret
18+ 27 leave
19+ 28 ret
2220
2321 Listing.main:
2422 17 proc main*;

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

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

test/run/KeptCheck.raz (history) added +19 -0

@@ -0,0 +1,19 @@
1+(* A procedure that calls nothing keeps its first parameters in rdi and
2+ rsi; a failed check in it still says their values. *)
3+module KeptCheck;
4+import Out;
5+
6+var a: [4]I64;
7+
8+proc at(i: INT; b: U8): I64;
9+begin
10+ return a[i] + I64(b);
11+end;
12+
13+proc main*;
14+begin
15+ Out.int(at(3, 1)); Out.ln;
16+ Out.int(at(4, 200)); Out.ln;
17+end;
18+
19+end KeptCheck.

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

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

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

@@ -0,0 +1,3 @@
1+test/run/KeptCheck.raz:10:14: index 4 is out of range for a length of 4
2+ in KeptCheck.at at test/run/KeptCheck.raz:10:14: i = 4, b = 200
3+ in KeptCheck.main at test/run/KeptCheck.raz:16:5

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

An empty file.

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

@@ -0,0 +1,21 @@
1+(* A parameter kept in rdi or rsi is shown when a signal stops the
2+ procedure that keeps it. *)
3+module KeptNil;
4+import Out;
5+
6+type Rec = record x: INT; end;
7+
8+proc get(p: ^Rec; k: INT): INT;
9+begin
10+ return p^.x + k;
11+end;
12+
13+proc main*;
14+var r: Rec;
15+begin
16+ r.x := 1;
17+ Out.int(get(ADR(r), 2)); Out.ln;
18+ Out.int(get(nil, 5)); Out.ln;
19+end;
20+
21+end KeptNil.

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

@@ -0,0 +1 @@
1+139

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

@@ -0,0 +1,3 @@
1+test/run/KeptNil.raz:10:5: stopped by a nil pointer
2+ in KeptNil.get at test/run/KeptNil.raz:10:5: p = nil, k = 5
3+ in KeptNil.main at test/run/KeptNil.raz:18:5