N64 Decompilation Patterns

From Decompedia
Jump to navigation Jump to search

Intro

The act of compiling source code loses a lot of the context and information present in the original source - and the higher the level of optimisation, the more information is lost; decompiling code compiled with -O0 is significantly more straightforward than -O2.

Some of the codegen differences can be fixed by adding random snippets such as if(1){}; or wrapping statements in do { ...} while(0) statements.

Common Patterns

Division by power-of-two

// when you see this:
if (x > 0) {
    y = x >> 0x10;
} else {
    y = (x + 0xFFFF) >> 0x10;
}

// it's likely:
y = x / 0x10000;

IDO Patterns

IDO is full of quirks.

  • Whitespace can affect codegen
  • Comparing a float with 0 is different to comparing a float with 0.0f
  • Defined vs declared (i.e. use of extern) can affect codegen, including defining variables as static


GCC Patterns

GCC is less quirky than IDO.