Skip to content

s9 emit: port Ghidra markLabelBumpUp — hoist loop-head labels out of loop conditions (fix invalid C) - #150

Merged
mahaloz merged 1 commit into
mainfrom
feat/label-bumpup-hoist
Jul 8, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/label-bumpup-hoist

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Ports Ghidra's unported markLabelBumpUp / emitAnyLabelStatement label-hoist — fixing an invalid-C emission bug (a loop-head label rendered inside a for(...) condition). Found while disentangling proposal #39 (condfold): this is the separable, low-risk half; the large condfold region-structurer work is a follow-up.

The bug (grep/parse_grep_colors @ 0x9ddf, O0)

Before — label inside the for condition (uncompilable):

    for (; (
label_9e38:*v3 == ':' || (*v3 == '\0')); v3 = &v3[1]) {

After — hoisted to its own line above the loop; the goto label_9e38; still targets it:

label_9e38:
  do {
    for (; (*v3 == ':' || (*v3 == '\0')); v3 = &v3[1]) {

Root cause

kuna had the f_label_bumpup flag (block.rs:175) but never ported the mechanism that sets it: Ghidra's markLabelBumpUp was commented out at the ActionFinalStructure seam (blockaction.rs:3617), and emitAnyLabelStatement didn't exist, so loop emitters never hoisted the head label — emit_block_copy emitted it inline (inside the for).

The port (line-faithful, cites C++ origins)

  • substrate/block.rs: mark_label_bump_up + graph walk — port of FlowBlock::markLabelBumpUp (block.cc:259), BlockGraph::markLabelBumpUp (block.cc:1259) + the WhileDo/DoWhile/InfLoop overrides that steal the front-leaf's label (block.cc:3364/3477/3505).
  • s8_structure/blockaction.rs: call mark_label_bump_up(sroot, false) at the seam (blockaction.cc:2196).
  • s9_emit/printc.rs: added emit_any_label_statement (printc.cc:3354); routed emit_block_copy (printc.cc:2894) + the 4 loop emitters (printc.cc:3097/3146/3208/3236) through it. Double-emit avoidance preserved.

Verification

  • Gates: make test 675/675 PARITY OK (byte-identical, no baseline edit), make test-stages 256/256, make rust-test 0 failures (no golden-fixture updates needed).
  • Whole-binary safety (decompile-all, true pre/post baseline): grep — label-in-condition 2→0, total gotos 207→207 (unchanged — this is a label-position fix, not a goto fix), 0 brace-unbalanced, 0 new errors; factor — clean. All 10 changed functions are pure label-position hoists to the Ghidra-canonical position.

🤖 Generated with Claude Code

https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J

…tions

kuna emitted a goto-target label INSIDE a for/while condition (invalid C),
e.g. grep sub_9ddf: `for (; (\nlabel_9e38:*v3 == ':' ...); ...)`. The label
mechanism (`f_label_bumpup`, `is_label_bump_up`) existed but nothing set it,
and the printer emitted the label directly instead of routing through the
label-hoist path. This ports Ghidra's markLabelBumpUp / emitAnyLabelStatement
so a loop-head label is hoisted to its own line ABOVE the loop.

Port (line-faithful):
- substrate/block.rs: `mark_label_bump_up`/`block_graph_mark_label_bump_up`
  (C++ FlowBlock::markLabelBumpUp block.cc:259, BlockGraph::markLabelBumpUp
  block.cc:1259, and the BlockWhileDo/BlockDoWhile/BlockInfLoop overrides that
  steal the lower block's label, block.cc:3364/3477/3505).
- s8_structure/blockaction.rs: call `graph.mark_label_bump_up(false)` at the
  ActionFinalStructure seam, after markUnstructured (port of blockaction.cc:2196).
- s9_emit/printc.rs: add `emit_any_label_statement` (C++ PrintC::emitAnyLabelStatement
  printc.cc:3354 — `if isLabelBumpUp return; bl=getFrontLeaf(); emitLabelStatement`);
  route emit_block_copy through it (printc.cc:2894) and add it to the 4 loop
  emitters at the same sites C++ has them (printc.cc:3097/3146/3208/3236).

Gates: datatests 675/675 PARITY OK (byte-identical, no baseline change — the
corpus asserts 0 labels so the hoist never fires); test-stages 256/256 PARITY OK;
rust-test exit 0 (275 suites ok, 0 failures, no golden-fixture changes).

Whole-binary safety (decompile-all, before vs after): grep 6 fns changed, all
label-position hoists (sub_9ddf + sub_19334 were the genuine label-in-condition
bug; 4 more moved a loop-head label from first-statement-inside to its own line
above the loop, Ghidra-canonical); factor 4 fns changed, same hoist. In both:
label-in-condition 2->0 (grep) / 0->0 (factor); total gotos UNCHANGED (grep 207,
factor 88 — a label-position fix, not a goto fix); 0 functions brace-unbalanced;
no new errors.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant