raz / commit dd5e5a98

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

patch · log

6 files changed, +116 -74

conform/run/TwoWords.expect (history) modified +2 -0

@@ -6,3 +6,5 @@
66 llo
77 0 0
88 14 111
9+18
10+5 22

conform/run/TwoWords.raz (history) modified +4 -1

@@ -46,7 +46,7 @@
4646 end;
4747
4848 proc main*;
49-var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT);
49+var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT); pv: ^INT;
5050 begin
5151 x, y := pair(-5, 7); Out.int(I64(x)); Out.str(" "); Out.int(I64(y)); Out.ln;
5252 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 @@
5656 Out.str(tail("hello", 2)); Out.ln;
5757 p, q := fallOff(0); Out.int(p); Out.str(" "); Out.int(q); Out.ln;
5858 _, v := nested(3); Out.int(v); Out.str(" "); Out.int(log); Out.ln;
59+ (* in order: the second result is the one left *)
60+ v, v := nested(4); Out.int(v); Out.ln;
61+ pv := ADR(q); pv^, p := nested(5); Out.int(q); Out.str(" "); Out.int(p); Out.ln;
5962 end;
6063
6164 end TwoWords.

doc/compiler.md (history) modified +12 -6

@@ -13,9 +13,9 @@
1313 | `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,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 |
16+| `src/gen.pas` | 1,650 | 1,282 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17+| `src/item.pas` | 1,478 | 1,234 | Items: types, constants, variables, operators, conversions, calls |
18+| `src/parse.pas` | 2,927 | 2,588 | 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 |
@@ -349,15 +349,16 @@
349349 variable in a register only noted, the rest in registers, or spilled
350350 by a call in a later argument, so that it can't disturb them. `Call`
351351 spills what is under them, moves each into the register System V
352-wants (`ArgTo`), `r8` and `r9` last since they may hold others, and
352+wants (`ValueTo`), `r8` and `r9` last since they may hold others, and
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
356356 passed by value goes as the address of a copy. A result of two words,
357357 a slice or two single values that aren't reals (`InTwoRegs`, in
358358 `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
359+two integers: `return` leaves both on the stack for `ReturnJump`.
360+`Call` leaves two results on the stack, and stores a slice in a
361+temporary of the caller's. Any other record or
361362 several results come back through a hidden pointer in `rdi`, as C's
362363 structs do (`BehindPointer`, `ResultTo`, `CopyResult`).
363364
@@ -392,6 +393,11 @@
392393 - `mVar`: a variable, at `base`, `chunk` and `off`, which `AdrOf`
393394 makes a `TAdr` for gen;
394395 - `mStack`: a value on gen's stack;
396+- `mWords`: the two results of a call, as rax and rdx brought them, on
397+ gen's stack, the first under the second. `a, b := f(x)` brings the
398+ first to the top (`Swap`) and pops each into its variable, so that
399+ nothing goes through memory; `return f(x)` leaves them for
400+ `ReturnJump`; anything else makes them a temporary (`CopyToTemp`);
395401 - `mCond`: a condition, as Wirth's compilers keep one: two integers on
396402 gen's stack, compared by `rel` once something wants the result, and
397403 the jumps of `and` and `or` that land where it is false (`fj`) or

src/gen.pas modified +71 -62

