Skip to content

ABD-AZE/Velox

Repository files navigation

Velox

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.

Roadmap

Core Compiler Infrastructure

  • Implement a compiler driver with custom lexer, parser, IR generation, and assembly emission
  • Add support for preprocessing and linking (via GCC)

Operators & Expressions

  • 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 (+= -= *= /= <<= >>= %= &= |= ^=)

Variables & Scope

  • Add support for user-defined local variables
  • Add support for static keyword and global scope
  • Add support for extern keyword

Control Flow

  • 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

Functions

  • 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

Type System

  • 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

Pointers & Memory

  • 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)

Arrays

  • 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

Structures

  • 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)

Features

Syntax Restrictions

  • Multiple declarations: Declaration like int x, y; are not supported. Use separate lines: int x; and int 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 of int foo(int, int);
  • Array parameters: Not supported. Rewrite int foo(int a[]) to int foo(int *a)
  • Const keyword: Not supported
  • Function pointers: Not supported

Semantic Differences

  • Compound operations: These are expanded to their base expressions (e.g., a += 2 becomes a = a + 2, b++ becomes b = 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)

Not Yet Implemented

  • 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

Resources

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

Building Velox

make

The compiler binary will be generated in the build/ directory.

Make Targets

  • make or make all - Build release and debug versions (build/velox and build/debug)
  • make debug - Build only the debug version with symbols
  • make install - Install to /usr/local/bin (requires sudo) or custom path with PREFIX=~/.local
  • make uninstall - Remove installed binary
  • make test - Build and run unit tests (requires Google Test)
  • make clean - Remove all build artifacts
  • make help - Show all available targets
# Quick start
make                          # Build
sudo make install             # Install system-wide
make install PREFIX=~/.local  # Install locally (no sudo)

Usage

./build/velox source.vlx 

The command would generate the executable binary with the name "source"

Examples

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.

Contributing

We welcome contributions to Velox! Here are some guidelines to get you started:

Code Formatting

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

Pull Requests

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Ensure your code is formatted
  5. Submit a pull request

Authors

Abdullah Azeem
Pradyuman Singh Shekhawat
Pradyumn Kejriwal
Nisarg Prajapati

About

Velox - a minimal compiler for a C like language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors