Welcome to the Cow Compiler โ a lightweight compiler written in C that compiles a minimalistic language called CowLang into LLVM IR.
This project walks you through a complete compilation pipeline, making it ideal for learning how compilers work at a low level.
This compiler includes the following stages:
- Lexer: Tokenizes the input source code.
- Parser: Parses tokens into an abstract syntax tree (AST)
- AST: Represents the structure of the program
- IR Generation: Converts the AST into a simple intermediate representation (IR)
- LLVM Code Emission: Emits LLVM IR for a simple
main()function
- ๐งฎ Basic arithmetic expressions (
+,-) - ๐ Variable declarations
- ๐ง Constant folding via IR
- ๐งพ LLVM IR code generation (for testing purposes)
- ๐ง Easy to extend (add new operators, variables, data types)
- The current LLVM IR backend is basic and linked-list-based.
- LLVM output is minimal โ a simple
mainfunction is emitted. - You are encouraged to:
- โ
Extend the linked list IR to support variable types (
VAR) - ๐ Replace
switch-casebased IR-to-LLVM mapping with real LLVM C API for scalability and real-world usage
- โ
Extend the linked list IR to support variable types (
x = 4 + 4;
make # Compile all source files
make run # Compile + run the CowLang file
Custom IR (Extend the Linkedlist for printing):
t0 : const 4 t1 : const 4 t2 : add t0, t1 LLVM IR (saved to output.ll):
define i32 @main() { entry: %t0 = add i32 4, 4 ret i32 %t0 }
cow-compiler/
โโโ front-end/
โ โโโ lexer/ # Lexical analyzer
โ โโโ parser/ # Syntax parser
โ โโโ ast/ # Abstract syntax tree definitions
โ โโโ token/ # Token types and utilities
โโโ middle-end/
โ โโโ ir/ # Custom intermediate representation (IR)
โ โโโ irgen/ # AST โ IR transformation logic
โโโ back-end/
โ โโโ llvm/ # IR โ LLVM IR generator
โ โโโ evaluator/ # IR evaluator (optional)
โโโ tests/ # Sample test cases for CowLang
โโโ src/
โ โโโ main.c # Entry point (main compiler driver)
โโโ Makefile # Build system
โโโ README.md # Project documentation
Have questions, ideas, or suggestions? Want to contribute but donโt know where to start? Letโs talk!
We use GitHub Discussions to foster a collaborative and welcoming community around the Cow Compiler project. Whether you're a beginner learning how compilers work or an experienced contributor looking to improve our IR/LLVM pipeline โ youโre welcome here!
-
๐ก Ask Questions
Not sure how something works? Curious about compiler internals? Ask away! -
๐ง Share Ideas
Got a feature request or a suggestion? Weโd love to hear your thoughts. -
๐ Report Issues
Found a bug or unexpected behavior? Drop a message before opening an issue. -
๐ Collaborate on Features
Join other developers to brainstorm or discuss new enhancements.
We welcome all contributions โ whether you're fixing bugs, improving documentation, or adding exciting new features!