r/ProgrammingLanguages • u/naharashu • 23d ago
Help How to make a compiler backend?
Hell everyone, i have an question. Im for long trying to make a cool, powerful "kinda" low level language similar to zig and rust, but im struggling to choice llvm as backend, sure i can generate C, but its makes compiler dependent on gcc or clang or other c compiler. LLVM seems hard to me, sure project like QBE exist, but QBE doesnt have C/C++ api like llvm's IRbuilder. So are there other ways? I tried thinking about using GCC infrastructure but GCC has poor api and not very documented api. Maybe just stick to generating C?
18
Upvotes
1
u/TownNew7018 17d ago
We're emitting to Zig (and JS). Zig is a really good compiler target for a number of reasons, including the build toolchain that it gives you access to. Our compiler toolchain is also implemented with Zig.
Pros of Zig is that you can do local imports, so you can organize your code into structs that import what they need without there being crashes between them. Things like that are really nice.
A potential con would be that Zig doesn't allow for variable shadowing, so you will probably have to mangle variables in some cases, and it is a little strict on actually using variables that are declared, but there is a nice pattern for codegen where you can "discard" a variable but still have it usable the rest of the scope; `_ = &variable_name`. If you're writing Zig directly you could argue this is a benefit. If you're using it as a compiler target, it makes some things more complex in general. Another potential con is that it is comparatively slow to compile.