raz / commit a312fba8

commit
a312fba82ee57e8403e7feb7f5264de3be7df364
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
71b16e4f
Take back the push of a left operand when the right needs no code

Op2: if the left operand's push is the last code emitted, with no
jump or line starting after it, and the right is a constant or a frame
or global variable, drop the push and load the right into rcx.

patch · log

3 files changed, +41 -19

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

@@ -11,8 +11,8 @@
1111 | `src/diag.pas` | 243 | 174 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout |
14-| `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder |
15-| `src/gen.pas` | 1,443 | 1,264 | Items: loading, storing, copying, frames, calls |
14+| `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder |
15+| `src/gen.pas` | 1,460 | 1,276 | Items: loading, storing, copying, frames, calls |
1616 | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins |
1717 | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
@@ -30,7 +30,9 @@
3030
3131 The code generator is a stack machine through `rax`. An expression's
3232 value is computed into `rax`; when a second operand has to be computed,
33-the first is pushed, and popped into `rcx` when both are ready. No
33+the first is pushed, and popped into `rax` when both are ready, the
34+second going to `rcx`; if the second needed no code of its own (a
35+constant, or a plain variable), the push is taken back instead. No
3436 register is ever allocated, so there is no register allocator, and
3537 every access the program writes is one instruction, in the order
3638 written. That is also why `VOLATILE` costs the compiler nothing: it
@@ -92,11 +94,8 @@
9294 mov QWORD PTR [rbp-0x10],rsi
9395 mov rax,QWORD PTR [rbp-0x8] ; a
9496 push rax ; kept while b * 2 is computed
95-mov rax,QWORD PTR [rbp-0x10] ; b
96-push rax ; kept while 2 is read
97-pop rax
98-mov ecx,0x2 ; the constant goes straight to rcx
99-imul rax,rcx
97+mov rax,QWORD PTR [rbp-0x10] ; b, its push taken back for the 2
98+shl rax,0x1 ; times 2 is a shift
10099 mov rcx,rax
101100 pop rax ; a again
102101 add rax,rcx
@@ -247,8 +246,12 @@
247246 operand (or, for `and` and `or`, emits the test that skips the right
248247 one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`),
249248 and otherwise puts the left operand in `rax`, the right in `rcx`, and
250-emits the operation. A power of two on the right of `*`, `div` or
251-`mod` becomes shifts (`DivPow2`), since `idiv` is slow. Narrow integers are brought back to their width
249+emits the operation. When the left was the last thing pushed and the
250+right needs no code (a constant, or a variable on the frame or in a
251+chunk), `Unpush` takes the push back, and the right goes straight to
252+`rcx`; not if a jump or a line starts after the push. A power of two
253+on the right of `*`, `div` or `mod` becomes shifts (`DivPow2`), since
254+`idiv` is slow. Narrow integers are brought back to their width
252255 after each operation (`Norm`), which is how `U8` arithmetic wraps.
253256
254257 Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a

src/gen.pas modified +26 -9

@@ -134,6 +134,7 @@
134134 frameTop, frameMax, permTop: int64;
135135 frameFix, zeroFix: longint; { where the frame size is patched in }
136136 retSlot: int64; { holds the hidden result pointer }
137+ pushEnd: longint; { the code just after the last push of rax }
137138 result: PType; { the procedure's result, or nil }
138139 exitL: longint;
139140 { parameters arriving in registers: where each is stored on entry }
@@ -237,6 +238,7 @@
237238 begin
238239 Push(RAX);
239240 inc(depth);
241+ pushEnd := pc;
240242 end;
241243
242244 procedure PopR(r: longint);
@@ -304,16 +306,26 @@
304306 end;
305307 end;
306308
309+procedure LoadVar(var x: TItem; r: longint);
310+begin
311+ if not IsScalar(x.typ) then MarkAt(x.pos, 'not a single value');
312+ Load(r, MemOf(x, r), ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed);
313+end;
314+
315+{ Takes back the push of rax just emitted, if it is the last code, and
316+ neither a jump nor a line starts after it: the value is still in rax. }
317+function Unpush: boolean;
318+begin
319+ Unpush := (pc = pushEnd) and (labelAt <> pc)
320+ and ((numLines = 0) or (lines^[numLines - 1].at < pc));
321+ if Unpush then begin dec(pc); dec(depth); pushEnd := -1 end;
322+end;
323+
307324 procedure Value(var x: TItem);
308-var m: TMem;
309325 begin
310326 case x.mode of
311327 mConst: ConstTo(RAX, x);
312- mVar: begin
313- if not IsScalar(x.typ) then MarkAt(x.pos, 'not a single value');
314- m := MemOf(x, RAX);
315- Load(RAX, m, ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed);
316- end;
328+ mVar: LoadVar(x, RAX);
317329 mReg: ;
318330 mStack: PopR(RAX);
319331 mProc: begin
@@ -892,11 +904,16 @@
892904 k := -1;
893905 if (y.mode = mConst) and (op in [sTimes, sDiv, sMod]) and (t^.form = fInt) then k := Log2(y.val);
894906
895- { x into rax, y into rcx }
907+ { x into rax, y into rcx; a left operand just pushed stays in rax when
908+ the right one needs no code of its own }
896909 if y.mode = mConst then begin
897- if x.mode = mStack then PopR(RAX) else Value(x);
910+ if x.mode <> mStack then Value(x)
911+ else if not Unpush then PopR(RAX);
898912 if k < 1 then ConstTo(RCX, y);
899- end else begin
913+ end else if (x.mode = mStack) and (y.mode = mVar) and (y.base in [bFrame, bGlobal])
914+ and Unpush then
915+ LoadVar(y, RCX)
916+ else begin
900917 Value(y);
901918 MovRR(RCX, RAX);
902919 if x.mode = mConst then ConstTo(RAX, x) else PopR(RAX);

src/x64.pas modified +2 -0

@@ -92,6 +92,7 @@
9292 lines: PLines;
9393 numLines: longint;
9494 curFile: longint; { the file being compiled, for NoteLine }
95+ labelAt: longint; { the last place a jump was made to land }
9596
9697 function NewChunk(sec: TSection; module, name: longint): longint;
9798 procedure BeginChunk(c: longint);
@@ -649,6 +650,7 @@
649650 procedure FixLinkTo(L, target: longint);
650651 var next: longint;
651652 begin
653+ if L <> NoLabel then labelAt := target;
652654 while L <> NoLabel do begin
653655 next := Get4At(L);
654656 Put4At(L, target - (L + 4));