Introduction to Compilers #1 - Folds

This post is composed of infographics on the basics of compilation, assembly, and compiler optimizations.

Here's how it's going to work:

Each article will contain 2-3 infographics about a specific topic and 1-2 support graphics to provide context.

We shall start with compilation plus assembly language basics, and from there, we will cover one of the more straightforward compiler optimizations that compilers do, which is Folding.

Compilation in C# happens at two distinct stages. Offline compilation from C# to IL and the online compilation from IL to Assembly code via Just-In-Time compiler.


In contrast, languages like C++ will compile from C++ code to LLVM IR and Assembly code.

The difference here is that this will all happen offline. Of course, not all Cpp compiles go through the LLVM IR, but most modern compilers do. LLVM IR shares some similarity to IL, but it's much closer to the native assembly language.

EAX is a specialized register to accumulate values and most opcodes have a special one argument overload that uses this register as a default target.

ECX is a counter register designed to be a loop counter, you will see most loop increments using this register.

EDX is a general-purpose register to store short-term variables. It's used with EAX since special operations will use both registers to store the result.

EBX is a general purpose register to store data. In functions that store parameters or variables on the stack, the base pointer holds the location of the current stack frame.

The C# compiler's current version will not fold expressions where a variable is on the left-hand side (3rd box). Historically this was due to correctness since the compiler would analyse +1 token at a time. That's not true any more, and it has been acknowledged as a bug and will get fixed.

Look here:
https://github.com/dotnet/runtime/issues/43551

If you would like to know more, I made a YouTube video about Folding in dotnet.

This content took a while to create, so if you like it, please consider buying me a coffee 😉