Standalone C compiler for RISC-V and ARM
Bootstrapped C compiler for 32-bit RISC-V and ARM ISAs
Bootstrapped compiler is a compiler that's able to compile its own source code which is a key verification milestone. Since rvcc is written in ANSI C, we can use any compiler on any platform for the initial compilation before bootstrapping with the help of RISC-V and/or ARM emulators.
The steps to validate rvcc's bootstrap on RISC-V are:
Compiler's source code is initially compiled using gcc which generates an x86 binary.
Built binary is invoked with its own source code as input and generates a RISC-V binary.
The RISC-V binary is invoked (via an emulator) with its own source code as input and generates another RISC-V binary.
If outputs generated in steps 2. and 3. are identical then bootstrap has been successful - the compiler regenerated itself from its own source code in two consecutive generations.
The bootstrap can be tested by running make bootstrap-riscv (requires gcc and rv8 RISC-V emulator installed):
user@ubuntu:~/rvcc$ make bootstrap-riscv
gcc -ansi -pedantic -m32 -Wall -Wextra -g -Llib -o bin/rvcc src/rvcc.c
bin/rvcc -o bin/rvcc_1.elf -march=riscv -Llib src/rvcc.c
rv-jit -- bin/rvcc_1.elf -o bin/rvcc_2.elf -march=riscv -Llib src/rvcc.c
diff -q bin/rvcc_1.elf bin/rvcc_2.elf
Files are the same - bootstrap successful!Bootstrapping on ARM follows the same process and the test can be run via make bootstrap-arm (requires qemu-arm-static installed). To run both bootstraps use make bootstrap.
rvcc [-o outfile] [-noclib] [-march=riscv|arm]
The compiler generates an executable binary file without going through explicit linking and assembly steps, it directly encodes all RISC-V/ARM opcode instructions and packages them in an ELF file. The generated executable includes a symbol table so by using a disassembler it's possible to peek into the machine code for introspection. The compiler also generates a listing of its internal IL representation for debugging purposes.
To run a RISC-V ELF file on an x86 machine you can use the excellent rv8 emulator, which allows running and debugging RISC-V binaries as if they were native ones, including proxying system calls for I/O. The usage is:
rv-jit
To disassemble a RISC-V binary:
rv-bin dump -d
To run ARM on x86 you can use qemu user emulation through qemu-arm-static.
qemu-arm-static
To disassemble an ARM binary you can use the GNU toolchain:
arm-linux-gnueabi-objdump -d
Supported language constructs fall within ANSI C and initial external compilation can be done using gcc or any other C compiler. While common language constructs (structs, pointers, indirection etc.) are implemented due to the simplicity of the compiler (no preprocessor, AST nor a formalised parser) it doesn't implement full ANSI C syntax.
Compiler sequentially translates C source code into IL and then binary meaning instruction order is preserved from C source all the way to the binary with only jumps and glue logic being inserted. This makes some language constructs (for example for loops or some comparisons) quite inefficient due to excessive jumps but compiler's design stays very simple.
RISC-V is an open source Instruction Set Architecture, an alternative to the likes of x86 and ARM, which can be used freely without any licenses required. It's a RISC architecture meaning the only instructions that operate on memory are simple loads and stores, with all other instructions operating only on registers (32 of them) and immediate values.
ARM output generates code compatible with ARMv7 ISA which is owned by Arm Holdings. Similarly to RISC-V, ARM is a RISC ISA but it offers a wider variety of operations by adding conditional execution codes, automatic index increments, value shifts etc. to many instructions resulting in potentially denser code (fewer instructions required), at the expense of reducing the number of bits available for immediate values.
The compiler generates Linux ARMv7 EABI-compliant binaries which can be run directly on a Raspberry Pi (verified on Pi 4 Model B running armv7l).
Sample RISC-V binary code generated for a recursive Fibonacci sequence function.
…Aim of the project is purely recreational/educational and thanks are due to:
RISC-V International for the RISC-V initiative
K&R for giving the world C language (here we are 40 years later...)
everyone else who supports RISC architectures!
No open issues yet, or sync has not completed.