Tech News
← Back to articles

Rust GCC back end: Why and how

read original related products more articles

Rust GCC backend: Why and how

Whenever you compile using Rust, the compiler goes through different passes and in the end, generated binary code for the target processor. By default, it uses LLVM as backend to generate the binary code, but more backends exist like cranelift and GCC. This post is about how it's possible for one compiler to use different backend to generate binaries, in particular GCC.

Passes

Before going into details, we need to describe how compilers actually work. They read source code and convert it internally into a format they can manipulate, commonly called Abstract Syntax Tree (shortened "AST").

However, compilers go through multiple passes, and often each pass has their own AST. Let's take a short and very incomplete example with the Rust compiler passes. We have 4 steps (again, this is simplified!):

AST: checks that the syntax is valid HIR: checks if types are valid MIR: checks lifetimes and runs borrow-checker codegen: generate binary code (which also has multiple steps, but not detailed here)

Each step generates a new AST with new information if no error was encountered and provides it to the next pass.

Little side-note: If enough people are interested by this topic, I can write a (much) longer explanation of these passes.

Backend vs front-end

So now that we have a high-level idea of Rust compiler passes, what is the difference between "front-end" and "back-end" exactly?

... continue reading