<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shaw</title>
    <description>The latest articles on Forem by Shaw (@shawsumma).</description>
    <link>https://forem.com/shawsumma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F748218%2F030f238f-9550-4302-acf5-34ce4069626f.png</url>
      <title>Forem: Shaw</title>
      <link>https://forem.com/shawsumma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shawsumma"/>
    <language>en</language>
    <item>
      <title>SIMD Opcodes Use LEB128</title>
      <dc:creator>Shaw</dc:creator>
      <pubDate>Thu, 01 Jun 2023 09:00:49 +0000</pubDate>
      <link>https://forem.com/shawsumma/simd-opcodes-use-leb128-3lf7</link>
      <guid>https://forem.com/shawsumma/simd-opcodes-use-leb128-3lf7</guid>
      <description>&lt;h2&gt;
  
  
  Most WebAssembly opcodes use a single byte.
&lt;/h2&gt;

&lt;p&gt;i32.add = 0x6A&lt;br&gt;
local.tee = 0x22&lt;/p&gt;

&lt;h2&gt;
  
  
  Some opcodes use two bytes.
&lt;/h2&gt;

&lt;p&gt;memory.copy = 0xFC + 0x0A&lt;br&gt;
i32.atomic.store = 0xFE + 0x17&lt;/p&gt;

&lt;h2&gt;
  
  
  SIMD opcodes use &lt;a href="https://en.wikipedia.org/wiki/LEB128"&gt;Leb128&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pengowray.github.io/wasm-ops/"&gt;This table&lt;/a&gt; seems to claim the following&lt;br&gt;
v128.load = 0xFD + 0x00&lt;/p&gt;

&lt;p&gt;But it actually is: 0xFD + encode_leb128(0x00)&lt;br&gt;
which expands to: 0xFD + 0x80&lt;/p&gt;

