From dd5e5a98e6db2b3ac6fa8c6e63b4e25381a5ae08 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:02:29 +0100 Subject: [PATCH] Two results left on the stack; values popped where they go v0.7.0 step 3. A call's two results, as rax and rdx bring them, stay on gen's stack (item mode mWords): a, b := f(x) brings the first to the top and pops each into its variable, in order, with nothing through memory; return f(x) passes them on as they are; anything else makes them a temporary. rat's two hottest lines were such calls. PopTo(r) puts the value on top straight into r from wherever it is (ValueTo, ArgTo that was), not through its stack register first; and IndexSlice works out an element's address in the index's register. Those save moves, which this machine renames for free: size, not time. rat: 547 -> 505 ms (-8%), text 552,812 -> 548,332 (-0.8%); now 1.19 times pt built by FPC -O2 -Cr (424 ms), inside the milestone's 1.25. --- diff --git a/conform/run/TwoWords.expect b/conform/run/TwoWords.expect index 7ce21ed..84ef5a1 100644 --- a/conform/run/TwoWords.expect +++ b/conform/run/TwoWords.expect @@ -6,3 +6,5 @@ llo 0 0 14 111 +18 +5 22 diff --git a/conform/run/TwoWords.raz b/conform/run/TwoWords.raz index 17209c3..07f2ad0 100644 --- a/conform/run/TwoWords.raz +++ b/conform/run/TwoWords.raz @@ -46,7 +46,7 @@ end; proc main*; -var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT); +var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT); pv: ^INT; begin x, y := pair(-5, 7); Out.int(I64(x)); Out.str(" "); Out.int(I64(y)); Out.ln; b, w := mixed(-4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln; @@ -56,6 +56,9 @@ Out.str(tail("hello", 2)); Out.ln; p, q := fallOff(0); Out.int(p); Out.str(" "); Out.int(q); Out.ln; _, v := nested(3); Out.int(v); Out.str(" "); Out.int(log); Out.ln; + (* in order: the second result is the one left *) + v, v := nested(4); Out.int(v); Out.ln; + pv := ADR(q); pv^, p := nested(5); Out.int(q); Out.str(" "); Out.int(p); Out.ln; end; end TwoWords. diff --git a/doc/compiler.md b/doc/compiler.md index f875217..4f49da3 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -13,9 +13,9 @@ | `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout | | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,641 | 1,287 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,467 | 1,223 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,916 | 2,580 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/gen.pas` | 1,650 | 1,282 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/item.pas` | 1,478 | 1,234 | Items: types, constants, variables, operators, conversions, calls | +| `src/parse.pas` | 2,927 | 2,588 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 197 | 147 | The command line | @@ -349,15 +349,16 @@ variable in a register only noted, the rest in registers, or spilled by a call in a later argument, so that it can't disturb them. `Call` spills what is under them, moves each into the register System V -wants (`ArgTo`), `r8` and `r9` last since they may hold others, and +wants (`ValueTo`), `r8` and `r9` last since they may hold others, and aligns the stack. A slice goes as its two words, in two registers or both on the stack, as System V passes a struct of two integers, and the procedure keeps them in its frame. A record passed by value goes as the address of a copy. A result of two words, a slice or two single values that aren't reals (`InTwoRegs`, in `sym.pas`), comes back in rax and rdx, as System V returns a struct of -two integers: `return` leaves both on the stack for `ReturnJump`, and -`Call` stores them in a temporary of the caller's. Any other record or +two integers: `return` leaves both on the stack for `ReturnJump`. +`Call` leaves two results on the stack, and stores a slice in a +temporary of the caller's. Any other record or several results come back through a hidden pointer in `rdi`, as C's structs do (`BehindPointer`, `ResultTo`, `CopyResult`). @@ -392,6 +393,11 @@ - `mVar`: a variable, at `base`, `chunk` and `off`, which `AdrOf` makes a `TAdr` for gen; - `mStack`: a value on gen's stack; +- `mWords`: the two results of a call, as rax and rdx brought them, on + gen's stack, the first under the second. `a, b := f(x)` brings the + first to the top (`Swap`) and pops each into its variable, so that + nothing goes through memory; `return f(x)` leaves them for + `ReturnJump`; anything else makes them a temporary (`CopyToTemp`); - `mCond`: a condition, as Wirth's compilers keep one: two integers on gen's stack, compared by `rel` once something wants the result, and the jumps of `and` and `or` that land where it is false (`fj`) or diff --git a/src/gen.pas b/src/gen.pas index 3f7e5c4..49388d8 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -75,7 +75,7 @@ target: int64; { the frame slot of a procedure value, if chunk < 0 } chunk: longint; { else the procedure's chunk } ext: boolean; { C's: called through the chunk holding its address } - ret: int64; { set by Call: the temporary a result of more than one word is in } + ret: int64; { set by Call: the temporary a slice or a record result is in } end; const @@ -335,8 +335,30 @@ begin dec(depth); if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1; +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 + value where it was, so r is its only new copy. } +procedure ValueTo(i, r: longint); +begin + case stack[i].loc of + lConst: MovRI(r, stack[i].val); + lHeld: MovRR(r, stack[i].val); + lAdr: Lea(r, AdrMem(i, 0)); + lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); + else Load(r, Mem(RBP, stack[i].val), 8, false); + end; end; +{ The value on top, popped into register r } +procedure PopTo(r: longint); +begin + ValueTo(depth - 1, r); + Drop; +end; + { Whether the value on top is a constant, and if so, which } function TopConst(var v: int64): boolean; begin @@ -493,10 +515,8 @@ size bytes to off bytes past the destination. } procedure CopyBlock(size, off: int64); begin - MovRR(RSI, Top); - Drop; - MovRR(RDI, Top); - Drop; + PopTo(RSI); + PopTo(RDI); if off <> 0 then Lea(RDI, Mem(RDI, off)); CopyBytes(size); end; @@ -504,8 +524,7 @@ { Pops a source's address, and copies size bytes to a. } procedure CopyTo(const a: TAdr; size: int64); begin - MovRR(RSI, Top); - Drop; + PopTo(RSI); Lea(RDI, Place(a, RDI)); CopyBytes(size); end; @@ -796,8 +815,7 @@ procedure ErrorText(tmp: int64); var L: longint; begin - MovRR(RAX, Top); - Drop; + PopTo(RAX); MovRI(RCX, 0); L := NoLabel; TestRR(RAX, RAX); @@ -861,8 +879,7 @@ var r, n: longint; begin if which = bCas then begin n := Top; Drop end; - MovRR(RAX, Top); - Drop; + PopTo(RAX); r := Top; case which of bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end; @@ -1059,21 +1076,28 @@ end; { Pops an index, and the address of the slice at a if it is on the - stack, and pushes the address of its element of size bytes. } + stack, and pushes the address of its element of size bytes: the + index, checked against the length, times size, plus the pointer. + It is worked out in the index's own register, which then holds the + address; but if the slice's address is on the stack, under the + index, the element's register is the one that holds that, still + needed, so the work is done in rax. } procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); -var m, ml: TMem; +var m, ml: TMem; r: longint; inRax: boolean; begin - MovRR(RAX, Top); - Drop; + inRax := a.base = bStack; + if inRax then begin r := RAX; PopTo(RAX) end + else begin r := Top; Drop end; m := Place(a, RCX); ml := m; ml.disp := ml.disp + 8; if checked then begin - AluM(aluCmp, RAX, ml); - Check(ccB, 0, p, RAX, ml, 0, -1); + AluM(aluCmp, r, ml); + Check(ccB, 0, p, r, ml, 0, -1); end; - if size <> 1 then ImulI(RAX, size); - AluM(aluAdd, RAX, m); - MovRR(Target, RAX); + if size <> 1 then ImulI(r, size); + AluM(aluAdd, r, m); + if inRax then MovRR(Target, RAX) + else Claim(depth); { the index's register again, now for the address } Computed(kWord); end; @@ -1083,10 +1107,8 @@ procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); var m, ml: TMem; begin - MovRR(RDX, Top); - Drop; - MovRR(RCX, Top); - Drop; + PopTo(RDX); + PopTo(RCX); m := Place(a, RDI); ml := m; ml.disp := ml.disp + 8; if checked then begin @@ -1142,12 +1164,9 @@ bounds can't be known, but it can't run backwards. } procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); begin - MovRR(RDX, Top); - Drop; - MovRR(RCX, Top); - Drop; - MovRR(RDI, Top); - Drop; + PopTo(RDX); + PopTo(RCX); + PopTo(RDI); if checked then begin Alu(aluCmp, RCX, RDX); Check(ccLE, 3, p, RCX, NoMem, 0, -1); @@ -1165,17 +1184,6 @@ { The call c, its arguments already in their frame slots: they go to registers or the stack, the procedure is called, and its result, if any, is pushed on the value stack. } -{ Argument value i, as it is on the stack, into register r } -procedure ArgTo(i, r: longint); -begin - case stack[i].loc of - lConst: MovRI(r, stack[i].val); - lHeld: MovRR(r, stack[i].val); - lAdr: Lea(r, AdrMem(i, 0)); - lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); - else Load(r, Mem(RBP, stack[i].val), 8, false); - end; -end; procedure Call(var c: TCall); var @@ -1189,7 +1197,7 @@ res := c.sig^.base; c.ret := 0; ni := 0; nf := 0; - if (res <> nil) and IsAggregate(res) then begin + if (res <> nil) and (BehindPointer(res) or (res^.form = fSlice)) then begin c.ret := AllocTemp(res^.size, res^.align); if BehindPointer(res) then ni := 1; end; @@ -1213,7 +1221,7 @@ if pad <> 0 then AluI(aluSub, RSP, 8); for i := c.n - 1 downto 0 do if onStack[i] then begin - ArgTo(first + i, RAX); + ValueTo(first + i, RAX); Push(RAX); end; ni := 0; nf := 0; @@ -1225,14 +1233,14 @@ if not onStack[i] then if c.isF[i] then begin if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val)) - else begin ArgTo(first + i, RAX); MovqXR(nf, RAX) end; + else begin ValueTo(first + i, RAX); MovqXR(nf, RAX) end; inc(nf); end else begin - if ni < 4 then ArgTo(first + i, IntRegs[ni]) else late[ni] := first + i; + if ni < 4 then ValueTo(first + i, IntRegs[ni]) else late[ni] := first + i; inc(ni); end; - if late[4] >= 0 then ArgTo(late[4], RAX); - if late[5] >= 0 then ArgTo(late[5], R9); + if late[4] >= 0 then ValueTo(late[4], RAX); + if late[5] >= 0 then ValueTo(late[5], R9); if late[4] >= 0 then MovRR(R8, RAX); for i := 1 to c.n do Drop; { a procedure taking ... is told in al how many xmm registers it got } @@ -1246,15 +1254,17 @@ CallR(R11); end; if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); - if (c.ret <> 0) and InTwoRegs(res) then - if res^.form = fSlice then begin - Store(Mem(RBP, c.ret), RAX, 8); - Store(Mem(RBP, c.ret + 8), RDX, 8); - end else begin - Store(Mem(RBP, c.ret + res^.fields^.val), RAX, res^.fields^.typ^.size); - Store(Mem(RBP, c.ret + res^.fields^.next^.val), RDX, res^.fields^.next^.typ^.size); - end; - if (res <> nil) and (c.ret = 0) then begin + { the result: a slice in rax and rdx goes to the caller's temporary, + two results on the stack, the first under the second, and a single + value on top } + if res = nil then + else if res^.form = fSlice then begin + Store(Mem(RBP, c.ret), RAX, 8); + Store(Mem(RBP, c.ret + 8), RDX, 8); + end else if InTwoRegs(res) then begin + MovRR(Target, RAX); Computed(kWord); + MovRR(Target, RDX); Computed(kWord); + end else if c.ret = 0 then begin r := Target; if res^.form = fReal then MovqRX(r, 0) else begin @@ -1465,8 +1475,7 @@ bytes into it. } procedure CopyResult(off, size: int64); begin - MovRR(RSI, Top); - Drop; + PopTo(RSI); Load(RDI, Mem(RBP, retSlot), 8, false); if off <> 0 then Lea(RDI, Mem(RDI, off)); CopyBytes(size); @@ -1480,11 +1489,11 @@ if resType <> nil then if BehindPointer(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) else if InTwoRegs(resType) then begin - MovRR(RDX, Top); Drop; - MovRR(RAX, Top); Drop; + PopTo(RDX); + PopTo(RAX); end else begin - if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top); - Drop; + if resType^.form = fReal then begin MovqXR(0, Top); Drop end + else PopTo(RAX); end; Jmp(exitL); end; diff --git a/src/item.pas b/src/item.pas index 0551723..f49d4d3 100644 --- a/src/item.pas +++ b/src/item.pas @@ -20,7 +20,8 @@ uses scan, diag, sym, chunk, gen; type - TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone, mCond); + TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone, mCond, + mWords); { two results of a call, on the stack, the first under the second } TItem = record mode: TMode; @@ -383,6 +384,7 @@ begin if x.mode = mCond then begin y := x; Value(y); Drop; exit end; if (x.mode = mStack) or ((x.mode = mVar) and (x.base = bStack)) then Drop; + if x.mode = mWords then begin Drop; Drop end; end; { Frames } @@ -408,10 +410,17 @@ { Copying } procedure CopyToTemp(var x: TItem); -var t: int64; +var t: int64; f, g: PObj; begin if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); t := AllocTemp(x.typ^.size, x.typ^.align); + if x.mode = mWords then begin + f := x.typ^.fields; g := f^.next; + PopVar(FrameAdr(t + g^.val), ScalarSize(g^.typ)); + PopVar(FrameAdr(t + f^.val), ScalarSize(f^.typ)); + TempItem(x, x.typ, t); + exit; + end; Address(x); CopyTo(FrameAdr(t), x.typ^.size); TempItem(x, x.typ, t); @@ -1144,6 +1153,7 @@ procedure PushWords(var x: TItem); var adr: TAdr; f: PObj; begin + if x.mode = mWords then exit; { there already } if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x); adr := AdrOf(x); if x.typ^.form = fSlice then begin @@ -1223,6 +1233,7 @@ TempItem(x, res, c.ret) else begin x.mode := mStack; x.typ := res; + if res^.tuple then x.mode := mWords; end; end; diff --git a/src/parse.pas b/src/parse.pas index 0ec1fc6..ce093bb 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1097,6 +1097,7 @@ if x.typ^.tuple and not several then begin if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; MarkHelp(x.pos, s + ' returns several results', 'they go to variables: a, b := ' + s + '(...)'); + if x.mode = mWords then CopyToTemp(x); end; end; sLparen: begin Next; Expression(x, want); Expect(sRparen) end; @@ -1542,13 +1543,23 @@ until false; Expect(sBecomes); Designator(y, called); - if not called or not y.typ^.tuple or (y.typ^.len <> n) then + if not called or not y.typ^.tuple or (y.typ^.len <> n) then begin MarkAt(y.pos, 'expected a call with ' + IntStr(n) + ' results'); + if y.mode = mWords then CopyToTemp(y); + end; + { two results on the stack: the first is brought to the top, and each + is popped into its variable in turn, as one value; else each is + read from the temporary they came back in } + if y.mode = mWords then Swap; f := y.typ^.fields; for i := 0 to n - 1 do begin - if not blanks[i] then begin + if y.mode = mWords then begin z := y; - Field(z, f); + z.mode := mStack; z.typ := f^.typ; + end; + if blanks[i] then Discard(z) + else begin + if y.mode <> mWords then begin z := y; Field(z, f) end; CoerceExact(z, d[i].typ, 'several results go to variables of just their types'); Assign(d[i], z); end;