| | @@ -53,6 +53,17 @@ |
| 53 | 53 | at: Diag.Pos; (* the import *) |
| 54 | 54 | end; |
| 55 | 55 | |
| 56 | + (* A type a type section names before it declares it (report, 4): |
| 57 | + a pointer's base, or a procedure type's parameter or result. A |
| 58 | + placeholder stands for it in its owner until the section's end, |
| 59 | + when resolveLaters puts the type declared in its place. *) |
| 60 | + Later = record |
| 61 | + name: INT; (* the name of the type *) |
| 62 | + at: Diag.Pos; (* where it is named *) |
| 63 | + held: PType; (* the placeholder, a type of form fNone *) |
| 64 | + owner: PType; (* the pointer or procedure type that holds it *) |
| 65 | + end; |
| 66 | + |
| 56 | 67 | const |
| 57 | 68 | (* Whether the target has reals (report, appendix A): x86-64 always has |
| 58 | 69 | SSE2. Without them, used code that computes with one is an error, |
| | @@ -60,7 +71,7 @@ |
| 60 | 71 | HasReals* = true; |
| 61 | 72 | |
| 62 | 73 | MaxNames = 64; (* names in one declaration, and the like *) |
| 63 | | - MaxFwd = 256; (* pointers to types not yet declared *) |
| 74 | + MaxLater = 256; (* types named before they are declared *) |
| 64 | 75 | MaxDefers = 64; (* defer statements in force *) |
| 65 | 76 | MaxDoubts = 4096; (* places to warn of, noted while a module is read *) |
| 66 | 77 | MaxDeferred = 64; (* generic imports not yet made *) |
| | @@ -81,10 +92,9 @@ |
| 81 | 92 | firstModule*, lastModule*: PModule; (* in the order they were finished *) |
| 82 | 93 | |
| 83 | 94 | checkMode: BOOL; (* reading a file only for its interface *) |
| 84 | | - inTypes: BOOL; (* in a type section: ^T may name a later type *) |
| 85 | | - fwdTypes: [MaxFwd]PType; |
| 86 | | - fwdPos: [MaxFwd]Diag.Pos; (* where each names its type *) |
| 87 | | - numFwd: INT; |
| 95 | + inTypes: BOOL; (* in a type section: ^T, and a procedure type, may name a later type *) |
| 96 | + laters: [MaxLater]Later; |
| 97 | + numLater: INT; |
| 88 | 98 | lastCalled: PObj; (* the procedure the last call was of, if it had a name *) |
| 89 | 99 | passOn: BOOL; (* the next factor may be a call with several results, for return *) |
| 90 | 100 | curProc: PObj; (* the procedure being compiled, or nil *) |
| | @@ -600,6 +610,86 @@ |
| 600 | 610 | |
| 601 | 611 | (* Types *) |
| 602 | 612 | |
| 613 | +(* Lays the results t holds out as C would, each field at its offset; |
| 614 | + again by resolveLaters, if one names a type declared later. *) |
| 615 | +proc layOut(t: PType); |
| 616 | +var f: PObj; off: I64; |
| 617 | +begin |
| 618 | + off := 0; |
| 619 | + t^.align := 1; |
| 620 | + f := t^.fields; |
| 621 | + while f # nil do |
| 622 | + off := Sym.alignUp(off, f^.typ^.align); |
| 623 | + f^.val := off; |
| 624 | + off := off + f^.typ^.size; |
| 625 | + if f^.typ^.align > t^.align then t^.align := f^.typ^.align; end; |
| 626 | + f := f^.next; |
| 627 | + end; |
| 628 | + t^.size := Sym.alignUp(off, t^.align); |
| 629 | +end; |
| 630 | + |
| 631 | +(* A type that owner, a pointer or a procedure type, holds. In a type |
| 632 | + section it may name a type not declared yet (report, 4): a |
| 633 | + placeholder of its own stands for it, noted in laters, and |
| 634 | + resolveLaters puts the type in its place at the section's end. A |
| 635 | + pointer is a word and a procedure value an address, so neither's |
| 636 | + size waits for it. *) |
| 637 | +proc laterType(owner: PType): (PType, ERROR); |
| 638 | +var held: PType; |
| 639 | +begin |
| 640 | + if not inTypes or (Scan.tok # Scan.sIdent) or (Sym.lookup(Scan.id) # nil) then return parseType(); end; |
| 641 | + if numLater >= MaxLater then return nil, Scan.mark(tooMany(MaxLater, "types named before they are declared")); end; |
| 642 | + held := Sym.newType(Sym.fNone, 0, 1); |
| 643 | + laters[numLater].name := Scan.id; |
| 644 | + laters[numLater].at := Scan.tokPos(); |
| 645 | + laters[numLater].held := held; |
| 646 | + laters[numLater].owner := owner; |
| 647 | + INC(numLater); |
| 648 | + Scan.next; |
| 649 | + return held, nil; |
| 650 | +end; |
| 651 | + |
| 652 | +(* Gives t to each of the list f whose type is the placeholder held *) |
| 653 | +proc putFor(f: PObj; held, t: PType); |
| 654 | +begin |
| 655 | + while f # nil do |
| 656 | + if f^.typ = held then f^.typ := t; end; |
| 657 | + f := f^.next; |
| 658 | + end; |
| 659 | +end; |
| 660 | + |
| 661 | +(* Puts in place of each placeholder of laters the type it names, now |
| 662 | + that the type section is read: in a pointer's base, a procedure |
| 663 | + type's parameters and result, and its results, laid out again as |
| 664 | + their sizes are now known. A name that isn't a type is poison. *) |
| 665 | +proc resolveLaters; |
| 666 | +var i: INT; o: PObj; held, t, owner: PType; |
| 667 | +begin |
| 668 | + for i := 0 to numLater - 1 do |
| 669 | + held := laters[i].held; |
| 670 | + owner := laters[i].owner; |
| 671 | + o := Sym.lookup(laters[i].name); |
| 672 | + if (o # nil) and (o^.typ = Sym.tPoison) then |
| 673 | + t := Sym.tPoison; (* its declaration had an error, told already *) |
| 674 | + elsif (o = nil) or (o^.cls # Sym.oType) then |
| 675 | + notDeclared(laters[i].at, laters[i].name); |
| 676 | + t := Sym.tPoison; |
| 677 | + else |
| 678 | + t := o^.typ; |
| 679 | + named(o); |
| 680 | + end; |
| 681 | + if owner^.base = held then owner^.base := t; end; |
| 682 | + if owner^.form = Sym.fProc then |
| 683 | + putFor(owner^.fields, held, t); |
| 684 | + if (owner^.base # nil) and owner^.base^.tuple then |
| 685 | + putFor(owner^.base^.fields, held, t); |
| 686 | + layOut(owner^.base); |
| 687 | + end; |
| 688 | + end; |
| 689 | + end; |
| 690 | + numLater := 0; |
| 691 | +end; |
| 692 | + |
| 603 | 693 | proc recordType: (PType, ERROR); |
| 604 | 694 | var |
| 605 | 695 | t, ft: PType; f, last: PObj; names: [MaxNames]INT; |
| | @@ -699,23 +789,19 @@ |
| 699 | 789 | return t, nil; |
| 700 | 790 | end; |
| 701 | 791 | |
| 702 | | -(* Several results: a record of them, laid out as C would, and |
| 703 | | - returned in rax and rdx if they are two words (inTwoRegs), or else |
| 704 | | - through a hidden pointer. *) |
| 705 | | -proc results: (PType, ERROR); |
| 706 | | -var t, ft: PType; f, last: PObj; off: I64; |
| 792 | +(* The results of the procedure type sig: a record of them, laid out |
| 793 | + as C would, and returned in rax and rdx if they are two words |
| 794 | + (inTwoRegs), or else through a hidden pointer. One alone in |
| 795 | + parentheses is the result itself. *) |
| 796 | +proc results(sig: PType): (PType, ERROR); |
| 797 | +var t: PType; f, last: PObj; |
| 707 | 798 | begin |
| 708 | 799 | Scan.next; |
| 709 | 800 | t := Sym.newType(Sym.fRecord, 0, 1); |
| 710 | 801 | t^.tuple := true; |
| 711 | | - off := 0; last := nil; |
| 802 | + last := nil; |
| 712 | 803 | repeat |
| 713 | | - ft := try parseType(); |
| 714 | | - off := Sym.alignUp(off, ft^.align); |
| 715 | | - f := Sym.newObj(Sym.oField, -1, ft); |
| 716 | | - f^.val := off; |
| 717 | | - off := off + ft^.size; |
| 718 | | - if ft^.align > t^.align then t^.align := ft^.align; end; |
| 804 | + f := Sym.newObj(Sym.oField, -1, try laterType(sig)); |
| 719 | 805 | if last = nil then t^.fields := f; else last^.next := f; end; |
| 720 | 806 | last := f; |
| 721 | 807 | INC(t^.len); |
| | @@ -723,8 +809,8 @@ |
| 723 | 809 | Scan.next; |
| 724 | 810 | until false; |
| 725 | 811 | try expect(Scan.sRparen); |
| 726 | | - t^.size := Sym.alignUp(off, t^.align); |
| 727 | 812 | if t^.len = 1 then return t^.fields^.typ, nil; end; |
| 813 | + layOut(t); |
| 728 | 814 | return t, nil; |
| 729 | 815 | end; |
| 730 | 816 | |
| | @@ -762,7 +848,7 @@ |
| 762 | 848 | Scan.next; |
| 763 | 849 | until false; |
| 764 | 850 | try expect(Scan.sColon); |
| 765 | | - pt := try parseType(); |
| 851 | + pt := try laterType(t); |
| 766 | 852 | while first # nil do first^.typ := pt; first := first^.next; end; |
| 767 | 853 | if Scan.tok # Scan.sSemicolon then break; end; |
| 768 | 854 | Scan.next; |
| | @@ -773,7 +859,7 @@ |
| 773 | 859 | t^.len := n; |
| 774 | 860 | if Scan.tok = Scan.sColon then |
| 775 | 861 | Scan.next; |
| 776 | | - if Scan.tok = Scan.sLparen then t^.base := try results(); else t^.base := try parseType(); end; |
| 862 | + if Scan.tok = Scan.sLparen then t^.base := try results(t); else t^.base := try laterType(t); end; |
| 777 | 863 | end; |
| 778 | 864 | return t, nil; |
| 779 | 865 | end; |
| | @@ -854,20 +940,8 @@ |
| 854 | 940 | Scan.next; |
| 855 | 941 | vol := Scan.tok = Scan.sVolatile; |
| 856 | 942 | if vol then Scan.next; end; |
| 857 | | - if inTypes and (Scan.tok = Scan.sIdent) and (Sym.lookup(Scan.id) = nil) then |
| 858 | | - (* a type declared later in this section *) |
| 859 | | - t := newPtr(nil, vol); |
| 860 | | - t^.fwd := Scan.id; |
| 861 | | - if numFwd >= MaxFwd then |
| 862 | | - return nil, Scan.mark(tooMany(MaxFwd, "pointers to types not yet declared")); |
| 863 | | - end; |
| 864 | | - fwdTypes[numFwd] := t; |
| 865 | | - fwdPos[numFwd] := Scan.tokPos(); |
| 866 | | - INC(numFwd); |
| 867 | | - Scan.next; |
| 868 | | - else |
| 869 | | - t := newPtr(try parseType(), vol); |
| 870 | | - end; |
| 943 | + t := newPtr(nil, vol); |
| 944 | + t^.base := try laterType(t); |
| 871 | 945 | | sPacked, sRecord: t := try recordType(); |
| 872 | 946 | | sVariant: t := try variantType(); |
| 873 | 947 | | sProc: Scan.next; t := try signature(); |
| | @@ -3765,7 +3839,7 @@ |
| 3765 | 3839 | end; |
| 3766 | 3840 | |
| 3767 | 3841 | proc declarations(top: BOOL); |
| 3768 | | -var i: INT; o: PObj; k: Scan.Sym; |
| 3842 | +var k: Scan.Sym; |
| 3769 | 3843 | begin |
| 3770 | 3844 | repeat |
| 3771 | 3845 | k := Scan.tok; |
| | @@ -3775,20 +3849,10 @@ |
| 3775 | 3849 | while Scan.tok = Scan.sIdent do declaration(k, top); end; |
| 3776 | 3850 | | sType: |
| 3777 | 3851 | Scan.next; |
| 3778 | | - numFwd := 0; |
| 3852 | + numLater := 0; |
| 3779 | 3853 | while Scan.tok = Scan.sIdent do declaration(Scan.sType, top); end; |
| 3780 | 3854 | inTypes := false; |
| 3781 | | - for i := 0 to numFwd - 1 do |
| 3782 | | - o := Sym.lookup(fwdTypes[i]^.fwd); |
| 3783 | | - if (o = nil) or (o^.cls # Sym.oType) then |
| 3784 | | - notDeclared(fwdPos[i], fwdTypes[i]^.fwd); |
| 3785 | | - fwdTypes[i]^.base := Sym.tPoison; |
| 3786 | | - else |
| 3787 | | - fwdTypes[i]^.base := o^.typ; |
| 3788 | | - named(o); |
| 3789 | | - end; |
| 3790 | | - fwdTypes[i]^.fwd := -1; |
| 3791 | | - end; |
| 3855 | + resolveLaters(); |
| 3792 | 3856 | | sProc, sExtern: declaration(k, top); |
| 3793 | 3857 | | else: return; |
| 3794 | 3858 | end; |
| | @@ -4420,7 +4484,7 @@ |
| 4420 | 4484 | proc makeInstance(i: INT; at: Diag.Pos): ERROR; |
| 4421 | 4485 | var |
| 4422 | 4486 | d: Deferred; st: Scan.State; args: Sym.Args; types, exported, ext, wasQuiet: BOOL; nf, nd: INT; |
| 4423 | | - ft: [MaxFwd]PType; fp: [MaxFwd]Diag.Pos; decl: [MaxNames]INT; |
| 4487 | + lt: [MaxLater]Later; decl: [MaxNames]INT; |
| 4424 | 4488 | begin |
| 4425 | 4489 | d := deferred[i]; |
| 4426 | 4490 | DEC(numDeferred); |
| | @@ -4434,10 +4498,10 @@ |
| 4434 | 4498 | Scan.restoreScan(st); |
| 4435 | 4499 | args.at := d.at; |
| 4436 | 4500 | types := inTypes; exported := declExported; ext := inExtern; wasQuiet := Diag.quiet; |
| 4437 | | - nf := numFwd; ft := fwdTypes; fp := fwdPos; nd := numDeclaring; decl := declaring; |
| 4501 | + nf := numLater; lt := laters; nd := numDeclaring; decl := declaring; |
| 4438 | 4502 | d.o^.home := try importModule(d.mname, args); |
| 4439 | 4503 | inTypes := types; declExported := exported; inExtern := ext; Diag.setQuiet(wasQuiet); |
| 4440 | | - numFwd := nf; fwdTypes := ft; fwdPos := fp; numDeclaring := nd; declaring := decl; |
| 4504 | + numLater := nf; laters := lt; numDeclaring := nd; declaring := decl; |
| 4441 | 4505 | addImport(Sym.curModule, d.o^.home); |
| 4442 | 4506 | return nil; |
| 4443 | 4507 | end; |