| | @@ -4,13 +4,14 @@ |
| 4 | 4 | The same stack machine as x86-64's, with the same calls, which Item |
| 5 | 5 | and Parse make: they push constants, addresses and the values of |
| 6 | 6 | variables, operate on the values on top, store them and jump on |
| 7 | | - them. This one is begun: what it makes so far is frames, constants, |
| 8 | | - the values of variables and their stores, the integer operations but |
| 9 | | - division, comparisons and jumps, elements and slices unchecked, |
| 10 | | - calls of procedures with integer arguments in registers, C's through |
| 11 | | - the quadword the loader fills in, code procedures and the program's |
| 12 | | - entry point. What else is |
| 13 | | - asked is noted as "not yet on arm64" (notYet), and a program that |
| 7 | + them. What it makes so far is frames, constants, the values of |
| 8 | + variables and their stores, the integer operations, comparisons and |
| 9 | + jumps, jump tables, elements and slices and their checks, calls with |
| 10 | + integer arguments in registers and on the stack, C's through the |
| 11 | + quadword the loader fills in, results in memory, try, deferred |
| 12 | + statements, the atomics, COPY, code procedures and the program's |
| 13 | + entry point. What else |
| 14 | + is asked is noted as "not yet on arm64" (notYet), and a program that |
| 14 | 15 | uses it fails to compile rather than run wrong; PLAN.md 5 says the |
| 15 | 16 | pieces still to come. |
| 16 | 17 | |
| | @@ -33,17 +34,20 @@ |
| 33 | 34 | pointer, and x30, the return address. |
| 34 | 35 | |
| 35 | 36 | Calls use Apple's arm64 convention: the first eight integer arguments |
| 36 | | - in x0 to x7, results in x0 and x1, sp a multiple of 16 always, a |
| 37 | + in x0 to x7, the rest on the machine's stack, results in x0 and x1, |
| 38 | + or in memory at the address in x8, sp a multiple of 16 always, a |
| 37 | 39 | frame record of x29 and x30 that x29 points at, so that the chain of |
| 38 | 40 | frames can be followed. A frame is made whole by the prologue, so |
| 39 | | - nothing is pushed in a body: sp stays where the prologue put it, and |
| 40 | | - pushed is always 0. |
| 41 | + nothing is pushed in a body but a call's stack arguments, for the |
| 42 | + call, and a deferred statement's return address: pushed is always 0. |
| 41 | 43 | |
| 44 | + What x86-64 catches by itself, and arm64 doesn't, the code checks: a |
| 45 | + division by zero, and the smallest value by -1 (divide). |
| 46 | + |
| 42 | 47 | Depends on: Scan and Diag for errors, Sym for types, Chunk for code |
| 43 | 48 | and data, A64 for instructions. |
| 44 | | - Leaves out, for now: reals, division, checks, COPY and FILL, |
| 45 | | - arguments and parameters on the machine's stack, results in memory, |
| 46 | | - try, deferred statements, atomics and system calls. *) |
| 49 | + Leaves out, for now: reals, FILL, NEW's zeroing, the counts of bits |
| 50 | + and the rotations; and SYSCALL, which is Linux's only. *) |
| 47 | 51 | module Gen; |
| 48 | 52 | import Fmt, Diag, Scan, Sym, Chunk, A := A64; |
| 49 | 53 | import SYSTEM; |
| | @@ -53,9 +57,11 @@ |
| 53 | 57 | NoLabel* = -1; |
| 54 | 58 | WordSize* = 8; (* the size of an address, and of INT *) |
| 55 | 59 | (* What the machine catches by itself: a load or store within |
| 56 | | - NilPage bytes of address 0 (the Mac's page zero is 4 GB; this is |
| 57 | | - Linux's figure until Rt tells the Mac's faults) *) |
| 58 | | - NilPage* = 4096; |
| 60 | + NilPage bytes of address 0, in the Mac's page zero of 4 GB, which |
| 61 | + Rt's handler tells as a nil pointer; and a call to nil, and a |
| 62 | + stack overflow at the guard page under the stack. A division by |
| 63 | + zero it doesn't catch: op checks for it. *) |
| 64 | + NilPage* = 100000000H; |
| 59 | 65 | |
| 60 | 66 | FP = A.FP; LR = A.LR; SP = A.SP; ZR = A.ZR; |
| 61 | 67 | T = 15; U = 17; (* Gen's scratch *) |
| | @@ -140,8 +146,18 @@ |
| 140 | 146 | into that chunk, reached by adrp (A64.loadChunk) *) |
| 141 | 147 | Mem = record base, chunk: INT; off: I64; end; |
| 142 | 148 | |
| 149 | + (* a check of the procedure, whose failure endFails makes: what |
| 150 | + failed, a's register, the length's (or -1 for the constant n), |
| 151 | + the enumeration's name, and the place *) |
| 152 | + Fail = record |
| 153 | + L, what, ra, lenReg, name: INT; |
| 154 | + n: I64; |
| 155 | + path, line, col: INT; |
| 156 | + end; |
| 157 | + |
| 143 | 158 | const |
| 144 | 159 | MaxStack = 1024; (* values waiting in one expression *) |
| 160 | + StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; |
| 145 | 161 | NumRegs = 6; |
| 146 | 162 | Regs: [NumRegs]INT = [9, 10, 11, 12, 13, 14]; |
| 147 | 163 | (* the condition codes of the comparisons, from sEql: = # < <= > >= *) |
| | @@ -161,17 +177,24 @@ |
| 161 | 177 | retSlot: I64; |
| 162 | 178 | resType: ^Sym.Type; (* the procedure's result, or nil *) |
| 163 | 179 | exitL: INT; |
| 164 | | - (* parameters arriving in registers: where each is stored on entry *) |
| 165 | | - inReg: [8]INT; |
| 166 | | - inSlot: [8]I64; |
| 167 | | - inSize: [8]INT; |
| 168 | | - inObj: [8]^Sym.Obj; |
| 180 | + (* parameters arriving in registers, x0 to x7, and a result's address |
| 181 | + in x8: where each is stored on entry *) |
| 182 | + inReg: [9]INT; |
| 183 | + inSlot: [9]I64; |
| 184 | + inSize: [9]INT; |
| 185 | + inObj: [9]^Sym.Obj; |
| 169 | 186 | numIn: INT; |
| 187 | + procParams: ^Sym.Obj; (* the parameters, for enterBody *) |
| 170 | 188 | regMask: INT; (* the registers of variables, as bits (XBit) *) |
| 171 | 189 | saveAt: I64; (* where they are saved, a quadword each *) |
| 172 | 190 | frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) |
| 173 | 191 | pending: []const U8; (* what beginProc found not yet made, for enterBody's chunk *) |
| 174 | 192 | pendingAt: Diag.Pos; |
| 193 | + (* the stubs that a failed check calls, one for each kind of failure, |
| 194 | + made by entryPoint for those used; and the procedure's checks *) |
| 195 | + failStubs: [8]INT; |
| 196 | + fails: [4096]Fail; |
| 197 | + numFails: INT; |
| 175 | 198 | |
| 176 | 199 | (* What this Gen can't make yet marks the procedure being compiled |
| 177 | 200 | (Chunk.setMissing), which is an error if the program uses it, and the |
| | @@ -179,21 +202,21 @@ |
| 179 | 202 | calls, as Sys does. So each routine not yet made keeps the stack as |
| 180 | 203 | the real one would, and makes what code it likes, never run; it |
| 181 | 204 | reads into _ the parameters it doesn't use yet, so that none is |
| 182 | | - warned of. *) |
| 183 | | -proc notYetAt(what: []const U8; p: Diag.Pos); |
| 205 | + warned of. What this Gen will never make, SYSCALL, is marked so too, |
| 206 | + with its own message. *) |
| 207 | +proc missing(msg: []const U8); |
| 184 | 208 | begin |
| 185 | 209 | if codeChunk >= 0 then |
| 186 | | - Chunk.setMissing(codeChunk, Diag.say("not yet on arm64: {}", [Fmt.str(what)]), p); |
| 210 | + Chunk.setMissing(codeChunk, msg, Scan.tokPos()); |
| 187 | 211 | elsif LEN(pending) = 0 then (* beginProc's, before enterBody names the chunk *) |
| 188 | | - pending := Diag.say("not yet on arm64: {}", [Fmt.str(what)]); |
| 189 | | - pendingAt := p; |
| 212 | + pending := msg; |
| 213 | + pendingAt := Scan.tokPos(); |
| 190 | 214 | end; |
| 191 | 215 | end; |
| 192 | 216 | |
| 193 | | -(* The same, at the place being read *) |
| 194 | 217 | proc notYet(what: []const U8); |
| 195 | 218 | begin |
| 196 | | - notYetAt(what, Scan.tokPos()); |
| 219 | + missing(Diag.say("not yet on arm64: {}", [Fmt.str(what)])); |
| 197 | 220 | end; |
| 198 | 221 | |
| 199 | 222 | (* -S: the text of each instruction is kept, and list prints it *) |
| | @@ -499,14 +522,40 @@ |
| 499 | 522 | end; |
| 500 | 523 | |
| 501 | 524 | (* Copying, by loads and stores of 8, 4, 2 and 1 bytes, from [x1] to |
| 502 | | - [x0], through x2 *) |
| 525 | + [x0], through x2. Past MaxMoves bytes the words go by a loop, which |
| 526 | + moves x0 and x1 on, counting down in x3, and the last few bytes |
| 527 | + after it as before: |
| 503 | 528 | |
| 529 | + mov x3, #words |
| 530 | + top: ldr x2, [x1], #8 |
| 531 | + str x2, [x0], #8 |
| 532 | + subs x3, x3, #1 |
| 533 | + b.ne top |
| 534 | + ... ; the size mod 8 bytes left, from [x1] to [x0] |
| 535 | + |
| 536 | + zeroBytes is the same, storing the zero register. *) |
| 537 | + |
| 504 | 538 | const MaxMoves = 256; |
| 505 | 539 | |
| 540 | +(* The loop's head and tail, of the words of size bytes, by store and |
| 541 | + load if load; returns what is left after the words *) |
| 542 | +proc wordLoop(size: I64; load: BOOL): I64; |
| 543 | +var top: INT; |
| 544 | +begin |
| 545 | + if size <= MaxMoves then return size; end; |
| 546 | + A.movRI(3, size div 8); |
| 547 | + top := Chunk.pc; |
| 548 | + if load then A.loadIdx(Tmp, Src, 8, 8, A.mPost); A.storeIdx(Tmp, Dst, 8, 8, A.mPost); |
| 549 | + else A.storeIdx(ZR, Dst, 8, 8, A.mPost); end; |
| 550 | + A.aluI(A.opSubs, 3, 3, 1); |
| 551 | + A.jccBack(A.ccNE, top); |
| 552 | + return size mod 8; |
| 553 | +end; |
| 554 | + |
| 506 | 555 | proc copyBytes(size: I64); |
| 507 | 556 | var o, n: I64; |
| 508 | 557 | begin |
| 509 | | - if size > MaxMoves then notYet("a copy of more than 256 bytes"); return; end; |
| 558 | + size := wordLoop(size, true); |
| 510 | 559 | o := 0; |
| 511 | 560 | while o < size do |
| 512 | 561 | n := 8; |
| | @@ -520,7 +569,7 @@ |
| 520 | 569 | proc zeroBytes(size: I64); |
| 521 | 570 | var o, n: I64; |
| 522 | 571 | begin |
| 523 | | - if size > MaxMoves then notYet("zeroing more than 256 bytes"); return; end; |
| 572 | + size := wordLoop(size, false); |
| 524 | 573 | o := 0; |
| 525 | 574 | while o < size do |
| 526 | 575 | n := 8; |
| | @@ -560,6 +609,71 @@ |
| 560 | 609 | zeroBytes(size); |
| 561 | 610 | end; |
| 562 | 611 | |
| 612 | +(* Checks *) |
| 613 | + |
| 614 | +(* The name s, interned *) |
| 615 | +proc internStr(s: []const U8): INT; |
| 616 | +begin |
| 617 | + return Scan.intern(SYSTEM.PTR(s), LEN(s)); |
| 618 | +end; |
| 619 | + |
| 620 | +(* The program goes on if the condition cc holds, and otherwise stops |
| 621 | + in Rt.fail(what, a, b, len, at, name), as on x86-64, whose check says |
| 622 | + what each is (Gen.x86_64.raz). Here a is in register ra and b, where |
| 623 | + there is one, in x2; len is in register lenReg, or the constant n if |
| 624 | + lenReg is -1. The failing code is out of the way, at the procedure's |
| 625 | + end (endFails), so that a check that holds is a branch not taken; it |
| 626 | + has p for its place, worked out now. *) |
| 627 | +proc check(cc, what: INT; p: Diag.Pos; ra, lenReg: INT; n: I64; name: INT); |
| 628 | +var f: INT; |
| 629 | +begin |
| 630 | + if numFails > LEN(fails) - 1 then |
| 631 | + _ := Scan.mark(Diag.say("more than {} checks in one procedure", [Fmt.int(LEN(fails))])); |
| 632 | + return; |
| 633 | + end; |
| 634 | + f := numFails; |
| 635 | + fails[f].L := NoLabel; |
| 636 | + A.jcc(A.invert(cc), fails[f].L); |
| 637 | + fails[f].what := what; |
| 638 | + fails[f].ra := ra; |
| 639 | + fails[f].lenReg := lenReg; |
| 640 | + fails[f].n := n; |
| 641 | + fails[f].name := name; |
| 642 | + fails[f].path := Chunk.curFilePath(); |
| 643 | + fails[f].line, fails[f].col := Scan.lineCol(p.a); |
| 644 | + INC(numFails); |
| 645 | +end; |
| 646 | + |
| 647 | +(* The failures of the procedure's checks: each puts Rt.fail's values in |
| 648 | + the registers it takes them in, x3 first, since a is never there, |
| 649 | + then x1 and x5, and calls the stub for its kind, which adds what |
| 650 | + failed and where (entryPoint): |
| 651 | + |
| 652 | + mov x3, x10 ; the length, or mov x3, #n |
| 653 | + mov x1, x15 ; a |
| 654 | + adrp x5, name; add x5 ... ; an enumeration's name |
| 655 | + bl check0 *) |
| 656 | +proc endFails; |
| 657 | +var i, path: INT; f: Fail; |
| 658 | +begin |
| 659 | + path := Chunk.curFilePath(); |
| 660 | + for i := 0 to numFails - 1 do |
| 661 | + f := fails[i]; |
| 662 | + A.fixLink(f.L); |
| 663 | + Chunk.setCurFile(f.path); |
| 664 | + Chunk.noteLine(f.line, f.col); |
| 665 | + Chunk.setCurFile(path); |
| 666 | + if f.lenReg < 0 then A.movRI(3, f.n); elsif f.lenReg # 3 then A.movRR(3, f.lenReg); end; |
| 667 | + if f.ra # 1 then A.movRR(1, f.ra); end; |
| 668 | + if f.name >= 0 then A.addrChunk(5, f.name, 0); end; |
| 669 | + if failStubs[f.what] < 0 then |
| 670 | + failStubs[f.what] := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr(StubNames[f.what])); |
| 671 | + end; |
| 672 | + A.callChunk(failStubs[f.what]); |
| 673 | + end; |
| 674 | + numFails := 0; |
| 675 | +end; |
| 676 | + |
| 563 | 677 | (* Operations *) |
| 564 | 678 | |
| 565 | 679 | (* The right operand of the two values on top, popped, in register y: |
| | @@ -586,6 +700,46 @@ |
| 586 | 700 | end; |
| 587 | 701 | end; |
| 588 | 702 | |
| 703 | +(* r div y, or r mod y, of bits bits, signed or not, y being the |
| 704 | + constant v if c. x86's idiv stops the program for a division by |
| 705 | + zero, and for the smallest value divided by -1, which overflows; the |
| 706 | + report binds both (6.1), and arm64's sdiv and udiv give a value for |
| 707 | + each, so the code checks first, as Rt.fail's kind 5, a division |
| 708 | + fault. The smallest value is that of the type's width, for a narrow |
| 709 | + one, an I32's -2^31, sign-extended in 64 bits. Of an I64: |
| 710 | + |
| 711 | + cmp x17, #0 |
| 712 | + b.eq fail ; by zero |
| 713 | + cmn x17, #1 |
| 714 | + b.ne L |
| 715 | + mov x15, #8000000000000000H |
| 716 | + cmp x9, x15 |
| 717 | + b.eq fail ; the smallest by -1 |
| 718 | + L: sdiv x15, x9, x17 |
| 719 | + mov x9, x15 ; div; for mod, msub x9, x15, x17, x9 |
| 720 | + |
| 721 | + msub is x9 - x15 * x17, what is left. A constant divisor other than |
| 722 | + 0 needs no first check, and other than -1 no second. *) |
| 723 | +proc divide(sy: Scan.Sym; r, y, bits: INT; signed, c: BOOL; v: I64); |
| 724 | +var L: INT; |
| 725 | +begin |
| 726 | + if not c or (v = 0) then |
| 727 | + A.cmpI(y, 0); |
| 728 | + check(A.ccNE, 5, Scan.tokPos(), r, -1, 0, -1); |
| 729 | + end; |
| 730 | + if signed and (not c or (v = -1)) then |
| 731 | + L := NoLabel; |
| 732 | + A.cmpI(y, -1); |
| 733 | + A.jcc(A.ccNE, L); |
| 734 | + A.movRI(T, I64(-1) shl (bits - 1)); |
| 735 | + A.cmp(r, T); |
| 736 | + check(A.ccNE, 5, Scan.tokPos(), r, -1, 0, -1); |
| 737 | + A.fixLink(L); |
| 738 | + end; |
| 739 | + A.divide(signed, T, r, y); |
| 740 | + if sy = Scan.sDiv then A.movRR(r, T); else A.madd(r, T, y, r, true); end; |
| 741 | +end; |
| 742 | + |
| 589 | 743 | (* The two values on top, the left op the right, an integer of size |
| 590 | 744 | bytes, signed or not, computed in 64 bits and brought back to its |
| 591 | 745 | width, which is how a narrow integer wraps. A shift's count is taken |
| | @@ -594,7 +748,6 @@ |
| 594 | 748 | var v: I64; r, y, bits, sh: INT; c, alu: BOOL; |
| 595 | 749 | begin |
| 596 | 750 | if k in {kF32, kF64} then notYet("reals"); end; |
| 597 | | - if (sy = Scan.sDiv) or (sy = Scan.sMod) then notYet("div and mod"); end; |
| 598 | 751 | bits := size * 8; |
| 599 | 752 | alu := (sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor); |
| 600 | 753 | (* a constant goes right, where it can be an immediate *) |
| | @@ -616,6 +769,8 @@ |
| 616 | 769 | A.alu(aluOf(sy), r, r, y); |
| 617 | 770 | elsif sy = Scan.sTimes then |
| 618 | 771 | A.mul(r, r, y); |
| 772 | + elsif (sy = Scan.sDiv) or (sy = Scan.sMod) then |
| 773 | + divide(sy, r, y, bits, signed, c, v); |
| 619 | 774 | else (* sShl, sShr: the machine takes the count modulo 64 *) |
| 620 | 775 | if bits < 64 then A.aluI(A.opAnd, U, y, bits - 1); y := U; end; |
| 621 | 776 | A.shift(sh, r, r, y, 8); |
| | @@ -643,10 +798,21 @@ |
| 643 | 798 | notYet("POPCOUNT"); |
| 644 | 799 | end; |
| 645 | 800 | |
| 801 | +(* ABS of the signed integer on top: negated if negative, and cut back |
| 802 | + to its width, so that the smallest value gives itself, as -x does |
| 803 | + (Gen.x86_64.raz says more): |
| 804 | + |
| 805 | + cmp x9, #0 |
| 806 | + cneg x9, x9, lt ; csneg x9, x9, x9, ge |
| 807 | + sxtw x9, w9 ; an I32 *) |
| 646 | 808 | proc abs*(k: Kind; size: INT); |
| 809 | +var r: INT; |
| 647 | 810 | begin |
| 648 | | - _ := k; _ := size; |
| 649 | | - notYet("ABS"); |
| 811 | + if k in {kF32, kF64} then notYet("reals"); end; |
| 812 | + r := top(); |
| 813 | + A.cmpI(r, 0); |
| 814 | + A.csel(A.csNeg, A.ccGE, r, r, r); |
| 815 | + if size < 8 then A.extend(r, r, size, true); end; |
| 650 | 816 | end; |
| 651 | 817 | |
| 652 | 818 | (* The two values on top, compared: a BOOL, 1 or 0 *) |
| | @@ -714,11 +880,48 @@ |
| 714 | 880 | stack[depth - 1].k := k; |
| 715 | 881 | end; |
| 716 | 882 | |
| 883 | +(* Whether the slices in the frame at a and b (the offsets of their |
| 884 | + pointers, the lengths 8 bytes on) are equal (or not, if not eq): the |
| 885 | + lengths, then the bytes, len shl shift of them, a byte at a time. The |
| 886 | + flags say equal or not, where the loop leaves it: |
| 887 | + |
| 888 | + ldr x2, [x29, #b + 8] |
| 889 | + ldr x15, [x29, #a + 8] |
| 890 | + cmp x2, x15 |
| 891 | + b.ne out ; the lengths differ |
| 892 | + ldr x1, [x29, #b] |
| 893 | + ldr x0, [x29, #a] |
| 894 | + lsl x2, x2, #shift |
| 895 | + top: cmp x2, #0 |
| 896 | + b.eq out ; no bytes left: equal |
| 897 | + ldrb w15, [x0], #1 |
| 898 | + ldrb w17, [x1], #1 |
| 899 | + sub x2, x2, #1 |
| 900 | + cmp x15, x17 |
| 901 | + b.eq top |
| 902 | + out: cset x9, eq ; or ne *) |
| 717 | 903 | proc compareSeq*(a, b: I64; shift: INT; eq: BOOL); |
| 904 | +var r, out, loop: INT; |
| 718 | 905 | begin |
| 719 | | - _ := a; _ := b; _ := shift; _ := eq; |
| 720 | | - notYet("comparing slices"); |
| 721 | | - _ := target(); |
| 906 | + r := target(); |
| 907 | + out := NoLabel; |
| 908 | + A.load(Tmp, FP, INT(b) + 8, 8, false); |
| 909 | + A.load(T, FP, INT(a) + 8, 8, false); |
| 910 | + A.cmp(Tmp, T); |
| 911 | + A.jcc(A.ccNE, out); |
| 912 | + A.load(Src, FP, INT(b), 8, false); |
| 913 | + A.load(Dst, FP, INT(a), 8, false); |
| 914 | + if shift > 0 then A.shiftI(A.shLsl, Tmp, Tmp, shift, 8); end; |
| 915 | + loop := Chunk.pc; |
| 916 | + A.cmpI(Tmp, 0); |
| 917 | + A.jcc(A.ccEQ, out); |
| 918 | + A.loadIdx(T, Dst, 1, 1, A.mPost); |
| 919 | + A.loadIdx(U, Src, 1, 1, A.mPost); |
| 920 | + A.aluI(A.opSub, Tmp, Tmp, 1); |
| 921 | + A.cmp(T, U); |
| 922 | + A.jccBack(A.ccEQ, loop); |
| 923 | + A.fixLink(out); |
| 924 | + if eq then A.cset(A.ccEQ, r); else A.cset(A.ccNE, r); end; |
| 722 | 925 | computed(kWord); |
| 723 | 926 | end; |
| 724 | 927 | |
| | @@ -768,19 +971,39 @@ |
| 768 | 971 | _ := size; |
| 769 | 972 | notYet("NEW"); |
| 770 | 973 | end; |
| 974 | + |
| 975 | +(* CAS(v, old, new), XCHG(v, x) or XADD(v, x), from the stack, v's |
| 976 | + address under the rest: the result where the address was. By the |
| 977 | + LSE instructions every Apple processor has, each with acquire and |
| 978 | + release, as x86's lock does: casal stores new if [v] holds old, |
| 979 | + leaving what [v] held where old was, which says whether it did: |
| 980 | + |
| 981 | + mov x17, x15 ; old |
| 982 | + casal x17, x10, [x9] ; x10, new |
| 983 | + cmp x17, x15 |
| 984 | + cset x9, eq |
| 771 | 985 | |
| 986 | + swpal and ldaddal leave what [v] held in x17, the result. *) |
| 772 | 987 | proc atomic*(which: INT); |
| 988 | +var r, n: INT; |
| 773 | 989 | begin |
| 774 | | - notYet("CAS, XCHG and XADD"); |
| 775 | | - if which = Sym.bCas then drop(); end; |
| 776 | | - drop(); |
| 990 | + if which = Sym.bCas then n := top(); drop(); end; |
| 991 | + popTo(T); |
| 992 | + r := top(); |
| 993 | + match which of |
| 994 | + | Sym.bCas: A.movRR(U, T); A.casal(U, n, r, 8); A.cmp(U, T); A.cset(A.ccEQ, r); |
| 995 | + | Sym.bXchg: A.swpal(T, U, r, 8); A.movRR(r, U); |
| 996 | + | else: A.ldaddal(T, U, r, 8); A.movRR(r, U); |
| 997 | + end; |
| 777 | 998 | stack[depth - 1].k := kWord; |
| 778 | 999 | end; |
| 779 | 1000 | |
| 1001 | +(* SYSTEM.SYSCALL is Linux's only (report 6.8): macOS has no stable |
| 1002 | + system calls, and a program calls the C library there *) |
| 780 | 1003 | proc sysCall*(n: INT); |
| 781 | 1004 | var i: INT; |
| 782 | 1005 | begin |
| 783 | | - notYet("SYSCALL"); |
| 1006 | + missing("SYSTEM.SYSCALL is Linux's only: call the C library"); |
| 784 | 1007 | for i := 1 to n do drop(); end; |
| 785 | 1008 | _ := target(); |
| 786 | 1009 | computed(kWord); |
| | @@ -856,14 +1079,45 @@ |
| 856 | 1079 | A.fixLink(L); |
| 857 | 1080 | end; |
| 858 | 1081 | |
| 1082 | +(* A jump table, for a match on the integer in frame slot slot, whose |
| 1083 | + labels run from lo up for n values. The selector less lo, compared |
| 1084 | + unsigned (so below lo is above n too), is out of the table if it |
| 1085 | + isn't below n, and goes to the else arm at elseAt, or, if there is |
| 1086 | + none, to endL, the end of the match. Else it picks a word of the |
| 1087 | + table that follows the jump, an offset from the table's start, which |
| 1088 | + adr finds four instructions on: |
| 1089 | + |
| 1090 | + ldr x15, [x29, #slot] |
| 1091 | + sub x15, x15, #lo |
| 1092 | + cmp x15, #n - 1 |
| 1093 | + b.hi else |
| 1094 | + adr x17, table |
| 1095 | + ldrsw x15, [x17, x15, lsl #2] |
| 1096 | + add x15, x17, x15 |
| 1097 | + br x15 |
| 1098 | + table: .word arm0 - table, arm1 - table, ... |
| 1099 | + |
| 1100 | + It returns the table's start; the caller then makes the n entries |
| 1101 | + with tableEntry. *) |
| 859 | 1102 | proc jumpTable*(slot, lo: I64; n, elseAt: INT; var endL: INT): INT; |
| 1103 | +const TableAt = 16; |
| 1104 | +var start: INT; |
| 860 | 1105 | begin |
| 861 | | - _ := slot; _ := lo; _ := n; _ := elseAt; _ := endL; |
| 862 | | - notYet("a jump table"); |
| 863 | | - return Chunk.pc; |
| 1106 | + A.load(T, FP, INT(slot), 8, false); |
| 1107 | + if lo # 0 then A.aluI(A.opSub, T, T, lo); end; |
| 1108 | + A.cmpI(T, n - 1); |
| 1109 | + if elseAt # NoLabel then A.jccBack(A.ccHI, elseAt); else A.jcc(A.ccHI, endL); end; |
| 1110 | + start := Chunk.pc + TableAt; |
| 1111 | + A.adr(U, TableAt); |
| 1112 | + A.loadR(T, U, T, 4, true, true); |
| 1113 | + A.alu(A.opAdd, T, U, T); |
| 1114 | + A.jmpR(T); |
| 1115 | + if Chunk.pc # start then Diag.fatal("internal error: the jump table is not where it was said"); end; |
| 1116 | + return start; |
| 864 | 1117 | end; |
| 865 | 1118 | |
| 866 | | -(* The next entry of the table that jumpTable started at table *) |
| 1119 | +(* The next entry of the table that jumpTable started at table: the |
| 1120 | + code at target *) |
| 867 | 1121 | proc tableEntry*(table, target: INT); |
| 868 | 1122 | begin |
| 869 | 1123 | Chunk.put4(target - table); |
| | @@ -884,30 +1138,55 @@ |
| 884 | 1138 | A.fixLinkTo(L, top); |
| 885 | 1139 | end; |
| 886 | 1140 | |
| 887 | | -(* Checks, and what checks as it goes *) |
| 1141 | +(* Checks, and what checks as it goes, as on x86-64, whose comments |
| 1142 | + say more; a comparison is unsigned, so that a negative index is a |
| 1143 | + huge one *) |
| 888 | 1144 | |
| 1145 | +(* The index on top is below n *) |
| 889 | 1146 | proc checkIndex*(n: I64; p: Diag.Pos); |
| 1147 | +var r: INT; |
| 890 | 1148 | begin |
| 891 | | - _ := n; |
| 892 | | - notYetAt("checks", p); |
| 1149 | + r := top(); |
| 1150 | + A.cmpI(r, n); |
| 1151 | + check(A.ccLO, 0, p, r, -1, n, -1); |
| 893 | 1152 | end; |
| 894 | 1153 | |
| 1154 | +(* The index on top is where a view starts in an array of n elements, |
| 1155 | + below below *) |
| 895 | 1156 | proc checkView*(n, below: I64; p: Diag.Pos); |
| 1157 | +var r: INT; |
| 896 | 1158 | begin |
| 897 | | - _ := n; _ := below; |
| 898 | | - notYetAt("checks", p); |
| 1159 | + r := top(); |
| 1160 | + A.cmpI(r, below); |
| 1161 | + check(A.ccLO, 7, p, r, -1, n, -1); |
| 899 | 1162 | end; |
| 900 | 1163 | |
| 1164 | +(* The integer on top is a position of an enumeration of n names *) |
| 901 | 1165 | proc checkEnum*(n: I64; name: INT; p: Diag.Pos); |
| 1166 | +var r: INT; |
| 902 | 1167 | begin |
| 903 | | - _ := n; _ := name; |
| 904 | | - notYetAt("checks", p); |
| 1168 | + r := top(); |
| 1169 | + A.cmpI(r, n); |
| 1170 | + check(A.ccLO, 2, p, r, -1, n, name); |
| 905 | 1171 | end; |
| 906 | 1172 | |
| 1173 | +(* The address on top, to be used reach bytes past, isn't nil, unless |
| 1174 | + the machine would stop it, within page zero *) |
| 907 | 1175 | proc checkNil*(reach: I64; p: Diag.Pos); |
| 1176 | +var r: INT; |
| 1177 | +begin |
| 1178 | + if reach <= NilPage then return; end; |
| 1179 | + r := top(); |
| 1180 | + A.cmpI(r, 0); |
| 1181 | + check(A.ccNE, 4, p, r, -1, 0, -1); |
| 1182 | +end; |
| 1183 | + |
| 1184 | +(* What a failed index check is: an index past the length (0), or a |
| 1185 | + view that runs past it (7) when it needs room elements more *) |
| 1186 | +proc kindOfIndex(room: I64): INT; |
| 908 | 1187 | begin |
| 909 | | - _ := reach; |
| 910 | | - notYetAt("checks", p); |
| 1188 | + if room > 0 then return 7; end; |
| 1189 | + return 0; |
| 911 | 1190 | end; |
| 912 | 1191 | |
| 913 | 1192 | (* r times v, an element's size: by a shift where v is a power of two, |
| | @@ -927,17 +1206,42 @@ |
| 927 | 1206 | |
| 928 | 1207 | (* Pops an index, and the address of the slice at a if it is on the |
| 929 | 1208 | stack, and makes the address of the element it picks: the slice's |
| 930 | | - pointer plus the index times size *) |
| 1209 | + pointer plus the index times size. The index is checked against the |
| 1210 | + length, in its register or loaded into x3; a view, which needs room |
| 1211 | + elements after the one indexed, against the length less the index, |
| 1212 | + which can't wrap once the index is below it: |
| 1213 | + |
| 1214 | + ldr x3, [x29, #-24] ; the length |
| 1215 | + cmp x15, x3 |
| 1216 | + b.hs fail |
| 1217 | + sub x2, x3, x15 |
| 1218 | + cmp x2, #room |
| 1219 | + b.ls fail *) |
| 931 | 1220 | proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos); |
| 932 | | -var m: Mem; r: INT; |
| 1221 | +var m, ml: Mem; r, len: INT; |
| 933 | 1222 | begin |
| 934 | | - if checked or (room > 0) then notYetAt("checks", p); end; |
| 935 | 1223 | popTo(T); |
| 1224 | + if a.base = bReg then |
| 1225 | + len := a.hi; |
| 1226 | + else |
| 1227 | + m := place(a, U); |
| 1228 | + ml := m; ml.off := ml.off + 8; |
| 1229 | + len := 3; |
| 1230 | + if checked then loadMem(len, ml, 8, false); end; |
| 1231 | + end; |
| 1232 | + if checked then |
| 1233 | + A.cmp(T, len); |
| 1234 | + check(A.ccLO, kindOfIndex(room), p, T, len, 0, -1); |
| 1235 | + if room > 0 then |
| 1236 | + A.alu(A.opSub, Tmp, len, T); |
| 1237 | + A.cmpI(Tmp, room); |
| 1238 | + check(A.ccHI, 7, p, T, len, 0, -1); |
| 1239 | + end; |
| 1240 | + end; |
| 936 | 1241 | mulI(T, size); |
| 937 | 1242 | if a.base = bReg then |
| 938 | 1243 | A.alu(A.opAdd, T, T, a.chunk); |
| 939 | 1244 | else |
| 940 | | - m := place(a, U); |
| 941 | 1245 | loadMem(U, m, 8, false); |
| 942 | 1246 | A.alu(A.opAdd, T, T, U); |
| 943 | 1247 | end; |
| | @@ -966,20 +1270,32 @@ |
| 966 | 1270 | (* Pops hi and lo, and the address of the array or slice at a if it is |
| 967 | 1271 | on the stack, and makes the slice lo..hi of it, of elements of size |
| 968 | 1272 | bytes, in the frame at tmp: its pointer, the start plus lo times size, |
| 969 | | - and its length, hi - lo *) |
| 1273 | + and its length, hi - lo. The array has n elements. Two checks, both |
| 1274 | + unsigned, so that a negative bound fails too: hi against the length, |
| 1275 | + then lo against hi; lo is in x1 and hi in x2, as Rt.fail takes a and |
| 1276 | + b. *) |
| 970 | 1277 | proc slice*(a: Adr; isArray: BOOL; n, size, tmp: I64; checked: BOOL; p: Diag.Pos); |
| 971 | | -var m: Mem; |
| 1278 | +var m, ml: Mem; len: INT; |
| 972 | 1279 | begin |
| 973 | | - _ := n; |
| 974 | | - if checked then notYetAt("checks", p); end; |
| 975 | 1280 | popTo(Tmp); (* hi *) |
| 976 | 1281 | popTo(Src); (* lo *) |
| 977 | | - if a.base = bReg then |
| 978 | | - A.movRR(U, a.chunk); |
| 979 | | - else |
| 980 | | - m := place(a, U); |
| 981 | | - if isArray then adrTo(U, m); else loadMem(U, m, 8, false); end; |
| 1282 | + if a.base # bReg then m := place(a, U); end; |
| 1283 | + if checked then |
| 1284 | + if isArray then |
| 1285 | + len := -1; |
| 1286 | + A.cmpI(Tmp, n); |
| 1287 | + else |
| 1288 | + len := a.hi; |
| 1289 | + if a.base # bReg then len := 3; ml := m; ml.off := ml.off + 8; loadMem(len, ml, 8, false); end; |
| 1290 | + A.cmp(Tmp, len); |
| 1291 | + end; |
| 1292 | + check(A.ccLS, 1, p, Src, len, n, -1); |
| 1293 | + A.cmp(Src, Tmp); |
| 1294 | + check(A.ccLS, 1, p, Src, len, n, -1); |
| 982 | 1295 | end; |
| 1296 | + if a.base = bReg then A.movRR(U, a.chunk); |
| 1297 | + elsif isArray then adrTo(U, m); |
| 1298 | + else loadMem(U, m, 8, false); end; |
| 983 | 1299 | A.alu(A.opSub, Tmp, Tmp, Src); |
| 984 | 1300 | A.store(Tmp, FP, INT(tmp) + 8, 8); |
| 985 | 1301 | mulI(Src, size); |
| | @@ -987,10 +1303,79 @@ |
| 987 | 1303 | A.store(U, FP, INT(tmp), 8); |
| 988 | 1304 | end; |
| 989 | 1305 | |
| 1306 | +(* The x2 bytes from [x1] to [x0], n at a time while x2 holds n or more, |
| 1307 | + through x15: forward, moving x1 and x0 on after each, or back, from |
| 1308 | + the ends, moving them down first *) |
| 1309 | +proc copyRun(n: INT; back: BOOL); |
| 1310 | +var top, out: INT; |
| 1311 | +begin |
| 1312 | + top := Chunk.pc; |
| 1313 | + out := NoLabel; |
| 1314 | + A.cmpI(Tmp, n); |
| 1315 | + A.jcc(A.ccLO, out); |
| 1316 | + if back then |
| 1317 | + A.loadIdx(T, Src, -n, n, A.mPre); A.storeIdx(T, Dst, -n, n, A.mPre); |
| 1318 | + else |
| 1319 | + A.loadIdx(T, Src, n, n, A.mPost); A.storeIdx(T, Dst, n, n, A.mPost); |
| 1320 | + end; |
| 1321 | + A.aluI(A.opSub, Tmp, Tmp, n); |
| 1322 | + A.jmpBack(top); |
| 1323 | + A.fixLink(out); |
| 1324 | +end; |
| 1325 | + |
| 1326 | +(* COPY: the elements of the slice in the frame at sOff, of size bytes |
| 1327 | + each, to the start of the one at dOff, which must be at least as long |
| 1328 | + (the source may be shorter): slice 0..LEN(s) of d, checked. They go |
| 1329 | + by words, then the last few bytes one by one, forward; but back, from |
| 1330 | + the ends, when the destination starts inside the source, so that an |
| 1331 | + overlap comes out right (report 6.8): |
| 1332 | + |
| 1333 | + ldr x2, [x29, #sOff + 8] |
| 1334 | + ldr x3, [x29, #dOff + 8] |
| 1335 | + cmp x2, x3 |
| 1336 | + b.hi fail ; the source longer than the destination |
| 1337 | + ldr x1, [x29, #sOff] |
| 1338 | + ldr x0, [x29, #dOff] |
| 1339 | + cmp x0, x1 |
| 1340 | + b.ls fwd ; the destination not above: forward |
| 1341 | + add x15, x1, x2 |
| 1342 | + cmp x0, x15 |
| 1343 | + b.hs fwd ; nor inside the source |
| 1344 | + add x1, x1, x2 ; back, from the ends |
| 1345 | + add x0, x0, x2 |
| 1346 | + ... ; copyRun(8, back), copyRun(1, back) |
| 1347 | + b out |
| 1348 | + fwd: ... ; copyRun(8), copyRun(1) |
| 1349 | + out: *) |
| 990 | 1350 | proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos); |
| 1351 | +var fwd, out: INT; |
| 991 | 1352 | begin |
| 992 | | - _ := dOff; _ := sOff; _ := size; _ := checked; |
| 993 | | - notYetAt("COPY", p); |
| 1353 | + A.load(Tmp, FP, INT(sOff) + 8, 8, false); |
| 1354 | + if checked then |
| 1355 | + A.load(3, FP, INT(dOff) + 8, 8, false); |
| 1356 | + A.movRI(Src, 0); (* a, the slice's start *) |
| 1357 | + A.cmp(Tmp, 3); |
| 1358 | + check(A.ccLS, 1, p, Src, 3, 0, -1); |
| 1359 | + end; |
| 1360 | + mulI(Tmp, size); |
| 1361 | + A.load(Src, FP, INT(sOff), 8, false); |
| 1362 | + A.load(Dst, FP, INT(dOff), 8, false); |
| 1363 | + fwd := NoLabel; |
| 1364 | + A.cmp(Dst, Src); |
| 1365 | + A.jcc(A.ccLS, fwd); |
| 1366 | + A.alu(A.opAdd, T, Src, Tmp); |
| 1367 | + A.cmp(Dst, T); |
| 1368 | + A.jcc(A.ccHS, fwd); |
| 1369 | + A.alu(A.opAdd, Src, Src, Tmp); |
| 1370 | + A.alu(A.opAdd, Dst, Dst, Tmp); |
| 1371 | + copyRun(8, true); |
| 1372 | + copyRun(1, true); |
| 1373 | + out := NoLabel; |
| 1374 | + A.jmp(out); |
| 1375 | + A.fixLink(fwd); |
| 1376 | + copyRun(8, false); |
| 1377 | + copyRun(1, false); |
| 1378 | + A.fixLink(out); |
| 994 | 1379 | end; |
| 995 | 1380 | |
| 996 | 1381 | proc fillSlice*(dOff, size, n: I64; scalar: BOOL); |
| | @@ -1000,13 +1385,17 @@ |
| 1000 | 1385 | drop(); |
| 1001 | 1386 | end; |
| 1002 | 1387 | |
| 1003 | | -(* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp *) |
| 1388 | +(* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp. Its |
| 1389 | + bounds can't be known, but it can't run backwards. *) |
| 1004 | 1390 | proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos); |
| 1005 | 1391 | begin |
| 1006 | | - if checked then notYetAt("checks", p); end; |
| 1007 | 1392 | popTo(Tmp); (* hi *) |
| 1008 | 1393 | popTo(Src); (* lo *) |
| 1009 | 1394 | popTo(U); (* the pointer *) |
| 1395 | + if checked then |
| 1396 | + A.cmp(Src, Tmp); |
| 1397 | + check(A.ccLE, 3, p, Src, -1, 0, -1); |
| 1398 | + end; |
| 1010 | 1399 | A.alu(A.opSub, Tmp, Tmp, Src); |
| 1011 | 1400 | A.store(Tmp, FP, INT(tmp) + 8, 8); |
| 1012 | 1401 | mulI(Src, size); |
| | @@ -1014,36 +1403,74 @@ |
| 1014 | 1403 | A.store(U, FP, INT(tmp), 8); |
| 1015 | 1404 | end; |
| 1016 | 1405 | |
| 1406 | +(* The words the parameters of procedure type sig take: two for a |
| 1407 | + slice passed by value, one for anything else *) |
| 1408 | +proc paramWords(sig: ^Sym.Type): INT; |
| 1409 | +var q: ^Sym.Obj; n: INT; |
| 1410 | +begin |
| 1411 | + n := 0; |
| 1412 | + q := sig^.fields; |
| 1413 | + while q # nil do |
| 1414 | + if (q^.typ^.form = Sym.fSlice) and not q^.isVar then INC(n, 2); else INC(n); end; |
| 1415 | + q := q^.next; |
| 1416 | + end; |
| 1417 | + return n; |
| 1418 | +end; |
| 1419 | + |
| 1017 | 1420 | (* Calls. The arguments are the c.n values on top, each moved into the |
| 1018 | 1421 | next of x0 to x7 just before the call; the values under them are |
| 1019 | 1422 | spilled first, as the call may change their registers. As the stack's |
| 1020 | 1423 | registers carry no argument, the moves may go in any order. A slice |
| 1021 | | - goes as its two words, a record by value as the address of a copy |
| 1022 | | - (Item makes it). A single result comes back in x0, two words in x0 |
| 1023 | | - and x1: a slice is stored in the temporary c.ret, two results pushed |
| 1024 | | - as two values. *) |
| 1424 | + goes as its two words, both in registers or both on the stack, a |
| 1425 | + record by value as the address of a copy (Item makes it). The |
| 1426 | + arguments past the registers go on the machine's stack, 8 bytes |
| 1427 | + each, the first at sp, in an area sp is lowered by for the call, a |
| 1428 | + multiple of 16; and so do those past a C procedure's own parameters |
| 1429 | + where it ends in ..., in registers or not, as Apple's convention has |
| 1430 | + it (where a C procedure's own parameters past x7 narrower than 8 |
| 1431 | + bytes would be packed, which raz doesn't do). A result in memory, a |
| 1432 | + record, an array or several results, goes to the caller's temporary |
| 1433 | + c.ret, its address in x8. A single result comes back in x0, two words |
| 1434 | + in x0 and x1: a slice is stored in c.ret, two results pushed as two |
| 1435 | + values. *) |
| 1025 | 1436 | proc call*(var c: CallInfo); |
| 1026 | | -var res: ^Sym.Type; i, ni, first, r: INT; |
| 1437 | +var res: ^Sym.Type; i, ni, k, first, own, r: INT; area: I64; onStack: [64]BOOL; |
| 1027 | 1438 | begin |
| 1028 | 1439 | first := depth - c.n; |
| 1029 | 1440 | spillBelow(first); |
| 1030 | 1441 | res := c.sig^.base; |
| 1031 | 1442 | c.ret := 0; |
| 1032 | | - if c.sig^.variadic then notYet("a call of a C procedure that takes ..."); end; |
| 1033 | | - if (res # nil) and (Sym.holdsArray(res) or Sym.behindPointer(res)) then |
| 1034 | | - notYet("a result in memory"); |
| 1035 | | - c.ret := allocPerm(res^.size, res^.align); |
| 1036 | | - elsif (res # nil) and (res^.form = Sym.fSlice) then |
| 1443 | + if (res # nil) and Sym.holdsArray(res) then |
| 1444 | + c.ret := allocPerm(res^.size, res^.align); (* kept, as on x86-64 (report 8) *) |
| 1445 | + elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then |
| 1037 | 1446 | c.ret := allocTemp(res^.size, res^.align); |
| 1038 | 1447 | elsif (res # nil) and (res^.form = Sym.fReal) then |
| 1039 | 1448 | notYet("reals"); |
| 1040 | 1449 | end; |
| 1041 | | - ni := 0; |
| 1450 | + own := c.n; |
| 1451 | + if c.sig^.variadic then own := paramWords(c.sig); end; |
| 1452 | + ni := 0; k := 0; |
| 1042 | 1453 | for i := 0 to c.n - 1 do |
| 1043 | 1454 | if c.isF[i] then notYet("reals"); end; |
| 1044 | | - if ni < 8 then valueTo(first + i, ni); else notYet("an argument past x7"); end; |
| 1045 | | - INC(ni); |
| 1455 | + if i >= own then onStack[i] := true; |
| 1456 | + elsif (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]; |
| 1457 | + else onStack[i] := ni + 1 + INT(c.pair[i]) > 8; end; |
| 1458 | + if onStack[i] then INC(k); else INC(ni); end; |
| 1046 | 1459 | end; |
| 1460 | + area := Sym.alignUp(8 * k, 16); |
| 1461 | + if area > 0 then A.aluI(A.opSub, SP, SP, area); end; |
| 1462 | + k := 0; ni := 0; |
| 1463 | + for i := 0 to c.n - 1 do |
| 1464 | + if onStack[i] then |
| 1465 | + valueTo(first + i, T); |
| 1466 | + A.store(T, SP, 8 * k, 8); |
| 1467 | + INC(k); |
| 1468 | + else |
| 1469 | + valueTo(first + i, ni); |
| 1470 | + INC(ni); |
| 1471 | + end; |
| 1472 | + end; |
| 1473 | + if (c.ret # 0) and Sym.behindPointer(res) then A.aluI(A.opAdd, 8, FP, c.ret); end; |
| 1047 | 1474 | for i := 1 to c.n do drop(); end; |
| 1048 | 1475 | if c.ext then |
| 1049 | 1476 | A.loadChunk(U, c.chunk, 0, 8, false); |
| | @@ -1054,6 +1481,7 @@ |
| 1054 | 1481 | A.load(U, FP, INT(c.target), 8, false); |
| 1055 | 1482 | A.callR(U); |
| 1056 | 1483 | end; |
| 1484 | + if area > 0 then A.aluI(A.opAdd, SP, SP, area); end; |
| 1057 | 1485 | if res # nil then |
| 1058 | 1486 | if res^.form = Sym.fSlice then |
| 1059 | 1487 | A.store(0, FP, INT(c.ret), 8); |
| | @@ -1109,10 +1537,12 @@ |
| 1109 | 1537 | end; |
| 1110 | 1538 | |
| 1111 | 1539 | (* The parameters' places: each in a frame slot of its own, stored there |
| 1112 | | - from its register on entry. A var parameter, and a record or array |
| 1113 | | - passed by value, holds an address (ind). *) |
| 1540 | + from its register on entry, or above the frame where the caller put |
| 1541 | + it, past the frame record (call). A var parameter, and a record or |
| 1542 | + array passed by value, holds an address (ind). The address of a |
| 1543 | + result in memory arrives in x8, and is kept in retSlot. *) |
| 1114 | 1544 | proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj); |
| 1115 | | -var p: ^Sym.Obj; ni, k, w: INT; |
| 1545 | +var p: ^Sym.Obj; ni, k, w: INT; stackOff: I64; |
| 1116 | 1546 | begin |
| 1117 | 1547 | frameTop := 0; frameMax := 0; permTop := 0; |
| 1118 | 1548 | resetStack(0, 0); |
| | @@ -1120,21 +1550,30 @@ |
| 1120 | 1550 | numIn := 0; |
| 1121 | 1551 | lost := false; |
| 1122 | 1552 | resType := sig^.base; |
| 1553 | + procParams := params; |
| 1123 | 1554 | regMask := 0; |
| 1124 | 1555 | if (resType # nil) and Sym.behindPointer(resType) then |
| 1125 | | - notYet("a result in memory"); |
| 1126 | 1556 | retSlot := allocPerm(8, 8); |
| 1557 | + inReg[0] := 8; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; |
| 1558 | + numIn := 1; |
| 1127 | 1559 | end; |
| 1128 | 1560 | ni := 0; |
| 1561 | + stackOff := 16; |
| 1129 | 1562 | p := params; |
| 1130 | 1563 | while p # nil do |
| 1131 | 1564 | p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice)); |
| 1132 | 1565 | k := 1; |
| 1133 | 1566 | if (p^.typ^.form = Sym.fSlice) and not p^.isVar then k := 2; end; |
| 1134 | | - p^.val := allocPerm(8 * k, 8); |
| 1135 | | - if (p^.typ^.form = Sym.fReal) and not p^.isVar or (ni + k > 8) then |
| 1136 | | - notYet("a real parameter, or one past x7"); |
| 1567 | + if (p^.typ^.form = Sym.fReal) and not p^.isVar then |
| 1568 | + notYet("a real parameter"); |
| 1569 | + p^.val := allocPerm(8, 8); |
| 1137 | 1570 | k := 0; |
| 1571 | + elsif ni + k > 8 then (* on the stack, as call puts it *) |
| 1572 | + p^.val := stackOff; |
| 1573 | + INC(stackOff, 8 * k); |
| 1574 | + k := 0; |
| 1575 | + else |
| 1576 | + p^.val := allocPerm(8 * k, 8); |
| 1138 | 1577 | end; |
| 1139 | 1578 | for w := 0 to k - 1 do (* a slice's two words, in two slots *) |
| 1140 | 1579 | inReg[numIn] := ni; inSlot[numIn] := p^.val + I64(8 * w); inObj[numIn] := p; |
| | @@ -1191,7 +1630,7 @@ |
| 1191 | 1630 | proc markCode*(var m: CodeMark); |
| 1192 | 1631 | begin |
| 1193 | 1632 | m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := A.saidCount(); |
| 1194 | | - m.fails := 0; m.exit := exitL; |
| 1633 | + m.fails := numFails; m.exit := exitL; |
| 1195 | 1634 | end; |
| 1196 | 1635 | |
| 1197 | 1636 | (* The code made since m taken back; the frame is left as it is *) |
| | @@ -1199,6 +1638,7 @@ |
| 1199 | 1638 | begin |
| 1200 | 1639 | Chunk.rewindTo(m.pc, m.fixups, m.lines); |
| 1201 | 1640 | A.unsay(m.said); |
| 1641 | + numFails := m.fails; |
| 1202 | 1642 | exitL := m.exit; |
| 1203 | 1643 | end; |
| 1204 | 1644 | |
| | @@ -1208,6 +1648,7 @@ |
| 1208 | 1648 | begin |
| 1209 | 1649 | Chunk.rewindTo(m.pc, m.fixups, m.lines); |
| 1210 | 1650 | A.unsay(m.said); |
| 1651 | + numFails := 0; |
| 1211 | 1652 | frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; |
| 1212 | 1653 | exitL := NoLabel; |
| 1213 | 1654 | end; |
| | @@ -1222,7 +1663,7 @@ |
| 1222 | 1663 | parameters stored from their registers to their slots, or moved to |
| 1223 | 1664 | their registers. *) |
| 1224 | 1665 | proc enterBody*(c, line: INT); |
| 1225 | | -var i, loop, r: INT; n: I64; |
| 1666 | +var i, loop, r: INT; n: I64; p: ^Sym.Obj; |
| 1226 | 1667 | begin |
| 1227 | 1668 | Chunk.beginChunk(c); |
| 1228 | 1669 | codeChunk := c; |
| | @@ -1255,7 +1696,7 @@ |
| 1255 | 1696 | end; |
| 1256 | 1697 | end; |
| 1257 | 1698 | for i := 0 to numIn - 1 do |
| 1258 | | - if inObj[i]^.reg # 0 then |
| 1699 | + if (inObj[i] # nil) and (inObj[i]^.reg # 0) then |
| 1259 | 1700 | r := inObj[i]^.reg; |
| 1260 | 1701 | if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end; |
| 1261 | 1702 | A.movRR(r, inReg[i]); |
| | @@ -1264,6 +1705,21 @@ |
| 1264 | 1705 | A.store(inReg[i], FP, INT(inSlot[i]), inSize[i]); |
| 1265 | 1706 | end; |
| 1266 | 1707 | end; |
| 1708 | + (* a register variable's parameter that came on the stack, above the |
| 1709 | + frame (val > 0), is loaded from there: one passed by its address |
| 1710 | + (ind) is the address, 8 bytes, whatever its type *) |
| 1711 | + p := procParams; |
| 1712 | + while p # nil do |
| 1713 | + if (p^.reg # 0) and (p^.val > 0) then |
| 1714 | + if not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then |
| 1715 | + A.load(p^.reg, FP, INT(p^.val), INT(p^.typ^.size), p^.typ^.signed); |
| 1716 | + else |
| 1717 | + A.load(p^.reg, FP, INT(p^.val), 8, false); |
| 1718 | + if not p^.ind and (p^.typ^.form = Sym.fSlice) then A.load(p^.reg2, FP, INT(p^.val) + 8, 8, false); end; |
| 1719 | + end; |
| 1720 | + end; |
| 1721 | + p := p^.next; |
| 1722 | + end; |
| 1267 | 1723 | end; |
| 1268 | 1724 | |
| 1269 | 1725 | proc resultSlot*: I64; |
| | @@ -1271,27 +1727,32 @@ |
| 1271 | 1727 | return retSlot; |
| 1272 | 1728 | end; |
| 1273 | 1729 | |
| 1730 | +(* Pops one of several results, a value of size bytes, into its place |
| 1731 | + off bytes into the caller's, at the address that came in x8 *) |
| 1274 | 1732 | proc resultTo*(off: I64; size: INT); |
| 1733 | +var r: INT; |
| 1275 | 1734 | begin |
| 1276 | | - _ := off; _ := size; |
| 1277 | | - notYet("a result in memory"); |
| 1735 | + r := top(); |
| 1278 | 1736 | drop(); |
| 1737 | + A.load(U, FP, INT(retSlot), 8, false); |
| 1738 | + A.store(r, U, INT(off), size); |
| 1279 | 1739 | end; |
| 1280 | 1740 | |
| 1741 | +(* Pops an address, and copies size bytes from it to the result, off |
| 1742 | + bytes into it *) |
| 1281 | 1743 | proc copyResult*(off, size: I64); |
| 1282 | 1744 | begin |
| 1283 | | - _ := off; _ := size; |
| 1284 | | - notYet("a result in memory"); |
| 1285 | | - drop(); |
| 1745 | + popTo(Src); |
| 1746 | + A.load(Dst, FP, INT(retSlot), 8, false); |
| 1747 | + if off # 0 then A.aluI(A.opAdd, Dst, Dst, off); end; |
| 1748 | + copyBytes(size); |
| 1286 | 1749 | end; |
| 1287 | 1750 | |
| 1288 | 1751 | (* return: the result, a value on top, goes in x0; two words, the two |
| 1289 | | - values on top, in x0 and x1 *) |
| 1752 | + values on top, in x0 and x1; one in memory is there already *) |
| 1290 | 1753 | proc returnJump*; |
| 1291 | 1754 | begin |
| 1292 | | - if (resType # nil) and Sym.behindPointer(resType) then |
| 1293 | | - A.load(0, FP, INT(retSlot), 8, false); (* not yet made: marked by beginProc *) |
| 1294 | | - elsif resType # nil then |
| 1755 | + if (resType # nil) and not Sym.behindPointer(resType) then |
| 1295 | 1756 | if Sym.inTwoRegs(resType) then popTo(1); end; |
| 1296 | 1757 | popTo(0); |
| 1297 | 1758 | end; |
| | @@ -1304,10 +1765,35 @@ |
| 1304 | 1765 | begin |
| 1305 | 1766 | end; |
| 1306 | 1767 | |
| 1768 | +(* The procedure returns the error in register r, and a zero for each |
| 1769 | + of its other results; r isn't x0, x1 or x2, which this uses. *) |
| 1770 | +proc returnError(r: INT); |
| 1771 | +var f: ^Sym.Obj; |
| 1772 | +begin |
| 1773 | + if Sym.behindPointer(resType) then |
| 1774 | + A.load(Dst, FP, INT(retSlot), 8, false); |
| 1775 | + zeroBytes(resType^.size); |
| 1776 | + f := resType^.fields; |
| 1777 | + while f^.next # nil do f := f^.next; end; |
| 1778 | + A.load(Dst, FP, INT(retSlot), 8, false); |
| 1779 | + A.store(r, Dst, INT(f^.val), 8); |
| 1780 | + elsif Sym.inTwoRegs(resType) then |
| 1781 | + A.movRR(1, r); |
| 1782 | + A.movRI(0, 0); |
| 1783 | + else |
| 1784 | + A.movRR(0, r); |
| 1785 | + end; |
| 1786 | + A.jmp(exitL); |
| 1787 | +end; |
| 1788 | + |
| 1789 | +(* The rest of what try does where the error isn't nil: the procedure |
| 1790 | + returns the error in the frame slot. Only code is made: the stack of |
| 1791 | + values is left as it is, for the code that follows, where the error |
| 1792 | + is nil. *) |
| 1307 | 1793 | proc tryReturn*(slot: I64); |
| 1308 | 1794 | begin |
| 1309 | | - _ := slot; |
| 1310 | | - notYet("try"); |
| 1795 | + A.load(U, FP, INT(slot), 8, false); |
| 1796 | + returnError(U); |
| 1311 | 1797 | end; |
| 1312 | 1798 | |
| 1313 | 1799 | (* try, with no deferred statement to run: the error on top is tested in |
| | @@ -1323,8 +1809,7 @@ |
| 1323 | 1809 | |
| 1324 | 1810 | proc tryReturnFrom*(r: INT); |
| 1325 | 1811 | begin |
| 1326 | | - _ := r; |
| 1327 | | - notYet("try"); |
| 1812 | + returnError(r); |
| 1328 | 1813 | end; |
| 1329 | 1814 | |
| 1330 | 1815 | (* The end of the procedure in chunk c: the code that returns, and the |
| | @@ -1335,7 +1820,10 @@ |
| 1335 | 1820 | begin |
| 1336 | 1821 | codeChunk := -1; |
| 1337 | 1822 | if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end; |
| 1338 | | - if open then |
| 1823 | + if open and Sym.behindPointer(resType) then |
| 1824 | + A.load(Dst, FP, INT(retSlot), 8, false); |
| 1825 | + zeroBytes(resType^.size); |
| 1826 | + elsif open then |
| 1339 | 1827 | A.movRI(0, 0); |
| 1340 | 1828 | if Sym.inTwoRegs(resType) then A.movRI(1, 0); end; |
| 1341 | 1829 | else |
| | @@ -1352,6 +1840,7 @@ |
| 1352 | 1840 | A.movRR(SP, FP); |
| 1353 | 1841 | A.ldp(FP, LR, SP, 16, A.mPost); |
| 1354 | 1842 | A.ret(); |
| 1843 | + endFails(); |
| 1355 | 1844 | n := Sym.alignUp(frameMax, 16); |
| 1356 | 1845 | if n >= 1000000H then return Scan.mark("frame too big: 16 MB or more"); end; |
| 1357 | 1846 | Chunk.put4At(frameFix, SubSp12 | INT((n shr 12) shl 10)); |
| | @@ -1364,29 +1853,40 @@ |
| 1364 | 1853 | return nil; |
| 1365 | 1854 | end; |
| 1366 | 1855 | |
| 1856 | +(* A deferred statement is compiled where it stands, as a subroutine of |
| 1857 | + its procedure that the code jumps over, in the procedure's own frame |
| 1858 | + (the same x29), so that it sees all its variables; deferBegin gives |
| 1859 | + its address and what pushed was. bl, which runs it, leaves its return |
| 1860 | + address in x30, which its own calls would change: so it keeps x30 on |
| 1861 | + the machine's stack, 16 bytes, so that sp stays a multiple of 16: |
| 1862 | + |
| 1863 | + b over |
| 1864 | + at: str x30, [sp, #-16]! |
| 1865 | + ... ; the statement |
| 1866 | + ldr x30, [sp], #16 |
| 1867 | + ret |
| 1868 | + over: *) |
| 1367 | 1869 | proc deferBegin*(var L: INT): (INT, INT); |
| 1870 | +var at: INT; |
| 1368 | 1871 | begin |
| 1369 | | - _ := L; |
| 1370 | | - notYet("defer"); |
| 1371 | | - return Chunk.pc, pushed; |
| 1872 | + A.jmp(L); |
| 1873 | + at := Chunk.pc; |
| 1874 | + A.storeIdx(LR, SP, -16, 8, A.mPre); |
| 1875 | + return at, pushed; |
| 1372 | 1876 | end; |
| 1373 | 1877 | |
| 1374 | 1878 | proc deferEnd*(L, saved: INT); |
| 1375 | 1879 | begin |
| 1376 | | - _ := L; _ := saved; |
| 1377 | 1880 | keepTemps(); |
| 1881 | + A.loadIdx(LR, SP, 16, 8, A.mPost); |
| 1882 | + A.ret(); |
| 1883 | + A.fixLink(L); |
| 1884 | + pushed := saved; |
| 1378 | 1885 | end; |
| 1379 | 1886 | |
| 1380 | 1887 | proc runDefer*(at: INT); |
| 1381 | 1888 | begin |
| 1382 | | - _ := at; |
| 1383 | | - notYet("defer"); |
| 1384 | | -end; |
| 1385 | | - |
| 1386 | | -(* The name s, interned *) |
| 1387 | | -proc internStr(s: []const U8): INT; |
| 1388 | | -begin |
| 1389 | | - return Scan.intern(SYSTEM.PTR(s), LEN(s)); |
| 1889 | + A.callBack(at); |
| 1390 | 1890 | end; |
| 1391 | 1891 | |
| 1392 | 1892 | (* The entry point, main, and its chunk. macOS starts every program in |
| | @@ -1428,6 +1928,29 @@ |
| 1428 | 1928 | A.callChunk(exitProc); |
| 1429 | 1929 | A.brk(1); (* exit never returns: trap if it does *) |
| 1430 | 1930 | Chunk.endChunk(entry); |
| 1931 | + (* Rt.fail, for the failures of each kind, given a, b, len and name |
| 1932 | + by the check's code (endFails), which called the stub by bl: the |
| 1933 | + place is that bl's, 4 bytes before the return address. Rt.fail's |
| 1934 | + last two arguments, the registers x86-64's trap report shows, are |
| 1935 | + none here. |
| 1936 | + |
| 1937 | + sub x4, x30, #4 |
| 1938 | + mov x0, #kind |
| 1939 | + mov x6, #0 |
| 1940 | + mov x7, #0 |
| 1941 | + bl Rt.fail *) |
| 1942 | + for i := 0 to LEN(failStubs) - 1 do |
| 1943 | + if failStubs[i] >= 0 then |
| 1944 | + Chunk.beginChunk(failStubs[i]); |
| 1945 | + A.aluI(A.opSub, 4, LR, 4); |
| 1946 | + A.movRI(0, i); |
| 1947 | + A.movRI(6, 0); |
| 1948 | + A.movRI(7, 0); |
| 1949 | + A.callChunk(rtFail); |
| 1950 | + A.brk(1); |
| 1951 | + Chunk.endChunk(failStubs[i]); |
| 1952 | + end; |
| 1953 | + end; |
| 1431 | 1954 | return entry; |
| 1432 | 1955 | end; |
| 1433 | 1956 | |
| | @@ -1455,7 +1978,9 @@ |
| 1455 | 1978 | notYet("reals"); |
| 1456 | 1979 | end; |
| 1457 | 1980 | |
| 1981 | +var i: INT; |
| 1458 | 1982 | begin |
| 1983 | + for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; |
| 1459 | 1984 | Chunk.setPad(0D4200020H); (* brk #1, a trap, between procedures *) |
| 1460 | 1985 | codeChunk := -1; |
| 1461 | 1986 | pending := ""; |