DEV Community

Paul Lefebvre
Paul Lefebvre

Posted on • Updated on

Compilers 108 – Code Generation

Code generation is one of the last steps of the compiler. This is where the compiler emits actual machine code for the IR that was previously created.

The Compiler series:

This is the simple code we started with in the Compilers 101 blog post:

sum = 3.14 + 2 * 4 // calculation
Enter fullscreen mode Exit fullscreen mode

It results in a constant value of 11. After the IR is generated and optimized, it can boil down to just a single line of machine code, which will vary by processor and architecture. Machine code is just binary and not readable, so below is what the Assembly code might look like.

This is the Assembly code for 32-bit ARM:

movs r0, #11
Enter fullscreen mode Exit fullscreen mode

This is the Assembly code for ARM64:

movz w0, #11
Enter fullscreen mode Exit fullscreen mode

x86 and x86-64 use this Assembly code:

movl $11, %eax
Enter fullscreen mode Exit fullscreen mode

Obviously this is the tricky part of making a multi-platform compiler since Assembly code is different between processors and architectures.

Once you have machine code that the computer can run, the last step is to link all the pieces together so that you have an app that the OS can run.

Top comments (0)