diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-03-27 10:09:53 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-03-27 10:09:53 -0400 |
| commit | 8103fc4b5061a5b3f9e674087f48f0db44b44000 (patch) | |
| tree | 35b94ab4c9eeebae79eec819f21ff9a0cbda4ab3 /docs/implementation | |
| parent | 8aa389aa1e6665df6b23fcbc0586e57972b668f9 (diff) | |
More accurate bytecode information
Diffstat (limited to 'docs/implementation')
| -rw-r--r-- | docs/implementation/vm.html | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/docs/implementation/vm.html b/docs/implementation/vm.html index bb99151c..f00f64ac 100644 --- a/docs/implementation/vm.html +++ b/docs/implementation/vm.html @@ -8,22 +8,24 @@ <p>BQN's self-hosted compiler and runtime mean that only a small amount of native code is needed to run BQN on any given platform. For example, the <a href="https://github.com/mlochbaum/BQN/blob/master/implementation/../docs/bqn.js">Javascript environment</a> requires about 300 lines of Javascript code even though it compiles BQN bytecode to Javascript, a more complex strategy than interpreting it directly. This makes it fairly easy to port BQN to new platforms, allowing BQN to run "natively" within other programming languages and interact with arrays in those languages.</p> <h2 id="bytecode">Bytecode</h2> <p>The BQN implementation here and dzaima/BQN share a stack-based object code format used to represent compiled code. This format is a list of numbers of unspecified precision (small precision will limit the length of list literals and number of locals per block, blocks, and constants). Previously it was encoded as bytes with the <a href="https://en.wikipedia.org/wiki/LEB128">LEB128</a> format; while it no longer has anything to do with bytes it's called a "bytecode" because this is shorter than "object code".</p> -<p>dzaima/BQN can interpret bytecode or convert it to <a href="https://en.wikipedia.org/wiki/Java_virtual_machine">JVM</a> bytecode, while the Javascript VM previously interpreted bytecode but now always compiles it. Since interpretation is a simpler strategy, it may be helpful to use the <a href="https://github.com/mlochbaum/BQN/blob/f74d9223ef880f2914030c2375f680dcc7e8c92b/bqn.js#L36">old Javascript bytecode interpreter</a> as a reference when implementing a BQN virtual machine.</p> +<p>The self-hosted compiler uses a simpler, and less capable, format for block and variable data than dzaima/BQN. Only this format is described here; <a href="https://github.com/mlochbaum/BQN/blob/master/implementation/../dc.bqn">dc.bqn</a> adapts it to be compatible with dzaima/BQN.</p> +<p>dzaima/BQN can interpret bytecode or convert it to <a href="https://en.wikipedia.org/wiki/Java_virtual_machine">JVM</a> bytecode, while the Javascript VM previously interpreted bytecode but now always compiles it. Since interpretation is a simpler strategy, it may be helpful to use the <a href="https://github.com/mlochbaum/BQN/blob/f74d9223ef880f2914030c2375f680dcc7e8c92b/bqn.js#L36">old Javascript bytecode interpreter</a> as a reference (for bytecode execution only) when implementing a BQN virtual machine.</p> <h3 id="components">Components</h3> <p>The complete bytecode for a program consists of the following:</p> <ul> <li>A bytecode sequence <code><span class='Value'>code</span></code></li> <li>A list <code><span class='Value'>consts</span></code> of constants that can be loaded</li> -<li><em>(dzaima/BQN only) A list of identifier names</em></li> -<li>A list <code><span class='Value'>blocks</span></code> of block information, described in the next section.</li> +<li>A list <code><span class='Value'>blocks</span></code> of block information, described in the next section</li> +<li>Optionally, source locations for each instruction</li> +<li>Optionally, tokenization information</li> </ul> <h3 id="blocks">Blocks</h3> <p>Each block in <code><span class='Value'>blocks</span></code> is a list of the following properties:</p> <ul> <li>Block type: (0) function/immediate, (1) 1-modifier, (2) 2-modifier</li> <li>Block immediateness: (1) immediate or (0) deferred</li> -<li><em>(dzaima/BQN only) List of local identifier names</em></li> <li>Block starting index in <code><span class='Value'>code</span></code></li> +<li>Number of variables the block needs to allocate</li> </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> |
