aboutsummaryrefslogtreecommitdiff
path: root/docs/implementation
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-09-22 13:41:15 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-09-22 13:41:15 -0400
commitd0f0a34d84722957650e3d20977bdf563bc9ac71 (patch)
tree081edf7fc553f3c2c5a881957dc9ff1cba51d29c /docs/implementation
parent23941024161ee15edb19f809f5cc29d53fe8fd25 (diff)
Forgot about LEB128 decoding
Diffstat (limited to 'docs/implementation')
-rw-r--r--docs/implementation/vm.html4
1 files changed, 4 insertions, 0 deletions
diff --git a/docs/implementation/vm.html b/docs/implementation/vm.html
index cd45b4e2..cf39c1ce 100644
--- a/docs/implementation/vm.html
+++ b/docs/implementation/vm.html
@@ -27,6 +27,10 @@
</ul>
<p>Compilation separates blocks so that they are not nested in bytecode. All compiled code is contained in some block. The self-hosted compiler compiles the entire program into an immediate block, and the program is run by evaluating this block. Blocks are terminated with the RETN instruction.</p>
<p>The starting index refers to the position where execution starts in order to evaluate the block. When the block is evaluated depends on its type and immediateness. An immediate block (0,1) is evaluated as soon as it is pushed; a function (0,0) is evaluated when called on arguments, an immediate modifier (1 or 2, 1) is evaluated when called on operands, and a deferred modifier (1 or 2, 0) creates a derived function when called on operands and is evaluated when this derived function is called on arguments.</p>
+<h3 id="leb128">LEB128</h3>
+<p>Arguments for BQN bytecode are always natural numbers, encoded as a sequence of bytes using the unsigned <a href="https://en.wikipedia.org/wiki/LEB128">LEB128</a> format. The environment only needs to decode this format and not encode it (encoding is done by <code><span class='Function'>LEBv</span></code> in the compiler).</p>
+<p>To read a number from the byte stream, read (unsigned) bytes from the stream in sequence, stopping when a byte less than 128 is found. Each byte contributes its lowest 7 bits to the number: the byte's value if it is less than 128, and its value minus 128 otherwise. Values are accumulated starting at the lowest-order bits. To accomplish this, begin with a multiplier of 1 (shift of 0). At each step, add the 7 bits read, times the multiplier, to a running total that starts at 0, then multiply the multiplier by 128 (or add 7 to the shift).</p>
+<p>Because most encoded numbers will be less than 128, for higher performance it's best to add a special case for the first byte, which simply returns the byte if it's less than 128.</p>
<h3 id="instructions">Instructions</h3>
<p>The following instructions are defined by dzaima/BQN. The ones emitted by the self-hosted BQN compiler are marked in the &quot;used&quot; column.</p>
<table>