rcc
rcc: a Rust C compiler
A C compiler written in Rust, with a focus on good error messages. Warning: my first rust project, code quality is pretty low.
Running
rcc
reads from standard in by default, so you can type in code directly.
It's not interactive though, you have to hit Ctrl+D to indicate end of file (Ctrl+Z on Windows).
Use rcc --help
for all options (or see below).
Running on Windows
You need to have cc
on your PATH. You can either install mingw + gcc or MSVC.
Other than that, it should work exactly the same as on Linux.
TODO
- Complex assignment (
a /= 5
) - Multiple translation units (files)
- Bitfields?
- Compile on non-x86 platforms
- Cross-compile
Examples
$ cat tests/runner-tests/readme.c
// output: j is 6
int ;
typedef struct s *sp;
int i = 1;
int a = ;
float f = 2.5;
my_struct;
int ;
int
int
$ rcc tests/runner-tests/readme.c
$️ ./a.out
j is 6
Debug output
$ cat tests/runner-tests/cpp/if/defined.c
// code: 2
int i = 2;
syntax
int
$ rcc -E tests/runner-tests/cpp/if/defined.c
int i = 2 ; int
$ echo 'int i = 1 + 2 ^ 3 % 5 / 2 & 1; int main(){}' | rcc --debug-ast
auto int i = ;
extern int
$ cat tests/runner-tests/hello_world.c
int
$ rcc --debug-asm tests/runner-tests/hello_world.c
function u0:0 -> i32 system_v
$ ./a.out
Hello, world!
All options
$ rcc --help
rcc 0.5.0
Joshua Nelson <jyn514@gmail.com>:Graham Scheaffer <me@gisch.dev>
A C compiler written in Rust, with a focus on good error messages.
Homepage: https://github.com/jyn514/rcc/
usage: rcc [FLAGS] [OPTIONS] [<file>]
FLAGS:
--debug-asm If set, print the intermediate representation of the program in addition to compiling
-a, --debug-ast If set, print the parsed abstract syntax tree in addition to compiling
--debug-lex If set, print all tokens found by the lexer in addition to compiling.
-h, --help Prints help information
-c, --no-link If set, compile and assemble but do not link. Object file is machine-dependent.
-E, --preprocess-only If set, preprocess only, but do not do anything else.
Note that preprocessing discards whitespace and comments.
There is not currently a way to disable this behavior.
-V, --version Prints version information
OPTIONS:
-o, --output <output> The output file to use. [default: a.out]
--max-errors <max> The maximum number of errors to allow before giving up.
Use 0 to allow unlimited errors. [default: 10]
ARGS:
<file> The file to read C source from. "-" means stdin (use ./- to read a file called '-').
Only one file at a time is currently accepted. [default: -]
Testing
# optionally, you can fuzz the compiler
# it may be more helpful to just `grep -R unimplemented src`, though
# libFuzzer/AFL
# Honggfuzz:
# Running Honggfuzz locally requires some parameters to use it at its full potential,
# so it is probably a good idea to have a look here: https://github.com/rust-fuzz/honggfuzz-rs/blob/master/README.md
# and here: https://github.com/google/honggfuzz/blob/master/docs/USAGE.md
# we suggest the following:
HFUZZ_RUN_ARGS="--tmout_sigvtalrm --exit_upon_crash"
FAQ
See FAQ.md
Implementation Defined Behavior
Contributing
See CONTRIBUTING.md. This also includes reporting bugs.