Show HN: Xcc700: Self-hosting mini C compiler for ESP32 (Xtensa) in 700 lines

github.com

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.
ValdikSS - 10 hours ago

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.
uecker - 17 hours ago

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)

- 2 hours ago
[deleted]
boznz - 19 hours ago

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.

saidnooneever - 15 hours ago

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

ladyanita22 - 18 hours ago

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.

MobiusHorizons - 19 hours ago

Very cool! What was the shell you are running in the demo video?

nunobrito - 17 hours ago

Can this run ELF programs that are placed on the memory card?