MCPcopy Index your code
hub / github.com/PhilippRados/wrecc

github.com/PhilippRados/wrecc @v0.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.0 ↗ · + Follow
1,055 symbols 2,895 edges 246 files 91 documented · 9%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README
<img src="https://i.ibb.co/1bfxpbb/wreckage-mj.jpg" width="800">

Test

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.

Table of contents

Installation

Pre-built binaries

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

Cargo

Using cargo binstall

cargo binstall wrecc

or building from source

cargo install wrecc

Features

Since not all keywords are currently implemented wrecc uses custom standard-headers which are built directly into the binary

Preprocessor

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.

Compiler

Supported Keywords

keywords

Other than that it even supports:

Aggregate initialization with designated initializers

C struct { union { int foo; long baz; } nested; int array[16]; } bar = { .nested.foo = 3, .array[6] = 1};

Function pointers

```C #include

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

}

```

Constant folding

C char **string_offset = (char **)&"hello" + (int)(3 * 1); int array[(long)3 * 2 - 1];

Unimplemented Features

Aside from the missing keywords these are the main missing features: - [x] Arrays with unspecified size - [x] Compiling multiple files at once - [ ] Raw structs/unions as function argument-/return-types - [ ] Floating point types

Here is a list of all the stuff still missing: todo

Error messages

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
```C int foo(void); int main() { int a = foo(1); long *p = a; return p * 2; } ``` error

Ast pretty-printer

When compiling using the --dump-ast option it prints the parse-tree

C code AST
```C #define SIZE 16 void foo(char); int main() { int arr[SIZE] = {1, 2}; char p = (char)(*arr + 3); switch (p) { case 'c': foo(p); } } ``` ``` Declaration: -Decl: 'foo' FuncDef: 'main' -Declaration: --Init: 'arr' ---Aggregate: ----Scalar: -----Literal: 1 ----Scalar: -----Literal: 2 -Declaration: --Init: 'p' ---Scalar: ----Cast: 'char' -----Grouping: ------Binary: '+' -------Unary: '*' --------Ident: 'arr' -------Literal: 3 -Switch: --Ident: 'p' --Block: ---Case: ----Literal: 99 ----Expr: -----FuncCall: ------Ident: 'foo' ------Ident: 'p'
</td>
</tr>
</table>

#### Inspect all options by running `wrecc --help`

## Testing
#### Unit tests <a name="unit"></a>
cargo test --workspace
#### Snapshot testing <a name="snap"></a>
This runs all [fixtures](https://github.com/PhilippRados/wrecc/tree/master/tests/fixtures) and compares them to the expected [snapshot](https://github.com/PhilippRados/wrecc/tree/master/tests/snapshots)
bash tests/snapshot_tests.sh
#### Fuzzer
Runs the fuzzer using [afl.rs](https://github.com/rust-fuzz/afl.rs)
// in fuzzer directory cargo afl build cargo afl fuzz -i inputs -o outputs target/debug/fuzz_target ``` ## Troubleshooting Reasons for `wrecc` not working properly on your machine: - Unsupported architecture/OS - Cannot find libc in standard library search paths (can be fixed by passing custom search path using `-L

Extension points exported contracts — how you extend this code

PrintIndent (Interface)
(no doc) [5 implementers]
wrecc_compiler/src/compiler/parser/hir/expr.rs
Location (Interface)
Trait which can be implemented by different error-tokens which are all locatable [2 implementers]
wrecc_compiler/src/compiler/common/error.rs
OrEmpty (Interface)
provides printable default for empty iterator produced string [1 implementers]
wrecc_compiler/src/compiler/parser/hir/stmt.rs
ScratchClone (Interface)
hacky way to get clone to work on trait object [1 implementers]
wrecc_compiler/src/compiler/codegen/register.rs
IsZero (Interface)
(no doc) [2 implementers]
wrecc_compiler/src/compiler/parser/hir/expr.rs

Core symbols most depended-on inside this repo

clone
called by 347
wrecc_compiler/src/compiler/codegen/register.rs
push
called by 126
wrecc_compiler/src/compiler/scanner.rs
write_out
called by 113
wrecc_compiler/src/compiler/codegen/mod.rs
to_string
called by 62
wrecc_compiler/src/compiler/typechecker/mir/decl.rs
next
called by 53
wrecc_compiler/src/compiler/parser/double_peek.rs
unwrap_string
called by 50
wrecc_compiler/src/compiler/common/token.rs
size
called by 49
wrecc_compiler/src/compiler/common/types.rs
assert_fold
called by 46
wrecc_compiler/src/compiler/parser/fold.rs

Shape

Function 463
Method 432
Class 108
Enum 45
Interface 7

Languages

Rust67%
C32%
C++1%

Modules by API surface

wrecc_compiler/src/compiler/typechecker/mod.rs107 symbols
wrecc_compiler/src/compiler/parser/mod.rs78 symbols
wrecc_compiler/src/compiler/codegen/mod.rs75 symbols
wrecc_compiler/src/preprocessor/mod.rs60 symbols
wrecc_compiler/src/compiler/common/types.rs50 symbols
wrecc_compiler/src/compiler/common/environment.rs42 symbols
wrecc_compiler/src/preprocessor/scanner.rs36 symbols
wrecc_compiler/src/compiler/scanner.rs35 symbols
wrecc_compiler/src/compiler/parser/fold.rs31 symbols
wrecc_compiler/src/compiler/codegen/register_allocation.rs30 symbols
wrecc_compiler/src/compiler/codegen/register.rs28 symbols
wrecc_compiler/src/compiler/parser/hir/decl.rs20 symbols

For agents

$ claude mcp add wrecc \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact