From c126e150268ebf36439d3ec4c7e2743a692478b6 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:23:22 +0100 Subject: [PATCH] Better code from the same one pass Three of Wirth's ways, no register allocator: - A comparison of integers is an mCond item until something wants it: if and while jump on it (cmp, jcc), not on a BOOL made by setcc, movzx and test; and and or carry its jumps as chains (fj, tj), so (a < b) and (c < d) is two compares and two jumps, and not is the other comparison. - An address in the frame or of a chunk is noted (lAdr) as a constant is: a field through it is one operand, and a constant or an address goes straight to its argument register (PopArg, LoadArg), not through a frame slot. - A constant is stored, added or taken away in memory itself (x64's StoreI, AluMI): DEC(n) is sub qword [rbp - 8], 1. The tools and examples: text 557,411 bytes to 518,579 (-7.0%); Pretty over 348k lines 1,663 ms to 1,405 (-15.5%), Doc 488 to 454. JumpKeep and FixKeep go. 8,066 lines of code; bench 1.07M lines/s. --- diff --git a/doc/compiler.md b/doc/compiler.md index 275be04..77bf31b 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of Free Pascal, under 7,900 lines of code. +The compiler is eleven files of Free Pascal, under 8,100 lines of code. This guide walks through them in the order they are best read: from the characters of a source file to the bytes of an executable. It assumes you know the language from `doc/raz.md`, and enough x86-64 to read a @@ -12,9 +12,9 @@ | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | | `src/sym.pas` | 615 | 510 | Types, objects, scopes, layout | | `src/chunk.pas` | 298 | 232 | The compiled program: chunks, fixups, line notes | -| `src/x64.pas` | 1,039 | 870 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,332 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,342 | 1,118 | Items: types, constants, variables, operators, conversions, calls | +| `src/x64.pas` | 1,094 | 916 | The instruction encoder, jumps within a procedure, the assembler | +| `src/gen.pas` | 1,428 | 1,118 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/item.pas` | 1,400 | 1,168 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 2,771 | 2,459 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 243 | 188 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | @@ -43,8 +43,10 @@ On x86-64 the stack lives in registers, as in Wirth's compilers: the value at depth `d` is in register `d`, of `r8` to `r11`, and a constant -is only noted, until something needs it in a register, so that it can -be an instruction's operand. Which register a value goes in is settled +or an address in the frame or of a chunk is only noted, until something +needs it in a register, so that it can be an instruction's operand. A +comparison, too, waits as a condition, so that `if` and `while` jump +on it directly. Which register a value goes in is settled by its depth, 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 never does anything @@ -272,8 +274,8 @@ the conversions between integers and reals, `CompareSeq` (of two slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`; - jumps: `Jump`, `JumpIf` (on a `BOOL`), `JumpCmp` (on two integers), - `JumpKeep` and `FixKeep` for `and` and `or`, `Fix` where the label - is; + `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for + `and` and `or`, `Fix` where the label is, and `JoinJumps`; - checks: `CheckIndex`, `CheckEnum`, and `IndexSlice`, `Slice` and `SlicePtr`, which check as they make their address or slice; - calls (`Call`), frames (`BeginProc`, `EnterBody`, `ReturnJump`, @@ -289,19 +291,22 @@ address first, so that a variable is read or written with one instruction. -The stack: `stack` notes where each value is, a constant (`lConst`), -in its register (`lReg`) or in a frame slot (`lFrame`); `owner` which +The stack: `stack` notes where each value is, a constant (`lConst`), an +address not yet computed (`lAdr`, `AdrMem`'s operand), in its register +(`lReg`) or in a frame slot (`lFrame`); `owner` which value each register holds. The value at depth `d` belongs in `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division and shifts need, and `rsi` and `rdi`, which copying needs, aren't among them. `Claim` gives a value its register, and `Spill`s a deeper one that holds it, when an expression is deeper than the registers; -`Loaded` brings a value into its register, from its constant or its -slot; `Top` is `Loaded` for the value on top, and `Target` the +`Loaded` brings a value into its register, from its constant, address +or slot; `Place` uses a noted address as the operand itself, and +`PopVar` and `IncDec` an operand of memory and a constant, so `DEC(n)` +is `sub qword [rbp - 8], 1`; `Top` is `Loaded` for the value on top, and `Target` the register a new one is computed in. Before a call every value in a register is spilled (`SpillBelow`), since the call may change them, and -so are the values under `and`'s and `or`'s left operand, which both -ways to its label must find in the same place. `pushed` counts what +so are the values under a comparison that jumps out of an expression +(`JumpOut`), which both ways to its label must find in the same place. `pushed` counts what is on the machine's stack, which a call must align: the return address, in a deferred statement. `Op` takes a constant on the right as it is, and a power of two on the right of @@ -329,9 +334,11 @@ own `gen.pas`: `Rt.fail` stops for a nil pointer, a division by zero and a stack overflow as the signals would. -Calls: the arguments are in frame slots by the time `Call` runs, one -each, computed left to right; only then are they loaded into the -registers System V wants, and the stack aligned for the call. A record +Calls: the arguments wait for `Call`, computed left to right: a +constant or a noted address as itself, anything else in a frame slot +(`PopArg`), so that computing a later one can't disturb it; only then +are they loaded into the registers System V wants (`LoadArg`), and the +stack aligned for the call. A record passed by value goes as the address of a copy; one returned comes back through a hidden pointer in `rdi`, as C's structs do, and so do several results (`ResultTo`, `CopyResult`). @@ -353,6 +360,10 @@ - `mVar`: a variable, at `base`, `chunk` and `off`, which `AdrOf` makes a `TAdr` for gen; - `mStack`: a value on gen's stack; +- `mCond`: a condition, as Wirth's compilers keep one: two integers on + gen's stack, compared by `rel` once something wants the result, and + the jumps of `and` and `or` that land where it is false (`fj`) or + true (`tj`); `CondJump` makes it a jump, `Value` a `BOOL`; - `mProc`, `mType`, `mStr`, and a few more the parser needs. An item also carries `pos`, the stretch of source it came from, which @@ -369,8 +380,10 @@ Expressions: `Op1` is called when the parser has seen an operator and its left operand, `Op2` when it has the right one. `Op1` pushes the -left operand, unless it is a constant (or, for `and` and `or`, has gen -jump past the right one). `Op2` checks the types, folds constants +left operand, unless it is a constant; for `and` and `or` it makes the +left one a condition (`MakeCond`) and jumps past the right one, which +`Op2` joins to it: `(a < b) and (c < d)` in an `if` is two compares +and two jumps, as `not` of a condition is the other comparison. `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), and otherwise pushes the right operand and has gen do the operation; a constant on the left goes on after the right, and the two `Swap`. A set's operations are operations on its bits diff --git a/src/gen.pas b/src/gen.pas index 13f1930..4b2cf0f 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -11,12 +11,15 @@ The stack is kept in registers, as in Wirth's compilers: the value at depth d in r8 to r11, register d mod 4, a deeper one's register taken - by spilling the shallower value it held to the frame. A constant is only noted until something + by spilling the shallower value it held to the frame. A constant, or + an address in the frame or of a chunk, is only noted until something needs it in a register, so that it can be an instruction's operand. Calls use the System V convention. Arguments are computed left to right into frame slots, then loaded into registers just before the - call, so that computing one argument can't disturb another. A record, + call, so that computing one argument can't disturb another; a + constant or an address, which nothing can disturb, goes straight to + its register. A record, array, slice or variant passed by value goes as the address of a copy the caller makes; one returned goes through a hidden pointer, as C's structs do, and so do several results. @@ -60,12 +63,18 @@ off: int64; { added to the address } end; + { How an argument waits for its call: in a frame slot, or noted, as a + constant or as the address of the frame or a chunk plus an offset } + TArgHow = (argSlot, argConst, argAdr); + TCall = record sig: PType; param: PObj; { the parameter the next argument is for } obj: PObj; { the procedure called, if it has a name } n: longint; { the arguments } - slots: array[0..63] of int64; { each one's frame slot } + slots: array[0..63] of int64; { each one's frame slot, constant or offset } + how: array[0..63] of TArgHow; + adrChunk: array[0..63] of longint; { argAdr: the chunk, or -1 for the frame } isF: array[0..63] of boolean; { ... goes in an xmm register } target: int64; { the frame slot of a procedure value, if chunk < 0 } chunk: longint; { else the procedure's chunk } @@ -114,12 +123,13 @@ procedure Jump(var L: longint); procedure JumpIf(var L: longint; when: boolean); -procedure JumpKeep(var L: longint; when: boolean); -procedure FixKeep(L: longint); procedure JumpCmp(op: TSym; signed: boolean; var L: longint); +procedure JumpOut(op: TSym; signed: boolean; var L: longint); +procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); procedure JumpBack(top: longint); procedure Fix(L: longint); procedure FixTo(L, top: longint); +function JoinJumps(L1, L2: longint): longint; procedure CheckIndex(n: int64; const p: TPos); procedure CheckEnum(n: int64; name: longint; const p: TPos); @@ -128,6 +138,7 @@ procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); +procedure PopArg(var c: TCall); procedure Call(var c: TCall); function AllocTemp(size, align: int64): int64; @@ -154,12 +165,15 @@ type { where a value on the stack is } TLoc = (lConst, { not loaded: val is the value } + lAdr, { not loaded: the address of chunk, or of the frame + if chunk is -1, plus val } lReg, { in its register } lFrame); { spilled: in the frame slot val } TEntry = record loc: TLoc; val: int64; + chunk: longint; k: TKind; end; @@ -235,6 +249,13 @@ Claim := Regs[r]; end; +{ The memory at the noted address i, plus off } +function AdrMem(i: longint; off: int64): TMem; +begin + if stack[i].chunk < 0 then AdrMem := Mem(RBP, stack[i].val + off) + else AdrMem := MemRip(stack[i].chunk, stack[i].val + off); +end; + { The value i, in its register } function Loaded(i: longint): longint; var r: longint; @@ -242,6 +263,7 @@ if stack[i].loc <> lReg then begin r := Claim(i); if stack[i].loc = lConst then MovRI(r, stack[i].val) + else if stack[i].loc = lAdr then Lea(r, AdrMem(i, 0)) else Load(r, Mem(RBP, stack[i].val), 8, false); stack[i].loc := lReg; end; @@ -260,6 +282,7 @@ stack[depth].k := k; stack[depth].loc := loc; stack[depth].val := v; + stack[depth].chunk := -1; inc(depth); end; @@ -301,6 +324,11 @@ NewEntry(k, lConst, v); end; +function Fits32(v: int64): boolean; +begin + Fits32 := (v >= low(longint)) and (v <= high(longint)); +end; + { The memory a names. An address on the stack is popped, and is in its register still; one in a slot is loaded into r. } function Place(const a: TAdr; r: longint): TMem; @@ -309,7 +337,15 @@ case a.base of bFrame: Place := Mem(RBP, a.off); bGlobal: Place := MemRip(a.chunk, a.off); - bStack: begin ra := Top; Drop; Place := Mem(ra, a.off) end; + bStack: + if stack[depth - 1].loc = lAdr then begin + Place := AdrMem(depth - 1, a.off); + Drop; + end else begin + ra := Top; + Drop; + Place := Mem(ra, a.off) + end; else Load(r, Mem(RBP, a.chunk), 8, false); Place := Mem(r, a.off); @@ -320,6 +356,11 @@ var m: TMem; r: longint; begin if (a.base = bStack) and (a.off = 0) then exit; + if a.base in [bFrame, bGlobal] then begin + NewEntry(kWord, lAdr, a.off); + if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; + exit; + end; if a.base = bStack then begin m := Place(a, -1); r := Target end else begin r := Target; m := Place(a, r) end; Lea(r, m); @@ -349,6 +390,11 @@ procedure PopVar(const a: TAdr; size: longint); var r: longint; v: int64; begin + if TopConst(v) and Fits32(v) then begin + Drop; + StoreI(Place(a, RCX), v, size); + exit; + end; if TopConst(v) then begin r := RAX; MovRI(RAX, v) end else r := Top; Drop; @@ -440,11 +486,6 @@ Log2 := n; end; -function Fits32(v: int64): boolean; -begin - Fits32 := (v >= low(longint)) and (v <= high(longint)); -end; - { r compared with v, through rdx if v needs more than 32 bits } procedure CmpI(r: longint; v: int64); begin @@ -728,8 +769,15 @@ { Pops a step and an integer variable's address, and adds the step to it, or takes it away. } procedure IncDec(size: longint; signed, dec: boolean); -var r, y: longint; +var r, y, op: longint; v: int64; a: TAdr; begin + if TopConst(v) and Fits32(v) then begin + Drop; + a.base := bStack; a.off := 0; + if dec then op := aluSub else op := aluAdd; + AluMI(op, Place(a, RCX), v, size); + exit; + end; r := Operands(y); Drop; Load(RAX, Mem(r, 0), size, signed); @@ -779,6 +827,7 @@ for i := 0 to n - 1 do with stack[depth - n + i] do if loc = lConst then MovRI(SysRegs[i], val) + else if loc = lAdr then Lea(SysRegs[i], AdrMem(depth - n + i, 0)) else Load(SysRegs[i], Mem(RBP, val), 8, false); for i := 1 to n do Drop; x64.Syscall; @@ -808,22 +857,6 @@ if when then Jcc(ccNE, L) else Jcc(ccE, L); end; -{ For and and or: jumps if the BOOL on top is when, keeping it, in its - register, for where the jump goes; drops it if not. The values under - it are spilled first, so that both ways find them in the same place. } -procedure JumpKeep(var L: longint; when: boolean); -begin - SpillBelow(depth - 1); - JumpIf(L, when); -end; - -{ Where JumpKeep went: the BOOL on top is there, as it was there. } -procedure FixKeep(L: longint); -begin - Top; - FixLink(L); -end; - { Pops two integers, and jumps if the left op the right } procedure JumpCmp(op: TSym; signed: boolean; var L: longint); const @@ -835,8 +868,34 @@ else begin r := Operands(y); Alu(aluCmp, r, y) end; Drop; if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L); +end; + +{ JumpCmp out of an expression, past code that may spill: the values + under the two compared are spilled first, so that they are where the + code after the landing place expects them, whichever way it came. } +procedure JumpOut(op: TSym; signed: boolean; var L: longint); +begin + SpillBelow(depth - 2); + JumpCmp(op, signed, L); end; +{ The comparison of the two values on top, unless notOp holds, as a + BOOL; jumps on its false path (fj) and its true one (tj) land as well } +procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); +var r, e: longint; +begin + JumpOut(notOp, signed, fj); + FixLink(tj); + r := Target; + MovRI(r, 1); + e := NoLabel; + Jmp(e); + FixLink(fj); + MovRI(r, 0); + FixLink(e); + Computed(kWord); +end; + procedure JumpBack(top: longint); begin JmpBack(top); @@ -847,6 +906,11 @@ FixLink(L); end; +function JoinJumps(L1, L2: longint): longint; +begin + JoinJumps := JoinLinks(L1, L2); +end; + procedure FixTo(L, top: longint); begin FixLinkTo(L, top); @@ -996,6 +1060,38 @@ { The call c, its arguments already in their frame slots: they go to registers or the stack, the procedure is called, and its result, if any, is pushed on the value stack. } +{ The value on top is argument c.n of c, which waits for the call: a + constant or an address is noted, a real or anything else goes to a + frame slot. } +procedure PopArg(var c: TCall); +var i: longint; +begin + i := depth - 1; + c.how[c.n] := argSlot; + if not c.isF[c.n] and (stack[i].loc = lConst) then c.how[c.n] := argConst + else if stack[i].loc = lAdr then c.how[c.n] := argAdr; + if c.how[c.n] = argSlot then begin + c.slots[c.n] := AllocTemp(8, 8); + Store(Mem(RBP, c.slots[c.n]), Top, 8); + end else begin + c.slots[c.n] := stack[i].val; + c.adrChunk[c.n] := stack[i].chunk; + end; + Drop; +end; + +{ Argument i of c into register r } +procedure LoadArg(const c: TCall; i, r: longint); +begin + case c.how[i] of + argConst: MovRI(r, c.slots[i]); + argAdr: + if c.adrChunk[i] < 0 then Lea(r, Mem(RBP, c.slots[i])) + else Lea(r, MemRip(c.adrChunk[i], c.slots[i])); + else Load(r, Mem(RBP, c.slots[i]), 8, false); + end; +end; + procedure Call(var c: TCall); var res: PType; i, ni, nf, k, pad, r: longint; @@ -1023,7 +1119,7 @@ if pad <> 0 then AluI(aluSub, RSP, 8); for i := c.n - 1 downto 0 do if onStack[i] then begin - Load(RAX, Mem(RBP, c.slots[i]), 8, false); + LoadArg(c, i, RAX); Push(RAX); end; ni := 0; nf := 0; @@ -1031,7 +1127,7 @@ for i := 0 to c.n - 1 do if not onStack[i] then if c.isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end - else begin Load(IntRegs[ni], Mem(RBP, c.slots[i]), 8, false); inc(ni) end; + else begin LoadArg(c, i, IntRegs[ni]); inc(ni) end; { a procedure taking ... is told in al how many xmm registers it got } if c.sig^.variadic then MovRI(RAX, nf); if c.ext then CallM(MemRip(c.chunk, 0)) diff --git a/src/item.pas b/src/item.pas index e3d9302..e34dea2 100644 --- a/src/item.pas +++ b/src/item.pas @@ -20,11 +20,17 @@ uses scan, diag, sym, chunk, gen; type - TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone); + TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone, mCond); TItem = record mode: TMode; typ: PType; + rel: TSym; { mCond: the comparison of the two integers on + the stack, not yet made, as Wirth's compilers + keep a condition } + relSigned: boolean; + fj, tj: longint; { mCond: the jumps taken where it is false, and + where it is true, to land past it } val: int64; { mConst: the value, an F64's bits; mStr: its length } base: TBase; { mVar: what the address is relative to } chunk: longint; { mVar with bGlobal, mStr: the chunk; bSlot: the slot } @@ -110,6 +116,10 @@ implementation +const + { the comparison that holds when op doesn't, for integers } + Negated: array[sEql..sGeq] of TSym = (sNeq, sEql, sGeq, sGtr, sLeq, sLss); + var result: PType; { the result of the procedure being compiled, or nil } @@ -313,6 +323,12 @@ procedure Value(var x: TItem); var f: single; begin + if x.mode = mCond then begin + if (x.fj = NoLabel) and (x.tj = NoLabel) then Compare(x.rel, kWord, x.relSigned) + else CondValue(Negated[x.rel], x.relSigned, x.fj, x.tj); + x.mode := mStack; + exit; + end; if x.typ^.form = fReal then UsesReals(x.pos); NilTest(x); case x.mode of @@ -350,7 +366,9 @@ { x isn't wanted: what it has on the stack is dropped } procedure Discard(const x: TItem); +var y: TItem; begin + if x.mode = mCond then begin y := x; Value(y); Drop; exit end; if (x.mode = mStack) or ((x.mode = mVar) and (x.base = bStack)) then Drop; end; @@ -428,6 +446,7 @@ procedure Coerce(var x: TItem; t: PType); var s: PType; tmp: int64; begin + if x.mode = mCond then Value(x); s := x.typ; if x.mode = mStr then begin if (t^.form = fSlice) and (t^.base = tU8) then begin @@ -625,10 +644,15 @@ end; procedure NotOp(var x: TItem); +var L: longint; begin if IsInt(x.typ) then MarkHelp(x.pos, '"not" needs a BOOL, found ' + What(x), 'the bits of an integer flip with ~'); if x.typ^.form <> fBool then MarkAt(x.pos, '"not" needs a BOOL, found ' + What(x)); if x.mode = mConst then x.val := 1 - x.val + else if x.mode = mCond then begin + x.rel := Negated[x.rel]; + L := x.fj; x.fj := x.tj; x.tj := L; + end else begin Value(x); NotBool end; end; @@ -668,15 +692,34 @@ end; end; +{ x, a BOOL, as a condition: a value compared with 0 if it isn't one } +procedure MakeCond(var x: TItem); +begin + if x.mode = mCond then exit; + Value(x); + PushConst(kWord, 0); + x.mode := mCond; + x.rel := sNeq; + x.relSigned := false; + x.fj := NoLabel; + x.tj := NoLabel; +end; + procedure Op1(op: TSym; var x: TItem; var L: longint); -var tmp: TItem; begin L := NoLabel; if (op in [sAnd, sOr]) and (x.typ^.form = fBool) then begin - tmp := x; - Value(tmp); - if x.mode <> mConst then x := tmp; - JumpKeep(L, op = sOr); + if x.mode = mConst then exit; { folded by Op2 } + MakeCond(x); + if op = sAnd then begin + JumpOut(Negated[x.rel], x.relSigned, x.fj); + Fix(x.tj); + x.tj := NoLabel; + end else begin + JumpOut(x.rel, x.relSigned, x.tj); + Fix(x.fj); + x.fj := NoLabel; + end; end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then SeqSlice(x) else if (op = sPlus) and (x.mode = mStr) then @@ -848,13 +891,18 @@ if xt^.form <> fBool then WrongLogic(op, x); if yt^.form <> fBool then WrongLogic(op, y); if (x.mode = mConst) and (y.mode = mConst) then begin - Fix(L); if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; - exit; + end else if x.mode = mConst then begin + { true and y, false or y: y; else x, y computed and dropped } + if (x.val = 1) = (op = sAnd) then x := y + else Discard(y); + end else begin + MakeCond(y); + if op = sAnd then begin x.fj := JoinJumps(x.fj, y.fj); x.tj := y.tj end + else begin x.tj := JoinJumps(x.tj, y.tj); x.fj := y.fj end; + x.rel := y.rel; + x.relSigned := y.relSigned; end; - Value(y); - FixKeep(L); - x.mode := mStack; x.typ := tBool; exit; end; @@ -964,8 +1012,15 @@ if op in Rel then Compare(op, KindOf(t), false) else gen.Op(op, KindOf(t), 8, false) end else if t^.form = fSet then SetOp(op) - else if op in Rel then - Compare(op, kWord, Signed(t)) + else if op in Rel then begin + x.mode := mCond; + x.rel := op; + x.relSigned := Signed(t); + x.fj := NoLabel; + x.tj := NoLabel; + x.typ := tBool; + exit; + end else if isPtr then begin if t <> tInt then ScaleBy(scale); gen.Op(op, kWord, 8, false); @@ -1087,7 +1142,8 @@ else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)') else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...'); c.isF[c.n] := a.typ^.form = fReal; - c.slots[c.n] := ToSlot(a); + Value(a); + PopArg(c); inc(c.n); exit; end; @@ -1101,19 +1157,15 @@ if not Same(a.typ, p^.typ) then Mismatch(a, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); Address(a); - s := AllocTemp(8, 8); - PopVar(FrameAdr(s), 8); end else if IsAggregate(p^.typ) then begin Coerce(a, p^.typ); if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); Address(a); - s := AllocTemp(8, 8); - PopVar(FrameAdr(s), 8); end else begin Coerce(a, p^.typ); - s := ToSlot(a); + Value(a); end; - c.slots[c.n] := s; + PopArg(c); inc(c.n); c.param := p^.next; end; @@ -1241,6 +1293,12 @@ procedure CondJump(var x: TItem; var L: longint); begin if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x)); + if x.mode = mCond then begin + JumpCmp(Negated[x.rel], x.relSigned, L); + L := JoinJumps(L, x.fj); + Fix(x.tj); + exit; + end; Value(x); JumpIf(L, false); end; diff --git a/src/x64.pas b/src/x64.pas index edd1829..bc7ce70 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -60,6 +60,8 @@ procedure Alu(op, dst, src: longint); procedure AluI(op, r: longint; v: longint); procedure AluM(op, r: longint; const m: TMem); +procedure AluMI(op: longint; m: TMem; v, size: longint); +procedure StoreI(m: TMem; v, size: longint); procedure Imul(dst, src: longint); procedure ImulI(r: longint; v: longint); procedure Shift(op, r: longint); @@ -103,6 +105,7 @@ procedure JccBack(cc, target: longint); procedure FixLink(L: longint); procedure FixLinkTo(L, target: longint); +function JoinLinks(L1, L2: longint): longint; type TConstFn = function: int64; @@ -195,9 +198,9 @@ Put1(reg shl 3 or 5); { mod 00 with r/m 101 is rip + disp32. The displacement counts from the end of the instruction, and the fixup takes it to end - with these four bytes: no rip operand has an immediate after it, - since only the compiler's loads, stores, lea and call make one - (the assembler can't name a chunk). } + with these four bytes: an instruction with an immediate after + them, AluMI's or StoreI's, takes the immediate's size off disp + first (the assembler can't name a chunk). } AddFixup(pc, m.chunk, m.disp); Put4(0); exit; @@ -316,6 +319,44 @@ OpR(0, true, $89, src, dst); end; +{ The immediate of n bytes that ends an instruction } +procedure PutImm(v, n: longint); +begin + if n = 4 then Put4(v) + else begin + Put1(v and $FF); + if n = 2 then Put1((v shr 8) and $FF); + end; +end; + +{ op [m], v, on size bytes; v fits in them, or in 32 bits for 8 } +procedure AluMI(op: longint; m: TMem; v, size: longint); +var n: longint; +begin + if listing then Say(AluNames[op] + ' ' + MN(m, size) + ', ' + IntStr(v)); + if (size = 1) or (v >= -128) and (v <= 127) then n := 1 + else if size = 2 then n := 2 + else n := 4; + if m.base = RIP then dec(m.disp, n); + if size = 1 then OpM(0, false, $80, op, m) + else if n = 1 then OpM(ord(size = 2) * $66, size = 8, $83, op, m) + else OpM(ord(size = 2) * $66, size = 8, $81, op, m); + PutImm(v, n); +end; + +{ mov [m], v, of size bytes; v fits in them, or in 32 bits for 8 } +procedure StoreI(m: TMem; v, size: longint); +var n: longint; +begin + if listing then Say('mov ' + MN(m, size) + ', ' + IntStr(v)); + n := size; + if n > 4 then n := 4; + if m.base = RIP then dec(m.disp, n); + if size = 1 then OpM(0, false, $C6, 0, m) + else OpM(ord(size = 2) * $66, size = 8, $C7, 0, m); + PutImm(v, n); +end; + procedure MovRI(r: longint; v: int64); begin if not listing then @@ -646,6 +687,20 @@ procedure FixLink(L: longint); begin FixLinkTo(L, pc); +end; + +{ The jumps of chains L1 and L2, as one chain: each jump's field holds + the next one's place until it is fixed, the last NoLabel } +function JoinLinks(L1, L2: longint): longint; +var L: longint; +begin + if L2 = NoLabel then exit(L1); + if L1 <> NoLabel then begin + L := L2; + while Get4At(L) <> NoLabel do L := Get4At(L); + Put4At(L, L1); + end; + JoinLinks := L2; end; diff --git a/test/list/Listing.expect b/test/list/Listing.expect index c3e2a80..42f11da 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -31,43 +31,38 @@ 11 xor eax, eax 13 mov [rbp - 8], rax 20 for i := 1 to 3 do - 17 mov rax, 1 - 22 mov [rbp - 8], rax - 26 mov rax, 3 - 31 mov [rbp - 16], rax - 35 mov r8, [rbp - 8] - 39 mov r9, [rbp - 16] - 43 cmp r8, r9 - 46 jg 173 + 17 mov qword [rbp - 8], 1 + 25 mov qword [rbp - 16], 3 + 33 mov r8, [rbp - 8] + 37 mov r9, [rbp - 16] + 41 cmp r8, r9 + 44 jg 160 21 if i # 2 then total := add(total, twice(i)); end; - 52 mov r8, [rbp - 8] - 56 cmp r8, 2 - 60 setne r8b - 64 movzx r8d, r8b - 68 test r8, r8 - 71 je 135 - 77 mov r8, [Listing.total] - 84 mov [rbp - 24], r8 - 88 mov r8, [rbp - 8] - 92 mov [rbp - 32], r8 - 96 mov rdi, [rbp - 32] - 100 call Listing.twice - 105 mov r8, rax - 108 mov [rbp - 40], r8 - 112 mov rdi, [rbp - 24] - 116 mov rsi, [rbp - 40] - 120 call Listing.add - 125 mov r8, rax - 128 mov [Listing.total], r8 - 135 mov r8, [rbp - 16] - 139 mov r9, [rbp - 8] - 143 sub r8, r9 - 146 cmp r8, 1 - 150 jb 173 - 156 mov r8, [rbp - 8] - 160 add r8, 1 - 164 mov [rbp - 8], r8 - 168 jmp 35 + 50 mov r8, [rbp - 8] + 54 cmp r8, 2 + 58 je 122 + 64 mov r8, [Listing.total] + 71 mov [rbp - 24], r8 + 75 mov r8, [rbp - 8] + 79 mov [rbp - 32], r8 + 83 mov rdi, [rbp - 32] + 87 call Listing.twice + 92 mov r8, rax + 95 mov [rbp - 40], r8 + 99 mov rdi, [rbp - 24] + 103 mov rsi, [rbp - 40] + 107 call Listing.add + 112 mov r8, rax + 115 mov [Listing.total], r8 + 122 mov r8, [rbp - 16] + 126 mov r9, [rbp - 8] + 130 sub r8, r9 + 133 cmp r8, 1 + 137 jb 160 + 143 mov r8, [rbp - 8] + 147 add r8, 1 + 151 mov [rbp - 8], r8 + 155 jmp 33 23 end main; - 173 leave - 174 ret + 160 leave + 161 ret