Skip to content

Commit

Permalink
deploy: 2da11ad
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwalker committed Jul 2, 2024
1 parent b25e536 commit 816d9db
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 52 deletions.
67 changes: 42 additions & 25 deletions print.html
Original file line number Diff line number Diff line change
Expand Up @@ -1325,20 +1325,20 @@ <h3 id="procedures"><a class="header" href="#procedures">Procedures</a></h3>
&lt;instructions&gt;
end
</code></pre>
<p>A procedure label must start with a letter and can contain any combination of numbers, ASCII letters, and underscores (<code>_</code>). The number of characters in the procedure label cannot exceed 100.</p>
<p>A procedure label must start with a letter and can contain any combination of numbers, ASCII letters, and underscores (<code>_</code>). Should you need to represent a label with other characters, an extended set is permitted via quoted identifiers, i.e. an identifier surrounded by <code>".."</code>. Quoted identifiers additionally allow any alphanumeric letter (ASCII or UTF-8), as well as various common punctuation characters: <code>!</code>, <code>?</code>, <code>:</code>, <code>.</code>, <code>&lt;</code>, <code>&gt;</code>, and <code>-</code>. Quoted identifiers are primarily intended for representing symbols/identifiers when compiling higher-level languages to Miden Assembly, but can be used anywhere that normal identifiers are expected.</p>
<p>The number of locals specifies the number of memory-based local words a procedure can access (via <code>loc_load</code>, <code>loc_store</code>, and <a href="user_docs/assembly/./io_operations.html#random-access-memory">other instructions</a>). If a procedure doesn't need any memory-based locals, this parameter can be omitted or set to <code>0</code>. A procedure can have at most <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">16</span></span></span></span></span></span></span></span></span></span></span></span> locals, and the total number of locals available to all procedures at runtime is limited to <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">30</span></span></span></span></span></span></span></span></span></span></span></span>.</p>
<p>To execute a procedure, the <code>exec.&lt;label&gt;</code>, <code>call.&lt;label&gt;</code>, and <code>syscall.&lt;label&gt;</code> instructions can be used. For example:</p>
<pre><code>exec.foo
</code></pre>
<p>The difference between using each of these instructions is explained in the <a href="user_docs/assembly/./execution_contexts.html#procedure-invocation-semantics">next section</a>.</p>
<p>A procedure may execute any other previously defined procedure, but it cannot execute itself or any of the subsequent procedures. Thus, recursive procedure calls are not possible. For example, the following code block defines a program with two procedures:</p>
<pre><code>proc.foo
<p>A procedure may execute any other procedure, however recursion is not currently permitted, due to limitations imposed by the Merkalized Abstract Syntax Tree. Recursion is caught by static analysis of the call graph during assembly, so in general you don't need to think about this, but it is a limitation to be aware of. For example, the following code block defines a program with two procedures:</p>
<pre><code>proc.bar
&lt;instructions&gt;
exec.foo
&lt;instructions&gt;
end

proc.bar
&lt;instructions&gt;
exec.foo
proc.foo
&lt;instructions&gt;
end

