Repository navigation
regionstructure Inc 5: short-circuit/condition folding - #79
Merged
Merged
Conversation
…und conditions)
Port Ghidra's `CollapseStructure::ruleBlockOr` (the `collapseConditions`
pre-pass) into the region structurer as `match_acyclic_short_circuit_conditions`
(the kuna analog of angr `phoenix._match_acyclic_short_circuit_conditions`,
types a-d). When a 2-out condition `bl` has a single-in, non-complex, 2-out
successor `orblock` that reconverges through a shared clause, the cascading
`if (A) { if (B) { body } }` shape folds into a single `BlockCondition` carrying a
compound `A && B` / `A || B` condition via `BlockGraph::new_block_condition`
(which picks `CPUI_INT_AND`/`CPUI_INT_OR` from the edge orientation exactly as
Ghidra does, so the rendered `&&`/`||` is byte-identical).
The fold runs as an up-front fixpoint before the sequence/ITE/loop cascade,
mirroring Ghidra's `collapseAll` order (`collapseConditions`, which is only
`ruleBlockOr`, runs before `collapseInternal`). The `isComplex` gate reuses
`BlockBasic::isComplex` (precomputed `complex_blocks`, the same set
`CollapseStructure` consumes); orientation uses the existing `negate_condition_rec`.
This closes the only datatest where the region structurer (`regionstructure on`)
diverged from Ghidra: `elseif.xml` Else-if #14, which now folds to the identical
`else if ((b == 0xc9) && (a <= 299))` instead of a nested `if`. With the option
temporarily flipped default-ON, the full 675-assertion datatest suite is
byte-identical to default-OFF (0/675 divergences, PARITY OK) -- so the future
default-on flip is clean.
Honest-partial-safe: a node that does not match the cascade is left for the
sequence/ITE/virtualize schemas (never a panic). Default-OFF -> 675/675
byte-identical; ON-vs-OFF decompile speed unchanged.
New stage test `tests/stages/regionstructure-shortcircuit.xml` asserts the
`&&`-folded form across both passes (off=CollapseStructure, on=region structurer);
baseline-stages.json regenerated (200 -> 203), xml.rs corpus count bumped
(144 -> 145), roadmap Inc 5 status section added. docs/baseline.json untouched.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
regionstructure Inc 5 — short-circuit / condition folding (
&&/||compound conditions)Extends the
regionstructureoption (default OFF) so the region structurer foldscascading short-circuit conditions into a single compound
&&/||condition,matching Ghidra byte-for-byte. This is the missing piece that makes the region
structurer match Ghidra on compound conditions — the default-on blocker.
The bug this fixes
With
--option regionstructure on, a cascadingif (A) { if (B) { body } }rendered asa nested
if, where Ghidra folds the condition:This was the only datatest (
tests/datatests/elseif.xml, Else-if #14) where theregion structurer diverged from Ghidra.
What landed
region_structurer::match_acyclic_short_circuit_conditions(→try_block_or) is afaithful port of Ghidra
CollapseStructure::ruleBlockOr(blockaction.cc:1321) — thekuna analog of angr
phoenix._match_acyclic_short_circuit_conditions(types a–d). When a2-out condition
blhas a single-in, non-complex, 2-out successororblockthatreconverges through a shared clause, the pair folds into a
BlockConditionviaBlockGraph::new_block_condition, which picksCPUI_INT_AND/CPUI_INT_ORfrom the edgeorientation exactly as Ghidra does — so the rendered
&&/||is byte-identical tothe default
CollapseStructureform.Ghidra's
collapseAllorder (collapseConditions, which is onlyruleBlockOr, runsbefore
collapseInternal).isComplexgate reusesBlockBasic::isComplex(precomputedcomplex_blocks, thesame set
CollapseStructureconsumes); orientation uses the existingnegate_condition_rec(recording the deferred data-flow flips).sequence/ITE/virtualize schemas — never a panic, never a whole-function abort.
Parity & default-on readiness
make test675/675 PARITY OK (byte-identical).is not in this PR): the full 675-assertion suite is byte-identical to OFF
(0/675 divergences, PARITY OK). The per-function C of the
elseifbinary isbyte-identical between the OFF (CollapseStructure) and ON (region structurer) passes.
→ the future Inc 6 default-on flip is clean.
Tests / gates
tests/stages/regionstructure-shortcircuit.xmlasserts the&&-foldedelse if ((b == 0xc9) && (a <= 299))form across both passes (off + on), plus anegative (the inner
a <= 299is not left as a standalone nestedif).make test→ 675/675 PARITY OKmake test-stages→ 203/203 PARITY OK (baseline-stages.json regenerated 200 → 203)make rust-test→ green (xml.rs corpus count bumped 144 → 145)kuna catalog --check→ OK (no new option;docs/assertions.mdunchanged)docs/baseline.json→ untouchedSpeed
Measured
decomp_test_dbgon theelseifbinary, 30 runs each: OFF 5.064s vs ON 5.082s(~169 ms/run) — a 0.4% delta, within noise. No measurable regression (the fold only runs
under the option; the up-front fixpoint is bounded by the same
round_capas the main loop).Files
decompiler/crates/kuna-decomp/src/s8_structure/region_structurer.rs— the schema(
match_acyclic_short_circuit_conditions/try_block_or/is_complex/compute_complex_blocks/with_complex_blocks) + up-front fixpoint wiring.decompiler/crates/kuna-base/src/xml.rs— corpus count 144 → 145.tests/stages/regionstructure-shortcircuit.xml— new both-directions stage test.docs/baseline-stages.json— +3 passing keys.docs/region-structurer-roadmap.md— Inc 5 status section.🤖 Generated with Claude Code
https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac