| | @@ -5,8 +5,57 @@ |
| 5 | 5 | stopping for the owner's review. A finished milestone is cut from this |
| 6 | 6 | file: git keeps the history, and the report's section 12 the language's. |
| 7 | 7 | |
| 8 | | -No milestone is under way. v0.6.0 is tagged; its steps 5 to 10 await |
| 8 | +v0.7.0, better code, is under way. v0.6.0 is tagged; its steps 5 to 10 await |
| 9 | 9 | the owner's review. |
| 10 | + |
| 11 | +## Under way: better code (v0.7.0) |
| 12 | + |
| 13 | +For: rat within 1.25 times the time of pt built by FPC `-O2 -Cr` |
| 14 | +(today 634 ms against 428, 1.48 times), with the compiler still a |
| 15 | +model of clarity. No change to the language. Measured on rat's 26 MB |
| 16 | +workload, best of 9 interleaved runs, and on `make bench`. |
| 17 | + |
| 18 | +Where rat's time goes (ulam-prof): `Utf8.next` 22%, `Str.decode` 15%, |
| 19 | +`Screen.put` 11%, `Parser.feed` 11%, `Screen.unsplit` 9%. Their |
| 20 | +listings show the steps below. ulam's `doc/codegen.md` measured the |
| 21 | +same rewrites in a compiler of the same kind; its gains are quoted. |
| 22 | + |
| 23 | +1. **A prologue that does only what it must.** A register variable |
| 24 | + keeps a frame slot that is zeroed for nothing, and so do the slots |
| 25 | + the registers are saved in: `Str.decode`, whose four variables are |
| 26 | + all in registers, zeroes seven quadwords in a loop on every call. |
| 27 | +2. **Two results in rax and rdx** for every procedure whose two |
| 28 | + results are each one word, as a code procedure's are now; not |
| 29 | + through a hidden pointer to the caller's slots. Changes a reading |
| 30 | + of the report already taken: the owner's to approve. |
| 31 | +3. **Values left where they are.** Gen's value stack keeps a value in |
| 32 | + the place it is until it must move: a slice made from a slice |
| 33 | + (`s[i..LEN(s)]`) isn't written to the frame and read back to be |
| 34 | + passed; `v := v + x` adds into `v`'s register; a value is not moved |
| 35 | + through `r8` on its way from one register to another; a value that |
| 36 | + fits its type already isn't extended again (ulam's P1). |
| 37 | +4. **Conditions as jumps.** `test` for a compare with zero; a `BOOL` |
| 38 | + result tested where it is; a jump over a jump made one jump (ulam's |
| 39 | + P7: with step 5, `heap` 11% faster); no dead jump after a `return` |
| 40 | + or `break`; no check of what a slice's own length makes certain |
| 41 | + (`s[i..LEN(s)]`'s upper bound). |
| 42 | +5. **Addresses as the machine makes them.** An index scaled in the |
| 43 | + address (`[rdi + rax*8]`), not by `imul` and `add`; a product by a |
| 44 | + power of two as a shift; a constant on the right of `+ * & |`, |
| 45 | + where it becomes an immediate (ulam's P5: `tree` 5 to 10% faster). |
| 46 | +6. **`for` loops as FPC writes them**: stepped at the top, one compare |
| 47 | + and one jump back at the bottom, the limit in a register when one is |
| 48 | + free (ulam's P2: `matmul` 16% faster). |
| 49 | +7. **Read by a beginner.** A reader with no part in this milestone |
| 50 | + (a fresh agent) reads `gen.pas` and `doc/compiler.md` and lists |
| 51 | + every place it can't follow; each is mended. `doc/compiler.md` |
| 52 | + gains a section, "How the code is made good", walking one of rat's |
| 53 | + procedures through the steps, before and after. |
| 54 | + |
| 55 | +Each step stops if it buys less than its lines: one that gains under |
| 56 | +2% on rat and on no kernel is reverted and noted here. Short branches |
| 57 | +(code 5 to 8% smaller in ulam's estimate) are left for after, measured |
| 58 | +on their own. |
| 10 | 59 | |
| 11 | 60 | ## Next, when the owner says |
| 12 | 61 | |