raz / commit ff8675c8

commit
ff8675c833ca4fcd8cd9cb5c5a55f01d404a1d60
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
969b2572
Comments a beginner can follow

v0.7.0 step 7, second part: the fresh reader's review, comments and
compiler.md only (and parentheses in BeginProc that change nothing).
gen.pas's header says what a slice is, what spill, System V and
callee-saved mean, and which results come back in rax and rdx; Check
lists every kind of failure and the registers Rt.fail gets; Drop's
register keeping its value is said once; the magic numbers of div,
idiv, neg, not and the SSE operations are named where used; DivPow2,
the real compares, Call's r8 and r9, the stack's alignment, the
prologue, the entry point and the fail stubs are walked through.
ForNext's comment is the report's rule, then each case. compiler.md
stops saying the value at depth d is in register d, and lists lHeld.

Left unexplained: EndProc's frame of at least 16 bytes, whose reason
neither the code nor its history gives; failStubs's slot 5, which no
Check uses.

patch · log

6 files changed, +284 -140

doc/compiler.md (history) modified +44 -29

@@ -10,12 +10,12 @@
1010 |---|---|---|---|
1111 | `src/diag.pas` | 316 | 233 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions |
13-| `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout |
13+| `src/sym.pas` | 644 | 527 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
15-| `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,666 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17-| `src/item.pas` | 1,536 | 1,278 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,967 | 2,612 | Declarations, statements, expressions, built-ins, recovery, warnings |
15+| `src/x64.pas` | 1,128 | 936 | The instruction encoder, jumps within a procedure, the assembler |
16+| `src/gen.pas` | 1,768 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17+| `src/item.pas` | 1,551 | 1,278 | Items: types, constants, variables, operators, conversions, calls |
18+| `src/parse.pas` | 2,975 | 2,612 | 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 |
@@ -41,18 +41,23 @@
4141 another machine is a new `gen.pas`, with an encoder of its own in place
4242 of `x64.pas`.
4343
44-On x86-64 the stack lives in registers, as in Wirth's compilers: the
45-value at depth `d` is in register `d`, of `r8` to `r11`, and a constant
46-or an address in the frame or of a chunk is only noted, until something
47-needs it in a register, so that it can be an instruction's operand. A
44+On x86-64 the stack lives in registers, as in Wirth's compilers
45+(Niklaus Wirth's Pascal and Oberon compilers): the value at depth `d`,
46+depth 0 being the bottom, is in register `d mod 4`, of `r8` to `r11`.
47+A value that needs a register a lower one holds *spills* it: stores it
48+in a frame slot, to free the register. A constant or an address in the
49+frame or of a chunk is only noted, until something needs it in a
50+register, so that it can be an instruction's operand. A
4851 comparison, too, waits as a condition, so that `if` and `while` jump
4952 on it directly. Which register a value goes in is settled
5053 by its depth, so there is no register allocator for values, and every
5154 access the program writes is one instruction, in the order written.
5255 That is also why `VOLATILE` costs the compiler nothing: it never does
5356 anything clever to begin with. A procedure's busiest variables, up to
54-five, live in the registers that calls leave alone (`rbx` and `r12` to
55-`r15`), where nothing but their own assignments can change them.
57+five, live in registers that calls leave alone: the callee-saved ones
58+of System V, the calling convention of x86-64 Linux, which a called
59+procedure must give back as it found them (`rbx` and `r12` to `r15`).
60+Nothing but their own assignments can change them.
5661
5762 The output is a set of *chunks*: one for each procedure, each module
5863 variable, each string. Code refers to other chunks through *fixups*,
@@ -432,23 +437,28 @@
432437 instruction.
433438
434439 The stack: `stack` notes where each value is, a constant (`lConst`), an
435-address not yet computed (`lAdr`, `AdrMem`'s operand), in its register
436-(`lReg`) or in a frame slot (`lFrame`); `owner` which
440+address not yet computed (`lAdr`, `AdrMem`'s operand), a register
441+variable's own register, read in place (`lHeld`), the value's own
442+register (`lReg`) or a frame slot (`lFrame`); `owner` says which
437443 value each register holds. The value at depth `d` belongs in
438444 `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division
439445 and shifts need, and `rsi` and `rdi`, which copying needs, aren't among
440-them. `Claim` gives a value its register, and `Spill`s a deeper one
446+them. `Claim` gives a value its register, and `Spill`s the lower one
441447 that holds it, when an expression is deeper than the registers;
442448 `Loaded` brings a value into its register, from its constant, address
443-or slot; `Place` uses a noted address as the operand itself, and
444-`PopVar` and `IncDec` an operand of memory and a constant, so `DEC(n)`
445-is `sub qword [rbp - 8], 1`; `Top` is `Loaded` for the value on top, and `Target` the
446-register a new one is computed in. Before a call every value in a
447-register is spilled (`SpillBelow`), since the call may change them, and
448-so are the values under a comparison that jumps out of an expression
449-(`JumpOut`), which both ways to its label must find in the same place. `pushed` counts what
450-is on the machine's stack, which a call must align: the return
451-address, in a deferred statement. `Op` takes a
449+or slot; `Place` uses a noted address as the operand itself, so that
450+`PopVar`, storing a constant, and `IncDec` are one instruction on
451+memory: `DEC(n)` is `sub qword [rbp - 8], 1`. `Top` is `Loaded` for the
452+value on top, and `Target` the register a new one is computed in.
453+`Drop` emits no code, and the dropped value's register still holds it
454+until the next push, which several routines rely on. Before a call every
455+value in a register is spilled (`SpillBelow`), since the call may change
456+them, and so are the values under a comparison that jumps out of an
457+expression (`JumpOut`), which both ways to its label must find in the
458+same place. `pushed` counts the quadwords on the machine's stack. A
459+call needs `rsp` a multiple of 16, so `Call` pads by one quadword when
460+`pushed` plus its stack arguments is odd; in a deferred statement
461+`pushed` starts at 1, for the return address. `Op` takes a
452462 constant on the right as it is, after moving one on the left of `+`,
453463 `*`, `&`, `|` or `xor` there, and a power of two on the right of `*`,
454464 `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow; an
@@ -497,9 +507,12 @@
497507 `sym.pas`), comes back in rax and rdx, as System V returns a struct of
498508 two integers: `return` leaves both on the stack for `ReturnJump`.
499509 `Call` leaves two results on the stack, and stores a slice in a
500-temporary of the caller's. Any other record or
501-several results come back through a hidden pointer in `rdi`, as C's
502-structs do (`BehindPointer`, `ResultTo`, `CopyResult`).
510+temporary of the caller's. Any other record, or
511+several results, come back in memory the caller provides, through a
512+hidden pointer in `rdi` (`BehindPointer`, `ResultTo`, `CopyResult`). C
513+returns a struct of up to 16 bytes in `rax` and `rdx`; raz sends every
514+record through memory, and only a slice or two single values in
515+registers.
503516
504517 Frames: `BeginProc` gives the parameters their slots; `EnterBody`
505518 writes the prologue, which zeroes the locals in memory (with a few
@@ -515,8 +528,9 @@
515528 in a register has no slot of its own. Gen saves the registers in slots
516529 a quadword each, after the locals and not zeroed, as each is written
517530 first. The
518-prologue saves them, zeroes them, and takes a parameter's value from
519-where it came; the epilogue restores them. A variable in one is an
531+prologue saves them, zeroes them, and moves a parameter into its
532+register from the register or stack slot it arrived in; the epilogue
533+restores them. A variable in one is an
520534 item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it,
521535 `Peek` reads it in place where the value is only read, so `i < n` is
522536 `cmp rbx, r12`, and `PopVar` and `IncDecReg` write it. Rt finds the
@@ -708,7 +722,8 @@
708722 **Register variables.** One pass can't know whether a variable's
709723 address will be taken before its code is made, so `ChooseRegs` reads
710724 the body ahead, as `InstancesFor` does, and counts each variable's
711-uses: 1 for each, 4 in a loop, 16 in two; one `ADR` takes is out. The
725+uses: 1 for each, 4 in a loop, 16 in two, at most 64 (loops nested three
726+deep or more); one `ADR` takes is out. The
712727 five busiest used at least three times get registers (`o^.reg`). An
713728 address wanted after all, by a `var` parameter, say, comes to
714729 `Address` (`item.pas`), which puts the variable back in memory and

src/gen.pas modified +185 -83

@@ -9,19 +9,30 @@
99 code for those calls, and nothing above it knows the machine: a port
1010 is a new gen.pas, with an encoder of its own.
1111
12- The stack is kept in registers, as in Wirth's compilers: the value at
13- depth d in r8 to r11, register d mod 4, a deeper one's register taken
14- by spilling the shallower value it held to the frame. A constant, or
15- an address in the frame or of a chunk, is only noted until something
16- needs it in a register, so that it can be an instruction's operand.
12+ The stack is kept in registers, as in Wirth's compilers (Niklaus
13+ Wirth's Pascal and Oberon compilers): the value at depth d, depth 0
14+ being the bottom, is in r8 to r11, register d mod 4. A value that
15+ needs a register a lower one holds spills that one: stores it in a
16+ frame slot, to free the register. A constant, or an address in the
17+ frame or of a chunk, is only noted until something needs it in a
18+ register, so that it can be an instruction's operand.
1719
18- Calls use the System V convention. Arguments are computed left to
19- right onto the stack, and moved into their registers just before the
20- call: a call in a later argument spills the earlier ones, as any call
21- spills the values under it, so that it can't disturb them. A slice goes as its two words, as System V passes a
22- 16-byte struct of two integers; a record, array or variant passed by
23- value goes as the address of a copy the caller makes; one returned goes through a hidden pointer, as C's
24- structs do, and so do several results.
20+ A slice is two words, a pointer and then a length.
21+
22+ Calls use the System V convention, which x86-64 Linux has for C:
23+ arguments in rdi, rsi, rdx, rcx, r8, r9 and xmm0 to xmm7, the rest on
24+ the machine's stack; rbx, rbp and r12 to r15 are callee-saved, which
25+ is to say a called procedure gives them back as it found them. The
26+ arguments are computed left to right onto the stack of values, and
27+ moved into their registers just before the call: a call in a later
28+ argument spills the earlier ones, as any call spills the values under
29+ it, so that it can't disturb them. A slice goes as its two words, as
30+ System V passes a 16-byte struct of two integers; a record, array or
31+ variant passed by value goes as the address of a copy the caller
32+ makes. A slice, or two results that are single values, come back in
33+ rax and rdx (InTwoRegs); any other record, or several results, comes
34+ back in memory the caller provides, through a hidden pointer
35+ (BehindPointer), where C would send a small struct back in registers.
2536
2637 A procedure's frame holds its parameters, its variables, and the
2738 temporaries of the statement running. The variables are zeroed on
@@ -80,7 +91,7 @@
8091
8192 const
8293 { the registers a procedure keeps its busiest variables in: System V's
83- callee-saved ones, so they live through calls; a procedure saves
94+ callee-saved ones, so they live through calls (a register variable); a procedure saves
8495 those it uses, and Rt finds them there for the trap report }
8596 VarRegs: array[0..4] of longint = (RBX, R12, R13, R14, R15);
8697
@@ -174,8 +185,9 @@
174185 type
175186 { where a value on the stack is }
176187 TLoc = (lConst, { not loaded: val is the value }
177- lHeld, { not loaded: in register val, a variable's own,
178- which nothing in an expression can change }
188+ lHeld, { not loaded: in register val, a register
189+ variable's own (see VarRegs), read in place, which
190+ nothing in an expression can change }
179191 lAdr, { not loaded: the address of chunk, or of the frame
180192 if chunk is -1, plus val }
181193 lReg, { in its register }
@@ -183,8 +195,9 @@
183195
184196 TEntry = record
185197 loc: TLoc;
186- val: int64;
187- chunk: longint;
198+ val: int64; { lConst: the value; lHeld: the register; lAdr: the
199+ offset; lFrame: the frame slot }
200+ chunk: longint; { lAdr: the chunk, or -1 for the frame }
188201 k: TKind;
189202 end;
190203
@@ -214,7 +227,7 @@
214227 inObj: array[0..13] of PObj;
215228 numIn: longint;
216229 procParams: PObj; { the parameters, for EnterBody }
217- regMask: longint; { the registers of variables, as bits of VarRegs }
230+ regMask: longint; { the registers of variables: bit i is VarRegs[i] }
218231 saveAt: int64; { where they are saved, a quadword each }
219232 frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations }
220233 { the checks of the procedure, whose failures EndFails makes }
@@ -234,10 +247,20 @@
234247 end;
235248
236249 { The stack, as in Wirth's compilers: the value at depth d is held in
237- register d, for as many registers as there are. A value deeper than
238- that, when its register is wanted, and every value in a register
239- when a call would clobber them, is spilled to a frame slot, and loaded
240- again where it is used. A constant is only noted. }
250+ register d mod NumRegs. A value, when a higher one wants its register,
251+ and every value in a register when a call would clobber them, is
252+ spilled to a frame slot, and loaded again where it is used. A constant
253+ is only noted.
254+
255+ Drop emits no code, and a dropped value's register still holds it
256+ until the next push claims it: Place (for bStack), Operands,
257+ IndexSlice and JumpCmp read a register after dropping its value.
258+
259+ Registers outside the stack, free within one operation: rax, rcx and
260+ rdx for arithmetic; rsi and rdi for copying; and, where the stack holds
261+ nothing in them, r10 for the prologue's zeroing loop and r11 for a call
262+ through a procedure value. Reals are kept as their bits in the integer
263+ registers, and moved to xmm0 and xmm1 for each operation (RealOp). }
241264
242265 function RegOf(i: longint): longint;
243266 begin
@@ -263,7 +286,7 @@
263286 if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]);
264287 end;
265288
266-{ The register of value i, for it to hold: a deeper value in it is
289+{ The register of value i, for it to hold: a lower value in it is
267290 spilled. }
268291 function Claim(i: longint): longint;
269292 var r: longint;
@@ -402,21 +425,25 @@
402425 Drop;
403426 Place := Mem(ra, a.off)
404427 end;
405- else
428+ else { bSlot: the address is in the frame slot a.chunk }
406429 Load(r, Mem(RBP, a.chunk), 8, false);
407430 Place := Mem(r, a.off);
408431 end;
409432 end;
410433
434+{ The address a names, on top of the stack }
411435 procedure PushAdr(const a: TAdr);
412436 var m: TMem; r: longint;
413437 begin
414- if (a.base = bStack) and (a.off = 0) then exit;
438+ if (a.base = bStack) and (a.off = 0) then exit; { it is on top already }
415439 if a.base in [bFrame, bGlobal] then begin
416440 NewEntry(kWord, lAdr, a.off);
417441 if a.base = bGlobal then stack[depth - 1].chunk := a.chunk;
418442 exit;
419443 end;
444+ { The order matters. With the address on the stack, Place pops it and
445+ must come first, so that Target may reuse its register; otherwise
446+ Target comes first, since Place may need r to load a slot's pointer. }
420447 if a.base = bStack then begin m := Place(a, -1); r := Target end
421448 else begin r := Target; m := Place(a, r) end;
422449 Lea(r, m);
@@ -430,6 +457,7 @@
430457 NewEntry(k, lHeld, a.chunk);
431458 exit;
432459 end;
460+ { in the order PushAdr explains }
433461 if a.base = bStack then begin m := Place(a, -1); r := Target end
434462 else begin r := Target; m := Place(a, r) end;
435463 Load(r, m, size, signed);
@@ -539,6 +567,7 @@
539567
540568 { Operations }
541569
570+{ n if v is 2^n, which is 0 for 1; -1 if v isn't a power of two }
542571 function ExactLog2(v: int64): longint;
543572 var n: longint;
544573 begin
@@ -569,11 +598,16 @@
569598 zero, so a negative dividend first gets 2^k - 1 added (the bias). }
570599 procedure DivPow2(op: TSym; r, k: longint; unsign: boolean);
571600 begin
601+ { unsigned: div shifts right; mod keeps the low k bits, by shifting
602+ them up to the top and back down }
572603 if unsign then begin
573604 if op = sDiv then ShiftI(shShr, r, k)
574605 else begin ShiftI(shShl, r, 64 - k); ShiftI(shShr, r, 64 - k) end;
575606 exit;
576607 end;
608+ { signed: sar 63 makes the sign into all ones or all zeros, and shr
609+ 64 - k keeps k of them, the bias 2^k - 1 or 0; with it added, sar k
610+ rounds towards zero. The remainder is r less the quotient times 2^k. }
577611 MovRR(RCX, r);
578612 ShiftI(shSar, RCX, 63);
579613 ShiftI(shShr, RCX, 64 - k);
@@ -618,10 +652,10 @@
618652 MovqXR(0, r);
619653 MovqXR(1, y);
620654 case op of
621- sPlus: Sse($58, 0, 1, f);
622- sMinus: Sse($5C, 0, 1, f);
623- sTimes: Sse($59, 0, 1, f);
624- sSlash: Sse($5E, 0, 1, f);
655+ sPlus: Sse($58, 0, 1, f); { addsd, or addss for an F32 }
656+ sMinus: Sse($5C, 0, 1, f); { subsd, subss }
657+ sTimes: Sse($59, 0, 1, f); { mulsd, mulss }
658+ sSlash: Sse($5E, 0, 1, f); { divsd, divss }
625659 end;
626660 MovqRX(r, 0);
627661 end;
@@ -672,10 +706,12 @@
672706 sTimes: Imul(r, y);
673707 sDiv, sMod: begin
674708 MovRR(RAX, r);
675- if not signed then begin MovRI(RDX, 0); Unary(6, y) end
709+ if not signed then begin MovRI(RDX, 0); Unary(6, y) end { div }
676710 else begin
711+ { only -1 can make the division overflow, so a constant
712+ divisor other than -1 needs no check }
677713 if (bits < 64) and (not c or (v = -1)) then DivTrap(bits, y);
678- Cqo; Unary(7, y)
714+ Cqo; Unary(7, y) { idiv, of rdx:rax, which cqo made from rax }
679715 end;
680716 if op = sDiv then MovRR(r, RAX) else MovRR(r, RDX);
681717 end;
@@ -704,6 +740,12 @@
704740 r := Operands(y);
705741 MovqXR(0, r);
706742 MovqXR(1, y);
743+ { ucomis sets the flags as an unsigned comparison would, but for an
744+ unordered pair (a NaN) it sets zero, parity and carry all. So only
745+ "above" can be tested straight: it is false for NaN, as every
746+ comparison but <> must be. a < b is made b > a. = also asks for
747+ no parity, as NaN = NaN sets the zero flag, and <> asks for
748+ parity as well. }
707749 case op of
708750 sEql: begin Ucomis(0, 1, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end;
709751 sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end;
@@ -728,7 +770,7 @@
728770 kF32: Btc(r, 31);
729771 kF64: Btc(r, 63);
730772 else
731- Unary(3, r);
773+ Unary(3, r); { neg }
732774 if size < 8 then Extend(r, size, signed);
733775 end;
734776 end;
@@ -737,7 +779,7 @@
737779 var r: longint;
738780 begin
739781 r := Top;
740- Unary(2, r);
782+ Unary(2, r); { not }
741783 if size < 8 then Extend(r, size, signed);
742784 end;
743785
@@ -775,7 +817,7 @@
775817 AluI(aluAnd, r, 1);
776818 Alu(aluOr, RCX, r);
777819 Cvtsi2s(0, RCX, f);
778- Sse($58, 0, 0, f);
820+ Sse($58, 0, 0, f); { adds it to itself: doubles }
779821 FixLink(L2);
780822 end else Cvtsi2s(0, r, f);
781823 MovqRX(r, 0);
@@ -798,14 +840,15 @@
798840 begin
799841 r := Top;
800842 MovqXR(0, r);
801- Sse($5A, 0, 0, k = kF32);
843+ Sse($5A, 0, 0, k = kF32); { cvtss2sd, or cvtsd2ss }
802844 MovqRX(r, 0);
803845 if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32;
804846 end;
805847
806-{ Whether the slices in the frame at a and b are equal (or not, if not
807- eq): the lengths, then the bytes, len shl shift of them, compared
808- right where the comparison stands. }
848+{ Whether the slices in the frame at a and b (the offsets of their
849+ pointers, the lengths 8 bytes on) are equal (or not, if not eq): the
850+ lengths, then the bytes, len shl shift of them, compared where the
851+ comparison stands. }
809852 procedure CompareSeq(a, b: int64; shift: longint; eq: boolean);
810853 var differ, r: longint;
811854 begin
@@ -817,15 +860,18 @@
817860 Load(RSI, Mem(RBP, b), 8, false);
818861 Load(RDI, Mem(RBP, a), 8, false);
819862 if shift > 0 then ShiftI(shShl, RCX, shift);
820- Alu(aluCmp, RCX, RCX); { equal, if there are no bytes }
863+ { repe cmpsb with rcx = 0 leaves the flags alone, so make them say
864+ equal first, which is right if there are no bytes }
865+ Alu(aluCmp, RCX, RCX);
821866 RepeCmpsb;
822867 FixLink(differ);
823868 if eq then SetCC(ccE, r) else SetCC(ccNE, r);
824869 Computed(kWord);
825870 end;
826871
827-{ An error is the address of its length, followed by its message: pops
828- one, and makes the slice of its message at tmp, empty for nil. }
872+{ An error points at its message's length, an 8-byte word, with the
873+ text after it; nil is no error. Pops one, and makes the slice of its
874+ message at tmp, empty for nil. }
829875 procedure ErrorText(tmp: int64);
830876 var L: longint;
831877 begin
@@ -893,7 +939,7 @@
893939 var r, n: longint;
894940 begin
895941 if which = bCas then begin n := Top; Drop end;
896- PopTo(RAX);
942+ PopTo(RAX); { cmpxchg compares with rax, so CAS's old value goes there }
897943 r := Top;
898944 case which of
899945 bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end;
@@ -903,8 +949,11 @@
903949 stack[depth - 1].k := kWord;
904950 end;
905951
906-{ The system call and its arguments, n values on the stack; every value
907- in a register is spilled first, as the arguments go in its registers }
952+{ The system call and its arguments, n values on the stack. Linux takes
953+ the number in rax and the arguments in rdi, rsi, rdx, r10, r8 and r9,
954+ and the syscall instruction clobbers rcx and r11. Every value in a
955+ register is spilled first, as r8 to r11 are the stack's registers,
956+ and the arguments are loaded from their slots. }
908957 procedure SysCall(n: longint);
909958 const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9);
910959 var i: longint;
@@ -960,15 +1009,19 @@
9601009
9611010 { JumpCmp out of an expression, past code that may spill: the values
9621011 under the two compared are spilled first, so that they are where the
963- code after the landing place expects them, whichever way it came. }
1012+ code after the landing place expects them, whichever way it came. In
1013+ v + (a < b and f(x) = 0), a call to f spills v on the way through, so
1014+ v must be in its slot too where a < b jumps past the call. }
9641015 procedure JumpOut(op: TSym; signed: boolean; var L: longint);
9651016 begin
9661017 SpillBelow(depth - 2);
9671018 JumpCmp(op, signed, L);
9681019 end;
9691020
970-{ The comparison of the two values on top, unless notOp holds, as a
971- BOOL; jumps on its false path (fj) and its true one (tj) land as well }
1021+{ The two values on top, compared, as a BOOL: true unless notOp, the
1022+ opposite comparison, holds. fj and tj are the chains of jumps, made
1023+ earlier by and, or and not, that are taken when the whole condition is
1024+ false and when it is true; they land here, on the 0 and the 1. }
9721025 procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint);
9731026 var r, e: longint;
9741027 begin
@@ -984,6 +1037,10 @@
9841037 Computed(kWord);
9851038 end;
9861039
1040+{ JumpBack, Fix, FixTo and JoinJumps are gen's names for the encoder's,
1041+ so that a port can place jumps its own way. Fix(L) places label L here;
1042+ FixTo(L, top) makes every jump on chain L go to the earlier place top;
1043+ JoinJumps makes one chain of two. }
9871044 procedure JumpBack(top: longint);
9881045 begin
9891046 JmpBack(top);
@@ -1011,14 +1068,18 @@
10111068
10121069 { The program goes on if the condition cc holds, and otherwise stops
10131070 in Rt.fail(what, a, b, len, at, name), which says what failed at p,
1014- found by at, the address of the call: an index a (what = 0), a slice
1015- a..b (1), each for a length len, a position a of the enumeration
1016- called name (2), or a slice of a pointer a..b that runs backwards
1017- (3). a is in register ra, b in RDX; len is in memory at m, or n if
1018- m.base is NoBase. The failing code is out of the way, at the
1071+ found by at, which a stub works out from where it was called; what is
1072+ an index a (0), a slice a..b (1), each for a length len, a position a
1073+ of the enumeration called name (2), a slice of a pointer a..b that
1074+ runs backwards (3), or a nil pointer (4). Rt.fail knows 5 and 6, a
1075+ division fault and a stack overflow, which this machine catches
1076+ itself, so no check here has them. The arguments go in rdi, rsi, rdx,
1077+ rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m,
1078+ or n if m.base is NoBase. The failing code is out of the way, at the
10191079 procedure's end (EndFails), so that a check that holds costs a jump
1020- not taken; it has p for its place, never returns, and may use any
1021- register once it has read what it needs. }
1080+ not taken; it has p for its place (at is p's offset in the source),
1081+ never returns, and may use any register once it has read what it
1082+ needs. }
10221083 procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint);
10231084 begin
10241085 if numFails > High(fails) then Mark('more than ' + IntStr(High(fails) + 1) + ' checks in one procedure');
@@ -1126,6 +1187,8 @@
11261187 m := Place(a, RDI);
11271188 ml := m; ml.disp := ml.disp + 8;
11281189 if checked then begin
1190+ { two checks, both unsigned, so that a negative bound fails too:
1191+ hi against the length, then lo against hi }
11291192 if isArray then begin CmpI(RDX, n); ml := NoMem end
11301193 else AluM(aluCmp, RDX, ml);
11311194 Check(ccBE, 1, p, RCX, ml, n, -1);
@@ -1142,9 +1205,9 @@
11421205 end;
11431206
11441207 { COPY: the elements of the slice in the frame at sOff, of size bytes
1145- each, to the start of the one at dOff, which must be as long, as one
1146- block; backwards when the destination is above the source, so that
1147- an overlap comes out right. }
1208+ each, to the start of the one at dOff, which must be at least as long
1209+ (the source may be shorter), as one block; backwards when the
1210+ destination is above the source, so that an overlap comes out right. }
11481211 procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos);
11491212 var ml: TMem; L, E: longint;
11501213 begin
@@ -1161,7 +1224,9 @@
11611224 Load(RDI, Mem(RBP, dOff), 8, false);
11621225 Alu(aluCmp, RDI, RSI);
11631226 L := NoLabel;
1164- Jcc(ccBE, L);
1227+ Jcc(ccBE, L); { the destination is not above: forwards, at L }
1228+ { backwards: std makes rep movsb run downward, so it starts at the
1229+ last byte of each }
11651230 Lea(RSI, MemIdx(RSI, RCX, 1, -1));
11661231 Lea(RDI, MemIdx(RDI, RCX, 1, -1));
11671232 Std;
@@ -1195,9 +1260,15 @@
11951260
11961261 { Calls }
11971262
1198-{ The call c, its arguments already in their frame slots: they go to
1199- registers or the stack, the procedure is called, and its result, if
1200- any, is pushed on the value stack. }
1263+{ The call c, its c.n arguments the values on top of the stack, in
1264+ registers, noted as constants or spilled. They go to registers or the
1265+ machine's stack, the procedure is called, and its result, if any,
1266+ comes back: one value is pushed, two results are pushed as two values,
1267+ a slice or a record is stored in the temporary c.ret.
1268+
1269+ ni and nf count the integer and the real registers used so far, k the
1270+ arguments on the machine's stack, and pad is 1 when a padding quadword
1271+ is needed. }
12011272
12021273 procedure Call(var c: TCall);
12031274 var
@@ -1229,8 +1300,11 @@
12291300 end;
12301301 if onStack[i] then inc(k);
12311302 end;
1232- { System V wants rsp a multiple of 16 at the call: a quadword of
1233- padding makes the count pushed since the frame began even }
1303+ { System V wants rsp a multiple of 16 at the call. A procedure's body
1304+ has it so (push rbp, and a frame size that is a multiple of 16), so
1305+ a quadword of padding makes the count pushed since even. The stack
1306+ arguments go right to left, so that the first is at the lowest
1307+ address. }
12341308 pad := ord(odd(pushed + k));
12351309 if pad <> 0 then AluI(aluSub, RSP, 8);
12361310 for i := c.n - 1 downto 0 do
@@ -1242,7 +1316,9 @@
12421316 late[4] := -1; late[5] := -1;
12431317 if (c.ret <> 0) and BehindPointer(res) then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end;
12441318 { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as
1245- they may hold arguments still }
1319+ they are stack registers and may hold arguments not yet moved: late
1320+ is the value for each. The fifth goes through rax, so that writing r8
1321+ doesn't destroy a value still wanted for r9. }
12461322 for i := 0 to c.n - 1 do
12471323 if not onStack[i] then
12481324 if c.isF[i] then begin
@@ -1293,6 +1369,9 @@
12931369
12941370 { Frames }
12951371
1372+{ A slot of size bytes in the frame: its offset from rbp, negative, as
1373+ the frame grows down. A temporary is released at the end of its
1374+ statement (TempMark, ReleaseTemps); AllocPerm's lasts the procedure. }
12961375 function AllocTemp(size, align: int64): int64;
12971376 begin
12981377 if align < 1 then align := 1;
@@ -1312,6 +1391,8 @@
13121391 TempMark := frameTop;
13131392 end;
13141393
1394+{ The slots allocated since the mark m are free again, but those
1395+ AllocPerm made stay }
13151396 procedure ReleaseTemps(m: int64);
13161397 begin
13171398 if m < permTop then m := permTop;
@@ -1340,7 +1421,7 @@
13401421 resType := sig^.base;
13411422 procParams := params;
13421423 regMask := 0;
1343- ni := 0; nf := 0; stackOff := 16;
1424+ ni := 0; nf := 0; stackOff := 16; { stackOff: above the saved rbp and the return address }
13441425 if (resType <> nil) and BehindPointer(resType) then begin
13451426 retSlot := AllocPerm(8, 8);
13461427 inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil;
@@ -1348,7 +1429,7 @@
13481429 end;
13491430 p := params;
13501431 while p <> nil do begin
1351- p^.ind := p^.isVar or IsAggregate(p^.typ) and (p^.typ^.form <> fSlice);
1432+ p^.ind := p^.isVar or (IsAggregate(p^.typ) and (p^.typ^.form <> fSlice));
13521433 f := (p^.typ^.form = fReal) and not p^.isVar;
13531434 if (p^.typ^.form = fSlice) and not p^.isVar then begin
13541435 { its two words, in two registers or both on the stack }
@@ -1365,6 +1446,8 @@
13651446 end;
13661447 end else if (f and (nf < 8)) or (not f and (ni < 6)) then begin
13671448 p^.val := AllocPerm(8, 8);
1449+ { a real's register, xmm nf, is told from an integer register by
1450+ a negative number (EnterBody decodes it) }
13681451 if f then begin inReg[numIn] := -1 - nf; inc(nf) end
13691452 else begin inReg[numIn] := IntRegs[ni]; inc(ni) end;
13701453 inSlot[numIn] := p^.val;
@@ -1434,22 +1517,25 @@
14341517 Push(RBP);
14351518 MovRR(RBP, RSP);
14361519 Say('sub rsp, frame');
1437- Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 }
1520+ { sub rsp, imm32: always the form with a 32-bit field, so that EndProc
1521+ can patch the frame's size in }
1522+ Put1($48); Put1($81); Put1($EC);
14381523 frameFix := pc; Put4(0);
14391524 n := (AlignUp(zeroTop, 8) - paramTop) div 8;
14401525 if n > 0 then MovRI(RAX, 0);
14411526 if n <= 4 then
14421527 for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8)
14431528 else begin
1444- { a quadword at a time: r10 counts up to zero }
1529+ { a quadword at a time: r10 runs from -n up to 0, and the store's
1530+ address is scaled by it }
14451531 MovRI(R10, -n);
14461532 top := pc;
14471533 Store(MemIdx(RBP, R10, 8, -paramTop), RAX, 8);
14481534 AluI(aluAdd, R10, 1);
14491535 JccBack(ccNE, top);
14501536 end;
1451- { the registers of variables: saved, as a variable starts at zero,
1452- a parameter taken from where it came }
1537+ { each register that holds a variable is saved, for the caller, and
1538+ zeroed, as a variable starts at zero }
14531539 chunks^[c].saveMask := regMask;
14541540 chunks^[c].saveAt := saveAt;
14551541 n := 0;
@@ -1459,12 +1545,16 @@
14591545 MovRI(VarRegs[i], 0);
14601546 inc(n);
14611547 end;
1548+ { the parameters that came in registers, to their slots or, for a
1549+ register variable, to its register }
14621550 for i := 0 to numIn - 1 do
14631551 if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i])
14641552 else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin
14651553 MovRR(inObj[i]^.reg, inReg[i]);
14661554 if inSize[i] < 8 then Extend(inObj[i]^.reg, inSize[i], inObj[i]^.typ^.signed);
14671555 end else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]);
1556+ { a register variable's parameter that came on the stack, above the
1557+ frame (val > 0), is loaded from there }
14681558 p := procParams;
14691559 while p <> nil do begin
14701560 if (p^.reg <> 0) and (p^.val > 0) then
@@ -1555,7 +1645,8 @@
15551645 end;
15561646
15571647 { A deferred statement is compiled where it stands, as a subroutine of
1558- its procedure that the code jumps over; DeferBegin gives its address
1648+ its procedure that the code jumps over, in the procedure's own frame
1649+ (the same rbp), so that it sees all its variables; DeferBegin gives its address
15591650 and starts it, with the return address pushed. The stack is empty
15601651 where it is called, and where it begins. }
15611652 function DeferBegin(var L, saved: longint): longint;
@@ -1584,7 +1675,13 @@
15841675 main; and ends with Rt.exit, its status main's or 0. A dynamic program
15851676 starts as C's crt1 does, through __libc_start_main(main, argc, argv,
15861677 nil, nil, rtld_fini, stack_end), with rsp aligned; its main first
1587- gives Rt C's exit, in cExit. }
1678+ gives Rt C's exit, in cExit.
1679+
1680+ dynamic: the program uses C. first: the first module, whose chain
1681+ gives the order of the bodies. main: the program's main procedure, or
1682+ nil. cExit, placesVar and exitProc: the chunks of Rt's cExit, places
1683+ and exit. places: the chunk the linker fills with the places (the
1684+ address of each line of code, for Rt to say where a check failed). }
15881685 function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint;
15891686 var entry, start, i: longint; m: PModule;
15901687 begin
@@ -1592,18 +1689,20 @@
15921689 if dynamic then begin
15931690 start := NewChunk(secText, -1, Intern('main', 4));
15941691 BeginChunk(entry);
1595- MovRI(RBP, 0);
1596- MovRR(R9, RDX);
1597- Pop(RSI);
1598- MovRR(RDX, RSP);
1692+ { at the start rdx holds rtld_fini, from the dynamic loader, and the
1693+ stack argc, then argv }
1694+ MovRI(RBP, 0); { no frame above, where a trace stops }
1695+ MovRR(R9, RDX); { rtld_fini, the sixth argument }
1696+ Pop(RSI); { argc, the second }
1697+ MovRR(RDX, RSP); { argv, the third }
15991698 AluI(aluAnd, RSP, -16);
1600- Push(RAX);
1601- Push(RSP);
1602- MovRI(R8, 0);
1699+ Push(RAX); { padding, so that the next push, the seventh }
1700+ Push(RSP); { argument, stack_end, leaves rsp aligned }
1701+ MovRI(R8, 0); { fini and init: none }
16031702 MovRI(RCX, 0);
1604- Lea(RDI, MemRip(start, 0));
1703+ Lea(RDI, MemRip(start, 0)); { main, the first argument }
16051704 CallM(MemRip(ExternChunk(Intern('__libc_start_main', 17)), 0));
1606- Ud2;
1705+ Ud2; { it never returns }
16071706 EndChunk(entry);
16081707 BeginChunk(start);
16091708 Push(RBP); { aligns the stack again }
@@ -1629,10 +1728,13 @@
16291728 if main <> nil then CallChunk(main^.chunk);
16301729 if (main <> nil) and (main^.typ^.base <> nil) then MovRR(RDI, RAX) else MovRI(RDI, 0);
16311730 CallChunk(exitProc);
1632- Ud2;
1731+ Ud2; { exit never returns: trap if it does }
16331732 EndChunk(start);
16341733 { Rt.fail, for the failures of each kind: the place is where the
1635- check's call is, a byte before where it would return to }
1734+ check's call is. The return address is the next instruction's, which
1735+ may belong to another line; one byte back is inside the call, so the
1736+ lookup finds the call's line. rsp is aligned for the call, as the
1737+ check's code may have pushed an odd number of quadwords. }
16361738 for i := 0 to High(failStubs) do
16371739 if failStubs[i] >= 0 then begin
16381740 BeginChunk(failStubs[i]);

src/item.pas modified +26 -11

@@ -30,8 +30,10 @@
3030 the stack, not yet made, as Wirth's compilers
3131 keep a condition }
3232 relSigned: boolean;
33- fj, tj: longint; { mCond: the jumps taken where it is false, and
34- where it is true, to land past it }
33+ fj, tj: longint; { mCond: the chains of jumps already made that are
34+ taken when the whole condition is false, and when
35+ it is true, not yet given a place (and, or and
36+ not make them); see gen's JumpOut }
3537 val: int64; { mConst: the value, an F64's bits; mStr: its length }
3638 base: TBase; { mVar: what the address is relative to }
3739 chunk: longint; { mVar with bGlobal, mStr: the chunk; bSlot: the slot }
@@ -48,7 +50,8 @@
4850 end;
4951
5052 var
51- demoted: boolean; { Address wanted a register variable's address }
53+ demoted: boolean; { Address wanted a register variable's address: its
54+ procedure is compiled again, the variable in memory }
5255 checks: boolean; { the module's index and slice checks are made: it
5356 doesn't say check off }
5457
@@ -412,12 +415,14 @@
412415
413416 { Copying }
414417
418+{ x, copied to a temporary }
415419 procedure CopyToTemp(var x: TItem);
416420 var t: int64; f, g: PObj;
417421 begin
418422 if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer');
419423 t := AllocTemp(x.typ^.size, x.typ^.align);
420424 if x.mode = mWords then begin
425+ { the second result is on top, so it is popped first }
421426 f := x.typ^.fields; g := f^.next;
422427 PopVar(FrameAdr(t + g^.val), ScalarSize(g^.typ));
423428 PopVar(FrameAdr(t + f^.val), ScalarSize(f^.typ));
@@ -1157,6 +1162,8 @@
11571162 var adr: TAdr; f: PObj;
11581163 begin
11591164 if x.mode = mWords then exit; { there already }
1165+ { copied to a temporary unless its two words are in memory at a place
1166+ that a later argument can't change }
11601167 if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x);
11611168 adr := AdrOf(x);
11621169 if x.typ^.form = fSlice then begin
@@ -1223,6 +1230,9 @@
12231230 c.param := p^.next;
12241231 end;
12251232
1233+{ The call c made, with x its result as an item: none; a temporary, for a
1234+ slice or a record; two results on gen's stack (mWords); else one value
1235+ on the stack. }
12261236 procedure EndCall(var c: TCall; var x: TItem);
12271237 var res: PType;
12281238 begin
@@ -1343,15 +1353,17 @@
13431353 JumpCmp(op, o^.typ^.signed, L);
13441354 end;
13451355
1346-{ The end of a round of a for loop, as the report has it for a positive
1347- step s: if v <= b and b - v < s, the loop ends; otherwise v becomes
1356+{ The end of a round of a for loop, for a positive step s, as the report
1357+ has it: if v <= b and b - v < s, the loop ends; otherwise v becomes
13481358 v + s and the loop goes on while v <= b. Counting the distance left,
13491359 rather than comparing after the step, can't be fooled by v wrapping
1350- around. So the loop goes on, to top, where v is stepped, if v < b for
1351- a step of 1, or if v <= b and b - v >= s for another: then v + s <= b
1352- needs no test. It ends if v = b, or b - v < s. Only where the body has
1353- put v past b is it stepped here and tested, past. A negative step is
1354- the same the other way round. }
1360+ around. A negative step is the same the other way round: Before, Past
1361+ and Within are indexed by up, whether the step is positive.
1362+ For a step of 1: v < b goes on to top, where v is stepped; v = b
1363+ ends; otherwise the body moved v past b.
1364+ For another step: v past b goes to past; else b - v (unsigned, as
1365+ v <= b) >= |step| goes on to top, and else the loop ends.
1366+ Past: v is stepped here and tested, going to body if within b. }
13551367 procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint);
13561368 const
13571369 Before: array[boolean] of TSym = (sGtr, sLss); { by a negative step, by a positive }
@@ -1385,6 +1397,8 @@
13851397 FixTo(L, body);
13861398 end;
13871399
1400+{ Jumps to L if the condition x is false. The jumps waiting in x.fj join
1401+ L; those in x.tj are placed here, as x is true past this. }
13881402 procedure CondJump(var x: TItem; var L: longint);
13891403 begin
13901404 if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x));
@@ -1398,7 +1412,8 @@
13981412 JumpIf(L, false);
13991413 end;
14001414
1401-{ As CondJump, but the jump is taken where the condition holds }
1415+{ As CondJump, but the jump to L is taken where the condition is true;
1416+ x.tj joins L and x.fj is placed here }
14021417 procedure CondJumpTrue(var x: TItem; var L: longint);
14031418 begin
14041419 if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x));

