A Compiler translating C code (coherent to the C11 standard) into x86-64 bit assembly. Follows the cdecl calling convention. Grammar documentation follows Backus–Naur form.
- Unary Operators
- Arithmetic Negation (-)
- Logical Negation (!)
- Bitwise Complement (~)
- Arithmetic Binary Operators
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulo (%)
- Logical And (&&)
- Logical Or (||)
- Bitwise And (&)
- Bitwise Or (|)
- Left Shift (<<)
- Right Shift (>>)
- Xor (^)
- Relational Binary Operators
- Equality (==)
- Inequality (!=)
- Comparison (<, >, <=, >=)
- Local Variables
- Assignment
- Declaration
- Global Variables
- Implementation consistent with position-independent execution
- Calculates constant arithmetic expressions at compile time
- Conditionals
- Ternary Expressions
- If Statements
- If-Else Statements
- Compound Statements
- Variable Scoping
- Variable Shadowing
- Loops
- While
- Do-While
- For
- Functions and Function Calls
- Validation to reject programs inconsistent with the C11 standard for all features listed above
- Support for types beyond ints
- Pointers
- Additional optimization layers
CCIR follows a three step architecture:
- Lexing: Reads a .c file and converts the contents into a token stack
- Parsing: Executes semantic analysis to convert the token stack into an abstract syntax tree
- Code Generation: Traverses AST to produce x86 assembly
- MacOS does not natively support compilation of x86 assembly. If you wish to convert the generated .s file into an executable, please install Rosetta. See here.
- Requires int functions have a return statement, inconsistent with C11's specification that int functions without a return statement should automatically return 0.
- r10 and r11 not caller saved
- rbx not callee saved
You may run this compiler locally by following these steps:
- Clone the repo
git clone https://github.com/KyleleeSea/ccir
-
cargo build
- Assemble and produce executable for desired .c file
cargo run (path)
- Run the executable
./out
The above assumes the following:
- Installation of rust. If you do not have rust, please see: Rust installation guide.
- Installation of gcc to convert assembly into an executable.