sysprog21/shecc logo

sysprog21/shecc

Free

A self-hosting and educational C optimizing compiler

FreeFree tier
Type
Open Source

About sysprog21/shecc

shecc is a self-hosting and educational C optimizing compiler built from scratch, targeting both 32-bit Arm (ARMv7-A) and RISC-V (RV32IM) architectures. It generates executable Linux ELF binaries without requiring external assemblers or linkers, and includes a minimal C standard library for basic I/O. Written in portable ANSI C, shecc employs a two-pass compilation process: the first pass checks syntax and decomposes complex statements, while the second pass translates to machine code. It features an architecture-independent static single assignment (SSA)-based middle-end for optimizations and a register allocation system compatible with RISC-style architectures. The compiler supports a substantial subset of the C language, including data types (char, int, struct, enum, typedef, pointers), control flow (if, while, for, do-while, switch, etc.), all arithmetic/logical/bitwise operators with compound assignments, arrays (including multi-dimensional), functions (including variadic with pointer arithmetic), full pointer arithmetic, preprocessor directives (#define, #ifdef, #include, etc.), and function-like macros with VA_ARGS. shecc is verified on Raspberry Pi 3 (ARM) and via QEMU (RISC-V), and its bootstrapping process ensures the compiler can compile its own source code identically across stages.

Key Features

Self-hosting: can compile its own source code, verified through a bootstrapping process
Targets both ARMv7-A and RV32IM architectures, generating Linux ELF binaries
Written in portable ANSI C, compatible with most platforms
Two-pass compilation: first pass for syntax analysis and statement decomposition, second pass for machine code generation
Architecture-independent static single assignment (SSA)-based middle-end for optimizations
Register allocation system designed for RISC-style architectures
No external assembler or linker required; integrated machine code generator
Minimal C standard library for basic I/O on GNU/Linux
Supports a wide subset of C including struct, enum, typedef, pointers, arrays, variadic functions, and preprocessor directives with macro support
Bootstrapping validation ensures byte-identical compilers across stages

Pros & Cons

Pros
  • Open source with a permissive license, hosted on GitHub
  • Self-hosting capability demonstrates compiler correctness and completeness
  • Educational value: well-structured codebase suitable for study and modification
  • Supports two modern 32-bit architectures (ARM and RISC-V)
  • Includes basic optimization techniques via SSA middle-end
  • No dependencies on external assemblers or linkers for binary generation
  • Minimal standard library reduces complexity while enabling real programs
  • Active community and contributions from sysprog21 organization
Cons
  • Only supports a subset of the C language (not fully standard-compliant)
  • Targets only 32-bit architectures; no 64-bit or x86 support
  • Minimal standard library may lack features needed for complex programs
  • Bootstrapping requires QEMU or physical ARM/RISC-V hardware for full verification
  • Not intended for production use; primarily educational and experimental
  • Limited documentation beyond the README and repository files

Best For

Educational tool for learning compiler design and optimization techniquesTeaching how self-hosting compilers and bootstrapping workCross-compiling C code to ARMv7-A or RV32IM for embedded or educational environmentsResearch and experimentation with SSA-based optimization on real architecturesDemonstrating a minimal yet functional C compiler with multiple target backends

FAQ

What architectures does shecc target?
shecc targets ARMv7-A (32-bit Arm) and RV32IM (32-bit RISC-V) architectures, generating Linux ELF binaries.
Is shecc self-hosting?
Yes, shecc is self-hosting. The bootstrapping process involves compiling the compiler with itself in multiple stages, verifying that the generated binaries are byte-identical.
What subset of C does shecc support?
shecc supports data types like char, int, struct, enum, typedef, and pointers; control flow (if, else, while, for, do-while, switch, etc.); all arithmetic, logical, bitwise, and assignment operators; arrays (including multi-dimensional); functions with fixed and variadic arguments; full pointer arithmetic; preprocessor directives (#define, #ifdef, #include, etc.), and function-like macros with __VA_ARGS__.
Does shecc require an external assembler or linker?
No, shecc includes an integrated machine code generator and does not require any external assembler or linker.
How can I verify the bootstrapping of shecc?
The bootstrapping process involves three stages: stage0 compiles shecc with an ordinary compiler; stage1 uses that binary to compile shecc's source into an ARM/RISC-V binary; stage2 uses the stage1 binary (via QEMU or on real hardware) to compile again. If stage1 and stage2 outputs are byte-identical, bootstrapping is successful.