CompCert Formally-Verified C Compiler

repository·master·Indexed 24 days ago

https://github.com/absint/compcert

A formally-verified C compiler for the core C language, verified using the Coq proof assistant to guarantee that generated assembly code matches source C semantics. It supports ARM, PowerPC, RISC-V, and x86 architectures. The toolset includes the ccomp CLI for compilation, the clightgen tool for transforming C source into Coq-formatted abstract syntax, and a reference interpreter for executing C files. It includes a runtime support library for 64-bit integer arithmetic and va_arg macro implementation.

Tokens
2.4K
Snippets
2
Records
15
Agent score
81%

What's inside CompCert

  1. Overview of CompCert verified C compiler

    master

    CompCert is a formally-verified C compiler for the core C language. It is distinguished by being verified using the Coq proof assistant, which provides a formal guarantee that the generated assembly code behaves exactly as prescribed by the semantics of the source C code.

    Supported Architectures:

    • ARM
    • PowerPC
    • RISC-V
    • x86
  2. Understand CompCert licensing and commercial options

    master

    CompCert is not free software. The version provided in this repository is a non-commercial release restricted to:

    • Evaluation
    • Research
    • Educational purposes
    • Personal purposes

    For professional use without these restrictions, including access to professional support and additional features, a commercial version must be purchased from AbsInt.

  3. Use the clightgen tool to generate Coq files

    master

    Use clightgen to convert C source files into Coq-formatted abstract syntax files. For every input file src.c, the tool generates a corresponding src.v file containing the Clight or Csyntax representation.

    These generated .v files are intended for use in Coq sessions for interactive verification, such as with the VST toolchain.

    Command Syntax:

    clightgen [options] <C source files>

    Options:

    • Run clightgen -help to see all available options.
    • Many options are shared with the ccomp compiler. For a complete list of compiler-compatible options, refer to the CompCert user's manual.
  4. Enable and build the clightgen tool

    master

    The clightgen tool is an experimental feature that transforms C source files into Clight or Csyntax abstract syntax, outputting them as Coq-formatted .v files. To use it, you must explicitly enable it during the CompCert configuration step.

    1. Configure CompCert with the -clightgen flag:
      ./configure -clightgen
    2. Build CompCert using your standard build process.

    Once built, the clightgen binary will be located in the same directory as the ccomp compiler.

    ./configure -clightgen
  5. Understand the CompCert runtime support library

    master

    The CompCert runtime support library is a collection of helper functions required by code generated by the CompCert compiler. It primarily provides implementations for:

    • 64-bit integer arithmetic: Essential for handling 64-bit operations on architectures where they are not natively supported or require specific handling.
    • va_arg macro implementation: Provides the logic necessary to implement the va_arg macro from <stdarg.h>, enabling variadic function support.

    The library is implemented in assembly language for specific architectures to ensure performance and correctness. If you need to understand the logic behind the assembly implementations, a reference C implementation is available in the c/ directory.

  6. Use CompCert in Interpreter Mode

    master

    CompCert can execute C files using a reference interpreter instead of compiling to machine code.

    Interpreter Options:

    • -interp: Execute the given .c files using the reference interpreter.
    • -quiet: Suppress diagnostic messages from the interpreter.
    • -trace: Produce a detailed trace of reductions.
    • -random: Randomize execution order.
    • -all: Simulate all possible execution orders.
  7. Configure CompCert configuration files

    master

    You can specify how CompCert should be configured using files or environment variables:

    1. Via CLI flag: Use -conf <file> to read a specific configuration file. This has the highest precedence.
    2. Via CLI flag: Use -target <triple> to read configuration from <triple>.ini instead of the default compcert.ini. The file is searched for in the CompCert installation's share directory.
    3. Via Environment Variable: Set COMPCERT_CONFIG to the path of the configuration file. This takes precedence over default search paths and the -target option, but is overridden by -conf.
  8. Use the ccomp CLI

    master

    CompCert C is a formally verified compiler for the C programming language, designed for life-critical and mission-critical software. It accepts most of the ISO C 99 language and produces machine code for PowerPC (32bit), ARM (32bit), AArch64 (ARM 64bit), x86 (32bit and 64bit), and RISC-V (32bit and 64bit) architectures.

    Basic Syntax:

    ccomp [options] file ...

    Recognized Source Files:

    • .c: C source file.
    • .i, .p: C source file that should not be preprocessed.
    • .s: Assembly file.
    • .S: Assembly file that must be preprocessed.
    • .o: Object file.
    • .a: Library file.
  9. Save intermediate files for tracing and debugging

    master

    You can use -d<suffix> flags to save various stages of the compilation process to files for inspection:

    • -dprepro: Save C file after preprocessing (<file>.i).
    • -dparse: Save C file after parsing and elaboration (<file>.parsed.c).
    • -dc: Save generated CompCert C (<file>.compcert.c).
    • -dclight: Save generated Clight (<file>.light.c).
    • -dcminor: Save generated Cminor (<file>.cm).
    • -drtl: Save RTL at various optimization points (<file>.rtl.<n>).
    • -dltl: Save LTL after register allocation (<file>.ltl).
    • -dmach: Save generated Mach code (<file>.mach).
    • -dasm: Save generated assembly (<file>.s).
    • -dall: Save all generated intermediate files (<file>.<ext>).
    • -sdump: Save abstract syntax tree of generated assembly for post-linking validation (<file>.json).
  10. Manage Diagnostics and Warnings

    master

    CompCert provides extensive diagnostic controls to ensure code quality and conformance.

    Warning Control:

    • -Wall: Enable all warnings.
    • -W<warning>: Enable a specific warning class (e.g., -Wcompare-distinct-pointer-types).
    • -Wno-<warning>: Disable a specific warning class.
    • -w: Suppress all warnings.
    • -Werror: Treat all warnings as errors.
    • -Werror=<warning>: Treat a specific warning as an error.
    • -Wno-error=<warning>: Prevent a specific warning from being treated as an error even if -Werror is set.
    • -Wfatal-errors: Abort compilation immediately upon encountering an error.
    • -fmax-errors=<n>: Limit the number of error messages printed (default: unlimited).

    Diagnostic Formatting:

    • -fdiagnostics-format=<format>: Select format for location information (ccomp (default), msvc, or vi).
    • -fdiagnostics-color / -fno-diagnostics-color: Toggle colored diagnostics.
    • -fdiagnostics-show-option: Print the option name with mappable diagnostics.