raz / commit 69fbd033

commit
69fbd0335cbeaaf00d23bbb579af633cbf1b0ddc
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b14e8145
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.

patch · log

8 files changed, +191 -54

CLAUDE.md modified +4 -2

@@ -129,8 +129,10 @@
129129 - `for`: the step is a constant; the loop never steps past the limit, so
130130 it can't wrap; afterwards the variable holds the last value it took;
131131 the body may assign it. Leave as is.
132-- Several results come back through a hidden pointer; a code procedure
133- returns two in rax and rdx.
132+- Two results that are each a single value of a word at most, not a
133+ real, come back in rax and rdx, and so does a slice, as from a code
134+ procedure (owner's word, v0.7.0); other several results, and records,
135+ through a hidden pointer.
134136 - `NEW(p, a)` requires `a: ^Mem.Allocator` and calls its `alloc` field:
135137 the one library type the compiler knows by name. `Rt.fail` is
136138 found by name too.

conform/run/TwoWords.expect (history) added +8 -0

@@ -0,0 +1,8 @@
1+7 -5
2+F 4
3+T 12
4+5 22
5+1 6
6+llo
7+0 0
8+14 111

conform/run/TwoWords.raz (history) added +61 -0

@@ -0,0 +1,61 @@
1+module TwoWords;
2+
3+(* Two results, and a slice, returned: with deferred statements, passed
4+ on, through a procedure value, run off the end, and with a call in
5+ the second. *)
6+import Out;
7+
8+var log: INT;
9+
10+proc note; begin log := log * 10 + 1; end;
11+
12+proc pair(a, b: I32): (I32, I32);
13+begin
14+ defer note;
15+ return b, a;
16+end;
17+
18+proc mixed(n: INT): (BOOL, I64);
19+begin
20+ if n < 0 then return false, -n; end;
21+ return true, n * 3;
22+end;
23+
24+proc twice(n: INT): INT; begin return 2 * n; end;
25+
26+proc nested(n: INT): (INT, INT);
27+begin
28+ return n, twice(n) + twice(n + 1);
29+end;
30+
31+proc passOn(n: INT): (INT, INT);
32+begin
33+ defer note;
34+ return nested(n);
35+end;
36+
37+proc tail(s: []U8; i: INT): []U8;
38+begin
39+ defer note;
40+ return s[i..LEN(s)];
41+end;
42+
43+proc fallOff(n: INT): (INT, INT);
44+begin
45+ if n > 0 then return 1, 2; end;
46+end;
47+
48+proc main*;
49+var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT);
50+begin
51+ x, y := pair(-5, 7); Out.int(I64(x)); Out.str(" "); Out.int(I64(y)); Out.ln;
52+ b, w := mixed(-4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln;
53+ b, w := mixed(4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln;
54+ p, q := passOn(5); Out.int(p); Out.str(" "); Out.int(q); Out.ln;
55+ h := nested; p, q := h(1); Out.int(p); Out.str(" "); Out.int(q); Out.ln;
56+ Out.str(tail("hello", 2)); Out.ln;
57+ p, q := fallOff(0); Out.int(p); Out.str(" "); Out.int(q); Out.ln;
58+ _, v := nested(3); Out.int(v); Out.str(" "); Out.int(log); Out.ln;
59+end;
60+
61+end TwoWords.

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

@@ -10,12 +10,12 @@
1010 |---|---|---|---|
1111 | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions |
13-| `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout |
13+| `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,633 | 1,280 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17-| `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,913 | 2,579 | Declarations, statements, expressions, built-ins, recovery, warnings |
16+| `src/gen.pas` | 1,641 | 1,287 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17+| `src/item.pas` | 1,467 | 1,223 | Items: types, constants, variables, operators, conversions, calls |
18+| `src/parse.pas` | 2,916 | 2,580 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 197 | 147 | The command line |
@@ -353,9 +353,13 @@
353353 aligns the stack. A slice goes as its two words, in two registers or
354354 both on the stack, as System V passes a struct of two integers, and
355355 the procedure keeps them in its frame. A record
356-passed by value goes as the address of a copy; one returned comes back
357-through a hidden pointer in `rdi`, as C's structs do, and so do several
358-results (`ResultTo`, `CopyResult`).
356+passed by value goes as the address of a copy. A result of two words,
357+a slice or two single values that aren't reals (`InTwoRegs`, in
358+`sym.pas`), comes back in rax and rdx, as System V returns a struct of
359+two integers: `return` leaves both on the stack for `ReturnJump`, and
360+`Call` stores them in a temporary of the caller's. Any other record or
361+several results come back through a hidden pointer in `rdi`, as C's
362+structs do (`BehindPointer`, `ResultTo`, `CopyResult`).
359363
360364 Frames: `BeginProc` gives the parameters their slots; `EnterBody`
361365 writes the prologue, which zeroes the locals in memory (with a few
@@ -444,10 +448,11 @@
444448 conversion, `T(x)`.
445449
446450 Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`,
447-which has gen's `Call` make the call. Each argument is computed into a
448-frame slot of its own. `return f(x);` passing on f's several results
449-copies them, with `ReturnAll`, from the call's temporary to the place
450-its own hidden pointer names.
451+which has gen's `Call` make the call. Each argument is left on gen's
452+stack, a slice as its two words (`PushWords`). `return f(x);` passing
453+on f's several results, with `ReturnAll`, pushes them from the call's
454+temporary if they are two words, or else copies them to the place its
455+own hidden pointer names.
451456
452457 ## parse.pas: the parser
453458
@@ -678,8 +683,9 @@
678683 - **Registers for variables.** Up to five of a procedure's single
679684 values, not reals, whose address is never taken, live in `rbx` and
680685 `r12` to `r15`; the trap report shows them as it shows the others.
681-- **Several results** come back through a hidden pointer to the
682- caller's slots; a code procedure returns two in rax and rdx.
686+- **Several results** that are two words, and a slice, come back in
687+ rax and rdx, as from a code procedure; others through a hidden
688+ pointer to the caller's slots.
683689 - **Reals.** A real constant is held as an F64. An `F32` in a register
684690 is its low 32 bits; the upper ones are left as they fall.
685691 - **How a failure is caught.** A division by zero, a stack overflow

src/gen.pas modified +22 -14

@@ -74,9 +74,8 @@
7474 pair: array[0..63] of boolean; { ... is a slice's first word, the next its second }
7575 target: int64; { the frame slot of a procedure value, if chunk < 0 }
7676 chunk: longint; { else the procedure's chunk }
77- code: boolean; { a code procedure: two results come in rax and rdx }
7877 ext: boolean; { C's: called through the chunk holding its address }
79- ret: int64; { set by Call: the temporary an aggregate result is in }
78+ ret: int64; { set by Call: the temporary a result of more than one word is in }
8079 end;
8180
8281 const
@@ -1192,7 +1191,7 @@
11921191 ni := 0; nf := 0;
11931192 if (res <> nil) and IsAggregate(res) then begin
11941193 c.ret := AllocTemp(res^.size, res^.align);
1195- if not c.code then ni := 1;
1194+ if BehindPointer(res) then ni := 1;
11961195 end;
11971196 { the System V classes: each argument in the next free register of
11981197 its kind, or else on the stack }
@@ -1219,7 +1218,7 @@
12191218 end;
12201219 ni := 0; nf := 0;
12211220 late[4] := -1; late[5] := -1;
1222- if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end;
1221+ if (c.ret <> 0) and BehindPointer(res) then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end;
12231222 { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as
12241223 they may hold arguments still }
12251224 for i := 0 to c.n - 1 do
@@ -1247,10 +1246,14 @@
12471246 CallR(R11);
12481247 end;
12491248 if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad));
1250- if (c.ret <> 0) and c.code then begin
1251- Store(Mem(RBP, c.ret), RAX, 8);
1252- Store(Mem(RBP, c.ret + 8), RDX, 8);
1253- end;
1249+ if (c.ret <> 0) and InTwoRegs(res) then
1250+ if res^.form = fSlice then begin
1251+ Store(Mem(RBP, c.ret), RAX, 8);
1252+ Store(Mem(RBP, c.ret + 8), RDX, 8);
1253+ end else begin
1254+ Store(Mem(RBP, c.ret + res^.fields^.val), RAX, res^.fields^.typ^.size);
1255+ Store(Mem(RBP, c.ret + res^.fields^.next^.val), RDX, res^.fields^.next^.typ^.size);
1256+ end;
12541257 if (res <> nil) and (c.ret = 0) then begin
12551258 r := Target;
12561259 if res^.form = fReal then MovqRX(r, 0)
@@ -1313,7 +1316,7 @@
13131316 procParams := params;
13141317 regMask := 0;
13151318 ni := 0; nf := 0; stackOff := 16;
1316- if (resType <> nil) and IsAggregate(resType) then begin
1319+ if (resType <> nil) and BehindPointer(resType) then begin
13171320 retSlot := AllocPerm(8, 8);
13181321 inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil;
13191322 numIn := 1; ni := 1;
@@ -1469,13 +1472,17 @@
14691472 CopyBytes(size);
14701473 end;
14711474
1472-{ return: the result, a value on top, goes in rax or xmm0; one behind
1473- the hidden pointer goes back with that pointer in rax. }
1475+{ return: the result, a value on top, goes in rax or xmm0; two words,
1476+ the two values on top, in rax and rdx; one behind the hidden pointer
1477+ goes back with that pointer in rax. }
14741478 procedure ReturnJump;
14751479 begin
14761480 if resType <> nil then
1477- if IsAggregate(resType) then Load(RAX, Mem(RBP, retSlot), 8, false)
1478- else begin
1481+ if BehindPointer(resType) then Load(RAX, Mem(RBP, retSlot), 8, false)
1482+ else if InTwoRegs(resType) then begin
1483+ MovRR(RDX, Top); Drop;
1484+ MovRR(RAX, Top); Drop;
1485+ end else begin
14791486 if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top);
14801487 Drop;
14811488 end;
@@ -1491,7 +1498,7 @@
14911498 if (depth <> 0) or (pushed <> 0) then Fatal('internal error: stack depth');
14921499 { running off the end returns zeros }
14931500 if resType <> nil then
1494- if IsAggregate(resType) then begin
1501+ if BehindPointer(resType) then begin
14951502 Load(RDI, Mem(RBP, retSlot), 8, false);
14961503 MovRI(RCX, resType^.size);
14971504 MovRI(RAX, 0);
@@ -1499,6 +1506,7 @@
14991506 Load(RAX, Mem(RBP, retSlot), 8, false);
15001507 end else begin
15011508 MovRI(RAX, 0);
1509+ if InTwoRegs(resType) then MovRI(RDX, 0);
15021510 MovqXR(0, RAX);
15031511 end;
15041512 FixLink(exitL);

src/item.pas modified +39 -16

@@ -1119,9 +1119,9 @@
11191119 begin
11201120 if x.mode = mProc then begin
11211121 c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0;
1122- c.obj := x.obj; c.code := x.obj^.isCode; c.ext := x.obj^.ext;
1122+ c.obj := x.obj; c.ext := x.obj^.ext;
11231123 end else begin
1124- c.obj := nil; c.code := false; c.ext := false;
1124+ c.obj := nil; c.ext := false;
11251125 if x.typ^.form <> fProc then MarkAt(x.pos, 'expected a procedure, found ' + What(x));
11261126 c.sig := x.typ; c.chunk := -1;
11271127 c.target := ToSlot(x);
@@ -1139,8 +1139,29 @@
11391139 Fail;
11401140 end;
11411141
1142+{ Pushes the two words of x, a slice or two results (InTwoRegs), read
1143+ from memory now, so that what comes after can't change them. }
1144+procedure PushWords(var x: TItem);
1145+var adr: TAdr; f: PObj;
1146+begin
1147+ if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x);
1148+ adr := AdrOf(x);
1149+ if x.typ^.form = fSlice then begin
1150+ PushVar(adr, 8, false, kWord);
1151+ inc(adr.off, 8);
1152+ PushVar(adr, 8, false, kWord);
1153+ end else begin
1154+ f := x.typ^.fields;
1155+ while f <> nil do begin
1156+ adr.off := AdrOf(x).off + f^.val;
1157+ PushVar(adr, ScalarSize(f^.typ), Signed(f^.typ), KindOf(f^.typ));
1158+ f := f^.next;
1159+ end;
1160+ end;
1161+end;
1162+
11421163 procedure PassArg(var c: TCall; var a: TItem);
1143-var p: PObj; adr: TAdr; s: int64;
1164+var p: PObj;
11441165 begin
11451166 p := c.param;
11461167 if c.n >= High(c.isF) then MarkAt(a.pos, 'too many arguments');
@@ -1171,16 +1192,11 @@
11711192 if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter');
11721193 Address(a);
11731194 end else if p^.typ^.form = fSlice then begin
1174- { its two words, read now, so that a later argument can't change them }
11751195 Coerce(a, p^.typ);
1176- if (a.mode <> mVar) or a.vol or not (a.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(a);
1177- adr := AdrOf(a);
1178- PushVar(adr, 8, false, kWord);
1196+ PushWords(a);
11791197 c.pair[c.n] := true;
11801198 inc(c.n);
11811199 c.isF[c.n] := false;
1182- inc(adr.off, 8);
1183- PushVar(adr, 8, false, kWord);
11841200 end else if IsAggregate(p^.typ) then begin
11851201 Coerce(a, p^.typ);
11861202 if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a);
@@ -1335,24 +1351,28 @@
13351351 gen.BeginProc(sig, params);
13361352 end;
13371353
1338-{ The result of return x: a value on top, or one copied behind the
1339- hidden pointer }
1354+{ The result of return x: a value on top, a slice's two words on top,
1355+ or one copied behind the hidden pointer }
13401356 procedure ReturnValue(var x: TItem);
13411357 begin
13421358 if result = nil then MarkAt(x.pos, 'this procedure returns nothing');
13431359 Coerce(x, result);
1344- if IsAggregate(result) then begin
1360+ if InTwoRegs(result) then PushWords(x)
1361+ else if IsAggregate(result) then begin
13451362 if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer');
13461363 Address(x);
13471364 CopyResult(0, result^.size);
13481365 end else Value(x);
13491366 end;
13501367
1351-{ One of several results, into its place behind the hidden pointer. }
1368+{ One of several results: of two words, a value left on top for
1369+ ReturnJump to put in rax or rdx; else into its place behind the
1370+ hidden pointer. }
13521371 procedure ReturnField(f: PObj; var x: TItem);
13531372 begin
13541373 Coerce(x, f^.typ);
1355- if IsScalar(f^.typ) then begin
1374+ if InTwoRegs(result) then Value(x)
1375+ else if IsScalar(f^.typ) then begin
13561376 Value(x);
13571377 ResultTo(f^.val, ScalarSize(f^.typ));
13581378 end else begin
@@ -1365,8 +1385,11 @@
13651385 { Several results at once, the call's that return f(x) passes on. }
13661386 procedure ReturnAll(var x: TItem);
13671387 begin
1368- Address(x);
1369- CopyResult(0, result^.size);
1388+ if InTwoRegs(result) then PushWords(x)
1389+ else begin
1390+ Address(x);
1391+ CopyResult(0, result^.size);
1392+ end;
13701393 end;
13711394
13721395 procedure ReturnNone;

src/parse.pas modified +11 -8

@@ -513,7 +513,8 @@
513513 end;
514514
515515 { Several results: a record of them, laid out as C would, and
516- returned through a hidden pointer. }
516+ returned in rax and rdx if they are two words (InTwoRegs), or else
517+ through a hidden pointer. }
517518 function Results: PType;
518519 var t, ft: PType; f, last: PObj; off: int64;
519520 begin
@@ -1599,7 +1600,7 @@
15991600 end;
16001601
16011602 procedure OneStatement;
1602-var x, y: TItem; called, scalar: boolean; m: int64; c: TCall; start: TPos; first: longint;
1603+var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind;
16031604 begin
16041605 start := TokPos;
16051606 first := id;
@@ -1664,13 +1665,15 @@
16641665 Expression(x, curResult);
16651666 ReturnValue(x);
16661667 end;
1667- { the result waits in the frame while the deferred statements run }
1668- scalar := (curResult <> nil) and not IsAggregate(curResult);
1669- if (numDefers > 0) and scalar then begin
1670- if deferSlot = 0 then deferSlot := AllocPerm(8, 8);
1671- PopVar(FrameAdr(deferSlot), 8);
1668+ { the result, one value or two words on top, waits in the frame
1669+ while the deferred statements run }
1670+ if (numDefers > 0) and (curResult <> nil) and not BehindPointer(curResult) then begin
1671+ n := 1; k := KindOf(curResult);
1672+ if InTwoRegs(curResult) then begin n := 2; k := kWord end;
1673+ if deferSlot = 0 then deferSlot := AllocPerm(16, 8);
1674+ for i := n - 1 downto 0 do PopVar(FrameAdr(deferSlot + 8 * i), 8);
16721675 RunDefers(0);
1673- PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult));
1676+ for i := 0 to n - 1 do PushVar(FrameAdr(deferSlot + 8 * i), 8, false, k);
16741677 end else RunDefers(0);
16751678 ReturnJump;
16761679 ends := true;

src/sym.pas modified +27 -1

@@ -43,7 +43,7 @@
4343 variadic: boolean; { fProc: ends in ... }
4444 tuple: boolean; { fRecord: the results of a procedure that has several }
4545 base: PType; { fPtr, fArray, fSlice: what it holds; fProc: result }
46- len: int64; { fArray: elements; fProc: parameters; fVariant: cases }
46+ len: int64; { fArray: elements; fProc: parameters; fVariant: cases; a tuple: results }
4747 fields: PObj; { fRecord: fields; fVariant: cases; fProc: parameters }
4848 scope: longint; { fRecord: the scope of its fields }
4949 fwd: longint; { fPtr whose base isn't declared yet: its name, else -1 }
@@ -156,6 +156,8 @@
156156 function IsInt(t: PType): boolean;
157157 function IsScalar(t: PType): boolean;
158158 function IsAggregate(t: PType): boolean;
159+function InTwoRegs(t: PType): boolean;
160+function BehindPointer(t: PType): boolean;
159161 function Fits(v: int64; t: PType): boolean;
160162 function TypeName(t: PType): shortstring;
161163 function Fingerprint(o: PObj): qword;
@@ -391,6 +393,30 @@
391393 function IsAggregate(t: PType): boolean;
392394 begin
393395 IsAggregate := t^.form in [fArray, fSlice, fRecord, fVariant];
396+end;
397+
398+{ Whether a procedure's result t is two words, which come back in rax
399+ and rdx, as System V returns a struct of two integers: a slice (its
400+ pointer, then its length), or two results that are each a single
401+ value of 8 bytes at most and not a real. }
402+function InTwoRegs(t: PType): boolean;
403+
404+ function OneWord(f: PObj): boolean;
405+ begin
406+ OneWord := IsScalar(f^.typ) and (f^.typ^.form <> fReal) and (f^.typ^.size <= 8);
407+ end;
408+
409+begin
410+ if t^.form = fSlice then exit(true);
411+ InTwoRegs := t^.tuple and (t^.len = 2) and OneWord(t^.fields) and OneWord(t^.fields^.next);
412+end;
413+
414+{ Whether a procedure's result t comes back in the caller's memory, in
415+ a place a hidden pointer, passed first, says: any result that is
416+ neither one value, in rax or xmm0, nor two words, in rax and rdx. }
417+function BehindPointer(t: PType): boolean;
418+begin
419+ BehindPointer := IsAggregate(t) and not InTwoRegs(t);
394420 end;
395421
396422 function Fits(v: int64; t: PType): boolean;