r8asm: make .loop consume the bit it just cleared

.endloop expanded to >-+]< : flb clears the LSB and rnz then tests that
very same bit, so it always sees 0 and the body ran exactly once no
matter how many 1s the counter cell held.  Measured: a counter of 0b111
came out as 0b110 after a single pass.

Putting a ror after the flb pulls the next bit down before the test,
which is what SPEC describes -- consecutive 1s from the LSB, one per
iteration.  Measured:
    0b0000_0111 -> 3 passes, counter 0
    0b1111_1111 -> 8 passes, counter 0 (a bare ror would spin forever)
Nothing under asm/ uses .loop, so no existing tape changes.
This commit is contained in:
starcells committed 2026-11-05 22:49:54 +08:00
1 parent 3f3e431d64
commit 1af92eb156
1 file changed
+5
+5
View File
@@ -281,8 +281,13 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
// >[<
} break;
case builtin_t::ENDLOOP: {
// Consume one '1' from the auxiliary cell and pull the next bit down.
// Without the ror, the rnz could only ever see the bit that flb just
// cleared, so a .loop over 0b111 ran once instead of three times
// (SPEC: "consecutive 1s starting from the LSB").
to_return.children.push_back(ROT8_NODE("stp"));
to_return.children.push_back(ROT8_NODE("flb"));
to_return.children.push_back(ROT8_NODE("ror"));
to_return.children.push_back(ROT8_NODE("rnz"));
to_return.children.push_back(ROT8_NODE("btp"));
// >-+]<