My implementation of a C compiler for x64 processors.
Reference: Writing a C Compiler — Build a Real Programming Language from Scratch by Nora Sandler
- Stage 1 - Minimal C Program (function decl, return constant)
- Stage 2 - Unary Operators
- Negation (
-) - Bitwise Complement (
~) - Logical Negation (
!)
- Negation (
- Stage 3 - Binary Operators
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/)
- Addition (
- Stage 4 - Relational & Logical Operators
- Relational (
<, >, <=, >=) - Equality (
==, !=) - Logical AND (
&&) - Logical OR (
||)
- Relational (
- Stage 5 - Local Variables & Assignment
- Variable Declaration (
int x = ...) - Variable Usage (
return x) - Assignment (
x = y)
- Variable Declaration (
- Stage 6 - Conditionals
- If Statements (
if (cond) { ... } else { ... }) - Conditional Operator (
cond ? true : false) - Compound Statements / Blocks (
{ ... })
- If Statements (
- Stage 7 - Loops
- While (
while) - Do-While (
do ... while) - For (
for) - Break (
break) - Continue (
continue)
- While (
- Stage 8 - Functions
- Function Function Definition & Calling (Multi-argument)
- System V ABI (Arguments in registers)
- Stage 9 - Global Variables
- Global Variable Declaration
- Global vs Local Scope (Shadowing)
- Stage 10 - Static Variables
- Static Global Variables (Internal Linkage)
- Static Local Variables (Persistence)
- Stage 11 - Long Integers
- 64-bit Integer type (
long) - Type Promotion
- 64-bit Integer type (
- Stage 12 - Unsigned Integers
- Unsigned types (
unsigned int,unsigned long) - Unsigned Arithmetic & Comparison
- Unsigned types (
- Stage 13 - Bitwise Operators
- AND (
&) - OR (
|) - XOR (
^) - Shift Left (
<<) - Shift Right (
>>)
- AND (
- Stage 14 - Compound Assignment
- Arithmetic Compound (
+=,-=,*=,/=,%=) - Bitwise Compound (
&=,|=,^=,<<=,>>=)
- Arithmetic Compound (
- Stage 15 - Increment & Decrement
- Pre-increment (
++x) - Post-increment (
x++) - Pre-decrement (
--x) - Post-decrement (
x--)
- Pre-increment (
- Stage 16 - Pointers
- Pointer Declaration (
int *p) - Address-Of Operator (
&x) - Dereference Operator (
*p) - Pointer Arithmetic (Basic)
- Pointer Declaration (
The test runner
test_runner.py supports Automatic Test Discovery.
- Add a
.cfile totests/. - Include
// RETURN: <expected_code>in the file. make test-allwill automatically compile, run, and verify the exit code.