Compilers

Hand-drawn wavy terracotta band passing through five upright hatched terracotta bars and straightening as it goes, over flowing cream, sand, umber and moss contour lines.

A compiler translates a program from one language into an equivalent program in another, such as machine instructions or another language meant for programmers to read (Aho et al., 2007). Compilers can rewrite programs to run faster, but measured speed gains depend on the programs and on what is compared (Pfenning et al., 2026; Kästner et al., 2017; Keiserås et al., 2026). Their output is not always faithful. Random test programs find bugs in widely used compilers, programs’ own tests miss many of them, and proofs cover only stated stages (Yang et al., 2011; Marcozzi et al., 2019; CompCert, n.d.). Studies of improved error messages for beginners give mixed results (Becker et al., 2019).

Translation and execution

An interpreter, by contrast, carries out the program’s operations on the inputs it receives (Aho et al., 2007). It can work from the source code or from bytecode, instructions produced by an earlier translation (Universidad Politécnica de Cartagena, 2022).

Usage varies for translation between two languages written for programmers. Some accounts call it compiling; others separate this source-to-source translation from compiling into lower-level instructions. The TypeScript documentation calls generating JavaScript from TypeScript transpiling (Aho et al., 2007; Harsu, 2000; TypeScript Team, 2026).

From early compilers to reusable tools

Historians name different first compilers depending on how they define one. Donald Knuth and Luis Trabb Pardo date Grace Hopper’s A-0, which assembled programs from existing routines, to spring 1952, and credit Alick Glennie’s Autocode as the first compiler (Hopper, 1952; Knuth & Trabb Pardo, 1976). John Backus instead credits Laning and Zierler’s Whirlwind system as the first working algebraic compiler, one that translated mathematical notation, and disputed whether Glennie’s example qualified (Backus, 1978b; Laning & Zierler, 1954).

Compiler design also developed formal ways to describe languages. The ALGOL 60 report separated a reference language from the ways different machines represented it (Naur et al., 1960). A grammar describes how pieces of a program combine. Knuth formalised which grammars allow left-to-right analysis looking a fixed number of symbols ahead, and Yacc generated code to recognise such structure (Knuth, 1965; Johnson, 1978).

The GNU Compiler Collection (GCC) and LLVM show two ways to reuse compiler machinery. Supporting a new kind of computer in GCC means describing how its intermediate code maps to that machine’s assembly language, a notation for machine instructions (Stallman, 1987; Perumal & Karkare, 2013). LLVM was built around one shared code representation that can be analysed and rewritten at several points, including when separately compiled pieces are linked and while programs run (Lattner & Adve, 2003; LLVM Project, n.d.a).

Phases and intermediate representations

A compiler’s work divides into phases, though one pass through the code can do several of them together (Aho et al., 2014).

PhaseWhat it does
LexingGroups source characters into tokens, such as names and operators (Appel & Palsberg, 2002).
ParsingWorks out how tokens group into expressions and other parts of the program (Appel & Palsberg, 2002).
Semantic analysisConnects variable uses to their definitions and checks types, such as whether an expression produces a number (Appel & Palsberg, 2002).
Intermediate code generationTranslates the analysed program into an intermediate representation (IR), a notation the compiler uses internally (Appel & Palsberg, 2002).
OptimisationRewrites intermediate code to improve properties such as execution time (Águila, 2004).
Code generationTurns the intermediate code into output, for instance by selecting instructions for the target machine (Águila, 2004; Appel & Palsberg, 2002).

The choice of representation makes some analyses easier. GCC’s GIMPLE uses a simplified three-address code, in which an instruction names at most two inputs and one destination, and lets GCC share analyses across source languages (Johnson & Zelenski, 2012; GCC Team, 2026). In static single assignment (SSA) form, each name is assigned once in the program text. Where two paths join, such as after an if and its else, a phi operation picks the value from the path taken (Cytron et al., 1991).

Compiling while a program runs

Just-in-time (JIT) compilation translates code after the program starts, unlike ahead-of-time compilation (Universidad Politécnica de Cartagena, 2022; Aycock, 2003). It can use what it observes during execution to decide where optimisation is worth the time. In adaptive SELF, a method is first translated quickly, and heavily used methods are compiled again with an optimising compiler (Hölzle, 1994).

The idea is old. A historical survey names a 1960 paper on Lisp as a candidate for its earliest account; Smalltalk-80 and SELF later translated methods when needed (Aycock, 2003; Chambers et al., 1991). Modern engines combine strategies. JavaScriptCore runs an interpreter, a bytecode-template JIT and two optimising tiers (Pizlo, 2020). V8’s Sparkplug turns bytecode straight into machine instructions without another intermediate representation (Swirski, 2021).

Optimisation and its measured gains

An optimisation changes a program’s form to improve execution time, memory use or code size. One optimisation, constant folding, computes an operation such as 2 + 3 during compilation. Another, constant propagation, replaces a variable with a value known to be constant, which can expose more operations to fold (Pfenning et al., 2026).

Language rules set the limits of these rewrites. In standard C, signed integer arithmetic that exceeds the type’s range has undefined behaviour: the standard imposes no requirements, so an optimiser may assume such overflow never happens (Svoboda, n.d.). A GCC setting instead makes overflowing signed arithmetic wrap around, which changes which rewrites are allowed (GCC Team, 2024).

