<story><title>QBE vs. LLVM</title><url>https://c9x.me/compile/doc/llvm.html</url></story><parent_chain><item><author>siraben</author><text>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.&lt;p&gt;Comparatively, writing frontends is quite easy these days and many examples of compiling to IR exist.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;trigraph.net&amp;#x2F;powdertoy&amp;#x2F;R216&amp;#x2F;manual.md&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;trigraph.net&amp;#x2F;powdertoy&amp;#x2F;R216&amp;#x2F;manual.md&lt;/a&gt;&lt;p&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;llvm.org&amp;#x2F;docs&amp;#x2F;WritingAnLLVMBackend.html&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;llvm.org&amp;#x2F;docs&amp;#x2F;WritingAnLLVMBackend.html&lt;/a&gt;&lt;p&gt;[2] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;c9x.me&amp;#x2F;git&amp;#x2F;qbe.git&amp;#x2F;tree&amp;#x2F;amd64&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;c9x.me&amp;#x2F;git&amp;#x2F;qbe.git&amp;#x2F;tree&amp;#x2F;amd64&lt;/a&gt;</text></item></parent_chain><comment><author>pjmlp</author><text>Writing backends to integration into compiler stacks like LLVM is as challenge unless one is quite deep into compilers.&lt;p&gt;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.&lt;p&gt;It won&amp;#x27;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.</text></comment>
<story><title>QBE vs. LLVM</title><url>https://c9x.me/compile/doc/llvm.html</url></story><parent_chain><item><author>siraben</author><text>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.&lt;p&gt;Comparatively, writing frontends is quite easy these days and many examples of compiling to IR exist.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;trigraph.net&amp;#x2F;powdertoy&amp;#x2F;R216&amp;#x2F;manual.md&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;trigraph.net&amp;#x2F;powdertoy&amp;#x2F;R216&amp;#x2F;manual.md&lt;/a&gt;&lt;p&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;llvm.org&amp;#x2F;docs&amp;#x2F;WritingAnLLVMBackend.html&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;llvm.org&amp;#x2F;docs&amp;#x2F;WritingAnLLVMBackend.html&lt;/a&gt;&lt;p&gt;[2] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;c9x.me&amp;#x2F;git&amp;#x2F;qbe.git&amp;#x2F;tree&amp;#x2F;amd64&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;c9x.me&amp;#x2F;git&amp;#x2F;qbe.git&amp;#x2F;tree&amp;#x2F;amd64&lt;/a&gt;</text></item></parent_chain><comment><author>mhh__</author><text>Writing compiler backend in a declarative manner is something I&amp;#x27;ve been toying with for a while now, and I don&amp;#x27;t think it&amp;#x27;s realistically possible with current technology.&lt;p&gt;Backends &lt;i&gt;are&lt;/i&gt; hard, unfortunately.&lt;p&gt;However, since you are targeting the Z80, you might be able to just take the IR and transform it yourself? I haven&amp;#x27;t touched an LLVM backend for a while, so I don&amp;#x27;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&amp;#x27;t really have a need for.&lt;p&gt;LLVM is very good for the industry, but it is a little over-engineered(?). Maybe that&amp;#x27;s a bit harsh, but the difficulty is definitely thrown at you earlier than hacking on most compilers.</text></comment>