Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

shecc

> 数据库
Open source

A self-hosting and educational C optimizing compiler

1.4K stars0 likes1 views
WebsiteGitHub

About

A self-hosting and educational C optimizing compiler

shecc : self-hosting and educational C optimizing compiler

Introduction

shecc is built from scratch, targeting 32-bit Arm, AArch64, 32-bit RISC-V, and x86-64, as a self-compiling compiler for a subset of the C language. Despite its simplistic nature, it is capable of performing basic optimization strategies as a standalone optimizing compiler.

Features

  • Generate executable Linux ELF binaries for ARMv7-A, AArch64, RV32IM, and x86-64.
  • Provide a minimal C standard library for basic I/O on GNU/Linux.
  • The cross-compiler is written in ANSI C, making it compatible with most platforms.
  • Include a self-contained C front-end with an integrated machine code generator; no external assembler or linker needed.
  • Utilize a two-pass compilation process: the first pass checks syntax and breaks down complex statements into basic operations, while the second pass translates these operations into target machine code.
  • Develop a register allocation system that is compatible with RISC-style architectures.
  • Implement an architecture-independent, static single assignment (SSA)-based middle-end for enhanced optimizations.
  • Support dynamic linking to allow generated executables to run with glibc.
  • Emit both ELF32 (Arm, RISC-V) and ELF64 (AArch64, x86-64) images; the ELF class follows the target pointer width.

Compatibility

