| | @@ -67,6 +67,7 @@ |
| 67 | 67 | MaxElements = 1024; (* elements of an array literal *) |
| 68 | 68 | MaxFixes = 4096; (* addresses in one constant *) |
| 69 | 69 | MaxPending = 64; (* modules being compiled at once *) |
| 70 | + MaxFors = 256; (* for loops of a procedure whose limit may go in a register *) |
| 70 | 71 | Suffixes: [4][]U8 = [".linux-x86_64", ".linux", ".x86_64", ""]; |
| 71 | 72 | |
| 72 | 73 | var |
| | @@ -113,6 +114,13 @@ |
| 113 | 114 | their end: each way through them returns *) |
| 114 | 115 | useAt: Diag.Pos; (* ... which is here *) |
| 115 | 116 | guards: INT; (* the recovery points open *) |
| 117 | + (* A variable register no variable took, for the limits of the |
| 118 | + procedure's innermost for loops that aren't constant: compared |
| 119 | + each round, they would be read from the frame. limitK is its place |
| 120 | + in VarRegs, or -1; limitIn says which for loops, by their order |
| 121 | + in the procedure (chooseRegs); numFors counts them as compiled. *) |
| 122 | + limitK, numFors: INT; |
| 123 | + limitIn: [MaxFors]BOOL; |
| 116 | 124 | |
| 117 | 125 | proc expression(var x: TItem; want: PType): ERROR; forward; |
| 118 | 126 | proc parseType: (PType, ERROR); forward; |
| | @@ -1713,9 +1721,11 @@ |
| 1713 | 1721 | so that a round of a loop by 1 takes one compare and one jump. *) |
| 1714 | 1722 | proc forStatement(start: Diag.Pos; ind: INT): ERROR; |
| 1715 | 1723 | var |
| 1716 | | - o: PObj; v, a, b, s: TItem; step: I64; top, body, bodyL, exitL: INT; |
| 1724 | + o, lim: PObj; v, a, b, s: TItem; step: I64; top, body, bodyL, exitL, n: INT; |
| 1717 | 1725 | lp: Loop; old: PLoop; f: Sym.Form; slot: I64; |
| 1718 | 1726 | begin |
| 1727 | + n := numFors; |
| 1728 | + INC(numFors); |
| 1719 | 1729 | Scan.next; |
| 1720 | 1730 | if Scan.tok # Scan.sIdent then return expected("a name"); end; |
| 1721 | 1731 | o := Sym.lookup(Scan.id); |
| | @@ -1738,8 +1748,17 @@ |
| 1738 | 1748 | try expect(Scan.sTo); |
| 1739 | 1749 | try expression(b, o^.typ); |
| 1740 | 1750 | try Item.coerceExact(b, o^.typ, ""); |
| 1741 | | - (* the limit, evaluated once: a constant as it is, else in a slot *) |
| 1742 | | - if b.mode # Item.mConst then |
| 1751 | + (* the limit, evaluated once: a constant as it is; else, in an |
| 1752 | + innermost loop, in the register chooseRegs kept for it, as a |
| 1753 | + variable no one can name; else in a slot *) |
| 1754 | + if (b.mode # Item.mConst) and (limitK >= 0) and (n < MaxFors) and limitIn[n] then |
| 1755 | + lim := Sym.newObj(Sym.oVar, o^.name, b.typ); |
| 1756 | + lim^.lev := 1; |
| 1757 | + lim^.reg := Gen.VarRegs[limitK]; |
| 1758 | + try Item.makeItem(v, lim); |
| 1759 | + try Item.assign(v, b); |
| 1760 | + b := v; |
| 1761 | + elsif b.mode # Item.mConst then |
| 1743 | 1762 | slot := try Item.toSlot(b); |
| 1744 | 1763 | Item.tempItem(b, b.typ, slot); |
| 1745 | 1764 | end; |
| | @@ -2465,10 +2484,12 @@ |
| 2465 | 2484 | proc chooseRegs(params, before: PObj); |
| 2466 | 2485 | var |
| 2467 | 2486 | isLoop: [256]BOOL; |
| 2468 | | - i, k, kx, d, loops, best, need: INT; o, q, qual: PObj; st: Scan.State; |
| 2487 | + forAt: [256]INT; (* the for loop opened at each depth, or -1 *) |
| 2488 | + i, k, kx, d, loops, best, need, nf, inLim: INT; o, q, qual: PObj; st: Scan.State; |
| 2469 | 2489 | afterDot, wasSkipping, leaf: BOOL; |
| 2470 | 2490 | begin |
| 2471 | 2491 | numCand := 0; |
| 2492 | + limitK := -1; nf := 0; inLim := -1; |
| 2472 | 2493 | o := params; |
| 2473 | 2494 | while o # nil do addCandidate(o); o := o^.next; end; |
| 2474 | 2495 | o := firstLocal(before); |
| | @@ -2498,7 +2519,19 @@ |
| 2498 | 2519 | INC(d); |
| 2499 | 2520 | isLoop[d] := (Scan.tok = Scan.sWhile) or (Scan.tok = Scan.sFor) or (Scan.tok = Scan.sRepeat); |
| 2500 | 2521 | if isLoop[d] then INC(loops); end; |
| 2522 | + forAt[d] := -1; |
| 2523 | + if (Scan.tok = Scan.sFor) and (nf < MaxFors) then |
| 2524 | + (* innermost until a for is found inside it; its |
| 2525 | + limit constant until a name in it isn't one *) |
| 2526 | + forAt[d] := nf; limitIn[nf] := false; |
| 2527 | + i := d - 1; |
| 2528 | + while (i > 0) and (forAt[i] < 0) do DEC(i); end; |
| 2529 | + if i > 0 then limitIn[forAt[i]] := false; end; |
| 2530 | + INC(nf); |
| 2531 | + end; |
| 2501 | 2532 | end; |
| 2533 | + | sTo: if (d > 0) and (forAt[d] >= 0) then inLim := forAt[d]; end; |
| 2534 | + | sDo, sBy: inLim := -1; |
| 2502 | 2535 | | sEnd, sUntil: |
| 2503 | 2536 | (* an end with no begin before it, in a body the parser will |
| 2504 | 2537 | report, leaves d at 0 *) |
| | @@ -2517,6 +2550,10 @@ |
| 2517 | 2550 | k := candidate(Scan.id); |
| 2518 | 2551 | if loops > 2 then INC(weight[k], 64); else INC(weight[k], 1 shl (2 * loops)); end; |
| 2519 | 2552 | end; |
| 2553 | + if (inLim >= 0) and not afterDot then |
| 2554 | + q := Sym.lookup(Scan.id); |
| 2555 | + if (q = nil) or (q^.cls # Sym.oConst) then limitIn[inLim] := true; end; |
| 2556 | + end; |
| 2520 | 2557 | | else: |
| 2521 | 2558 | end; |
| 2522 | 2559 | afterDot := Scan.tok = Scan.sPeriod; (* a name after a dot is a field, not a variable *) |
| | @@ -2563,6 +2600,9 @@ |
| 2563 | 2600 | weight[best] := 0; (* too big for what is left *) |
| 2564 | 2601 | end; |
| 2565 | 2602 | until false; |
| 2603 | + for i := 0 to nf - 1 do |
| 2604 | + if limitIn[i] and (k < 5) then limitK := k; end; |
| 2605 | + end; |
| 2566 | 2606 | end; |
| 2567 | 2607 | |
| 2568 | 2608 | (* The registers the variables of the procedure are in, as bits: bit i |
| | @@ -2594,6 +2634,7 @@ |
| 2594 | 2634 | if o^.cls = Sym.oVar then m := m | regBits(o); end; |
| 2595 | 2635 | o := o^.chain; |
| 2596 | 2636 | end; |
| 2637 | + if limitK >= 0 then m := m | (1 shl limitK); end; |
| 2597 | 2638 | return m; |
| 2598 | 2639 | end; |
| 2599 | 2640 | |
| | @@ -2663,7 +2704,7 @@ |
| 2663 | 2704 | try expect(Scan.sBegin); |
| 2664 | 2705 | curResult := sig^.base; |
| 2665 | 2706 | Gen.enterBody(p^.chunk, line); |
| 2666 | | - stray := false; elseIf := false; deferSlot := 0; |
| 2707 | + stray := false; elseIf := false; deferSlot := 0; numFors := 0; |
| 2667 | 2708 | try statements(); |
| 2668 | 2709 | p^.openEnd := (curResult # nil) and not ends; |
| 2669 | 2710 | Chunk.noteLine(Scan.curLine(), Scan.curCol()); |
| | @@ -2697,6 +2738,7 @@ |
| 2697 | 2738 | end; |
| 2698 | 2739 | curResult := old; |
| 2699 | 2740 | curProc := nil; |
| 2741 | + limitK := -1; |
| 2700 | 2742 | Sym.closeScope(); |
| 2701 | 2743 | return nil; |
| 2702 | 2744 | end; |
| | @@ -3841,6 +3883,7 @@ |
| 3841 | 3883 | end; |
| 3842 | 3884 | |
| 3843 | 3885 | begin |
| 3886 | + limitK := -1; |
| 3844 | 3887 | nameRt := internName("Rt"); |
| 3845 | 3888 | nameFail := internName("fail"); |
| 3846 | 3889 | nameAdr := internName("ADR"); |