35 ms·
I wanted to write an LLVM backend for various instruction sets (e.g. Z80, R216[0]), since those would deal with the problems of optimization passes and register
by siraben 6y ago
I wanted to write an LLVM backend for various instruction sets (e.g. Z80, R216[0]), since those would deal with the problems of optimization passes and register allocation for me, but the LLVM tutorial[1] makes it look so goddamn hard. Does anyone know of a tutorial or of a declarative way to write such backends for either QBE or LLVM? The QBE git repository[2] has a few backends but also looks similarly involved.
Comparatively, writing frontends is quite easy these days and many examples of compiling to IR exist.
[0] https://trigraph.net/powdertoy/R216/manual.md https://trigraph.net/powdertoy/R216/manual.md
[1] https://llvm.org/docs/WritingAnLLVMBackend.html https://llvm.org/docs/WritingAnLLVMBackend.html
[2] https://c9x.me/git/qbe.git/tree/amd64 https://c9x.me/git/qbe.git/tree/amd64
- mhh__ 6y agoWriting compiler backend in a declarative manner is something I've been toying with for a while now, and I don't think it's realistically possible with current technology. Backends are hard, unfortunately. However, since you are targeting the Z80, you might be able to just take the IR and transform it yourself? I haven't touched an LLVM backend for a while, so I don't know what the process is atm, but if you imagine that a lot of a modern compiler is dealing with instruction scheduling and the like , which the Z80 doesn't really have a need for. LLVM is very good for the industry, but it is a little over-engineered(?). Maybe that's a bit harsh, but the difficulty is definitely thrown at you earlier than hacking on most compilers.
- beagle3 6y agoIt is not purely declarative, but Fraser & Hanson's lcc comes close while still delivering good optimization -- and has a book describing it (IIRC it was written literate-programming style). It's been 20 years or more, I played with an older version that required an Icon interpreter as part of the build chain, and I think they had a later one that did away with that requirement. The code generator is basically built from a declarative description of tree matching/rwriting templates with a cost model, and the compiler will find the optimal cost match.
- vmchale 6y ago> Writing compiler backend in a declarative manner is something I've been toying with for a while now, and I don't think it's realistically possible with current technology. Functional style isn't too bad. Maximal munch lines up with pattern matching, then linear scan is easy enough if you don't need gcc-tier results. GHC has bits of its backend that are shared across architectures, maybe I should have a look at that...
- pjmlp 6y agoWriting backends to integration into compiler stacks like LLVM is as challenge unless one is quite deep into compilers. If the idea is just to learn how they work, it is easier to just dump an IR that is compatible with a macro assembler, then just call the assembler with your macro definitions on the generated IR. It won't win prices in performance, but one gets to learn how things work and will have a compiler with proper binaries at the end, and one can fine tune the macros to improve the quality of the code anyway.
- Rochus 6y agoHave you seen these: https://jonathan2251.github.io/lbd/about.html https://jonathan2251.github.io/lbd/about.html https://llvm.org/devmtg/2014-04/PDFs/Talks/Building%20an%20LLVM%20backend.pdf https://llvm.org/devmtg/2014-04/PDFs/Talks/Building%20an%20L...
- chillee 6y agoThis is a nice tutorial: https://www.cs.cornell.edu/~asampson/blog/llvm.html https://www.cs.cornell.edu/~asampson/blog/llvm.html
- tieze 6y agoIt's certainly not trivial, but still an interesting journey. I made a backend for MMIX, heavily influenced by the risc-v implementation, and kept the patches logically consistent by rebasing new changes to serve as an example. The last rebase was a year ago, but I think following a previous implementation is the best way forward as there is so much boiler-plate: - https://github.com/stuij/mmix-llvm-support https://github.com/stuij/mmix-llvm-support - https://github.com/stuij/mmix-llvm-backend https://github.com/stuij/mmix-llvm-backend That said, those kind of olden CPU's aren't a terribly snug fit for LLVM, due to their peculiarities. You'll have to bend LLVM quite a bit to bend it to your will, which also means that you need to get quite familiar with it as well. edit: this tutorial is even older still, but for me it was one of the best resources, next to checking other simple backends: https://jonathan2251.github.io/lbd/llvmstructure.html https://jonathan2251.github.io/lbd/llvmstructure.html