From a312fba82ee57e8403e7feb7f5264de3be7df364 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:50:03 +0100 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index 8b9e5b5..47d5175 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -11,8 +11,8 @@ | `src/diag.pas` | 243 | 174 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout | -| `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder | -| `src/gen.pas` | 1,443 | 1,264 | Items: loading, storing, copying, frames, calls | +| `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | +| `src/gen.pas` | 1,460 | 1,276 | Items: loading, storing, copying, frames, calls | | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | @@ -30,7 +30,9 @@ The code generator is a stack machine through `rax`. An expression's value is computed into `rax`; when a second operand has to be computed, -the first is pushed, and popped into `rcx` when both are ready. No +the first is pushed, and popped into `rax` when both are ready, the +second going to `rcx`; if the second needed no code of its own (a +constant, or a plain variable), the push is taken back instead. No register is ever allocated, so there is no register allocator, and every access the program writes is one instruction, in the order written. That is also why `VOLATILE` costs the compiler nothing: it @@ -92,11 +94,8 @@ mov QWORD PTR [rbp-0x10],rsi mov rax,QWORD PTR [rbp-0x8] ; a push rax ; kept while b * 2 is computed -mov rax,QWORD PTR [rbp-0x10] ; b -push rax ; kept while 2 is read -pop rax -mov ecx,0x2 ; the constant goes straight to rcx -imul rax,rcx +mov rax,QWORD PTR [rbp-0x10] ; b, its push taken back for the 2 +shl rax,0x1 ; times 2 is a shift mov rcx,rax pop rax ; a again add rax,rcx @@ -247,8 +246,12 @@ operand (or, for `and` and `or`, emits the test that skips the right one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), and otherwise puts the left operand in `rax`, the right in `rcx`, and -emits the operation. A power of two on the right of `*`, `div` or -`mod` becomes shifts (`DivPow2`), since `idiv` is slow. Narrow integers are brought back to their width +emits the operation. When the left was the last thing pushed and the +right needs no code (a constant, or a variable on the frame or in a +chunk), `Unpush` takes the push back, and the right goes straight to +`rcx`; not if a jump or a line starts after the push. A power of two +on the right of `*`, `div` or `mod` becomes shifts (`DivPow2`), since +`idiv` is slow. Narrow integers are brought back to their width after each operation (`Norm`), which is how `U8` arithmetic wraps. Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a diff --git a/src/gen.pas b/src/gen.pas index 830b3a9..c1e0ec3 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -134,6 +134,7 @@ frameTop, frameMax, permTop: int64; frameFix, zeroFix: longint; { where the frame size is patched in } retSlot: int64; { holds the hidden result pointer } + pushEnd: longint; { the code just after the last push of rax } result: PType; { the procedure's result, or nil } exitL: longint; { parameters arriving in registers: where each is stored on entry } @@ -237,6 +238,7 @@ begin Push(RAX); inc(depth); + pushEnd := pc; end; procedure PopR(r: longint); @@ -304,16 +306,26 @@ end; end; +procedure LoadVar(var x: TItem; r: longint); +begin + if not IsScalar(x.typ) then MarkAt(x.pos, 'not a single value'); + Load(r, MemOf(x, r), ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed); +end; + +{ Takes back the push of rax just emitted, if it is the last code, and + neither a jump nor a line starts after it: the value is still in rax. } +function Unpush: boolean; +begin + Unpush := (pc = pushEnd) and (labelAt <> pc) + and ((numLines = 0) or (lines^[numLines - 1].at < pc)); + if Unpush then begin dec(pc); dec(depth); pushEnd := -1 end; +end; + procedure Value(var x: TItem); -var m: TMem; begin case x.mode of mConst: ConstTo(RAX, x); - mVar: begin - if not IsScalar(x.typ) then MarkAt(x.pos, 'not a single value'); - m := MemOf(x, RAX); - Load(RAX, m, ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed); - end; + mVar: LoadVar(x, RAX); mReg: ; mStack: PopR(RAX); mProc: begin @@ -892,11 +904,16 @@ k := -1; if (y.mode = mConst) and (op in [sTimes, sDiv, sMod]) and (t^.form = fInt) then k := Log2(y.val); - { x into rax, y into rcx } + { x into rax, y into rcx; a left operand just pushed stays in rax when + the right one needs no code of its own } if y.mode = mConst then begin - if x.mode = mStack then PopR(RAX) else Value(x); + if x.mode <> mStack then Value(x) + else if not Unpush then PopR(RAX); if k < 1 then ConstTo(RCX, y); - end else begin + end else if (x.mode = mStack) and (y.mode = mVar) and (y.base in [bFrame, bGlobal]) + and Unpush then + LoadVar(y, RCX) + else begin Value(y); MovRR(RCX, RAX); if x.mode = mConst then ConstTo(RAX, x) else PopR(RAX); diff --git a/src/x64.pas b/src/x64.pas index 83964ef..9a6e30d 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -92,6 +92,7 @@ lines: PLines; numLines: longint; curFile: longint; { the file being compiled, for NoteLine } + labelAt: longint; { the last place a jump was made to land } function NewChunk(sec: TSection; module, name: longint): longint; procedure BeginChunk(c: longint); @@ -649,6 +650,7 @@ procedure FixLinkTo(L, target: longint); var next: longint; begin + if L <> NoLabel then labelAt := target; while L <> NoLabel do begin next := Get4At(L); Put4At(L, target - (L + 4));