Expand All @@ -1350,18 +1350,19 @@ <h3 id="procedures"><a class="header" href="#procedures">Procedures</a></h3>
end
</code></pre>
<h4 id="dynamic-procedure-invocation"><a class="header" href="#dynamic-procedure-invocation">Dynamic procedure invocation</a></h4>
<p>It is also possible to invoke procedures dynamically - i.e., without specifying target procedure labels at compile time. There are two instructions, <code>dynexec</code> and <code>dyncall</code>, which can be used to execute dynamically-specified code targets. Both instructions expect <a href="user_docs/assembly/../../design/programs.html">MAST root</a> of the target to be provided via the stack. The difference between <code>dynexec</code> and <code>dyncall</code> is that <code>dyncall</code> will <a href="user_docs/assembly/./execution_contexts.html">change context</a> before executing the dynamic code target, while <code>dynexec</code> will cause the code target to be executed in the current context.</p>
<p>Dynamic code execution in the same context is achieved by setting the top <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">4</span></span></span></span> elements of the stack to the hash of the dynamic code block and then executing the following instruction:</p>
<pre><code>dynexec
<p>It is also possible to invoke procedures dynamically - i.e., without specifying target procedure labels at compile time. Unlike static procedure invocation, recursion is technically possible using dynamic invocation, but dynamic invocation is more expensive, and has less available operand stack capacity for procedure arguments, as 4 elements are required for the MAST root of the callee. There are two instructions, <code>dynexec</code> and <code>dyncall</code>, which can be used to execute dynamically-specified code targets. Both instructions expect the <a href="user_docs/assembly/../../design/programs.html">MAST root</a> of the target to be provided via the stack. The difference between <code>dynexec</code> and <code>dyncall</code> corresponds to the difference between <code>exec</code> and <code>call</code>, see the documentation on <a href="user_docs/assembly/./execution_contexts.html#procedure-invocation-semantics">procedure invocation semantics</a> for more detail.</p>
<p>Dynamic code execution in the same context is achieved by setting the top <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">4</span></span></span></span> elements of the stack to the hash of the dynamic code block and then executing the <code>dynexec</code> or <code>dyncall</code> instruction. You can obtain the hash of a procedure in the current program, by name, using the <code>procref</code> instruction. See the following example of pairing the two:</p>
<pre><code>procref.foo
dynexec
</code></pre>
<p>This causes the VM to do the following:</p>
<p>During assembly, the <code>procref.foo</code> instruction is compiled to a <code>push.HASH</code>, where <code>HASH</code> is the hash of the MAST root of the <code>foo</code> procedure.</p>
<p>During execution of the <code>dynexec</code> instruction, the VM does the following:</p>
<ol>
<li>Read the top 4 elements of the stack to get the hash of the dynamic target (leaving the stack unchanged).</li>
<li>Execute the code block which hashes to the specified target. The VM must know the specified code block and hash: they must be in the CodeBlockTable of the executing Program. Hashes can be put into the CodeBlockTable manually, or by executing <code>call</code>, <code>syscall</code>, or <code>procref</code> instructions.</li>
<li>Reads, but does not consume, the top 4 elements of the stack to get the hash of the dynamic target (i.e. the operand stack is left unchanged).</li>
<li>Load the code block referenced by the hash, or trap if no such MAST root is known.</li>
<li>Execute the loaded code block</li>
</ol>
<p>Dynamic code execution in a new context can be achieved similarly by setting the top <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">4</span></span></span></span> elements of the stack to the hash of the dynamic code block and then executing the following instruction:</p>
<pre><code>dyncall
</code></pre>
<p>The <code>dyncall</code> instruction is used the same way, with the difference that it involves a context switch to a new context when executing the referenced block, and switching back to the calling context once execution of the callee completes.</p>
<blockquote>
<p><strong>Note</strong>: In both cases, the stack is left unchanged. Therefore, if the dynamic code is intended to manipulate the stack, it should start by either dropping or moving the code block hash from the top of the stack.</p>
</blockquote>
Expand Down Expand Up @@ -1394,26 +1395,35 @@ <h4 id="programs"><a class="header" href="#programs">Programs</a></h4>
<p>A program cannot contain any exported procedures.</p>
<p>When a program is executed, the execution starts at the first instruction following the <code>begin</code> instruction. The main procedure is expected to be the last procedure in the program and can be followed only by comments.</p>
<h4 id="importing-modules"><a class="header" href="#importing-modules">Importing modules</a></h4>
<p>To invoke a procedure from an external module, the module first needs to be imported using a <code>use</code> instruction. Once a module is imported, procedures from this module can be invoked via the regular <code>exec</code> or <code>call</code> instructions as <code>exec|call.&lt;module&gt;::&lt;label&gt;</code> where <code>label</code> is the name of the procedure. For example:</p>
<p>To reference items in another module, you must either import the module you wish to use, or specify a fully-qualified path to the item you want to reference.</p>
<p>To import a module, you must use the <code>use</code> keyword in the top level scope of the current module, as shown below:</p>
<pre><code>use.std::math::u64

begin
push.1.0
push.2.0
exec.u64::wrapping_add
...
end
</code></pre>
<p>In the above example we import <code>std::math::u64</code> module from the <a href="user_docs/assembly/../stdlib/main.html">standard library</a>. We then execute a program which pushes two 64-bit integers onto the stack, and then invokes a 64-bit addition procedure from the imported module.</p>
<p>We can also define aliases for imported modules. For example:</p>
<p>In this example, the <code>std::math::u64</code> module is imported as <code>u64</code>, the default "alias" for the imported module. You can specify a different alias like so:</p>
<pre><code>use.std::math::u64-&gt;bigint
</code></pre>
<p>This would alias the imported module as <code>bigint</code> rather than <code>u64</code>. The alias is needed to reference items from the imported module, as shown below:</p>
<pre><code>use.std::math::u64

