{"id":1351,"date":"2020-02-06T20:30:23","date_gmt":"2020-02-06T20:30:23","guid":{"rendered":"http:\/\/retroramblings.net\/?p=1351"},"modified":"2020-02-08T15:15:16","modified_gmt":"2020-02-08T15:15:16","slug":"time-to-stop-being-lazy","status":"publish","type":"post","link":"https:\/\/retroramblings.net\/?p=1351","title":{"rendered":"Time to stop being lazy!"},"content":{"rendered":"\n<p><strong>The EightThirtyTwo ISA &#8211; Part 15 &#8211; 2020-02-06<\/strong><\/p>\n\n\n\n<p>I&#8217;ve joked a few times in this series about being too lazy to write an assember &#8211; but it would be more true to say that the stop-gap solution I was using was adequate, so my time was better spent on the more enjoyable aspects of the project.  I am now feeling the limitations of using the GNU assembler to produce a bytestream for a target it knows nothing about, and to improve either the performance or code density of the vbcc backend&#8217;s output any further, I need to address the problem I&#8217;ve had so far with cross-module references&#8230;<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The most efficient way to reference a symbol with the 832 architecture is to use successive &#8220;li&#8221; instructions to build a PC-relative offset six bits at a time.  That&#8217;s fine if the reference and its target are in the same source module and section, since then the offset is fixed, and can&#8217;t change during the linking process.  The problem is with cross-module references, since we then have a link-time determined value to be broken up into six-bit chunks and inserted into successive &#8216;li&#8217; instructions. <\/p>\n\n\n\n<p>That&#8217;s not possible with the current solution, so I&#8217;m currently using a less efficient method, a &#8216;ldinc r7&#8217; instruction followed by an inline 32-bit reference.  This works fine but is a bit of a performance drag since it causes the instruction prefetch to be flushed as the program counter skips over the inlined constant, then triggers a separate load for that constant.  Worse still, since the instruction stream is a stream of bytes, there&#8217;s no alignment guarantee for the loaded reference, so on average three out of four such references will require two loads!<\/p>\n\n\n\n<p>For this reason, the time has come to write not only an assembler, but a linker too.<\/p>\n\n\n\n<p>The options are to adapt an existing project or start from scratch, and I&#8217;m likely to learn more from the latter option, so that&#8217;s my plan.  While my first instinct when tackling this kind of project would be to reach for C++, I&#8217;m actually going to use plain C this time, simply because it would be nice if the toolchain could eventually be self-hosting, and I don&#8217;t imagine a C++ compiler for 832 will come into existence any time soon!<\/p>\n\n\n\n<p>So in the interests of keeping things as simple as possible I have defined a new object file format specifically for 832 consisting of&#8230;:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Header &#8211; 4 bytes:  ASCII &#8220;832&#8221; followed by 01, which will serve as a version number if I change the format later on.  The linker will skip over this header if it&#8217;s encountered in the middle of an object file, so linker libraries can be created simply by concatenating object files.  (Is my Amiga heritage showing?!)<\/li><li>&#8220;SECT&#8221; designates a section, which will contain a list of symbols, references, and optionally a binary blob.<ul><li>&#8220;SYMB&#8221; designates a list of symbols<\/li><li>&#8220;REFS&#8221; designates a list of references<\/li><li>&#8220;BNRY&#8221; designates a binary blob.  This will be missing from BSS sections.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>There&#8217;s no great trick to assembling a file to this object format &#8211; it&#8217;s merely an exercise in parsing.  For now there&#8217;s no support for expressions or fancy calculations between symbols &#8211; it merely supports the facilities required to assemble C code emitted by the vbcc backend.<\/p>\n\n\n\n<p>Assembling, however, is very much the easy bit&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The EightThirtyTwo ISA &#8211; Part 15 &#8211; 2020-02-06 I&#8217;ve joked a few times in this series about being too lazy to write an assember &#8211; but it would be more true to say that the stop-gap solution I was using &hellip; <a href=\"https:\/\/retroramblings.net\/?p=1351\">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":[12,4,8,1],"tags":[],"class_list":["post-1351","post","type-post","status-publish","format-standard","hentry","category-eightthirtytwo","category-fpga","category-hardware","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1351"}],"version-history":[{"count":3,"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":1354,"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions\/1354"}],"wp:attachment":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}