Crates.io | wrecc |
lib.rs | wrecc |
version | 0.2.0 |
source | src |
created_at | 2024-04-04 10:13:15.038211 |
updated_at | 2024-04-19 18:38:34.750321 |
description | A x86_64 C99 compiler written from scratch |
homepage | |
repository | https://github.com/PhilippRados/wrecc |
max_upload_size | |
id | 1196093 |
size | 152,361 |
wrecc
is a small,lean x86-64 C99 compiler written from scratch. The name is a play on the word wreck which describes a rusting ship on the sea floor.
The compiler emits x86-64 assembly in AT&T syntax, it adheres to the System V ABI which I could only test for Ubuntu and Macos. There are no dependencies you only need your assembler and linker which the compiler then invokes to create the final binary.
If you don't have the rust toolchain installed on your system you can install the latest binary (MacOs, Linux) from the releases directly:
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/PhilippRados/wrecc/releases/download/v0.2.0/wrecc-installer.sh | sh
Using cargo binstall
cargo binstall wrecc
or building from source
cargo install wrecc
Since not all keywords are currently implemented wrecc uses custom standard-headers which are built directly into the binary
The preprocessor implements all C99 preprocessor directives, except #line
, #error
and #pragma
. Most prominently it currently also misses function-like macros which are on the agenda though.
struct {
union {
int foo;
long baz;
} nested;
int array[16];
} bar = { .nested.foo = 3, .array[6] = 1};
#include <stdio.h>
typedef int (*BinaryOperation)(int, int);
typedef struct {
BinaryOperation add;
BinaryOperation subtract;
} Calculator;
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int main() {
Calculator calc = {add, subtract};
printf("Result of addition: %d\n", calc.add(10, 5));
printf("Result of subtraction: %d\n", calc.subtract(10, 5));
}
char **string_offset = (char **)&"hello" + (int)(3 * 1);
int array[(long)3 * 2 - 1];
Aside from the missing keywords these are the main missing features:
Here is a list of all the stuff still missing: todo
Wrecc also has nice looking messages. Error reporting doesn't stop after the first error. Using the --no-color
option you can switch off color-highlighting in errors. Currently there are only errors and no warnings.
C code | Errors |
---|---|
|
When compiling using the --dump-ast
option it prints the parse-tree
C code | AST |
---|---|
|
|
wrecc --help
cargo test --workspace
This runs all fixtures and compares them to the expected snapshot
bash tests/snapshot_tests.sh
Runs the fuzzer using afl.rs
// in fuzzer directory
cargo afl build
cargo afl fuzz -i inputs -o outputs target/debug/fuzz_target
Reasons for wrecc
not working properly on your machine:
-L <path>
option)If you want to help me with this compiler I would really welcome it. The easiest place to start is probably by implementing one of the missing keywords/types mentioned in the unimplemented features section. Make sure all tests still pass and implement your own if it's something new that is not already being tested.
Have a look at the documentation to get a high level overview of the compiler pipeline.
The following resources helped me in building this compiler: