electrosoup.com

git clone git://git.electrosoup.com/electrosoup.com
Log | Files | Refs

index.xml (6351B)


      1 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      2 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
      3   <channel>
      4     <title>ARM on Electrosoup</title>
      5     <link>https://electrosoup.com/tags/arm/</link>
      6     <description>Recent content in ARM on Electrosoup</description>
      7     <generator>Hugo -- gohugo.io</generator>
      8     <language>en-US</language>
      9     <managingEditor>christianermann@gmail.com (Christian Ermann)</managingEditor>
     10     <webMaster>christianermann@gmail.com (Christian Ermann)</webMaster>
     11     <lastBuildDate>Tue, 12 Nov 2024 13:01:09 -0800</lastBuildDate>
     12     <atom:link href="https://electrosoup.com/tags/arm/index.xml" rel="self" type="application/rss+xml" />
     13     <item>
     14       <title>The Infamous &#39;link&#39; Macro</title>
     15       <link>https://electrosoup.com/posts/the-infamous-link-macro/</link>
     16       <pubDate>Tue, 12 Nov 2024 13:01:09 -0800</pubDate><author>christianermann@gmail.com (Christian Ermann)</author>
     17       <guid>https://electrosoup.com/posts/the-infamous-link-macro/</guid>
     18       <description>&lt;p&gt;Each word (or function, in other languages) in Forth is stored as an entry in&#xA;a linked list known as the dictionary. When bootstrapping a Forth from&#xA;assembly, it is your responsibility to create and maintain this linked list&#xA;structure. This is a tedious process and is the source of many errors when&#xA;re-arranging words or defining new words; it&amp;rsquo;s incredibly easy to turn your&#xA;list into a graph by mistake.&lt;/p&gt;</description>
     19       <content:encoded><![CDATA[<p>Each word (or function, in other languages) in Forth is stored as an entry in
     20 a linked list known as the dictionary. When bootstrapping a Forth from
     21 assembly, it is your responsibility to create and maintain this linked list
     22 structure. This is a tedious process and is the source of many errors when
     23 re-arranging words or defining new words; it&rsquo;s incredibly easy to turn your
     24 list into a graph by mistake.</p>
     25 <p>In
     26 <a href="https://rwmj.wordpress.com/2010/08/07/jonesforth-git-repository/">jonesforth</a>,
     27 an x86 implementation of Forth, the assembler supports re-assigning new values
     28 to assembler variables. This means an assember variable can be used to store
     29 the memory address of the previous entry, and we have the ability to update it
     30 or use it whenever we need to. I used this exact strategy in my
     31 <a href="https://git.christianermann.dev/forth/log.html">incomplete x86 Forth</a> although
     32 I used <a href="https://flatassembler.net/">FASM</a> instead of
     33 <a href="https://wiki.osdev.org/GAS">GAS</a>. Unfortunately, GAS doesn&rsquo;t support this
     34 feature for ARM or RISC-V targets. It may be possible to pull this off on ARM
     35 with the right set of relocation and/or relaxation parameters, but I was unable
     36 to find success with RISC-V.</p>
     37 <p>I did come up with an alternative set of macros that achieves the same goal
     38 and should be more portable than the <code>jonesforth</code> solution:</p>
     39 
     40 
     41 
     42 
     43 
     44 <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-GAS" data-lang="GAS"><span class="line"><span class="ln"> 1</span><span class="cl"><span class="na">.macro</span> <span class="no">this_link</span>
     45 </span></span><span class="line"><span class="ln"> 2</span><span class="cl">    <span class="na">.globl</span> <span class="no">link_</span><span class="err">\+</span>
     46 </span></span><span class="line"><span class="ln"> 3</span><span class="cl"><span class="nf">link_</span><span class="err">\+</span><span class="p">:</span>
     47 </span></span><span class="line"><span class="ln"> 4</span><span class="cl"><span class="na">.endm</span>
     48 </span></span><span class="line"><span class="ln"> 5</span><span class="cl">
     49 </span></span><span class="line"><span class="ln"> 6</span><span class="cl"><span class="na">.macro</span> <span class="no">prev_link</span>
     50 </span></span><span class="line"><span class="ln"> 7</span><span class="cl">    <span class="na">.int</span> <span class="no">link_</span><span class="err">\+</span>
     51 </span></span><span class="line"><span class="ln"> 8</span><span class="cl"><span class="na">.endm</span>
     52 </span></span><span class="line"><span class="ln"> 9</span><span class="cl">
     53 </span></span><span class="line"><span class="ln">10</span><span class="cl"><span class="na">.macro</span> <span class="no">link</span>
     54 </span></span><span class="line"><span class="ln">11</span><span class="cl">    <span class="nf">this_link</span>
     55 </span></span><span class="line"><span class="ln">12</span><span class="cl">    <span class="nf">prev_link</span>
     56 </span></span><span class="line"><span class="ln">13</span><span class="cl"><span class="na">.endm</span>
     57 </span></span><span class="line"><span class="ln">14</span><span class="cl">
     58 </span></span><span class="line"><span class="ln">15</span><span class="cl"><span class="nf">this_link</span></span></span></code></pre></div><p>These macros depend on the special value
     59 
     60 
     61 
     62 
     63 <code class="code-inline language-GAS"><span class="err">\+</span></code>.
     64 This value is replaced by the invocation count of the current macro during
     65 assembly. Since we want to
     66 
     67 
     68 
     69 
     70 <code class="code-inline language-GAS"><span class="nf">link</span></code>
     71 back to the previous word in the dictionary, we need
     72 
     73 
     74 
     75 
     76 <code class="code-inline language-GAS"><span class="nf">prev_link</span></code>
     77 to resolve to 1 word before
     78 
     79 
     80 
     81 
     82 <code class="code-inline language-GAS"><span class="nf">this_link</span></code>,
     83 which is why we call
     84 
     85 
     86 
     87 
     88 <code class="code-inline language-GAS"><span class="nf">this_link</span></code>
     89 once before defining any words.</p>
     90 <p>You&rsquo;ll also need to add this to your linker script to null-terminate your
     91 dictionary, otherwise the start of your dictionary will be marked by the
     92 address where the
     93 
     94 
     95 
     96 
     97 <code class="code-inline language-GAS"><span class="nf">link_0</span></code>
     98 label created by
     99 
    100 
    101 
    102 
    103 <code class="code-inline language-GAS"><span class="nf">this_link</span></code> was stored:</p>
    104 
    105 
    106 
    107 
    108 
    109 <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-GAS" data-lang="GAS"><span class="line"><span class="ln">1</span><span class="cl"><span class="nf">link_0</span> <span class="err">=</span> <span class="mi">0</span><span class="err">;</span></span></span></code></pre></div>]]></content:encoded>
    110     </item>
    111   </channel>
    112 </rss>