From 69fbd0335cbeaaf00d23bbb579af633cbf1b0ddc Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:53:32 +0100 Subject: [PATCH] Two words of results in rax and rdx v0.7.0 step 2, by the owner's word. A result of two words, a slice or two single values that aren't reals (InTwoRegs), comes back in rax and rdx as System V returns a struct of two integers, not through a hidden pointer: return leaves both on gen's stack for ReturnJump, and Call stores them in the caller's temporary. TCall.code goes, since a code procedure's two results now follow the same rule. PushWords pushes a slice's or a pair's two words, for arguments and results alike. rat: within noise of step 1 (547 ms both); a kernel of calls returning two results and a slice 9% faster (491 -> 447 ms); rat's text -0.1%. conform/run/TwoWords covers defers, passing on, procedure values and running off the end. --- diff --git a/CLAUDE.md b/CLAUDE.md index be893a1..1b7bc1b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -129,8 +129,10 @@ - `for`: the step is a constant; the loop never steps past the limit, so it can't wrap; afterwards the variable holds the last value it took; the body may assign it. Leave as is. -- Several results come back through a hidden pointer; a code procedure - returns two in rax and rdx. +- Two results that are each a single value of a word at most, not a + real, come back in rax and rdx, and so does a slice, as from a code + procedure (owner's word, v0.7.0); other several results, and records, + through a hidden pointer. - `NEW(p, a)` requires `a: ^Mem.Allocator` and calls its `alloc` field: the one library type the compiler knows by name. `Rt.fail` is found by name too. diff --git a/conform/run/TwoWords.expect b/conform/run/TwoWords.expect new file mode 100644 index 0000000..7ce21ed --- /dev/null +++ b/conform/run/TwoWords.expect @@ -0,0 +1,8 @@ +7 -5 +F 4 +T 12 +5 22 +1 6 +llo +0 0 +14 111 diff --git a/conform/run/TwoWords.raz b/conform/run/TwoWords.raz new file mode 100644 index 0000000..17209c3 --- /dev/null +++ b/conform/run/TwoWords.raz @@ -0,0 +1,61 @@ +module TwoWords; + +(* Two results, and a slice, returned: with deferred statements, passed + on, through a procedure value, run off the end, and with a call in + the second. *) +import Out; + +var log: INT; + +proc note; begin log := log * 10 + 1; end; + +proc pair(a, b: I32): (I32, I32); +begin + defer note; + return b, a; +end; + +proc mixed(n: INT): (BOOL, I64); +begin + if n < 0 then return false, -n; end; + return true, n * 3; +end; + +proc twice(n: INT): INT; begin return 2 * n; end; + +proc nested(n: INT): (INT, INT); +begin + return n, twice(n) + twice(n + 1); +end; + +proc passOn(n: INT): (INT, INT); +begin + defer note; + return nested(n); +end; + +proc tail(s: []U8; i: INT): []U8; +begin + defer note; + return s[i..LEN(s)]; +end; + +proc fallOff(n: INT): (INT, INT); +begin + if n > 0 then return 1, 2; end; +end; + +proc main*; +var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, 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; + b, w := mixed(4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln; + p, q := passOn(5); Out.int(p); Out.str(" "); Out.int(q); Out.ln; + h := nested; p, q := h(1); Out.int(p); Out.str(" "); Out.int(q); Out.ln; + 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; +end; + +end TwoWords. diff --git a/doc/compiler.md b/doc/compiler.md index c538d16..f875217 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -10,12 +10,12 @@ |---|---|---|---| | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | -| `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | +| `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,633 | 1,280 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,913 | 2,579 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `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/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 | @@ -353,9 +353,13 @@ 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; one returned comes back -through a hidden pointer in `rdi`, as C's structs do, and so do several -results (`ResultTo`, `CopyResult`). +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 +several results come back through a hidden pointer in `rdi`, as C's +structs do (`BehindPointer`, `ResultTo`, `CopyResult`). Frames: `BeginProc` gives the parameters their slots; `EnterBody` writes the prologue, which zeroes the locals in memory (with a few @@ -444,10 +448,11 @@ conversion, `T(x)`. Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`, -which has gen's `Call` make the call. Each argument is computed into a -frame slot of its own. `return f(x);` passing on f's several results -copies them, with `ReturnAll`, from the call's temporary to the place -its own hidden pointer names. +which has gen's `Call` make the call. Each argument is left on gen's +stack, a slice as its two words (`PushWords`). `return f(x);` passing +on f's several results, with `ReturnAll`, pushes them from the call's +temporary if they are two words, or else copies them to the place its +own hidden pointer names. ## parse.pas: the parser @@ -678,8 +683,9 @@ - **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`; the trap report shows them as it shows the others. -- **Several results** come back through a hidden pointer to the - caller's slots; a code procedure returns two in rax and rdx. +- **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. - **Reals.** A real constant is held as an F64. An `F32` in a register is its low 32 bits; the upper ones are left as they fall. - **How a failure is caught.** A division by zero, a stack overflow diff --git a/src/gen.pas b/src/gen.pas index 89398db..3f7e5c4 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -74,9 +74,8 @@ pair: array[0..63] of boolean; { ... is a slice's first word, the next its second } target: int64; { the frame slot of a procedure value, if chunk < 0 } chunk: longint; { else the procedure's chunk } - code: boolean; { a code procedure: two results come in rax and rdx } ext: boolean; { C's: called through the chunk holding its address } - ret: int64; { set by Call: the temporary an aggregate result is in } + ret: int64; { set by Call: the temporary a result of more than one word is in } end; const @@ -1192,7 +1191,7 @@ ni := 0; nf := 0; if (res <> nil) and IsAggregate(res) then begin c.ret := AllocTemp(res^.size, res^.align); - if not c.code then ni := 1; + if BehindPointer(res) then ni := 1; end; { the System V classes: each argument in the next free register of its kind, or else on the stack } @@ -1219,7 +1218,7 @@ end; ni := 0; nf := 0; late[4] := -1; late[5] := -1; - if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; + if (c.ret <> 0) and BehindPointer(res) then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as they may hold arguments still } for i := 0 to c.n - 1 do @@ -1247,10 +1246,14 @@ CallR(R11); end; if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); - if (c.ret <> 0) and c.code then begin - Store(Mem(RBP, c.ret), RAX, 8); - Store(Mem(RBP, c.ret + 8), RDX, 8); - end; + 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 r := Target; if res^.form = fReal then MovqRX(r, 0) @@ -1313,7 +1316,7 @@ procParams := params; regMask := 0; ni := 0; nf := 0; stackOff := 16; - if (resType <> nil) and IsAggregate(resType) then begin + if (resType <> nil) and BehindPointer(resType) then begin retSlot := AllocPerm(8, 8); inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; numIn := 1; ni := 1; @@ -1469,13 +1472,17 @@ CopyBytes(size); end; -{ return: the result, a value on top, goes in rax or xmm0; one behind - the hidden pointer goes back with that pointer in rax. } +{ return: the result, a value on top, goes in rax or xmm0; two words, + the two values on top, in rax and rdx; one behind the hidden pointer + goes back with that pointer in rax. } procedure ReturnJump; begin if resType <> nil then - if IsAggregate(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) - else begin + 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; + end else begin if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top); Drop; end; @@ -1491,7 +1498,7 @@ if (depth <> 0) or (pushed <> 0) then Fatal('internal error: stack depth'); { running off the end returns zeros } if resType <> nil then - if IsAggregate(resType) then begin + if BehindPointer(resType) then begin Load(RDI, Mem(RBP, retSlot), 8, false); MovRI(RCX, resType^.size); MovRI(RAX, 0); @@ -1499,6 +1506,7 @@ Load(RAX, Mem(RBP, retSlot), 8, false); end else begin MovRI(RAX, 0); + if InTwoRegs(resType) then MovRI(RDX, 0); MovqXR(0, RAX); end; FixLink(exitL); diff --git a/src/item.pas b/src/item.pas index 999949b..0551723 100644 --- a/src/item.pas +++ b/src/item.pas @@ -1119,9 +1119,9 @@ begin if x.mode = mProc then begin c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0; - c.obj := x.obj; c.code := x.obj^.isCode; c.ext := x.obj^.ext; + c.obj := x.obj; c.ext := x.obj^.ext; end else begin - c.obj := nil; c.code := false; c.ext := false; + c.obj := nil; c.ext := false; if x.typ^.form <> fProc then MarkAt(x.pos, 'expected a procedure, found ' + What(x)); c.sig := x.typ; c.chunk := -1; c.target := ToSlot(x); @@ -1139,8 +1139,29 @@ Fail; end; +{ Pushes the two words of x, a slice or two results (InTwoRegs), read + from memory now, so that what comes after can't change them. } +procedure PushWords(var x: TItem); +var adr: TAdr; f: PObj; +begin + 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 + PushVar(adr, 8, false, kWord); + inc(adr.off, 8); + PushVar(adr, 8, false, kWord); + end else begin + f := x.typ^.fields; + while f <> nil do begin + adr.off := AdrOf(x).off + f^.val; + PushVar(adr, ScalarSize(f^.typ), Signed(f^.typ), KindOf(f^.typ)); + f := f^.next; + end; + end; +end; + procedure PassArg(var c: TCall; var a: TItem); -var p: PObj; adr: TAdr; s: int64; +var p: PObj; begin p := c.param; if c.n >= High(c.isF) then MarkAt(a.pos, 'too many arguments'); @@ -1171,16 +1192,11 @@ if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); Address(a); end else if p^.typ^.form = fSlice then begin - { its two words, read now, so that a later argument can't change them } Coerce(a, p^.typ); - if (a.mode <> mVar) or a.vol or not (a.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(a); - adr := AdrOf(a); - PushVar(adr, 8, false, kWord); + PushWords(a); c.pair[c.n] := true; inc(c.n); c.isF[c.n] := false; - inc(adr.off, 8); - PushVar(adr, 8, false, kWord); end else if IsAggregate(p^.typ) then begin Coerce(a, p^.typ); if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); @@ -1335,24 +1351,28 @@ gen.BeginProc(sig, params); end; -{ The result of return x: a value on top, or one copied behind the - hidden pointer } +{ The result of return x: a value on top, a slice's two words on top, + or one copied behind the hidden pointer } procedure ReturnValue(var x: TItem); begin if result = nil then MarkAt(x.pos, 'this procedure returns nothing'); Coerce(x, result); - if IsAggregate(result) then begin + if InTwoRegs(result) then PushWords(x) + else if IsAggregate(result) then begin if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); Address(x); CopyResult(0, result^.size); end else Value(x); end; -{ One of several results, into its place behind the hidden pointer. } +{ One of several results: of two words, a value left on top for + ReturnJump to put in rax or rdx; else into its place behind the + hidden pointer. } procedure ReturnField(f: PObj; var x: TItem); begin Coerce(x, f^.typ); - if IsScalar(f^.typ) then begin + if InTwoRegs(result) then Value(x) + else if IsScalar(f^.typ) then begin Value(x); ResultTo(f^.val, ScalarSize(f^.typ)); end else begin @@ -1365,8 +1385,11 @@ { Several results at once, the call's that return f(x) passes on. } procedure ReturnAll(var x: TItem); begin - Address(x); - CopyResult(0, result^.size); + if InTwoRegs(result) then PushWords(x) + else begin + Address(x); + CopyResult(0, result^.size); + end; end; procedure ReturnNone; diff --git a/src/parse.pas b/src/parse.pas index d11474c..0ec1fc6 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -513,7 +513,8 @@ end; { Several results: a record of them, laid out as C would, and - returned through a hidden pointer. } + returned in rax and rdx if they are two words (InTwoRegs), or else + through a hidden pointer. } function Results: PType; var t, ft: PType; f, last: PObj; off: int64; begin @@ -1599,7 +1600,7 @@ end; procedure OneStatement; -var x, y: TItem; called, scalar: boolean; m: int64; c: TCall; start: TPos; first: longint; +var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind; begin start := TokPos; first := id; @@ -1664,13 +1665,15 @@ Expression(x, curResult); ReturnValue(x); end; - { the result waits in the frame while the deferred statements run } - scalar := (curResult <> nil) and not IsAggregate(curResult); - if (numDefers > 0) and scalar then begin - if deferSlot = 0 then deferSlot := AllocPerm(8, 8); - PopVar(FrameAdr(deferSlot), 8); + { the result, one value or two words on top, waits in the frame + while the deferred statements run } + if (numDefers > 0) and (curResult <> nil) and not BehindPointer(curResult) then begin + n := 1; k := KindOf(curResult); + if InTwoRegs(curResult) then begin n := 2; k := kWord end; + if deferSlot = 0 then deferSlot := AllocPerm(16, 8); + for i := n - 1 downto 0 do PopVar(FrameAdr(deferSlot + 8 * i), 8); RunDefers(0); - PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult)); + for i := 0 to n - 1 do PushVar(FrameAdr(deferSlot + 8 * i), 8, false, k); end else RunDefers(0); ReturnJump; ends := true; diff --git a/src/sym.pas b/src/sym.pas index ec8b27a..0ca538a 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -43,7 +43,7 @@ variadic: boolean; { fProc: ends in ... } tuple: boolean; { fRecord: the results of a procedure that has several } base: PType; { fPtr, fArray, fSlice: what it holds; fProc: result } - len: int64; { fArray: elements; fProc: parameters; fVariant: cases } + len: int64; { fArray: elements; fProc: parameters; fVariant: cases; a tuple: results } fields: PObj; { fRecord: fields; fVariant: cases; fProc: parameters } scope: longint; { fRecord: the scope of its fields } fwd: longint; { fPtr whose base isn't declared yet: its name, else -1 } @@ -156,6 +156,8 @@ function IsInt(t: PType): boolean; function IsScalar(t: PType): boolean; function IsAggregate(t: PType): boolean; +function InTwoRegs(t: PType): boolean; +function BehindPointer(t: PType): boolean; function Fits(v: int64; t: PType): boolean; function TypeName(t: PType): shortstring; function Fingerprint(o: PObj): qword; @@ -391,6 +393,30 @@ function IsAggregate(t: PType): boolean; begin IsAggregate := t^.form in [fArray, fSlice, fRecord, fVariant]; +end; + +{ Whether a procedure's result t is two words, which come back in rax + and rdx, as System V returns a struct of two integers: a slice (its + pointer, then its length), or two results that are each a single + value of 8 bytes at most and not a real. } +function InTwoRegs(t: PType): boolean; + + function OneWord(f: PObj): boolean; + begin + OneWord := IsScalar(f^.typ) and (f^.typ^.form <> fReal) and (f^.typ^.size <= 8); + end; + +begin + if t^.form = fSlice then exit(true); + InTwoRegs := t^.tuple and (t^.len = 2) and OneWord(t^.fields) and OneWord(t^.fields^.next); +end; + +{ Whether a procedure's result t comes back in the caller's memory, in + a place a hidden pointer, passed first, says: any result that is + neither one value, in rax or xmm0, nor two words, in rax and rdx. } +function BehindPointer(t: PType): boolean; +begin + BehindPointer := IsAggregate(t) and not InTwoRegs(t); end; function Fits(v: int64; t: PType): boolean;