mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 10:22:19 +00:00
158 lines
22 KiB
HTML
158 lines
22 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `wasmer_clif_fork_frontend` crate."><meta name="keywords" content="rust, rustlang, rust-lang, wasmer_clif_fork_frontend"><title>wasmer_clif_fork_frontend - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../wasmer_clif_fork_frontend/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Crate wasmer_clif_fork_frontend</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all wasmer_clif_fork_frontend's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#constants">Constants</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'wasmer_clif_fork_frontend', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../src/wasmer_clif_fork_frontend/lib.rs.html#1-204' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>wasmer_clif_fork_frontend</a></span></h1><div class='docblock'><p>Cranelift IR builder library.</p>
|
||
<p>Provides a straightforward way to create a Cranelift IR function and fill it with instructions
|
||
corresponding to your source program written in another language.</p>
|
||
<p>To get started, create an <a href="struct.FunctionBuilderContext.html"><code>FunctionBuilderContext</code></a> and
|
||
pass it as an argument to a <a href="struct.FunctionBuilder.html"><code>FunctionBuilder</code></a>.</p>
|
||
<h1 id="mutable-variables-and-cranelift-ir-values" class="section-header"><a href="#mutable-variables-and-cranelift-ir-values">Mutable variables and Cranelift IR values</a></h1>
|
||
<p>The most interesting feature of this API is that it provides a single way to deal with all your
|
||
variable problems. Indeed, the <a href="struct.FunctionBuilder.html"><code>FunctionBuilder</code></a> struct has a
|
||
type <code>Variable</code> that should be an index of your source language variables. Then, through
|
||
calling the functions
|
||
<a href="struct.FunctionBuilder.html#method.declare_var"><code>declare_var</code></a>,
|
||
<a href="struct.FunctionBuilder.html#method.def_var"><code>def_var</code></a> and
|
||
<a href="struct.FunctionBuilder.html#method.use_var"><code>use_var</code></a>, the
|
||
<a href="struct.FunctionBuilder.html"><code>FunctionBuilder</code></a> will create for you all the Cranelift IR
|
||
values corresponding to your variables.</p>
|
||
<p>This API has been designed to help you translate your mutable variables into
|
||
<a href="https://en.wikipedia.org/wiki/Static_single_assignment_form"><code>SSA</code></a> form.
|
||
<a href="struct.FunctionBuilder.html#method.use_var"><code>use_var</code></a> will return the Cranelift IR value
|
||
that corresponds to your mutable variable at a precise point in the program. However, if you know
|
||
beforehand that one of your variables is defined only once, for instance if it is the result
|
||
of an intermediate expression in an expression-based language, then you can translate it
|
||
directly by the Cranelift IR value returned by the instruction builder. Using the
|
||
<a href="struct.FunctionBuilder.html#method.use_var"><code>use_var</code></a> API for such an immutable variable
|
||
would also work but with a slight additional overhead (the SSA algorithm does not know
|
||
beforehand if a variable is immutable or not).</p>
|
||
<p>The moral is that you should use these three functions to handle all your mutable variables,
|
||
even those that are not present in the source code but artifacts of the translation. It is up
|
||
to you to keep a mapping between the mutable variables of your language and their <code>Variable</code>
|
||
index that is used by Cranelift. Caution: as the <code>Variable</code> is used by Cranelift to index an
|
||
array containing information about your mutable variables, when you create a new <code>Variable</code>
|
||
with [<code>Variable::new(var_index)</code>] you should make sure that <code>var_index</code> is provided by a
|
||
counter incremented by 1 each time you encounter a new mutable variable.</p>
|
||
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
|
||
<p>Here is a pseudo-program we want to transform into Cranelift IR:</p>
|
||
<pre><code class="language-clif">function(x) {
|
||
x, y, z : i32
|
||
block0:
|
||
y = 2;
|
||
z = x + y;
|
||
jump block1
|
||
block1:
|
||
z = z + y;
|
||
brnz y, block2;
|
||
z = z - x;
|
||
return y
|
||
block2:
|
||
y = y - x
|
||
jump block1
|
||
}
|
||
</code></pre>
|
||
<p>Here is how you build the corresponding Cranelift IR function using <code>FunctionBuilderContext</code>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">cranelift_codegen</span>;
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">cranelift_frontend</span>;
|
||
|
||
<span class="kw">use</span> <span class="ident">cranelift_codegen</span>::<span class="ident">entity</span>::<span class="ident">EntityRef</span>;
|
||
<span class="kw">use</span> <span class="ident">cranelift_codegen</span>::<span class="ident">ir</span>::<span class="ident">types</span>::<span class="kw-2">*</span>;
|
||
<span class="kw">use</span> <span class="ident">cranelift_codegen</span>::<span class="ident">ir</span>::{<span class="ident">AbiParam</span>, <span class="ident">ExternalName</span>, <span class="ident">Function</span>, <span class="ident">InstBuilder</span>, <span class="ident">Signature</span>};
|
||
<span class="kw">use</span> <span class="ident">cranelift_codegen</span>::<span class="ident">isa</span>::<span class="ident">CallConv</span>;
|
||
<span class="kw">use</span> <span class="ident">cranelift_codegen</span>::<span class="ident">settings</span>;
|
||
<span class="kw">use</span> <span class="ident">cranelift_codegen</span>::<span class="ident">verifier</span>::<span class="ident">verify_function</span>;
|
||
<span class="kw">use</span> <span class="ident">cranelift_frontend</span>::{<span class="ident">FunctionBuilder</span>, <span class="ident">FunctionBuilderContext</span>, <span class="ident">Position</span>, <span class="ident">Variable</span>};
|
||
|
||
<span class="kw">fn</span> <span class="ident">main</span>() {
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">sig</span> <span class="op">=</span> <span class="ident">Signature</span>::<span class="ident">new</span>(<span class="ident">CallConv</span>::<span class="ident">SystemV</span>);
|
||
<span class="ident">sig</span>.<span class="ident">returns</span>.<span class="ident">push</span>(<span class="ident">AbiParam</span>::<span class="ident">new</span>(<span class="ident">I32</span>));
|
||
<span class="ident">sig</span>.<span class="ident">params</span>.<span class="ident">push</span>(<span class="ident">AbiParam</span>::<span class="ident">new</span>(<span class="ident">I32</span>));
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">fn_builder_ctx</span> <span class="op">=</span> <span class="ident">FunctionBuilderContext</span>::<span class="ident">new</span>();
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">func</span> <span class="op">=</span> <span class="ident">Function</span>::<span class="ident">with_name_signature</span>(<span class="ident">ExternalName</span>::<span class="ident">user</span>(<span class="number">0</span>, <span class="number">0</span>), <span class="ident">sig</span>);
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">position</span> <span class="op">=</span> <span class="ident">Position</span>::<span class="ident">default</span>();
|
||
{
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">builder</span> <span class="op">=</span> <span class="ident">FunctionBuilder</span>::<span class="ident">new</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">func</span>, <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">fn_builder_ctx</span>, <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">position</span>);
|
||
|
||
<span class="kw">let</span> <span class="ident">block0</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">create_ebb</span>();
|
||
<span class="kw">let</span> <span class="ident">block1</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">create_ebb</span>();
|
||
<span class="kw">let</span> <span class="ident">block2</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">create_ebb</span>();
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">Variable</span>::<span class="ident">new</span>(<span class="number">0</span>);
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="ident">Variable</span>::<span class="ident">new</span>(<span class="number">1</span>);
|
||
<span class="kw">let</span> <span class="ident">z</span> <span class="op">=</span> <span class="ident">Variable</span>::<span class="ident">new</span>(<span class="number">2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">declare_var</span>(<span class="ident">x</span>, <span class="ident">I32</span>);
|
||
<span class="ident">builder</span>.<span class="ident">declare_var</span>(<span class="ident">y</span>, <span class="ident">I32</span>);
|
||
<span class="ident">builder</span>.<span class="ident">declare_var</span>(<span class="ident">z</span>, <span class="ident">I32</span>);
|
||
<span class="ident">builder</span>.<span class="ident">append_ebb_params_for_function_params</span>(<span class="ident">block0</span>);
|
||
|
||
<span class="ident">builder</span>.<span class="ident">switch_to_block</span>(<span class="ident">block0</span>);
|
||
<span class="ident">builder</span>.<span class="ident">seal_block</span>(<span class="ident">block0</span>);
|
||
{
|
||
<span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">ebb_params</span>(<span class="ident">block0</span>)[<span class="number">0</span>]; <span class="comment">// the first function parameter</span>
|
||
<span class="ident">builder</span>.<span class="ident">def_var</span>(<span class="ident">x</span>, <span class="ident">tmp</span>);
|
||
}
|
||
{
|
||
<span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">iconst</span>(<span class="ident">I32</span>, <span class="number">2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">def_var</span>(<span class="ident">y</span>, <span class="ident">tmp</span>);
|
||
}
|
||
{
|
||
<span class="kw">let</span> <span class="ident">arg1</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">x</span>);
|
||
<span class="kw">let</span> <span class="ident">arg2</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">y</span>);
|
||
<span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">iadd</span>(<span class="ident">arg1</span>, <span class="ident">arg2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">def_var</span>(<span class="ident">z</span>, <span class="ident">tmp</span>);
|
||
}
|
||
<span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">jump</span>(<span class="ident">block1</span>, <span class="kw-2">&</span>[]);
|
||
|
||
<span class="ident">builder</span>.<span class="ident">switch_to_block</span>(<span class="ident">block1</span>);
|
||
{
|
||
<span class="kw">let</span> <span class="ident">arg1</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">y</span>);
|
||
<span class="kw">let</span> <span class="ident">arg2</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">z</span>);
|
||
<span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">iadd</span>(<span class="ident">arg1</span>, <span class="ident">arg2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">def_var</span>(<span class="ident">z</span>, <span class="ident">tmp</span>);
|
||
}
|
||
{
|
||
<span class="kw">let</span> <span class="ident">arg</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">y</span>);
|
||
<span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">brnz</span>(<span class="ident">arg</span>, <span class="ident">block2</span>, <span class="kw-2">&</span>[]);
|
||
}
|
||
{
|
||
<span class="kw">let</span> <span class="ident">arg1</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">z</span>);
|
||
<span class="kw">let</span> <span class="ident">arg2</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">x</span>);
|
||
<span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">isub</span>(<span class="ident">arg1</span>, <span class="ident">arg2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">def_var</span>(<span class="ident">z</span>, <span class="ident">tmp</span>);
|
||
}
|
||
{
|
||
<span class="kw">let</span> <span class="ident">arg</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">y</span>);
|
||
<span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">return_</span>(<span class="kw-2">&</span>[<span class="ident">arg</span>]);
|
||
}
|
||
|
||
<span class="ident">builder</span>.<span class="ident">switch_to_block</span>(<span class="ident">block2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">seal_block</span>(<span class="ident">block2</span>);
|
||
|
||
{
|
||
<span class="kw">let</span> <span class="ident">arg1</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">y</span>);
|
||
<span class="kw">let</span> <span class="ident">arg2</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">use_var</span>(<span class="ident">x</span>);
|
||
<span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">isub</span>(<span class="ident">arg1</span>, <span class="ident">arg2</span>);
|
||
<span class="ident">builder</span>.<span class="ident">def_var</span>(<span class="ident">y</span>, <span class="ident">tmp</span>);
|
||
}
|
||
<span class="ident">builder</span>.<span class="ident">ins</span>().<span class="ident">jump</span>(<span class="ident">block1</span>, <span class="kw-2">&</span>[]);
|
||
<span class="ident">builder</span>.<span class="ident">seal_block</span>(<span class="ident">block1</span>);
|
||
|
||
<span class="ident">builder</span>.<span class="ident">finalize</span>();
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">flags</span> <span class="op">=</span> <span class="ident">settings</span>::<span class="ident">Flags</span>::<span class="ident">new</span>(<span class="ident">settings</span>::<span class="ident">builder</span>());
|
||
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">verify_function</span>(<span class="kw-2">&</span><span class="ident">func</span>, <span class="kw-2">&</span><span class="ident">flags</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">func</span>.<span class="ident">display</span>(<span class="prelude-val">None</span>));
|
||
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">errors</span>) <span class="op">=</span> <span class="ident">res</span> {
|
||
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">errors</span>);
|
||
}
|
||
}</pre></div>
|
||
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table><tr class='module-item'><td><a class="struct" href="struct.Block.html" title='wasmer_clif_fork_frontend::Block struct'>Block</a></td><td class='docblock-short'><p>A opaque reference to a basic block.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.FunctionBuilder.html" title='wasmer_clif_fork_frontend::FunctionBuilder struct'>FunctionBuilder</a></td><td class='docblock-short'><p>Temporary object used to build a single Cranelift IR <code>Function</code>.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.FunctionBuilderContext.html" title='wasmer_clif_fork_frontend::FunctionBuilderContext struct'>FunctionBuilderContext</a></td><td class='docblock-short'><p>Structure used for translating a series of functions into Cranelift IR.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Position.html" title='wasmer_clif_fork_frontend::Position struct'>Position</a></td><td class='docblock-short'><p>Position</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Switch.html" title='wasmer_clif_fork_frontend::Switch struct'>Switch</a></td><td class='docblock-short'><p>Unlike with <code>br_table</code>, <code>Switch</code> cases may be sparse or non-0-based.
|
||
They emit efficient code using branches, jump tables, or a combination of both.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Variable.html" title='wasmer_clif_fork_frontend::Variable struct'>Variable</a></td><td class='docblock-short'><p>An opaque reference to a variable.</p>
|
||
</td></tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2>
|
||
<table><tr class='module-item'><td><a class="constant" href="constant.VERSION.html" title='wasmer_clif_fork_frontend::VERSION constant'>VERSION</a></td><td class='docblock-short'><p>Version number of this crate.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "wasmer_clif_fork_frontend";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |