From 654579f9338fd9dcac00247add685fc4d8693356 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:00:31 +0100 Subject: [PATCH] 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%. --- diff --git a/PLAN.md b/PLAN.md index 898b7f8..cf3e00f 100644 --- a/PLAN.md +++ b/PLAN.md @@ -24,7 +24,7 @@ its length, chosen by the read-ahead as a single value is, and counted as two. FPC's search keeps its slice in `rdi` and `rsi`; raz's reads both from the frame on every round. -2. **Parameters left where they arrive.** In a procedure that calls +2. Done: **Parameters left where they arrive.** In a procedure that calls nothing, copies no block and divides by no variable, as the read-ahead can tell, a parameter stays in the register it came in, and no register is saved for it. Most of rat's hot procedures are diff --git a/doc/compiler.md b/doc/compiler.md index 42ced96..c59984d 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -110,22 +110,22 @@ push rbp ; EnterBody: the frame mov rbp,rsp sub rsp,0x10 ; its size, patched in by EndProc -mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots -mov QWORD PTR [rbp-0x10],rsi -mov r8,QWORD PTR [rbp-0x8] ; a, at depth 0 -mov r9,QWORD PTR [rbp-0x10] ; b, at depth 1; the 2 is only noted -shl r9,0x1 ; times 2 is a shift +mov r9,rsi ; b, at depth 1; a, at depth 0, and +shl r9,0x1 ; the 2 are only noted; times 2 +mov r8,rdi ; is a shift; a is loaded for + add r8,r9 mov rax,r8 ; return: the result leave ; no jump to here, as it is next ret ``` -Every line of it can be traced to a routine in `gen.pas`: the loads -are `PushVar`'s, the shift and the add `Op`'s, the result -`ReturnJump`'s, and the frame is `EnterBody`'s and `EndProc`'s. It is -not fast code. It is code whose every instruction you can account -for, which is the point. +`scale` calls nothing, so its parameters stay in `rdi` and `rsi`, +where they arrive (`KeepArrived`): they are register variables that +cost nothing to keep. Every line of it can be traced to a routine in +`gen.pas`: the moves are `Loaded`'s, the shift and the add `Op`'s, the +result `ReturnJump`'s, and the frame is `EnterBody`'s and `EndProc`'s. +It is not the fastest code. It is code whose every instruction you +can account for, which is the point. ## Better code, in one procedure @@ -304,6 +304,30 @@ temporary first (`CopyToTemp`). rat 5% faster, the heap sort 5%, the search 4%. +2. **Parameters left where they arrive.** `find(s, sub: []U8)` stored + its first slice in its frame, or in two registers it saved and + restored: + + ``` + mov [rbp - 40], r12 ; the caller's r12 and r13, saved + mov [rbp - 32], r13 + mov r12, rdi ; s, into them + mov r13, rsi + ``` + + In a procedure that names no procedure, a procedure variable, `NEW`, + `SYSCALL` or `COPY`, as `ChooseRegs` sees reading ahead, the + parameters that arrive in `rdi` and `rsi` stay there, which only a + call or a copy needs: nothing is saved or moved, and `s[i]` is read + through `rdi` itself (`KeepArrived`). Where gen needs them after + all, to copy a record or compare two slices, it sets `lost`, and + `Body` compiles the procedure again with the parameters in memory, + as for a variable demoted. The trap report shows a parameter kept + so: `Rt.fail` is given `rdi` and `rsi` as the failing code had + them, and the signal handler takes them from the context the + kernel saved. The search 9% faster, rat 1.5%, its text 0.7% + smaller. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is @@ -519,7 +543,9 @@ calls a stub for its kind of failure (`failStubs`, made by `EntryPoint`), which says the kind and passes the address it was called from, by which `Rt` finds the place: the failing code has the -place of the offending expression (`LineCol` in `scan.pas`). A check +place of the offending expression (`LineCol` in `scan.pas`). It leaves +`rdi` and `rsi` as they were, so that the stub can pass them on, for +the trap report to show a parameter kept in them. A check costs a compare and a jump where it stands, and a dozen bytes out of the way. Code is aligned to 32 bytes, the windows x86 fetches it in, so that where a procedure's loops fall doesn't depend on what comes @@ -906,8 +932,10 @@ `_ := f(x)` drops one on purpose. `-Werror` makes each an error. - **Registers for variables.** Up to five of a procedure's single values, not reals, whose address is never taken, live in `rbx` and - `r12` to `r15`, a slice taking two of them; the trap report shows - them as it shows the others, but for slices, which it never shows. + `r12` to `r15`, a slice taking two of them; in a procedure that + calls nothing, a parameter that arrives in `rdi` or `rsi` stays + there. The trap report shows them as it shows the others, but for + slices, which it never shows. - **Several results** that are two words, and a slice, come back in rax and rdx, as from a code procedure; others through a hidden pointer to the caller's slots. diff --git a/lib/Rt.raz b/lib/Rt.raz index 40b10e6..dfb0c7c 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -72,8 +72,10 @@ places: ^INT; line: [256]U8; (* the message being made *) (* rbx, r12, r13, r14 and r15 in the frame being told of, where the - compiler keeps a procedure's busiest variables *) - regs: [5]UINT; + compiler keeps a procedure's busiest variables; then rdi and rsi + where the program stopped, where a procedure that calls nothing + keeps the parameters that arrive in them *) + regs: [7]UINT; lineLen: INT; act: Action; alt: Stack; @@ -425,10 +427,13 @@ runs backwards. What a target's machine doesn't catch the code tests for, and stops at with what 4, a nil pointer; 5, a division by zero; 6, a stack overflow; these end the program by the signal - the machine would have given, as if it had. *) -proc fail*(what, a, b, len: INT; at: UINT; name: ^U8); + the machine would have given, as if it had. rdi and rsi are the + registers as the failing code had them. *) +proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT); var sig: INT; begin + regs[5] := rdi; + regs[6] := rsi; where(at); if what >= 4 then put("stopped by "); @@ -481,8 +486,10 @@ proc onSignal(sig: INT; info, ctx: ^U8); var adr, sp, ip, bp: UINT; q: ^UINT; why: []U8; begin - q := ^UINT(ctx + 120); (* uc_mcontext's rbp *) - bp := q^; + q := ^UINT(ctx + 104); (* uc_mcontext's rdi, then rsi, then rbp *) + regs[5] := q[0]; + regs[6] := q[1]; + bp := q[2]; q := ^UINT(info + 16); (* si_addr *) adr := q^; q := ^UINT(ctx + 160); (* uc_mcontext's rsp, then rip *) diff --git a/src/gen.pas b/src/gen.pas index 5e617c2..871e630 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -100,12 +100,19 @@ callee-saved ones, so they live through calls (a register variable); a procedure saves those it uses, and Rt finds them there for the trap report } VarRegs: array[0..4] of longint = (RBX, R12, R13, R14, R15); + { the registers the trap report can show a variable in: VarRegs, then + rdi and rsi, where a procedure that calls nothing keeps the + parameters that arrive in them (KeptIn) } + ShownRegs: array[0..6] of longint = (RBX, R12, R13, R14, R15, RDI, RSI); var rtFail, argcChunk, argvChunk: longint; { the stubs that Check's code calls, one for each kind of failure, made by EntryPoint for those used } failStubs: array[0..5] of longint = (-1, -1, -1, -1, -1, -1); + lost: boolean; { a parameter kept where it arrived was wanted + for other work: the procedure is compiled again, + the parameter in memory } depth: longint; { the values on the stack } pushed: longint; { quadwords pushed on the machine's stack } codeChunk: longint = -1; { the procedure or body being compiled, or -1 } @@ -171,6 +178,8 @@ procedure BeginProc(sig: PType; params: PObj); procedure ResetLocals; +procedure KeepArrived(o: PObj); +function KeptIn(o: PObj): boolean; procedure UseRegs(mask: longint); procedure MarkCode(var m: TCodeMark); procedure Redo(const m: TCodeMark); @@ -234,6 +243,7 @@ numIn: longint; procParams: PObj; { the parameters, for EnterBody } regMask: longint; { the registers of variables: bit i is VarRegs[i] } + kept: longint; { bit r: register r holds a parameter, as it arrived } saveAt: int64; { where they are saved, a quadword each } frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations } { the checks of the procedure, whose failures EndFails makes } @@ -366,6 +376,13 @@ if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1; end; +{ rsi and rdi are about to be used for other work, as in copying: a + parameter kept in either must go to memory (lost). } +procedure ClobberArgs; +begin + if kept <> 0 then lost := true; +end; + { The value i into register r, from wherever it is: a constant or an address is made there, a variable's register or the value's own register copied, a spilled value loaded. Unlike Loaded, it leaves the @@ -556,6 +573,7 @@ size bytes to off bytes past the destination. } procedure CopyBlock(size, off: int64); begin + ClobberArgs; PopTo(RSI); PopTo(RDI); if off <> 0 then Lea(RDI, Mem(RDI, off)); @@ -566,6 +584,7 @@ registers, its two words into them. } procedure CopyTo(const a: TAdr; size: int64); begin + ClobberArgs; PopTo(RSI); if a.base = bReg then begin Load(a.chunk, Mem(RSI, 0), 8, false); @@ -578,6 +597,7 @@ procedure ZeroVar(const a: TAdr; size: int64); begin + ClobberArgs; Lea(RDI, Place(a, RDI)); MovRI(RCX, size); MovRI(RAX, 0); @@ -871,6 +891,7 @@ procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); var differ, r: longint; begin + ClobberArgs; r := Target; Load(RCX, Mem(RBP, b + 8), 8, false); AluM(aluCmp, RCX, Mem(RBP, a + 8)); @@ -941,6 +962,7 @@ procedure ZeroNew(size: int64); var L, r: longint; begin + ClobberArgs; r := Top; L := NoLabel; TestRR(r, r); @@ -977,6 +999,7 @@ const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); var i: longint; begin + ClobberArgs; SpillBelow(depth); for i := 0 to n - 1 do with stack[depth - n + i] do @@ -1095,7 +1118,10 @@ division fault and a stack overflow, which this machine catches itself, so no check here has them. The arguments go in rdi, rsi, rdx, rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m, - or n if m.base is NoBase. The failing code is out of the way, at the + or n if m.base is NoBase, or in register m.index if it is ARegister. + rdi and rsi are passed on as they were, since a parameter may be kept + in them, for the trap report to show: so a goes to the stub in rax. + The failing code is out of the way, at the procedure's end (EndFails), so that a check that holds costs a jump not taken; it has p for its place (at is p's offset in the source), never returns, and may use any register once it has read what it @@ -1125,7 +1151,7 @@ FixLink(L); LineCol(at, line, col); NoteLine(line, col); - if ra <> RSI then MovRR(RSI, ra); + if ra <> RAX then MovRR(RAX, ra); if m.base = NoBase then MovRI(RCX, n) else if m.base = ARegister then MovRR(RCX, m.index) else Load(RCX, m, 8, false); @@ -1223,6 +1249,7 @@ begin PopTo(RDX); PopTo(RCX); + if a.base = bSlot then ClobberArgs; { Place loads its slot into rdi } if a.base = bReg then ml := InRegister(a.hi) { a slice in registers } else begin m := Place(a, RDI); @@ -1256,6 +1283,7 @@ procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); var ml: TMem; L, E: longint; begin + ClobberArgs; Load(RDX, Mem(RBP, sOff + 8), 8, false); if checked then begin ml := Mem(RBP, dOff + 8); @@ -1288,6 +1316,7 @@ bounds can't be known, but it can't run backwards. } procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); begin + ClobberArgs; PopTo(RDX); PopTo(RCX); PopTo(RDI); @@ -1322,6 +1351,7 @@ begin { the arguments are the c.n values on top, where computing them left them; the registers in use under them are saved around the call } + ClobberArgs; first := depth - c.n; SpillBelow(first); res := c.sig^.base; @@ -1463,6 +1493,8 @@ ResetStack(0, 0); exitL := NoLabel; numIn := 0; + lost := false; + kept := 0; resType := sig^.base; procParams := params; regMask := 0; @@ -1517,16 +1549,54 @@ frameTop := paramTop; frameMax := paramTop; permTop := paramTop; end; +{ The register the word (0, or 1 for a slice's second) of parameter o + arrives in, or -1 if it comes on the stack or in an xmm register } +function ArrivesIn(o: PObj; word: longint): longint; +var i: longint; +begin + ArrivesIn := -1; + for i := 0 to numIn - 1 do + if (inObj[i] = o) and (inSlot[i] = o^.val + 8 * word) and (inReg[i] >= 0) then ArrivesIn := inReg[i]; +end; + +{ Parameter o, of a procedure that calls nothing, stays in the register + it arrives in if that is rdi or rsi, which only a call or a copy needs: + no register is saved for it, and nothing is moved. A slice stays only + if it has both. If the procedure turns out to copy after all, lost is + set, and it is compiled again with o in memory. } +procedure KeepArrived(o: PObj); +var r: longint; +begin + r := ArrivesIn(o, 0); + if o^.typ^.form = fSlice then begin + if (r = RDI) and (ArrivesIn(o, 1) = RSI) then begin o^.reg := RDI; o^.reg2 := RSI end; + end else if (r = RDI) or (r = RSI) then o^.reg := r; +end; + +{ Whether parameter o is kept where it arrived } +function KeptIn(o: PObj): boolean; +begin + KeptIn := (o^.reg = RDI) or (o^.reg = RSI); +end; + { The locals in memory have their slots: the prologue zeroes those and nothing below. The procedure keeps variables in the registers of mask, which are saved next, a quadword each, in slots that need no zeroing; the frame as it then stands is where a second try at the body starts. } procedure UseRegs(mask: longint); -var i, k: longint; +var i, k: longint; p: PObj; begin zeroTop := permTop; regMask := mask; + { the parameters left in rdi and rsi, as they arrived } + kept := 0; + p := procParams; + while p <> nil do begin + if p^.reg in [RDI, RSI] then kept := kept or (1 shl p^.reg); + if p^.reg2 in [RDI, RSI] then kept := kept or (1 shl p^.reg2); + p := p^.next; + end; k := 0; for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k); if k > 0 then saveAt := AllocPerm(8 * k, 8); @@ -1544,6 +1614,7 @@ begin pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said); numFails := 0; + lost := false; frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; exitL := NoLabel; end; @@ -1598,7 +1669,7 @@ else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin r := inObj[i]^.reg; if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2; - MovRR(r, inReg[i]); + if r <> inReg[i] then MovRR(r, inReg[i]); { else it is kept where it is } if inSize[i] < 8 then Extend(r, inSize[i], inObj[i]^.typ^.signed); end else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); { a register variable's parameter that came on the stack, above the @@ -1631,6 +1702,7 @@ bytes into it. } procedure CopyResult(off, size: int64); begin + ClobberArgs; PopTo(RSI); Load(RDI, Mem(RBP, retSlot), 8, false); if off <> 0 then Lea(RDI, Mem(RDI, off)); @@ -1788,14 +1860,19 @@ check's call is. The return address is the next instruction's, which may belong to another line; one byte back is inside the call, so the lookup finds the call's line. rsp is aligned for the call, as the - check's code may have pushed an odd number of quadwords. } + check's code may have pushed an odd number of quadwords. rdi and rsi + as the failing code had them go on the stack, as Rt.fail's last two + arguments, and a comes in rax (EndFails). } for i := 0 to High(failStubs) do if failStubs[i] >= 0 then begin BeginChunk(failStubs[i]); - MovRI(RDI, i); Load(R8, Mem(RSP, 0), 8, false); AluI(aluSub, R8, 1); AluI(aluAnd, RSP, -16); + Push(RSI); + Push(RDI); + MovRR(RSI, RAX); + MovRI(RDI, i); CallChunk(rtFail); Ud2; EndChunk(failStubs[i]); diff --git a/src/parse.pas b/src/parse.pas index 174cbae..84dbc11 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1814,8 +1814,8 @@ else exit(0); end; VarKind := k + o^.typ^.size * 16 + ord(o^.ind) * 256; - for i := 0 to 4 do - if o^.reg = VarRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512; + for i := 0 to High(ShownRegs) do + if o^.reg = ShownRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512; end; { Whether variable o could live in registers: a single value, not a @@ -1840,12 +1840,16 @@ its "begin" to its end, a use counts 1, in a loop 4, in two 16, in three or more 64, and a variable ADR is taken of can't have one. A register costs a save and a restore, so a variable used less than three times keeps its - place in memory. A slice takes two, its pointer's and its length's. } + place in memory. A slice takes two, its pointer's and its length's. + In a procedure that names no procedure, so calls nothing, the + parameters that arrive in the registers no call needs stay there + (KeepArrived), at no cost. } procedure ChooseRegs(params, before: PObj); var cand: array[0..63] of PObj; weight: array[0..63] of longint; isLoop: array[0..255] of boolean; - n, i, k, d, loops, best, need: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; + n, i, k, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState; + afterDot, wasSkipping, leaf: boolean; function Candidate(name: longint): longint; var j: longint; @@ -1859,6 +1863,14 @@ if RegFit(o) and (n <= High(cand)) then begin cand[n] := o; weight[n] := 0; inc(n) end; end; + { Whether naming o may make a call: a procedure, a procedure variable, + or NEW, SYSCALL or COPY, which use the registers a call would } + function Calls(o: PObj): boolean; + begin + Calls := (o <> nil) and ((o^.cls = oProc) or (o^.cls = oVar) and (o^.typ^.form = fProc) + or (o^.cls = oBuiltin) and ((o^.val = bNew) or (o^.val = bSyscall) or (o^.val = bCopy))); + end; + begin n := 0; o := params; @@ -1869,8 +1881,18 @@ SaveScan(st); wasSkipping := skipping; skipping := true; { its slips are told when it is compiled } - d := 0; loops := 0; afterDot := false; + d := 0; loops := 0; afterDot := false; leaf := true; qual := nil; repeat + { a name, or a module's name and one of its exports, that calls } + if (tok = sIdent) and not afterDot then begin + q := Lookup(id); + if Calls(q) then leaf := false; + qual := nil; + if (q <> nil) and (q^.cls = oModule) then qual := q; + end else if (tok = sIdent) and (qual <> nil) then begin + if (qual^.module = nil) or Calls(Find(qual^.module^.scope, id)) then leaf := false; + qual := nil; + end; case tok of sBegin, sIf, sMatch, sWhile, sFor, sRepeat: if d < High(isLoop) then begin @@ -1899,6 +1921,13 @@ until (d = 0) or (tok = sEof); RestoreScan(st); skipping := wasSkipping; + if leaf then begin + o := params; + while o <> nil do begin + if RegFit(o) and (weight[Candidate(o^.name)] >= 0) then KeepArrived(o); + o := o^.next; + end; + end; { a slice's weight is shared by its two registers } for i := 0 to n - 1 do if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2; @@ -2001,7 +2030,16 @@ p^.openEnd := (curResult <> nil) and not ends; NoteLine(CurLine, CurCol); Expect(sEnd); - if not demoted or (errors <> errs) then break; + if not (demoted or lost) or (errors <> errs) then break; + { a parameter kept where it arrived goes to memory: a call or a copy + wanted its register } + if lost then begin + o := params; + while o <> nil do begin + if KeptIn(o) then begin o^.reg := 0; o^.reg2 := 0 end; + o := o^.next; + end; + end; numDrops := drops0; Redo(mark); PlaceLocals(params, before); diff --git a/src/x64.pas b/src/x64.pas index 4a2c5ab..4c72ce9 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -495,7 +495,7 @@ end else if signed then Say('movsx ' + RN(r) + ', ' + RN(r, size)) else Say('movzx ' + RN(r, 4) + ', ' + RN(r, size)); case size of - 1: if signed then OpR(0, true, $BE0F, r, r) else OpR(0, false, $B60F, r, r); + 1: if signed then OpR(0, true, $BE0F, r, r, true) else OpR(0, false, $B60F, r, r, true); 2: if signed then OpR(0, true, $BF0F, r, r) else OpR(0, false, $B70F, r, r); 4: if signed then OpR(0, true, $63, r, r) else OpR(0, false, $89, r, r); end; @@ -505,9 +505,9 @@ procedure SetCC(cc, r: longint); begin if listing then Say('set' + CondNames[cc] + ' ' + RN(r, 1)); - OpR(0, false, $900F + cc shl 8, 0, r); + OpR(0, false, $900F + cc shl 8, 0, r, true); if listing then Say('movzx ' + RN(r, 4) + ', ' + RN(r, 1)); - OpR(0, false, $B60F, r, r); + OpR(0, false, $B60F, r, r, true); end; procedure TestRR(a, b: longint); diff --git a/test/enctest.pas b/test/enctest.pas index 4179683..1ab67c1 100644 --- a/test/enctest.pas +++ b/test/enctest.pas @@ -19,8 +19,8 @@ AluI(aluAdd, RAX, 1); AluI(aluAnd, RAX, 1000); AluI(aluCmp, RSP, 8); AluM(aluCmp, RAX, Mem(RCX, 8)); AluM(aluAdd, RAX, Mem(RCX, 0)); Imul(RAX, RCX); ImulI(RAX, 24); Shift(shShl, RAX); Shift(shSar, RAX); ShiftI(shShr, RCX, 3); Unary(2, RAX); Unary(3, RAX); Unary(6, RCX); Unary(7, RCX); Cqo; - Extend(RAX, 1, true); Extend(RAX, 1, false); Extend(RAX, 2, true); Extend(RAX, 2, false); Extend(RAX, 4, true); Extend(RAX, 4, false); - SetCC(ccL, RAX); SetCC(ccA, RCX); TestRR(RAX, RAX); + 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); + SetCC(ccL, RAX); SetCC(ccA, RCX); SetCC(ccE, RDI); TestRR(RAX, RAX); Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb; LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX); MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsdXM(2, Mem(RBP, -8)); MovsdMX(Mem(RBP, -8), 7); diff --git a/test/list/Listing.expect b/test/list/Listing.expect index ae71ccc..0a6c659 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -8,17 +8,15 @@ 0 push rbp 1 mov rbp, rsp 4 sub rsp, 16 - 11 mov [rbp - 8], rdi - 15 mov [rbp - 16], rsi 14 return a + b * 2; - 19 mov r8, [rbp - 8] - 23 mov r9, [rbp - 16] - 27 shl r9, 1 - 31 add r8, r9 - 34 mov rax, r8 + 11 mov r9, rsi + 14 shl r9, 1 + 18 mov r8, rdi + 21 add r8, r9 + 24 mov rax, r8 15 end add; - 37 leave - 38 ret + 27 leave + 28 ret Listing.main: 17 proc main*; diff --git a/test/run/KeptCheck.expect b/test/run/KeptCheck.expect new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/test/run/KeptCheck.expect @@ -0,0 +1 @@ +1 diff --git a/test/run/KeptCheck.raz b/test/run/KeptCheck.raz new file mode 100644 index 0000000..78e99ab --- /dev/null +++ b/test/run/KeptCheck.raz @@ -0,0 +1,19 @@ +(* A procedure that calls nothing keeps its first parameters in rdi and + rsi; a failed check in it still says their values. *) +module KeptCheck; +import Out; + +var a: [4]I64; + +proc at(i: INT; b: U8): I64; +begin + return a[i] + I64(b); +end; + +proc main*; +begin + Out.int(at(3, 1)); Out.ln; + Out.int(at(4, 200)); Out.ln; +end; + +end KeptCheck. diff --git a/test/run/KeptCheck.status b/test/run/KeptCheck.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/KeptCheck.status @@ -0,0 +1 @@ +2 diff --git a/test/run/KeptCheck.stderr b/test/run/KeptCheck.stderr new file mode 100644 index 0000000..7b7733d --- /dev/null +++ b/test/run/KeptCheck.stderr @@ -0,0 +1,3 @@ +test/run/KeptCheck.raz:10:14: index 4 is out of range for a length of 4 + in KeptCheck.at at test/run/KeptCheck.raz:10:14: i = 4, b = 200 + in KeptCheck.main at test/run/KeptCheck.raz:16:5 diff --git a/test/run/KeptNil.expect b/test/run/KeptNil.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/KeptNil.raz b/test/run/KeptNil.raz new file mode 100644 index 0000000..75b2970 --- /dev/null +++ b/test/run/KeptNil.raz @@ -0,0 +1,21 @@ +(* A parameter kept in rdi or rsi is shown when a signal stops the + procedure that keeps it. *) +module KeptNil; +import Out; + +type Rec = record x: INT; end; + +proc get(p: ^Rec; k: INT): INT; +begin + return p^.x + k; +end; + +proc main*; +var r: Rec; +begin + r.x := 1; + Out.int(get(ADR(r), 2)); Out.ln; + Out.int(get(nil, 5)); Out.ln; +end; + +end KeptNil. diff --git a/test/run/KeptNil.status b/test/run/KeptNil.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/run/KeptNil.status @@ -0,0 +1 @@ +139 diff --git a/test/run/KeptNil.stderr b/test/run/KeptNil.stderr new file mode 100644 index 0000000..3ea206f --- /dev/null +++ b/test/run/KeptNil.stderr @@ -0,0 +1,3 @@ +test/run/KeptNil.raz:10:5: stopped by a nil pointer + in KeptNil.get at test/run/KeptNil.raz:10:5: p = nil, k = 5 + in KeptNil.main at test/run/KeptNil.raz:18:5