Commit b794a909

b794a909399aMerged

Yusuke Endoh <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen+parse): emit do-while for begin..end while/until (post-test loops)

Prism's PM_LOOP_FLAGS_BEGIN_MODIFIER (= 4, bit 2) marks begin..end while cond and begin..end until cond as post-test loops — body must run at least once. Spinel was treating them as plain pre-test while, so a body whose first iteration falsifies the condition (e.g. optcarrots poke_4014 slow-path DMA where data <<= 8 makes data & 0xff == 0 initially) silently no-op'd.

Parser now emits I <id> flags <n> for WhileNode / UntilNode, codegen reads bit 2 and switches between pre-test and post-test emission. Bootstrap fixpoint and 339 tests still pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b794a909399a
Parent
ef05cb1aa590

spinel_codegen.rbmodifiedView file

@@ -27928,15 +27928,32 @@
2792827928 @hoisted_strlen_var = len_tmp
2792927929 end
2793027930 end
27931- cond = compile_cond_expr(@nd_predicate[nid])
27932- emit(" while (" + cond + ") {")
27933- @indent = @indent + 1
27934- redo_label = push_redo_label
27935- emit_redo_label(redo_label)
27936- compile_stmts_body(@nd_body[nid])
27937- pop_redo_label
27938- @indent = @indent - 1
27939- emit(" }")
27931+ # Prism PM_LOOP_FLAGS_BEGIN_MODIFIER (bit 0): `begin..end while cond`
27932+ # is a post-test (do-while) loop — body runs at least once. Without
27933+ # this, optcarrots `poke_4014` slow-path DMA never runs because
27934+ # `data <<= 8` makes `data & 0xff == 0` initially, so the pre-test
27935+ # while condition is false and the body is skipped.
27936+ if (@nd_flags[nid] & 4) != 0
27937+ emit(" do {")
27938+ @indent = @indent + 1
27939+ redo_label = push_redo_label
27940+ emit_redo_label(redo_label)
27941+ compile_stmts_body(@nd_body[nid])
27942+ pop_redo_label
27943+ @indent = @indent - 1
27944+ cond = compile_cond_expr(@nd_predicate[nid])
27945+ emit(" } while (" + cond + ");")
27946+ else
27947+ cond = compile_cond_expr(@nd_predicate[nid])
27948+ emit(" while (" + cond + ") {")
27949+ @indent = @indent + 1
27950+ redo_label = push_redo_label
27951+ emit_redo_label(redo_label)
27952+ compile_stmts_body(@nd_body[nid])
27953+ pop_redo_label
27954+ @indent = @indent - 1
27955+ emit(" }")
27956+ end
2794027957 @hoisted_strlen_var = saved_var
2794127958 @hoisted_strlen_recv = saved_recv
2794227959 @in_loop = old
@@ -27945,15 +27962,27 @@
2794527962 def compile_until_stmt(nid)
2794627963 old = @in_loop
2794727964 @in_loop = 1
27948- cond = compile_cond_expr(@nd_predicate[nid])
27949- emit(" while (!(" + cond + ")) {")
27950- @indent = @indent + 1
27951- redo_label = push_redo_label
27952- emit_redo_label(redo_label)
27953- compile_stmts_body(@nd_body[nid])
27954- pop_redo_label
27955- @indent = @indent - 1
27956- emit(" }")
27965+ if (@nd_flags[nid] & 4) != 0
27966+ emit(" do {")
27967+ @indent = @indent + 1
27968+ redo_label = push_redo_label
27969+ emit_redo_label(redo_label)
27970+ compile_stmts_body(@nd_body[nid])
27971+ pop_redo_label
27972+ @indent = @indent - 1
27973+ cond = compile_cond_expr(@nd_predicate[nid])
27974+ emit(" } while (!(" + cond + "));")
27975+ else
27976+ cond = compile_cond_expr(@nd_predicate[nid])
27977+ emit(" while (!(" + cond + ")) {")
27978+ @indent = @indent + 1
27979+ redo_label = push_redo_label
27980+ emit_redo_label(redo_label)
27981+ compile_stmts_body(@nd_body[nid])
27982+ pop_redo_label
27983+ @indent = @indent - 1
27984+ emit(" }")
27985+ end
2795727986 @in_loop = old
2795827987 end
2795927988