展示 HN: Xcc700:适用于 ESP32 (Xtensa) 的 700 行自托管迷你 C 编译器
Show HN: Xcc700: Self-hosting mini C compiler for ESP32 (Xtensa) in 700 lines

原始链接: https://github.com/valdanylchuk/xcc700

## xcc700:一个微型、自托管编译器 xcc700是一个紧凑的编译器(700行C代码),专为现代平台设计,特别是ESP32。它注重可理解性和可修改性,为创建自定义语言或在嵌入式系统上快速测试代码提供了基础。 该编译器将单个C源文件作为输入,并输出可重定位的ELF文件,可以轻松地通过elf_loader组件集成到ESP-IDF项目中。它支持基本的C特性——while循环、if/else语句、整数、指针和函数调用——但为了简单起见,有意省略了许多高级特性。 xcc700可以在标准计算机上编译和运行(交叉编译),*或者*在ESP32上自编译并直接执行。虽然为了简单性牺牲了性能(将CPU视为堆栈机),但它实现了令人印象深刻的速度——在ESP32-S3上高达每秒17,500行代码。 xcc700以MIT许可证发布,旨在作为实验和扩展的起点,鼓励贡献和分支,以探索微型、自托管编译器的可能性。

一位开发者分享了他的第一个编译器“xcc700”,这是一个为ESP32(Xtensa)微控制器编写的迷你C编译器,代码量仅700行。它在GitHub上可用(github.com/valdanylchuk/xcc700),是一个单遍、递归下降编译器,可以直接生成通过ESP-IDF运行的REL ELF二进制文件。 xcc700的设计目标是自举,优先考虑简单性,将Xtensa CPU视为一个没有寄存器分配的栈机。目前可以在macOS(以及可能Linux)上编译,并可以交叉编译为ESP32。 该项目被构建为一个有趣的练习,以及为定制的、针对ESP32平台的语言提供潜在的基础,开发者认为ESP32在业余项目中被低估了。该编译器拥有令人印象深刻的速度,处理代码速度为每秒17,500行。
相关文章

原文

Why look into this project?

  • A compiler you can fully grasp and tweak, on a modern platform where small is still cool.
  • Basic features, not too entrenched, easy to morph into your language of choice.
  • Reusable ELF writer, and a basic Xtensa bytecodes emitter.
  • Possibly useful for hotfixes, CI, quick test/debug turnaround on esp32.
./xcc700 xcc700.c -o xcc700.elf 

[ xcc700 ] BUILD COMPLETED > OK
> IN  : 700 Lines / 7977 Tokens
> SYM : 69 Funcs / 91 Globals
> REL : 152 Literals / 1027 Patches
> MEM : 1041 B .rodata / 17120 B .bss
> OUT : 27735 B .text / 33300 B ELF
[ 40 ms ] >> 17500 Lines/sec <<

Note: that timing is from esp32-s3. Timings on Mac/POSIX will be reported 1000x slower than they are, as on esp32 ticks are millisecond, and on POSIX microsecond, but there is no adjustment here.

xcc700_demo10s.mov

.

Several options:

A. Compile with gcc xcc700.c and run it on your computer as a cross-compiler. It is fairly portable, tested on Mac x86_64 and arm64.

B. Compile for esp32 using xtensa-gcc or xcc700 from the option A (yes it can compile and cross-compile itself). Or grab the gcc-compiled version here: xcc700.elf (16kB). Run with ESP-IDF elf_loader.

C. Adapt the source code and call it as a function in your firmware.

  • C features: minimum required to write something like this compiler. While loop, if/then/else, limited support for int/char/pointers/arrays, function calls and definitions, basic arithmetic and bitwise operators.
  • Single source .c file as input, single REL ELF file as output.
  • The output files can be run directly by the ESP-IDF elf_loader component, which links them on load via relocation table to anything you have exposed in your firmware: newlib libc, LVGL, your custom functions, anything you like. Just declare the functions you use.
  • The rest of the C: for/do, include/define, long/float/double, struct/union/typedef, switch/case, array initializers, .data section, multi-line comments, too much to list.
  • Many features are implemented only partially. E.g. you can have .bss globals but not global initializers; ++/-- are only supported in prefix position, assignment as statement not expression, types are mostly not checked, etc.
  • Error handling and reporting. It is wildly optimistic, enforces nothing, has only a few error checks, and will crash in spectacular and unexpected ways on the most trivial errors.
  • Optimization. It treats the Xtensa CPU as a stack machine, with no attempt at register allocation, and no benefit from the sliding window. It is a major sacrifice of performance for simplicity. GCC-compiled: 16kB, 17,500 lines/s; self-compiled: 33kB, 3,900 lines/s.
  • Miss a feature? Just fork it! With a working foundation in only 700 lines, it is fairly easy to get started.

This is free software under MIT License, see LICENSE.

Contributing: Forks Welcome!

While I do not believe the world needs another C99 implementation, and do not intend to add features here, I am dead curious to see where the other creative minds can take a tiny self-hosting compiler on esp32.

If you organize hackathons, or assign coursework, or write tutorials, please consider xcc700 as a base to fork and extend! It can run on the available PCs, or on a $5 MCU if you want real cool hardware for the final test. Or you can port it to other systems, and use ld to link those ELF files.

I was making an esp32 "cyberdeck", and thought it cool to build some binaries directly on it. Esp32 is underrated in userland. It can do everything a 90s PC could do and more.

You can also take this as an artistic statement, and ask yourself:

  • How many Watts do you need to do fun/useful stuff on a computer?
  • Isn't it nice to have simple, tinker-friendly versions of common apps?
  • Do we really need 300MB mouse drivers?

Have fun!

联系我们 contact @ memedata.com