begin
push.1.0
push.2.0
exec.bigint::checked_add
exec.u64::wrapping_add
end
</code></pre>
<p>You can also bypass imports entirely, and specify an absolute procedure path, which requires prefixing the path with <code>::</code>. For example:</p>
<pre><code>begin
push.1.0
push.2.0
exec.::std::math::u64::wrapping_add
end
</code></pre>
<p>The set of modules which can be imported by a program can be specified via a Module Provider when instantiating the <a href="https://crates.io/crates/miden-assembly">Miden Assembler</a> used to compile the program.</p>
<p>In the examples above, we have been referencing the <code>std::math::u64</code> module, which is a module in the <a href="user_docs/assembly/../stdlib/main.html">Miden Standard Library</a>. There are a number of useful modules there, that provide a variety of helpful functionality out of the box.</p>
<p>If the assembler does not know about the imported modules, assembly will fail. You can register modules with the assembler when instantiating it, either in source form, or precompiled form. See the <a href="https://crates.io/crates/miden-assembly">miden-assembly docs</a> for details. The assembler will use this information to resolve references to imported procedures during assembly.</p>
<h4 id="re-exporting-procedures"><a class="header" href="#re-exporting-procedures">Re-exporting procedures</a></h4>
<p>A procedure defined in one module can be re-exported from a different module under the same or a different name. For example:</p>
<pre><code>use.std::math::u64
Expand All @@ -1425,9 +1435,16 @@ <h4 id="re-exporting-procedures"><a class="header" href="#re-exporting-procedure
&lt;instructions&gt;
end
</code></pre>
<p>In addition to the locally-defined procedure <code>foo</code>, the above module also exports procedures <code>add</code> and <code>mul64</code> implementations of which will be identical to <code>add</code> and <code>mul</code> procedures from the <code>std::math::u64</code> module respectively.</p>
<p>In the module shown above, not only is the locally-defined procedure <code>foo</code> exported, but so are two procedures named <code>add</code> and <code>mul64</code>, whose implementations are defined in the <code>std::math::u64</code> module.</p>
<p>Similar to procedure invocation, you can bypass the explicit import by specifying an absolute path, like so:</p>
<pre><code>export.::std::math::u64::mul-&gt;mul64
</code></pre>
<p>Additionally, you may re-export a procedure using its MAST root, so long as you specify an alias:</p>
<pre><code>export.0x0000..0000-&gt;mul64
</code></pre>
<p>In all of the forms described above, the actual implementation of the re-exported procedure is defined externally. Other modules which reference the re-exported procedure, will have those references resolved to the original procedure during assembly.</p>
<h3 id="constants"><a class="header" href="#constants">Constants</a></h3>
<p>Miden assembly supports constant declarations. These constants are scoped to the module they are defined in and can be used as immediate parameters for Miden assembly instructions. Constants are supported as immediate values for the following instructions: <code>push</code>, <code>assert</code>, <code>assertz</code>, <code>asert_eq</code>, <code>assert_eqw</code>, <code>locaddr</code>, <code>loc_load</code>, <code>loc_loadw</code>, <code>loc_store</code>, <code>loc_storew</code>, <code>mem_load</code>, <code>mem_loadw</code>, <code>mem_store</code>, <code>mem_storew</code>, <code>mtree_verify</code>.</p>
<p>Miden assembly supports constant declarations. These constants are scoped to the module they are defined in and can be used as immediate parameters for Miden assembly instructions. Constants are supported as immediate values for many of the instructions in the Miden Assembly instruction set, see the documentation for specific instructions to determine whether or not it provides a form which accepts immediate operands.</p>
<p>Constants must be declared right after module imports and before any procedures or program bodies. A constant's name must start with an upper-case letter and can contain any combination of numbers, upper-case ASCII letters, and underscores (<code>_</code>). The number of characters in a constant name cannot exceed 100.</p>
<p>A constant's value must be in a decimal or hexidecimal form and be in the range between <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span> and <span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">64</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">32</span></span></span></span></span></span></span></span></span></span></span></span> (both inclusive). Value can be defined by an arithmetic expression using <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>//</code>, <code>(</code>, <code>)</code> operators and references to the previously defined constants if it uses only decimal numbers. Here <code>/</code> is a field division and <code>//</code> is an integer division. Note that the arithmetic expression cannot contain spaces.</p>
<pre><code>use.std::math::u64
Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion searchindex.json

Large diffs are not rendered by default.

Loading

0 comments on commit 816d9db

Please sign in to comment.