What:

It’s the second part of a 2-step Compiler. It takes an Internal Representation from the Compiler Frontend and converts it into Machine Code.

Components of Backend:

The IR getting passed in:

Optimisation:

  • Constant Folding: There’s low-hanging fruit in our suboptimal IR Graph. For example, as we compile our code, if we see two constants getting added (i.e. LHS and RHS of a binary operator are both literals), we can just add them at once and then remove their parts. We’ve just turned 3 expressions into 1.
  • Dead Code Elimination: Literally, whenever some part of the IR is not used anymore, we just remove it.

Instruction Selection:

  • Translates the IR into a actual instructions.
    • Eg converting add a, b into the ADD function in Assembly

Register Allocation:

  • Maps variables to the hardware registers available.

Instruction Sampling:

  • Rearranges the order of instructions to improve performance without altering program’s semantics.
    • E.G. If one instruction depends on the result of a prior instruction, unrelated instructions may be executed in between to avoid delays.