From 98d81b780952c2c91fe686c55bb867d193472749 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 22 May 2025 18:06:32 -0600 Subject: Add UI action and API to control expression folding --- docs/guide/index.md | 22 ++++++++++++++++++++++ docs/img/folding-after.png | Bin 0 -> 36670 bytes docs/img/folding-before.png | Bin 0 -> 36957 bytes docs/img/folding-menu.png | Bin 0 -> 215713 bytes 4 files changed, 22 insertions(+) create mode 100644 docs/img/folding-after.png create mode 100644 docs/img/folding-before.png create mode 100644 docs/img/folding-menu.png (limited to 'docs') diff --git a/docs/guide/index.md b/docs/guide/index.md index eb29747f..f4a4e4af 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -807,6 +807,28 @@ Performing this action on both variables in the example results in the following ![Dead Store Elimination Results](../img/dead-store-after.png "Dead Store Elimination Results"){ width="500" } +## Expression Folding + +Binary Ninja automatically performs optimization passes on high level code. One of these optimizations is to fold +assignments with a single use into the statement that uses it. An example of a function call being folded into another +is shown below: + +![Expression Folding](../img/folding-before.png "Expression Folding"){ width="500" } + +Binary Ninja uses heuristics to determine if this will improve readability, but sometimes it doesn't make the preferred +choice. You can override the heuristic by right-clicking an expression and choosing "Allow" or "Prevent" from the +"Expression Folding" submenu. + +![Expression Folding Menu](../img/folding-menu.png "Expression Folding Menu"){ width="500" } + +This option will only appear if the expression can be folded. If Binary Ninja's analysis determines that it is not sound +to fold an expression, the submenu will not be present. + +Choosing "Prevent" from the "Expression Folding" menu on the call to `_strlen` in the example above results in the +following output: + +![Expression Folding Results](../img/folding-after.png "Expression Folding Results"){ width="500" } + ## Merging and Splitting Variables Binary Ninja automatically splits all variables that the analysis determines to be safely splittable. This allows the user to assign different types to different uses of the same register or memory location. Sometimes, however, the code is more clear if two or more of these variables are merged together into a single variable. diff --git a/docs/img/folding-after.png b/docs/img/folding-after.png new file mode 100644 index 00000000..ac0ad68e Binary files /dev/null and b/docs/img/folding-after.png differ diff --git a/docs/img/folding-before.png b/docs/img/folding-before.png new file mode 100644 index 00000000..4d760dbe Binary files /dev/null and b/docs/img/folding-before.png differ diff --git a/docs/img/folding-menu.png b/docs/img/folding-menu.png new file mode 100644 index 00000000..bd8f95ca Binary files /dev/null and b/docs/img/folding-menu.png differ -- cgit v1.3.1