A self-hosting and educational C optimizing compiler
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.
shecc is capable of compiling C source files written in the following
syntax:
char, short, int, _Bool, void, struct, union, enum, typedef, and pointer typesif, else, while, for, do-while, switch, case, default, break, continue, return, goto
with labels, and general expressions+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)<stdarg.h>)typedef int *ptr_t;)***ptr)int i = [expr];, int arr[] = {1, 2, 3};#define, #if, #ifdef, #ifndef, #elif, #else, #endif, #undef, #error, and #include__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.
The steps to validate shecc bootstrapping:
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.stage1: The built binary reads its own source code as input and generates a
binary for the selected target.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.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.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:
$ sudo apt-get install qemu-userThe 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.
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:
$ sudo apt-get install gcc-arm-linux-gnueabihfAnother 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:
$ sudo apt-get install gcc-aarch64-linux-gnu qemu-userSince 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.
Configure which backend you want. shecc supports the ARMv7-A, AArch64,
RV32IM, and x86-64 backends, with Arm as the default:
$ 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 x64The 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:
$ 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.elfRun make DYNLINK=1 to use the dynamic linking mode and generate the dynamically linked compiler:
…For development builds with memory safety checks:
$ make sanitizer
$ make check-sanitizerFile out/shecc is the first stage compiler. Its usage:
$ 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 stopExample 1: static linking mode
$ out/shecc -o fib tests/fib.c
$ qemu-arm fibThe 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.
$ 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=arm64shecc 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:
# 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 UBSanThe test suite covers:
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 =>
OKTo clean up the generated compiler files, execute the command make clean.
For resetting architecture configurations, use the command make distclean.
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:
#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;
}--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.
$ out/shecc --dot -o fib.dot tests/fib.c
$ dot -Tsvg fib.dot -o fib.svgGraphviz 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
No open issues yet, or sync has not completed.