Show HN: Xcc700: Self-hosting mini C compiler for ESP32 (Xtensa) in 700 lines
github.com130 points by isitcontent a day ago
130 points by isitcontent a day ago
Repo: https://github.com/valdanylchuk/xcc700
Hi Everyone! I just wrote my first compiler!
- single pass, recursive descent, direct emission
- generates REL ELF binaries, runnable using ESP-IDF elf_loader
- very basic features only, just enough for self-hosting
- treats the Xtensa CPU as a stack machine for simplicity, no register allocation / window usage
- compilable on Mac, probably also Linux, can cross-compile for esp32 there
- wrote for fun / cyberdeck project
Sample output from esp32:
xcc700.elf xcc700.c -o /d/cc.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 <<
My best hope is that some fork might grow into a unique nice language tailored to the esp32 platform. I think it is underrated in userland hobby projects.
In case anyone interested, you can run (nommu) Linux on ESP32 Xtensa boards
https://github.com/jcmvbkbc/esp32-linux-build
ESP32-S3 N16R8 is <$5 on aliexpress:
- Dual-core Xtensa 240 MHz
- 16 MB NOR flash (eXecute-in-place supported)
- 8 MB (PS)RAM
- Wi-Fi 2.4 GHz 802.11n, Bluetooth
As well as Zephyr, NuttX RTOSes, MicroPython.>eXecute-in-place supported
Losing this when you load ELFs is kind of a bummer. Probably a dumb question but I wonder if it'd be possible to only swap in the parts of the binary that are needed at any given time.
Cool! And I do think the world needs more C compilers. There is so much you could do with this language, but it needs to be disentangled from the C++ compiler behemoths. (yes, I now that there are other small C compilers)
What we need is safety improvements in C, we already have enough security exploits in the standard library, strings and arrays, no need for more, without fixing what is broken since 1979 (lint birth year).
There is the TCC. Adapting that would probably be a faster path to a full featured C compiler on esp32 than building up my xcc700. This is more of an educational sandbox project.
I know, and there are chibicc, kefir, etc. .. and I have my own experimental C compiler.
I would love to see how you handle variably-modified types, the way I retrofitted them onto chibicc never felt quite right, and I've never seen another non-gcc/clang compiler fully support noplate's VM type patterns.
Maybe I fill find the time to clean it up and make it public. But did not find VM-types difficult to implement, the type just depends on some run-time value, so at the point in time where the size expression is evaluated, one stores the result in a hidden variable which the type refers to.
Cool, always refreshing to see different approaches to the same problem, and you learn so much by doing, this is more the kind of tinkering I will be doing in retirement.
hats off. this is really easy to read and well written and easy to comprehend code imho because it only support basic features. its a really nice example to read through thanks. nice inspiration to see its possible to roll your own for this with a bit restricted featureset and goals :).
This one[1] is similar in style and supports a bit more C constructs. [1] https://github.com/Lulzx/sectorc/blob/master/stage5/cc.c
That's super cool! I have been wondering what could be done with ESP32 if it weren't for the lack of RAM.
As a fun of Rust, one thing that saddens me is knowing these things would be difficult to achieve with a Rust compiler, given the language seems to be vastly more complex.
Unless someone created a subset of Rust without (some?) safety checks, I guess.
Everything that was possible on PCs with MS-DOS.
I think people have no clue how powerful ESP 32 actually is.
Right, Rust is more complex, and it is the complex bits that make it Rust. There are sure some shortcuts possible for starters, for example scope-based lifetimes like in early Rust versions, but still, to make it a worthwhile upgrade over C, it would take some intermediate representation, and definitely more than 700 lines.
There is a lively movement for coding in Rust for esp32, that works, just not on the device I think.
I would love to see something like C with a borrow checker and Result/Option but not the fancy type system.
The big question is whether or not that is even possible. My money is on 'no'.
Very cool! What was the shell you are running in the demo video?
Thanks! That is just my small custom experimental mini shell. This project started as a retro DOS-like cyberdeck, and first thing I tried to run there was DOS programs, which is why it looks like that. Only got to COM files, when I learned about elf_loader, so I now focus on that. I might extract and release whatever is valuable/reusable in that shell later on.
Yeah, very cool. I was thinking about possibly porting it to risc-v and using it on my fpga based core. I was trying to build a monitor style shell, but C like environment would be very cool.
Porting to different CPU/bytecodes should be doable. Just keep in mind that in ESP-IDF we also get the dynamic linker and the libc for free (and any other C functions exposed in the firmware). On the other platforms, that may be some extra work for the compiler.
Thanks for posting this here. I star’d the project. Getting a portable computer with a minimal flexible and previously well supported operating system was one of my goals for the esp32 as well. I’ll be watching for more.
Can this run ELF programs that are placed on the memory card?
I only wrote the compiler. But ESP-IDF elf_loader component can do that, yes.