How the C view works
decomp::decompile turns one function into lines of C-like text in four steps.
Step 1: basic blocks
Section titled “Step 1: basic blocks”The function’s instructions are split into blocks: a new block starts at the function’s first instruction, at every jump target, and after every jump or return. For each block the engine records which registers it reads before writing, and which it writes. A backwards pass over the blocks then works out which registers are live at the end of each block, meaning some later block will read them.
Step 2: from instructions to statements
Section titled “Step 2: from instructions to statements”Each block is walked once while tracking what every register holds as a formula. mov eax, [rcx+8] followed by add eax, 1 leaves eax holding the formula *(rcx + 8) + 1, and produces no output yet. Output is only produced when something has a lasting effect:
- a write to memory becomes an assignment,
- a call becomes a call statement, with its result stored in a temporary,
- at the end of a block, each live register whose formula changed is assigned.
Along the way:
- Memory addressed relative to the stack pointer becomes a named local (
var_20) or stack argument (arg_28). The position of the stack pointer, and of the frame pointer if one is set up, is tracked across blocks. - The last comparison is remembered, so a following conditional jump becomes a readable condition such as
a < b. A block reached from exactly one other block starts with that block’s comparison, so a test and a jump split across two blocks still read as one condition. The carry flag is remembered separately, which is what makessbb,adcand the bit-test instructions readable. - Before a memory write or a call, any register whose formula reads memory is given a temporary name first, because what it reads is about to change. The same is done for the remembered comparison.
- Before a call, any register whose formula mentions a register the call may overwrite is given a temporary name too. Without this,
mov esi, ecx/call f/ use ofesiwould wrongly print as a use ofecx. - A call to a function listed in
winapi.rstakes exactly that function’s number of arguments: the first four from registers and the rest from the stack on 64-bit, the last pushed values on 32-bit. A 32-bit call to one of the program’s own functions looks at how that function returns (ret N) or at theadd esp, Nafter the call. - Simple copies through the
xmmregisters,rep movs/rep stos, the atomic instructions (lock cmpxchg,xadd,xchgwith memory), reads throughfs/gs, and plain floating-point arithmetic are modelled. - Formulas are capped in size.
- Instructions that are not modelled become
__asm("..."). Whatever registers they write are forgotten, and so are the flags, so a later condition is never built from a stale comparison.
Afterwards, temporaries that are never read are removed, and one that is read exactly once on the next line is folded into that line.
Step 2b: variables
Section titled “Step 2b: variables”Inside a block, a register read at the start means “whatever it held when this block began”, and a block hands a value on by assigning to the register’s name. name_variables then joins these across the whole function:
- For each register it works out which assignments can reach the start of each block along the jumps (reaching definitions).
- Assignments that reach the same read belong to the same variable, so they are joined (union-find). Each group gets one name:
argNif it includes the value the function was given, otherwise a freshvN. - If a group could also hold an unknown value (one left by a call or by an instruction that is not modelled) or a leftover from before the function started, it keeps the raw register name. A wrong name would be worse than an honest register.
- A variable with exactly one assignment whose value cannot change afterwards (a number, a text, an argument that is never reassigned, or a temporary) is folded into its uses, so
rcx = arg1;disappears andarg1is used directly. - A test that becomes a comparison of two numbers is replaced by the branch that is always taken.
Blocks are lifted in the order execution reaches them, so each knows where the stack and frame pointers stand. The C library and Windows tables give exact argument counts for known functions; for the program’s own functions the count comes from which argument registers the called function reads before setting them.
At print time, find_records looks for named values read or written at two or more fixed, non-negative offsets that never overlap or disagree in size; those become records (rec1, rec2, …), their accesses print as p->field_X, and a comment at the top lists each layout. find_arrays looks at every memory access. A base that is always accessed with the same item size, always at whole-item offsets, and at least once with a changing offset is shown with square brackets, and an argument used that way gets a pointer type in the first line. Finally the vN names are renumbered in reading order.
Step 3: finding the structure
Section titled “Step 3: finding the structure”First, two tests in a row that share a destination are joined into one: “if a goto X; if b goto X” becomes if (a || b), and the mirror image becomes &&. Compilers produce exactly this pattern for || and &&, and joining it back removes most of what would otherwise be gotos. Blocks that do nothing but jump on are skipped over.
The blocks and the jumps between them then form a graph. The structurer:
- finds jumps that go backwards, which mark loops, and works out which blocks belong to each loop,
- works out, for each branch, the block where its two sides meet again,
- walks the graph from the top, emitting blocks in order: a branch becomes an
ifwith two sides, and a loop becomeswhile (true)withbreakandcontinue.
Anything it cannot place becomes a goto to a label, and the labelled code is printed afterwards. The exception is a very short block that ends the function, such as return 0;: that is simply printed again where it is needed. Nesting is capped at 100 levels so deliberately tangled code cannot exhaust the stack.
A block that ends in a jump table becomes a switch, with one arm for each place the table leads (cases that go to the same place share an arm) and default: for the range check’s other side. Each arm runs until the code where the arms meet again; when some arms return, that is the place most arms reach. An arm that runs into the next arm’s code is left without break, as C allows; running into any other arm becomes a goto. Inside a switch, leaving a surrounding loop is written as a goto, because break would only leave the switch.
A tidy-up pass then makes it read naturally: an early exit is put first with no else, while (true) { if (x) break; ... } becomes while (!x) { ... }, and a test at the bottom becomes do { ... } while (x).
Switches
Section titled “Switches”Compilers build a large switch as a jump table: check the value is in range, look up entry number value in a list of addresses stored in the program, and jump there. In assembly it ends with a jump through a register:
cmp eax, 6 ; bigger than the last case?ja defaultlea rdx, [table]movsxd rax, [rdx+rax*4]add rax, rdxjmp rax ; switch: 7 cases going to 7 places (jump table at ...)jumptable.rs replays the instructions just before such a jump, keeping track of what each register holds in terms of the values it started with, until it can write the jump’s destination as “number + entry read from address + value x size”. It understands the shapes GCC, Clang and MSVC produce on 32-bit and 64-bit code:
| Shape | Who makes it | Each entry |
|---|---|---|
jmp [table + i*8] or jmp [table + i*4] |
Programs at a fixed address | The case’s address |
movsxd rax, [rdx+rax*4]; add rax, rdx |
GCC and Clang, position-independent | Distance from the table to the case |
mov ecx, [rdx+rax*4+X]; add rcx, rdx with rdx the image base |
MSVC, 64-bit | The case’s address minus the image base |
movzx eax, byte [bytes+eax]; jmp [table+eax*4] |
MSVC, many cases sharing code | A byte table first picks the entry |
The number of cases comes from the range check: cmp x, N followed by ja (or jbe the other way round, possibly from somewhere else in the function), or a mask such as and edi, 0xF. A table is only believed if every entry lands in code; with no range check it is not read at all, since guessing its length could read past it. A table address loaded once before a loop (lea r8, [table]) is found by looking further back.
Found tables are used everywhere: the Assembly view names the jump (; switch: 8 cases ...) and labels each case, the function’s code is read on through the cases, the Graph draws an arrow to every case, and the C view prints a switch. To check what was found in a file, run KarakStudio.exe --tables <file> <out.txt>.
Step 4: printing
Section titled “Step 4: printing”The result is printed with indentation. Each line remembers the address of the instruction it came from, which is how clicking a C line selects the matching assembly, and the function it calls, if any, which is how double-clicking opens it.
The whole thing runs inside a guard: if anything goes wrong for a function, that function shows a message and the app carries on.