Cmpler is a compiler for a subset of the C language (Small-C), built on top of LLVM and implemented in Rust. It consists of:
- cmpler-core: the compiler core library providing lexer, parser, semantic analysis, custom IR, and LLVM code generation.
- cmpler-cli: a command-line interface to compile, JIT‑execute, and link binaries.
- cmpler-tests: unit and integration tests covering all components.
- Lexical and syntactic analysis for Small-C syntax.
- Semantic checks: undefined variables, duplicates, and scope rules.
- Custom intermediate representation (IR) with lowering from AST.
- LLVM IR generation and object code emission using the
inkwellcrate. - Built-in optimizations (O0…O3) via LLVM pass manager.
- JIT execution through LLVM ExecutionEngine and AOT compilation with system linker.
- Configuration via
cmpler.tomland CLI flags.
- Rust toolchain (stable 1.x or later)
- LLVM development libraries
cc(system linker)
This repository is a Cargo workspace:
git clone https://github.com/yourusername/cmpler.git
cd cmpler
cargo build --releaseThe CLI binary will be at target/release/cmpler-cli.
Compile a Small-C source file (.c) to IR, object, or executable:
# Generate executable (a.out)
cmpler-cli build program.c
# Emit LLVM IR (.ll)
cmpler-cli build program.c --emit-ir
# Emit object code (.o)
cmpler-cli build program.c --emit-obj
# Specify output filename
cmpler-cli build program.c -o my_program
# Control optimization level: none, less, default, aggressive
cmpler-cli build program.c --opt-level aggressiveExecute via JIT or compile+run:
# JIT execution
cmpler-cli run program.c --jit
# Compile, link, and run
cmpler-cli run program.cCMPLER can load settings from a cmpler.toml file located in the working directory or any parent directory. Example cmpler.toml:
emit_ir = true
emit_obj = false
target = "x86_64-unknown-linux-gnu"
output = "build/myprog"
verbose = trueCLI flags override configuration file values.
cmpler/
├── cmpler-core/ # Compiler core library
├── cmpler-cli/ # Command-line tool
├── cmpler-tests/ # Unit and integration tests
└── Cargo.toml # Workspace manifest
- cmpler-core/src contains modules:
lexer,parser,ast,semantic,ir,codegen,config,error,logger,utils,driver. - cmpler-cli/src contains:
main.rs,args.rs, andcommands/{build.rs,run.rs}. - cmpler-tests/src contains automated tests for each compiler stage.
Contributions are welcome. To contribute:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/XYZ). - Implement tests and functionality.
- Submit a pull request for review.
This project is licensed under the MIT license. See LICENSE for details.