SamCoVT / TaliForth2

A Subroutine Threaded Code (STC) ANSI-like Forth for the 65c02
Other
29 stars 5 forks source link

Compromise edition #64

Closed patricksurry closed 5 months ago

patricksurry commented 5 months ago

I think this ends up as the best compromise. It doesn't try to cache i or do any other complicated conditional runtime so the code is straightforward. Each loop control block is 4 bytes on page 1, and we only cache the LSB of loopindex in zp as loopidx0 since we can often get away with only modifying that value and not checking anything in the LCB.

Not speculatively calculating i in loop makes tests with i look worse here. But loop (and now +loop) are shorter and faster. The i word is 20 bytes so just small enough to default to native compile, while j defaults to jsr. That's probably the right default, but now is your choice: by changing nc-limiit you can get both native or both jsr.

For comparison here's the dodowordij test result if you force J to be native compile as well.

: dodowordij 100 0 do 10 0 do i drop j drop loop loop ;  ok
             ' dodowordij    cycle_test            CYCLES: 168410 ok   # this branch, with J as JSR
             ' dodowordij    cycle_test            CYCLES: 156410 ok   # this branch, forcing J as AN
             ' dodowordij    cycle_test            CYCLES: 165748 ok   # master with J as AN
SamCoVT commented 5 months ago

I think this looks like a good compromise across size/speed/readability. When compared to the original speed, it's a massive speed boost. I only found a couple of things in the comments that could be better. I think you had mentioned that you wanted to clean things up and maybe update the documentation. Just let me know when you want me to pull this one.

patricksurry commented 5 months ago

I think the PR itself is good to go. I haven't tried to build documentation yet but there are a couple of things there that I should probably update now. Do you mind if I do that in a follow-on PR?

SamCoVT commented 5 months ago

No issues doing the documentation as a separate PR. I'll often just open an issue to keep track of ongoing documentation updates that are needed, and then I handle them all at once. I find that works best for me. I should have time to review and pull this PR this coming weekend. I do have "reorganizing the docs" still on the to-do list so that readers get less of that deja-vu effect if they read it linearly, and I agree that a little tutorial that people can follow along with that shows the basics of getting started with Forth is also a very good idea. If you think it makes sense to do that reorganization effort first, let me know. Otherwise (or perhaps regardless), you are likely to want docs/ch_internals.adoc and search for "Control Flow", which describes how branches and loops are implemented.

patricksurry commented 4 months ago

fixed #53