Rustcc - A C compiler written in Rust


I’m still really new to Rust but I thought I’d have a cool idea to share.

I’ve been thinking about how cool it’d be to build a C compiler completely written in Rust.

I have no knowledge how to build a compiler (regardless of the language) so I’m useless but if someone is feeling ambitious enough, I think I’d send a few Bitcoins their way.

And no, not a full Bitcoin either lol, at most, I’d be able to spare a few dollars worth.


should be doable to some level considering things out there like ‘TinyC’, perhaps it would be a stepping stone toward getting rust truly self-hosting i.e. not needing C++ LLVM as a back end (actually you could break this into two parts. “a C compiler to LLVM written in Rust, and a LLVM to x86, ARM , … written in Rust”

Another thing that I think would be interesting is a single compiler middle which could handle a superset of Rust and C++ features, although this would be a huge task (and very difficult to figure out the details of).

This hypothetical language would not have a syntax defined, just a single place with concepts like ‘cpp_reference’, ‘rust_borrowed_pointer’ etc. If such a thing existed it could be used for making dialects of rust with more features (e.g. handling of C++ internal vtables for better interfacing to C++ libs), or retrofitting better type-inference/borrow-checks to a C++ derived language. You could imagine hypothetical syntax extentions to Rust or C++ code to handle the features of the other language in a consistent way (… e.g, safety? just as Rust has an unsafe{} block, imagine adding a safe{} block which reverses it’s effect, and your ‘superset-AST’ just happens to have a safe{} root coming from Rust, and unsafe{} root coming from C++)


What is the benefit of rewriting a C Compiler in Rust? The complexity of a C compiler means that you will end up emptying your wallet :laughing:


What is the benefit of rewriting a C Compiler in Rust?

Rust has an aspiration to replacing C; however, a C derivative, C++, is currently essential for the Rust ecosystem… Rust cannot exist independantly.

Having C/C++ compilers in rust would go some way toward ‘proving independence’. “my starting point is Rust… oh I have some legacy C code I need to compile… no problem, my pure Rust ecosystem can still process that…”

it’s debatable that you might want llvm->asm first, but I think I get the OP’s sentiment


(also … imagine if rustc could read C header files directly, saving the step of external bindgen )


A full compiler is a quite large project for learning Rust. Maybe start with writing just a C preprocessor? That’s a more manageable scope for one person, and will already give you a taste of parsing and I/O in Rust, and also how bizarre and underspecified C is.


A LLVM replacement is a ton of work. There’s the cretonne project, written in Rust, which is an alternative backend for Rust but I don’t think it aims to be a full LLVM replacement.

Maybe someday someone will have new ideas for s compiler design and they’ll use Rust. LLVM started as Chris Lattner’s master’s thesis project and then caught on more widely. Perhaps we’ll see that again but in Rust.


Sure I’ve read a topic on one, (maybe not this site.) Was extremely minimal one that had been converted to rust.

All I find now is rucc.