mcc crate provides a library API for embedding the C compiler in tools, tests, and other applications. It exposes a pipeline-based architecture with incremental recomputation powered by Salsa.
Quick Start
Parse, typecheck, lower, generate, and render a C program to assembly text:Compilation Pipeline
The compiler follows a classic multi-stage pipeline:Preprocessing
preprocess invokes the system C preprocessor (cc -E -P)Typechecking
typechecking::typecheck validates types and builds HIRLowering
lowering::lower_program converts AST to three-address code (TAC)Code Generation
codegen::generate_assembly produces assembly IRRendering
render_program converts assembly IR to textAssembling & Linking
assemble_and_link invokes the system toolchainCore Types
Database
Database::default() and pass by reference to all pipeline functions.
SourceFile
Ast
Text
&str, String, and Cow<'_, str>.
Capturing Diagnostics
Each stage accumulates diagnostics rather than panicking. Retrieve them using the stage’saccumulated helper:
Target Configuration
Rendering assembly requires a target triple:Platform-Specific Behavior
- macOS: Symbol names are rendered with a leading underscore (e.g.,
_main) - Linux: A
.note.GNU-stacksection is emitted for stack protection
Pipeline Functions
Preprocessing
cc argument is typically "cc" or "gcc".
Parsing
Typechecking
Lowering
Codegen
Rendering
Assembling
Next Steps
Database
Learn about the Salsa-powered incremental computation layer
Diagnostics
Handle errors and warnings with codespan-reporting
Callbacks
Hook into compilation stages with the driver API
Architecture
Understand the overall system design