Skip to content
프로젝트

소개

Rust로 처음부터 작성한 x86_64용 C99 컴파일러입니다.

프로그래밍 언어 및 런타임

이 수치를 측정하는 방법

What you can do

  • Compiling multiple files at once

측정된 스타 증가

in 24h
0
in 7 days
+1
in 15 days
+1
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

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 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
struct {
  union {
    int foo;
    long baz;
  } nested;
  int array[16];
} bar = { .nested.foo = 3, .array[6] = 1};
Function pointers
#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));
}
Constant folding
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
  • 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
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
#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'

Inspect all options by running wrecc --help

Testing

Run different types of tests with make <target>. Available targets can be inspected using make help as shown below.

$ make help
Usage:
  make <target>
  help             Display this help
  unit-tests       Run rust unit-tests
  snapshot-tests   Run snapshot-tests located in tests/
  c-testsuite      Requires c-testsuite (https://github.com/c-testsuite/c-testsuite) and env-var C_TESTSUITE set to its path
  fuzzer           Launch afl.rs fuzzer

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 <path> option)
  • If it's not mentioned in the unimplemented features section then please raise an issue

Contribution

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.

Project goals

  • Not relying on custom headers
  • Passing all C99 tests in c-testsuite
  • Compiling real-world C projects like Git

Resources

The following resources helped me in building this compiler:

관련 프로젝트 살펴보기

불러오는 중…