diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-09-22 13:41:15 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-09-22 13:41:15 -0400 |
| commit | d0f0a34d84722957650e3d20977bdf563bc9ac71 (patch) | |
| tree | 081edf7fc553f3c2c5a881957dc9ff1cba51d29c /implementation/vm.md | |
| parent | 23941024161ee15edb19f809f5cc29d53fe8fd25 (diff) | |
Forgot about LEB128 decoding
Diffstat (limited to 'implementation/vm.md')
| -rw-r--r-- | implementation/vm.md | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/implementation/vm.md b/implementation/vm.md index cd1777b9..2e501bf5 100644 --- a/implementation/vm.md +++ b/implementation/vm.md @@ -30,6 +30,14 @@ Compilation separates blocks so that they are not nested in bytecode. All compil 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. +### LEB128 + +Arguments for BQN bytecode are always natural numbers, encoded as a sequence of bytes using the unsigned [LEB128](https://en.wikipedia.org/wiki/LEB128) format. The environment only needs to decode this format and not encode it (encoding is done by `LEBv` in the compiler). + +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). + +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. + ### Instructions The following instructions are defined by dzaima/BQN. The ones emitted by the self-hosted BQN compiler are marked in the "used" column. |
