百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rvcc

> 编程语言
开源

用于 RISC-V 和 ARM 的独立 C 编译器

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 RISC-V 和 ARM 的独立 C 编译器

rvcc

Bootstrapped C compiler for 32-bit RISC-V and ARM ISAs

Features

  • implements a subset of C language sufficient to compile itself
  • generates executable Linux ELF binaries for RV32IM and ARMv7
  • includes an embedded minimal Linux C standard library for basic I/O
  • single binary - a microcompiler for environments without GCC toolchain
  • written in ANSI C it can cross-compile from any platform
  • simple two-pass compilation process: source -> IL -> binary
  • lexer, parser and code generator all implemented by hand

Bootstrapping

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:

  1. Compiler's source code is initially compiled using gcc which generates an x86 binary.

  2. Built binary is invoked with its own source code as input and generates a RISC-V binary.

  3. The RISC-V binary is invoked (via an emulator) with its own source code as input and generates another RISC-V binary.

  4. 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):

bash
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.

Usage

rvcc [-o outfile] [-noclib] [-march=riscv|arm]

  • -o - output file name (default: out.elf)
  • -noclib - exclude embedded C library (default: include)
  • -march=riscv|arm - output architecture (default: riscv)

Output

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

Code

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

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

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).

Example RISC-V output

Sample RISC-V binary code generated for a recursive Fibonacci sequence function.

…

Thanks

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...)

  • rv8 and qemu teams for a great emulation environments

  • everyone else who supports RISC architectures!

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Carmccompilerrisc-v

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言