How large are speed gains?

The size of a speed gain depends on the starting point of the comparison. Turning optimisation on and upgrading an already optimising compiler measure different effects (Kästner et al., 2017; Keiserås et al., 2026).

What changedMeasured result
Turn on GCC optimisation for a suite of embedded-systems programs-O1 and -O2 cut execution time to 45% and 42% of the unoptimised -O0 time (Kästner et al., 2017).
Upgrade from GCC 4 to GCC 13 at the same setting, on the C programs in SPEC CPU 2017Estimated time and energy costs fell by 13% at -O2 and 9% at -O3 (Keiserås et al., 2026).

Even which setting is faster can depend on the setup. In one experiment, changing the size of the execution environment or the order in which program pieces were linked could reverse whether -O2 or -O3 appeared faster (Mytkowicz et al., 2009).

How far compiler output can be trusted

A compiler is correct when its translation preserves the program’s specified behaviour. Tests can reveal defects, while a proof can establish what a successful translation preserves under stated assumptions (Yang et al., 2011; Tan et al., 2019). A separate question is whether the compiler executable itself matches the compiler’s source code (Wheeler, 2009).

What bug reports show

Bug counts here rest on one hand-checked sample and one tracker study, and cover only what trackers record. Compiler bugs can appear before a program runs or change what it does. In a sample of 219 fixes linked to bug reports, the most frequent symptoms were incorrect messages, rejection of valid programs and wrong generated code (He & Zhong, 2025).

Fixes can take months. Across about 50,000 fixed GCC and LLVM reports, the mean time from filing to closing a bug was 200 days for GCC and 111 days for LLVM (Sun et al., 2016). One later analysis excluded LLVM’s newer tracker because it did not mark which reports were fixed (Zhong, 2025).

What tests detect

Randomly generated programs, a technique called fuzzing, find bugs in widely used compilers. Csmith generates C programs for differential testing, which compares the results of different compilers or settings on the same program. Its authors reported more than 325 bugs in mainstream C compilers over three years (Yang et al., 2011).

A program’s own tests rarely catch a compiler bug that changed it. In one study, almost half the sampled bugs, found by random test programs, changed the compiled binary of at least one package, yet the packages’ tests produced only two failures (Marcozzi et al., 2019). Those tests ran a median of 47% of statements, so about half the code went unexercised (Marcozzi et al., 2019).

What a proof guarantees

A semantic preservation theorem relates a program’s behaviour before and after a successful translation, but only between the stages it names. CompCert’s theorem starts from a tree representing the analysed C program and ends with assembly instructions, before they become machine code and are linked (CompCert, n.d.). An audit found further assumptions, including how the source and target languages are modelled and how the compiler uses external algorithms (Monniaux & Boulmé, 2022).

CakeML’s theorem reaches the machine code itself. It sets conditions on the machine’s starting state, configuration and environment, and allows the program to run out of memory after behaving correctly up to that point (Tan et al., 2019).

Testing can cover what a proof leaves out. Csmith found defects in an older, unverified part of CompCert that processed source programs. It found no wrong-code errors in the early-2011 development compiler after about six years of processor time (Yang et al., 2011).

Checking the built compiler

Compilers are often built by bootstrapping: an older version of a compiler builds a newer version of itself, usually in stages (Xu, 2025b). In his 1984 lecture “Reflections on trusting trust”, Ken Thompson used this to show that reviewing a program’s source leaves open whether the tools that built it can be trusted (Thompson, 1984).

Diverse double-compiling rebuilds a compiler with a different, independent compiler to check that the executable matches its stated source. David Wheeler proved that matching output establishes this under the method’s assumptions; what the source itself does still needs review (Wheeler, 2009). In one demonstration, three Linux distributions built identical GNU Mes binaries starting from different versions of GCC, and a later TinyCC build matched them (Reproducible Builds, 2019).

Error messages for beginners

Studies disagree on whether enhanced compiler error messages help beginners, partly because they measure different outcomes. With Java learners, enhanced messages reduced repeated errors per common message but made no significant difference to total errors per student. Another study found no significant effect of adding explanations and examples, and a working group judged comparisons between the two inconclusive (Becker, 2016; Denny et al., 2014; Becker et al., 2019).

Explanations can also be judged by how fast beginners fix errors. In an experiment with 106 participants repairing six C programs, GPT-4 explanations beat standard compiler messages on repair time in one of the six tasks (Santos & Becker, 2024).

Further reading

Known gaps

What this article cannot tell you yet.

  1. The early history does not survey compiler development in the Soviet Union or Japan.
  2. The benchmark studies do not establish an average speed gain across production programs and hardware.
  3. The probability of meeting a compiler bug in a production compilation is not estimated.
  4. Whether improved error messages help beginners on later tasks remains open.

References

Solid source
Rated highly trustworthy by the research, and every passage taken from it was found in it.
Weak source
Rated low, or a passage taken from it was not found in it.
Medium trust
Rated medium by the research.
Not checked
Rated high, but its passages could not all be checked against it.
Show all 48 references