src/parse.pas modified +19 -11

@@ -1217,7 +1217,8 @@
12171217 end;
12181218
12191219 { Whether what follows "then" is a lone "break;", with no deferred
1220- statement to run on the way out of the loop }
1220+ statement to run on the way out of the loop: the break must be the
1221+ branch's last statement, so ";" and then end, elsif or else follow. }
12211222 function LoneBreak: boolean;
12221223 var st: TScanState;
12231224 begin
@@ -1238,11 +1239,11 @@
12381239 procedure ThenPart(var x: TItem; var L: longint);
12391240 begin
12401241 Expect(sThen);
1241- L := NoLabel;
1242+ L := NoLabel; { stays so with a lone break: there is nothing to skip }
12421243 if LoneBreak then begin
1243- NoteLine(CurLine, CurCol);
1244+ NoteLine(CurLine, CurCol); { the jump gets the break's line }
12441245 CondJumpTrue(x, curLoop^.brk);
1245- Next; Next; { break; }
1246+ Next; Next; { skip "break" and ";" }
12461247 ends := true;
12471248 end else begin
12481249 CondJump(x, L);
@@ -1250,8 +1251,10 @@
12501251 end;
12511252 end;
12521253
1253-{ A branch that ends, in return or break, needs no jump past the
1254- others. }
1254+{ if, elsif and else: each condition's jump skips its branch to the
1255+ next; each branch that doesn't end jumps to the end. A branch that
1256+ ends, in return or break, needs no jump past the others, and all of
1257+ them ending is told to the caller in the global ends. }
12551258 procedure IfStatement(const start: TPos; ind: longint);
12561259 var x: TItem; L, endL: longint; all: boolean;
12571260 begin
@@ -1548,7 +1551,8 @@
15481551 end;
15491552
15501553 { a, b := f(x): the variables first, left to right, then the call;
1551- then each result to its variable, in order. }
1554+ then each result to its variable, in order. x is the first variable,
1555+ first its name, blank whether it is _. }
15521556 procedure MultiAssign(var x: TItem; first: longint; blank: boolean);
15531557 var
15541558 d: array[0..15] of TItem; dn: array[0..15] of longint; blanks: array[0..15] of boolean;
@@ -1562,7 +1566,7 @@
15621566 if not blanks[n] then begin
15631567 NotReadOnly(d[n]);
15641568 Assignable(d[n], dn[n]);
1565- Pin(d[n]);
1569+ Pin(d[n]); { works out the address now, so that the call can't change it }
15661570 end;
15671571 inc(n);
15681572 if tok <> sComma then break;
@@ -1833,8 +1837,8 @@
18331837
18341838 { Puts the busiest variables of the procedure, whose parameters are
18351839 params and whose locals follow before, in registers: read ahead from
1836- its "begin" to its end, a use counts 1, in a loop 4, in two 16, and
1837- a variable ADR is taken of can't have one. A register costs a save
1840+ its "begin" to its end, a use counts 1, in a loop 4, in two 16, in
1841+ three or more 64, and a variable ADR is taken of can't have one. A register costs a save
18381842 and a restore, so a variable used less than three times keeps its
18391843 place in memory. }
18401844 procedure ChooseRegs(params, before: PObj);
@@ -1882,17 +1886,21 @@
18821886 if id = nameAdr then begin
18831887 Next;
18841888 if tok = sLparen then Next;
1889+ { a hugely negative weight bars it from a register, whatever
1890+ its uses add }
18851891 if (tok = sIdent) and (Candidate(id) >= 0) then weight[Candidate(id)] := low(longint) div 2;
18861892 end else if not afterDot and (Candidate(id) >= 0) then begin
18871893 k := Candidate(id);
18881894 if loops > 2 then inc(weight[k], 64) else inc(weight[k], 1 shl (2 * loops));
18891895 end;
18901896 end;
1891- afterDot := tok = sPeriod;
1897+ afterDot := tok = sPeriod; { a name after a dot is a field, not a variable }
18921898 if (d > 0) and (tok <> sEof) then Next;
18931899 until (d = 0) or (tok = sEof);
18941900 RestoreScan(st);
18951901 skipping := wasSkipping;
1902+ { the five heaviest, each used at least three times, take the
1903+ registers in turn }
18961904 for k := 0 to 4 do begin
18971905 best := -1;
18981906 for i := 0 to n - 1 do

src/sym.pas modified +4 -3

@@ -411,9 +411,10 @@
411411 InTwoRegs := t^.tuple and (t^.len = 2) and OneWord(t^.fields) and OneWord(t^.fields^.next);
412412 end;
413413
414-{ Whether a procedure's result t comes back in the caller's memory, in
415- a place a hidden pointer, passed first, says: any result that is
416- neither one value, in rax or xmm0, nor two words, in rax and rdx. }
414+{ Whether a procedure's result t comes back in memory the caller
415+ provides, whose address the caller passes as a hidden first argument,
416+ in rdi: any result that is neither one value, in rax or xmm0, nor two
417+ words, in rax and rdx. }
417418 function BehindPointer(t: PType): boolean;
418419 begin
419420 BehindPointer := IsAggregate(t) and not InTwoRegs(t);

src/x64.pas modified +6 -3

@@ -709,9 +709,12 @@
709709
710710 { If the last instruction made is a jmp to L, it is taken back, since L
711711 is to be placed just after it: a return at the end of a procedure
712- needn't jump to the code that returns. The caller must place L next,
713- before anything can have noted where the jmp ended. A jcc's field
714- follows the bytes 0F 8x, never E9. }
712+ needn't jump to the code that returns. L is the place of the latest
713+ jump's 4-byte field, so the last instruction is that jump when L is
714+ 4 bytes back and an E9 byte (jmp) is before it; a jcc's field follows
715+ the bytes 0F 8x, never E9. L then becomes the next jump on the chain,
716+ which the field held. The caller must place L next, before anything
717+ can have noted where the jmp ended. }
715718 procedure DropJumpTo(var L: longint);
716719 begin
717720 if (L = NoLabel) or (L <> pc - 4) or (code^[L - 1] <> $E9) then exit;