shecc is capable of compiling C source files written in the following syntax:

  • data types: char, short, int, _Bool, void, struct, union, enum, typedef, and pointer types
  • condition statements: if, else, while, for, do-while, switch, case, default, break, continue, return, goto with labels, and general expressions
  • operators: all arithmetic, logical, bitwise, and assignment operators including compound assignments (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)
  • arrays: global/local arrays with initializers, multi-dimensional arrays
  • functions: function declarations, definitions, and calls with fixed arguments
  • variadic functions: basic support via direct pointer arithmetic (no <stdarg.h>)
  • typedef: type aliasing including typedef pointers (typedef int *ptr_t;)
  • pointers: full pointer arithmetic, multi-level pointer dereference (***ptr)
  • global/local variable initializations for all supported data types
    • e.g. int i = [expr];, int arr[] = {1, 2, 3};
  • preprocessor directives: #define, #if, #ifdef, #ifndef, #elif, #else, #endif, #undef, #error, and #include
  • function-like macros with parameters, __VA_ARGS__, stringification (#), and token pasting (##)

The Arm backend targets armv7hf with the Linux ABI, verified on Raspberry Pi 3. The AArch64 backend follows AAPCS64 and supports static and eager-bound dynamic linking, verified with QEMU AArch64 on eMag. Its images separate the load segments by 64 KiB so they load under any of the 4 KiB, 16 KiB and 64 KiB translation granules AArch64 Linux may be configured with. The RISC-V backend targets RV32IM, verified with QEMU. The x86-64 backend follows the System V AMD64 ABI and runs natively on an x86-64 GNU/Linux host, so no emulator is involved.

Bootstrapping

The steps to validate shecc bootstrapping:

  1. stage0: shecc source code is initially compiled using an ordinary compiler which generates a native executable. The generated compiler can be used as a cross-compiler.
  2. stage1: The built binary reads its own source code as input and generates a binary for the selected target.
  3. stage2: The generated target binary is invoked with its own source code as input and generates another target binary. It runs natively for x86-64 and on the Arm boards the build system recognizes; every other case, including the RISC-V target, goes through QEMU.
  4. bootstrap: Build the stage1 and stage2 compilers, and verify that they are byte-wise identical. If so, shecc can compile its own source code and produce new versions of that same program.

Prerequisites

Code generator in shecc does not rely on external utilities. You only need ordinary C compilers such as gcc and clang. However, shecc would bootstrap itself, so the target binaries have to run somewhere. Building for x64 on an x86-64 GNU/Linux host needs nothing extra. Building for Arm or RISC-V on such a host requires ISA emulation; install QEMU for Arm/RISC-V user emulation:

bash
$ sudo apt-get install qemu-user

The build system is able to verify whether the running machine can perform native execution without QEMU. The host machine may install the prebuilt fastfetch, which allows the build system to determine whether native execution can be enabled.

It is still possible to build shecc on macOS or Microsoft Windows. However, the second stage bootstrapping would fail due to qemu-arm absence, and the x64 target expects an x86-64 GNU/Linux host to execute its own output.

Additional packages

The dynamic linking mode needs an ELF interpreter and the matching glibc for the target. The x64 target resolves both from the host system, so it needs nothing beyond an x86-64 GNU/Linux installation. The Arm, AArch64, and RISC-V targets need a cross-compile GNU toolchain to obtain them.

For the Arm architecture, you can install the ARM GNU toolchain using apt-get:

bash
$ sudo apt-get install gcc-arm-linux-gnueabihf

Another approach is to manually download and install the toolchain from ARM Developer website. Select "x86_64 Linux hosted cross toolchains" - "AArch32 GNU/Linux target with hard float (arm-none-linux-gnueabihf)" to download the toolchain.

For AArch64 dynamic binaries, install the matching toolchain and user emulator:

bash
$ sudo apt-get install gcc-aarch64-linux-gnu qemu-user

Since apt-get does not provide the necessary RISC-V GNU toolchain, it must be downloaded manually if you want to run a dynamically linked shecc targeting the RISC-V architecture. For instance, you can download and extract the riscv32-glibc-ubuntu-22.04-gcc.tar.xz package from the riscv-gnu-gcc repository.

Build and Verify

Configure which backend you want. shecc supports the ARMv7-A, AArch64, RV32IM, and x86-64 backends, with Arm as the default:

bash
$ make config ARCH=arm
# Target machine code switch to arm

$ make config ARCH=riscv
# Target machine code switch to riscv

$ make config ARCH=arm64
# Target machine code switch to arm64

$ make config ARCH=x64
# Target machine code switch to x64

The selected target is recorded in the tree, so a later make with a different ARCH stops and asks for an explicit make config ARCH=... rather than pairing one target's settings with another target's generated configuration.

Run make and you should see this:

bash
$ make
  CC+LD	out/inliner
  GEN	out/libc.inc
  CC	out/src/main.o
  LD	out/shecc
  SHECC	out/shecc-stage1.elf
  SHECC	out/shecc-stage2.elf

Run make DYNLINK=1 to use the dynamic linking mode and generate the dynamically linked compiler:

…

For development builds with memory safety checks:

bash
$ make sanitizer
$ make check-sanitizer

File out/shecc is the first stage compiler. Its usage:

bash
$ shecc [-o output] [+m] [--dot] [--no-libc] [--dump-ir] [--dynlink] [-E] <infile.c>

Compiler options:

  • -o : Specify output file name (default: out.elf)
  • +m : Use hardware multiplication/division instructions (default: disabled)
  • --dot : Write the SSA control-flow graph in Graphviz DOT format and stop
  • --no-libc : Exclude embedded C library (default: embedded)
  • --dump-ir : Dump intermediate representation (IR)
  • --dynlink : Use dynamic linking (default: disabled)
  • -E : Preprocess only; write the expanded token stream and stop

Example 1: static linking mode

bash
$ out/shecc -o fib tests/fib.c
$ qemu-arm fib

The compiler marks its own output executable, so neither example needs a chmod +x in front of the run. Earlier revisions did: the mode used to be whatever the libc that opened the file chose, which was 0666 for a glibc-linked build.

An x64 build produces a native binary, so ./fib runs it directly with no emulator in front.

Example 2: dynamic linking mode

For AArch64, /usr/aarch64-linux-gnu is a typical ELF interpreter prefix. The path may differ if you manually install a GNU toolchain, so set it to the actual sysroot.

bash
$ out/shecc --dynlink -o fib tests/fib.c
$ qemu-arm -L /usr/arm-linux-gnueabihf fib      # ARCH=arm
$ qemu-aarch64 -L /usr/aarch64-linux-gnu fib    # ARCH=arm64

Unit Tests

shecc has one behavioral test flow. make check runs the executable and compiler-error tests with both the host-built and self-hosted compilers, then runs the selected target's ABI tests. To run it:

bash
# Add 'DYNLINK=1' if using the dynamic linking mode.
$ make check          # Consolidated suite: stage 0, stage 2, and ABI tests
$ make check-stage0   # Test stage 0 compiler only
$ make check-stage2   # Test stage 2 compiler only
$ make check-abi-stage0 # Check the target calling convention (stage 0)
$ make check-abi-stage2 # Same, for the stage 2 compiler
$ make check-sanitizer # Test with AddressSanitizer and UBSan

The test suite covers:

  • Basic data types and operators
  • Control flow statements
  • Arrays and pointers (including multi-level dereference)
  • Structs, enums, and typedefs
  • Variadic functions
  • Preprocessor directives and macros
  • Calling convention conformance for the selected target
  • Self-hosting validation

Reference output (Arm target; pointer sizes read 8 on x86-64):

  TEST STAGE 0
...
int main(int argc, int argv) { exit(sizeof(char)); } => 1
int main(int argc, int argv) { int a; a = 0; switch (3) { case 0: return 2; case 3: a = 10; break; case 1: return 0; } exit(a); } => 10
int main(int argc, int argv) { int a; a = 0; switch (3) { case 0: return 2; default: a = 10; break; } exit(a); } => 10
OK
  TEST STAGE 2
...
int main(int argc, int argv) { exit(sizeof(char*)); }
exit code => 4
output => 
int main(int argc, int argv) { exit(sizeof(int*)); }
exit code => 4
output => 
OK

To clean up the generated compiler files, execute the command make clean. For resetting architecture configurations, use the command make distclean.

Intermediate Representation

The middle end is SSA-based, and two options expose it. --dot draws the control-flow graph as it stands after SSA construction and pruning; --dump-ir prints the instructions as text, once before optimization and once after register allocation. Both use tests/fib.c below as the running example, a recursive Fibonacci function and its caller:

c
#include <stdio.h>

int fib(int n)
{
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    return fib(n - 1) + fib(n - 2);
}

int main()
{
    printf("F(10) = %d\n", fib(10));
    return 0;
}

Control-flow graph

--dot writes Graphviz DOT and stops; no executable is generated. Without -o, the name is the input path with its final suffix replaced, or .dot appended when it has none, so tests/fib.c would write tests/fib.dot into the source tree.

bash
$ out/shecc --dot -o fib.dot tests/fib.c
$ dot -Tsvg fib.dot -o fib.svg

Graphviz renders the result but is not needed to produce it, and nothing in make check depends on it.

The output holds one cluster per function definition, one nested cluster per basic block, and one node per IR instruction; a block holding no instruction gets a single placeholder node labeled pseudo. Edges inside a block are instruction order, and the edges leaving a block's last node are control flow. The graph is emitted before phi values are unwound

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Carmarmv7ccompiler

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库