&lt;p&gt;LEB128 stands for little endian base 128, and is basically a number of bytes &amp;lt;0x80 (which each are taken for literal bits `total = total * 0x80 + byte`), and a byte &amp;gt;=0x80 that terminates the sequence (which is taken for bits, after masking off the 0x80 &lt;code&gt;total = total * 0x80 + (byte | 0x7F)&lt;/code&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of LEB128
&lt;/h2&gt;

&lt;p&gt;Examples of decoding LEB128:&lt;br&gt;
&lt;code&gt;decode_leb([0x80])&lt;/code&gt; = &lt;code&gt;0x80&lt;/code&gt; - 0x80 = 0&lt;br&gt;
&lt;code&gt;decode_leb([0x01, 0x80])&lt;/code&gt; = &lt;code&gt;0x01&lt;/code&gt; * 0x80 + &lt;code&gt;0x80&lt;/code&gt; - 0x80 = 127&lt;br&gt;
&lt;code&gt;decode_leb([0x5F, 0x42, 0xFF])&lt;/code&gt; = &lt;code&gt;0x5F&lt;/code&gt; * 0x80 * 0x80 + &lt;code&gt;0x42&lt;/code&gt; * 0x80 + &lt;code&gt;0xFF&lt;/code&gt; - 0x80 = 0x17E17F&lt;/p&gt;

&lt;p&gt;That's all i got. I hope that this can save someone from headache with WASM SIMD by hand.&lt;/p&gt;

</description>
      <category>webassembly</category>
    </item>
    <item>
      <title>So... staitc is Sorta Shifty.</title>
      <dc:creator>Shaw</dc:creator>
      <pubDate>Wed, 21 Dec 2022 08:46:39 +0000</pubDate>
      <link>https://forem.com/shawsumma/so-staitc-is-sorta-shifty-3kh9</link>
      <guid>https://forem.com/shawsumma/so-staitc-is-sorta-shifty-3kh9</guid>
      <description>&lt;p&gt;In a &lt;a href="https://dev.to/shawsumma/static-has-only-one-meaning-5d1e"&gt;previous post&lt;/a&gt; I pondered that in the C programming language static only has one meaning.&lt;/p&gt;

&lt;p&gt;Long story short I though that it meant "translation unit local". A term that effectively means that it is owned by the .c file or .o file and not the executable produced.&lt;/p&gt;

&lt;p&gt;This turns out to be wrong. As I dove further into the c language I came upon a puzzling piece of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// WTF. What the Function?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a state of panic I looked all over for what it meant. Google was being no help. Eventually however, I learned that it meant that there must be atleast &lt;code&gt;argc&lt;/code&gt; elements in argv (there is argc+1).&lt;/p&gt;

&lt;p&gt;This disproves my static theory.&lt;/p&gt;

&lt;p&gt;That is all. Thanks!&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Cosmopolitan Libc</title>
      <dc:creator>Shaw</dc:creator>
      <pubDate>Sat, 11 Dec 2021 03:14:23 +0000</pubDate>
      <link>https://forem.com/shawsumma/cosmopolitan-libc-4la4</link>
      <guid>https://forem.com/shawsumma/cosmopolitan-libc-4la4</guid>
      <description>&lt;h2&gt;
  
  
  Portable but not Small
&lt;/h2&gt;

&lt;p&gt;Java was one of the first languages to emphasize cross platform binaries. It did tis with the &lt;code&gt;.jar&lt;/code&gt; file, which is just a piece of compiled code that is executable on any platform. This does still require a Java Virtual Machine installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small but not Portable
&lt;/h2&gt;

&lt;p&gt;C on the other hand has never had this luxury. One would need to build C for each platform that exists. However, it allowed for C developers to ship standalone binaries that had no dependancies other than the underlying Libc. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://justine.lol/cosmopolitan/"&gt;Cosmopolitan Libc&lt;/a&gt; (Cosmo for short) is an implementation of Libc that runs on almost any 64 bit x86 machine. Cosmo does all this in a single binary.&lt;/p&gt;

&lt;p&gt;The same file that runs on Linux runs on Windows runs on MacOS. Binaries built with Cosmo, however, have the same lack of dependancies as files built with a standard Libc. Cosmo is everything I love in modern C.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Start with a simple hello world&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace all standard headers with a single &lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;cosmopolitan.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get Cosmopolitan Libc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://justine.lol/cosmopolitan/cosmopolitan.zip
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; cosmo &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;cosmo &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; 7z x ../cosmopolitan.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build the &lt;code&gt;.c&lt;/code&gt; file into a &lt;code&gt;.com&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc main.c &lt;span class="nt"&gt;-O2&lt;/span&gt; &lt;span class="nt"&gt;-static&lt;/span&gt; &lt;span class="nt"&gt;-fno-pie&lt;/span&gt; &lt;span class="nt"&gt;-mno-red-zone&lt;/span&gt; &lt;span class="nt"&gt;-nostdlib&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-nostdinc&lt;/span&gt; &lt;span class="nt"&gt;-fno-omit-frame-pointer&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; main.com.dbg &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-Wl&lt;/span&gt;,--gc-sections &lt;span class="nt"&gt;-fuse-ld&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;bfd &lt;span class="nt"&gt;-Wl&lt;/span&gt;,-T,cosmo/ape.lds &lt;span class="se"&gt;\&lt;/span&gt;
    cosmo/crt.o cosmo/ape.o cosmo/cosmopolitan.a &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-Icosmo&lt;/span&gt;
objcopy &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;-O&lt;/span&gt; binary main.com.dbg main.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the &lt;code&gt;.com&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x main.com
&lt;span class="nb"&gt;env&lt;/span&gt; ./main.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>c</category>
    </item>
    <item>
      <title>static has only one meaning</title>
      <dc:creator>Shaw</dc:creator>
      <pubDate>Thu, 09 Dec 2021 18:17:54 +0000</pubDate>
      <link>https://forem.com/shawsumma/static-has-only-one-meaning-5d1e</link>
      <guid>https://forem.com/shawsumma/static-has-only-one-meaning-5d1e</guid>
      <description>&lt;p&gt;&lt;code&gt;static&lt;/code&gt; can be confusing with the wrong definition. Here's a simplified example:&lt;/p&gt;

&lt;p&gt;The task is to write a &lt;code&gt;counter&lt;/code&gt; function that takes no arguments and returns a different number each time, starting at &lt;code&gt;0&lt;/code&gt; and adding &lt;code&gt;1&lt;/code&gt; each time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;current_counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;current_counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine someone is using this library. They write a function called &lt;code&gt;counter&lt;/code&gt; just for testing. But when they try to link their code, it fails, telling them that &lt;code&gt;counter&lt;/code&gt; is being redefined. But no worry, We are C programmers and we have &lt;code&gt;static&lt;/code&gt; at our disposal!&lt;/p&gt;

&lt;p&gt;Many believe static to have multiple meanings in C. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;static functions are not exported globally&lt;/li&gt;
&lt;li&gt;static local variables are hidden globals&lt;/li&gt;
&lt;li&gt;static global variables are not exported globally
What could have caused this mess of keyword overloading?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The answer lies in a shift of perspective. &lt;strong&gt;&lt;code&gt;static&lt;/code&gt; means compilation unit local&lt;/strong&gt;. A compilation unit in C is just the files given to the c compiler (gcc, clang, msvc, etc) and what they &lt;code&gt;#include&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using this definition we can say the following.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;static functions are owned by the compilation unit&lt;/li&gt;
&lt;li&gt;static global variables are owned by the compilation unit&lt;/li&gt;
&lt;li&gt;static local variables are owned by the compilation unit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using this knowledge, we can rewrite the counter function to not leak &lt;code&gt;counter&lt;/code&gt; into the API. &lt;/p&gt;

&lt;p&gt;First, move &lt;code&gt;current_counter&lt;/code&gt; into a static in &lt;code&gt;counter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;current_counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;current_counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will make &lt;code&gt;current_counter&lt;/code&gt; not collide with anything else named current.&lt;/p&gt;

&lt;p&gt;Next is to make &lt;code&gt;counter&lt;/code&gt; a static function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;current_counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;current_counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will make &lt;code&gt;counter&lt;/code&gt; not visible to files not in the same compilation unit.&lt;/p&gt;

&lt;p&gt;Now things are fixed! The &lt;code&gt;counter&lt;/code&gt; function and &lt;code&gt;current_counter&lt;/code&gt; variable cannot be used in files not in the same compilation unit. &lt;/p&gt;

&lt;p&gt;I hope this helped someone. &lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>I wrote a Cozy Programming language</title>
      <dc:creator>Shaw</dc:creator>
      <pubDate>Mon, 08 Nov 2021 22:10:39 +0000</pubDate>
      <link>https://forem.com/shawsumma/i-wrote-a-cozy-programming-language-12lj</link>
      <guid>https://forem.com/shawsumma/i-wrote-a-cozy-programming-language-12lj</guid>
      <description>&lt;p&gt;When i started on writing &lt;a href="https://github.com/ShawSumma/paka"&gt;Paka&lt;/a&gt; in March 2020 I had no idea what I was getting into.&lt;/p&gt;

&lt;p&gt;I had only previously spent one month tops per project; Moving onto newer ideas and abandoning code was my modus operandi. I started new project, like the countless before, that was intended to be an interpreter for a new langugage called DEXT written in &lt;a href="https://dlang.org"&gt;The D Programming Language&lt;/a&gt;. As time passed and the code grew to about 3000 lines before the name change to Paka.&lt;/p&gt;

&lt;p&gt;Paka was intended to be a lisp originally, but ended up a scripting language with a 'polite' syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# add two numbers&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 ​&lt;/p&gt;

&lt;p&gt;It translated recursion into simple array pushes and pops. This allowed for recursion to one's heart's content. &lt;/p&gt;

&lt;p&gt;It supported freezing the entire running program, heap and all, to a JSON subset consisting of only objects and strings. Sadly this had to be removed for its performance penalty. This is a feature on the top of the list to bring back, and I have devised a system to keep performance up.&lt;/p&gt;
&lt;h2&gt;
  
  
  Quest (October 2020)
&lt;/h2&gt;

&lt;p&gt;In order to help others' languages that looked fun, I decided to implement a new language that works alongside Paka. A free little language called &lt;a href="https://github.com/sampersand/quest"&gt;Quest&lt;/a&gt;, made by a friend by the name of sampersand. It had more performance than the implementation they made, though had much head room to improve. It ended up not working out in the end, but it was a fun way to test my language's capability and try to improve quest as a language.&lt;/p&gt;
&lt;h2&gt;
  
  
  Passerine (February 2021)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/vrtbl/passerine"&gt;Passerine&lt;/a&gt; was the next language i decided to try to fit onto paka, but alas this one too was eventually put aside for the time being.&lt;/p&gt;
&lt;h2&gt;
  
  
  MiniVM
&lt;/h2&gt;

&lt;p&gt;One day in a discord call a twist of fate come. Someone asked how to implement a virtual machine in C. I, having learned much about virtual machines in the past 13 months, was asked how to write one. What was going to be a one-off example soon ballooned into a new runtime and virtual machine for Paka.&lt;/p&gt;

&lt;p&gt;Performance was through the roof&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which ran in 850ms was now taking  500ms.&lt;/p&gt;

&lt;p&gt;MiniVM was soon converted from a stack machine to a register machine. This came with another reduction, this time to 400ms, for the above program&lt;/p&gt;

&lt;h2&gt;
  
  
  A Dream
&lt;/h2&gt;

&lt;p&gt;One of my far-off seeming goals was to get Paka to the point where it could be used to write a compiler for itself in.&lt;br&gt;
This dream manifested almost out of thin air, when i made a simple Paka file to evaluate expressions like &lt;code&gt;1 + 2 + 3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then came the rest of the syntax to see how far i could get; It turns out it was less of an undertaking than i had expected.&lt;/p&gt;
&lt;h2&gt;
  
  
  The state of it all
&lt;/h2&gt;

&lt;p&gt;Paka is now a cozy little language with a 30kb vm and 20kb compiler.&lt;/p&gt;

&lt;p&gt;It builds in less than a second for most folks with a computer from the last decade...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# build minivm and paka&lt;/span&gt;
git clone https://github.com/shawsumma/purr &lt;span class="nt"&gt;--recursive&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;purr
make &lt;span class="nt"&gt;-C&lt;/span&gt; minivm
make &lt;span class="nt"&gt;-C&lt;/span&gt; ./
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# run a file
./minivm/minivm bin/stage3 lang/test/echo.paka -- hello world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Call to Action
&lt;/h2&gt;

&lt;p&gt;If you found any of this article interesting or helpful, it would be appreciated to give me a star over at &lt;a href="https://github.com/shawsumma/purr"&gt;shawsumma/purr&lt;/a&gt; and/or &lt;a href="https://github.com/shawsumma/minivm"&gt;shawsumma/minivm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And please be kind to me, this is my first time really writing about paka or minivm more than a paragraph.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
