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.
| @@ -11,8 +11,8 @@ | ||
| 11 | 11 | | `src/diag.pas` | 243 | 174 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | |
| 13 | 13 | | `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 | | |
| 16 | 16 | | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | |
| 17 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -30,7 +30,9 @@ | ||
| 30 | 30 | |
| 31 | 31 | The code generator is a stack machine through `rax`. An expression's |
| 32 | 32 | 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 | |
| 34 | 36 | register is ever allocated, so there is no register allocator, and |
| 35 | 37 | every access the program writes is one instruction, in the order |
| 36 | 38 | written. That is also why `VOLATILE` costs the compiler nothing: it |
| @@ -92,11 +94,8 @@ | ||
| 92 | 94 | mov QWORD PTR [rbp-0x10],rsi |
| 93 | 95 | mov rax,QWORD PTR [rbp-0x8] ; a |
| 94 | 96 | 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 | |
| 100 | 99 | mov rcx,rax |
| 101 | 100 | pop rax ; a again |
| 102 | 101 | add rax,rcx |
| @@ -247,8 +246,12 @@ | ||
| 247 | 246 | operand (or, for `and` and `or`, emits the test that skips the right |
| 248 | 247 | one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), |
| 249 | 248 | 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 | |
| 252 | 255 | after each operation (`Norm`), which is how `U8` arithmetic wraps. |
| 253 | 256 | |
| 254 | 257 | Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a |
| @@ -134,6 +134,7 @@ | ||
| 134 | 134 | frameTop, frameMax, permTop: int64; |
| 135 | 135 | frameFix, zeroFix: longint; { where the frame size is patched in } |
| 136 | 136 | retSlot: int64; { holds the hidden result pointer } |
| 137 | + pushEnd: longint; { the code just after the last push of rax } | |
| 137 | 138 | result: PType; { the procedure's result, or nil } |
| 138 | 139 | exitL: longint; |
| 139 | 140 | { parameters arriving in registers: where each is stored on entry } |
| @@ -237,6 +238,7 @@ | ||
| 237 | 238 | begin |
| 238 | 239 | Push(RAX); |
| 239 | 240 | inc(depth); |
| 241 | + pushEnd := pc; | |
| 240 | 242 | end; |
| 241 | 243 | |
| 242 | 244 | procedure PopR(r: longint); |
| @@ -304,16 +306,26 @@ | ||
| 304 | 306 | end; |
| 305 | 307 | end; |
| 306 | 308 | |
| 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 | + | |
| 307 | 324 | procedure Value(var x: TItem); |
| 308 | -var m: TMem; | |
| 309 | 325 | begin |
| 310 | 326 | case x.mode of |
| 311 | 327 | 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); | |
| 317 | 329 | mReg: ; |
| 318 | 330 | mStack: PopR(RAX); |
| 319 | 331 | mProc: begin |
| @@ -892,11 +904,16 @@ | ||
| 892 | 904 | k := -1; |
| 893 | 905 | if (y.mode = mConst) and (op in [sTimes, sDiv, sMod]) and (t^.form = fInt) then k := Log2(y.val); |
| 894 | 906 | |
| 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 } | |
| 896 | 909 | 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); | |
| 898 | 912 | 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 | |
| 900 | 917 | Value(y); |
| 901 | 918 | MovRR(RCX, RAX); |
| 902 | 919 | if x.mode = mConst then ConstTo(RAX, x) else PopR(RAX); |
| @@ -92,6 +92,7 @@ | ||
| 92 | 92 | lines: PLines; |
| 93 | 93 | numLines: longint; |
| 94 | 94 | curFile: longint; { the file being compiled, for NoteLine } |
| 95 | + labelAt: longint; { the last place a jump was made to land } | |
| 95 | 96 | |
| 96 | 97 | function NewChunk(sec: TSection; module, name: longint): longint; |
| 97 | 98 | procedure BeginChunk(c: longint); |
| @@ -649,6 +650,7 @@ | ||
| 649 | 650 | procedure FixLinkTo(L, target: longint); |
| 650 | 651 | var next: longint; |
| 651 | 652 | begin |
| 653 | + if L <> NoLabel then labelAt := target; | |
| 652 | 654 | while L <> NoLabel do begin |
| 653 | 655 | next := Get4At(L); |
| 654 | 656 | Put4At(L, target - (L + 4)); |