r/Compilers • u/AppledogHu • May 09 '26
Stuck trying to self-host from assembly
Hi! I wrote my own CPU emulator (the SD-8516), and I am trying to get a C compiler working on it. I have gotten quite far, the big thing was understanding the symbol table, but I got stuck because I made the symbol table only 16 bits and I had to rewrite everything because it couldn't hold a 24 bit pointer. Now it works and it can compile functions like strcmp and strlen (yay!) but I am completely lost as to what to do next. I'm getting a bit desperate. AI can't help me, it's not smart enough to figure this stuff out, and I am thinking, maybe I better try and rewrite it again. The problem is it's in assembly, so I have translate anything I get into my own custom assembly language. The faster I can get C up the better! Its currently a one pass compiler with no AST. My plan was to compile a C version of itself, but its getting too large to fit in my brain. Maybe what I need is to try rewriting it again but using ASTs? Whats an AST? ._.
Question: Are there any books that contain practical code examples, in C, that can be used during the bootstrapping process? Once I get a C version to compile itself I am sure I will be in business! Right now I can compile a kind of 'B with char pointers and arrays' (no for loops yet but we can do while). I had thought maybe there was a series of C compilers out there that could compile themselves and I could slowly bring up C that way. But now I realize its the same problem with trying to write a back-end. The C compiler is of course intimately connected with the system architecture. I wonder though, if there isn't some pseudo-code C compiler out there, somewhere, that I could study?
4
u/InkAxe_games May 09 '26
Instead of going faster perhaps slowdown a little and try writing an interpreter instead. It's easier than writing a compiler and teaches some great skills (like parsing) that will help with writing a compiler.
There is an excellent resource for this: https://craftinginterpreters.com/
3
u/Tasty_Replacement_29 May 09 '26
So, cross compilation is much easier than self-hosting. Existing C compilers that are simple to change are (as war as I know) the Tiny C compiler. I have used XCC to generate WASM, I found it to be good. I don't know about LLVM or GCC.
If you really want self-hosting + bootstrapping, I wrote a C-like compiler to WASM in about 250 lines: https://www.reddit.com/r/Compilers/comments/1t0tyo9/i_wrote_a_selfhosting_clike_compiler_250_lines/ - in theory you could change that, and then extend it to support functions etc.
2
u/tao_of_coffee May 10 '26
Check out https://github.com/shinh/ELVM. This project started with an existing compiler that is fairly easy to follow, 8cc, and modified it to generate assembler code for their VM. I would try to do that for your CPU. Once that’s done you can then use that to compile itself so you have a c compiler that runs on your CPU.
1
u/choikwa May 10 '26
i would write llvm backend for your cpu, continue write c compiler in c and compile using llvm cross compiler until ur c compiler has enough functionality to compile itself
1
u/raundoclair May 11 '26
Is there a repo for SD-8516?
1
u/juicyroaster May 15 '26
So basically before you write a fully fledged C compiler. I would recommend you to write a forth compiler as it is a simple language which can be implemented in about a week or so.
0
u/[deleted] May 09 '26
[deleted] — view deleted comment