| | @@ -9,19 +9,30 @@ |
| 9 | 9 | code for those calls, and nothing above it knows the machine: a port |
| 10 | 10 | is a new gen.pas, with an encoder of its own. |
| 11 | 11 | |
| 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. |
| 17 | 19 | |
| 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. |
| 25 | 36 | |
| 26 | 37 | A procedure's frame holds its parameters, its variables, and the |
| 27 | 38 | temporaries of the statement running. The variables are zeroed on |
| | @@ -80,7 +91,7 @@ |
| 80 | 91 | |
| 81 | 92 | const |
| 82 | 93 | { 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 |
| 84 | 95 | those it uses, and Rt finds them there for the trap report } |
| 85 | 96 | VarRegs: array[0..4] of longint = (RBX, R12, R13, R14, R15); |
| 86 | 97 | |
| | @@ -174,8 +185,9 @@ |
| 174 | 185 | type |
| 175 | 186 | { where a value on the stack is } |
| 176 | 187 | 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 } |
| 179 | 191 | lAdr, { not loaded: the address of chunk, or of the frame |
| 180 | 192 | if chunk is -1, plus val } |
| 181 | 193 | lReg, { in its register } |
| | @@ -183,8 +195,9 @@ |
| 183 | 195 | |
| 184 | 196 | TEntry = record |
| 185 | 197 | 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 } |
| 188 | 201 | k: TKind; |
| 189 | 202 | end; |
| 190 | 203 | |
| | @@ -214,7 +227,7 @@ |
| 214 | 227 | inObj: array[0..13] of PObj; |
| 215 | 228 | numIn: longint; |
| 216 | 229 | 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] } |
| 218 | 231 | saveAt: int64; { where they are saved, a quadword each } |
| 219 | 232 | frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations } |
| 220 | 233 | { the checks of the procedure, whose failures EndFails makes } |
| | @@ -234,10 +247,20 @@ |
| 234 | 247 | end; |
| 235 | 248 | |
| 236 | 249 | { 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). } |
| 241 | 264 | |
| 242 | 265 | function RegOf(i: longint): longint; |
| 243 | 266 | begin |
| | @@ -263,7 +286,7 @@ |
| 263 | 286 | if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]); |
| 264 | 287 | end; |
| 265 | 288 | |
| 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 |
| 267 | 290 | spilled. } |
| 268 | 291 | function Claim(i: longint): longint; |
| 269 | 292 | var r: longint; |
| | @@ -402,21 +425,25 @@ |
| 402 | 425 | Drop; |
| 403 | 426 | Place := Mem(ra, a.off) |
| 404 | 427 | end; |
| 405 | | - else |
| 428 | + else { bSlot: the address is in the frame slot a.chunk } |
| 406 | 429 | Load(r, Mem(RBP, a.chunk), 8, false); |
| 407 | 430 | Place := Mem(r, a.off); |
| 408 | 431 | end; |
| 409 | 432 | end; |
| 410 | 433 | |
| 434 | +{ The address a names, on top of the stack } |
| 411 | 435 | procedure PushAdr(const a: TAdr); |
| 412 | 436 | var m: TMem; r: longint; |
| 413 | 437 | 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 } |
| 415 | 439 | if a.base in [bFrame, bGlobal] then begin |
| 416 | 440 | NewEntry(kWord, lAdr, a.off); |
| 417 | 441 | if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; |
| 418 | 442 | exit; |
| 419 | 443 | 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. } |
| 420 | 447 | if a.base = bStack then begin m := Place(a, -1); r := Target end |
| 421 | 448 | else begin r := Target; m := Place(a, r) end; |
| 422 | 449 | Lea(r, m); |
| | @@ -430,6 +457,7 @@ |
| 430 | 457 | NewEntry(k, lHeld, a.chunk); |
| 431 | 458 | exit; |
| 432 | 459 | end; |
| 460 | + { in the order PushAdr explains } |
| 433 | 461 | if a.base = bStack then begin m := Place(a, -1); r := Target end |
| 434 | 462 | else begin r := Target; m := Place(a, r) end; |
| 435 | 463 | Load(r, m, size, signed); |
| | @@ -539,6 +567,7 @@ |
| 539 | 567 | |
| 540 | 568 | { Operations } |
| 541 | 569 | |
| 570 | +{ n if v is 2^n, which is 0 for 1; -1 if v isn't a power of two } |
| 542 | 571 | function ExactLog2(v: int64): longint; |
| 543 | 572 | var n: longint; |
| 544 | 573 | begin |
| | @@ -569,11 +598,16 @@ |
| 569 | 598 | zero, so a negative dividend first gets 2^k - 1 added (the bias). } |
| 570 | 599 | procedure DivPow2(op: TSym; r, k: longint; unsign: boolean); |
| 571 | 600 | begin |
| 601 | + { unsigned: div shifts right; mod keeps the low k bits, by shifting |
| 602 | + them up to the top and back down } |
| 572 | 603 | if unsign then begin |
| 573 | 604 | if op = sDiv then ShiftI(shShr, r, k) |
| 574 | 605 | else begin ShiftI(shShl, r, 64 - k); ShiftI(shShr, r, 64 - k) end; |
| 575 | 606 | exit; |
| 576 | 607 | 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. } |
| 577 | 611 | MovRR(RCX, r); |
| 578 | 612 | ShiftI(shSar, RCX, 63); |
| 579 | 613 | ShiftI(shShr, RCX, 64 - k); |
| | @@ -618,10 +652,10 @@ |
| 618 | 652 | MovqXR(0, r); |
| 619 | 653 | MovqXR(1, y); |
| 620 | 654 | 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 } |
| 625 | 659 | end; |
| 626 | 660 | MovqRX(r, 0); |
| 627 | 661 | end; |
| | @@ -672,10 +706,12 @@ |
| 672 | 706 | sTimes: Imul(r, y); |
| 673 | 707 | sDiv, sMod: begin |
| 674 | 708 | 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 } |
| 676 | 710 | else begin |
| 711 | + { only -1 can make the division overflow, so a constant |
| 712 | + divisor other than -1 needs no check } |
| 677 | 713 | 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 } |
| 679 | 715 | end; |
| 680 | 716 | if op = sDiv then MovRR(r, RAX) else MovRR(r, RDX); |
| 681 | 717 | end; |
| | @@ -704,6 +740,12 @@ |
| 704 | 740 | r := Operands(y); |
| 705 | 741 | MovqXR(0, r); |
| 706 | 742 | 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. } |
| 707 | 749 | case op of |
| 708 | 750 | sEql: begin Ucomis(0, 1, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end; |
| 709 | 751 | sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end; |
| | @@ -728,7 +770,7 @@ |
| 728 | 770 | kF32: Btc(r, 31); |
| 729 | 771 | kF64: Btc(r, 63); |
| 730 | 772 | else |
| 731 | | - Unary(3, r); |
| 773 | + Unary(3, r); { neg } |
| 732 | 774 | if size < 8 then Extend(r, size, signed); |
| 733 | 775 | end; |
| 734 | 776 | end; |
| | @@ -737,7 +779,7 @@ |
| 737 | 779 | var r: longint; |
| 738 | 780 | begin |
| 739 | 781 | r := Top; |
| 740 | | - Unary(2, r); |
| 782 | + Unary(2, r); { not } |
| 741 | 783 | if size < 8 then Extend(r, size, signed); |
| 742 | 784 | end; |
| 743 | 785 | |
| | @@ -775,7 +817,7 @@ |
| 775 | 817 | AluI(aluAnd, r, 1); |
| 776 | 818 | Alu(aluOr, RCX, r); |
| 777 | 819 | Cvtsi2s(0, RCX, f); |
| 778 | | - Sse($58, 0, 0, f); |
| 820 | + Sse($58, 0, 0, f); { adds it to itself: doubles } |
| 779 | 821 | FixLink(L2); |
| 780 | 822 | end else Cvtsi2s(0, r, f); |
| 781 | 823 | MovqRX(r, 0); |
| | @@ -798,14 +840,15 @@ |
| 798 | 840 | begin |
| 799 | 841 | r := Top; |
| 800 | 842 | MovqXR(0, r); |
| 801 | | - Sse($5A, 0, 0, k = kF32); |
| 843 | + Sse($5A, 0, 0, k = kF32); { cvtss2sd, or cvtsd2ss } |
| 802 | 844 | MovqRX(r, 0); |
| 803 | 845 | if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32; |
| 804 | 846 | end; |
| 805 | 847 | |
| 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. } |
| 809 | 852 | procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); |
| 810 | 853 | var differ, r: longint; |
| 811 | 854 | begin |
| | @@ -817,15 +860,18 @@ |
| 817 | 860 | Load(RSI, Mem(RBP, b), 8, false); |
| 818 | 861 | Load(RDI, Mem(RBP, a), 8, false); |
| 819 | 862 | 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); |
| 821 | 866 | RepeCmpsb; |
| 822 | 867 | FixLink(differ); |
| 823 | 868 | if eq then SetCC(ccE, r) else SetCC(ccNE, r); |
| 824 | 869 | Computed(kWord); |
| 825 | 870 | end; |
| 826 | 871 | |
| 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. } |
| 829 | 875 | procedure ErrorText(tmp: int64); |
| 830 | 876 | var L: longint; |
| 831 | 877 | begin |
| | @@ -893,7 +939,7 @@ |
| 893 | 939 | var r, n: longint; |
| 894 | 940 | begin |
| 895 | 941 | 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 } |
| 897 | 943 | r := Top; |
| 898 | 944 | case which of |
| 899 | 945 | bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end; |
| | @@ -903,8 +949,11 @@ |
| 903 | 949 | stack[depth - 1].k := kWord; |
| 904 | 950 | end; |
| 905 | 951 | |
| 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. } |
| 908 | 957 | procedure SysCall(n: longint); |
| 909 | 958 | const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); |
| 910 | 959 | var i: longint; |
| | @@ -960,15 +1009,19 @@ |
| 960 | 1009 | |
| 961 | 1010 | { JumpCmp out of an expression, past code that may spill: the values |
| 962 | 1011 | 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. } |
| 964 | 1015 | procedure JumpOut(op: TSym; signed: boolean; var L: longint); |
| 965 | 1016 | begin |
| 966 | 1017 | SpillBelow(depth - 2); |
| 967 | 1018 | JumpCmp(op, signed, L); |
| 968 | 1019 | end; |
| 969 | 1020 | |
| 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. } |
| 972 | 1025 | procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); |
| 973 | 1026 | var r, e: longint; |
| 974 | 1027 | begin |
| | @@ -984,6 +1037,10 @@ |
| 984 | 1037 | Computed(kWord); |
| 985 | 1038 | end; |
| 986 | 1039 | |
| 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. } |
| 987 | 1044 | procedure JumpBack(top: longint); |
| 988 | 1045 | begin |
| 989 | 1046 | JmpBack(top); |
| | @@ -1011,14 +1068,18 @@ |
| 1011 | 1068 | |
| 1012 | 1069 | { The program goes on if the condition cc holds, and otherwise stops |
| 1013 | 1070 | 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 |
| 1019 | 1079 | 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. } |
| 1022 | 1083 | procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); |
| 1023 | 1084 | begin |
| 1024 | 1085 | if numFails > High(fails) then Mark('more than ' + IntStr(High(fails) + 1) + ' checks in one procedure'); |
| | @@ -1126,6 +1187,8 @@ |
| 1126 | 1187 | m := Place(a, RDI); |
| 1127 | 1188 | ml := m; ml.disp := ml.disp + 8; |
| 1128 | 1189 | 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 } |
| 1129 | 1192 | if isArray then begin CmpI(RDX, n); ml := NoMem end |
| 1130 | 1193 | else AluM(aluCmp, RDX, ml); |
| 1131 | 1194 | Check(ccBE, 1, p, RCX, ml, n, -1); |
| | @@ -1142,9 +1205,9 @@ |
| 1142 | 1205 | end; |
| 1143 | 1206 | |
| 1144 | 1207 | { 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. } |
| 1148 | 1211 | procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); |
| 1149 | 1212 | var ml: TMem; L, E: longint; |
| 1150 | 1213 | begin |
| | @@ -1161,7 +1224,9 @@ |
| 1161 | 1224 | Load(RDI, Mem(RBP, dOff), 8, false); |
| 1162 | 1225 | Alu(aluCmp, RDI, RSI); |
| 1163 | 1226 | 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 } |
| 1165 | 1230 | Lea(RSI, MemIdx(RSI, RCX, 1, -1)); |
| 1166 | 1231 | Lea(RDI, MemIdx(RDI, RCX, 1, -1)); |
| 1167 | 1232 | Std; |
| | @@ -1195,9 +1260,15 @@ |
| 1195 | 1260 | |
| 1196 | 1261 | { Calls } |
| 1197 | 1262 | |
| 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. } |
| 1201 | 1272 | |
| 1202 | 1273 | procedure Call(var c: TCall); |
| 1203 | 1274 | var |
| | @@ -1229,8 +1300,11 @@ |
| 1229 | 1300 | end; |
| 1230 | 1301 | if onStack[i] then inc(k); |
| 1231 | 1302 | 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. } |
| 1234 | 1308 | pad := ord(odd(pushed + k)); |
| 1235 | 1309 | if pad <> 0 then AluI(aluSub, RSP, 8); |
| 1236 | 1310 | for i := c.n - 1 downto 0 do |
| | @@ -1242,7 +1316,9 @@ |
| 1242 | 1316 | late[4] := -1; late[5] := -1; |
| 1243 | 1317 | if (c.ret <> 0) and BehindPointer(res) then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; |
| 1244 | 1318 | { 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. } |
| 1246 | 1322 | for i := 0 to c.n - 1 do |
| 1247 | 1323 | if not onStack[i] then |
| 1248 | 1324 | if c.isF[i] then begin |
| | @@ -1293,6 +1369,9 @@ |
| 1293 | 1369 | |
| 1294 | 1370 | { Frames } |
| 1295 | 1371 | |
| 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. } |
| 1296 | 1375 | function AllocTemp(size, align: int64): int64; |
| 1297 | 1376 | begin |
| 1298 | 1377 | if align < 1 then align := 1; |
| | @@ -1312,6 +1391,8 @@ |
| 1312 | 1391 | TempMark := frameTop; |
| 1313 | 1392 | end; |
| 1314 | 1393 | |
| 1394 | +{ The slots allocated since the mark m are free again, but those |
| 1395 | + AllocPerm made stay } |
| 1315 | 1396 | procedure ReleaseTemps(m: int64); |
| 1316 | 1397 | begin |
| 1317 | 1398 | if m < permTop then m := permTop; |
| | @@ -1340,7 +1421,7 @@ |
| 1340 | 1421 | resType := sig^.base; |
| 1341 | 1422 | procParams := params; |
| 1342 | 1423 | 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 } |
| 1344 | 1425 | if (resType <> nil) and BehindPointer(resType) then begin |
| 1345 | 1426 | retSlot := AllocPerm(8, 8); |
| 1346 | 1427 | inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; |
| | @@ -1348,7 +1429,7 @@ |
| 1348 | 1429 | end; |
| 1349 | 1430 | p := params; |
| 1350 | 1431 | 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)); |
| 1352 | 1433 | f := (p^.typ^.form = fReal) and not p^.isVar; |
| 1353 | 1434 | if (p^.typ^.form = fSlice) and not p^.isVar then begin |
| 1354 | 1435 | { its two words, in two registers or both on the stack } |
| | @@ -1365,6 +1446,8 @@ |
| 1365 | 1446 | end; |
| 1366 | 1447 | end else if (f and (nf < 8)) or (not f and (ni < 6)) then begin |
| 1367 | 1448 | 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) } |
| 1368 | 1451 | if f then begin inReg[numIn] := -1 - nf; inc(nf) end |
| 1369 | 1452 | else begin inReg[numIn] := IntRegs[ni]; inc(ni) end; |
| 1370 | 1453 | inSlot[numIn] := p^.val; |
| | @@ -1434,22 +1517,25 @@ |
| 1434 | 1517 | Push(RBP); |
| 1435 | 1518 | MovRR(RBP, RSP); |
| 1436 | 1519 | 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); |
| 1438 | 1523 | frameFix := pc; Put4(0); |
| 1439 | 1524 | n := (AlignUp(zeroTop, 8) - paramTop) div 8; |
| 1440 | 1525 | if n > 0 then MovRI(RAX, 0); |
| 1441 | 1526 | if n <= 4 then |
| 1442 | 1527 | for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8) |
| 1443 | 1528 | 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 } |
| 1445 | 1531 | MovRI(R10, -n); |
| 1446 | 1532 | top := pc; |
| 1447 | 1533 | Store(MemIdx(RBP, R10, 8, -paramTop), RAX, 8); |
| 1448 | 1534 | AluI(aluAdd, R10, 1); |
| 1449 | 1535 | JccBack(ccNE, top); |
| 1450 | 1536 | 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 } |
| 1453 | 1539 | chunks^[c].saveMask := regMask; |
| 1454 | 1540 | chunks^[c].saveAt := saveAt; |
| 1455 | 1541 | n := 0; |
| | @@ -1459,12 +1545,16 @@ |
| 1459 | 1545 | MovRI(VarRegs[i], 0); |
| 1460 | 1546 | inc(n); |
| 1461 | 1547 | end; |
| 1548 | + { the parameters that came in registers, to their slots or, for a |
| 1549 | + register variable, to its register } |
| 1462 | 1550 | for i := 0 to numIn - 1 do |
| 1463 | 1551 | if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i]) |
| 1464 | 1552 | else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin |
| 1465 | 1553 | MovRR(inObj[i]^.reg, inReg[i]); |
| 1466 | 1554 | if inSize[i] < 8 then Extend(inObj[i]^.reg, inSize[i], inObj[i]^.typ^.signed); |
| 1467 | 1555 | 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 } |
| 1468 | 1558 | p := procParams; |
| 1469 | 1559 | while p <> nil do begin |
| 1470 | 1560 | if (p^.reg <> 0) and (p^.val > 0) then |
| | @@ -1555,7 +1645,8 @@ |
| 1555 | 1645 | end; |
| 1556 | 1646 | |
| 1557 | 1647 | { 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 |
| 1559 | 1650 | and starts it, with the return address pushed. The stack is empty |
| 1560 | 1651 | where it is called, and where it begins. } |
| 1561 | 1652 | function DeferBegin(var L, saved: longint): longint; |
| | @@ -1584,7 +1675,13 @@ |
| 1584 | 1675 | main; and ends with Rt.exit, its status main's or 0. A dynamic program |
| 1585 | 1676 | starts as C's crt1 does, through __libc_start_main(main, argc, argv, |
| 1586 | 1677 | 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). } |
| 1588 | 1685 | function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; |
| 1589 | 1686 | var entry, start, i: longint; m: PModule; |
| 1590 | 1687 | begin |
| | @@ -1592,18 +1689,20 @@ |
| 1592 | 1689 | if dynamic then begin |
| 1593 | 1690 | start := NewChunk(secText, -1, Intern('main', 4)); |
| 1594 | 1691 | 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 } |
| 1599 | 1698 | 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 } |
| 1603 | 1702 | MovRI(RCX, 0); |
| 1604 | | - Lea(RDI, MemRip(start, 0)); |
| 1703 | + Lea(RDI, MemRip(start, 0)); { main, the first argument } |
| 1605 | 1704 | CallM(MemRip(ExternChunk(Intern('__libc_start_main', 17)), 0)); |
| 1606 | | - Ud2; |
| 1705 | + Ud2; { it never returns } |
| 1607 | 1706 | EndChunk(entry); |
| 1608 | 1707 | BeginChunk(start); |
| 1609 | 1708 | Push(RBP); { aligns the stack again } |
| | @@ -1629,10 +1728,13 @@ |
| 1629 | 1728 | if main <> nil then CallChunk(main^.chunk); |
| 1630 | 1729 | if (main <> nil) and (main^.typ^.base <> nil) then MovRR(RDI, RAX) else MovRI(RDI, 0); |
| 1631 | 1730 | CallChunk(exitProc); |
| 1632 | | - Ud2; |
| 1731 | + Ud2; { exit never returns: trap if it does } |
| 1633 | 1732 | EndChunk(start); |
| 1634 | 1733 | { 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. } |
| 1636 | 1738 | for i := 0 to High(failStubs) do |
| 1637 | 1739 | if failStubs[i] >= 0 then begin |
| 1638 | 1740 | BeginChunk(failStubs[i]); |