Skip to content

hmunye/cc2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

134 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cc2

Tiny C Compiler (subset of C17).

Warning

This project is experimental and not intended for production use.

Note

Currently only emits gas_x86-64_linux assembly. Other backends may be added in the future.

Language Features Supported

Types

  • int

Functions

  • Function definitions and declarations (supports types listed above)
  • int main(void) { ... }

Declarations

  • Local variables
  • Global variables
  • Lexical block scoping
  • Shadowing
  • Internal and external linkage (extern and static)
  • Static/automatic storage duration (local/file-scope variables and static locals)

Statements

  • return
  • Expression statements
  • if / else
  • goto/labeled statements
  • Compound statements ({ ... })
  • for / do / while
  • break / continue
  • switch
  • default / case labels
  • Empty statements (;)

Expressions

  • Integer constants
  • Variables
  • Unary operators: -, !, ~, ++ (postfix and prefix), -- (postfix and prefix)
  • Binary operators: +, -, *, /, %
  • Binary bitwise operators: &, |, ^
  • Binary shift operators: <<, >>
  • Binary logical operators: &&, ||
  • Comparison operators: <, <=, >, >=, ==, !=
  • Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
  • Conditional (ternary) operator: ? :
  • Function calls

Optimizations

  • Constant Folding
  • Unreachable Code Elimination
  • Copy Propagation
  • Dead Store Elimination
  • Register Allocation
  • Register Coalescing

Testing

Compiler is tested using an external test suite: nlsandler/writing-a-c-compiler-tests/.

Quick Start

Clone the repository and build the project:

git clone https://github.com/hmunye/cc2.git
cd cc2
cargo build --release

Display help and available flags:

./target/release/cc2 --help

Optionally, install globally:

cargo install --path .
cc2 --help

Example: hello_world.c

# 1. Preprocess separately using `GCC` or `cpp`:
gcc -E hello_world.c -o hello_world.i
# or
cpp hello_world.c -o hello_world.i

# 2. Compile the preprocessed file to assembly:
./target/release/cc2 hello_world.i

# 3a. Use `GCC` to assemble and link to an executable:
gcc hello_world.s -o hello_world

# 3b. Or manually invoke assembler and linker:
as hello_world.s -o hello_world.o   # assemble to object file
ld hello_world.o -o hello_world     # link to produce executable

# -----------------------------
# Shortcut: Use the -p flag
# -----------------------------
#
# The `-p` flag preprocesses and compiles in a single step.
# This is just a convenience; no separate preprocessed file is needed.
./target/release/cc2 -p hello_world.c

# Assemble and link as usual:
gcc hello_world.s -o hello_world

License

This project is licensed under the MIT License.

References

About

Tiny C Compiler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages