Velox is a C compiler implementation that translates C source code to x86-64 assembly. Built from scratch with a custom lexer, parser, intermediate representation (IR), and code generator.
- Implement a compiler driver with custom lexer, parser, IR generation, and assembly emission
- Add support for preprocessing and linking (via GCC)
- Add support for Unary Operations (Negation, Logical NOT, Bitwise NOT)
- Add support for Binary Operations (Add, Sub, Mul, Div, Rem)
- Add support for Bitwise Binary Operations (And, Or, Xor, LeftShift, RightShift)
- Add support for logical and relational operators (
!&&||==!=<><=>=) - Add support for ternary operator (
? :) - Add support for increment/decrement operators (
++--) - Add support for compound assignment operators (
+=-=*=/=<<=>>=%=&=|=^=)
- Add support for user-defined local variables
- Add support for static keyword and global scope
- Add support for extern keyword
- Add support for if-else construct
- Add support for compound statements
- Add support for goto statement and labels
- Add support for Loops (for, while, do-while, until, do-until)
- Add support for break and continue
- Add support for function declarations and definitions
- Add support for function calls
- Add support for variadic argument function calling (printf, scanf, etc.)
- Add support for returning void
- Add support for a type system (int, long, unsigned, double, char, pointers)
- Add support for Long Integers (64-bit)
- Add support for Unsigned Integers (uint, ulong)
- Add support for double precision floating point
- Add support for characters (char, signed char, unsigned char)
- Add support for type casting and implicit conversions
- Add support for Pointers
- Add support for Pointer Arithmetic
- Add support for address-of (
&) and dereference (*) operators - Add support for dynamic memory allocation (malloc, free)
- Add support for one-dimensional Arrays
- Add support for multi-dimensional Arrays
- Add support for array subscripting (
[]) - Add support for string literals and array initialization
- Add support for arrays of structs
- Add support for user-defined structures (struct)
- Add support for struct member access (
.operator) - Add support for struct pointer member access (
->operator) - Add support for struct compound initializers
- Add support for nested structs
- Add support for arrays of structs
- Add support for struct function parameters (pass by value)
- Add support for struct function return values (return by value)
- Multiple declarations: Declaration like
int x, y;are not supported. Use separate lines:int x;andint y; - Self-referencing initializers: Initializers cannot reference the variable being declared.
int a = a + 1;is not supported. Declare first, then assign:int a; a = a + 1; - Unnamed parameters: Function declarations must name all parameters. Write
int foo(int a, int b);instead ofint foo(int, int); - Array parameters: Not supported. Rewrite
int foo(int a[])toint foo(int *a) - Const keyword: Not supported
- Function pointers: Not supported
- Compound operations: These are expanded to their base expressions (e.g.,
a += 2becomesa = a + 2,b++becomesb = b + 1) - Pointer comparison: For comparing pointers, the base type needs to be the same. Cast to
void*if needed - sizeof('a'): Evaluates to 1 (character size), unlike GCC/Clang which return 4 (int promotion)
- Struct pass/return by value: Structures cannot be passed as function parameters or returned from functions by value
- Static struct variables: Partial support - some edge cases may not work
- Optimizations: No optimization passes are implemented yet
Our compiler implementation draws primarily from Nora Sandler's "Writing a C Compiler" book, the test suite credit also goes to Nora Sandler. The test suite can be found at: https://github.com/nlsandler/writing-a-c-compiler-tests
makeThe compiler binary will be generated in the build/ directory.
makeormake all- Build release and debug versions (build/veloxandbuild/debug)make debug- Build only the debug version with symbolsmake install- Install to/usr/local/bin(requires sudo) or custom path withPREFIX=~/.localmake uninstall- Remove installed binarymake test- Build and run unit tests (requires Google Test)make clean- Remove all build artifactsmake help- Show all available targets
# Quick start
make # Build
sudo make install # Install system-wide
make install PREFIX=~/.local # Install locally (no sudo)./build/velox source.vlx The command would generate the executable binary with the name "source"
The tests/ directory contains various example programs demonstrating Velox's features:
- Control Flow:
test_ifelse.vlx,test_loops.vlx,test_control_flow.vlx - Operators:
test_arithmetic.vlx,test_logical.vlx - Arrays:
test_array.vlx - Pointers:
test_pointer.vlx - Memory:
test_malloc.vlx - Functions:
test_variadic.vlx,test_recursive.vlx - File I/O:
test_file_manipulation.vlx - Command Line:
test_cmdline.vlx - Static Variables:
test_static.vlx - Algorithms:
tower_of_hanoi.vlx,string_reversal.vlx
Feel free to explore these examples to understand Velox's capabilities. Contributions of new example programs are welcome! If you create interesting test cases or example programs, please submit them to the tests/ directory via pull request.
We welcome contributions to Velox! Here are some guidelines to get you started:
We use clang-format for code formatting. Please ensure all your modified files are formatted before submitting:
clang-format -i path/to/modified/files/You can also run the formatter on all source files:
find -name "*.[ch]pp" | xargs clang-format -i- Fork the repository
- Create a feature branch
- Make your changes
- Ensure your code is formatted
- Submit a pull request
Abdullah Azeem
Pradyuman Singh Shekhawat
Pradyumn Kejriwal
Nisarg Prajapati