Definition
Compiler correctness is the property that the code a compiler produces behaves as the source program’s semantics allows: every observable behavior of the compiled code, such as its input and output, is one that the source language permits for that program. The property is relative to three things that must be named: the semantics of the source language, the semantics of the target machine, and the programs, compiler options §12.3 and outside conventions covered. A verified compiler, such as CompCert for C, proves the property as a mathematical theorem.
The idea
Programmers test and verify source code, but the machine runs compiled code. Every guarantee established on the source reaches the machine only through the compiler, so a compiler that mistranslates a program silently voids the guarantee. Compiler bugs that produce wrong code are real. A study published in 2011 by Yang, Chen, Eide and Regehr [231] generated random C programs with a tool called Csmith and compared how different compilers translated them. It reported that, by early 2011, the development version of CompCert, a C compiler whose main passes are proved correct, was the only compiler tested in which Csmith could find no wrong-code errors.
Correctness here is not a promise that the compiled code follows the source line by line; correct compilers delete, merge and reorder code (the Compiler optimization §3.39 entry). It is a relation between two semantics: whatever the compiled code can be seen to do, the source program was allowed to do. A contract translated into another language is a fair picture. A faithful translation binds the parties to exactly the obligations the original allows. Where the original leaves a choice open, the translation may settle it either way; where a clause of the original is void, the translation owes nothing for that clause. The picture stops at scale: compiler correctness is one statement about every program the compiler accepts, not a judgment made translation by translation.
How it works
A compiler correctness claim has four parts.
Source semantics. A precise definition of what each source program may do, including which executions have undefined behavior §4.12. CompCert defines a semantics for its own version of C, called CompCert C, based on the ISO C99 standard [270].
Target semantics. A precise definition of what machine code does. CompCert defines one for the assembly language of each processor family it supports.
Observations. What counts as behavior. In CompCert, an observable behavior is the trace of calls to input and output functions and of accesses to volatile global variables, together with whether the program terminates, runs forever or stops on undefined behavior. Execution time §9.32 and memory consumption are not observed.
The theorem. It relates the observations of the compiled code to those of the source. CompCert’s manual states it as follows: if the compiler, applied to a source program , produces code without reporting a compile-time error, then the observable behavior of improves on one of the allowed observable behaviors of . To improve means to be equal, or to replace a run-time error of the source, such as a division by zero whose result is never used, with a defined behavior.
CompCert’s proof is checked by the Rocq proof assistant (formerly named Coq), a program that checks every step of a mathematical proof. The proof is built pass by pass. Each pass of the compiler, whether it translates one intermediate language §3.37 into the next or optimizes code within one, has its own preservation theorem, and composing these theorems gives the theorem for the whole compiler. The proof does not cover every step from source file to running program: CompCert’s manual lists preprocessing, parts of the front end such as elaboration and some early simplifications, assembling and linking as unverified, and the external assembler and linker §12.4 are not part of CompCert.
Instead of proving a compiler correct once, one can check each compilation after the fact. Translation validation §3.33 compares the source and the produced code of one run and either confirms that they correspond or reports a failure.
Undefined behavior, the ABI §12.47 (application binary interface) and compiler flags fix the scope of the claim.
Undefined behavior: once a source execution reaches it, the source semantics allows anything from that point on, so the compiled code may wrap, trap or skip a check, and the compiler is still correct.
The ABI: the conventions that separately compiled pieces of code rely on, such as the layout of data and the registers that carry arguments and results. For 32-bit Arm processors, the Procedure Call Standard for the Arm Architecture (AAPCS) passes the first four 32-bit arguments in registers
r0tor3, returns a 32-bit result inr0, and requires the stack pointer to be a multiple of 8 at every call between separately compiled functions. A theorem about one compiler assumes that all other code follows the same conventions.Compiler flags: they choose the language dialect, the semantics, the target processor with its ABI variant, and the optimizations. With GCC §12.17 (GNU Compiler Collection; GNU stands for GNU’s Not Unix) and Clang,
-fwrapvdefines signed overflow §4.15 in addition, subtraction and multiplication to wrap;-ffast-mathallows floating-point results that differ from those of IEEE 754 §4.49 arithmetic [271]; and, on Arm processors,-mfloat-abiselects whether floating-point arguments travel in floating-point registers or in ordinary ones.
Worked example
Undefined behavior. Consider int bigger(int x) { return x + 1 > x; } with a 32-bit int, whose largest value is 2,147,483,647. For x = 5, the source computes and , so it returns 1. For x = 2,147,483,647, the addition overflows, which is undefined behavior. Compiler A emits an addition and a comparison; on an Arm or x86 processor the addition wraps to , the comparison fails, and the function returns 0. Compiler B notes that holds for every x at which the addition is defined, and compiles the function to return 1. Both compilers are correct: the source semantics constrains neither of them at x = 2,147,483,647.
A flag changes the source semantics. With -fwrapv, signed addition is defined to wrap, so the source itself now computes and returns 0 at that input. Compiler B’s simplification is then incorrect, and a compiler honoring the flag must return 0. The same source text has two different semantics, and a correctness claim must say which one it is about.
The ABI. Under AAPCS, a caller of a function f that computes passes x = 5 in r0 and reads the result from r0. Suppose f is replaced by hand-written assembly that computes correctly but leaves the result in r1. The caller reads r0, which still holds the argument, and receives 5. The compiler translated the caller correctly and the assembly computed the right number, yet the program is wrong, because the two pieces do not follow the same ABI. No correctness theorem about the compiler can detect this mismatch.
Stated precisely
Let be a source program and the code the compiler produces from it. Let be the set of observable behaviors that the source semantics allows for , and the set that the target semantics allows for . An observable behavior is a trace of input and output events and volatile accesses, together with how the run ends: termination with a result, running forever, or, for the source, reaching undefined behavior, which the CompCert manual calls going wrong. A target behavior improves a source behavior when , or when goes wrong after trace and begins with that same trace .
Correctness, in the form CompCert proves, states: if the compiler produces from without reporting an error, then every improves some . If no behavior of goes wrong, this reduces to so the compiled code does only what the source allows. A consequence follows for any property of observable behavior: if no behavior of goes wrong and every behavior in has the property, then every behavior of has it too. This one-way inclusion is a form of refinement §3.32.
The guarantee rests on named assumptions.
The source semantics matches the language the programmer relied on.
The target semantics matches the real processor.
Every function outside the compiled code, including library functions and hand-written assembly, follows the ABI and behaves as the semantics assumes.
The unverified steps work: preprocessing, the unverified parts of the front end, assembling, linking and loading.
The compiler is used with options inside the theorem’s scope.
The proof assistant that checked the proof is itself correct.
Limits and common mistakes
A verified compiler is not a verified system. The source can implement the wrong rule, a library can break its contract, and the hardware can differ from its manual.
Time and memory are outside the theorem. CompCert’s observable behavior excludes execution time and memory consumption, so deadlines, timing side channels §1.29 and stack overflow §4.10 at run time are not covered.
Correct compilation does not repair source bugs. A source program with undefined behavior on some input has no required behavior on that input, and different correct compilers may produce code that behaves differently there.
Testing a compiler, for example with random programs, finds bugs but proves nothing about the programs not tested. A proof covers every program in its scope, and the scope is the part to read.
A theorem about one compiler covers that compiler’s own output. Linking objects from two compilers, or from one compiler run with two different sets of compiler flags, relies on an ABI agreement that neither compiler’s theorem checks.
Code outside the source semantics is outside the theorem. Inline assembly and hand-written assembly have no meaning in the semantics of C, so a theorem about C compilation says nothing about them.
Compiler correctness says nothing about details that are not observable. A correct compiler may remove an unused read, merge two stores or reorder ordinary memory accesses.
Questions answered
What role do undefined behavior, ABI, and compiler flags play?
Undefined behavior, the ABI (application binary interface) and compiler flags together fix the scope of a compiler correctness claim: which source executions it constrains, which outside code the compiled code may be combined with, and which language and target machine it is about.
Undefined behavior, behavior for which the language standard imposes no requirement, such as signed integer overflow in C, marks the executions the claim does not constrain. A correct compiler must preserve every execution that has no undefined behavior and may do anything once an execution reaches undefined behavior, so for such an input the compiled code may wrap, trap or skip a check. A guarantee about the machine code therefore also needs a separate argument that the source has no undefined behavior on the inputs that matter.
The ABI is the set of conventions that separately compiled code relies on: sizes, alignment §4.22 and layout of data, the registers that carry arguments and results, the registers a called function must preserve, and stack alignment. A correctness theorem for one compiler covers the code that compiler produced. Combining that code with libraries, the output of other compilers or hand-written assembly is correct only if all of them follow the same ABI.
Compiler flags select the language dialect, change the semantics, choose the target processor and ABI variant, and choose the optimizations. For the C compilers GCC and Clang,
-fwrapvmakes signed overflow in addition, subtraction and multiplication wrap, which defines executions that were undefined, and-ffast-mathallows floating-point results that differ from IEEE 754 arithmetic. A theorem, a validation or a test result holds for the configuration it was established with, and a change of flags can move a program outside it.
Related entries: Refinement (§3.32), Translation validation (§3.33), Guest-host ABI (§2.37), Undefined behavior (UB) (§4.12), Native application binary interface (ABI) (§12.47), Compiler flag (§12.3), Compiler optimization (§3.39). Further reading: [72, 78, 82, 231, 270, 271].