{"id":1277,"date":"2019-08-24T19:57:35","date_gmt":"2019-08-24T19:57:35","guid":{"rendered":"http:\/\/retroramblings.net\/?p=1277"},"modified":"2019-08-24T20:47:06","modified_gmt":"2019-08-24T20:47:06","slug":"assembly-language-is-all-very-well","status":"publish","type":"post","link":"http:\/\/retroramblings.net\/?p=1277","title":{"rendered":"Assembly language is all very well&#8230;"},"content":{"rendered":"\n<p><strong>The EightThirtyTwo ISA &#8211; Part 5 &#8211; 2019-08-24<\/strong><\/p>\n\n\n\n<p>Before proceeding very much further with the EightThirtyTwo design I wanted to have some way of generating code for it from a higher-level language, just to get a better feel for which instructions are useful for compilers, which will be pretty much unused and where I might find any gaps that make the generated code unneccessarily clumsy.  The most obvious route to this goal is to find a C compiler that can be easily retargetted to a new architecture.  (Anyone who&#8217;s attempted this before is probably laughing now at my use of the word &#8216;easily&#8217;!)<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>My preconceptions when starting were that I had two options, GCC and LLVM (this turned out to be very far from the truth) &#8211; and that the latter would be the easier to work with.  While in an ideal world I would have GCC support, I know from having worked with the ZPU toolchain that it&#8217;s a rapidly moving target, and if an architecture&#8217;s support isn&#8217;t updated it tends to suffer bitrot and become difficult to build as the ecosystem evolves.  So for now I&#8217;ve discounted GCC.<\/p>\n\n\n\n<p>I haven&#8217;t yet explored LLVM at all &#8211; but this may well end up being the long-term solution.  For now I wanted something simpler, however.  It turns out there are in fact a number of other C compilers out there &#8211; some that I&#8217;d never heard of, some that I know of old &#8211; and there may well be more that I haven&#8217;t yet found.  I am genuinely surprised by just how many options there are!<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PCC &#8211; the <a href=\"http:\/\/pcc.ludd.ltu.se\/\">Portable C Compiler<\/a>.  (BSD licensed).  This goes back to before I was born, and is still under active development now.  It&#8217;s specifically designed to be retargetable to different architectures, but I haven&#8217;t yet figured out how to find valid &#8211;target paramters &#8211; for mips and m68k anything other than &lt;arch&gt;-netbsdelf seems to give an error.  At this stage any friction makes me move on to examine the next option &#8211; but this is one I may well revisit.<\/li><li>TCC &#8211; the <a href=\"https:\/\/bellard.org\/tcc\/\">Tiny C Compiler<\/a>.  (GNU LGPL).  A very small, lightweight and fast compiler &#8211; mainly aimed at x86 but does have other backends, and in theory should do what I need, but the backend interface looks like it has a steep learning curve.<\/li><li>SDCC &#8211; <a href=\"http:\/\/sdcc.sourceforge.net\/\">The Small Device C Compiler<\/a>.  Targetted at 8-bit CPUs and microcontrollers, but may be applicable here.  The documentation seems to be incomplete though.<\/li><li>TACK &#8211; <a href=\"http:\/\/tack.sourceforge.net\">The Amsterdam Compiler Kit<\/a>.  Another compiler with backends for multiple architectures.  This compiler was originally a commercial product, part of MINIX, subsequently open-sourced.  The current maintainer writes that &#8220;it is very easy to port to a new architecture\u2026 but the generated code is terrible.&#8221;   &#8230; so perhaps not the best option for now, however the reason for this assessment is that it doesn&#8217;t make good use of plentiful registers &#8211; which we don&#8217;t have, so it might still be worth exploring.<\/li><li>8CC &#8211; an MIT Licensed <a href=\"https:\/\/github.com\/rui314\/8cc\">one-man project<\/a> to build a C11 compliant compiler.  Currently only targets x86-64, but could no doubt be used as a starting point for targetting other architectures.<\/li><li>DICE &#8211; <a href=\"http:\/\/legacy.obviously.com\/dice\/\">Dillon&#8217;s Integrated C Environment<\/a> (Own license &#8211; may be used to produce commercial code, but the compiler itself, its source and derived works may not be sold.)  This is a compiler I remember from my Amiga days, and used again when I became involved in the Minimig project.  Targets m68k only, but could be used as a starting point for a new architecture if the code generator were replaced.<\/li><li>LCC &#8211; <a href=\"https:\/\/github.com\/drh\/lcc\">the Little C Compiler<\/a>.  (Unusual license, free for personal use).  Again, written specifically with multiple architectures in mind, and the subject of Fraser and Hanson&#8217;s book <em>A Retargetable C Compiler: Design and Implementation<\/em>.  The current version&#8217;s code generator is different from that described in the book, however &#8211; and I discounted this one after (a) reading comments on the web about the quality of its code generation, and (b) stumbling across David Given&#8217;s attempt to create a Z-machine backend &#8211; he says: &#8220;the lcc back end interface is pretty naff and is only partially documented, so I never finished anything.&#8221;  He did in fact finish something, but for a different compiler, and on the strength of this, for my initial experiments I chose:<\/li><li><strong>VBCC<\/strong> &#8211; <a href=\"http:\/\/www.compilers.de\/vbcc.html\">Volker Barthelmann&#8217;s C Compiler<\/a>.  (Unusual license, free for personal use, permission required for commercial use.  Source can be distributed but only in unmodified form.)  In his blog post about <a href=\"http:\/\/cowlark.com\/vbcc-z-compiler\/index.html\">creating the Z-machine backend<\/a>, David says vbcc &#8220;&#8230;is really neat, by the way; it&#8217;s got excellent global optimisations, an easy-to-understand back end interface, is small and <em>incredibly<\/em> fast.&#8221;  Sounds good to me!<\/li><\/ul>\n\n\n\n<p>I have used VBCC in the past, since it&#8217;s a widely used and popular compiler when targetting the Amiga &#8211; but I hadn&#8217;t previously explored its code-generation facilities.  It&#8217;s incredibly easy to get started &#8211; no need to edit Makefiles or headers &#8211; simply copy an existing backend, type make TARGET=&lt;newtarget&gt; and you&#8217;ll have a new compiler.  There&#8217;s even a &#8220;generic risc&#8221; backend that serves as an ideal starting point.  The documentation is also good.<\/p>\n\n\n\n<p>[I must also give an honourable mention to <a href=\"http:\/\/www.archc.org\/\">ArchC<\/a> at this point &#8211; if time allows I will definitely explore this further.  It appears to be a framework for ISA research, with tools that will automatically build simulators, binutils-based assemblers and clang-based compilers from CPU models.  Very interesting stuff, well worth exploring, but too deep a rabbit-hole for the immediate present.]<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The EightThirtyTwo ISA &#8211; Part 5 &#8211; 2019-08-24 Before proceeding very much further with the EightThirtyTwo design I wanted to have some way of generating code for it from a higher-level language, just to get a better feel for which &hellip; <a href=\"http:\/\/retroramblings.net\/?p=1277\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[],"class_list":["post-1277","post","type-post","status-publish","format-standard","hentry","category-fpga","category-hardware"],"_links":{"self":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1277"}],"version-history":[{"count":2,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1277\/revisions"}],"predecessor-version":[{"id":1279,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1277\/revisions\/1279"}],"wp:attachment":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}