https://github.com/clementtsang/rustcc
A (toy) C compiler written in Rust with no dependencies.
https://github.com/clementtsang/rustcc
assembly c compiler rust x86
Last synced: 3 months ago
JSON representation
A (toy) C compiler written in Rust with no dependencies.
- Host: GitHub
- URL: https://github.com/clementtsang/rustcc
- Owner: ClementTsang
- License: mit
- Created: 2018-12-22T04:58:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-24T15:25:39.000Z (about 1 year ago)
- Last Synced: 2025-03-18T22:11:29.468Z (3 months ago)
- Topics: assembly, c, compiler, rust, x86
- Language: Rust
- Homepage:
- Size: 187 KB
- Stars: 60
- Watchers: 1
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rustcc - a Rust C compiler
A basic C compiler written in Rust that compiles C code into x86 assembly, following GAS/AT&T syntax, with no external Rust dependencies. Inspired by [this post by Nora Sandler](https://norasandler.com/2017/11/29/Write-a-Compiler.html).Note this is mostly done for learning, and isn't intended to be used seriously.
## Features
Currently, rustcc supports the following features:
* Unary operators (logical negation, bitwise complements, negation)
* Binary operators (basic arithmetic, bitwise operations, comparisons)
* Local variables (assignment, declaration, variable calling, postfix and prefix incrementing)
* If-else branching
* Ternary operator
* While loops, do-while loops, for loops, break, continue
* Function calling and creationAs of now, rustcc only supports variables of type int.
## Installation
To install, ensure beforehand that you have [Rust and Cargo installed.](https://www.rust-lang.org/tools/install) After that, clone the repository. Then, run ``cargo build --release``.## Usage
To use the compiler, run the `rustcc` script as follows:
```
./rustcc /path/to/source.c
```
Upon running, the compiled executable file will be in the same directory and name as the input source file. The created assembly ``source.s`` file will be deleted upon running the script.Alternatively, you can directly run ``./target/release/rustcc /path/to/source.c`` (or ``target/release/rustcc.exe /path/to/source.c`` on Windows) to retain the assembly file.
## Disclaimer
rustcc is a project done purely out of personal interest. The compiled x86 code is most likely not optimized and the possibility of something not working or being supported is quite probable. I am not responsible for anything going wrong with the use of this.