DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Edited on Originally published at delta1labs.com

How Glass recovers compound assignment

A decompiler's job is not to produce some C# that compiles — it is to produce C# that means the same thing. Compound assignment is where that distinction bites. Consider:

GetAccount(id).Balance += amount;
Enter fullscreen mode Exit fullscreen mode

Rewrite that as GetAccount(id).Balance = GetAccount(id).Balance + amount and you have changed the program: GetAccount now runs twice, possibly against two different accounts. The C# language guarantees the receiver of a compound assignment is evaluated exactly once. A faithful decompiler has to recover that guarantee from the IL, not assume it.

The real question

There is no += opcode. The compiler lowers every compound assignment into a load the current value, apply the operator, store the result triple. So the decompiler sees a get and a set of the same location with an arithmetic op between them — and has to decide: was the location computed once and reused, or computed twice? Get that wrong and you either duplicate side effects or invent a += that was never written. The answer lives in how the receiver reaches the stack.

The simplest shape: a field

Start with a plain field, where the "receiver" is just this:

class Counter
{
    private int _n;
    public void Bump() => _n += 5;
}
Enter fullscreen mode Exit fullscreen mode
ldarg.0            // this, for the store
ldarg.0            // this, for the load
ldfld  int32 Counter::_n
ldc.i4.5
add
stfld  int32 Counter::_n
ret
Enter fullscreen mode Exit fullscreen mode

this is trivially re-loadable, so the compiler just emits ldarg.0 twice rather than bothering to duplicate it. Glass sees ldfld/add/stfld on the same field with the same receiver and folds the triple back to _n += 5. Note that _n = _n + 5 would emit byte-for-byte identical IL — and that is fine, because for a field both readings are semantically equal. Glass is free to choose the compound form as the more idiomatic rendering.

The shape that matters: a property

Now give the location a getter and setter, and make the receiver a real value:

void Bump(Counter c) => c.Count += 3;
Enter fullscreen mode Exit fullscreen mode
ldarg.1            // c
dup                // keep one copy of c for the setter
callvirt instance int32 Counter::get_Count()
ldc.i4.3
add
callvirt instance void Counter::set_Count(int32)
ret
Enter fullscreen mode Exit fullscreen mode

Here is the whole story in one opcode: dup. The receiver c is pushed once, duplicated, and the two copies feed get_Count and set_Count respectively. The compiler is spending an instruction specifically to not evaluate c twice. That dup is Glass's proof that the single-evaluation guarantee held, which is exactly the licence it needs to emit +=. Had the IL instead loaded ldarg.1 twice around a side-effecting expression, Glass would decline to fold and render the explicit get/set form, because collapsing it would be a lie.

The address shape: array elements

Arrays add one more wrinkle — the location is a managed pointer, computed with ldelema:

void Bump(int[] arr, int i, int v) => arr[i] += v;
Enter fullscreen mode Exit fullscreen mode
ldarg.1            // arr
ldarg.2            // i
ldelema  [System.Runtime]System.Int32
dup                // one element address, used for load and store
ldind.i4
ldarg.3            // v
add
stind.i4
ret
Enter fullscreen mode Exit fullscreen mode

ldelema computes the address of arr[i] once — which also means the bounds check and the index i are evaluated once. The dup duplicates that address, not the value: one copy is dereferenced with ldind.i4 to read the current element, the other receives the result through stind.i4. This is the cleanest single-evaluation signal in the whole family, and it is why arr[i] += v never re-indexes.




The compiler spends a dup to guarantee single evaluation; Glass reads that dup back as the licence to render +=.

User-defined indexers

A this[...] indexer with a side-effecting argument is the hardest case, because both the receiver and the index must be evaluated once, yet both are needed twice (for get_Item and set_Item). The compiler spills them into temporary locals:

void Bump(Dictionary<string, int> d, string k) => d[Key(k)] += 1;
Enter fullscreen mode Exit fullscreen mode

The lowered shape stores the receiver and the computed key into compiler locals, then threads those same temporaries through get_Item and set_Item. Glass recognises the spill-then-reuse pattern — the identical locals feeding both calls — and folds it to d[Key(k)] += 1, with Key(k) appearing exactly once. If the temporaries did not match, the fold would not fire.

How this shows up in Glass

You do not configure any of this; the IL view and the C# view are two renderings of the same analysis. Open an assembly, select a method, and toggle the IL pane to see the dup/ldelema/stind triple directly under the recovered +=. The same recovery runs under the CLI and the MCP server, so an agent asking Glass to export a method gets the semantically-correct compound form, not a double-evaluating rewrite. This matters most when you are reading unfamiliar code during an interoperability or security-research pass and need to trust that the decompiled statement runs what the binary runs.

When it breaks — and the honest limits

  • Hand-written or obfuscated IL can load a side-effecting receiver twice with no dup. That is not a compound assignment, and Glass will not fold it — it renders the two evaluations faithfully, which is the correct, if uglier, reading.
  • String concatenation s += t does not lower to add; it calls String.Concat. That is a different recovery path entirely, and folding it back to += is a separate heuristic.
  • User-defined operators fold too: += on a type with an op_Addition emits call op_Addition where the add would be, and Glass treats the custom operator the same way.
  • Pre- vs post-increment is only observable when the value is used. As a bare statement, x++, ++x, and x += 1 are indistinguishable in IL, so the chosen rendering is a cosmetic call, not a semantic one.

The takeaway: compound assignment is not a token the decompiler pattern-matches — it is an invariant it has to verify. The dup and the shared temporary are the compiler telling you "this was evaluated once." Glass reads that signal and refuses to fold without it, which is the difference between C# that compiles and C# that is actually your program. Glass is a free .NET decompiler — IL-to-C#, metadata inspection, and project export in a VS-native UI, a CLI and an MCP server.

Top comments (0)