aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/llvm_kaleidoscope_rs/codegen.rs.html
diff options
context:
space:
mode:
authorjohannst <johannst@users.noreply.github.com>2021-09-24 22:49:57 +0000
committerjohannst <johannst@users.noreply.github.com>2021-09-24 22:49:57 +0000
commit4b1a7d8a1d315090fb808ba4695bbacdc91e1aff (patch)
treebefae4a33b331e0e8c322a4c3ea5587b4c94898e /src/llvm_kaleidoscope_rs/codegen.rs.html
parentbb721dfa28499d340baeec1dd730ec1cd771fdcf (diff)
downloadllvm-kaleidoscope-rs-4b1a7d8a1d315090fb808ba4695bbacdc91e1aff.tar.gz
llvm-kaleidoscope-rs-4b1a7d8a1d315090fb808ba4695bbacdc91e1aff.zip
deploy: 6eb6ad9f574c783d471f6a863299af25b6f5a8c7
Diffstat (limited to 'src/llvm_kaleidoscope_rs/codegen.rs.html')
-rw-r--r--src/llvm_kaleidoscope_rs/codegen.rs.html82
1 files changed, 68 insertions, 14 deletions
diff --git a/src/llvm_kaleidoscope_rs/codegen.rs.html b/src/llvm_kaleidoscope_rs/codegen.rs.html
index bcaa770..e94ac8d 100644
--- a/src/llvm_kaleidoscope_rs/codegen.rs.html
+++ b/src/llvm_kaleidoscope_rs/codegen.rs.html
@@ -1,6 +1,4 @@
-<!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="Source of the Rust file `src/codegen.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>codegen.rs - source</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="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings"></script><script src="../../storage.js"></script><script src="../../crates.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg">
-<link rel="alternate icon" type="image/png" href="../../favicon-16x16.png">
-<link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../../llvm_kaleidoscope_rs/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img src="../../brush.svg" width="18" height="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../../settings.html" title="settings"><img src="../../wheel.svg" width="18" height="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><div class="example-wrap"><pre class="line-numbers"><span id="1"> 1</span>
+<!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="Source of the Rust file `src&#x2F;codegen.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>codegen.rs - source</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="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings" ></script><script src="../../storage.js"></script><script src="../../crates.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg"><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../../llvm_kaleidoscope_rs/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../../brush.svg"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input"name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../../wheel.svg"></a></div></form></nav><section id="main" class="content"><div class="example-wrap"><pre class="line-numbers"><span id="1"> 1</span>
<span id="2"> 2</span>
<span id="3"> 3</span>
<span id="4"> 4</span>
@@ -135,10 +133,37 @@
<span id="133">133</span>
<span id="134">134</span>
<span id="135">135</span>
+<span id="136">136</span>
+<span id="137">137</span>
+<span id="138">138</span>
+<span id="139">139</span>
+<span id="140">140</span>
+<span id="141">141</span>
+<span id="142">142</span>
+<span id="143">143</span>
+<span id="144">144</span>
+<span id="145">145</span>
+<span id="146">146</span>
+<span id="147">147</span>
+<span id="148">148</span>
+<span id="149">149</span>
+<span id="150">150</span>
+<span id="151">151</span>
+<span id="152">152</span>
+<span id="153">153</span>
+<span id="154">154</span>
+<span id="155">155</span>
+<span id="156">156</span>
+<span id="157">157</span>
+<span id="158">158</span>
+<span id="159">159</span>
+<span id="160">160</span>
+<span id="161">161</span>
+<span id="162">162</span>
</pre><pre class="rust">
<span class="kw">use</span> <span class="ident">std::collections::HashMap</span>;
-<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::llvm</span>::{<span class="ident">Builder</span>, <span class="ident">FnValue</span>, <span class="ident">Module</span>, <span class="ident">Value</span>};
+<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::llvm</span>::{<span class="ident">FnValue</span>, <span class="ident">FunctionPassManager</span>, <span class="ident">IRBuilder</span>, <span class="ident">Module</span>, <span class="ident">Value</span>};
<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::parser</span>::{<span class="ident">ExprAST</span>, <span class="ident">FunctionAST</span>, <span class="ident">PrototypeAST</span>};
<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::Either</span>;
@@ -147,18 +172,23 @@
<span class="doccomment">/// Code generator from kaleidoscope AST to LLVM IR.</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Codegen</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span>, <span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="ident">module</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;llvm</span> <span class="ident">Module</span>,
- <span class="ident">builder</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="ident">Builder</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span>,
+ <span class="ident">builder</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="ident">IRBuilder</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span>,
+ <span class="ident">fpm</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="ident">FunctionPassManager</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span>,
+ <span class="ident">fn_protos</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="kw-2">mut</span> <span class="ident">HashMap</span><span class="op">&lt;</span><span class="ident">String</span>, <span class="ident">PrototypeAST</span><span class="op">&gt;</span>,
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span>, <span class="lifetime">&#39;a</span><span class="op">&gt;</span> <span class="ident">Codegen</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span>, <span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="doccomment">/// Compile either a [`PrototypeAST`] or a [`FunctionAST`] into the LLVM `module`.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">compile</span>(
<span class="ident">module</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;llvm</span> <span class="ident">Module</span>,
+ <span class="ident">fn_protos</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">HashMap</span><span class="op">&lt;</span><span class="ident">String</span>, <span class="ident">PrototypeAST</span><span class="op">&gt;</span>,
<span class="ident">compilee</span>: <span class="ident">Either</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="ident">PrototypeAST</span>, <span class="kw-2">&amp;</span><span class="ident">FunctionAST</span><span class="op">&gt;</span>,
) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">CodegenResult</span><span class="op">&lt;</span><span class="ident">FnValue</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span><span class="op">&gt;</span> {
- <span class="kw">let</span> <span class="ident">cg</span> <span class="op">=</span> <span class="ident">Codegen</span> {
+ <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cg</span> <span class="op">=</span> <span class="ident">Codegen</span> {
<span class="ident">module</span>,
- <span class="ident">builder</span>: <span class="kw-2">&amp;</span><span class="ident">Builder::with_ctx</span>(<span class="ident">module</span>),
+ <span class="ident">builder</span>: <span class="kw-2">&amp;</span><span class="ident">IRBuilder::with_ctx</span>(<span class="ident">module</span>),
+ <span class="ident">fpm</span>: <span class="kw-2">&amp;</span><span class="ident">FunctionPassManager::with_ctx</span>(<span class="ident">module</span>),
+ <span class="ident">fn_protos</span>,
};
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">variables</span> <span class="op">=</span> <span class="ident">HashMap::new</span>();
@@ -195,7 +225,7 @@
<span class="kw">_</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="string">&quot;invalid binary operator&quot;</span>.<span class="ident">into</span>()),
}
}
- <span class="ident">ExprAST::Call</span>(<span class="ident">callee</span>, <span class="ident">args</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">get_fn</span>(<span class="ident">callee</span>) {
+ <span class="ident">ExprAST::Call</span>(<span class="ident">callee</span>, <span class="ident">args</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">get_function</span>(<span class="ident">callee</span>) {
<span class="prelude-val">Some</span>(<span class="ident">callee</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="ident">callee</span>.<span class="ident">args</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">args</span>.<span class="ident">len</span>() {
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">&quot;Incorrect # arguments passed&quot;</span>.<span class="ident">into</span>());
@@ -235,14 +265,16 @@
}
<span class="kw">fn</span> <span class="ident">codegen_function</span>(
- <span class="kw-2">&amp;</span><span class="self">self</span>,
+ <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>,
<span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">body</span>): <span class="kw-2">&amp;</span><span class="ident">FunctionAST</span>,
<span class="ident">named_values</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">HashMap</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="lifetime">&#39;llvm</span> <span class="ident">str</span>, <span class="ident">Value</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span><span class="op">&gt;</span>,
) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">CodegenResult</span><span class="op">&lt;</span><span class="ident">FnValue</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span><span class="op">&gt;</span> {
- <span class="kw">let</span> <span class="ident">the_function</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">get_fn</span>(<span class="kw-2">&amp;</span><span class="ident">proto</span>.<span class="number">0</span>) {
- <span class="prelude-val">Some</span>(<span class="ident">f</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">f</span>,
- <span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="self">self</span>.<span class="ident">codegen_prototype</span>(<span class="ident">proto</span>),
- };
+ <span class="comment">// Insert the function prototype into the `fn_protos` map to keep track for re-generating</span>
+ <span class="comment">// declarations in other modules.</span>
+ <span class="self">self</span>.<span class="ident">fn_protos</span>.<span class="ident">insert</span>(<span class="ident">proto</span>.<span class="number">0</span>.<span class="ident">clone</span>(), <span class="ident">proto</span>.<span class="ident">clone</span>());
+
+ <span class="kw">let</span> <span class="ident">the_function</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">get_function</span>(<span class="kw-2">&amp;</span><span class="ident">proto</span>.<span class="number">0</span>)
+ .<span class="ident">expect</span>(<span class="string">&quot;If proto not already generated, get_function will do for us since we updated fn_protos before-hand!&quot;</span>);
<span class="kw">if</span> <span class="ident">the_function</span>.<span class="ident">basic_blocks</span>() <span class="op">&gt;</span> <span class="number">0</span> {
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">&quot;Function cannot be redefined.&quot;</span>.<span class="ident">into</span>());
@@ -265,11 +297,33 @@
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Ok</span>(<span class="ident">ret</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">body</span>, <span class="ident">named_values</span>) {
<span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">ret</span>(<span class="ident">ret</span>);
<span class="macro">assert!</span>(<span class="ident">the_function</span>.<span class="ident">verify</span>());
+
+ <span class="comment">// Run the optimization passes on the function.</span>
+ <span class="self">self</span>.<span class="ident">fpm</span>.<span class="ident">run</span>(<span class="ident">the_function</span>);
+
<span class="prelude-val">Ok</span>(<span class="ident">the_function</span>)
} <span class="kw">else</span> {
<span class="macro">todo!</span>(<span class="string">&quot;Failed to codegen function body, erase from module!&quot;</span>);
}
}
+
+ <span class="doccomment">/// Lookup function with `name` in the LLVM module and return the corresponding value reference.</span>
+ <span class="doccomment">/// If the function is not available in the module, check if the prototype is known and codegen</span>
+ <span class="doccomment">/// it.</span>
+ <span class="doccomment">/// Return [`None`] if the prototype is not known.</span>
+ <span class="kw">fn</span> <span class="ident">get_function</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">name</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">FnValue</span><span class="op">&lt;</span><span class="lifetime">&#39;llvm</span><span class="op">&gt;</span><span class="op">&gt;</span> {
+ <span class="kw">let</span> <span class="ident">callee</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">get_fn</span>(<span class="ident">name</span>) {
+ <span class="prelude-val">Some</span>(<span class="ident">callee</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">callee</span>,
+ <span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> {
+ <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">fn_protos</span>.<span class="ident">get</span>(<span class="ident">name</span>)<span class="question-mark">?</span>;
+ <span class="self">self</span>.<span class="ident">codegen_prototype</span>(<span class="ident">proto</span>)
+ }
+ };
+
+ <span class="prelude-val">Some</span>(<span class="ident">callee</span>)
+ }
}
</pre></div>
-</section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-search-index-js="../../search-index.js" data-search-js="../../search.js"></div><script src="../../main.js"></script><script src="../../source-script.js"></script><script src="../../source-files.js"></script></body></html> \ No newline at end of file
+</section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-search-index-js="../../search-index.js" data-search-js="../../search.js"></div>
+ <script src="../../main.js"></script><script src="../../source-script.js"></script><script src="../../source-files.js"></script>
+</body></html> \ No newline at end of file