@@ -75,7 +75,7 @@
7575 target: int64; { the frame slot of a procedure value, if chunk < 0 }
7676 chunk: longint; { else the procedure's chunk }
7777 ext: boolean; { C's: called through the chunk holding its address }
78- ret: int64; { set by Call: the temporary a result of more than one word is in }
78+ ret: int64; { set by Call: the temporary a slice or a record result is in }
7979 end;
8080
8181 const
@@ -335,8 +335,30 @@
335335 begin
336336 dec(depth);
337337 if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1;
338+end;
339+
340+{ The value i into register r, from wherever it is: a constant or an
341+ address is made there, a variable's register or the value's own
342+ register copied, a spilled value loaded. Unlike Loaded, it leaves the
343+ value where it was, so r is its only new copy. }
344+procedure ValueTo(i, r: longint);
345+begin
346+ case stack[i].loc of
347+ lConst: MovRI(r, stack[i].val);
348+ lHeld: MovRR(r, stack[i].val);
349+ lAdr: Lea(r, AdrMem(i, 0));
350+ lReg: if RegOf(i) <> r then MovRR(r, RegOf(i));
351+ else Load(r, Mem(RBP, stack[i].val), 8, false);
352+ end;
338353 end;
339354
355+{ The value on top, popped into register r }
356+procedure PopTo(r: longint);
357+begin
358+ ValueTo(depth - 1, r);
359+ Drop;
360+end;
361+
340362 { Whether the value on top is a constant, and if so, which }
341363 function TopConst(var v: int64): boolean;
342364 begin
@@ -493,10 +515,8 @@
493515 size bytes to off bytes past the destination. }
494516 procedure CopyBlock(size, off: int64);
495517 begin
496- MovRR(RSI, Top);
497- Drop;
498- MovRR(RDI, Top);
499- Drop;
518+ PopTo(RSI);
519+ PopTo(RDI);
500520 if off <> 0 then Lea(RDI, Mem(RDI, off));
501521 CopyBytes(size);
502522 end;
@@ -504,8 +524,7 @@
504524 { Pops a source's address, and copies size bytes to a. }
505525 procedure CopyTo(const a: TAdr; size: int64);
506526 begin
507- MovRR(RSI, Top);
508- Drop;
527+ PopTo(RSI);
509528 Lea(RDI, Place(a, RDI));
510529 CopyBytes(size);
511530 end;
@@ -796,8 +815,7 @@
796815 procedure ErrorText(tmp: int64);
797816 var L: longint;
798817 begin
799- MovRR(RAX, Top);
800- Drop;
818+ PopTo(RAX);
801819 MovRI(RCX, 0);
802820 L := NoLabel;
803821 TestRR(RAX, RAX);
@@ -861,8 +879,7 @@
861879 var r, n: longint;
862880 begin
863881 if which = bCas then begin n := Top; Drop end;
864- MovRR(RAX, Top);
865- Drop;
882+ PopTo(RAX);
866883 r := Top;
867884 case which of
868885 bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end;
@@ -1059,21 +1076,28 @@
10591076 end;
10601077
10611078 { Pops an index, and the address of the slice at a if it is on the
1062- stack, and pushes the address of its element of size bytes. }
1079+ stack, and pushes the address of its element of size bytes: the
1080+ index, checked against the length, times size, plus the pointer.
1081+ It is worked out in the index's own register, which then holds the
1082+ address; but if the slice's address is on the stack, under the
1083+ index, the element's register is the one that holds that, still
1084+ needed, so the work is done in rax. }
10631085 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
1064-var m, ml: TMem;
1086+var m, ml: TMem; r: longint; inRax: boolean;
10651087 begin
1066- MovRR(RAX, Top);
1067- Drop;
1088+ inRax := a.base = bStack;
1089+ if inRax then begin r := RAX; PopTo(RAX) end
1090+ else begin r := Top; Drop end;
10681091 m := Place(a, RCX);
10691092 ml := m; ml.disp := ml.disp + 8;
10701093 if checked then begin
1071- AluM(aluCmp, RAX, ml);
1072- Check(ccB, 0, p, RAX, ml, 0, -1);
1094+ AluM(aluCmp, r, ml);
1095+ Check(ccB, 0, p, r, ml, 0, -1);
10731096 end;
1074- if size <> 1 then ImulI(RAX, size);
1075- AluM(aluAdd, RAX, m);
1076- MovRR(Target, RAX);
1097+ if size <> 1 then ImulI(r, size);
1098+ AluM(aluAdd, r, m);
1099+ if inRax then MovRR(Target, RAX)
1100+ else Claim(depth); { the index's register again, now for the address }
10771101 Computed(kWord);
10781102 end;
10791103
@@ -1083,10 +1107,8 @@
10831107 procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos);
10841108 var m, ml: TMem;
10851109 begin
1086- MovRR(RDX, Top);
1087- Drop;
1088- MovRR(RCX, Top);
1089- Drop;
1110+ PopTo(RDX);
1111+ PopTo(RCX);
10901112 m := Place(a, RDI);
10911113 ml := m; ml.disp := ml.disp + 8;
10921114 if checked then begin
@@ -1142,12 +1164,9 @@
11421164 bounds can't be known, but it can't run backwards. }
11431165 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
11441166 begin
1145- MovRR(RDX, Top);
1146- Drop;
1147- MovRR(RCX, Top);
1148- Drop;
1149- MovRR(RDI, Top);
1150- Drop;
1167+ PopTo(RDX);
1168+ PopTo(RCX);
1169+ PopTo(RDI);
11511170 if checked then begin
11521171 Alu(aluCmp, RCX, RDX);
11531172 Check(ccLE, 3, p, RCX, NoMem, 0, -1);
@@ -1165,17 +1184,6 @@
11651184 { The call c, its arguments already in their frame slots: they go to
11661185 registers or the stack, the procedure is called, and its result, if
11671186 any, is pushed on the value stack. }
1168-{ Argument value i, as it is on the stack, into register r }
1169-procedure ArgTo(i, r: longint);
1170-begin
1171- case stack[i].loc of
1172- lConst: MovRI(r, stack[i].val);
1173- lHeld: MovRR(r, stack[i].val);
1174- lAdr: Lea(r, AdrMem(i, 0));
1175- lReg: if RegOf(i) <> r then MovRR(r, RegOf(i));
1176- else Load(r, Mem(RBP, stack[i].val), 8, false);
1177- end;
1178-end;
11791187
11801188 procedure Call(var c: TCall);
11811189 var
@@ -1189,7 +1197,7 @@
11891197 res := c.sig^.base;
11901198 c.ret := 0;
11911199 ni := 0; nf := 0;
1192- if (res <> nil) and IsAggregate(res) then begin
1200+ if (res <> nil) and (BehindPointer(res) or (res^.form = fSlice)) then begin
11931201 c.ret := AllocTemp(res^.size, res^.align);
11941202 if BehindPointer(res) then ni := 1;
11951203 end;
@@ -1213,7 +1221,7 @@
12131221 if pad <> 0 then AluI(aluSub, RSP, 8);
12141222 for i := c.n - 1 downto 0 do
12151223 if onStack[i] then begin
1216- ArgTo(first + i, RAX);
1224+ ValueTo(first + i, RAX);
12171225 Push(RAX);
12181226 end;
12191227 ni := 0; nf := 0;
@@ -1225,14 +1233,14 @@
12251233 if not onStack[i] then
12261234 if c.isF[i] then begin
12271235 if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val))
1228- else begin ArgTo(first + i, RAX); MovqXR(nf, RAX) end;
1236+ else begin ValueTo(first + i, RAX); MovqXR(nf, RAX) end;
12291237 inc(nf);
12301238 end else begin
1231- if ni < 4 then ArgTo(first + i, IntRegs[ni]) else late[ni] := first + i;
1239+ if ni < 4 then ValueTo(first + i, IntRegs[ni]) else late[ni] := first + i;
12321240 inc(ni);
12331241 end;
1234- if late[4] >= 0 then ArgTo(late[4], RAX);
1235- if late[5] >= 0 then ArgTo(late[5], R9);
1242+ if late[4] >= 0 then ValueTo(late[4], RAX);
1243+ if late[5] >= 0 then ValueTo(late[5], R9);
12361244 if late[4] >= 0 then MovRR(R8, RAX);
12371245 for i := 1 to c.n do Drop;
12381246 { a procedure taking ... is told in al how many xmm registers it got }
@@ -1246,15 +1254,17 @@
12461254 CallR(R11);
12471255 end;
12481256 if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad));
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;
1257- if (res <> nil) and (c.ret = 0) then begin
1257+ { the result: a slice in rax and rdx goes to the caller's temporary,
1258+ two results on the stack, the first under the second, and a single
1259+ value on top }
1260+ if res = nil then
1261+ else if res^.form = fSlice then begin
1262+ Store(Mem(RBP, c.ret), RAX, 8);
1263+ Store(Mem(RBP, c.ret + 8), RDX, 8);
1264+ end else if InTwoRegs(res) then begin
1265+ MovRR(Target, RAX); Computed(kWord);
1266+ MovRR(Target, RDX); Computed(kWord);
1267+ end else if c.ret = 0 then begin
12581268 r := Target;
12591269 if res^.form = fReal then MovqRX(r, 0)
12601270 else begin
@@ -1465,8 +1475,7 @@
14651475 bytes into it. }
14661476 procedure CopyResult(off, size: int64);
14671477 begin
1468- MovRR(RSI, Top);
1469- Drop;
1478+ PopTo(RSI);
14701479 Load(RDI, Mem(RBP, retSlot), 8, false);
14711480 if off <> 0 then Lea(RDI, Mem(RDI, off));
14721481 CopyBytes(size);
@@ -1480,11 +1489,11 @@
14801489 if resType <> nil then
14811490 if BehindPointer(resType) then Load(RAX, Mem(RBP, retSlot), 8, false)
14821491 else if InTwoRegs(resType) then begin
1483- MovRR(RDX, Top); Drop;
1484- MovRR(RAX, Top); Drop;
1492+ PopTo(RDX);
1493+ PopTo(RAX);
14851494 end else begin
1486- if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top);
1487- Drop;
1495+ if resType^.form = fReal then begin MovqXR(0, Top); Drop end
1496+ else PopTo(RAX);
14881497 end;
14891498 Jmp(exitL);
14901499 end;

