rcc is a toy C compiler written in Rust (In Progress). This compiler is inspired by the article - Writing a C Compiler. Tests are taken from the author's repository.
This program will output x64 Assembly in Intel syntax. Given the c program below as the first argument, rcc will emit the assembly language to stdout.
// test.c
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(1 + 2, 4);
return sum + sum;
}
$ cargo run test.c > test.s
$ gcc -o test test.s && ./test
$ echo $?
14
Although "int" is the only supported type, you can define variables and functions. Below are list of implemented features:
- Arithmetic Operator
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Logical Operator
- AND (&&)
- OR (||)
- NOT (!)
- Bitwise Operator
- AND (&)
- OR (|)
- XOR (^)
- Complement (~)
- Left Shift (<<)
- Right Shift (>>)
- Assignment Operator
- Assignment (=)
- Add and assign (+=)
- Subtract and assign (-=)
- Multiply and assign (*=)
- Divide and assign (/=)
- Modulo and assign (%=)
- Relational Operator
- Equal (==)
- Not Equal (!=)
- Less Than (<)
- Less Than Or Equal (<=)
- Greater Than (>)
- Greater Than or Equal (>=)
- Conditional
- if
- Ternary Operator (?)
- Loops
- for
- while
- do .. while
- break
- continue