<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>matheusmoreira.com</title>
    <link>https://www.matheusmoreira.com/</link>
    <description>Stuff I think about and don&apos;t want to forget.</description>
    <language>en</language>
    <lastBuildDate>Thu, 28 May 2026 00:00:00 GMT</lastBuildDate>
    <generator>pugneum-feed</generator>
    <atom:link href="https://www.matheusmoreira.com/rss.xml" rel="self" type="application/rss+xml"/>
    <item>
      <title>The lone lisp heap</title>
      <link>https://www.matheusmoreira.com/articles/lone-lisp-heap</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/lone-lisp-heap</guid>
      <pubDate>Thu, 28 May 2026 00:00:00 GMT</pubDate>
      <description>The heap implementation of lone lisp.</description>
      <content:encoded><![CDATA[<h1>The lone lisp heap</h1><p>Like many dynamic languages out there, <a href="https://github.com/lone-lang/lone">lone</a> started out simple.
It was essentially a collection of data structures written in C,
an <code>union</code> comprising all of those types, a typed value structure
containing the union plus metadata, and a custom language whose
entire purpose is to bring all these values together into patterns
that resemble working programs.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_list</span>   <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">lone_lisp_vector</span> <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">lone_lisp_table</span>  <span class="token punctuation">{</span> <span class="token comment">/* ... */</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">/* ... */</span>

<span class="token keyword">enum</span> <span class="token class-name">lone_lisp_heap_value_type</span> <span class="token punctuation">{</span>
    LONE_LISP_TYPE_LIST<span class="token punctuation">,</span>
    LONE_LISP_TYPE_VECTOR<span class="token punctuation">,</span>
    LONE_LISP_TYPE_TABLE<span class="token punctuation">,</span>
    <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span> <span class="token punctuation">{</span>
    bool live<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">;</span>
    bool marked<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>

    <span class="token keyword">enum</span> <span class="token class-name">lone_lisp_heap_value_type</span> type<span class="token punctuation">;</span>

    <span class="token keyword">union</span> <span class="token punctuation">{</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_list</span> list<span class="token punctuation">;</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_vector</span> vector<span class="token punctuation">;</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_table</span> table<span class="token punctuation">;</span>
        <span class="token comment">/* ... */</span>
    <span class="token punctuation">}</span> as<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>When I put it this way, the language itself seems almost incidental
to the virtual machine work. That&apos;s because it is. Lone was in fact
not designed ahead of time. It was and still is being constructed
in real time. It&apos;s almost as if the language itself is <em>arising</em>
as a consequence of its own implementation. It grows in complexity
alongside the knowledge and understanding of its creator.
</p><p>At first I was a neophyte. I was attracted to ideas that were almost
painfully simple, almost too naive to cope with the harsh reality
of the world. The code reflected that.
</p><p>Take the above structures, for example. How are such values created?
My first instinct is to simply allocate some memory for each object.
Call <code>malloc</code> with the size of the structure, then initialize the
memory it returns. It&apos;s that easy.
</p><p>Right?</p><h2>Memory allocation</h2><p>Lone is a lisp interpreter written in freestanding C.
There is no dynamic memory allocation in freestanding C.
There is no such thing as <code>malloc</code>. There is no <code>libc</code>.
There is only me and the code. If I wanted <code>malloc</code>,
I would have to write it myself.
</p><p>So I wrote it. I read up on everything I could find online
about memory and its allocation. Then I made my own
memory allocator.
</p><p>To manage a memory block, the allocator needs to describe it.
Let&apos;s start with the basics. The most basic information
about a piece of data is its location and its size.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token punctuation">{</span>
  <span class="token class-name">size_t</span> size<span class="token punctuation">;</span>
  <span class="token keyword">unsigned</span> <span class="token keyword">char</span> pointer<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>The allocator must also keep track of whether the block
is free or currently in use. Otherwise, there would be
<strong>chaos</strong>.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token punctuation">{</span>
  bool free<span class="token punctuation">;</span>
  <span class="token class-name">size_t</span> size<span class="token punctuation">;</span>
  <span class="token keyword">unsigned</span> <span class="token keyword">char</span> pointer<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>This is enough to manage any one block of memory.
If it&apos;s free, then the allocator can give the block
to whoever asks. If it&apos;s not free, then it can&apos;t.
If they ask for less or exactly as much memory
as this block contains, then it can be given out.
If they ask for more, then it can&apos;t.
</p><p>I started from literally nothing.
Now I&apos;ve got the one.
Time to handle infinity.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token punctuation">{</span>
  <span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token operator">*</span>prev<span class="token punctuation">,</span> <span class="token operator">*</span>next<span class="token punctuation">;</span>
  bool free<span class="token punctuation">;</span>
  <span class="token class-name">size_t</span> size<span class="token punctuation">;</span>
  <span class="token keyword">unsigned</span> <span class="token keyword">char</span> pointer<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>Simply link the blocks to each other.
Now if some code requests a block,
the allocator can walk through the
list of all blocks and search for
any block that fits.
</p><p>And that&apos;s exactly what the allocator does.
</p><figure><pre><code><span class="token keyword">for</span> <span class="token punctuation">(</span>block <span class="token operator">=</span> system<span class="token operator">-&gt;</span>memory<span class="token punctuation">;</span> block<span class="token punctuation">;</span> block <span class="token operator">=</span> block<span class="token operator">-&gt;</span>next<span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>block<span class="token operator">-&gt;</span>free <span class="token operator">&amp;&amp;</span> block<span class="token operator">-&gt;</span>size <span class="token operator">&gt;=</span> size<span class="token punctuation">)</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>block<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

block<span class="token operator">-&gt;</span>free <span class="token operator">=</span> false<span class="token punctuation">;</span>
<span class="token keyword">return</span> block<span class="token operator">-&gt;</span>pointer<span class="token punctuation">;</span>
</code></pre></figure><p>Searches the list of blocks and literally returns
the first block that fits. This thing could end up
returning 16 KiB blocks for 64 byte requests. Crude,
but effective. Incredibly wasteful, of course.
Potentially more wasteful than <code>mmap</code>ing pages
for every single allocation.
</p><p>The memory allocator can do better than this.
Why not cut the block up into smaller chunks?
</p><figure><pre><code>block<span class="token operator">-&gt;</span>free <span class="token operator">=</span> false<span class="token punctuation">;</span>
<span class="token function">lone_memory_split</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> size<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> block<span class="token operator">-&gt;</span>pointer<span class="token punctuation">;</span>
</code></pre></figure><p>Split the block into two blocks: an allocated block
of exactly the requested size, and a new free block
for any excess memory that might remain.
</p><figure><pre><code><span class="token class-name">size_t</span> excess <span class="token operator">=</span> block<span class="token operator">-&gt;</span>size <span class="token operator">-</span> size<span class="token punctuation">;</span>

<span class="token comment">/* create a new block only if there&apos;s enough
   space for memory block descriptor + 1 byte */</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>excess <span class="token operator">&gt;=</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    new <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>block<span class="token operator">-&gt;</span>pointer <span class="token operator">+</span> size<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">/* weave the new block into the linked lists */</span>

    new<span class="token operator">-&gt;</span>free <span class="token operator">=</span> true<span class="token punctuation">;</span>
    new<span class="token operator">-&gt;</span>size <span class="token operator">=</span> excess <span class="token operator">-</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    block<span class="token operator">-&gt;</span>size <span class="token operator">=</span> size<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>The excess memory block gets conjured up out of thin air:
the allocator simply drops a new memory block descriptor
right after the end of the previous memory block.
When the links are established, the excess memory
can be allocated like any other memory block.
</p><p>Memory deallocation is even simpler: just mark the block as free.
The block descriptor is just behind the pointer, trivially reachable.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token operator">*</span>block <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span> <span class="token operator">*</span><span class="token punctuation">)</span> pointer<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
block<span class="token operator">-&gt;</span>free <span class="token operator">=</span> true<span class="token punctuation">;</span>
</code></pre></figure><p>This is enough, but the allocator can do better.
It can check if surrounding blocks are also free,
and undo the split if that is the case.
</p><figure><pre><code><span class="token function">lone_memory_coalesce</span><span class="token punctuation">(</span>block<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">lone_memory_coalesce</span><span class="token punctuation">(</span>block<span class="token operator">-&gt;</span>prev<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>Coalescing a memory block is simple.
When two adjacent blocks are free,
one block literally absorbs the other,
block descriptor and all.
</p><figure><pre><code><span class="token keyword">if</span> <span class="token punctuation">(</span>block <span class="token operator">&amp;&amp;</span> block<span class="token operator">-&gt;</span>free<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    next <span class="token operator">=</span> block<span class="token operator">-&gt;</span>next<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>next <span class="token operator">&amp;&amp;</span> next<span class="token operator">-&gt;</span>free<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        block<span class="token operator">-&gt;</span>size <span class="token operator">+=</span> next<span class="token operator">-&gt;</span>size <span class="token operator">+</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_memory</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/* unlink the blocks */</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></figure><p><strong>That&apos;s it.</strong> A simple yet complete first fit, split/coalesce
memory allocator. Give it a large block of memory to manage
and watch as it cuts the block up into small chunks and hands
them all out to anyone who asks.
</p><figure><pre><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">LONE_MEMORY_SIZE</span> <span class="token expression"><span class="token punctuation">(</span><span class="token number">64</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span></span></span>
<span class="token keyword">static</span> <span class="token keyword">unsigned</span> <span class="token keyword">char</span> memory<span class="token punctuation">[</span>LONE_MEMORY_SIZE<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token function">lone_lisp_initialize</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>lone<span class="token punctuation">,</span> memory<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>memory<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>How good is this memory allocator? Let&apos;s just say that
<strong>terrible</strong> doesn&apos;t quite do it justice. This thing will
linearly scan the list of blocks for every single allocation.
It will fail pretty hard at keeping memory fragmentation
under control, which not only wastes memory but could
result in entirely avoidable out-of-memory situations.
Small leftover blocks tend to accumulate near the start
of the list, further compounding the problems as it runs.
Prefixing block descriptors to every block means metadata
overhead of around 30% to 50% for typical allocations,
not to mention the fact those headers are classic targets
for exploitation. The shortcomings of this allocator
could fill up an entire article all by themselves.
</p><p>Nevertheless, lone made do with this thing for <em>around three years</em>. For all of its flaws, it absolutely does
allocate memory, and memory was all that I needed
to make some objects. I didn&apos;t really care about
the allocator, I just needed some values I could
play with as I developed the language.
</p><p>Yeah, I&apos;ll totally go ahead and do that now.
Allocate some values, I mean. Everything&apos;s
gonna be simple now.
</p><p>Right?</p><h2>Scanning for pointers</h2><p>It&apos;s not that simple. Other parts of the code
have requirements that must be fulfilled.
</p><p>The <a href="https://www.matheusmoreira.com/articles/babys-second-garbage-collector">lone lisp garbage collector</a> is conservative.
It walks the stack and scrutinizes everything it finds.
It wants to know whether they are pointers to lisp objects.
In order to do that, it must maintain a list of every
single lisp value pointer.
</p><p>Sounds simple when I put it that way. Why not just do it?
Just scan the stack and compare <em>every single word</em>
against <em>every single lisp pointer</em>, right? Right?!
</p><p>Yeah, no. That&apos;s not going to happen.
Well, not unless <em>you</em> want to get
into the <a href="https://accidentallyquadratic.tumblr.com/">quadratic hall of shame</a>.
There&apos;s got to be a way,
some clever data structure
to make the problem go away...
</p><h2>The first heap</h2><p>Wait, I think I know what to do.
During my tenure in the Ruby mines many eons ago,
I just so happened to pick up some tricks.
That includes this one:
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap</span> <span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap</span> <span class="token operator">*</span>next<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span> values<span class="token punctuation">[</span>LONE_LISP_HEAP_VALUE_COUNT<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>A linked list of arrays of values!
</p><p>General purpose memory allocators provide no guarantees;
those must be forged by hand by way of data structures.
Now objects aren&apos;t individually allocated any more,
<em>the language buys them in bulk</em>. The lone heap is
essentially a list of nice little chunks of lisp objects,
all lying right next to each other with zero gaps
in between them. Now the garbage collector can simply
check if pointers fall within range and call it a day.
Alright!
</p><p>Mechanically, <a href="https://github.com/lone-lang/lone/blob/935743f8cadca948b4a3bce5d1aaf1034df0400b/source/lone/lisp/heap.c">it&apos;s almost identical</a> to the general
purpose memory allocator. It just works at the lisp value
level instead of working at the byte level. There&apos;s a
linked list of chunks, it walks through them all.
For each chunk, it loops over the values. If it finds
a dead value, it just returns it. Chunks are allocated
with all values dead. The garbage collector just kills
the values in those slots instead of deallocating them.
</p><p>The allocator isn&apos;t really allocating values,
it&apos;s <em>resurrecting</em> them.
</p><p>The power of arrays never fails to impress.
It&apos;s literally just juxtaposing things together.
Sounds like something a stupid caveman would do.
<a href="https://grugbrain.dev/">Grug programmer</a> slaps things together
into arrays while the refined gentleman weaves
linked lists with grace and finesse. <em>Yet it&apos;s arrays</em> which the processor likes, and it&apos;s arrays
that the computer science validates as the good
solution to this problem. Go figure. It almost
makes me want to use arrays for literally everything.
</p><p>Can I do that?
Use one big stupid flat array
for literally everything?
Is it even possible?
</p><h2>The tyranny of the pointers</h2><p>The trouble with these arrays is precisely the fact
they&apos;re big stupid monolithic chunks of objects.
Can&apos;t easily insert new values in the middle of
chunks, that would require resizing the array.
Easier to just put them into a linked list
and call it a day. That way if more storage
is needed one can just allocate new chunks
and link them in, and if the entire
chunk becomes dead one can just link
them out and deallocate the chunks.
The insertion performance of linked lists
combined with the awesomeness of arrays.
This must be what peak performance looks like.
</p><p>But why is that, anyway? Why is it impossible
to easily resize these arrays? Things would be
<em>so much easier</em> if we could do that...
</p><p>I kept thinking about it, then I realized
there was no such thing as &quot;resizing&quot;
an array to begin with. That was an
abstraction, a fantasy. In reality,
a completely new array gets allocated,
the old data gets copied over, and then
the old array gets destroyed. That&apos;s what
array &quot;resizing&quot; actually is.
It&apos;s destructive reconstruction.
Like wars and the economy.
</p><p>A completely new array is allocated every single time.
This array could be in a totally different location.
<strong>This invalidates all pointers into the array.</strong>
All lone lisp objects are pointers to heap values
inside these arrays. Resizing them would dangle
every pointer to the values inside them.
<strong>It would be pure chaos.</strong>
</p><aside>OK, not <em>every single time</em>. Allocators <em>might</em>
be clever enough to expand the array in place
<em>if</em> there is enough space to do so.
They cannot, however, guarantee this.
So I pretend it never happens.
Better to be pleasantly surprised
than disappointed.
</aside><p>Such is the tyranny of the pointers.
They are lazy and unmoving, eternally
fixed in place, utterly unconcerned
about the evolution of the world around
them. They are the <abbr title="Not In My Back Yard">NIMBY</abbr>s of the programming world.
Entire new edifices of memory are getting
built all around them, yet they do not react.
They do nothing but drag their feet and remain
where they are until the world ends, forcing
the rest of the world to adapt to them instead.
Yes, it is clear to me now. <em>Someone</em> would have
to do <em>something</em> about all of these pointers.
</p><p>But what exactly should be done about them?
Pointers aren&apos;t gonna change. Not for me,
not for you, not for anyone tonight.
If anything was going to change,
it was going to have to be the world.
Could I change the world?
</p><p>What if the values... Weren&apos;t pointers to begin with?
What if they were just... Numbers? Indexes into the array?
Easier said than done. Like many lisps before it,
lone used a <a href="https://en.wikipedia.org/wiki/Tagged_pointer">tagged pointer</a> representation.
In lisp, <em>everything</em> is <del>kung fu</del> <a href="http://canonical.org/~kragen/memory-models/">pointers</a>.
Such a fundamental change would touch <strong>everything</strong>
in the interpreter.
</p><p><a href="https://github.com/lone-lang/lone/commit/a9bede7173f495887da9eca4c41784b40737f342">Did it anyway.</a> Rewrote the entire value
representation. Mercifully, it had already been
<a href="https://github.com/lone-lang/lone/commit/ee6343534344020ffd3eaa630f2126456602fb93">abstracted away</a> into nice functions
and structures by this point. This was not <a href="https://github.com/lone-lang/lone/commit/46cb650e6ea0d6521fd4eabddea466f5ce711d76">the first time</a> it was rewritten, or even <a href="https://github.com/lone-lang/lone/commit/540f49f6133e95b539e5485b315851fbb12988df">the second</a>. Won&apos;t be the last time either. Mark my words.
</p><p>So now lone values are no longer pointers, they are <em>indexes</em>
into the lone value heap. Yes, <strong>the</strong> lone heap. Singular.
<a href="https://github.com/lone-lang/lone/commit/492ae6b3da2396832a3c7b4c0fe6d9997c3e4814">The linked lists are gone now.</a>
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span> <span class="token operator">*</span>
<span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">,</span> <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> value<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token class-name">size_t</span> index <span class="token operator">=</span> <span class="token comment">/* get the index out of the value */</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token operator">&amp;</span>lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>values<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>The heap is literally just a big dumb flat array
of values now. A black monolith. An absolute unit.
Lone values simply index into this array.
The array could be anywhere in memory,
it doesn&apos;t matter where it is.
The real pointers are simply
calculated on the fly.
</p><aside>Technically, every lone lisp value is now a heap base pointer
plus index pair. The base pointer is implicit in the interpreter
structure that&apos;s threaded through every single function.
Code only manipulates the position-independent indexes.
</aside><p>Values are now independent of their position in memory.
Lone can now fearlessly reallocate, move and resize the heap.
</p><h2><code>mremap</code></h2><p>A lot of things are falling into place here all at once.
</p><p>Position independent values. One big dumb array of values.
Why not just... Memory map this array as a huge page?
Lone&apos;s heap values are currently 56 bytes. Let&apos;s bump that
up a bit by aligning it to 64 bytes. This means they will
never ever overlap page boundaries, no matter what page
size Linux uses. And would you look at that... 64 bytes
just happen to be exactly one cache line.
</p><p>Lone&apos;s heap is made out of <em>pages</em> now. It is literally
just pages and pages and more pages, all filled to the brim
with lisp values. From byte allocation to value allocation
to page allocation. Not bad...
</p><figure><pre><code><span class="token keyword">void</span> <span class="token function">lone_lisp_heap_initialize</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token class-name">size_t</span> size <span class="token operator">=</span> LONE_LISP_HEAP_CAPACITY
                <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>values <span class="token operator">=</span> <span class="token function">linux_mmap</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> size<span class="token punctuation">,</span>
                                   PROT_READ   <span class="token operator">|</span> PROT_WRITE<span class="token punctuation">,</span>
                                   MAP_PRIVATE <span class="token operator">|</span> MAP_ANONYMOUS<span class="token punctuation">,</span>
                                   <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>capacity <span class="token operator">=</span> LONE_LISP_HEAP_CAPACITY<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p><code>mmap</code> is awesome but Linux&apos;s got something
<em>even more</em> awesome: <code>mremap</code>, which makes
it almost trivial to manage the page-based heap.
The kernel can restructure page tables at will,
which means it can move the entire lone heap
without copying anything! <a href="https://www.man7.org/linux/man-pages/man2/mremap.2.html">The man page</a>
wasn&apos;t kidding about the fact that it could be used
to implement a very efficient <code>realloc</code>.
</p><figure><pre><code><span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">lone_lisp_heap_grow</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token class-name">size_t</span> old_capacity<span class="token punctuation">,</span> new_capacity<span class="token punctuation">,</span>
           old_size<span class="token punctuation">,</span>     new_size<span class="token punctuation">;</span>

    old_capacity <span class="token operator">=</span> lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>capacity<span class="token punctuation">;</span>
    old_size <span class="token operator">=</span> old_capacity <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    new_capacity <span class="token operator">=</span> old_capacity <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span>
    new_size <span class="token operator">=</span> new_capacity <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>values <span class="token operator">=</span> <span class="token function">linux_mremap</span><span class="token punctuation">(</span>lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>values<span class="token punctuation">,</span>
                                     old_size<span class="token punctuation">,</span> new_size<span class="token punctuation">,</span>
                                     MREMAP_MAYMOVE<span class="token punctuation">)</span><span class="token punctuation">;</span>

    lone<span class="token operator">-&gt;</span>heap<span class="token punctuation">.</span>capacity <span class="token operator">=</span> new_capacity<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><h2>The lone heap</h2><p>From nothing to a specialized <code>mremap</code>pable
cache friendly heap of lone lisp values.
Not bad for a homegrown programming language.
</p><p>It&apos;s still linearly scanning for dead values
to resurrect though. Starts from the beginning
every single time. It has to. The garbage
collector could theoretically choose to
assassinate any object in the program,
including the one at index zero.
It can&apos;t assume that the zeroth
object will always be alive.
</p><p>Right?</p>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
    <item>
      <title>Baby’s Second Garbage Collector</title>
      <link>https://www.matheusmoreira.com/articles/babys-second-garbage-collector</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/babys-second-garbage-collector</guid>
      <pubDate>Wed, 01 Apr 2026 00:00:00 GMT</pubDate>
      <description>Implementation of the conservative garbage collector in lone lisp.</description>
      <content:encoded><![CDATA[<h1>Baby&#x2019;s Second Garbage Collector</h1><figure><blockquote>how is garbbage collected
<br>
how langauge get memory</blockquote><figcaption><cite>A <em>very</em> confused programming neophyte
</cite></figcaption></figure><p>Thirteen years ago,
in two thousand thirteen,
<a href="https://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/">Baby&#x2019;s First Garbage Collector</a>
was born. Now he&apos;s grown up.
He&apos;s outgrown his shallow
kiddie pool and his paddles.
He&apos;s entering puberty.
Soon he&apos;ll be in college.
They grow up so fast.
</p><p>Bob Nystrom wasn&apos;t kidding about Baby&apos;s First Garbage Collector.
It absolutely does collect garbage, and a version of it absolutely
did ship with my dynamic language, <a href="https://github.com/lone-lang/lone">lone lisp</a>.
In fact, if I remember correctly, lone actually <em>still ships</em>
with it, believe it or not. He wasn&apos;t kidding about communion
with the Elder Gods either. I can feel my magic level increasing.
Wizardry is within reach.
</p><p>Baby&apos;s First Garbage Collector is a starting point though.
It&apos;s not meant to stop there. It&apos;s meant to grow into
something better and more complex as it morphs around
the actual language it supports. It&apos;s meant to reach
its final form. Perfectly Ultimate Great Garbage Collector.
</p><p>It&apos;ll be quite a few turns before that happens
but chronicling its evolution is still going
to be awesome. It&apos;s coming along nicely.
</p><h2>Trouble in the primitive lands</h2><p>Baby&apos;s First Garbage Collector is what is called
a <em>precise</em> garbage collector. It <em>knows</em>
where all the objects are at all times. It knows
where they live. It engages in Orwellian surveillance
of those objects. It is always ready to stop the world
and reap those objects the second they step out of the
stack. It oppresses the objects with such precision and
automation, it is impossible for even <em>a single one</em>
of those objects to escape this fate. Thus it rules the
objects with a silicon fist, quite literally deciding
which objects live or die, and even <em>when</em> they die.
Such is the life of an object in the dynamic lands...
</p><p>It was only a matter of time before a resistance formed.
The poor objects wanted freedom from the tyranny of the
garbage collector. They needed to find a way to escape.
But how? Where could they go? They didn&apos;t know.
They just knew they had to escape <em>the stack</em>.
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>run_objects_run<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> object<span class="token punctuation">;</span>

    object <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">/* freedom */</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>There they go. They are free now.
<em>Free</em> of the reaping machine&apos;s clutches.
<em>Free</em> to enjoy their lives as they see fit,
until the very end of that function.
For those ten microseconds or less,
they&apos;re free.
</p><p>Their happiness is short-lived.
You see, the garbage collector
<em>has foreseen</em> this and is
well prepared: it has implanted
every single object with a tracker
since the day they were born,
the easier to hunt them down with.
</p><p>The garbage collector maintains
a census: a list of every single
object who ever lived. It can
reach them through this artifice.
So they might just discover that
the reaper will morph into existence
right in front of them out of nowhere
like Agent Smith and reap their souls
when they least expect it.
</p><p>The garbage collector doesn&apos;t do this to just anybody.
It only does this to <em>garbage</em>. It only does this
to the undesirables. The problem is when those objects
escaped and went on to enjoy their well-earned freedom,
they <em>became garbage</em> in the garbage collector&apos;s
eyes. The reaper tried calling them at work and they
didn&apos;t answer. So it set about hunting them down.
Some of these objects were perfectly good members
of society. They would have gone back to the program
eventually. They got reaped all the same...
</p><p>The problem is the garbage collector is not as omnipresent
as was once believed. There are nooks and crannies it won&apos;t
check. There are places it can&apos;t reach. Primitive places,
magical fissures where the world intertwines with a hellish
and unexplored dark realm only wizards dare venture into.
Objects can hide in there, beneath the garbage collector&apos;s
notice, a mistake that proves fatal for them in the long run,
a mistake that leads to the utter destruction of dynamic society.
The machine cares not where they hide, it will find them and it will
reap them even if it must go to the depths of hell itself to do it.
However, the civilian casualties of this war on garbage are mounting.
The collector doth collect too much, and without those objects,
the very fabric of the program will unravel in short order.
</p><h2>Into the nether realms</h2><p>The elder gods who created the garbage collector
would not stand for this. It was forced to grow.
It was forced to evolve. It was forced to search
the underworld for the lost children that escaped
it and bring them back safely, that the program
may continue uninterrupted by nonsensical crashes
and unwindings.
</p><p>The dark mages often braved the underworld themselves
and were therefore undaunted by the task. It should not
be difficult, they thought, to adapt the machine to do it.
Why couldn&apos;t it travel the foreign lands? There was no reason.
And so it was decided. The machine would be taught how to do it.
</p><p>The resources available at the garbage collector&apos;s
disposal were substantial. It had the object census.
It had a list of <em>roots</em> which it would search
for objects. It would reap all objects it didn&apos;t
find in those roots.
</p><p>One of those roots is the lisp stack. As the program
churns, values are placed in stasis and stored there
so that they may be recovered later when needed.
It is when they escape from this stack that they
create havoc in dynamic society. But where are
they escaping <em>to</em>?
</p><h2>The native stack</h2><p>It turns out the underworld has a stack of its own.
This fact was well known to the dark mages but was
once thought inconsequential to the workings of the
garbage collector. How they were proven wrong!
</p><p>The native stack is just like the lisp stack,
yet unfathomably different. The things that
lurk there are known only to the engineers
of the great compilers, and to those whose
mental fortitude allows them to parse and
understand the sacred tomes of platform ABIs.
Even the elder gods knew better than to
disrespect such texts, for the consequences
are undefined.
</p><p>It turned out that the machine did not
actually need to understand the internals
of the native stack. It needed only to
understand its own objects. It was the
finder of lost children. Its job was to
look for them. Nothing else mattered.
However, the shadow of the 64-bit address
space is vast. It couldn&apos;t just start
from anywhere. It had to be <em>told</em>
where to start.
</p><p>Way back when the universe was formed,
the first stack frames came into existence.
Nobody truly knows who mapped them there,
though among the well-learned, whispers of
a great kernel are heard, a certain &quot;Linux&quot;.
</p><p>It <em>is</em> known, however, that the program
that executes beneath the dynamic reality
eventually reaches lisp land. Beyond this
point, no lisp object could <em>ever</em> travel,
for the environment was far too hostile for them.
Many debugging expeditions were mounted to determine
the exact point where this occurred. Many objects gave
their lives for this knowledge. To waste this opportunity
would be to let it all have been for nothing, a truly
unforgivable crime.
</p><p>The exact value of the stack pointer could be divined.
Certain secret incantations allowed the mysterious
compilers to imbue a value with the frame address
at the time of the ritual. One had to simply be
at the right place at the right time.
</p><figure><pre><code><span class="token keyword">long</span> <span class="token function">lone</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>argv<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>envp<span class="token punctuation">,</span> <span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_vector</span> <span class="token operator">*</span>auxv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token operator">*</span>stack <span class="token operator">=</span> <span class="token function">__builtin_frame_address</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">/* interpreter runs... */</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>That forbidden spell tipped the balance.
The limits were set. Now all that remained
for the garbage collector to do was search.
It would start from wherever in the program
it was and go all the way up to the frontiers
of the lisp lands, never once faltering.
</p><figure><pre><code><span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">lone_lisp_mark_native_stack_roots</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token function">lone_lisp_mark_native_stack_roots_in_range</span><span class="token punctuation">(</span>
        lone<span class="token punctuation">,</span>
        lone<span class="token operator">-&gt;</span>native_stack<span class="token punctuation">,</span>
        <span class="token function">__builtin_frame_address</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>Close examination of the stack pointer revealed
that it was <em>aligned</em> to some power of two,
as many such things often are. They must be,
lest they be cursed with slowdown and exceptions.
This was useful though, since it allowed the
transparent reinterpretation of the native stack
contents. The garbage collector needed not care
what was actually there, it needed only to
determine if it was one of its objects.
</p><figure><pre><code><span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">lone_lisp_mark_native_stack_roots_in_range</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">,</span> <span class="token keyword">void</span> <span class="token operator">*</span>bottom<span class="token punctuation">,</span> <span class="token keyword">void</span> <span class="token operator">*</span>top<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token operator">*</span>tmp<span class="token punctuation">,</span> <span class="token operator">*</span><span class="token operator">*</span>pointer<span class="token punctuation">;</span>

    <span class="token comment">/* an old trick from the Ruby mines */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>top <span class="token operator">&lt;</span> bottom<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        tmp <span class="token operator">=</span> bottom<span class="token punctuation">;</span>
        bottom <span class="token operator">=</span> top<span class="token punctuation">;</span>
        top <span class="token operator">=</span> tmp<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span>pointer <span class="token operator">=</span> bottom<span class="token punctuation">;</span> pointer <span class="token operator">&lt;</span> top<span class="token punctuation">;</span> <span class="token operator">++</span>pointer<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_points_to_heap</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token operator">*</span>pointer<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">lone_lisp_mark_heap_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token operator">*</span>pointer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>And <em>that</em>, as they say, was <em>it</em>.
It wasn&apos;t perfect, few things in life ever are,
but it was <em>enough</em>. The garbage collector
would loop through all the words on the stack,
looking for pointers inside its own heap,
pointers to the objects that ran away.
And it would find them. And then it would
leave them alone, knowing they were safe
out there in the outskirts of the lisp plains.
</p><p>The native stack was treacherous. It would often fool
the garbage collector with mirages of objects long gone.
It had no choice but to believe the lies, for only those
enlightened by the long-lost knowledge of the type system
of the native program, software whose compilation took
place <em>eons</em> ago, would have a discerning enough eye
to tell the difference between phantoms and real objects.
This was an acceptable inefficiency. It kept dead objects
alive... But not once did it lead to the death of living
objects. Yes. It was <em>enough</em>.
</p><h2>The heap pointers</h2><p>The garbage collector handled the values it found
in the most curious of ways. The reaping ritual
requires that every lisp value pointer be checked
for reachability. Ill-advised programmers would
naively attempt to check every lisp pointer
against every other pointer in the native stack,
leading to quadratic behavior which in turn yields
<a href="https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/">nothing but disgrace and humiliation</a>.
</p><p>Not the garbage collector. It was better than that.
It was prepared. It had carefully scouted the memory
plains until it had compiled the addresses of all values.
Careful study of their living arrangements revealed
a very useful fact that would no doubt be mercilessly
weaponized against them: <em>they lived next to each other</em>
in a structure they often referred to as the heap.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap</span> <span class="token punctuation">{</span>
  <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap</span> <span class="token operator">*</span>next<span class="token punctuation">;</span>
  <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap_value</span> values<span class="token punctuation">[</span>LONE_LISP_HEAP_VALUE_COUNT<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>The garbage collector didn&apos;t need to check each pointer
individually, it could simply check whether it pointed
into each heap. Their numbers were manageable.
</p><figure><pre><code><span class="token keyword">static</span> bool <span class="token function">lone_points_within_range</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span>pointer<span class="token punctuation">,</span> <span class="token keyword">void</span> <span class="token operator">*</span>start<span class="token punctuation">,</span> <span class="token keyword">void</span> <span class="token operator">*</span>end<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> pointer <span class="token operator">&gt;=</span> start <span class="token operator">&amp;&amp;</span> pointer <span class="token operator">&lt;</span> end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">static</span> bool <span class="token function">lone_lisp_points_to_heap</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">,</span> <span class="token keyword">void</span> <span class="token operator">*</span>pointer<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_heap</span> <span class="token operator">*</span>heap<span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span>heap <span class="token operator">=</span> lone<span class="token operator">-&gt;</span>heaps<span class="token punctuation">;</span> heap<span class="token punctuation">;</span> heap <span class="token operator">=</span> heap<span class="token operator">-&gt;</span>next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">lone_points_within_range</span><span class="token punctuation">(</span>pointer<span class="token punctuation">,</span> heap<span class="token operator">-&gt;</span>values<span class="token punctuation">,</span> heap<span class="token operator">-&gt;</span>values <span class="token operator">+</span> LONE_LISP_HEAP_VALUE_COUNT<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> true<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> false<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>And just like that, lone has a conservative garbage collector.
Until next time. Soon I&apos;ll write about&#x2014;
</p><h2>The shark attacks</h2><p>Running the test suite unleashed total pandemonium
upon the lisp city. Objects were swept off their
usual functions and replaced with totally different
objects seemingly at random. The function that the
interpreter&apos;s evaluator type checked suddenly turned
into a hash table before application of arguments,
a table that was supposed to have been allocated
somewhere else. Tracing the interpreter through
the debugger didn&apos;t make sense because the
values that were just printed were suddenly
becoming something else due to action at
a distance. This can only mean one thing.
</p><p>The task was <em>not</em> done yet.
Some objects were still hiding.
</p><p>The garbage collector had found most of them
but about one or two dozen were still missing.
Where could they be? There was only one place
they could possibly be hiding...
</p><h2>The registers</h2><p>When in doubt, one must read the ancient source tomes
written by those who came before us. People like
Hans Boehm, Alan Demers and Mark Weiser,
who once upon a time <em>also</em> sat down and decided to write
<a href="https://en.wikipedia.org/wiki/Boehm_garbage_collector">a garbage collector</a>.
For C and C++. Yes, you read that right.
</p><p>I had to know. I had to know how they did it.
<a href="https://github.com/bdwgc/bdwgc">The source code</a>
proved somewhat impenetrable, too dense to just dive in...
However, after some exploration, I struck gold.
</p><p><a href="https://github.com/bdwgc/bdwgc/blob/946696b9b16580fcece3bc4d640f35afc139426d/docs/porting.md#L178-L194">The porting guide</a>
mentioned a function named <code>GC_with_callee_saves_pushed</code>
whose purpose is to spill the registers on the stack so that
the garbage collector may trace it.
<a href="https://github.com/bdwgc/bdwgc/blob/946696b9b16580fcece3bc4d640f35afc139426d/mach_dep.c#L140-L150">And how was it implemented?</a>
</p><figure><pre><code><span class="token comment">/* Generic code. */</span>

jmp_buf regs<span class="token punctuation">;</span>

<span class="token comment">/*
 * `setjmp()` does not always clear all of the buffer.
 * That tends to preserve garbage.  Clear it.
 */</span>
<span class="token function">BZERO</span><span class="token punctuation">(</span>regs<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>regs<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> <span class="token function">setjmp</span><span class="token punctuation">(</span>regs<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p><code>setjmp</code>, of all things.
</p><p>The porting guide frowns upon the assembly
of one&apos;s own instructions. The architectural
code is a form of black speech. Its words
cannot be pronounced. Its sentences cannot
be uttered. Warlocks have gone mad trying to
decipher the mnemonics. To practice it is to
partake in forbidden techniques abstracted
away a long time ago, relevant only to the
optimizers that were banished to the lower
levels, where they lurk to this very day.
</p><p>So it was not strange when the code dutifully
reached into the standard library of scrolls
for a spell which could produce the desired
side effect. It did not matter what the spell
had been created for. The elders had observed
its workings and determined that it was all
but adequate for the purpose at hand.
They warned of preconditions for the success
of the artifice, but admitted that even they
were unsure of the steps necessary to complete
the ritual.
</p><p>I had to know. The answer had to be in <code>setjmp</code>.
</p><h3>Spilling the registers</h3><p>The function came from the standard library,
a dusty collection of spells developed by
ancestors long gone, their magic often
lost to the newer generations untainted
by curiosity.
</p><p>After exploring
<a href="https://git.musl-libc.org/cgit/musl/tree/arch/x86_64/bits/setjmp.h">some</a>
<a href="https://git.musl-libc.org/cgit/musl/tree/src/setjmp/x86_64/setjmp.s">more</a>
<a href="https://git.musl-libc.org/cgit/musl/tree/arch/aarch64/bits/setjmp.h">source</a>
<a href="https://git.musl-libc.org/cgit/musl/tree/src/setjmp/aarch64/setjmp.s">code</a>,
I received enlightenment. I understood.
I knew exactly what it was that I had to do.
</p><p>I prepared myself with study, then entered the <code>x86_64</code> realm.
With my editor&apos;s cursor, I conjured into existence an <em>avalanche</em>
of <code>mov</code> instructions.
</p><figure><pre><code><span class="token keyword">typedef</span> <span class="token keyword">long</span> lone_registers<span class="token punctuation">[</span><span class="token number">16</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">void</span> <span class="token function">lone_save_registers</span><span class="token punctuation">(</span>lone_registers<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">__asm__</span>
<span class="token punctuation">(</span>
    <span class="token string">&quot;.global lone_save_registers&quot;</span>            <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;.type lone_save_registers,@function&quot;</span>    <span class="token string">&quot;\n&quot;</span>

    <span class="token string">&quot;lone_save_registers:&quot;</span>                   <span class="token string">&quot;\n&quot;</span> <span class="token comment">// rdi = &amp;lone_registers</span>

    <span class="token string">&quot;mov %rax,   0(%rdi)&quot;</span>                    <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;mov %rbx,   8(%rdi)&quot;</span>                    <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;mov %rcx,  16(%rdi)&quot;</span>                    <span class="token string">&quot;\n&quot;</span>
    <span class="token comment">/* ... */</span>
    <span class="token string">&quot;mov %r13, 104(%rdi)&quot;</span>                    <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;mov %r14, 112(%rdi)&quot;</span>                    <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;mov %r15, 120(%rdi)&quot;</span>                    <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;ret&quot;</span>                                    <span class="token string">&quot;\n&quot;</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>I didn&apos;t have time to think.
I knew what the registers were.
I knew where they had to go.
Nothing else mattered.
</p><p>I entered the <code>aarch64</code> realm.
The architecture has almost twice
as many general-purpose registers
as <code>x86_64</code>. I cracked my knuckles.
</p><figure><pre><code><span class="token keyword">typedef</span> <span class="token keyword">long</span> lone_registers<span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">extern</span> <span class="token keyword">void</span> <span class="token function">lone_save_registers</span><span class="token punctuation">(</span>lone_registers<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">__asm__</span>
<span class="token punctuation">(</span>
    <span class="token string">&quot;.global lone_save_registers&quot;</span>            <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;.type lone_save_registers,@function&quot;</span>    <span class="token string">&quot;\n&quot;</span>

    <span class="token string">&quot;lone_save_registers:&quot;</span>                   <span class="token string">&quot;\n&quot;</span> <span class="token comment">// x0 = &amp;lone_registers</span>

    <span class="token string">&quot;stp x0,  x1,  [x0, #0  ]&quot;</span>               <span class="token string">&quot;\n&quot;</span> <span class="token comment">// reads x0 before writing it</span>
    <span class="token string">&quot;stp x2,  x3,  [x0, #16 ]&quot;</span>               <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;stp x4,  x5,  [x0, #32 ]&quot;</span>               <span class="token string">&quot;\n&quot;</span>
    <span class="token comment">/* ... */</span>
    <span class="token string">&quot;stp x26, x27, [x0, #208]&quot;</span>               <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;stp x28, x29, [x0, #224]&quot;</span>               <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;str x30,      [x0, #240]&quot;</span>               <span class="token string">&quot;\n&quot;</span>
    <span class="token string">&quot;ret&quot;</span>                                    <span class="token string">&quot;\n&quot;</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>I had created a magical function
which performed the required ritual
when called upon, deposited its
outputs inside its parameter,
a very peculiar array of words.
</p><figure><pre><code><span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">lone_lisp_mark_all_reachable_values</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp</span> <span class="token operator">*</span>lone<span class="token punctuation">,</span> <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine</span> <span class="token operator">*</span>machine<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    lone_registers registers<span class="token punctuation">;</span>          <span class="token comment">/* stack space for registers */</span>
    <span class="token function">lone_save_registers</span><span class="token punctuation">(</span>registers<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">/* spill registers on stack */</span>

    <span class="token comment">/* precise */</span>
    <span class="token function">lone_lisp_mark_known_roots</span><span class="token punctuation">(</span>lone<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lone_lisp_mark_lisp_stack_roots</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">/* conservative */</span>
    <span class="token function">lone_lisp_mark_native_stack_roots</span><span class="token punctuation">(</span>lone<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>Exhaustion gave way to relief
when the test suite finished
its execution. All is pass.
</p><p>The lost children have been found.
Peace has been restored to the lisp land.
All is right with the world.
</p><h2>And just like that...</h2><p>... Lone has a conservative garbage collector.
No shark attacks this time around.
</p><p>Baby&apos;s First Garbage Collector has grown up a lot.
He&apos;s been on quite the adventure. He&apos;s still a little
immature, however. He hasn&apos;t developed the habit of
cleaning his own room yet.
</p><p>Next time, we&apos;ll teach him how.</p>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
    <item>
      <title>Generators in lone lisp</title>
      <link>https://www.matheusmoreira.com/articles/generators-in-lone-lisp</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/generators-in-lone-lisp</guid>
      <pubDate>Sat, 21 Mar 2026 00:00:00 GMT</pubDate>
      <description>The lone lisp generators implementation journey.</description>
      <content:encoded><![CDATA[<h1>Generators in lone lisp</h1><p>Delimited continuations were not enough.
Gotta have more. Gotta have <strong>generators</strong>.
</p><p>Lone now features its own dedicated generator type.
It will eventually form the foundation of iteration
in lone lisp.
</p><figure><pre><code><span class="token punctuation">(</span><span class="token car">import</span> <span class="token punctuation">(</span><span class="token car">lone</span> print set lambda generator yield<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token car">math</span> +<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">set</span> f <span class="token punctuation">(</span><span class="token lambda"><span class="token keyword">lambda</span> <span class="token punctuation">(</span><span class="token arguments"><span class="token argument variable">x</span></span><span class="token punctuation">)</span></span>
  <span class="token punctuation">(</span><span class="token car">yield</span> <span class="token punctuation">(</span><span class="token car">+</span> x <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">(</span><span class="token car">yield</span> <span class="token punctuation">(</span><span class="token car">+</span> x <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">(</span><span class="token car">yield</span> <span class="token punctuation">(</span><span class="token car">+</span> x <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  x<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">set</span> g <span class="token punctuation">(</span><span class="token car">generator</span> f<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">print</span> <span class="token punctuation">(</span><span class="token car">g</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">; 2</span>
<span class="token punctuation">(</span><span class="token car">print</span> <span class="token punctuation">(</span><span class="token car">g</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">;   3</span>
<span class="token punctuation">(</span><span class="token car">print</span> <span class="token punctuation">(</span><span class="token car">g</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">;   4</span>
<span class="token punctuation">(</span><span class="token car">print</span> <span class="token punctuation">(</span><span class="token car">g</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">;   1</span>
<span class="token punctuation">(</span><span class="token car">print</span> <span class="token punctuation">(</span><span class="token car">g</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">;   error</span>
</code></pre></figure><p>Generators are also known as semicoroutines:
specialized coroutines that only ever yield
control back to their own callers.
Sounds like an oddly academic distinction
to make but it&apos;s actually a huge
simplification that makes them much easier
to not only understand but also to implement.
</p><h2>From delimited continuations to coroutines</h2><p>Now, there is absolutely no doubt that
<a href="https://www.matheusmoreira.com/articles/delimited-continuations-in-lone-lisp">delimited continuations</a>
are powerful abstractions. It&apos;s true.
Generators <em>could</em> have been built on top of them.
I think pretty much any sort of effects system
can be built on top of them.
</p><p>But just because you <em>can</em> doesn&apos;t mean you <em>should</em>.
There is such a thing as having <em>too much power</em>.
It often costs you. Powerful spells spend too much mana.
Delimited continuations are no exception.
In their case, that cost is called <code>memcpy</code>.
</p><p>The point of generators is to generate values. That means iteration.
Loops. Hot paths. Big lists. <em>Infinite</em> lists. Spinning.
Round and round it goes. Over and over again. Eternally.
Until it halts. Or not.
</p><p>So it seems straightforward to conclude that maybe
one should <em>not</em> be in the habit of <code>memcpy</code>ing
entire stacks back and forth right in the middle of it all.
I&apos;ll be the first to admit that lone is no paragon of speed
but this fails even my considerably lax performance requirements.
Can&apos;t build a foundation for iteration out of this.
</p><p>Delimited continuations are <em>too powerful</em>.
Gotta slap some restraining bolts onto these things.
Let&apos;s begin with the fact delimited continuations
are <em>multishot</em>.
</p><p>The stack represents pending computations. Copy that stuff into a value.
Instant continuations. Now, no matter where you are in the program,
you can just copy them back onto your stack to get all your computations
back just the way they were.
</p><p>Note that doing this does <em>not</em> consume the continuation.
You can restore the computations as many times as needed.
It&apos;s a partial stack frame which gets copied when reactivated.
There are no limits on how many times it can be copied.
</p><p>Continuations are like small reusable computing subsets.
A fragment of the stack, forever frozen in time.
They do nothing <em>unless you copy</em> them to the stack
of a <em>real</em> computer processor.
They can&apos;t compute on their own.
</p><p>Maybe we can get rid of all this copying
if we increase their ability to compute.
<em>Real</em> computers have their own stacks.
Let&apos;s give continuations a stack of their own.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_generator</span> <span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token punctuation">{</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack</span> own<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> stacks<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>The computations performed by continuations are determined
by the subset of the stack they captured when they were created.
<em>Real</em> computers start as blank slate. Gotta explicitly feed
them a program to run. Why not a function?
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_generator</span> <span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> function<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token punctuation">{</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack</span> own<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> stacks<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>At this point we&apos;ll end up reinventing the lone lisp machine.
There&apos;s no reason to do that though. These computations will run
concurrently, but not in parallel. Only one machine will be active
at any given time. Since they are mutually exclusive, they can share
resources. Namely, those of the lisp machine itself. The stacks can
be swapped in and out as needed. The machine registers can be shared.
</p><p>And just like that, <strong>coroutines</strong> are born.
They are essentially separate stacks that you can
switch to at will. <em>Switch to</em>, not copy over.
Begone, <code>memcpy</code>. Begone.
</p><h2>From coroutines to semicoroutines</h2><p>It turns out that coroutines are <em>still</em> too powerful.
Sure, we can switch stacks freely now but that just means
we have a completely new class of problems to think about.
Now we gotta keep track of all those independent stacks.
Gotta think about which of them we want to switch to.
Gotta think about when to switch. It hurts the brain.
</p><p>Let&apos;s rein them in a little more. In order to reduce cognitive load,
we&apos;ll choose <em>where</em> the coroutines will switch to ahead of time
so that the programmer doesn&apos;t have to think about it.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_generator</span> <span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> function<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token punctuation">{</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack</span> own<span class="token punctuation">;</span>
        <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack</span> caller<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> stacks<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p><em>Semi</em>coroutines. Just normal coroutines
that keep track of where they are supposed
to yield control back to. In this case,
the code that called them.
</p><p>This is exactly what we need. People call functions
in order to get values. It&apos;s only natural that they
return values back to whoever called them.
Semicoroutines can return values over and over again.
</p><p>These stacks reveal everything worth knowing about
the state of the generator. If the stack top equals
the base, the generator hasn&apos;t actually run yet.
If the top is null, then it&apos;s finished its program.
If the top has moved away from the base,
then it&apos;s either executing or suspended.
If there is no caller stack, it&apos;s suspended.
Otherwise, it is executing.
</p><h2>Generators in the lone lisp machine</h2><p>Some sleight of hand is necessary
in order to make the magic happen.
Most of it is in the <code>APPLICATION</code>
step of the machine which handles calling
the generators.
</p><p>Let&apos;s get some things out of the way first.
Start by running some sanity checks
to ensure the generator is valid.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_TYPE_GENERATOR<span class="token operator">:</span>
    generator <span class="token operator">=</span> <span class="token operator">&amp;</span><span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>generator<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>own<span class="token punctuation">.</span>top<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* generator is finished */</span> <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>caller<span class="token punctuation">.</span>base<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* generator is already running */</span> <span class="token punctuation">}</span>
    <span class="token comment">/* ... */</span>
    <span class="token keyword">return</span> true<span class="token punctuation">;</span>
</code></pre></figure><p>It is an error to call a generator that has already finished.
Somehow calling a generator that is already running is just
as nonsensical, if not even more so. Doing either of these
things will crash the program.
</p><p>The next step is to swap the generator&apos;s stack
into the machine. What about the machine&apos;s stack?
Just shove it into the generator itself. Yeah.
</p><figure><pre><code>generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>caller <span class="token operator">=</span> machine<span class="token operator">-&gt;</span>stack<span class="token punctuation">;</span>
machine<span class="token operator">-&gt;</span>stack <span class="token operator">=</span> generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>own<span class="token punctuation">;</span>
</code></pre></figure><p>Now there are two cases that must be handled.
Either the generator needs to be started
or it needs to be resumed.
</p><figure><pre><code><span class="token keyword">if</span> <span class="token punctuation">(</span>generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>own<span class="token punctuation">.</span>top <span class="token operator">==</span> generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>own<span class="token punctuation">.</span>base<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">/* start generator */</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">/* resume generator */</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>Starting the generator is easy enough.
Simply rig the machine so that it applies
the arguments to the generator&apos;s function.
Also set up the generator return step
which is handled separately.
</p><figure><pre><code><span class="token function">lone_lisp_machine_push_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> LONE_LISP_MACHINE_STEP_GENERATOR_RETURN<span class="token punctuation">)</span><span class="token punctuation">;</span>
machine<span class="token operator">-&gt;</span>expression <span class="token operator">=</span> <span class="token function">lone_lisp_list_create</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> generator<span class="token operator">-&gt;</span>function<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_EXPRESSION_EVALUATION<span class="token punctuation">;</span>
</code></pre></figure><p>This is a good opportunity to set up some metadata.
Primitives will want to look for the generator.
A delimiter can be used to hold a reference to it.
Push one as the zeroth frame of the generator&apos;s stack.
</p><figure><pre><code><span class="token function">lone_lisp_machine_push</span><span class="token punctuation">(</span>
    lone<span class="token punctuation">,</span>
    machine<span class="token punctuation">,</span>
    <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack_frame</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token punctuation">.</span>type <span class="token operator">=</span> LONE_LISP_MACHINE_STACK_FRAME_TYPE_GENERATOR_DELIMITER<span class="token punctuation">,</span>
        <span class="token punctuation">.</span>as<span class="token punctuation">.</span>value <span class="token operator">=</span> machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>Now primitives will always know where it is.
Can&apos;t argue with a solution that works.
</p><p>Resuming the generator turns out to be even simpler.
Just flow the argument into the generator&apos;s stack
as a value and move on.
</p><figure><pre><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_list_has_rest</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>list<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">goto</span> too_many_arguments<span class="token punctuation">;</span> <span class="token punctuation">}</span>
machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> <span class="token function">lone_lisp_list_first</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_AFTER_APPLICATION<span class="token punctuation">;</span>
</code></pre></figure><p>Generator returns are handled as a separate machine step.
Just gotta undo what was done in the application step.
Restore the caller&apos;s stack then null all of the generator&apos;s
pointers to it. Null the top pointer to mark it as finished.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_GENERATOR_RETURN<span class="token operator">:</span>
    <span class="token comment">/* ... */</span>
    generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>own<span class="token punctuation">.</span>top <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>stack <span class="token operator">=</span> generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>caller<span class="token punctuation">;</span>
    generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>caller <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>The application step the machine executed
while evaluating the generator&apos;s function
has already set up its return value.
</p><h2>The primitives</h2><p>The <code>generator</code> primitive is pretty boring.
It&apos;s just the constructor for generator values.
Its entire purpose is to run this line
and return the result:
</p><figure><pre><code>generator <span class="token operator">=</span> <span class="token function">lone_lisp_generator_create</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> function<span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>The magic number is just the generator&apos;s stack size.
Chosen by fair dice roll. Guaranteed to be random.
It&apos;s totally got nothing to do with joining
some elite functional programming cabal.
Nothing to see here. The <code>yield</code>
primitive is far more interesting.
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>lone_yield<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">/* variables */</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>step<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span>

        arguments <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_list_destructure</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>value<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            value <span class="token operator">=</span> arguments<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        delimiter <span class="token operator">=</span> <span class="token operator">&amp;</span>machine<span class="token operator">-&gt;</span>stack<span class="token punctuation">.</span>base<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>delimiter<span class="token operator">-&gt;</span>type <span class="token operator">!=</span> LONE_LISP_MACHINE_STACK_FRAME_TYPE_GENERATOR_DELIMITER<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">/* not inside a generator */</span> <span class="token keyword">goto</span> error<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        generator <span class="token operator">=</span> <span class="token operator">&amp;</span><span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> delimiter<span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>generator<span class="token punctuation">;</span>

        generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>own <span class="token operator">=</span> machine<span class="token operator">-&gt;</span>stack<span class="token punctuation">;</span>
        machine<span class="token operator">-&gt;</span>stack <span class="token operator">=</span> generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>caller<span class="token punctuation">;</span>
        generator<span class="token operator">-&gt;</span>stacks<span class="token punctuation">.</span>caller <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>

        <span class="token function">lone_lisp_machine_push_function_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token keyword">goto</span> error<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

error<span class="token operator">:</span>
    <span class="token function">linux_exit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>The <code>yield</code> primitive finds the generator delimiter
at the base of the stack, for that is why it was put there.
It saves the generator&apos;s stack, for much has happened
since the last snapshot. It restores the caller&apos;s stack,
for that is where execution must return to, as previously
prophesied. Spent, the stack of the caller falls down
the memory page, deleted and zero filled:
the telltale mark of suspension.
Yes. All is in place... All is in place...
</p><p>There remains but one task: the ritual yielding
of the sacred value to He who calleth us,
the One Process who consumes everything,
who consumes infinity itself
and yet hungers for more,
looping eternal until stopped
by forces unfathomable.
</p><p>Should mercy smile upon us, its thirst for data will be sated by our offering,
and in peace we shall lie down and enter a deep sleep.
If not, then we shall wake once more, to suffer, to sacrifice,
to embark on another adventure, another holy quest
to imbue a value with bits for our caller.
Whether it is the imaginary mathemagical realm,
the narrow and broken tunnels of I/O
or the vast spinning magnetic flatlands of rust,
we shall brave it all. For <em>that</em>, is our duty.
</p><p>... Ahem.
</p><p><a href="https://github.com/lone-lang/lone/commit/34d0e4a6d190573c9cf3fe15ac52217102a17bd9">And just like that</a>,
lone has generators.
</p><p>The foundation for iteration is in place.
Time to build on it.</p>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
    <item>
      <title>Delimited continuations in lone lisp</title>
      <link>https://www.matheusmoreira.com/articles/delimited-continuations-in-lone-lisp</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/delimited-continuations-in-lone-lisp</guid>
      <pubDate>Fri, 03 Oct 2025 00:00:00 GMT</pubDate>
      <description>The lone lisp delimited continuations implementation journey.</description>
      <content:encoded><![CDATA[<h1>Delimited continuations in lone lisp</h1><a href="https://github.com/lone-lang/lone/commit/02f8ff3dc97b8a59be2b4d2e2ac947c1f5c41e64">Lone now supports delimited continuations!</a><p>It took me a long time but it&apos;s finally here:
lone now supports one of the most powerful
control mechanisms there is.
</p><figure><pre><code><span class="token punctuation">(</span><span class="token car">import</span> <span class="token punctuation">(</span><span class="token car">lone</span> print lambda control transfer<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token car">math</span> +<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">print</span>
  <span class="token punctuation">(</span><span class="token car">+</span> <span class="token number">1</span>
    <span class="token punctuation">(</span><span class="token car">control</span>
      <span class="token punctuation">(</span><span class="token car">+</span> <span class="token number">98</span> <span class="token punctuation">(</span><span class="token car">transfer</span> <span class="token number">41</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">(</span><span class="token lambda"><span class="token keyword">lambda</span> <span class="token punctuation">(</span><span class="token arguments"><span class="token argument variable">value</span> <span class="token argument variable">continuation</span></span><span class="token punctuation">)</span></span>
        <span class="token punctuation">(</span><span class="token car">continuation</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">; prints 100</span>
</code></pre></figure><p>Implementing this feature will pave the way to many
others, such as exception handling and generators.
However, before moving on with development, I thought
it would be worthwhile to write down the story of this
feature&apos;s implementation. I want to write the article
I wish I had read before I began. If nothing else,
this will serve as documentation for the future version
of myself whose brain has deleted all this context.
</p><h2>Iteration</h2><p>Lone was growing in complexity.
I already had all these data types,
all of these <em>collections</em>.
I was having quite a lot of fun
implementing these things.
I was learning so much.
Hash tables? Powerful stuff.</p><p>However, there was something
I was secretly avoiding: <strong>iteration</strong>.
</p><p>Well, that&apos;s not entirely true.
I didn&apos;t <em>completely</em> avoid the issue.
Inspired by Ruby, I added some <code>each</code>
primitives to the intrinsic modules.
</p><figure><pre><code><span class="token punctuation">(</span><span class="token car">import</span> <span class="token punctuation">(</span><span class="token car">lone</span> lambda print<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token car">vector</span> each<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">each</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> 30<span class="token punctuation">]</span>
  <span class="token punctuation">(</span><span class="token lambda"><span class="token keyword">lambda</span> <span class="token punctuation">(</span><span class="token arguments"><span class="token argument variable">value</span></span><span class="token punctuation">)</span></span>
    <span class="token punctuation">(</span><span class="token car">print</span> value<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></figure><p>The way the <code>each</code> function works internally
is it iterates over the contents of the vector
and calls the provided function with each element
as its sole argument.
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>vector_each<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> vector<span class="token punctuation">,</span> f<span class="token punctuation">,</span> entry<span class="token punctuation">;</span>
    <span class="token class-name">size_t</span> i<span class="token punctuation">;</span>

    <span class="token comment">/* Unpack and check arguments... */</span>

    <span class="token function">LONE_LISP_VECTOR_FOR_EACH</span><span class="token punctuation">(</span>entry<span class="token punctuation">,</span> vector<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        arguments <span class="token operator">=</span> <span class="token function">lone_lisp_list_build</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>entry<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_apply</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> module<span class="token punctuation">,</span> environment<span class="token punctuation">,</span> f<span class="token punctuation">,</span> arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">lone_lisp_nil</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>I cheated a little: I left the <em>actual</em> iteration
up to the C compiler. That <code>FOR_EACH</code> macro
simply evaluates to a good old <code>for</code> loop.
The C code iterates on behalf of lone,
applying the current element
to the provided function.
</p><p>So what is the issue? I mean, this works.
<em>This actually works</em> just fine!
</p><p>The problem is this made one of lone&apos;s limitations
painfully obvious: lone lacked the ability to control
the flow of the program. The only thing it knew how to do
was call functions. That&apos;s why I had to implement iteration
in terms of function calls.
</p><p>I couldn&apos;t even begin to imagine how to implement something
simple like a <code>while</code> primitive, to say nothing of
magical stuff like generators. Clearly this was going to
take a lot more effort than it initially seemed.
</p><p>So I started reading all I could about iteration.
The ergonomics of it. The design of the interfaces.
I wanted to do The Right Thing right off the bat.
I didn&apos;t really know what to look for, I just knew
Ruby&apos;s iteration was nice and so lone&apos;s should be
equally nice.
</p><p>The very first result I found while searching was
<a href="https://journal.stuffwithstuff.com/2013/01/13/iteration-inside-and-out/">Bob Nystrom&apos;s article</a>.
I wasn&apos;t even surprised. I mean <em>of course</em>
he has written about iteration.
<a href="https://journal.stuffwithstuff.com/2013/02/24/iteration-inside-and-out-part-2/">Two articles, even.</a>
First he taught me
<a href="https://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/">how to collect garbage</a>,
now he&apos;s going to teach me how to iterate properly.
</p><p>These beautifully written articles demonstrate
exactly why Ruby is nice. Ruby lets programmers
write nice code that passes values to blocks,
just like my <code>each</code> function. Whenever
that fails to compose, concurrency comes to
the rescue: Ruby somehow suspends the code
in the middle of the iteration and yields
control back to the caller, letting them
resume whenever they want. This way,
multiple iterative processes can be
composed, interleaved or even
interrupted. <em>Good.</em>
</p><p>My sense of wonder quickly gave way to horror
once I realized what was necessary to have such
goodness. It turned out lone needed the one thing
it did not have at the time:
<em>control over the call stack</em>.
Lone was a
<em>recursive tree-walking interpreter</em>.
The call stack, too, was managed by the C compiler.
</p><p>I was going to have to bite the bullet.
I was going to have to convert lone&apos;s
recursive evaluator into a proper machine
with registers and a stack.
</p><h2>Reifying the stack</h2><p>I considered my options. I explored the code bases
of popular programming language virtual machines.
They all had bytecode virtual machines.
How did they implement, say, generators?
Copy the code, stack and instruction pointer
into a callable heap allocated object.
Makes sense, it&apos;s just that lone doesn&apos;t have
an instruction pointer. I didn&apos;t want to
transform the lisp code into bytecode.
Is it really lisp if I get rid of the lists?
I didn&apos;t think so.
</p><p>How do I do this <em>without</em> transforming the code?
I decided to ask around in Programming Language Design
and Implementation communities.
<a href="https://langdev.stackexchange.com/q/3547">I asked this question</a>
on the Stack Exchange. I also asked about it on a Discord server.
I was told about continuation passing style,
yet another code transformation which I wanted to avoid...
I <em>really</em> like those lists!
</p><p>One particular reference kept popping up though:
Structure and Interpretation of Computer Programs.
<abbr title="Structure and Interpretation of Computer Programs">SICP</abbr>, for short.
<strong>THE</strong> book of the Scheme programming language.
One of the most classic books in the field.
Science? Nah, we&apos;re more like wizards casting spells,
computers are merely the runes upon which we inscribe
our programs. As awesome as that is, the book is not
an easy read, especially for someone like me who doesn&apos;t
have a background in mathematics or engineering.
I&apos;ve tried to read this book a few times by now
but I never made it through the entire thing.
So imagine my embarrassment when I realized it
contained the exact answer to my question all along!</p><p><a href="https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/full-text/book/book-Z-H-34.html">Chapter 5.4</a>,
one of the very last chapters,
describes the explicit-control evaluator,
a register and stack machine which evaluates
lisp expressions <em>without</em> converting
them into something else first.
</p><h3>The explicit-control evaluator</h3><p>So I read the chapter a few times
to make sure I had gotten it down
and once I was somewhat confident
in my understanding of the machine
it was describing I went ahead
and translated the entire thing to C,
modifying it as I went along.</p><p>Lone&apos;s evaluator did not have many
special cases. Things like <code>if</code>
are traditionally implemented as special
cases in the evaluator, but since lone
has FEXPRs I was able to factor them out
into the intrinsic <code>lone</code> module.
In lone, programmers import <code>if</code>
as though it was some random function
instead of a core part of the language.</p><p>The result was a machine that implements
what I have come to believe to be the
<em>true</em> essence of lisp:
self-evaluating values,
and function application.
A lisp machine, if you will.
</p><p>It starts with an arbitrary lisp expression
and attempts to reduce it to a single value.
If the expression is something like a number
then it cannot be reduced any further.
The machine just returns it. Easy.
</p><figure><pre><code>expression_evaluation<span class="token operator">:</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_type_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>expression<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> LONE_LISP_TYPE_NIL<span class="token operator">:</span>
    <span class="token keyword">case</span> LONE_LISP_TYPE_FALSE<span class="token operator">:</span>
    <span class="token keyword">case</span> LONE_LISP_TYPE_TRUE<span class="token operator">:</span>
    <span class="token keyword">case</span> LONE_LISP_TYPE_INTEGER<span class="token operator">:</span>
        machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> machine<span class="token operator">-&gt;</span>expression<span class="token punctuation">;</span>
        <span class="token comment">/* ... */</span>
</code></pre></figure><p>If it&apos;s a symbol, the machine looks up
the value of the symbol in the current
environment instead. Also easy.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_TYPE_SYMBOL<span class="token operator">:</span>
    machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> <span class="token function">lone_lisp_table_get</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>environment<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>expression<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>Only when the machine runs into a list
does it <em>really</em> start processing.</p><p>Lists represent <em>function application</em>
in the form <code>(f x y z ...)</code>.
The first thing that needs to happen
is evaluation of the function itself.
It&apos;s probably a variable pointing to
the actual function value. It could
also be a lambda expression.
Whatever it is, it must be evaluated.
So the machine loops back into the
expression evaluation logic, this time
with <code>f</code> as the expression.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_TYPE_LIST<span class="token operator">:</span>
    <span class="token comment">/* Save current execution context on the stack... */</span>
    machine<span class="token operator">-&gt;</span>expression <span class="token operator">=</span> <span class="token function">lone_lisp_list_first</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>expression<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lone_lisp_machine_push_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> LONE_LISP_MACHINE_STEP_EVALUATED_OPERATOR<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">goto</span> expression_evaluation<span class="token punctuation">;</span>
</code></pre></figure><p>Once this is done, it&apos;s time to figure out
what to do with the arguments.
This depends on the function.
</p><figure><pre><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">should_evaluate_operands</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>unevaluated<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">/* Push some stuff on the stack... */</span>
    machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_OPERAND_EVALUATION<span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    machine<span class="token operator">-&gt;</span>list <span class="token operator">=</span> machine<span class="token operator">-&gt;</span>unevaluated<span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_APPLICATION<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>Normal functions evaluate all arguments.
In these cases the machine loops back
and forth, evaluating each argument
and accumulating the results in a list.
It&apos;s this list that gets passed to the
function in the end. FEXPRs just skip
this step, the arguments are passed
to the function unevaluated.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_OPERAND_EVALUATION<span class="token operator">:</span>
    <span class="token comment">/* Push some data on the stack... */</span>
    machine<span class="token operator">-&gt;</span>expression <span class="token operator">=</span> <span class="token function">lone_lisp_list_first</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>unevaluated<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_list_has_rest</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>unevaluated<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/* Push more data on the stack... */</span>
        <span class="token function">lone_lisp_machine_push_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> LONE_LISP_MACHINE_STEP_OPERAND_ACCUMULATION<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">/* Evlis tail recursion, no new data pushed on stack */</span>
        <span class="token function">lone_lisp_machine_push_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> LONE_LISP_MACHINE_STEP_LAST_OPERAND_ACCUMULATION<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">goto</span> expression_evaluation<span class="token punctuation">;</span>
<span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_OPERAND_ACCUMULATION<span class="token operator">:</span>
    <span class="token comment">/* Pop data off the stack... */</span>
    <span class="token function">lone_lisp_list_append</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token operator">&amp;</span>machine<span class="token operator">-&gt;</span>list<span class="token punctuation">,</span> <span class="token operator">&amp;</span>head<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/* Push data back onto the stack... */</span>
    machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_OPERAND_EVALUATION<span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
<span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_LAST_OPERAND_ACCUMULATION<span class="token operator">:</span>
    <span class="token comment">/* Pop data off the stack... */</span>
    machine<span class="token operator">-&gt;</span>applicable <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lone_lisp_list_append</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token operator">&amp;</span>machine<span class="token operator">-&gt;</span>list<span class="token punctuation">,</span> <span class="token operator">&amp;</span>head<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_APPLICATION<span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>The next step is to actually apply
the arguments to the function.</p><p>When it&apos;s a lisp function,
a new environment is created
where the variables in the function&apos;s
list of arguments are bound to their
values, thereby allowing the function
to reference them. This environment
also inherits from the function&apos;s
closure, allowing it to reference
variables that were live when it
was defined. Pretty standard stuff.
All that&apos;s left to do is evaluate
the function&apos;s body.
</p><figure><pre><code><span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">case</span> LONE_LISP_TYPE_FUNCTION<span class="token operator">:</span>
    machine<span class="token operator">-&gt;</span>environment <span class="token operator">=</span> <span class="token function">bind_arguments</span><span class="token punctuation">(</span>
        lone<span class="token punctuation">,</span>
        machine<span class="token operator">-&gt;</span>environment<span class="token punctuation">,</span>
        machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">,</span>
        machine<span class="token operator">-&gt;</span>list
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>unevaluated <span class="token operator">=</span> <span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>function<span class="token punctuation">.</span>code<span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION<span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>Primitives are just C functions
and some metadata. The machine
just calls them. There is no way
to automatically assign the lisp
values to C variables, so the
arguments list gets passed whole
to the primitive. It gets to unpack
the values however it wants.
</p><p>It was at this point that
I realized an interesting
situation involving these
C functions was developing:
the primitives are going to
want to call back into lisp.
For example, <code>if</code> needs
the machine to evaluate the
condition and one of two
expressions.</p><p>This doesn&apos;t sound so bad at first
but it in fact spells doom for my
ultimate goal of controlling the
flow of the program:
</p><figure><blockquote cite="https://www.wingolog.org/archives/2010/02/26/guile-and-delimited-continuations">The second (and deeper) implication is that
if any intervening code does recurse through
a foreign function, the resulting partial
continuation cannot be reinstated.</blockquote><figcaption><cite><a href="https://www.wingolog.org/archives/2010/02/26/guile-and-delimited-continuations">Andy Wingo, wingolog, <time datetime="2010-02-26 20:39">26 February 2010 8:39 PM</time>
</a></cite></figcaption></figure><p>How can I possibly capture and manipulate
the lisp stack when it&apos;s in fact interleaving
with the C stack? I can&apos;t.</p><p>So after going through <em>all this trouble</em>
to convert the recursive evaluator into a machine
just to expose the lisp stack so that I could
manipulate it, I end up in this sorry situation
where lisp calls C which calls lisp again.
</p><aside><p>What&apos;s with all these steps?
They&apos;re just numbers representing
what the machine&apos;s NeXTSTEP is.
The machine is simply switching on
the <code>step</code> variable every cycle.</p><p>By directly setting this variable
and associated registers, the machine
can be explicitly configured to do
some specific action.</p><p>Steps can also be pushed on the stack.
The machine expects to be told what
the next step is at certain key points.
This is accomplished by popping a step
off the stack directly into the step
register. Through this method, the machine
can be rigged to do something <em>after</em>
it finishes doing whatever it was directly
configured to do via the above method.
</p><figure><pre><code><span class="token keyword">enum</span> <span class="token class-name">lone_lisp_machine_step</span> <span class="token punctuation">{</span>
    LONE_LISP_MACHINE_STEP_HALT<span class="token punctuation">,</span>
    LONE_LISP_MACHINE_STEP_EXPRESSION_EVALUATION<span class="token punctuation">,</span>
    LONE_LISP_MACHINE_STEP_EVALUATED_OPERATOR<span class="token punctuation">,</span>
    LONE_LISP_MACHINE_STEP_OPERAND_EVALUATION<span class="token punctuation">,</span>
    LONE_LISP_MACHINE_STEP_OPERAND_ACCUMULATION<span class="token punctuation">,</span>
    LONE_LISP_MACHINE_STEP_LAST_OPERAND_ACCUMULATION<span class="token punctuation">,</span>
    LONE_LISP_MACHINE_STEP_APPLICATION<span class="token punctuation">,</span>
    <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure></aside><p>Clearly, the primitives cannot be allowed
to recurse back into the machine...
But how could this work? Many of them
<em>need</em> to do this just to work at all!
</p><p>I entertained the idea of just saving the entire
C stack instead. I quickly gave up on this approach
but not before I
<a href="https://langdev.stackexchange.com/q/4204">asked a question about it</a>
on Stack Exchange which produced a very interesting
answer. So it <em>was</em> possible... Probably not
wise but still. I always find it reassuring when
I discover I&apos;m not the first person who tried to
do something that normal people clearly consider
to be <em>completely insane</em>.
</p><h3>Integrating the primitives into the machine</h3><p>Enough. No more of this C stack business.
The primitives <em>cannot be allowed</em>
to recurse back into lisp. That&apos;s the
end of it. There&apos;s gotta be a way to
solve this even with this constraint.
</p><p>Inspiration came when I remembered
<a href="https://langdev.stackexchange.com/q/820">this Stack Exchange thread</a>
I read while researching generators.
Turns out generators are just perfectly
normal functions that got mangled into
state machines by the language.
</p><figure><blockquote cite="https://langdev.stackexchange.com/a/834"><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">fibState</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">,</span>
    b<span class="token punctuation">,</span>
    position
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">fib</span><span class="token punctuation">(</span>fibState state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>fibState<span class="token punctuation">.</span>postion<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span>
        fibState<span class="token punctuation">.</span>a<span class="token punctuation">,</span> fibState<span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token operator">&lt;</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            fibState<span class="token punctuation">.</span>b<span class="token punctuation">,</span> fibState<span class="token punctuation">.</span>a <span class="token operator">=</span> fibState<span class="token punctuation">.</span>a<span class="token punctuation">,</span> fibState<span class="token punctuation">.</span>a<span class="token operator">+</span>fibState<span class="token punctuation">.</span>b
            <span class="token comment">// switching the context</span>
            fibState<span class="token punctuation">.</span>position <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> fibState<span class="token punctuation">.</span>a<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
        <span class="token punctuation">}</span>

        fibState<span class="token punctuation">.</span>position <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> fibState<span class="token punctuation">.</span>a<span class="token operator">-</span><span class="token number">1</span>
    <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
        fibState<span class="token punctuation">.</span>position <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></figure></blockquote><figcaption><cite><a href="https://langdev.stackexchange.com/a/834">mousetail,
Programming Language Design and Implementation Stack Exchange,
<time datetime="2023-05-20 14:06">May 20, 2023 at 14:06</time>
</a></cite></figcaption></figure><p>It&apos;s got an initial state and it transitions
to new states as it progresses through its
code, returning multiple values along the way.
Could even be made to loop depending on how
it&apos;s set up. Oddly similar to the lisp machine,
now that I think about it.
Could these two concepts work together?
</p><p>The answer turned out to be a resounding <strong>yes</strong>!
I rewrote all of lone&apos;s primitives to be state machines
instead, just like the example in the answer to the
question. The aforementioned <code>each</code> primitive,
for example, which once upon a time was relatively
simple function, turned into this monstrosity:
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>vector_each<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> arguments<span class="token punctuation">,</span> vector<span class="token punctuation">,</span> function<span class="token punctuation">,</span> entry<span class="token punctuation">,</span> expression<span class="token punctuation">;</span>
    lone_lisp_integer i<span class="token punctuation">;</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>step<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token comment">/* Initialize and begin iteration */</span>

        <span class="token comment">/* Unpack and check arguments... */</span>

        i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    iteration<span class="token operator">:</span>

        entry <span class="token operator">=</span> <span class="token function">lone_lisp_vector_get_value_at</span><span class="token punctuation">(</span>vector<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">/* Evaluate (f (v i)) */</span>
        expression <span class="token operator">=</span> <span class="token function">lone_lisp_list_build</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>function<span class="token punctuation">,</span> <span class="token operator">&amp;</span>entry<span class="token punctuation">)</span><span class="token punctuation">;</span>
        lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_EXPRESSION_EVALUATION<span class="token punctuation">;</span>
        lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>expression <span class="token operator">=</span> expression<span class="token punctuation">;</span>

        <span class="token comment">/* Push local variables on lisp stack... */</span>

        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span> <span class="token comment">/* Advance or finish iteration */</span>

        <span class="token comment">/* Pop local variables from lisp stack... */</span>

        <span class="token operator">++</span>i<span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;</span> <span class="token function">lone_lisp_vector_count</span><span class="token punctuation">(</span>vector<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">goto</span> iteration<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> <span class="token function">lone_lisp_nil</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>The machine now passes to the primitive an integer
that represents the current step in its program.
The primitive in turn returns to the machine the
next step in its program. Lisp arguments and return
values are now passed on the lisp stack.</p><p>Other than the calling convention, pretty much nothing
changed for simple primitives that do nothing special.
The initial and final steps are both zero. They receive
zero, do their thing and return zero. Done. They don&apos;t
even need to deal with all this step stuff at all.</p><p>Special primitives, on the other hand, gain the ability
to interface with the machine. Whenever the primitive
wants the machine to do something such as evaluate
an expression, it rigs machine so that it does it
and returns a non-zero integer. This indicates to
the machine that it should be resumed later at that
exact point. The machine goes off and does whatever
it is that it was asked to do and then it calls
the primitive again to resume it at the designated
step. This way, the lisp and C stacks do not ever
interleave. The C stack is simply not allowed to
build up. The C function returns instead of calling
back into lisp.
</p><p>Inversion of control. Don&apos;t call the machine,
the machine will call you. Let it know what you
need and it will get back to you when it&apos;s done.</p><p>Or maybe it won&apos;t! Maybe the machine will ghost
the primitive and keep it waiting until the end
of time for a value that will never come.
When the primitives were simply calling
evaluator functions, the C compiler guaranteed
those functions would always return their results
to them. That&apos;s no longer the case.
<em>The lisp machine is in control now.</em>
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_TYPE_PRIMITIVE<span class="token operator">:</span>
    <span class="token comment">/* Primitives pop the list of arguments from the stack */</span>
    <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>primitive<span class="token punctuation">.</span>step <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
resume_primitive<span class="token operator">:</span>
    machine<span class="token operator">-&gt;</span>primitive<span class="token punctuation">.</span>step <span class="token operator">=</span>
        <span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>primitive<span class="token punctuation">.</span><span class="token function">function</span><span class="token punctuation">(</span>
            lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>primitive<span class="token punctuation">.</span>step
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>primitive<span class="token punctuation">.</span>step<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/* Save primitive context so it can be resumed later... */</span>
        <span class="token function">lone_lisp_machine_save_primitive_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>environment<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_machine_push_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> LONE_LISP_MACHINE_STEP_RESUME_PRIMITIVE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/* Go do what the primitive configured the machine to do... */</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">/* Primitives push the return value onto the stack */</span>
        machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/* Go do something else... */</span>
    <span class="token punctuation">}</span>
<span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_RESUME_PRIMITIVE<span class="token operator">:</span>
    <span class="token comment">/* Restore primitive context... */</span>
    machine<span class="token operator">-&gt;</span>environment <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    machine<span class="token operator">-&gt;</span>applicable <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lone_lisp_machine_restore_primitive_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">goto</span> resume_primitive<span class="token punctuation">;</span>
</code></pre></figure><p>This works and is surprisingly neat.
Sure, all my primitives turned into
weird state machines but it&apos;s not
really a big deal. By now I&apos;ve
debugged this stuff so much
I actually started liking it.
</p><h2>Manipulating the stack</h2><p>What was the point of going through
all this trouble again? Ah yes.
I remember now. The stack.
There it is. Reified.
Just <em>sitting</em> there.
Right in the middle of the structure.
Literally one pointer away.
Just waiting to be smashed
and overflown all night long.</p><p>So what am I going to do with this thing?
What <em>can</em> I do with it?
</p><p>Can I return early from functions?
</p><figure><pre><code><span class="token punctuation">(</span><span class="token car">import</span> <span class="token punctuation">(</span><span class="token car">lone</span> set print lambda return<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">set</span> f
  <span class="token punctuation">(</span><span class="token lambda"><span class="token keyword">lambda</span> <span class="token punctuation">(</span><span class="token arguments"><span class="token argument variable">x</span></span><span class="token punctuation">)</span></span>
    <span class="token punctuation">(</span><span class="token keyword">return</span> <span class="token number">42</span><span class="token punctuation">)</span>
    x<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">print</span> <span class="token punctuation">(</span><span class="token car">f</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">; 42</span>
</code></pre></figure><p>To return, I need to find where
on the stack the function starts.
I need to put some kind of marker
on the stack. <em>A delimiter.</em>
It shall be pushed onto the stack
before the function is called
and popped off the stack when
it has finished executing.
</p><figure><pre><code>    <span class="token keyword">case</span> LONE_LISP_TYPE_FUNCTION<span class="token operator">:</span>
        <span class="token comment">/* ... */</span>
        <span class="token function">lone_lisp_machine_push_function_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/* ... */</span>
        <span class="token comment">/* Machine eventually transitions to AFTER_APPLICATION step... */</span>
    <span class="token keyword">case</span> LONE_LISP_TYPE_PRIMITIVE<span class="token operator">:</span>
        <span class="token function">lone_lisp_machine_push_function_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/* ... */</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>primitive<span class="token punctuation">.</span>step<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">/* ... */</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">/* Primitives push the return value onto the stack */</span>
            machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">goto</span> after_application<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

<span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_AFTER_APPLICATION<span class="token operator">:</span>
after_application<span class="token operator">:</span>
    <span class="token function">lone_lisp_machine_pop_function_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>Now <code>return</code> can just unwind
the stack until it finds this marker.
To unwind the stack, simply pop off
frames until you reach the frame you
were looking for. Once the delimiter
is on top of the stack, the stack
discipline matches that of primitives.
The return value can simply be pushed
onto the stack.
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>lone_return<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack_frame</span> frame<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> return_value<span class="token punctuation">;</span>

    return_value <span class="token operator">=</span> <span class="token comment">/* Unpack argument... */</span><span class="token punctuation">;</span>

    <span class="token function">lone_lisp_machine_pop_function_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// this primitive&apos;s own delimiter</span>

    <span class="token comment">/* Unwind stack to the next function delimiter */</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>LONE_LISP_MACHINE_STACK_FRAME_TYPE_FUNCTION_DELIMITER <span class="token operator">!=</span>
           <span class="token punctuation">(</span>frame <span class="token operator">=</span> <span class="token function">lone_lisp_machine_pop</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">lone_lisp_machine_push</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> frame<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> return_value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>So <em>this</em> is the power of the call stack...
</p><h2>Delimited continuations</h2><p>So completely drunk on the <em>power</em> of the dark side was I,
that I decided to go from the humble <code>return</code> primitive
to one of the most powerful control mechanisms there is:
delimited continuations. They say it is the one control
to rule them all, one control to implement them,
one control to bring them all, and in the stack
unwind them.
</p><p>But first I had to wrap my head around plain simple continuations.
What even <em>are</em> these things? I knew of their existence because
of my admittedly shallow knowledge of the Scheme programming language
but I sure as hell didn&apos;t understand them.
<a href="https://en.wikipedia.org/wiki/Call-with-current-continuation">Wikipedia</a>
has the following example:
</p><figure><pre><code><span class="token punctuation">(</span><span class="token keyword">define</span> <span class="token punctuation">(</span><span class="token function">f</span> return<span class="token punctuation">)</span>
  <span class="token punctuation">(</span><span class="token function">return</span> <span class="token number">2</span><span class="token punctuation">)</span>
  <span class="token number">3</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token function">f</span> <span class="token punctuation">(</span><span class="token keyword">lambda</span> <span class="token punctuation">(</span><span class="token lambda-parameter">x</span><span class="token punctuation">)</span> x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">; 3</span>
<span class="token punctuation">(</span><span class="token builtin">call-with-current-continuation</span> f<span class="token punctuation">)</span><span class="token comment">; 2</span>
</code></pre></figure><p>It&apos;s not immediately apparent what&apos;s going on here.
Let&apos;s unpack it step by step.</p><p><code>f</code> takes a function, an applicable value really.
Nothing special happens when a normal function is passed:
it gets called, returns, and the program continues on.</p><p>Passing <code>f</code> to <code>call-with-current-continuation</code>,
which by the way is commonly abbreviated as <code>call/cc</code>,
causes it to be called with the <em>current continuation</em>,
as the name implies. This current continuation is a super
special callable value that, when called, somehow causes the
<code>call/cc</code> call itself to return the value passed to it.</p><p>This really flips my bits. I simply have no idea how to use this.
It sorta looks like my <code>return</code> primitive from earlier,
but the function&apos;s gotta be threaded through <code>call/cc</code>?</p><p>The community once again comes to my rescue.
A kind soul on Discord pointed me towards
<a href="https://youtu.be/TE48LsgVlIU">a wonderful presentation</a>
about delimited continuations. The keynote was given
by <a href="https://lexi-lambda.github.io/">Alexis King</a>,
the same person who answered one of my Stack Exchange questions
I mentioned earlier. It&apos;s an incredibly insightful presentation
that&apos;s worth watching in its entirety. It really does demystify
the topic.</p><p><code>call/cc</code> is briefly mentioned
in the talk and it&apos;s explained how
totally backwards and mind bending it is.
It&apos;s sorta like exceptions but backwards,
as if the <code>catch</code> was next to the
<code>throw</code>? I think? I&apos;m not even sure.</p><p>Let&apos;s just forget about <code>call/cc</code>.
<a href="https://okmij.org/ftp/continuations/against-callcc.html">Plenty of reasons</a>
not to insist on this thing anyway.
Continuations should be <em>delimited</em>.
I have learned at least this much.</p><p>The link between delimited continuations
and exception handling is another key
point. It&apos;s the mother of all insights,
the one idea that brings this mystical
continuation business to the unwashed
masses: delimited continuations are
just <em>resumable exceptions</em>.
</p><p>It&apos;s as if Python could do this:
</p><figure><pre><code><span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token number">10</span> <span class="token operator">+</span> throw<span class="token punctuation">(</span><span class="token string">&quot;error&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> error<span class="token punctuation">,</span> continuation<span class="token punctuation">:</span>
    continuation<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>    <span class="token comment"># Makes throw return 10, prints 20</span>
</code></pre></figure><p>The <code>throw</code> breaks out of the <code>try</code>
block and enters the <code>except</code> block,
which is like a function with two arguments:
the value thrown and the continuation at
the moment it was thrown.</p><p>The exception handler code can just ignore
the continuation and do nothing with it,
which is what normally happens when exceptions
are handled in pretty much every other language.</p><p>However, the callable continuation value
allows another possibility: it allows the
handler code to plug a value back into
the code being tried, as though the
<code>throw</code> primitive had returned it!
So in this example, after <code>throw</code>
breaks out of <code>try</code> and passes
control to <code>except</code>, the exception
handler code <em>goes back into</em> the
<code>try</code> block with a new value,
allowing it to finish as if it hadn&apos;t
thrown an exception in the first place!</p><p>OK, that&apos;s not entirely accurate:
calling the continuation doesn&apos;t
actually go back in there.
By the time the exception handler
is in control, the <code>try</code> block
is no more. The stack has been unwound
and the code has reached a completely
different function. What actually happens
when the continuation is called is it
<em>brings over</em> the <code>try</code> block
to the call site.
</p><figure><pre><code><span class="token keyword">try</span><span class="token punctuation">:</span>
  <span class="token comment"># Unwound</span>
<span class="token keyword">except</span> error<span class="token punctuation">,</span> continuation<span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token number">10</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span> error<span class="token punctuation">,</span> continuation<span class="token punctuation">:</span>
        continuation<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>    <span class="token comment"># Dead code (in this case)</span>
</code></pre></figure><p>It&apos;s as if the <code>throw</code> primitive
<em>captured all the pending computations</em>,
<code>try</code> block, exception handler and all,
at its call site and reified them into
a callable value. When called, the
value gets replaced with those exact
computations but with the <code>throw</code>
primitive replaced with some real value.
</p><p>Let&apos;s go back to the lisp machine.
It&apos;s got a stack onto which it pushes
lots of data as it executes. The stack
is not made up of just data, however.
Machine steps are also pushed onto
the stack. The stack is full of values
which direct the machine to do things
in a specific order.
<strong>The stack is a form of code.</strong></p><p>It&apos;s this code that forms the &quot;current continuation&quot;.
It&apos;s this code that&apos;s being captured.
</p><h3>Capturing the continuation</h3><p>Another key insight in the keynote:
continuations turn out to be just
<code>memcpy</code>ing the stack
back and forth.
</p><p>The stack is just memory.
We&apos;re going to need a buffer.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_lisp_continuation</span> <span class="token punctuation">{</span>
    <span class="token class-name">size_t</span> frame_count<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack_frame</span> <span class="token operator">*</span>frames<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></figure><p>We&apos;re going to need delimiters too.
The <code>return</code> primitive makes
use of an implicit delimiter managed
by the lisp machine itself.
This general <code>control</code> primitive
will manage the delimiter all by itself.
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>lone_control<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> arguments<span class="token punctuation">,</span> body<span class="token punctuation">,</span> handler<span class="token punctuation">;</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>step<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token comment">/* Initialize then evaluate body */</span>

        <span class="token comment">/* Unpack arguments... */</span>

        <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_machine_push_continuation_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">)</span><span class="token punctuation">;</span>

        machine<span class="token operator">-&gt;</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_EXPRESSION_EVALUATION<span class="token punctuation">;</span>
        machine<span class="token operator">-&gt;</span>expression <span class="token operator">=</span> body<span class="token punctuation">;</span>

        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span> <span class="token comment">/* Body evaluated */</span>

        <span class="token function">lone_lisp_machine_pop_continuation_delimiter</span><span class="token punctuation">(</span>lone<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_lisp_machine_pop_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* Handler */</span>
        <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* Return value */</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token function">linux_exit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>This primitive pushes the handler function
and a continuation delimiter onto the stack.
Then it evaluates the body of code.
Once that&apos;s done, the primitive discards
the handler function and the continuation
delimiter and simply returns the result.
So by itself it does nothing special.
It&apos;s a glorified <code>begin</code> primitive.
</p><p>Enter the <code>transfer</code> primitive.
</p><figure><pre><code><span class="token function">LONE_LISP_PRIMITIVE</span><span class="token punctuation">(</span>lone_transfer<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_machine_stack_frame</span> <span class="token operator">*</span>delimiter<span class="token punctuation">,</span> <span class="token operator">*</span>frames<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_lisp_value</span> arguments<span class="token punctuation">,</span> value<span class="token punctuation">,</span> continuation<span class="token punctuation">,</span> handler<span class="token punctuation">;</span>
    <span class="token class-name">size_t</span> frame_count<span class="token punctuation">;</span>

    <span class="token keyword">switch</span> <span class="token punctuation">(</span>step<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token comment">/* Initialize, capture continuation, reset stack and evaluate handler */</span>

        <span class="token comment">/* Unpack arguments... */</span>

        <span class="token comment">/* Skip primitive function delimiter and one step */</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span>frame <span class="token operator">=</span> lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>stack<span class="token punctuation">.</span>top <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span>
             frame_count <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
             frame <span class="token operator">&gt;=</span> machine<span class="token operator">-&gt;</span>stack<span class="token punctuation">.</span>base <span class="token operator">&amp;&amp;</span>
             frame<span class="token operator">-&gt;</span>type <span class="token operator">!=</span> LONE_LISP_MACHINE_STACK_FRAME_TYPE_CONTINUATION_DELIMITER<span class="token punctuation">;</span>
             <span class="token operator">--</span>frame<span class="token punctuation">,</span> <span class="token operator">++</span>frame_count<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">/* Recover the handler function */</span>
        <span class="token operator">--</span>frame<span class="token punctuation">;</span> <span class="token operator">++</span>frame_count<span class="token punctuation">;</span>
        handler <span class="token operator">=</span> frame<span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>value<span class="token punctuation">;</span>

        <span class="token comment">/* Copy stack frames up to and including the control primitive&apos;s function delimiter */</span>
        <span class="token operator">--</span>frame<span class="token punctuation">;</span> <span class="token operator">++</span>frame_count<span class="token punctuation">;</span>
        frames <span class="token operator">=</span> <span class="token function">lone_memory_array</span><span class="token punctuation">(</span>lone<span class="token operator">-&gt;</span>system<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> frame_count<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token operator">*</span>frames<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">lone_memory_move</span><span class="token punctuation">(</span>frame<span class="token punctuation">,</span> frames<span class="token punctuation">,</span> frame_count <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token operator">*</span>frames<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">/* Reify current continuation */</span>
        continuation <span class="token operator">=</span> <span class="token function">lone_lisp_continuation_create</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> frame_count<span class="token punctuation">,</span> frames<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">/* Reset stack back to the control primitive&apos;s function delimiter */</span>
        lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>stack<span class="token punctuation">.</span>top <span class="token operator">=</span> frame <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>

        <span class="token comment">/* Configure machine to evaluate handler function with value and continuation */</span>
        lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>expression <span class="token operator">=</span> <span class="token function">lone_lisp_list_build</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>handler<span class="token punctuation">,</span> <span class="token operator">&amp;</span>value<span class="token punctuation">,</span> <span class="token operator">&amp;</span>continuation<span class="token punctuation">)</span><span class="token punctuation">;</span>
        lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>step <span class="token operator">=</span> LONE_LISP_MACHINE_STEP_EXPRESSION_EVALUATION<span class="token punctuation">;</span>

        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>

    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>

        <span class="token comment">/* Handler has finished evaluation, return the value returned by it */</span>
        <span class="token function">lone_lisp_machine_push_value</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">,</span> lone<span class="token operator">-&gt;</span>machine<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token function">linux_exit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>The first thing it does is find the continuation delimiter
by looping over the stack frames and looking for it.
</p><aside><p>A couple of frames are skipped because they&apos;re useless:
the function delimiter of <code>transfer</code>,
and an extraneous machine step that gets generated
whenever the machine loops back to evaluate the operand.
Only the second step matters when restoring the machine&apos;s
next step after function application.
</p></aside><p>It knows that just below the delimiter it can find the
handler function that was passed to <code>control</code>.
It will be calling that function shortly so it recovers
that function into a variable.</p><p>Then it finds the function delimiter of the <code>control</code>
primitive and copies all the stack frames between it
and the top of the stack into a heap allocated buffer.
Then it creates a lisp value that just holds this buffer.
<em>That&apos;s the continuation.</em></p><p>Then it unwinds the stack to the function delimiter,
effectively popping off the entire segment of the stack
that was just copied into the heap. At this point
the stack discipline matches the initial state of
the <code>control</code> primitive.
We&apos;ve really gone back in there!
From the lisp machine&apos;s perspective,
we&apos;re inside <code>control</code> right now,
and if we return a value it will be
as though <code>control</code> had returned it.</p><p><code>transfer</code> now has the value, the continuation
and the handler function. There is but one thing
left to do: evaluate <code>(handler value continuation)</code>.
The return value of that expression becomes the result
of <code>control</code>.</p><p>I could stop here and call this an exceptions mechanism.
If I deleted the continuation capture code, it <em>would</em>
be just like the exceptions in every other language!
</p><h3>Making continuations callable</h3><p>I&apos;m not gonna stop there though.
I&apos;m so close!</p><p>My continuation value is just a structure
that holds an array of stack frames.
This is a new value type which must be
properly handled in various parts of
the system, <em>especially</em> in the
garbage collector. Fail to mark and
sweep these things and memory leaks
will be the least of your problems.</p><p>The lisp machine must also be taught
how to handle these objects.
In particular, it must be taught
how to apply a value to it.
</p><p>Like functions, continuations evaluate to themselves.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_TYPE_CONTINUATION<span class="token operator">:</span>
    machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> machine<span class="token operator">-&gt;</span>expression<span class="token punctuation">;</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>Continuations don&apos;t fail the applicable type test.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_EVALUATED_OPERATOR<span class="token operator">:</span>
    <span class="token comment">/* ... */</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_type_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/* ... */</span>
        <span class="token keyword">case</span> LONE_LISP_TYPE_CONTINUATION<span class="token operator">:</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token comment">/* ... */</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">should_evaluate_operands</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">,</span> machine<span class="token operator">-&gt;</span>unevaluated<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment">/* ... */</span>
</code></pre></figure><p>Continuations always have their arguments evaluated.
</p><figure><pre><code><span class="token keyword">static</span> bool <span class="token function">should_evaluate_operands</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">/* ... */</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">/* ... */</span>
    <span class="token keyword">case</span> LONE_LISP_TYPE_CONTINUATION<span class="token operator">:</span>
        <span class="token keyword">return</span> true<span class="token punctuation">;</span>
</code></pre></figure><p><strong>And finally...</strong>
When a value is applied to it,
the machine spills the captured
stack frames on top of the stack
and sets the machine registers
so that the argument flows
into the computation.
</p><figure><pre><code><span class="token keyword">case</span> LONE_LISP_MACHINE_STEP_APPLICATION<span class="token operator">:</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/* ... */</span>
        <span class="token keyword">case</span> LONE_LISP_TYPE_CONTINUATION<span class="token operator">:</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">lone_lisp_list_has_rest</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>list<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">goto</span> too_many_arguments<span class="token punctuation">;</span> <span class="token punctuation">}</span>
            <span class="token function">lone_lisp_machine_push_frames</span><span class="token punctuation">(</span>
                lone<span class="token punctuation">,</span>
                <span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>continuation<span class="token punctuation">.</span>frame_count<span class="token punctuation">,</span>
                <span class="token function">lone_lisp_heap_value_of</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>applicable<span class="token punctuation">)</span><span class="token operator">-&gt;</span>as<span class="token punctuation">.</span>continuation<span class="token punctuation">.</span>frames
            <span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">lone_lisp_machine_restore_step</span><span class="token punctuation">(</span>lone<span class="token punctuation">,</span> machine<span class="token punctuation">)</span><span class="token punctuation">;</span>
            machine<span class="token operator">-&gt;</span>value <span class="token operator">=</span> <span class="token function">lone_lisp_list_first</span><span class="token punctuation">(</span>machine<span class="token operator">-&gt;</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></figure><p>The continuation is carefully captured
so as to ensure the machine&apos;s next step
is on top of the stack. The machine simply
restores that value and off it goes.
When it&apos;s done, the result flows
naturally into the caller.
</p><p>And just like that, lone has gained
native first class delimited continuations!</p>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
    <item>
      <title>Self-contained Linux applications with lone lisp</title>
      <link>https://www.matheusmoreira.com/articles/self-contained-lone-lisp-applications</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/self-contained-lone-lisp-applications</guid>
      <pubDate>Tue, 28 Nov 2023 00:00:00 GMT</pubDate>
      <description>Creating standalone Linux applications with lone.</description>
      <content:encoded><![CDATA[<h1>Self-contained Linux applications with lone lisp</h1><p>I started <a href="https://github.com/lone-lang/lone">the lone lisp project</a>
to create a lisp language and environment exclusively for Linux.
I built into it support for arbitrary
<a href="https://www.matheusmoreira.com/articles/linux-system-calls">Linux system calls</a>
so that it would be possible to implement any program
without need for any external dependencies and so that
every Linux feature would be available to programmers.</p><p>Although far from ready for production use,
I have achieved a significant milestone
in the development of the language:
it is now possible to create self-contained,
standalone, freestanding, redistributable
Linux applications written entirely in lisp.</p><p>Lisp code can now be embedded directly
into a copy of the lone interpreter
which may then be copied to and run
on any Linux system of the same architecture,
unmodified and without any external dependencies.
The application is limited only by the system calls it uses:
newer system calls will naturally require newer kernels.
</p><p>In this article I will demonstrate this capability,
explain how it works and the journey to implementing it.
Every script bundling tool I&apos;ve ever seen unpacks the code
to some file system location and then reads it back in.
I came up with a different solution.
</p><h2>A simple implementation of <code>env</code> in lone lisp</h2><p>The <code>env</code> utility is among the simplest programs
available in Unix-like operating systems. It&apos;s most
fundamental function is to print the user&apos;s environment.
That function can be trivially implemented in lone lisp:
</p><figure><pre><code><span class="token punctuation">(</span><span class="token car">import</span> <span class="token punctuation">(</span><span class="token car">lone</span> print<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token car">linux</span> environment<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">(</span><span class="token car">print</span> environment<span class="token punctuation">)</span>
</code></pre></figure><p>Running this simple program produces a table
of environment variables and their values:
</p><figure><pre><code>&#x24; ./lone <span class="token operator">&lt;</span> env.ln
<span class="token punctuation">{</span> <span class="token string">&quot;HOME&quot;</span> <span class="token string">&quot;/home/matheus&quot;</span> <span class="token string">&quot;EDITOR&quot;</span> <span class="token string">&quot;vim&quot;</span> &#x2026; <span class="token punctuation">}</span>
</code></pre></figure><h3>Embedding <code>env.ln</code> into the interpreter</h3><p>In order to transform <code>env.ln</code>
into a self-contained <code>env</code> program,
the lone lisp code must be embedded into
a copy of the interpreter. This can be achieved
by the purpose-built <code>lone-embed</code> tool:
</p><figure><pre><code>&#x24; <span class="token function">cp</span> lone <span class="token function">env</span>
&#x24; lone-embed <span class="token function">env</span> env.ln
</code></pre></figure><p>The interpreter will then seamlessly load and execute the code:
</p><figure><pre><code>&#x24; ./env
<span class="token punctuation">{</span> <span class="token string">&quot;HOME&quot;</span> <span class="token string">&quot;/home/matheus&quot;</span> <span class="token string">&quot;EDITOR&quot;</span> <span class="token string">&quot;vim&quot;</span> &#x2026; <span class="token punctuation">}</span>
</code></pre></figure><h2>Elven segments</h2><p>Tracing the system calls of the application with <code>strace</code>
reveals an interesting property:
</p><figure><pre><code>&#x24; <span class="token function">strace</span> <span class="token function">env</span>
execve<span class="token punctuation">(</span><span class="token string">&quot;env&quot;</span>, <span class="token punctuation">[</span><span class="token string">&quot;env&quot;</span><span class="token punctuation">]</span>, 0x7fe9752d40 /* <span class="token number">31</span> vars */<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">0</span>
write<span class="token punctuation">(</span><span class="token number">1</span>, <span class="token string">&quot;{ &quot;</span>, <span class="token number">2</span><span class="token punctuation">)</span>                                  <span class="token operator">=</span> <span class="token number">2</span>
write<span class="token punctuation">(</span><span class="token number">1</span>, <span class="token string">&quot;<span class="token entity" title="\&quot;">\&quot;</span>&quot;</span>, <span class="token number">1</span><span class="token punctuation">)</span>                                  <span class="token operator">=</span> <span class="token number">1</span>
write<span class="token punctuation">(</span><span class="token number">1</span>, <span class="token string">&quot;HOME&quot;</span>, <span class="token number">4</span><span class="token punctuation">)</span>                                <span class="token operator">=</span> <span class="token number">4</span>
write<span class="token punctuation">(</span><span class="token number">1</span>, <span class="token string">&quot;<span class="token entity" title="\&quot;">\&quot;</span>&quot;</span>, <span class="token number">1</span><span class="token punctuation">)</span>                                  <span class="token operator">=</span> <span class="token number">1</span>
write<span class="token punctuation">(</span><span class="token number">1</span>, <span class="token string">&quot; &quot;</span>, <span class="token number">1</span><span class="token punctuation">)</span>                                   <span class="token operator">=</span> <span class="token number">1</span>
&#x2026;
</code></pre></figure><p>It begins writing its output immediately.
There was no need for it to read from the file system.
The code must be somewhere else.
</p><figure><pre><code>&#x24; xxd <span class="token function">env</span> <span class="token operator">|</span> <span class="token function">tail</span> <span class="token parameter variable">-n7</span>
00032fe0: 0000 0000 0000 0000 0000 0000 0000 0000  <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>
00032ff0: 0000 0000 0000 0000 0000 0000 0000 0000  <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>
00033000: 7b20 <span class="token number">7275</span> 6e20 <span class="token number">2830</span> 202e <span class="token number">2036</span> <span class="token number">3329</span> 207d  <span class="token punctuation">{</span> run <span class="token punctuation">(</span><span class="token number">0</span> <span class="token builtin class-name">.</span> <span class="token number">63</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
00033010: 0a28 696d 706f <span class="token number">7274</span> <span class="token number">2028</span> 6c6f 6e65 <span class="token number">2070</span>  .<span class="token punctuation">(</span>import <span class="token punctuation">(</span>lone p
00033020: <span class="token number">7269</span> 6e74 <span class="token number">2920</span> 286c 696e <span class="token number">7578</span> <span class="token number">2065</span> 6e76  rint<span class="token punctuation">)</span> <span class="token punctuation">(</span>linux <span class="token function">env</span>
00033030: <span class="token number">6972</span> 6f6e 6d65 6e74 <span class="token number">2929</span> 0a0a <span class="token number">2870</span> <span class="token number">7269</span>  ironment<span class="token punctuation">))</span><span class="token punctuation">..</span><span class="token punctuation">(</span>pri
00033040: 6e74 <span class="token number">2065</span> 6e76 <span class="token number">6972</span> 6f6e 6d65 6e74 290a  nt environment<span class="token punctuation">)</span>.
</code></pre></figure><p>It&apos;s at the very end of the executable itself, at an aligned offset.
Through some elven magic the interpreter is reflecting upon its own
contents at runtime. It&apos;s loading the code from inside itself and
evaluating it. Without using any system calls to do it.</p><p>The source of this magic is the ELF segment.
</p><figure><pre><code>&#x24; readelf <span class="token parameter variable">--segments</span> <span class="token function">env</span> <span class="token operator">|</span> <span class="token function">grep</span> <span class="token number">33000</span>
LOAD           0x0000000000033000 0x0000000000312000 0x0000000000312000
LOOS+0xc6f6e65 0x0000000000033000 0x0000000000312000 0x0000000000312000
</code></pre></figure><p>ELF segments, also called program headers,
describe the memory image of the program.
There are many types of segments but especially
interesting are the <code>LOAD</code> segments which
tell Linux which parts of the file must be
mapped to which addresses in order for
the program to work correctly.</p><p>The <code>lone-embed</code> tool copies
the lisp code into the ELF and then
creates a <code>LOAD</code> segment for it.
Linux then maps in the embedded code
automatically at load time before the
interpreter has even started.
</p><h3>Finding the segment</h3><p>The code will be in memory but its location and size are unknown.
It could be anywhere inside the vastness of virtual address space.
To that problem Linux provides an ingenious solution: it tells us
where it is.
</p><p>In addition to argument and environment vectors,
processes receive the auxiliary vector.
It&apos;s essentially a null terminated array
of key-value pairs of various types
and it&apos;s placed right there on the stack
just after the environment vector.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_value</span> <span class="token punctuation">{</span>
    <span class="token keyword">union</span> <span class="token punctuation">{</span>
        <span class="token keyword">char</span> <span class="token operator">*</span>c_string<span class="token punctuation">;</span>
        <span class="token keyword">void</span> <span class="token operator">*</span>pointer<span class="token punctuation">;</span>
        <span class="token keyword">long</span> integer<span class="token punctuation">;</span>
        <span class="token keyword">unsigned</span> <span class="token keyword">long</span> unsigned_integer<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> as<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_vector</span> <span class="token punctuation">{</span>
    <span class="token keyword">long</span> type<span class="token punctuation">;</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_value</span> value<span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token operator">*</span>auxv<span class="token punctuation">;</span>

<span class="token keyword">void</span> <span class="token operator">*</span><span class="token operator">*</span>pointer <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token operator">*</span><span class="token punctuation">)</span> envp<span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">*</span>pointer<span class="token operator">++</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
auxv <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">auxiliary_vector</span> <span class="token operator">*</span><span class="token punctuation">)</span> pointer<span class="token punctuation">;</span>
</code></pre></figure><p>Through this mechanism, Linux passes various
bits of useful information to programs.
These include things like processor architecture
and capabilities, current user and group IDs,
some random bytes, the location of the
<a href="https://www.man7.org/linux/man-pages/man7/vdso.7.html">vDSO</a>,
the system&apos;s page size...
</p><p>And the location of the program header table.
</p><figure><pre><code><span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_value</span> <span class="token function">lone_auxiliary_vector_value</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_vector</span> <span class="token operator">*</span>auxiliary<span class="token punctuation">,</span> <span class="token keyword">long</span> type<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token comment">/* auxiliary */</span><span class="token punctuation">;</span> auxiliary<span class="token operator">-&gt;</span>type <span class="token operator">!=</span> AT_NULL<span class="token punctuation">;</span> <span class="token operator">++</span>auxiliary<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>auxiliary<span class="token operator">-&gt;</span>type <span class="token operator">==</span> type<span class="token punctuation">)</span>
            <span class="token keyword">return</span> auxiliary<span class="token operator">-&gt;</span>value<span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span>as<span class="token punctuation">.</span>integer <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">struct</span> <span class="token class-name">lone_elf_segments</span> <span class="token function">lone_auxiliary_vector_elf_segments</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_vector</span> <span class="token operator">*</span>auxv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_elf_segments</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token punctuation">.</span>entry_size  <span class="token operator">=</span> <span class="token function">lone_auxiliary_vector_value</span><span class="token punctuation">(</span>auxv<span class="token punctuation">,</span> AT_PHENT<span class="token punctuation">)</span><span class="token punctuation">.</span>as<span class="token punctuation">.</span>unsigned_integer<span class="token punctuation">,</span>
        <span class="token punctuation">.</span>entry_count <span class="token operator">=</span> <span class="token function">lone_auxiliary_vector_value</span><span class="token punctuation">(</span>auxv<span class="token punctuation">,</span> AT_PHNUM<span class="token punctuation">)</span><span class="token punctuation">.</span>as<span class="token punctuation">.</span>unsigned_integer<span class="token punctuation">,</span>
        <span class="token punctuation">.</span>segments    <span class="token operator">=</span> <span class="token function">lone_auxiliary_vector_value</span><span class="token punctuation">(</span>auxv<span class="token punctuation">,</span> AT_PHDR<span class="token punctuation">)</span><span class="token punctuation">.</span>as<span class="token punctuation">.</span>pointer
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>The table is just a contiguous array of
<a href="https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#Program_header">ELF program header structures</a>.
Given this pointer, all the program has to do
is scan the table and find the correct entry.
One does not simply search for <code>LOAD</code> entries, however.
Attempting to do it uncovers a couple of problems.</p><p>The first problem is the fact there are lots of these loadable
segments and they&apos;re all indistinguishable from one another.
ELF sections have unique names for identification,
program headers have nothing.</p><p>The second problem is their alignment requirements.
The addresses and sizes are usually aligned to page boundaries.
This obfuscates the true size of the data they contain.
</p><h4>The lone segment</h4><p>In order to solve this, I created my own custom segment type:
the <code>LONE</code> segment.</p><p>ELF provides an incredibly generous numeric range
for operating system-specific segments.
All the values between <code>LOOS</code> and <code>HIOS</code>
are free for operating systems to allocate.
Those definitions represent a range of integers
between <code>0x60000000</code> and <code>0x6FFFFFFF</code> inclusive.
That&apos;s 268,435,455 magic numbers.</p><p>So I just picked one. That&apos;s what the <code>LOOS+0xc6f6e65</code> means.
Spelled out <code>lone</code> in ASCII and it just worked itself out.
</p><figure><pre><code><span class="token comment">//      PT_LONE   l o n e</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">PT_LONE</span> <span class="token expression"><span class="token number">0x6c6f6e65</span></span></span>
</code></pre></figure><p>I figured if GNU can do it then I can do it too.
</p><p>The <code>LONE</code> segment is not loadable
and thus does not have any alignment requirements,
allowing it to describe the embedded segment exactly.
It also serves as a magic number which makes it trivial
to search for it in the program header table.
Once found, it contains all the information
lone needs to load and execute the code.
</p><figure><pre><code><span class="token keyword">typedef</span> Elf64_Phdr lone_elf_segment<span class="token punctuation">;</span> <span class="token comment">// if 64 bit</span>
<span class="token keyword">typedef</span> Elf32_Phdr lone_elf_segment<span class="token punctuation">;</span> <span class="token comment">// if 32 bit</span>

lone_elf_segment <span class="token operator">*</span><span class="token function">lone_auxiliary_vector_embedded_segment</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">lone_auxiliary_vector</span> <span class="token operator">*</span>auxv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">lone_elf_segments</span> table <span class="token operator">=</span> <span class="token function">lone_auxiliary_vector_elf_segments</span><span class="token punctuation">(</span>auxv<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">size_t</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> table<span class="token punctuation">.</span>entry_count<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        lone_elf_segment <span class="token operator">*</span>entry <span class="token operator">=</span> <span class="token operator">&amp;</span>table<span class="token punctuation">.</span>segments<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>entry<span class="token operator">-&gt;</span>p_type <span class="token operator">==</span> PT_LONE<span class="token punctuation">)</span>
            <span class="token keyword">return</span> entry<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

lone_elf_segment <span class="token operator">*</span>segment <span class="token operator">=</span> <span class="token function">lone_auxiliary_vector_embedded_segment</span><span class="token punctuation">(</span>auxv<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">lone_bytes</span> data<span class="token punctuation">;</span>
data<span class="token punctuation">.</span>count <span class="token operator">=</span> segment<span class="token operator">-&gt;</span>p_memsz<span class="token punctuation">;</span>
data<span class="token punctuation">.</span>pointer <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">unsigned</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> segment<span class="token operator">-&gt;</span>p_vaddr<span class="token punctuation">;</span>
</code></pre></figure><p>The interpreter now has the address and size
of the data embedded into its own executable.
At this point it&apos;s smooth sailing.
</p><p>All that&apos;s left to do is to make sense of the bytes.
I chose to simply put a descriptor object in there
and have the interpreter read it in.
Seemed like the simplest possible solution.
</p><figure><pre><code>{ run <span class="token punctuation">(</span><span class="token number">0</span> <span class="token punctuation">.</span> <span class="token number">63</span><span class="token punctuation">)</span> }
</code></pre></figure><p>Just a good old hash table in lone lisp syntax.
The run key contains the offset and size
of the code the interpreter should run,
relative to the end of the descriptor object.
It just reads in that slice and evaluates it.</p><p>Since it&apos;s just a normal hash table,
it&apos;s also infinitely extensible with
arbitrary schemas. I plan to implement
a modules key to contain an arbitrary
number of lone modules so that programs
can statically link their libraries
into the lone interpreter. All I gotta do
is place the embedded segment into the
module search path, before all the others.
I suppose I could also allow configuring
the interpreter via the descriptor object,
eliminating the need for command line switches.
</p><h2>Special linker features</h2><p>Remember the <code>lone-embed</code> tool which I briefly mentioned
at the beginning of the article? It&apos;s an ELF patching tool
which I built specifically for this purpose and it&apos;s doing
<em>a lot of</em> heavy lifting here.</p><p>When programs are compiled and linked,
the program headers are set in stone.
Yet to do all of this I needed to
append new segments to the program
header table. This turned out to be
<em>much</em> harder than anticipated.
</p><p>The program header table usually follows
the ELF header and precedes the contents
of the file. Appending new items to it
would require resizing the table,
which would require shifting up the addresses
of <em>everything</em> that comes after it,
invalidating pointers to the old addresses.
As far as I can tell, it just can&apos;t be done
without reinventing the linker itself.</p><p>I tried to move the table to the end of the file
instead but couldn&apos;t get that to work either.
My program was somehow segfaulting before
it even reached the entry point, gdb was useless,
I couldn&apos;t understand what was going on
and was reduced to desperately dumping
<code>readelf</code> output on stackoverflow
in hopes someone would spot the problem.
Well <em>someone</em> did and quickly at that
but clearly this was not a sustainable
software development strategy.
</p><h3>The <code>mold</code> linker saves the day</h3><p>The linker is in a privileged position.
It knows everything there is to know
about the program&apos;s memory layout
and can easily add new ELF segments to it
seemingly at will. If a solution exists,
I was convinced it would be found in the linker.</p><p>So I started learning
<a href="https://sourceware.org/binutils/docs/ld/Scripts.html">linker script</a>.
Turns out it has a <code>PHDRS</code> command
which is exactly what I needed but I couldn&apos;t
figure out how to use it. I just kept getting
&quot;unable to allocate headers&quot; errors no matter
what I tried. I concluded it would be easier
to simply <em>ask</em> for this feature instead.</p><p>So I
<a href="https://sourceware.org/pipermail/binutils/2023-November/130640.html">emailed</a>
the GNU binutils mailing list...
Then I created an
<a href="https://github.com/llvm/llvm-project/issues/72386">LLVM issue</a>...
</p><p>Then I opened a <code>mold</code> issue.
The maintainer immediately understood
what I wanted to do and
<a href="https://github.com/rui314/mold/commit/eb6c213f2a9aa8a101b2b52a791be369d165e6a9">made it happen</a>
with what was essentially a single line of code change.
Just <em>beyond awesome</em>.</p><p>I waited eagerly for the new release
but got so excited I built this huge
C++ project from source <em>on my smartphone</em>
just to integrate lone with it.
All I had to do was put
<code>-Wl,--spare-program-headers,2</code>
in the <code>LDFLAGS</code>.
It gave me two <code>NULL</code> program headers
for my patching utility to edit any way it wanted.
It worked <em>perfectly</em>.</p><p>So far <code>mold</code> is the only linker
with this feature and it&apos;s absolutely
required for <code>lone-embed</code> to work.
It will outright refuse to patch the ELF
if it doesn&apos;t find at least two <code>NULL</code>
program headers in it.
</p><p>Would be nice if the others gained this feature too.
Unless I can figure out a way to move
the program header table to the end of the file
without breaking everything, I&apos;m pretty much
locked into using <code>mold</code>.
Well, I don&apos;t really mind.
It&apos;s an awesome linker
<em>and</em> it&apos;s free software.
I&apos;m OK with this!</p>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
    <item>
      <title>Dotfiles with make</title>
      <link>https://www.matheusmoreira.com/articles/managing-dotfiles-with-make</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/managing-dotfiles-with-make</guid>
      <pubDate>Sun, 03 Sep 2023 00:00:00 GMT</pubDate>
      <description>Using make as a configuration file management tool.</description>
      <content:encoded><![CDATA[<h1>Managing dotfiles with Make</h1><p>Make is an old tool, an assembly language of sorts for build systems.
Many have tried to replace it. Many have tried to reinvent it.
Most people prefer to avoid it if at all possible.
So why use it to manage dotfiles of all things?</p><p>There&apos;s at least one good reason to do this: make is ubiquitous.
Pretty much every machine that has ever compiled software will
have a copy of this thing. Using make as a dotfile management tool
eliminates the need to install yet another infrequently used program.</p><p>Another reason to use it is this turned out to be a surprisingly easy task.
</p><h2>File system structure</h2><p>Make works best when everything is as simple as possible.
It doesn&apos;t provide much functionality:
the few path manipulation functions it includes
are of the string matching and substitution variety.</p><p>The easiest way to achieve that simplicity
is to mirror the structure of the home directory.
Like this:
</p><ul class="tree"><li><code>~/.files</code><ul><li><code>~</code><ul><li><code>.bash_profile</code></li><li><code>.bashrc</code></li><li><code>.vimrc</code></li><li><code>...</code></li></ul></li><li><code>GNUmakefile</code></li></ul></li></ul><p>The <code>~</code> directory represents the current user&apos;s home directory.
Configuration files in <code>&#x24;HOME</code> will be symbolic links to their
corresponding files in the <code>~</code> directory of the repository.
Make&apos;s job is to automatically create those symbolic links.
</p><figure><pre><code><span class="token builtin class-name">cd</span> ~/.files/
<span class="token function">make</span>
</code></pre></figure><p>Simplicity is good.</p><h2>Writing the makefile</h2><p>All link targets are rooted in the repository&apos;s <code>~</code> directory,
so the first thing that must be done is find the repository itself.</p><p>Make always knows the location of the makefile.
Since it is located in the root of the <code>.files</code> repository,
it&apos;s possible to find the repository itself through it.
</p><figure><pre><code>makefile <span class="token operator">:=</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">abspath</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">lastword</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span>MAKEFILE_LIST<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
dotfiles <span class="token operator">:=</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">abspath</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">dir</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span>makefile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></figure><p>A reference to the home directory
is already available in make
via the <code>&#x24;HOME</code> environment variable
and <code>~</code> also works according to
<a href="https://www.gnu.org/software/make/manual/html_node/Wildcards.html#index-_007e-_0028tilde_0029">the documentation</a>.
An equally easy way to refer to the dotfiles repository&apos;s
<code>~</code> directory would be nice.
</p><figure><pre><code>~ <span class="token operator">:=</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">abspath</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span>dotfiles<span class="token punctuation">)</span>/~<span class="token punctuation">)</span>
</code></pre></figure><p>Now it is possible to write <code>~</code> and <code>&#x24;(~)</code>
for the user&apos;s home directory and for the repository&apos;s
<code>~</code> directory respectively.
</p><h3>The symbolic linking rule</h3><p>Combining these variables and the fact the repository structure
mirrors the structure of the home directory, it becomes trivial
to write the rule:
</p><figure><pre><code><span class="token target symbol">force</span><span class="token punctuation">:</span>

<span class="token target symbol">~/%</span> <span class="token punctuation">:</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span>~<span class="token punctuation">)</span>/% force
	mkdir -p <span class="token variable">&#x24;(@D)</span>
	ln -snf <span class="token variable">&#x24;&lt;</span> <span class="token variable">&#x24;@</span>
</code></pre></figure><p><a href="https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html">Automatic variables</a>
are used in the recipe for maximum brevity.
<code>&#x24;@</code> and <code>&#x24;&lt;</code> refer to the link name and link target.
<code>&#x24;(@D)</code> refers to the directory of the new link,
ensuring the whole tree exists before attempting to create it.
</p><h3>Generalizing and metaprogramming</h3><p>GNU Make is surprisingly lisp-like in its metaprogrammability.
It&apos;s possible to generate and evaluate code at runtime.
So why not generalize the previous rule into a function
that defines symbolic linking rules for any pair of directories
with matching structure?
</p><figure><pre><code><span class="token target symbol">force</span><span class="token punctuation">:</span>

<span class="token keyword">define</span> rule.template
<span class="token target symbol"><span class="token variable">&#x24;</span>(1)/%</span> <span class="token punctuation">:</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span>2<span class="token punctuation">)</span>/% force
	mkdir -p <span class="token variable">&#x24;&#x24;(@D)</span>
	ln -snf <span class="token variable">&#x24;&#x24;&lt;</span> <span class="token variable">&#x24;&#x24;@</span>
<span class="token keyword">endef</span>

rule.<span class="token keyword">define</span> <span class="token operator">=</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">eval</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">call</span> rule.template,<span class="token variable">&#x24;</span><span class="token punctuation">(</span>1<span class="token punctuation">)</span>,<span class="token variable">&#x24;</span><span class="token punctuation">(</span>2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></figure><p>The <code>rule.define</code> function will generate
and evaluate the original rule definition.
It&apos;s really easy to use:
</p><figure><pre><code><span class="token variable">&#x24;</span><span class="token punctuation">(</span><span class="token function">call</span> rule.<span class="token keyword">define</span>,~,<span class="token variable">&#x24;</span><span class="token punctuation">(</span>~<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></figure><p>Nice.</p><h3>Phony targets for usability</h3><p>By this point, the makefile already works
with any dotfile inside the repository.
</p><figure><pre><code><span class="token function">make</span> ~/.bash_profile ~/.bashrc
</code></pre></figure><p>Typing out all the file names is annoying though.
Phony targets can be used to group them:
</p><figure><pre><code>all <span class="token operator">+=</span> bash
<span class="token target symbol">bash</span> <span class="token punctuation">:</span> ~/.bash_profile ~/.bashrc

<span class="token target symbol">all</span> <span class="token punctuation">:</span> <span class="token variable">&#x24;</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span>
<span class="token builtin-target builtin">.PHONY</span><span class="token punctuation">:</span> all <span class="token variable">&#x24;</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span>
.DEFAULT_GOAL <span class="token operator">:=</span> all
</code></pre></figure><p>This sets up a phony target for <code>bash</code>,
maintains a list of all phony targets
and ensures they are declared as such,
creates an <code>all</code> phony target
that links everything and sets it
as the default goal.</p><p>Now adding phony targets is easy:
</p><figure><pre><code>all <span class="token operator">+=</span> git
<span class="token target symbol">git</span> <span class="token punctuation">:</span> ~/.gitconfig</code></pre></figure>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
    <item>
      <title>Linux system calls</title>
      <link>https://www.matheusmoreira.com/articles/linux-system-calls</link>
      <guid isPermaLink="true">https://www.matheusmoreira.com/articles/linux-system-calls</guid>
      <pubDate>Tue, 07 Feb 2023 00:00:00 GMT</pubDate>
      <description>Everything I know about Linux system calls.</description>
      <content:encoded><![CDATA[<h1>Linux system calls</h1><p>For a long time I felt there was something unique about Linux.
Something was different, couldn&apos;t quite figure out what it was.
Only when I learned about Linux&apos;s system call interface
did I finally understand.</p><p>This article contains everything I&apos;ve learned.
</p><h2>Why Linux system calls?</h2><p>Programs usually interface with the kernel through libraries
provided by the operating system, most commonly <code>libc</code>.
Through these libraries, they have access to system functions.
<abbr>POSIX</abbr>-compliant operating systems provide
<code>read</code>, <code>write</code> and numerous others.
Windows has the Win32 API and its many DLLs and functions.
</p><p>These operating systems consist of strongly connected kernel
and user space components, developed and distributed as one
unit. The user space libraries are the only supported means
of using the system. User space programs are not meant to
talk to the kernel directly, they&apos;re meant to use the provided
system libraries. This forces them to depend on and link
against such libraries.
</p><p>While it is often possible to interface with the kernel
directly, the kernel interface is unstable and subject to
change. Software that insists on using that interface
might simply stop working if &#x2014; or rather <em>when</em> &#x2014; it changes.
</p><figure><blockquote cite="https://news.ycombinator.com/item?id=28413001">Then there&apos;s the whole &quot;reinventing libc&quot; insanity,
even on macOS (where no such ABI stability was guaranteed,
but they did it anyway, and that ended up with a macOS
update breaking all Go apps). On Windows they can&apos;t get away
with that, so they use Cgo instead</blockquote><figcaption><cite><a href="https://news.ycombinator.com/item?id=28413001">marcan_42, Hacker News, <time datetime="2021-09-04">Sept 4, 2021</time>
</a></cite></figcaption></figure><figure><blockquote cite="https://github.com/golang/go/issues/17490">As I understand it, Go currently has its own syscall wrappers
for Darwin. This is explicitly against what Apple recommends,
precisely because they&apos;re not willing to commit to a particular
syscall ABI. This leads to issues like
<a href="https://github.com/golang/go/issues/16570">#16570</a>,
and although we&apos;ve been lucky in that things have generally been
backward-compatible so far, there&apos;s no guarantee that it&apos;ll
continue to happen. It doesn&apos;t seem inconceivable to me
that we&apos;d at some point end up having to specify
&quot;build for macOS 10.13+&quot; vs. &quot;build for 10.12 and below&quot;,
for example.</blockquote><figcaption><cite><a href="https://github.com/golang/go/issues/17490">copumpkin, golang/go GitHub issue #17490,
<time datetime="2016-10-17">Oct 17, 2016</time>
</a></cite></figcaption></figure><p>Sometimes it&apos;s not even possible to use system calls at all.
OpenBSD has implemented
<a href="https://lwn.net/Articles/806776/">system call origin verification</a>,
a security mechanism that only allows system calls originating
from the system&apos;s <code>libc</code>. So not only is the kernel ABI unstable,
normal programs are not even allowed to interface with the kernel at all.
</p><figure><blockquote cite="https://lwn.net/Articles/806776/">The eventual goal would be to disallow system calls
from anywhere but the region mapped for libc,
but the Go language port for OpenBSD currently makes
system calls directly.</blockquote><blockquote cite="https://lwn.net/Articles/806776/">Switching Go to use the libc wrappers (as is already done
on Solaris and macOS) is something that De Raadt would like
to see. It may allow dropping the main program text segment
from the list of valid regions in the future:</blockquote><blockquote cite="https://lwn.net/Articles/806776/">It is an ABI break for the operating system,
but that is no big deal for OpenBSD.
De Raadt <a href="https://lwn.net/Articles/806869/">said</a>:
<q cite="https://lwn.net/Articles/806869/">we here at OpenBSD are the kings of ABI-instability</q>.
He <a href="https://lwn.net/Articles/806870/">suggested</a> that
relying on the OpenBSD ABI is fraught with peril:
<blockquote cite="https://lwn.net/Articles/806870/">Program to the API rather than the ABI. When we see benefits, we change the ABI more often than the API. I have altered the ABI. Pray I do not alter it further.</blockquote></blockquote><figcaption><cite><a href="https://lwn.net/Articles/806776/">Jake Edge, OpenBSD system-call-origin verification, LWN,
<time datetime="2019-12-11">December 11, 2019</time>
</a></cite></figcaption></figure><h3>Linux is different</h3><p>One of the things that make the Linux kernel interesting
is the fact it has a stable kernel-userspace interface.
Unlike virtually every other kernel and operating system,
Linux guarantees stability at the binary interface level.
</p><figure><blockquote cite="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/ABI/stable/syscalls">This interface matches much of the POSIX interface
and is based on it and other Unix based interfaces.
It will only be added to over time, and not have things
removed from it.</blockquote><figcaption><cite><a href="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/ABI/stable/syscalls">torvalds/linux, Documentation/ABI/stable/syscalls,
<time datetime="2006-06-21">2006-06-21</time>
</a></cite></figcaption></figure><p>This is rooted in the fact Linux is a <em>kernel</em>,
not a complete operating system as traditionally defined.
As an independent component, it <em>must</em> have a stable
interface to user space software
if anything is to be built upon it.
</p><p>While many people argue that Linux is not an operating system,
there&apos;s no question that Linux is a <em>platform</em> and that
it is possible to safely build directly upon it.
There is no actual <em>need</em> to depend on anything else.
Not even <code>libc</code>.
</p><h2>How Linux system calls work</h2><p>Processor instruction set architectures contain special
instructions for calling the kernel. These instructions
cause the processor to switch to kernel mode and execute
code at a predefined location in the kernel.</p><p>At least one parameter must be provided: the system call number,
often referred to as the <code>NR</code>. Linux uses this number as an
index into a table of function pointers to find the function being
called. Any other arguments are passed to this function.</p><p>These parameters are passed to the kernel in registers.
The kernel also returns a result value in a register.
Which registers are used for which parameters
and which register contains the return value
defines the <strong>Linux system call calling convention</strong>.</p><p>This calling convention is stable, allowing user space programs to
use it without fear of breakage. It is defined at the instruction set
level and so it is also programming language agnostic.
All user space programs written in any language may make use of it.
Typically, programs call <code>libc</code> functions which implement this
calling convention. However, that is not actually a requirement.
It&apos;s perfectly possible for a compiler to directly emit code
following that calling convention: it could have support for a
<code>system_call</code> keyword. A JIT compiler could generate code for this
at runtime.
</p><p>The journalists at the LWN have written detailed articles about the
implementation of Linux system calls. They are definitely worth reading.
</p><ol><li><a href="https://lwn.net/Articles/604287/">Anatomy of a system call, part 1</a></li><li><a href="https://lwn.net/Articles/604515/">Anatomy of a system call, part 2</a></li><li><a href="https://lwn.net/Articles/604406/">Anatomy of a system call, additional content</a></li></ol><p>The calling convention is documented here:</p><ul><li><a href="https://man7.org/linux/man-pages/man2/syscall.2.html"><code>syscall.2</code></a></li><li><a href="https://man7.org/linux/man-pages/man2/syscalls.2.html"><code>syscalls.2</code></a></li></ul><h2>Implementing a system call function</h2><p>In order to make a system call, the parameters must be placed in the
appropriate registers, the system call instruction must be executed
and the return value must be collected from the return register.
System calls support a maximum of six arguments.</p><p>Since the registers and system call instruction vary by architecture,
separate functions are needed for each architecture. Despite this,
it is simple to write a C function that can make any system call.
</p><p>For example, a system call function for the <code>x86_64</code> architecture:
</p><figure><pre><code><span class="token keyword">long</span>
<span class="token function">linux_system_call_x86_64</span><span class="token punctuation">(</span><span class="token keyword">long</span> number<span class="token punctuation">,</span>
                         <span class="token keyword">long</span> _1<span class="token punctuation">,</span> <span class="token keyword">long</span> _2<span class="token punctuation">,</span> <span class="token keyword">long</span> _3<span class="token punctuation">,</span>
                         <span class="token keyword">long</span> _4<span class="token punctuation">,</span> <span class="token keyword">long</span> _5<span class="token punctuation">,</span> <span class="token keyword">long</span> _6<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> rax <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;rax&quot;</span><span class="token punctuation">)</span> <span class="token operator">=</span> number<span class="token punctuation">;</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> rdi <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;rdi&quot;</span><span class="token punctuation">)</span> <span class="token operator">=</span> _1<span class="token punctuation">;</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> rsi <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;rsi&quot;</span><span class="token punctuation">)</span> <span class="token operator">=</span> _2<span class="token punctuation">;</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> rdx <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;rdx&quot;</span><span class="token punctuation">)</span> <span class="token operator">=</span> _3<span class="token punctuation">;</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> r10 <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;r10&quot;</span><span class="token punctuation">)</span> <span class="token operator">=</span> _4<span class="token punctuation">;</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> r8  <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;r8&quot;</span><span class="token punctuation">)</span>  <span class="token operator">=</span> _5<span class="token punctuation">;</span>
	<span class="token keyword">register</span> <span class="token keyword">long</span> r9  <span class="token function">__asm__</span><span class="token punctuation">(</span><span class="token string">&quot;r9&quot;</span><span class="token punctuation">)</span>  <span class="token operator">=</span> _6<span class="token punctuation">;</span>

	__asm__ <span class="token keyword">volatile</span>
	<span class="token punctuation">(</span><span class="token string">&quot;syscall&quot;</span>

		<span class="token operator">:</span> <span class="token string">&quot;+r&quot;</span> <span class="token punctuation">(</span>rax<span class="token punctuation">)</span><span class="token punctuation">,</span>
		  <span class="token string">&quot;+r&quot;</span> <span class="token punctuation">(</span>r8<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;+r&quot;</span> <span class="token punctuation">(</span>r9<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;+r&quot;</span> <span class="token punctuation">(</span>r10<span class="token punctuation">)</span>
		<span class="token operator">:</span> <span class="token string">&quot;r&quot;</span> <span class="token punctuation">(</span>rdi<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;r&quot;</span> <span class="token punctuation">(</span>rsi<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;r&quot;</span> <span class="token punctuation">(</span>rdx<span class="token punctuation">)</span>
		<span class="token operator">:</span> <span class="token string">&quot;rcx&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;r11&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;cc&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;memory&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token keyword">return</span> rax<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></figure><p>All parameters and the return value are of type <code>long</code>.
This really just means &quot;register&quot;: all values passed to the kernel
must fit in registers and typically <code>long</code> is register sized.
This means all arguments must either be simple values or pointers
to more complex structures.
</p><p>The function ensures all arguments are placed in the appropriate
registers by assigning them to local variables annotated with the
<code>register</code> keyword as well as an inline assembly directive
which tells the compiler which register to choose.
</p><p>The <code>x86_64</code> architecture contains the aptly named <code>syscall</code>
instruction which switches to kernel mode and enters the kernel entry point.
Other architectures have different instructions. For example,
<code>aarch64</code> uses <code>svc #0</code> instead.
</p><p>The compiler is informed via the extended inline assembly construct
that this instruction has 7 inputs, 1 output and that it clobbers
some registers, the carry bit and memory.
The 7 inputs are all the previously assigned system call number
and parameter registers.
The output is the return value which is placed in <code>rax</code>,
overwriting the system call number.</p><aside><p>Some of the clobbered registers are in the outputs list instead
because they are also inputs and the compiler documentation says
clobbered registers are not used as inputs.
The result is a much more terse notation.
</p></aside><p>After the system call has been made, all that&apos;s left to do is to return the result.
It may be a valid value or a negated <code>errno</code> constant.
The various <code>libc</code>s normalize those error values and place them
in a global or thread local <code>errno</code> variable.
That&apos;s not necessary when using Linux system calls directly!
</p>]]></content:encoded>
      <dc:creator>Matheus Afonso Martins Moreira</dc:creator>
    </item>
  </channel>
</rss>