src/item.pas modified +13 -2

@@ -20,7 +20,8 @@
2020 uses scan, diag, sym, chunk, gen;
2121
2222 type
23- TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone, mCond);
23+ TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone, mCond,
24+ mWords); { two results of a call, on the stack, the first under the second }
2425
2526 TItem = record
2627 mode: TMode;
@@ -383,6 +384,7 @@
383384 begin
384385 if x.mode = mCond then begin y := x; Value(y); Drop; exit end;
385386 if (x.mode = mStack) or ((x.mode = mVar) and (x.base = bStack)) then Drop;
387+ if x.mode = mWords then begin Drop; Drop end;
386388 end;
387389
388390 { Frames }
@@ -408,10 +410,17 @@
408410 { Copying }
409411
410412 procedure CopyToTemp(var x: TItem);
411-var t: int64;
413+var t: int64; f, g: PObj;
412414 begin
413415 if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer');
414416 t := AllocTemp(x.typ^.size, x.typ^.align);
417+ if x.mode = mWords then begin
418+ f := x.typ^.fields; g := f^.next;
419+ PopVar(FrameAdr(t + g^.val), ScalarSize(g^.typ));
420+ PopVar(FrameAdr(t + f^.val), ScalarSize(f^.typ));
421+ TempItem(x, x.typ, t);
422+ exit;
423+ end;
415424 Address(x);
416425 CopyTo(FrameAdr(t), x.typ^.size);
417426 TempItem(x, x.typ, t);
@@ -1144,6 +1153,7 @@
11441153 procedure PushWords(var x: TItem);
11451154 var adr: TAdr; f: PObj;
11461155 begin
1156+ if x.mode = mWords then exit; { there already }
11471157 if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x);
11481158 adr := AdrOf(x);
11491159 if x.typ^.form = fSlice then begin
@@ -1223,6 +1233,7 @@
12231233 TempItem(x, res, c.ret)
12241234 else begin
12251235 x.mode := mStack; x.typ := res;
1236+ if res^.tuple then x.mode := mWords;
12261237 end;
12271238 end;
12281239

