I’m working on creating a custom programming language as a learning exercise, and I’ve built most of the frontend components already. The lexical analyzer, syntax parser, and type checker are all functioning well. However, I’m facing difficulties with the code generation aspect.
I attempted to create my own x64 assembly generator, but incorporating new features into the language is quite complicated. LLVM appears to be excessive for my needs and is quite resource-heavy for a straightforward educational project. I explored QBE as another option, but I find the code structure challenging to navigate.
Does anyone have suggestions for other compact backend solutions that could be effective for hobby compiler projects? I’m seeking something easier to comprehend and integrate compared to LLVM, yet more versatile than manually coding raw assembly.
Check out Cranelift - it’s way more approachable than LLVM but still gives you solid optimization. I used it for a similar compiler project and the docs were actually readable. The API’s cleaner and compiles much faster than LLVM, which is perfect when you’re iterating a lot. You could also try TinyCC as a backend. Just generate C code from your language and let TCC handle the machine code. You get portability across different architectures without touching assembly. I found this super helpful when I wanted to focus on language semantics instead of getting stuck in low-level code generation. Plus debugging’s way easier since you can check the intermediate C output.
what about tablegen or checking out how smaller languages like wren handle codegen? also, what features are you adding that made x64 so tricky? there might be a simpler approach depending on what you’re trying to build.
webassembly’s another solid option - generate wasm and it runs everywhere without the architecture headaches. wasmtime’s got good tooling for it. you could also target something like lua’s vm bytecode or build your own stack machine interpreter. way easier than native codegen but you’ll still learn tons.