src/parse.pas modified +14 -3

@@ -1097,6 +1097,7 @@
10971097 if x.typ^.tuple and not several then begin
10981098 if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure';
10991099 MarkHelp(x.pos, s + ' returns several results', 'they go to variables: a, b := ' + s + '(...)');
1100+ if x.mode = mWords then CopyToTemp(x);
11001101 end;
11011102 end;
11021103 sLparen: begin Next; Expression(x, want); Expect(sRparen) end;
@@ -1542,13 +1543,23 @@
15421543 until false;
15431544 Expect(sBecomes);
15441545 Designator(y, called);
1545- if not called or not y.typ^.tuple or (y.typ^.len <> n) then
1546+ if not called or not y.typ^.tuple or (y.typ^.len <> n) then begin
15461547 MarkAt(y.pos, 'expected a call with ' + IntStr(n) + ' results');
1548+ if y.mode = mWords then CopyToTemp(y);
1549+ end;
1550+ { two results on the stack: the first is brought to the top, and each
1551+ is popped into its variable in turn, as one value; else each is
1552+ read from the temporary they came back in }
1553+ if y.mode = mWords then Swap;
15471554 f := y.typ^.fields;
15481555 for i := 0 to n - 1 do begin
1549- if not blanks[i] then begin
1556+ if y.mode = mWords then begin
15501557 z := y;
1551- Field(z, f);
1558+ z.mode := mStack; z.typ := f^.typ;
1559+ end;
1560+ if blanks[i] then Discard(z)
1561+ else begin
1562+ if y.mode <> mWords then begin z := y; Field(z, f) end;
15521563 CoerceExact(z, d[i].typ, 'several results go to variables of just their types');
15531564 Assign(d[i], z);
15541565 end;