diff options
author | johannst <johannst@users.noreply.github.com> | 2023-01-05 20:25:01 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2023-01-05 20:25:01 +0000 |
commit | 617f0d65b20b7b405b1acecea6f99ccd0a6d73f3 (patch) | |
tree | e6a099742adc94662482bd305e025d56ed6d3a24 /src/llvm_kaleidoscope_rs | |
parent | 73e25a571f12d3deaa6f4493a5b4792a9dae39eb (diff) | |
download | llvm-kaleidoscope-rs-617f0d65b20b7b405b1acecea6f99ccd0a6d73f3.tar.gz llvm-kaleidoscope-rs-617f0d65b20b7b405b1acecea6f99ccd0a6d73f3.zip |
deploy: 802cca1963bc27f8ea4e4923029909e45464d3df
Diffstat (limited to 'src/llvm_kaleidoscope_rs')
-rw-r--r-- | src/llvm_kaleidoscope_rs/codegen.rs.html | 521 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/lexer.rs.html | 307 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/lib.rs.html | 175 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/basic_block.rs.html | 69 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/builder.rs.html | 423 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/lljit.rs.html | 217 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/mod.rs.html | 115 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/module.rs.html | 325 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/pass_manager.rs.html | 115 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/type_.rs.html | 95 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/llvm/value.rs.html | 361 | ||||
-rw-r--r-- | src/llvm_kaleidoscope_rs/parser.rs.html | 915 |
12 files changed, 1777 insertions, 1861 deletions
diff --git a/src/llvm_kaleidoscope_rs/codegen.rs.html b/src/llvm_kaleidoscope_rs/codegen.rs.html index 44ee848..f802910 100644 --- a/src/llvm_kaleidoscope_rs/codegen.rs.html +++ b/src/llvm_kaleidoscope_rs/codegen.rs.html @@ -1,10 +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="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/codegen.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>codegen.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -326,328 +320,327 @@ <span id="320">320</span> <span id="321">321</span> <span id="322">322</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">std::collections::HashMap</span>; +</pre><pre class="rust"><code><span class="kw">use </span>std::collections::HashMap; -<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::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="ident"><span class="kw">crate</span>::parser</span>::{<span class="ident">ExprAST</span>, <span class="ident">FunctionAST</span>, <span class="ident">PrototypeAST</span>}; -<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::Either</span>; +<span class="kw">use </span><span class="kw">crate</span>::llvm::{FnValue, FunctionPassManager, IRBuilder, Module, Value}; +<span class="kw">use </span><span class="kw">crate</span>::parser::{ExprAST, FunctionAST, PrototypeAST}; +<span class="kw">use </span><span class="kw">crate</span>::Either; -<span class="kw">type</span> <span class="ident">CodegenResult</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="op">=</span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">T</span>, <span class="ident">String</span><span class="op">></span>; +<span class="kw">type </span>CodegenResult<T> = <span class="prelude-ty">Result</span><T, String>; -<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"><</span><span class="lifetime">'llvm</span>, <span class="lifetime">'a</span><span class="op">></span> { - <span class="ident">module</span>: <span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="ident">Module</span>, - <span class="ident">builder</span>: <span class="kw-2">&</span><span class="lifetime">'a</span> <span class="ident">IRBuilder</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, - <span class="ident">fpm</span>: <span class="kw-2">&</span><span class="lifetime">'a</span> <span class="ident">FunctionPassManager</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, - <span class="ident">fn_protos</span>: <span class="kw-2">&</span><span class="lifetime">'a</span> <span class="kw-2">mut</span> <span class="ident">HashMap</span><span class="op"><</span><span class="ident">String</span>, <span class="ident">PrototypeAST</span><span class="op">></span>, +<span class="doccomment">/// Code generator from kaleidoscope AST to LLVM IR. +</span><span class="kw">pub struct </span>Codegen<<span class="lifetime">'llvm</span>, <span class="lifetime">'a</span>> { + module: <span class="kw-2">&</span><span class="lifetime">'llvm </span>Module, + builder: <span class="kw-2">&</span><span class="lifetime">'a </span>IRBuilder<<span class="lifetime">'llvm</span>>, + fpm: <span class="kw-2">&</span><span class="lifetime">'a </span>FunctionPassManager<<span class="lifetime">'llvm</span>>, + fn_protos: <span class="kw-2">&</span><span class="lifetime">'a </span><span class="kw-2">mut </span>HashMap<String, PrototypeAST>, } -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span>, <span class="lifetime">'a</span><span class="op">></span> <span class="ident">Codegen</span><span class="op"><</span><span class="lifetime">'llvm</span>, <span class="lifetime">'a</span><span class="op">></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">&</span><span class="lifetime">'llvm</span> <span class="ident">Module</span>, - <span class="ident">fn_protos</span>: <span class="kw-2">&mut</span> <span class="ident">HashMap</span><span class="op"><</span><span class="ident">String</span>, <span class="ident">PrototypeAST</span><span class="op">></span>, - <span class="ident">compilee</span>: <span class="ident">Either</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">PrototypeAST</span>, <span class="kw-2">&</span><span class="ident">FunctionAST</span><span class="op">></span>, - ) -> <span class="ident">CodegenResult</span><span class="op"><</span><span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></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">&</span><span class="ident">IRBuilder::with_ctx</span>(<span class="ident">module</span>), - <span class="ident">fpm</span>: <span class="kw-2">&</span><span class="ident">FunctionPassManager::with_ctx</span>(<span class="ident">module</span>), - <span class="ident">fn_protos</span>, +<span class="kw">impl</span><<span class="lifetime">'llvm</span>, <span class="lifetime">'a</span>> Codegen<<span class="lifetime">'llvm</span>, <span class="lifetime">'a</span>> { + <span class="doccomment">/// Compile either a [`PrototypeAST`] or a [`FunctionAST`] into the LLVM `module`. + </span><span class="kw">pub fn </span>compile( + module: <span class="kw-2">&</span><span class="lifetime">'llvm </span>Module, + fn_protos: <span class="kw-2">&mut </span>HashMap<String, PrototypeAST>, + compilee: Either<<span class="kw-2">&</span>PrototypeAST, <span class="kw-2">&</span>FunctionAST>, + ) -> CodegenResult<FnValue<<span class="lifetime">'llvm</span>>> { + <span class="kw">let </span><span class="kw-2">mut </span>cg = Codegen { + module, + builder: <span class="kw-2">&</span>IRBuilder::with_ctx(module), + fpm: <span class="kw-2">&</span>FunctionPassManager::with_ctx(module), + fn_protos, }; - <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>(); + <span class="kw">let </span><span class="kw-2">mut </span>variables = HashMap::new(); - <span class="kw">match</span> <span class="ident">compilee</span> { - <span class="ident">Either::A</span>(<span class="ident">proto</span>) => <span class="prelude-val">Ok</span>(<span class="ident">cg</span>.<span class="ident">codegen_prototype</span>(<span class="ident">proto</span>)), - <span class="ident">Either::B</span>(<span class="ident">func</span>) => <span class="ident">cg</span>.<span class="ident">codegen_function</span>(<span class="ident">func</span>, <span class="kw-2">&mut</span> <span class="ident">variables</span>), + <span class="kw">match </span>compilee { + Either::A(proto) => <span class="prelude-val">Ok</span>(cg.codegen_prototype(proto)), + Either::B(func) => cg.codegen_function(func, <span class="kw-2">&mut </span>variables), } } - <span class="kw">fn</span> <span class="ident">codegen_expr</span>( + <span class="kw">fn </span>codegen_expr( <span class="kw-2">&</span><span class="self">self</span>, - <span class="ident">expr</span>: <span class="kw-2">&</span><span class="ident">ExprAST</span>, - <span class="ident">named_values</span>: <span class="kw-2">&mut</span> <span class="ident">HashMap</span><span class="op"><</span><span class="ident">String</span>, <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></span>, - ) -> <span class="ident">CodegenResult</span><span class="op"><</span><span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></span> { - <span class="kw">match</span> <span class="ident">expr</span> { - <span class="ident">ExprAST::Number</span>(<span class="ident">num</span>) => <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>().<span class="ident">const_f64</span>(<span class="kw-2">*</span><span class="ident">num</span>)), - <span class="ident">ExprAST::Variable</span>(<span class="ident">name</span>) => <span class="kw">match</span> <span class="ident">named_values</span>.<span class="ident">get</span>(<span class="ident">name</span>.<span class="ident">as_str</span>()) { - <span class="prelude-val">Some</span>(<span class="ident">value</span>) => <span class="prelude-val">Ok</span>(<span class="kw-2">*</span><span class="ident">value</span>), - <span class="prelude-val">None</span> => <span class="prelude-val">Err</span>(<span class="string">"Unknown variable name"</span>.<span class="ident">into</span>()), + expr: <span class="kw-2">&</span>ExprAST, + named_values: <span class="kw-2">&mut </span>HashMap<String, Value<<span class="lifetime">'llvm</span>>>, + ) -> CodegenResult<Value<<span class="lifetime">'llvm</span>>> { + <span class="kw">match </span>expr { + ExprAST::Number(num) => <span class="prelude-val">Ok</span>(<span class="self">self</span>.module.type_f64().const_f64(<span class="kw-2">*</span>num)), + ExprAST::Variable(name) => <span class="kw">match </span>named_values.get(name.as_str()) { + <span class="prelude-val">Some</span>(value) => <span class="prelude-val">Ok</span>(<span class="kw-2">*</span>value), + <span class="prelude-val">None </span>=> <span class="prelude-val">Err</span>(<span class="string">"Unknown variable name"</span>.into()), }, - <span class="ident">ExprAST::Binary</span>(<span class="ident">binop</span>, <span class="ident">lhs</span>, <span class="ident">rhs</span>) => { - <span class="kw">let</span> <span class="ident">l</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">lhs</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">r</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">rhs</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - - <span class="kw">match</span> <span class="ident">binop</span> { - <span class="string">'+'</span> => <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">fadd</span>(<span class="ident">l</span>, <span class="ident">r</span>)), - <span class="string">'-'</span> => <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">fsub</span>(<span class="ident">l</span>, <span class="ident">r</span>)), - <span class="string">'*'</span> => <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">fmul</span>(<span class="ident">l</span>, <span class="ident">r</span>)), - <span class="string">'<'</span> => { - <span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">fcmpult</span>(<span class="ident">l</span>, <span class="ident">r</span>); - <span class="comment">// Turn bool into f64.</span> - <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">uitofp</span>(<span class="ident">res</span>, <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>())) + ExprAST::Binary(binop, lhs, rhs) => { + <span class="kw">let </span>l = <span class="self">self</span>.codegen_expr(lhs, named_values)<span class="question-mark">?</span>; + <span class="kw">let </span>r = <span class="self">self</span>.codegen_expr(rhs, named_values)<span class="question-mark">?</span>; + + <span class="kw">match </span>binop { + <span class="string">'+' </span>=> <span class="prelude-val">Ok</span>(<span class="self">self</span>.builder.fadd(l, r)), + <span class="string">'-' </span>=> <span class="prelude-val">Ok</span>(<span class="self">self</span>.builder.fsub(l, r)), + <span class="string">'*' </span>=> <span class="prelude-val">Ok</span>(<span class="self">self</span>.builder.fmul(l, r)), + <span class="string">'<' </span>=> { + <span class="kw">let </span>res = <span class="self">self</span>.builder.fcmpult(l, r); + <span class="comment">// Turn bool into f64. + </span><span class="prelude-val">Ok</span>(<span class="self">self</span>.builder.uitofp(res, <span class="self">self</span>.module.type_f64())) } - <span class="kw">_</span> => <span class="prelude-val">Err</span>(<span class="string">"invalid binary operator"</span>.<span class="ident">into</span>()), + <span class="kw">_ </span>=> <span class="prelude-val">Err</span>(<span class="string">"invalid binary operator"</span>.into()), } } - <span class="ident">ExprAST::Call</span>(<span class="ident">callee</span>, <span class="ident">args</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="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">"Incorrect # arguments passed"</span>.<span class="ident">into</span>()); + ExprAST::Call(callee, args) => <span class="kw">match </span><span class="self">self</span>.get_function(callee) { + <span class="prelude-val">Some</span>(callee) => { + <span class="kw">if </span>callee.args() != args.len() { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Incorrect # arguments passed"</span>.into()); } - <span class="comment">// Generate code for function argument expressions.</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">args</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">Value</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span><span class="op">></span> <span class="op">=</span> <span class="ident">args</span> - .<span class="ident">iter</span>() - .<span class="ident">map</span>(<span class="op">|</span><span class="ident">arg</span><span class="op">|</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">arg</span>, <span class="ident">named_values</span>)) - .<span class="ident">collect</span>::<span class="op"><</span><span class="ident">CodegenResult</span><span class="op"><</span><span class="kw">_</span><span class="op">></span><span class="op">></span>()<span class="question-mark">?</span>; + <span class="comment">// Generate code for function argument expressions. + </span><span class="kw">let </span><span class="kw-2">mut </span>args: Vec<Value<<span class="lifetime">'_</span>>> = args + .iter() + .map(|arg| <span class="self">self</span>.codegen_expr(arg, named_values)) + .collect::<CodegenResult<<span class="kw">_</span>>>()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">call</span>(<span class="ident">callee</span>, <span class="kw-2">&mut</span> <span class="ident">args</span>)) + <span class="prelude-val">Ok</span>(<span class="self">self</span>.builder.call(callee, <span class="kw-2">&mut </span>args)) } - <span class="prelude-val">None</span> => <span class="prelude-val">Err</span>(<span class="string">"Unknown function referenced"</span>.<span class="ident">into</span>()), + <span class="prelude-val">None </span>=> <span class="prelude-val">Err</span>(<span class="string">"Unknown function referenced"</span>.into()), }, - <span class="ident">ExprAST::If</span> { <span class="ident">cond</span>, <span class="ident">then</span>, <span class="ident">else_</span> } => { - <span class="comment">// For 'if' expressions we are building the following CFG.</span> - <span class="comment">//</span> - <span class="comment">// ; cond</span> - <span class="comment">// br</span> - <span class="comment">// |</span> - <span class="comment">// +-----+------+</span> - <span class="comment">// v v</span> - <span class="comment">// ; then ; else</span> - <span class="comment">// | |</span> - <span class="comment">// +-----+------+</span> - <span class="comment">// v</span> - <span class="comment">// ; merge</span> - <span class="comment">// phi then, else</span> - <span class="comment">// ret phi</span> - - <span class="kw">let</span> <span class="ident">cond_v</span> <span class="op">=</span> { - <span class="comment">// Codgen 'cond' expression.</span> - <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">cond</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - <span class="comment">// Compare 'v' against '0' as 'one = ordered not equal'.</span> - <span class="self">self</span>.<span class="ident">builder</span> - .<span class="ident">fcmpone</span>(<span class="ident">v</span>, <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>().<span class="ident">const_f64</span>(<span class="number">0f64</span>)) + ExprAST::If { cond, then, else_ } => { + <span class="comment">// For 'if' expressions we are building the following CFG. + // + // ; cond + // br + // | + // +-----+------+ + // v v + // ; then ; else + // | | + // +-----+------+ + // v + // ; merge + // phi then, else + // ret phi + + </span><span class="kw">let </span>cond_v = { + <span class="comment">// Codgen 'cond' expression. + </span><span class="kw">let </span>v = <span class="self">self</span>.codegen_expr(cond, named_values)<span class="question-mark">?</span>; + <span class="comment">// Compare 'v' against '0' as 'one = ordered not equal'. + </span><span class="self">self</span>.builder + .fcmpone(v, <span class="self">self</span>.module.type_f64().const_f64(<span class="number">0f64</span>)) }; - <span class="comment">// Get the function we are currently inserting into.</span> - <span class="kw">let</span> <span class="ident">the_function</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">get_insert_block</span>().<span class="ident">get_parent</span>(); - - <span class="comment">// Create basic blocks for the 'then' / 'else' expressions as well as the return</span> - <span class="comment">// instruction ('merge').</span> - <span class="comment">//</span> - <span class="comment">// Append the 'then' basic block to the function, don't insert the 'else' and</span> - <span class="comment">// 'merge' basic blocks yet.</span> - <span class="kw">let</span> <span class="ident">then_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">append_basic_block</span>(<span class="ident">the_function</span>); - <span class="kw">let</span> <span class="ident">else_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">create_basic_block</span>(); - <span class="kw">let</span> <span class="ident">merge_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">create_basic_block</span>(); - - <span class="comment">// Create a conditional branch based on the result of the 'cond' expression.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">cond_br</span>(<span class="ident">cond_v</span>, <span class="ident">then_bb</span>, <span class="ident">else_bb</span>); - - <span class="comment">// Move to 'then' basic block and codgen the 'then' expression.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">pos_at_end</span>(<span class="ident">then_bb</span>); - <span class="kw">let</span> <span class="ident">then_v</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">then</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - <span class="comment">// Create unconditional branch to 'merge' block.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">br</span>(<span class="ident">merge_bb</span>); - <span class="comment">// Update reference to current basic block (in case the 'then' expression added new</span> - <span class="comment">// basic blocks).</span> - <span class="kw">let</span> <span class="ident">then_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">get_insert_block</span>(); - - <span class="comment">// Now append the 'else' basic block to the function.</span> - <span class="ident">the_function</span>.<span class="ident">append_basic_block</span>(<span class="ident">else_bb</span>); - <span class="comment">// Move to 'else' basic block and codgen the 'else' expression.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">pos_at_end</span>(<span class="ident">else_bb</span>); - <span class="kw">let</span> <span class="ident">else_v</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">else_</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - <span class="comment">// Create unconditional branch to 'merge' block.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">br</span>(<span class="ident">merge_bb</span>); - <span class="comment">// Update reference to current basic block (in case the 'else' expression added new</span> - <span class="comment">// basic blocks).</span> - <span class="kw">let</span> <span class="ident">else_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">get_insert_block</span>(); - - <span class="comment">// Now append the 'merge' basic block to the function.</span> - <span class="ident">the_function</span>.<span class="ident">append_basic_block</span>(<span class="ident">merge_bb</span>); - <span class="comment">// Move to 'merge' basic block.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">pos_at_end</span>(<span class="ident">merge_bb</span>); - <span class="comment">// Codegen the phi node returning the appropriate value depending on the branch</span> - <span class="comment">// condition.</span> - <span class="kw">let</span> <span class="ident">phi</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">phi</span>( - <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>(), - <span class="kw-2">&</span>[(<span class="ident">then_v</span>, <span class="ident">then_bb</span>), (<span class="ident">else_v</span>, <span class="ident">else_bb</span>)], + <span class="comment">// Get the function we are currently inserting into. + </span><span class="kw">let </span>the_function = <span class="self">self</span>.builder.get_insert_block().get_parent(); + + <span class="comment">// Create basic blocks for the 'then' / 'else' expressions as well as the return + // instruction ('merge'). + // + // Append the 'then' basic block to the function, don't insert the 'else' and + // 'merge' basic blocks yet. + </span><span class="kw">let </span>then_bb = <span class="self">self</span>.module.append_basic_block(the_function); + <span class="kw">let </span>else_bb = <span class="self">self</span>.module.create_basic_block(); + <span class="kw">let </span>merge_bb = <span class="self">self</span>.module.create_basic_block(); + + <span class="comment">// Create a conditional branch based on the result of the 'cond' expression. + </span><span class="self">self</span>.builder.cond_br(cond_v, then_bb, else_bb); + + <span class="comment">// Move to 'then' basic block and codgen the 'then' expression. + </span><span class="self">self</span>.builder.pos_at_end(then_bb); + <span class="kw">let </span>then_v = <span class="self">self</span>.codegen_expr(then, named_values)<span class="question-mark">?</span>; + <span class="comment">// Create unconditional branch to 'merge' block. + </span><span class="self">self</span>.builder.br(merge_bb); + <span class="comment">// Update reference to current basic block (in case the 'then' expression added new + // basic blocks). + </span><span class="kw">let </span>then_bb = <span class="self">self</span>.builder.get_insert_block(); + + <span class="comment">// Now append the 'else' basic block to the function. + </span>the_function.append_basic_block(else_bb); + <span class="comment">// Move to 'else' basic block and codgen the 'else' expression. + </span><span class="self">self</span>.builder.pos_at_end(else_bb); + <span class="kw">let </span>else_v = <span class="self">self</span>.codegen_expr(else_, named_values)<span class="question-mark">?</span>; + <span class="comment">// Create unconditional branch to 'merge' block. + </span><span class="self">self</span>.builder.br(merge_bb); + <span class="comment">// Update reference to current basic block (in case the 'else' expression added new + // basic blocks). + </span><span class="kw">let </span>else_bb = <span class="self">self</span>.builder.get_insert_block(); + + <span class="comment">// Now append the 'merge' basic block to the function. + </span>the_function.append_basic_block(merge_bb); + <span class="comment">// Move to 'merge' basic block. + </span><span class="self">self</span>.builder.pos_at_end(merge_bb); + <span class="comment">// Codegen the phi node returning the appropriate value depending on the branch + // condition. + </span><span class="kw">let </span>phi = <span class="self">self</span>.builder.phi( + <span class="self">self</span>.module.type_f64(), + <span class="kw-2">&</span>[(then_v, then_bb), (else_v, else_bb)], ); - <span class="prelude-val">Ok</span>(<span class="kw-2">*</span><span class="ident">phi</span>) + <span class="prelude-val">Ok</span>(<span class="kw-2">*</span>phi) } - <span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>, - <span class="ident">end</span>, - <span class="ident">step</span>, - <span class="ident">body</span>, + ExprAST::For { + var, + start, + end, + step, + body, } => { - <span class="comment">// For 'for' expression we build the following structure.</span> - <span class="comment">//</span> - <span class="comment">// entry:</span> - <span class="comment">// init = start expression</span> - <span class="comment">// br loop</span> - <span class="comment">// loop:</span> - <span class="comment">// i = phi [%init, %entry], [%new_i, %loop]</span> - <span class="comment">// ; loop body ...</span> - <span class="comment">// new_i = increment %i by step expression</span> - <span class="comment">// ; check end condition and branch</span> - <span class="comment">// end:</span> - - <span class="comment">// Compute initial value for the loop variable.</span> - <span class="kw">let</span> <span class="ident">start_val</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">start</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - - <span class="kw">let</span> <span class="ident">the_function</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">get_insert_block</span>().<span class="ident">get_parent</span>(); - <span class="comment">// Get current basic block (used in the loop variable phi node).</span> - <span class="kw">let</span> <span class="ident">entry_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">get_insert_block</span>(); - <span class="comment">// Add new basic block to emit loop body.</span> - <span class="kw">let</span> <span class="ident">loop_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">append_basic_block</span>(<span class="ident">the_function</span>); - - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">br</span>(<span class="ident">loop_bb</span>); - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">pos_at_end</span>(<span class="ident">loop_bb</span>); - - <span class="comment">// Build phi not to pick loop variable in case we come from the 'entry' block.</span> - <span class="comment">// Which is the case when we enter the loop for the first time.</span> - <span class="comment">// We will add another incoming value once we computed the updated loop variable</span> - <span class="comment">// below.</span> - <span class="kw">let</span> <span class="ident">variable</span> <span class="op">=</span> <span class="self">self</span> - .<span class="ident">builder</span> - .<span class="ident">phi</span>(<span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>(), <span class="kw-2">&</span>[(<span class="ident">start_val</span>, <span class="ident">entry_bb</span>)]); - - <span class="comment">// Insert the loop variable into the named values map that it can be referenced</span> - <span class="comment">// from the body as well as the end condition.</span> - <span class="comment">// In case the loop variable shadows an existing variable remember the shared one.</span> - <span class="kw">let</span> <span class="ident">old_val</span> <span class="op">=</span> <span class="ident">named_values</span>.<span class="ident">insert</span>(<span class="ident">var</span>.<span class="ident">into</span>(), <span class="kw-2">*</span><span class="ident">variable</span>); - - <span class="comment">// Generate the loop body.</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="question-mark">?</span>; - - <span class="comment">// Generate step value expression if available else use '1'.</span> - <span class="kw">let</span> <span class="ident">step_val</span> <span class="op">=</span> <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">step</span>) <span class="op">=</span> <span class="ident">step</span> { - <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">step</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span> - } <span class="kw">else</span> { - <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>().<span class="ident">const_f64</span>(<span class="number">1f64</span>) + <span class="comment">// For 'for' expression we build the following structure. + // + // entry: + // init = start expression + // br loop + // loop: + // i = phi [%init, %entry], [%new_i, %loop] + // ; loop body ... + // new_i = increment %i by step expression + // ; check end condition and branch + // end: + + // Compute initial value for the loop variable. + </span><span class="kw">let </span>start_val = <span class="self">self</span>.codegen_expr(start, named_values)<span class="question-mark">?</span>; + + <span class="kw">let </span>the_function = <span class="self">self</span>.builder.get_insert_block().get_parent(); + <span class="comment">// Get current basic block (used in the loop variable phi node). + </span><span class="kw">let </span>entry_bb = <span class="self">self</span>.builder.get_insert_block(); + <span class="comment">// Add new basic block to emit loop body. + </span><span class="kw">let </span>loop_bb = <span class="self">self</span>.module.append_basic_block(the_function); + + <span class="self">self</span>.builder.br(loop_bb); + <span class="self">self</span>.builder.pos_at_end(loop_bb); + + <span class="comment">// Build phi not to pick loop variable in case we come from the 'entry' block. + // Which is the case when we enter the loop for the first time. + // We will add another incoming value once we computed the updated loop variable + // below. + </span><span class="kw">let </span>variable = <span class="self">self + </span>.builder + .phi(<span class="self">self</span>.module.type_f64(), <span class="kw-2">&</span>[(start_val, entry_bb)]); + + <span class="comment">// Insert the loop variable into the named values map that it can be referenced + // from the body as well as the end condition. + // In case the loop variable shadows an existing variable remember the shared one. + </span><span class="kw">let </span>old_val = named_values.insert(var.into(), <span class="kw-2">*</span>variable); + + <span class="comment">// Generate the loop body. + </span><span class="self">self</span>.codegen_expr(body, named_values)<span class="question-mark">?</span>; + + <span class="comment">// Generate step value expression if available else use '1'. + </span><span class="kw">let </span>step_val = <span class="kw">if let </span><span class="prelude-val">Some</span>(step) = step { + <span class="self">self</span>.codegen_expr(step, named_values)<span class="question-mark">? + </span>} <span class="kw">else </span>{ + <span class="self">self</span>.module.type_f64().const_f64(<span class="number">1f64</span>) }; - <span class="comment">// Increment loop variable.</span> - <span class="kw">let</span> <span class="ident">next_var</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">fadd</span>(<span class="kw-2">*</span><span class="ident">variable</span>, <span class="ident">step_val</span>); + <span class="comment">// Increment loop variable. + </span><span class="kw">let </span>next_var = <span class="self">self</span>.builder.fadd(<span class="kw-2">*</span>variable, step_val); - <span class="comment">// Generate the loop end condition.</span> - <span class="kw">let</span> <span class="ident">end_cond</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">codegen_expr</span>(<span class="ident">end</span>, <span class="ident">named_values</span>)<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">end_cond</span> <span class="op">=</span> <span class="self">self</span> - .<span class="ident">builder</span> - .<span class="ident">fcmpone</span>(<span class="ident">end_cond</span>, <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>().<span class="ident">const_f64</span>(<span class="number">0f64</span>)); + <span class="comment">// Generate the loop end condition. + </span><span class="kw">let </span>end_cond = <span class="self">self</span>.codegen_expr(end, named_values)<span class="question-mark">?</span>; + <span class="kw">let </span>end_cond = <span class="self">self + </span>.builder + .fcmpone(end_cond, <span class="self">self</span>.module.type_f64().const_f64(<span class="number">0f64</span>)); - <span class="comment">// Get current basic block.</span> - <span class="kw">let</span> <span class="ident">loop_end_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">get_insert_block</span>(); - <span class="comment">// Add new basic block following the loop.</span> - <span class="kw">let</span> <span class="ident">after_bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">append_basic_block</span>(<span class="ident">the_function</span>); + <span class="comment">// Get current basic block. + </span><span class="kw">let </span>loop_end_bb = <span class="self">self</span>.builder.get_insert_block(); + <span class="comment">// Add new basic block following the loop. + </span><span class="kw">let </span>after_bb = <span class="self">self</span>.module.append_basic_block(the_function); - <span class="comment">// Register additional incoming value for the loop variable. This will choose the</span> - <span class="comment">// updated loop variable if we are iterating in the loop.</span> - <span class="ident">variable</span>.<span class="ident">add_incoming</span>(<span class="ident">next_var</span>, <span class="ident">loop_end_bb</span>); + <span class="comment">// Register additional incoming value for the loop variable. This will choose the + // updated loop variable if we are iterating in the loop. + </span>variable.add_incoming(next_var, loop_end_bb); - <span class="comment">// Branch depending on the loop end condition.</span> - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">cond_br</span>(<span class="ident">end_cond</span>, <span class="ident">loop_bb</span>, <span class="ident">after_bb</span>); + <span class="comment">// Branch depending on the loop end condition. + </span><span class="self">self</span>.builder.cond_br(end_cond, loop_bb, after_bb); - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">pos_at_end</span>(<span class="ident">after_bb</span>); + <span class="self">self</span>.builder.pos_at_end(after_bb); - <span class="comment">// Restore the shadowed variable if there was one.</span> - <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">old_val</span>) <span class="op">=</span> <span class="ident">old_val</span> { - <span class="comment">// We inserted 'var' above so it must exist.</span> - <span class="kw-2">*</span><span class="ident">named_values</span>.<span class="ident">get_mut</span>(<span class="ident">var</span>).<span class="ident">unwrap</span>() <span class="op">=</span> <span class="ident">old_val</span>; - } <span class="kw">else</span> { - <span class="ident">named_values</span>.<span class="ident">remove</span>(<span class="ident">var</span>); + <span class="comment">// Restore the shadowed variable if there was one. + </span><span class="kw">if let </span><span class="prelude-val">Some</span>(old_val) = old_val { + <span class="comment">// We inserted 'var' above so it must exist. + </span><span class="kw-2">*</span>named_values.get_mut(var).unwrap() = old_val; + } <span class="kw">else </span>{ + named_values.remove(var); } - <span class="comment">// Loops just always return 0.</span> - <span class="prelude-val">Ok</span>(<span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>().<span class="ident">const_f64</span>(<span class="number">0f64</span>)) + <span class="comment">// Loops just always return 0. + </span><span class="prelude-val">Ok</span>(<span class="self">self</span>.module.type_f64().const_f64(<span class="number">0f64</span>)) } } } - <span class="kw">fn</span> <span class="ident">codegen_prototype</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">PrototypeAST</span>(<span class="ident">name</span>, <span class="ident">args</span>): <span class="kw-2">&</span><span class="ident">PrototypeAST</span>) -> <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">type_f64</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_f64</span>(); + <span class="kw">fn </span>codegen_prototype(<span class="kw-2">&</span><span class="self">self</span>, PrototypeAST(name, args): <span class="kw-2">&</span>PrototypeAST) -> FnValue<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>type_f64 = <span class="self">self</span>.module.type_f64(); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">doubles</span> <span class="op">=</span> <span class="ident">Vec::new</span>(); - <span class="ident">doubles</span>.<span class="ident">resize</span>(<span class="ident">args</span>.<span class="ident">len</span>(), <span class="ident">type_f64</span>); + <span class="kw">let </span><span class="kw-2">mut </span>doubles = Vec::new(); + doubles.resize(args.len(), type_f64); - <span class="comment">// Build the function type: fn(f64, f64, ..) -> f64</span> - <span class="kw">let</span> <span class="ident">ft</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">type_fn</span>(<span class="kw-2">&mut</span> <span class="ident">doubles</span>, <span class="ident">type_f64</span>); + <span class="comment">// Build the function type: fn(f64, f64, ..) -> f64 + </span><span class="kw">let </span>ft = <span class="self">self</span>.module.type_fn(<span class="kw-2">&mut </span>doubles, type_f64); - <span class="comment">// Create the function declaration.</span> - <span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">add_fn</span>(<span class="ident">name</span>, <span class="ident">ft</span>); + <span class="comment">// Create the function declaration. + </span><span class="kw">let </span>f = <span class="self">self</span>.module.add_fn(name, ft); - <span class="comment">// Set the names of the function arguments.</span> - <span class="kw">for</span> <span class="ident">idx</span> <span class="kw">in</span> <span class="number">0</span>..<span class="ident">f</span>.<span class="ident">args</span>() { - <span class="ident">f</span>.<span class="ident">arg</span>(<span class="ident">idx</span>).<span class="ident">set_name</span>(<span class="kw-2">&</span><span class="ident">args</span>[<span class="ident">idx</span>]); + <span class="comment">// Set the names of the function arguments. + </span><span class="kw">for </span>idx <span class="kw">in </span><span class="number">0</span>..f.args() { + f.arg(idx).set_name(<span class="kw-2">&</span>args[idx]); } - <span class="ident">f</span> + f } - <span class="kw">fn</span> <span class="ident">codegen_function</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">&</span><span class="ident">FunctionAST</span>, - <span class="ident">named_values</span>: <span class="kw-2">&mut</span> <span class="ident">HashMap</span><span class="op"><</span><span class="ident">String</span>, <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></span>, - ) -> <span class="ident">CodegenResult</span><span class="op"><</span><span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></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">fn </span>codegen_function( + <span class="kw-2">&mut </span><span class="self">self</span>, + FunctionAST(proto, body): <span class="kw-2">&</span>FunctionAST, + named_values: <span class="kw-2">&mut </span>HashMap<String, Value<<span class="lifetime">'llvm</span>>>, + ) -> CodegenResult<FnValue<<span class="lifetime">'llvm</span>>> { + <span class="comment">// Insert the function prototype into the `fn_protos` map to keep track for re-generating + // declarations in other modules. + </span><span class="self">self</span>.fn_protos.insert(proto.<span class="number">0</span>.clone(), proto.clone()); - <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">&</span><span class="ident">proto</span>.<span class="number">0</span>) - .<span class="ident">expect</span>(<span class="string">"If proto not already generated, get_function will do for us since we updated fn_protos before-hand!"</span>); + <span class="kw">let </span>the_function = <span class="self">self</span>.get_function(<span class="kw-2">&</span>proto.<span class="number">0</span>) + .expect(<span class="string">"If proto not already generated, get_function will do for us since we updated fn_protos before-hand!"</span>); - <span class="kw">if</span> <span class="ident">the_function</span>.<span class="ident">basic_blocks</span>() <span class="op">></span> <span class="number">0</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Function cannot be redefined."</span>.<span class="ident">into</span>()); + <span class="kw">if </span>the_function.basic_blocks() > <span class="number">0 </span>{ + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Function cannot be redefined."</span>.into()); } - <span class="comment">// Create entry basic block to insert code.</span> - <span class="kw">let</span> <span class="ident">bb</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">module</span>.<span class="ident">append_basic_block</span>(<span class="ident">the_function</span>); - <span class="self">self</span>.<span class="ident">builder</span>.<span class="ident">pos_at_end</span>(<span class="ident">bb</span>); + <span class="comment">// Create entry basic block to insert code. + </span><span class="kw">let </span>bb = <span class="self">self</span>.module.append_basic_block(the_function); + <span class="self">self</span>.builder.pos_at_end(bb); - <span class="comment">// New scope, clear the map with the function args.</span> - <span class="ident">named_values</span>.<span class="ident">clear</span>(); + <span class="comment">// New scope, clear the map with the function args. + </span>named_values.clear(); - <span class="comment">// Update the map with the current functions args.</span> - <span class="kw">for</span> <span class="ident">idx</span> <span class="kw">in</span> <span class="number">0</span>..<span class="ident">the_function</span>.<span class="ident">args</span>() { - <span class="kw">let</span> <span class="ident">arg</span> <span class="op">=</span> <span class="ident">the_function</span>.<span class="ident">arg</span>(<span class="ident">idx</span>); - <span class="ident">named_values</span>.<span class="ident">insert</span>(<span class="ident">arg</span>.<span class="ident">get_name</span>().<span class="ident">into</span>(), <span class="ident">arg</span>); + <span class="comment">// Update the map with the current functions args. + </span><span class="kw">for </span>idx <span class="kw">in </span><span class="number">0</span>..the_function.args() { + <span class="kw">let </span>arg = the_function.arg(idx); + named_values.insert(arg.get_name().into(), arg); } - <span class="comment">// Codegen function body.</span> - <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">// Codegen function body. + </span><span class="kw">if let </span><span class="prelude-val">Ok</span>(ret) = <span class="self">self</span>.codegen_expr(body, named_values) { + <span class="self">self</span>.builder.ret(ret); + <span class="macro">assert!</span>(the_function.verify()); - <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="comment">// Run the optimization passes on the function. + </span><span class="self">self</span>.fpm.run(the_function); - <span class="prelude-val">Ok</span>(<span class="ident">the_function</span>) - } <span class="kw">else</span> { + <span class="prelude-val">Ok</span>(the_function) + } <span class="kw">else </span>{ <span class="macro">todo!</span>(<span class="string">"Failed to codegen function body, erase from module!"</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">&</span><span class="self">self</span>, <span class="ident">name</span>: <span class="kw-2">&</span><span class="ident">str</span>) -> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></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="ident">callee</span>, - <span class="prelude-val">None</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="doccomment">/// Lookup function with `name` in the LLVM module and return the corresponding value reference. + /// If the function is not available in the module, check if the prototype is known and codegen + /// it. + /// Return [`None`] if the prototype is not known. + </span><span class="kw">fn </span>get_function(<span class="kw-2">&</span><span class="self">self</span>, name: <span class="kw-2">&</span>str) -> <span class="prelude-ty">Option</span><FnValue<<span class="lifetime">'llvm</span>>> { + <span class="kw">let </span>callee = <span class="kw">match </span><span class="self">self</span>.module.get_fn(name) { + <span class="prelude-val">Some</span>(callee) => callee, + <span class="prelude-val">None </span>=> { + <span class="kw">let </span>proto = <span class="self">self</span>.fn_protos.get(name)<span class="question-mark">?</span>; + <span class="self">self</span>.codegen_prototype(proto) } }; - <span class="prelude-val">Some</span>(<span class="ident">callee</span>) + <span class="prelude-val">Some</span>(callee) } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/lexer.rs.html b/src/llvm_kaleidoscope_rs/lexer.rs.html index 2249ba0..240080c 100644 --- a/src/llvm_kaleidoscope_rs/lexer.rs.html +++ b/src/llvm_kaleidoscope_rs/lexer.rs.html @@ -1,10 +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/lexer.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lexer.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/lexer.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lexer.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -210,212 +204,211 @@ <span id="204">204</span> <span id="205">205</span> <span id="206">206</span> -</pre><pre class="rust"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span> -<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">Token</span> { - <span class="ident">Eof</span>, - <span class="ident">Def</span>, - <span class="ident">Extern</span>, - <span class="ident">Identifier</span>(<span class="ident">String</span>), - <span class="ident">Number</span>(<span class="ident">f64</span>), - <span class="ident">Char</span>(<span class="ident">char</span>), - <span class="ident">If</span>, - <span class="ident">Then</span>, - <span class="ident">Else</span>, - <span class="ident">For</span>, - <span class="ident">In</span>, +</pre><pre class="rust"><code><span class="attribute">#[derive(Debug, PartialEq)] +</span><span class="kw">pub enum </span>Token { + Eof, + Def, + Extern, + Identifier(String), + Number(f64), + Char(char), + If, + Then, + Else, + For, + In, } -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> -<span class="kw">where</span> - <span class="ident">I</span>: <span class="ident">Iterator</span><span class="op"><</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">char</span><span class="op">></span>, +<span class="kw">pub struct </span>Lexer<I> +<span class="kw">where + </span>I: Iterator<Item = char>, { - <span class="ident">input</span>: <span class="ident">I</span>, - <span class="ident">last_char</span>: <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">char</span><span class="op">></span>, + input: I, + last_char: <span class="prelude-ty">Option</span><char>, } -<span class="kw">impl</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> -<span class="kw">where</span> - <span class="ident">I</span>: <span class="ident">Iterator</span><span class="op"><</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">char</span><span class="op">></span>, +<span class="kw">impl</span><I> Lexer<I> +<span class="kw">where + </span>I: Iterator<Item = char>, { - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span>(<span class="kw-2">mut</span> <span class="ident">input</span>: <span class="ident">I</span>) -> <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">last_char</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">next</span>(); - <span class="ident">Lexer</span> { <span class="ident">input</span>, <span class="ident">last_char</span> } + <span class="kw">pub fn </span>new(<span class="kw-2">mut </span>input: I) -> Lexer<I> { + <span class="kw">let </span>last_char = input.next(); + Lexer { input, last_char } } - <span class="kw">fn</span> <span class="ident">step</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">char</span><span class="op">></span> { - <span class="self">self</span>.<span class="ident">last_char</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">input</span>.<span class="ident">next</span>(); - <span class="self">self</span>.<span class="ident">last_char</span> + <span class="kw">fn </span>step(<span class="kw-2">&mut </span><span class="self">self</span>) -> <span class="prelude-ty">Option</span><char> { + <span class="self">self</span>.last_char = <span class="self">self</span>.input.next(); + <span class="self">self</span>.last_char } - <span class="doccomment">/// Lex and return the next token.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `int gettok();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">gettok</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">Token</span> { - <span class="comment">// Eat up whitespaces.</span> - <span class="kw">while</span> <span class="macro">matches!</span>(<span class="self">self</span>.<span class="ident">last_char</span>, <span class="prelude-val">Some</span>(<span class="ident">c</span>) <span class="kw">if</span> <span class="ident">c</span>.<span class="ident">is_ascii_whitespace</span>()) { - <span class="self">self</span>.<span class="ident">step</span>(); + <span class="doccomment">/// Lex and return the next token. + /// + /// Implement `int gettok();` from the tutorial. + </span><span class="kw">pub fn </span>gettok(<span class="kw-2">&mut </span><span class="self">self</span>) -> Token { + <span class="comment">// Eat up whitespaces. + </span><span class="kw">while </span><span class="macro">matches!</span>(<span class="self">self</span>.last_char, <span class="prelude-val">Some</span>(c) <span class="kw">if </span>c.is_ascii_whitespace()) { + <span class="self">self</span>.step(); } - <span class="comment">// Unpack last char or return EOF.</span> - <span class="kw">let</span> <span class="ident">last_char</span> <span class="op">=</span> <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">c</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">last_char</span> { - <span class="ident">c</span> - } <span class="kw">else</span> { - <span class="kw">return</span> <span class="ident">Token::Eof</span>; + <span class="comment">// Unpack last char or return EOF. + </span><span class="kw">let </span>last_char = <span class="kw">if let </span><span class="prelude-val">Some</span>(c) = <span class="self">self</span>.last_char { + c + } <span class="kw">else </span>{ + <span class="kw">return </span>Token::Eof; }; - <span class="comment">// Identifier: [a-zA-Z][a-zA-Z0-9]*</span> - <span class="kw">if</span> <span class="ident">last_char</span>.<span class="ident">is_ascii_alphabetic</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">ident</span> <span class="op">=</span> <span class="ident">String::new</span>(); - <span class="ident">ident</span>.<span class="ident">push</span>(<span class="ident">last_char</span>); + <span class="comment">// Identifier: [a-zA-Z][a-zA-Z0-9]* + </span><span class="kw">if </span>last_char.is_ascii_alphabetic() { + <span class="kw">let </span><span class="kw-2">mut </span>ident = String::new(); + ident.push(last_char); - <span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">c</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">step</span>() { - <span class="kw">if</span> <span class="ident">c</span>.<span class="ident">is_ascii_alphanumeric</span>() { - <span class="ident">ident</span>.<span class="ident">push</span>(<span class="ident">c</span>) - } <span class="kw">else</span> { + <span class="kw">while let </span><span class="prelude-val">Some</span>(c) = <span class="self">self</span>.step() { + <span class="kw">if </span>c.is_ascii_alphanumeric() { + ident.push(c) + } <span class="kw">else </span>{ <span class="kw">break</span>; } } - <span class="kw">match</span> <span class="ident">ident</span>.<span class="ident">as_ref</span>() { - <span class="string">"def"</span> => <span class="kw">return</span> <span class="ident">Token::Def</span>, - <span class="string">"extern"</span> => <span class="kw">return</span> <span class="ident">Token::Extern</span>, - <span class="string">"if"</span> => <span class="kw">return</span> <span class="ident">Token::If</span>, - <span class="string">"then"</span> => <span class="kw">return</span> <span class="ident">Token::Then</span>, - <span class="string">"else"</span> => <span class="kw">return</span> <span class="ident">Token::Else</span>, - <span class="string">"for"</span> => <span class="kw">return</span> <span class="ident">Token::For</span>, - <span class="string">"in"</span> => <span class="kw">return</span> <span class="ident">Token::In</span>, - <span class="kw">_</span> => {} + <span class="kw">match </span>ident.as_ref() { + <span class="string">"def" </span>=> <span class="kw">return </span>Token::Def, + <span class="string">"extern" </span>=> <span class="kw">return </span>Token::Extern, + <span class="string">"if" </span>=> <span class="kw">return </span>Token::If, + <span class="string">"then" </span>=> <span class="kw">return </span>Token::Then, + <span class="string">"else" </span>=> <span class="kw">return </span>Token::Else, + <span class="string">"for" </span>=> <span class="kw">return </span>Token::For, + <span class="string">"in" </span>=> <span class="kw">return </span>Token::In, + <span class="kw">_ </span>=> {} } - <span class="kw">return</span> <span class="ident">Token::Identifier</span>(<span class="ident">ident</span>); + <span class="kw">return </span>Token::Identifier(ident); } - <span class="comment">// Number: [0-9.]+</span> - <span class="kw">if</span> <span class="ident">last_char</span>.<span class="ident">is_ascii_digit</span>() <span class="op">|</span><span class="op">|</span> <span class="ident">last_char</span> <span class="op">==</span> <span class="string">'.'</span> { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">num</span> <span class="op">=</span> <span class="ident">String::new</span>(); - <span class="ident">num</span>.<span class="ident">push</span>(<span class="ident">last_char</span>); + <span class="comment">// Number: [0-9.]+ + </span><span class="kw">if </span>last_char.is_ascii_digit() || last_char == <span class="string">'.' </span>{ + <span class="kw">let </span><span class="kw-2">mut </span>num = String::new(); + num.push(last_char); - <span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">c</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">step</span>() { - <span class="kw">if</span> <span class="ident">c</span>.<span class="ident">is_ascii_digit</span>() <span class="op">|</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'.'</span> { - <span class="ident">num</span>.<span class="ident">push</span>(<span class="ident">c</span>) - } <span class="kw">else</span> { + <span class="kw">while let </span><span class="prelude-val">Some</span>(c) = <span class="self">self</span>.step() { + <span class="kw">if </span>c.is_ascii_digit() || c == <span class="string">'.' </span>{ + num.push(c) + } <span class="kw">else </span>{ <span class="kw">break</span>; } } - <span class="kw">let</span> <span class="ident">num</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">num</span>.<span class="ident">parse</span>().<span class="ident">unwrap_or_default</span>(); - <span class="kw">return</span> <span class="ident">Token::Number</span>(<span class="ident">num</span>); + <span class="kw">let </span>num: f64 = num.parse().unwrap_or_default(); + <span class="kw">return </span>Token::Number(num); } - <span class="comment">// Eat up comment.</span> - <span class="kw">if</span> <span class="ident">last_char</span> <span class="op">==</span> <span class="string">'#'</span> { - <span class="kw">loop</span> { - <span class="kw">match</span> <span class="self">self</span>.<span class="ident">step</span>() { - <span class="prelude-val">Some</span>(<span class="ident">c</span>) <span class="kw">if</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'\r'</span> <span class="op">|</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'\n'</span> => <span class="kw">return</span> <span class="self">self</span>.<span class="ident">gettok</span>(), - <span class="prelude-val">None</span> => <span class="kw">return</span> <span class="ident">Token::Eof</span>, - <span class="kw">_</span> => { <span class="comment">/* consume comment */</span> } + <span class="comment">// Eat up comment. + </span><span class="kw">if </span>last_char == <span class="string">'#' </span>{ + <span class="kw">loop </span>{ + <span class="kw">match </span><span class="self">self</span>.step() { + <span class="prelude-val">Some</span>(c) <span class="kw">if </span>c == <span class="string">'\r' </span>|| c == <span class="string">'\n' </span>=> <span class="kw">return </span><span class="self">self</span>.gettok(), + <span class="prelude-val">None </span>=> <span class="kw">return </span>Token::Eof, + <span class="kw">_ </span>=> { <span class="comment">/* consume comment */ </span>} } } } - <span class="comment">// Advance last char and return currently last char.</span> - <span class="self">self</span>.<span class="ident">step</span>(); - <span class="ident">Token::Char</span>(<span class="ident">last_char</span>) + <span class="comment">// Advance last char and return currently last char. + </span><span class="self">self</span>.step(); + Token::Char(last_char) } } -<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span> -<span class="kw">mod</span> <span class="ident">test</span> { - <span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">Lexer</span>, <span class="ident">Token</span>}; +<span class="attribute">#[cfg(test)] +</span><span class="kw">mod </span>test { + <span class="kw">use super</span>::{Lexer, Token}; - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_identifier</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"a b c"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"a"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"b"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"c"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_identifier() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"a b c"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"a"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"b"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"c"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_keyword</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"def extern"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Def</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Extern</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_keyword() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"def extern"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Def, lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Extern, lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_number</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"12.34"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Number</span>(<span class="number">12.34f64</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_number() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"12.34"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Number(<span class="number">12.34f64</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">" 1.0 2.0 3.0"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Number</span>(<span class="number">1.0f64</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Number</span>(<span class="number">2.0f64</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Number</span>(<span class="number">3.0f64</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">" 1.0 2.0 3.0"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Number(<span class="number">1.0f64</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Number(<span class="number">2.0f64</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Number(<span class="number">3.0f64</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"12.34.56"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Number</span>(<span class="number">0f64</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"12.34.56"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Number(<span class="number">0f64</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_comment</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"# some comment"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_comment() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"# some comment"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"abc # some comment \n xyz"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"abc"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"xyz"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"abc # some comment \n xyz"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"abc"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"xyz"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_chars</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"a+b-c"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"a"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Char</span>(<span class="string">'+'</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"b"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Char</span>(<span class="string">'-'</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"c"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_chars() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"a+b-c"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"a"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Char(<span class="string">'+'</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"b"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Char(<span class="string">'-'</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"c"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_whitespaces</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">" +a b c! "</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Char</span>(<span class="string">'+'</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"a"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"b"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"c"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Char</span>(<span class="string">'!'</span>), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_whitespaces() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">" +a b c! "</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Char(<span class="string">'+'</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"a"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"b"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"c"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Char(<span class="string">'!'</span>), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"\n a \n\r b \r \n c \r\r \n "</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"a"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"b"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Identifier</span>(<span class="string">"c"</span>.<span class="ident">into</span>()), <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Eof</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"\n a \n\r b \r \n c \r\r \n "</span>.chars()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"a"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"b"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Identifier(<span class="string">"c"</span>.into()), lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Eof, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_ite</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"if then else"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::If</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Then</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::Else</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_ite() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"if then else"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::If, lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Then, lex.gettok()); + <span class="macro">assert_eq!</span>(Token::Else, lex.gettok()); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_for</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lex</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="string">"for in"</span>.<span class="ident">chars</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::For</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); - <span class="macro">assert_eq!</span>(<span class="ident">Token::In</span>, <span class="ident">lex</span>.<span class="ident">gettok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_for() { + <span class="kw">let </span><span class="kw-2">mut </span>lex = Lexer::new(<span class="string">"for in"</span>.chars()); + <span class="macro">assert_eq!</span>(Token::For, lex.gettok()); + <span class="macro">assert_eq!</span>(Token::In, lex.gettok()); } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/lib.rs.html b/src/llvm_kaleidoscope_rs/lib.rs.html index 97ad5f7..acd8915 100644 --- a/src/llvm_kaleidoscope_rs/lib.rs.html +++ b/src/llvm_kaleidoscope_rs/lib.rs.html @@ -1,10 +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/lib.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lib.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/lib.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lib.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -115,117 +109,116 @@ <span id="109">109</span> <span id="110">110</span> <span id="111">111</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">std::convert::TryFrom</span>; - -<span class="kw">pub</span> <span class="kw">mod</span> <span class="ident">codegen</span>; -<span class="kw">pub</span> <span class="kw">mod</span> <span class="ident">lexer</span>; -<span class="kw">pub</span> <span class="kw">mod</span> <span class="ident">llvm</span>; -<span class="kw">pub</span> <span class="kw">mod</span> <span class="ident">parser</span>; - -<span class="doccomment">/// Fixed size of [`SmallCStr`] including the trailing `\0` byte.</span> -<span class="kw">pub</span> <span class="kw">const</span> <span class="ident">SMALL_STR_SIZE</span>: <span class="ident">usize</span> <span class="op">=</span> <span class="number">16</span>; - -<span class="doccomment">/// Small C string on the stack with fixed size [`SMALL_STR_SIZE`].</span> -<span class="doccomment">///</span> -<span class="doccomment">/// This is specially crafted to interact with the LLVM C API and get rid of some heap allocations.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">SmallCStr</span>([<span class="ident">u8</span>; <span class="ident">SMALL_STR_SIZE</span>]); - -<span class="kw">impl</span> <span class="ident">SmallCStr</span> { - <span class="doccomment">/// Create a new C string from `src`.</span> - <span class="doccomment">/// Returns [`None`] if `src` exceeds the fixed size or contains any `\0` bytes.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">AsRef</span><span class="op"><</span>[<span class="ident">u8</span>]<span class="op">></span><span class="op">></span>(<span class="ident">src</span>: <span class="kw-2">&</span><span class="ident">T</span>) -> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">SmallCStr</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">as_ref</span>(); - <span class="kw">let</span> <span class="ident">len</span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">len</span>(); - - <span class="comment">// Check for \0 bytes.</span> - <span class="kw">let</span> <span class="ident">contains_null</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="op">!</span><span class="ident">libc::memchr</span>(<span class="ident">src</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), <span class="number">0</span>, <span class="ident">len</span>).<span class="ident">is_null</span>() }; - - <span class="kw">if</span> <span class="ident">contains_null</span> <span class="op">|</span><span class="op">|</span> <span class="ident">len</span> <span class="op">></span> <span class="ident">SMALL_STR_SIZE</span> <span class="op">-</span> <span class="number">1</span> { - <span class="prelude-val">None</span> - } <span class="kw">else</span> { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">dest</span> <span class="op">=</span> [<span class="number">0</span>; <span class="ident">SMALL_STR_SIZE</span>]; - <span class="ident">dest</span>[..<span class="ident">len</span>].<span class="ident">copy_from_slice</span>(<span class="ident">src</span>); - <span class="prelude-val">Some</span>(<span class="ident">SmallCStr</span>(<span class="ident">dest</span>)) +</pre><pre class="rust"><code><span class="kw">use </span>std::convert::TryFrom; + +<span class="kw">pub mod </span>codegen; +<span class="kw">pub mod </span>lexer; +<span class="kw">pub mod </span>llvm; +<span class="kw">pub mod </span>parser; + +<span class="doccomment">/// Fixed size of [`SmallCStr`] including the trailing `\0` byte. +</span><span class="kw">pub const </span>SMALL_STR_SIZE: usize = <span class="number">16</span>; + +<span class="doccomment">/// Small C string on the stack with fixed size [`SMALL_STR_SIZE`]. +/// +/// This is specially crafted to interact with the LLVM C API and get rid of some heap allocations. +</span><span class="attribute">#[derive(Debug, PartialEq)] +</span><span class="kw">pub struct </span>SmallCStr([u8; SMALL_STR_SIZE]); + +<span class="kw">impl </span>SmallCStr { + <span class="doccomment">/// Create a new C string from `src`. + /// Returns [`None`] if `src` exceeds the fixed size or contains any `\0` bytes. + </span><span class="kw">pub fn </span>new<T: AsRef<[u8]>>(src: <span class="kw-2">&</span>T) -> <span class="prelude-ty">Option</span><SmallCStr> { + <span class="kw">let </span>src = src.as_ref(); + <span class="kw">let </span>len = src.len(); + + <span class="comment">// Check for \0 bytes. + </span><span class="kw">let </span>contains_null = <span class="kw">unsafe </span>{ !libc::memchr(src.as_ptr().cast(), <span class="number">0</span>, len).is_null() }; + + <span class="kw">if </span>contains_null || len > SMALL_STR_SIZE - <span class="number">1 </span>{ + <span class="prelude-val">None + </span>} <span class="kw">else </span>{ + <span class="kw">let </span><span class="kw-2">mut </span>dest = [<span class="number">0</span>; SMALL_STR_SIZE]; + dest[..len].copy_from_slice(src); + <span class="prelude-val">Some</span>(SmallCStr(dest)) } } - <span class="doccomment">/// Return pointer to C string.</span> - <span class="kw">pub</span> <span class="kw">const</span> <span class="kw">fn</span> <span class="ident">as_ptr</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">*const</span> <span class="ident">libc::c_char</span> { - <span class="self">self</span>.<span class="number">0</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>() + <span class="doccomment">/// Return pointer to C string. + </span><span class="kw">pub const fn </span>as_ptr(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">*const </span>libc::c_char { + <span class="self">self</span>.<span class="number">0</span>.as_ptr().cast() } } -<span class="kw">impl</span> <span class="ident">TryFrom</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="kw">for</span> <span class="ident">SmallCStr</span> { - <span class="kw">type</span> <span class="ident">Error</span> <span class="op">=</span> (); +<span class="kw">impl </span>TryFrom<<span class="kw-2">&</span>str> <span class="kw">for </span>SmallCStr { + <span class="kw">type </span>Error = (); - <span class="kw">fn</span> <span class="ident">try_from</span>(<span class="ident">value</span>: <span class="kw-2">&</span><span class="ident">str</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="self">Self</span>, <span class="ident"><span class="self">Self</span>::Error</span><span class="op">></span> { - <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">value</span>).<span class="ident">ok_or</span>(()) + <span class="kw">fn </span>try_from(value: <span class="kw-2">&</span>str) -> <span class="prelude-ty">Result</span><<span class="self">Self</span>, <span class="self">Self</span>::Error> { + SmallCStr::new(<span class="kw-2">&</span>value).ok_or(()) } } -<span class="doccomment">/// Either type, for APIs accepting two types.</span> -<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">Either</span><span class="op"><</span><span class="ident">A</span>, <span class="ident">B</span><span class="op">></span> { - <span class="ident">A</span>(<span class="ident">A</span>), - <span class="ident">B</span>(<span class="ident">B</span>), +<span class="doccomment">/// Either type, for APIs accepting two types. +</span><span class="kw">pub enum </span>Either<A, B> { + A(A), + B(B), } -<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span> -<span class="kw">mod</span> <span class="ident">test</span> { - <span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">SmallCStr</span>, <span class="ident">SMALL_STR_SIZE</span>}; - <span class="kw">use</span> <span class="ident">std::convert::TryInto</span>; +<span class="attribute">#[cfg(test)] +</span><span class="kw">mod </span>test { + <span class="kw">use super</span>::{SmallCStr, SMALL_STR_SIZE}; + <span class="kw">use </span>std::convert::TryInto; - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_create</span>() { - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> <span class="string">"\x30\x31\x32\x33"</span>; - <span class="kw">let</span> <span class="ident">scs</span> <span class="op">=</span> <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">src</span>).<span class="ident">unwrap</span>(); - <span class="macro">assert_eq!</span>(<span class="kw-2">&</span><span class="ident">scs</span>.<span class="number">0</span>[..<span class="number">5</span>], <span class="kw-2">&</span>[<span class="number">0x30</span>, <span class="number">0x31</span>, <span class="number">0x32</span>, <span class="number">0x33</span>, <span class="number">0x00</span>]); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_create() { + <span class="kw">let </span>src = <span class="string">"\x30\x31\x32\x33"</span>; + <span class="kw">let </span>scs = SmallCStr::new(<span class="kw-2">&</span>src).unwrap(); + <span class="macro">assert_eq!</span>(<span class="kw-2">&</span>scs.<span class="number">0</span>[..<span class="number">5</span>], <span class="kw-2">&</span>[<span class="number">0x30</span>, <span class="number">0x31</span>, <span class="number">0x32</span>, <span class="number">0x33</span>, <span class="number">0x00</span>]); - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> <span class="string">b"abcd1234"</span>; - <span class="kw">let</span> <span class="ident">scs</span> <span class="op">=</span> <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">src</span>).<span class="ident">unwrap</span>(); + <span class="kw">let </span>src = <span class="string">b"abcd1234"</span>; + <span class="kw">let </span>scs = SmallCStr::new(<span class="kw-2">&</span>src).unwrap(); <span class="macro">assert_eq!</span>( - <span class="kw-2">&</span><span class="ident">scs</span>.<span class="number">0</span>[..<span class="number">9</span>], + <span class="kw-2">&</span>scs.<span class="number">0</span>[..<span class="number">9</span>], <span class="kw-2">&</span>[<span class="number">0x61</span>, <span class="number">0x62</span>, <span class="number">0x63</span>, <span class="number">0x64</span>, <span class="number">0x31</span>, <span class="number">0x32</span>, <span class="number">0x33</span>, <span class="number">0x34</span>, <span class="number">0x00</span>] ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_contain_null</span>() { - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> <span class="string">"\x30\x00\x32\x33"</span>; - <span class="kw">let</span> <span class="ident">scs</span> <span class="op">=</span> <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">src</span>); - <span class="macro">assert_eq!</span>(<span class="ident">scs</span>, <span class="prelude-val">None</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_contain_null() { + <span class="kw">let </span>src = <span class="string">"\x30\x00\x32\x33"</span>; + <span class="kw">let </span>scs = SmallCStr::new(<span class="kw-2">&</span>src); + <span class="macro">assert_eq!</span>(scs, <span class="prelude-val">None</span>); - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> <span class="string">"\x30\x31\x32\x33\x00"</span>; - <span class="kw">let</span> <span class="ident">scs</span> <span class="op">=</span> <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">src</span>); - <span class="macro">assert_eq!</span>(<span class="ident">scs</span>, <span class="prelude-val">None</span>); + <span class="kw">let </span>src = <span class="string">"\x30\x31\x32\x33\x00"</span>; + <span class="kw">let </span>scs = SmallCStr::new(<span class="kw-2">&</span>src); + <span class="macro">assert_eq!</span>(scs, <span class="prelude-val">None</span>); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_too_large</span>() { - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> (<span class="number">0</span>..<span class="ident">SMALL_STR_SIZE</span>).<span class="ident">map</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="string">'a'</span>).<span class="ident">collect</span>::<span class="op"><</span><span class="ident">String</span><span class="op">></span>(); - <span class="kw">let</span> <span class="ident">scs</span> <span class="op">=</span> <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">src</span>); - <span class="macro">assert_eq!</span>(<span class="ident">scs</span>, <span class="prelude-val">None</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_too_large() { + <span class="kw">let </span>src = (<span class="number">0</span>..SMALL_STR_SIZE).map(|<span class="kw">_</span>| <span class="string">'a'</span>).collect::<String>(); + <span class="kw">let </span>scs = SmallCStr::new(<span class="kw-2">&</span>src); + <span class="macro">assert_eq!</span>(scs, <span class="prelude-val">None</span>); - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> (<span class="number">0</span>..<span class="ident">SMALL_STR_SIZE</span> <span class="op">+</span> <span class="number">10</span>).<span class="ident">map</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="string">'a'</span>).<span class="ident">collect</span>::<span class="op"><</span><span class="ident">String</span><span class="op">></span>(); - <span class="kw">let</span> <span class="ident">scs</span> <span class="op">=</span> <span class="ident">SmallCStr::new</span>(<span class="kw-2">&</span><span class="ident">src</span>); - <span class="macro">assert_eq!</span>(<span class="ident">scs</span>, <span class="prelude-val">None</span>); + <span class="kw">let </span>src = (<span class="number">0</span>..SMALL_STR_SIZE + <span class="number">10</span>).map(|<span class="kw">_</span>| <span class="string">'a'</span>).collect::<String>(); + <span class="kw">let </span>scs = SmallCStr::new(<span class="kw-2">&</span>src); + <span class="macro">assert_eq!</span>(scs, <span class="prelude-val">None</span>); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">test_try_into</span>() { - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> <span class="string">"\x30\x31\x32\x33"</span>; - <span class="kw">let</span> <span class="ident">scs</span>: <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">SmallCStr</span>, ()<span class="op">></span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">try_into</span>(); - <span class="macro">assert!</span>(<span class="ident">scs</span>.<span class="ident">is_ok</span>()); + <span class="attribute">#[test] + </span><span class="kw">fn </span>test_try_into() { + <span class="kw">let </span>src = <span class="string">"\x30\x31\x32\x33"</span>; + <span class="kw">let </span>scs: <span class="prelude-ty">Result</span><SmallCStr, ()> = src.try_into(); + <span class="macro">assert!</span>(scs.is_ok()); - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> (<span class="number">0</span>..<span class="ident">SMALL_STR_SIZE</span>).<span class="ident">map</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="string">'a'</span>).<span class="ident">collect</span>::<span class="op"><</span><span class="ident">String</span><span class="op">></span>(); - <span class="kw">let</span> <span class="ident">scs</span>: <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">SmallCStr</span>, ()<span class="op">></span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">as_str</span>().<span class="ident">try_into</span>(); - <span class="macro">assert!</span>(<span class="ident">scs</span>.<span class="ident">is_err</span>()); + <span class="kw">let </span>src = (<span class="number">0</span>..SMALL_STR_SIZE).map(|<span class="kw">_</span>| <span class="string">'a'</span>).collect::<String>(); + <span class="kw">let </span>scs: <span class="prelude-ty">Result</span><SmallCStr, ()> = src.as_str().try_into(); + <span class="macro">assert!</span>(scs.is_err()); - <span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> (<span class="number">0</span>..<span class="ident">SMALL_STR_SIZE</span> <span class="op">+</span> <span class="number">10</span>).<span class="ident">map</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="string">'a'</span>).<span class="ident">collect</span>::<span class="op"><</span><span class="ident">String</span><span class="op">></span>(); - <span class="kw">let</span> <span class="ident">scs</span>: <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">SmallCStr</span>, ()<span class="op">></span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">as_str</span>().<span class="ident">try_into</span>(); - <span class="macro">assert!</span>(<span class="ident">scs</span>.<span class="ident">is_err</span>()); + <span class="kw">let </span>src = (<span class="number">0</span>..SMALL_STR_SIZE + <span class="number">10</span>).map(|<span class="kw">_</span>| <span class="string">'a'</span>).collect::<String>(); + <span class="kw">let </span>scs: <span class="prelude-ty">Result</span><SmallCStr, ()> = src.as_str().try_into(); + <span class="macro">assert!</span>(scs.is_err()); } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/basic_block.rs.html b/src/llvm_kaleidoscope_rs/llvm/basic_block.rs.html index 581b8e2..2370022 100644 --- a/src/llvm_kaleidoscope_rs/llvm/basic_block.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/basic_block.rs.html @@ -1,10 +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/llvm/basic_block.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>basic_block.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/basic_block.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>basic_block.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -43,45 +37,44 @@ <span id="37">37</span> <span id="38">38</span> <span id="39">39</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">llvm_sys</span>::{<span class="ident">core::LLVMGetBasicBlockParent</span>, <span class="ident">prelude::LLVMBasicBlockRef</span>}; +</pre><pre class="rust"><code><span class="kw">use </span>llvm_sys::{core::LLVMGetBasicBlockParent, prelude::LLVMBasicBlockRef}; -<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>; +<span class="kw">use </span>std::marker::PhantomData; -<span class="kw">use</span> <span class="ident"><span class="kw">super</span>::FnValue</span>; +<span class="kw">use </span><span class="kw">super</span>::FnValue; -<span class="doccomment">/// Wrapper for a LLVM Basic Block.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Copy</span>, <span class="ident">Clone</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>(<span class="ident">LLVMBasicBlockRef</span>, <span class="ident">PhantomData</span><span class="op"><</span><span class="kw-2">&</span><span class="lifetime">'llvm</span> ()<span class="op">></span>); +<span class="doccomment">/// Wrapper for a LLVM Basic Block. +</span><span class="attribute">#[derive(Copy, Clone)] +</span><span class="kw">pub struct </span>BasicBlock<<span class="lifetime">'llvm</span>>(LLVMBasicBlockRef, PhantomData<<span class="kw-2">&</span><span class="lifetime">'llvm </span>()>); -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new BasicBlock instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if `bb_ref` is a null pointer.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">bb_ref</span>: <span class="ident">LLVMBasicBlockRef</span>) -> <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">bb_ref</span>.<span class="ident">is_null</span>()); - <span class="ident">BasicBlock</span>(<span class="ident">bb_ref</span>, <span class="ident">PhantomData</span>) +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> BasicBlock<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new BasicBlock instance. + /// + /// # Panics + /// + /// Panics if `bb_ref` is a null pointer. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new(bb_ref: LLVMBasicBlockRef) -> BasicBlock<<span class="lifetime">'llvm</span>> { + <span class="macro">assert!</span>(!bb_ref.is_null()); + BasicBlock(bb_ref, PhantomData) } - <span class="doccomment">/// Get the raw LLVM value reference.</span> - <span class="attribute">#[<span class="ident">inline</span>]</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">bb_ref</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMBasicBlockRef</span> { - <span class="self">self</span>.<span class="number">0</span> - } + <span class="doccomment">/// Get the raw LLVM value reference. + </span><span class="attribute">#[inline] + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>bb_ref(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMBasicBlockRef { + <span class="self">self</span>.<span class="number">0 + </span>} - <span class="doccomment">/// Get the function to which the basic block belongs.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">get_parent</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMGetBasicBlockParent</span>(<span class="self">self</span>.<span class="ident">bb_ref</span>()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">value_ref</span>.<span class="ident">is_null</span>()); + <span class="doccomment">/// Get the function to which the basic block belongs. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>get_parent(<span class="kw-2">&</span><span class="self">self</span>) -> FnValue<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ LLVMGetBasicBlockParent(<span class="self">self</span>.bb_ref()) }; + <span class="macro">assert!</span>(!value_ref.is_null()); - <span class="ident">FnValue::new</span>(<span class="ident">value_ref</span>) + FnValue::new(value_ref) } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/builder.rs.html b/src/llvm_kaleidoscope_rs/llvm/builder.rs.html index 934351a..fad1867 100644 --- a/src/llvm_kaleidoscope_rs/llvm/builder.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/builder.rs.html @@ -1,10 +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/llvm/builder.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>builder.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/builder.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>builder.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -283,285 +277,284 @@ <span id="277">277</span> <span id="278">278</span> <span id="279">279</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">llvm_sys</span>::{ - <span class="ident">core</span>::{ - <span class="ident">LLVMAddIncoming</span>, <span class="ident">LLVMBuildBr</span>, <span class="ident">LLVMBuildCondBr</span>, <span class="ident">LLVMBuildFAdd</span>, <span class="ident">LLVMBuildFCmp</span>, <span class="ident">LLVMBuildFMul</span>, - <span class="ident">LLVMBuildFSub</span>, <span class="ident">LLVMBuildPhi</span>, <span class="ident">LLVMBuildRet</span>, <span class="ident">LLVMBuildUIToFP</span>, <span class="ident">LLVMCreateBuilderInContext</span>, - <span class="ident">LLVMDisposeBuilder</span>, <span class="ident">LLVMGetInsertBlock</span>, <span class="ident">LLVMPositionBuilderAtEnd</span>, +</pre><pre class="rust"><code><span class="kw">use </span>llvm_sys::{ + core::{ + LLVMAddIncoming, LLVMBuildBr, LLVMBuildCondBr, LLVMBuildFAdd, LLVMBuildFCmp, LLVMBuildFMul, + LLVMBuildFSub, LLVMBuildPhi, LLVMBuildRet, LLVMBuildUIToFP, LLVMCreateBuilderInContext, + LLVMDisposeBuilder, LLVMGetInsertBlock, LLVMPositionBuilderAtEnd, }, - <span class="ident">prelude</span>::{<span class="ident">LLVMBuilderRef</span>, <span class="ident">LLVMValueRef</span>}, - <span class="ident">LLVMRealPredicate</span>, + prelude::{LLVMBuilderRef, LLVMValueRef}, + LLVMRealPredicate, }; -<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>; +<span class="kw">use </span>std::marker::PhantomData; -<span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">BasicBlock</span>, <span class="ident">FnValue</span>, <span class="ident">Module</span>, <span class="ident">PhiValue</span>, <span class="ident">Type</span>, <span class="ident">Value</span>}; +<span class="kw">use super</span>::{BasicBlock, FnValue, Module, PhiValue, Type, Value}; -<span class="comment">// Definition of LLVM C API functions using our `repr(transparent)` types.</span> -<span class="kw">extern</span> <span class="string">"C"</span> { - <span class="kw">fn</span> <span class="ident">LLVMBuildCall2</span>( - <span class="ident">arg1</span>: <span class="ident">LLVMBuilderRef</span>, - <span class="ident">arg2</span>: <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span>, - <span class="ident">Fn</span>: <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span>, - <span class="ident">Args</span>: <span class="kw-2">*mut</span> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span>, - <span class="ident">NumArgs</span>: <span class="ident">::libc::c_uint</span>, - <span class="ident">Name</span>: <span class="kw-2">*const</span> <span class="ident">::libc::c_char</span>, - ) -> <span class="ident">LLVMValueRef</span>; +<span class="comment">// Definition of LLVM C API functions using our `repr(transparent)` types. +</span><span class="kw">extern </span><span class="string">"C" </span>{ + <span class="kw">fn </span>LLVMBuildCall2( + arg1: LLVMBuilderRef, + arg2: Type<<span class="lifetime">'_</span>>, + Fn: FnValue<<span class="lifetime">'_</span>>, + Args: <span class="kw-2">*mut </span>Value<<span class="lifetime">'_</span>>, + NumArgs: ::libc::c_uint, + Name: <span class="kw-2">*const </span>::libc::c_char, + ) -> LLVMValueRef; } -<span class="doccomment">/// Wrapper for a LLVM IR Builder.</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">IRBuilder</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="ident">builder</span>: <span class="ident">LLVMBuilderRef</span>, - <span class="ident">_ctx</span>: <span class="ident">PhantomData</span><span class="op"><</span><span class="kw-2">&</span><span class="lifetime">'llvm</span> ()<span class="op">></span>, +<span class="doccomment">/// Wrapper for a LLVM IR Builder. +</span><span class="kw">pub struct </span>IRBuilder<<span class="lifetime">'llvm</span>> { + builder: LLVMBuilderRef, + _ctx: PhantomData<<span class="kw-2">&</span><span class="lifetime">'llvm </span>()>, } -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">IRBuilder</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new LLVM IR Builder with the `module`s context.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if creating the IR Builder fails.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">with_ctx</span>(<span class="ident">module</span>: <span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="ident">Module</span>) -> <span class="ident">IRBuilder</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">builder</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMCreateBuilderInContext</span>(<span class="ident">module</span>.<span class="ident">ctx</span>()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">builder</span>.<span class="ident">is_null</span>()); +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> IRBuilder<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new LLVM IR Builder with the `module`s context. + /// + /// # Panics + /// + /// Panics if creating the IR Builder fails. + </span><span class="kw">pub fn </span>with_ctx(module: <span class="kw-2">&</span><span class="lifetime">'llvm </span>Module) -> IRBuilder<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>builder = <span class="kw">unsafe </span>{ LLVMCreateBuilderInContext(module.ctx()) }; + <span class="macro">assert!</span>(!builder.is_null()); - <span class="ident">IRBuilder</span> { - <span class="ident">builder</span>, - <span class="ident">_ctx</span>: <span class="ident">PhantomData</span>, + IRBuilder { + builder, + _ctx: PhantomData, } } - <span class="doccomment">/// Position the IR Builder at the end of the given Basic Block.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">pos_at_end</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">bb</span>: <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { - <span class="kw">unsafe</span> { - <span class="ident">LLVMPositionBuilderAtEnd</span>(<span class="self">self</span>.<span class="ident">builder</span>, <span class="ident">bb</span>.<span class="ident">bb_ref</span>()); + <span class="doccomment">/// Position the IR Builder at the end of the given Basic Block. + </span><span class="kw">pub fn </span>pos_at_end(<span class="kw-2">&</span><span class="self">self</span>, bb: BasicBlock<<span class="lifetime">'llvm</span>>) { + <span class="kw">unsafe </span>{ + LLVMPositionBuilderAtEnd(<span class="self">self</span>.builder, bb.bb_ref()); } } - <span class="doccomment">/// Get the BasicBlock the IRBuilder currently inputs into.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">get_insert_block</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">bb_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMGetInsertBlock</span>(<span class="self">self</span>.<span class="ident">builder</span>) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">bb_ref</span>.<span class="ident">is_null</span>()); + <span class="doccomment">/// Get the BasicBlock the IRBuilder currently inputs into. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>get_insert_block(<span class="kw-2">&</span><span class="self">self</span>) -> BasicBlock<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>bb_ref = <span class="kw">unsafe </span>{ LLVMGetInsertBlock(<span class="self">self</span>.builder) }; + <span class="macro">assert!</span>(!bb_ref.is_null()); - <span class="ident">BasicBlock::new</span>(<span class="ident">bb_ref</span>) + BasicBlock::new(bb_ref) } - <span class="doccomment">/// Emit a [fadd](https://llvm.org/docs/LangRef.html#fadd-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">fadd</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">lhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">rhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">debug_assert!</span>(<span class="ident">lhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fadd: Expected f64 as lhs operand!"</span>); - <span class="macro">debug_assert!</span>(<span class="ident">rhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fadd: Expected f64 as rhs operand!"</span>); + <span class="doccomment">/// Emit a [fadd](https://llvm.org/docs/LangRef.html#fadd-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>fadd(<span class="kw-2">&</span><span class="self">self</span>, lhs: Value<<span class="lifetime">'llvm</span>>, rhs: Value<<span class="lifetime">'llvm</span>>) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">debug_assert!</span>(lhs.is_f64(), <span class="string">"fadd: Expected f64 as lhs operand!"</span>); + <span class="macro">debug_assert!</span>(rhs.is_f64(), <span class="string">"fadd: Expected f64 as rhs operand!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildFAdd</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">lhs</span>.<span class="ident">value_ref</span>(), - <span class="ident">rhs</span>.<span class="ident">value_ref</span>(), - <span class="string">b"fadd\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildFAdd( + <span class="self">self</span>.builder, + lhs.value_ref(), + rhs.value_ref(), + <span class="string">b"fadd\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [fsub](https://llvm.org/docs/LangRef.html#fsub-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">fsub</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">lhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">rhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">debug_assert!</span>(<span class="ident">lhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fsub: Expected f64 as lhs operand!"</span>); - <span class="macro">debug_assert!</span>(<span class="ident">rhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fsub: Expected f64 as rhs operand!"</span>); + <span class="doccomment">/// Emit a [fsub](https://llvm.org/docs/LangRef.html#fsub-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>fsub(<span class="kw-2">&</span><span class="self">self</span>, lhs: Value<<span class="lifetime">'llvm</span>>, rhs: Value<<span class="lifetime">'llvm</span>>) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">debug_assert!</span>(lhs.is_f64(), <span class="string">"fsub: Expected f64 as lhs operand!"</span>); + <span class="macro">debug_assert!</span>(rhs.is_f64(), <span class="string">"fsub: Expected f64 as rhs operand!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildFSub</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">lhs</span>.<span class="ident">value_ref</span>(), - <span class="ident">rhs</span>.<span class="ident">value_ref</span>(), - <span class="string">b"fsub\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildFSub( + <span class="self">self</span>.builder, + lhs.value_ref(), + rhs.value_ref(), + <span class="string">b"fsub\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [fmul](https://llvm.org/docs/LangRef.html#fmul-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">fmul</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">lhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">rhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">debug_assert!</span>(<span class="ident">lhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fmul: Expected f64 as lhs operand!"</span>); - <span class="macro">debug_assert!</span>(<span class="ident">rhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fmul: Expected f64 as rhs operand!"</span>); + <span class="doccomment">/// Emit a [fmul](https://llvm.org/docs/LangRef.html#fmul-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>fmul(<span class="kw-2">&</span><span class="self">self</span>, lhs: Value<<span class="lifetime">'llvm</span>>, rhs: Value<<span class="lifetime">'llvm</span>>) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">debug_assert!</span>(lhs.is_f64(), <span class="string">"fmul: Expected f64 as lhs operand!"</span>); + <span class="macro">debug_assert!</span>(rhs.is_f64(), <span class="string">"fmul: Expected f64 as rhs operand!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildFMul</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">lhs</span>.<span class="ident">value_ref</span>(), - <span class="ident">rhs</span>.<span class="ident">value_ref</span>(), - <span class="string">b"fmul\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildFMul( + <span class="self">self</span>.builder, + lhs.value_ref(), + rhs.value_ref(), + <span class="string">b"fmul\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [fcmpult](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">fcmpult</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">lhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">rhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">debug_assert!</span>(<span class="ident">lhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fcmpult: Expected f64 as lhs operand!"</span>); - <span class="macro">debug_assert!</span>(<span class="ident">rhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fcmpult: Expected f64 as rhs operand!"</span>); + <span class="doccomment">/// Emit a [fcmpult](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>fcmpult(<span class="kw-2">&</span><span class="self">self</span>, lhs: Value<<span class="lifetime">'llvm</span>>, rhs: Value<<span class="lifetime">'llvm</span>>) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">debug_assert!</span>(lhs.is_f64(), <span class="string">"fcmpult: Expected f64 as lhs operand!"</span>); + <span class="macro">debug_assert!</span>(rhs.is_f64(), <span class="string">"fcmpult: Expected f64 as rhs operand!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildFCmp</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">LLVMRealPredicate::LLVMRealULT</span>, - <span class="ident">lhs</span>.<span class="ident">value_ref</span>(), - <span class="ident">rhs</span>.<span class="ident">value_ref</span>(), - <span class="string">b"fcmpult\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildFCmp( + <span class="self">self</span>.builder, + LLVMRealPredicate::LLVMRealULT, + lhs.value_ref(), + rhs.value_ref(), + <span class="string">b"fcmpult\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [fcmpone](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">fcmpone</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">lhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">rhs</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">debug_assert!</span>(<span class="ident">lhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fcmone: Expected f64 as lhs operand!"</span>); - <span class="macro">debug_assert!</span>(<span class="ident">rhs</span>.<span class="ident">is_f64</span>(), <span class="string">"fcmone: Expected f64 as rhs operand!"</span>); + <span class="doccomment">/// Emit a [fcmpone](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>fcmpone(<span class="kw-2">&</span><span class="self">self</span>, lhs: Value<<span class="lifetime">'llvm</span>>, rhs: Value<<span class="lifetime">'llvm</span>>) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">debug_assert!</span>(lhs.is_f64(), <span class="string">"fcmone: Expected f64 as lhs operand!"</span>); + <span class="macro">debug_assert!</span>(rhs.is_f64(), <span class="string">"fcmone: Expected f64 as rhs operand!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildFCmp</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">LLVMRealPredicate::LLVMRealONE</span>, - <span class="ident">lhs</span>.<span class="ident">value_ref</span>(), - <span class="ident">rhs</span>.<span class="ident">value_ref</span>(), - <span class="string">b"fcmpone\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildFCmp( + <span class="self">self</span>.builder, + LLVMRealPredicate::LLVMRealONE, + lhs.value_ref(), + rhs.value_ref(), + <span class="string">b"fcmpone\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [uitofp](https://llvm.org/docs/LangRef.html#uitofp-to-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">uitofp</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">val</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">dest_type</span>: <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">debug_assert!</span>(<span class="ident">val</span>.<span class="ident">is_int</span>(), <span class="string">"uitofp: Expected integer operand!"</span>); + <span class="doccomment">/// Emit a [uitofp](https://llvm.org/docs/LangRef.html#uitofp-to-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>uitofp(<span class="kw-2">&</span><span class="self">self</span>, val: Value<<span class="lifetime">'llvm</span>>, dest_type: Type<<span class="lifetime">'llvm</span>>) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">debug_assert!</span>(val.is_int(), <span class="string">"uitofp: Expected integer operand!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildUIToFP</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">val</span>.<span class="ident">value_ref</span>(), - <span class="ident">dest_type</span>.<span class="ident">type_ref</span>(), - <span class="string">b"uitofp\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildUIToFP( + <span class="self">self</span>.builder, + val.value_ref(), + dest_type.type_ref(), + <span class="string">b"uitofp\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [call](https://llvm.org/docs/LangRef.html#call-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">call</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">fn_value</span>: <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">args</span>: <span class="kw-2">&mut</span> [<span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>]) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildCall2</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">fn_value</span>.<span class="ident">ret_type</span>(), - <span class="ident">fn_value</span>, - <span class="ident">args</span>.<span class="ident">as_mut_ptr</span>(), - <span class="ident">args</span>.<span class="ident">len</span>() <span class="kw">as</span> <span class="ident">libc::c_uint</span>, - <span class="string">b"call\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="doccomment">/// Emit a [call](https://llvm.org/docs/LangRef.html#call-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>call(<span class="kw-2">&</span><span class="self">self</span>, fn_value: FnValue<<span class="lifetime">'llvm</span>>, args: <span class="kw-2">&mut </span>[Value<<span class="lifetime">'llvm</span>>]) -> Value<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ + LLVMBuildCall2( + <span class="self">self</span>.builder, + fn_value.ret_type(), + fn_value, + args.as_mut_ptr(), + args.len() <span class="kw">as </span>libc::c_uint, + <span class="string">b"call\0"</span>.as_ptr().cast(), ) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + Value::new(value_ref) } - <span class="doccomment">/// Emit a [ret](https://llvm.org/docs/LangRef.html#ret-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">ret</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">ret</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { - <span class="kw">let</span> <span class="ident">ret</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMBuildRet</span>(<span class="self">self</span>.<span class="ident">builder</span>, <span class="ident">ret</span>.<span class="ident">value_ref</span>()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">ret</span>.<span class="ident">is_null</span>()); + <span class="doccomment">/// Emit a [ret](https://llvm.org/docs/LangRef.html#ret-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>ret(<span class="kw-2">&</span><span class="self">self</span>, ret: Value<<span class="lifetime">'llvm</span>>) { + <span class="kw">let </span>ret = <span class="kw">unsafe </span>{ LLVMBuildRet(<span class="self">self</span>.builder, ret.value_ref()) }; + <span class="macro">assert!</span>(!ret.is_null()); } - <span class="doccomment">/// Emit an unconditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">br</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">dest</span>: <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { - <span class="kw">let</span> <span class="ident">br_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMBuildBr</span>(<span class="self">self</span>.<span class="ident">builder</span>, <span class="ident">dest</span>.<span class="ident">bb_ref</span>()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">br_ref</span>.<span class="ident">is_null</span>()); + <span class="doccomment">/// Emit an unconditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>br(<span class="kw-2">&</span><span class="self">self</span>, dest: BasicBlock<<span class="lifetime">'llvm</span>>) { + <span class="kw">let </span>br_ref = <span class="kw">unsafe </span>{ LLVMBuildBr(<span class="self">self</span>.builder, dest.bb_ref()) }; + <span class="macro">assert!</span>(!br_ref.is_null()); } - <span class="doccomment">/// Emit a conditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">cond_br</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">cond</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">then</span>: <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">else_</span>: <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { - <span class="kw">let</span> <span class="ident">br_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMBuildCondBr</span>( - <span class="self">self</span>.<span class="ident">builder</span>, - <span class="ident">cond</span>.<span class="ident">value_ref</span>(), - <span class="ident">then</span>.<span class="ident">bb_ref</span>(), - <span class="ident">else_</span>.<span class="ident">bb_ref</span>(), + <span class="doccomment">/// Emit a conditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>cond_br(<span class="kw-2">&</span><span class="self">self</span>, cond: Value<<span class="lifetime">'llvm</span>>, then: BasicBlock<<span class="lifetime">'llvm</span>>, else_: BasicBlock<<span class="lifetime">'llvm</span>>) { + <span class="kw">let </span>br_ref = <span class="kw">unsafe </span>{ + LLVMBuildCondBr( + <span class="self">self</span>.builder, + cond.value_ref(), + then.bb_ref(), + else_.bb_ref(), ) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">br_ref</span>.<span class="ident">is_null</span>()); + <span class="macro">assert!</span>(!br_ref.is_null()); } - <span class="doccomment">/// Emit a [phi](https://llvm.org/docs/LangRef.html#phi-instruction) instruction.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">phi</span>( + <span class="doccomment">/// Emit a [phi](https://llvm.org/docs/LangRef.html#phi-instruction) instruction. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>phi( <span class="kw-2">&</span><span class="self">self</span>, - <span class="ident">phi_type</span>: <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, - <span class="ident">incoming</span>: <span class="kw-2">&</span>[(<span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>)], - ) -> <span class="ident">PhiValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">phi_ref</span> <span class="op">=</span> - <span class="kw">unsafe</span> { <span class="ident">LLVMBuildPhi</span>(<span class="self">self</span>.<span class="ident">builder</span>, <span class="ident">phi_type</span>.<span class="ident">type_ref</span>(), <span class="string">b"phi\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">phi_ref</span>.<span class="ident">is_null</span>()); + phi_type: Type<<span class="lifetime">'llvm</span>>, + incoming: <span class="kw-2">&</span>[(Value<<span class="lifetime">'llvm</span>>, BasicBlock<<span class="lifetime">'llvm</span>>)], + ) -> PhiValue<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>phi_ref = + <span class="kw">unsafe </span>{ LLVMBuildPhi(<span class="self">self</span>.builder, phi_type.type_ref(), <span class="string">b"phi\0"</span>.as_ptr().cast()) }; + <span class="macro">assert!</span>(!phi_ref.is_null()); - <span class="kw">for</span> (<span class="ident">val</span>, <span class="ident">bb</span>) <span class="kw">in</span> <span class="ident">incoming</span> { + <span class="kw">for </span>(val, bb) <span class="kw">in </span>incoming { <span class="macro">debug_assert_eq!</span>( - <span class="ident">val</span>.<span class="ident">type_of</span>().<span class="ident">kind</span>(), - <span class="ident">phi_type</span>.<span class="ident">kind</span>(), - <span class="string">"Type of incoming phi value must be the same as the type used to build the phi node."</span> - ); + val.type_of().kind(), + phi_type.kind(), + <span class="string">"Type of incoming phi value must be the same as the type used to build the phi node." + </span>); - <span class="kw">unsafe</span> { - <span class="ident">LLVMAddIncoming</span>(<span class="ident">phi_ref</span>, <span class="kw-2">&mut</span> <span class="ident">val</span>.<span class="ident">value_ref</span>() <span class="kw">as</span> <span class="kw">_</span>, <span class="kw-2">&mut</span> <span class="ident">bb</span>.<span class="ident">bb_ref</span>() <span class="kw">as</span> <span class="kw">_</span>, <span class="number">1</span>); + <span class="kw">unsafe </span>{ + LLVMAddIncoming(phi_ref, <span class="kw-2">&mut </span>val.value_ref() <span class="kw">as _</span>, <span class="kw-2">&mut </span>bb.bb_ref() <span class="kw">as _</span>, <span class="number">1</span>); } } - <span class="ident">PhiValue::new</span>(<span class="ident">phi_ref</span>) + PhiValue::new(phi_ref) } } -<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">IRBuilder</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span> { - <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="kw">unsafe</span> { <span class="ident">LLVMDisposeBuilder</span>(<span class="self">self</span>.<span class="ident">builder</span>) } +<span class="kw">impl </span>Drop <span class="kw">for </span>IRBuilder<<span class="lifetime">'_</span>> { + <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ LLVMDisposeBuilder(<span class="self">self</span>.builder) } } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/lljit.rs.html b/src/llvm_kaleidoscope_rs/llvm/lljit.rs.html index 5a5a376..80be225 100644 --- a/src/llvm_kaleidoscope_rs/llvm/lljit.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/lljit.rs.html @@ -1,10 +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/llvm/lljit.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lljit.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/lljit.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lljit.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -158,160 +152,159 @@ <span id="152">152</span> <span id="153">153</span> <span id="154">154</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">llvm_sys::orc2</span>::{ - <span class="ident">lljit</span>::{ - <span class="ident">LLVMOrcCreateLLJIT</span>, <span class="ident">LLVMOrcLLJITAddLLVMIRModuleWithRT</span>, <span class="ident">LLVMOrcLLJITGetGlobalPrefix</span>, - <span class="ident">LLVMOrcLLJITGetMainJITDylib</span>, <span class="ident">LLVMOrcLLJITLookup</span>, <span class="ident">LLVMOrcLLJITRef</span>, +</pre><pre class="rust"><code><span class="kw">use </span>llvm_sys::orc2::{ + lljit::{ + LLVMOrcCreateLLJIT, LLVMOrcLLJITAddLLVMIRModuleWithRT, LLVMOrcLLJITGetGlobalPrefix, + LLVMOrcLLJITGetMainJITDylib, LLVMOrcLLJITLookup, LLVMOrcLLJITRef, }, - <span class="ident">LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess</span>, <span class="ident">LLVMOrcDefinitionGeneratorRef</span>, - <span class="ident">LLVMOrcJITDylibAddGenerator</span>, <span class="ident">LLVMOrcJITDylibCreateResourceTracker</span>, <span class="ident">LLVMOrcJITDylibRef</span>, - <span class="ident">LLVMOrcReleaseResourceTracker</span>, <span class="ident">LLVMOrcResourceTrackerRef</span>, <span class="ident">LLVMOrcResourceTrackerRemove</span>, + LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess, LLVMOrcDefinitionGeneratorRef, + LLVMOrcJITDylibAddGenerator, LLVMOrcJITDylibCreateResourceTracker, LLVMOrcJITDylibRef, + LLVMOrcReleaseResourceTracker, LLVMOrcResourceTrackerRef, LLVMOrcResourceTrackerRemove, }; -<span class="kw">use</span> <span class="ident">std::convert::TryFrom</span>; -<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>; +<span class="kw">use </span>std::convert::TryFrom; +<span class="kw">use </span>std::marker::PhantomData; -<span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">Error</span>, <span class="ident">Module</span>}; -<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::SmallCStr</span>; +<span class="kw">use super</span>::{Error, Module}; +<span class="kw">use </span><span class="kw">crate</span>::SmallCStr; -<span class="doccomment">/// Marker trait to constrain function signatures that can be looked up in the JIT.</span> -<span class="kw">pub</span> <span class="kw">trait</span> <span class="ident">JitFn</span> {} +<span class="doccomment">/// Marker trait to constrain function signatures that can be looked up in the JIT. +</span><span class="kw">pub trait </span>JitFn {} -<span class="kw">impl</span> <span class="ident">JitFn</span> <span class="kw">for</span> <span class="kw">unsafe</span> <span class="kw">extern</span> <span class="string">"C"</span> <span class="kw">fn</span>() -> <span class="ident">f64</span> {} +<span class="kw">impl </span>JitFn <span class="kw">for unsafe extern </span><span class="string">"C" </span><span class="kw">fn</span>() -> f64 {} -<span class="doccomment">/// Wrapper for a LLVM [LLJIT](https://www.llvm.org/docs/ORCv2.html#lljit-and-lllazyjit).</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">LLJit</span> { - <span class="ident">jit</span>: <span class="ident">LLVMOrcLLJITRef</span>, - <span class="ident">dylib</span>: <span class="ident">LLVMOrcJITDylibRef</span>, +<span class="doccomment">/// Wrapper for a LLVM [LLJIT](https://www.llvm.org/docs/ORCv2.html#lljit-and-lllazyjit). +</span><span class="kw">pub struct </span>LLJit { + jit: LLVMOrcLLJITRef, + dylib: LLVMOrcJITDylibRef, } -<span class="kw">impl</span> <span class="ident">LLJit</span> { - <span class="doccomment">/// Create a new LLJit instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer or an error.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span>() -> <span class="ident">LLJit</span> { - <span class="kw">let</span> (<span class="ident">jit</span>, <span class="ident">dylib</span>) <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">jit</span> <span class="op">=</span> <span class="ident">std::ptr::null_mut</span>(); - <span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">LLVMOrcCreateLLJIT</span>( - <span class="kw-2">&mut</span> <span class="ident">jit</span> <span class="kw">as</span> <span class="kw">_</span>, - <span class="ident">std::ptr::null_mut</span>(), <span class="comment">/* builder: nullptr -> default */</span> - ); +<span class="kw">impl </span>LLJit { + <span class="doccomment">/// Create a new LLJit instance. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer or an error. + </span><span class="kw">pub fn </span>new() -> LLJit { + <span class="kw">let </span>(jit, dylib) = <span class="kw">unsafe </span>{ + <span class="kw">let </span><span class="kw-2">mut </span>jit = std::ptr::null_mut(); + <span class="kw">let </span>err = LLVMOrcCreateLLJIT( + <span class="kw-2">&mut </span>jit <span class="kw">as _</span>, + std::ptr::null_mut(), <span class="comment">/* builder: nullptr -> default */ + </span>); - <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">err</span>) <span class="op">=</span> <span class="ident">Error::from</span>(<span class="ident">err</span>) { - <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, <span class="ident">err</span>.<span class="ident">as_str</span>()); + <span class="kw">if let </span><span class="prelude-val">Some</span>(err) = Error::from(err) { + <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, err.as_str()); } - <span class="kw">let</span> <span class="ident">dylib</span> <span class="op">=</span> <span class="ident">LLVMOrcLLJITGetMainJITDylib</span>(<span class="ident">jit</span>); - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">dylib</span>.<span class="ident">is_null</span>()); + <span class="kw">let </span>dylib = LLVMOrcLLJITGetMainJITDylib(jit); + <span class="macro">assert!</span>(!dylib.is_null()); - (<span class="ident">jit</span>, <span class="ident">dylib</span>) + (jit, dylib) }; - <span class="ident">LLJit</span> { <span class="ident">jit</span>, <span class="ident">dylib</span> } + LLJit { jit, dylib } } - <span class="doccomment">/// Add an LLVM IR module to the JIT. Return a [`ResourceTracker`], which when dropped, will</span> - <span class="doccomment">/// remove the code of the LLVM IR module from the JIT.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer or an error.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">add_module</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">module</span>: <span class="ident">Module</span>) -> <span class="ident">ResourceTracker</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">tsmod</span> <span class="op">=</span> <span class="ident">module</span>.<span class="ident">into_raw_thread_safe_module</span>(); + <span class="doccomment">/// Add an LLVM IR module to the JIT. Return a [`ResourceTracker`], which when dropped, will + /// remove the code of the LLVM IR module from the JIT. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer or an error. + </span><span class="kw">pub fn </span>add_module(<span class="kw-2">&</span><span class="self">self</span>, module: Module) -> ResourceTracker<<span class="lifetime">'_</span>> { + <span class="kw">let </span>tsmod = module.into_raw_thread_safe_module(); - <span class="kw">let</span> <span class="ident">rt</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="kw">let</span> <span class="ident">rt</span> <span class="op">=</span> <span class="ident">LLVMOrcJITDylibCreateResourceTracker</span>(<span class="self">self</span>.<span class="ident">dylib</span>); - <span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">LLVMOrcLLJITAddLLVMIRModuleWithRT</span>(<span class="self">self</span>.<span class="ident">jit</span>, <span class="ident">rt</span>, <span class="ident">tsmod</span>); + <span class="kw">let </span>rt = <span class="kw">unsafe </span>{ + <span class="kw">let </span>rt = LLVMOrcJITDylibCreateResourceTracker(<span class="self">self</span>.dylib); + <span class="kw">let </span>err = LLVMOrcLLJITAddLLVMIRModuleWithRT(<span class="self">self</span>.jit, rt, tsmod); - <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">err</span>) <span class="op">=</span> <span class="ident">Error::from</span>(<span class="ident">err</span>) { - <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, <span class="ident">err</span>.<span class="ident">as_str</span>()); + <span class="kw">if let </span><span class="prelude-val">Some</span>(err) = Error::from(err) { + <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, err.as_str()); } - <span class="ident">rt</span> + rt }; - <span class="ident">ResourceTracker::new</span>(<span class="ident">rt</span>) + ResourceTracker::new(rt) } - <span class="doccomment">/// Find the symbol with the name `sym` in the JIT.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if the symbol is not found in the JIT.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">find_symbol</span><span class="op"><</span><span class="ident">F</span>: <span class="ident">JitFn</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">sym</span>: <span class="kw-2">&</span><span class="ident">str</span>) -> <span class="ident">F</span> { - <span class="kw">let</span> <span class="ident">sym</span> <span class="op">=</span> - <span class="ident">SmallCStr::try_from</span>(<span class="ident">sym</span>).<span class="ident">expect</span>(<span class="string">"Failed to convert 'sym' argument to small C string!"</span>); + <span class="doccomment">/// Find the symbol with the name `sym` in the JIT. + /// + /// # Panics + /// + /// Panics if the symbol is not found in the JIT. + </span><span class="kw">pub fn </span>find_symbol<F: JitFn>(<span class="kw-2">&</span><span class="self">self</span>, sym: <span class="kw-2">&</span>str) -> F { + <span class="kw">let </span>sym = + SmallCStr::try_from(sym).expect(<span class="string">"Failed to convert 'sym' argument to small C string!"</span>); - <span class="kw">unsafe</span> { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">addr</span> <span class="op">=</span> <span class="number">0u64</span>; - <span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">LLVMOrcLLJITLookup</span>(<span class="self">self</span>.<span class="ident">jit</span>, <span class="kw-2">&mut</span> <span class="ident">addr</span> <span class="kw">as</span> <span class="kw">_</span>, <span class="ident">sym</span>.<span class="ident">as_ptr</span>()); + <span class="kw">unsafe </span>{ + <span class="kw">let </span><span class="kw-2">mut </span>addr = <span class="number">0u64</span>; + <span class="kw">let </span>err = LLVMOrcLLJITLookup(<span class="self">self</span>.jit, <span class="kw-2">&mut </span>addr <span class="kw">as _</span>, sym.as_ptr()); - <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">err</span>) <span class="op">=</span> <span class="ident">Error::from</span>(<span class="ident">err</span>) { - <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, <span class="ident">err</span>.<span class="ident">as_str</span>()); + <span class="kw">if let </span><span class="prelude-val">Some</span>(err) = Error::from(err) { + <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, err.as_str()); } - <span class="macro">debug_assert_eq!</span>(<span class="ident">core::mem::size_of_val</span>(<span class="kw-2">&</span><span class="ident">addr</span>), <span class="ident">core::mem::size_of</span>::<span class="op"><</span><span class="ident">F</span><span class="op">></span>()); - <span class="ident">std::mem::transmute_copy</span>(<span class="kw-2">&</span><span class="ident">addr</span>) + <span class="macro">debug_assert_eq!</span>(core::mem::size_of_val(<span class="kw-2">&</span>addr), core::mem::size_of::<F>()); + std::mem::transmute_copy(<span class="kw-2">&</span>addr) } } - <span class="doccomment">/// Enable lookup of dynamic symbols available in the current process from the JIT.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns an error.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">enable_process_symbols</span>(<span class="kw-2">&</span><span class="self">self</span>) { - <span class="kw">unsafe</span> { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">proc_syms_gen</span>: <span class="ident">LLVMOrcDefinitionGeneratorRef</span> <span class="op">=</span> <span class="ident">std::ptr::null_mut</span>(); - <span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess</span>( - <span class="kw-2">&mut</span> <span class="ident">proc_syms_gen</span> <span class="kw">as</span> <span class="kw">_</span>, - <span class="self">self</span>.<span class="ident">global_prefix</span>(), - <span class="prelude-val">None</span>, <span class="comment">/* filter */</span> - <span class="ident">std::ptr::null_mut</span>(), <span class="comment">/* filter ctx */</span> - ); + <span class="doccomment">/// Enable lookup of dynamic symbols available in the current process from the JIT. + /// + /// # Panics + /// + /// Panics if LLVM API returns an error. + </span><span class="kw">pub fn </span>enable_process_symbols(<span class="kw-2">&</span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ + <span class="kw">let </span><span class="kw-2">mut </span>proc_syms_gen: LLVMOrcDefinitionGeneratorRef = std::ptr::null_mut(); + <span class="kw">let </span>err = LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess( + <span class="kw-2">&mut </span>proc_syms_gen <span class="kw">as _</span>, + <span class="self">self</span>.global_prefix(), + <span class="prelude-val">None</span>, <span class="comment">/* filter */ + </span>std::ptr::null_mut(), <span class="comment">/* filter ctx */ + </span>); - <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">err</span>) <span class="op">=</span> <span class="ident">Error::from</span>(<span class="ident">err</span>) { - <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, <span class="ident">err</span>.<span class="ident">as_str</span>()); + <span class="kw">if let </span><span class="prelude-val">Some</span>(err) = Error::from(err) { + <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, err.as_str()); } - <span class="ident">LLVMOrcJITDylibAddGenerator</span>(<span class="self">self</span>.<span class="ident">dylib</span>, <span class="ident">proc_syms_gen</span>); + LLVMOrcJITDylibAddGenerator(<span class="self">self</span>.dylib, proc_syms_gen); } } - <span class="doccomment">/// Return the global prefix character according to the LLJITs data layout.</span> - <span class="kw">fn</span> <span class="ident">global_prefix</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">libc::c_char</span> { - <span class="kw">unsafe</span> { <span class="ident">LLVMOrcLLJITGetGlobalPrefix</span>(<span class="self">self</span>.<span class="ident">jit</span>) } + <span class="doccomment">/// Return the global prefix character according to the LLJITs data layout. + </span><span class="kw">fn </span>global_prefix(<span class="kw-2">&</span><span class="self">self</span>) -> libc::c_char { + <span class="kw">unsafe </span>{ LLVMOrcLLJITGetGlobalPrefix(<span class="self">self</span>.jit) } } } -<span class="doccomment">/// A resource handle for code added to an [`LLJit`] instance.</span> -<span class="doccomment">///</span> -<span class="doccomment">/// When a `ResourceTracker` handle is dropped, the code corresponding to the handle will be</span> -<span class="doccomment">/// removed from the JIT.</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">ResourceTracker</span><span class="op"><</span><span class="lifetime">'jit</span><span class="op">></span>(<span class="ident">LLVMOrcResourceTrackerRef</span>, <span class="ident">PhantomData</span><span class="op"><</span><span class="kw-2">&</span><span class="lifetime">'jit</span> ()<span class="op">></span>); +<span class="doccomment">/// A resource handle for code added to an [`LLJit`] instance. +/// +/// When a `ResourceTracker` handle is dropped, the code corresponding to the handle will be +/// removed from the JIT. +</span><span class="kw">pub struct </span>ResourceTracker<<span class="lifetime">'jit</span>>(LLVMOrcResourceTrackerRef, PhantomData<<span class="kw-2">&</span><span class="lifetime">'jit </span>()>); -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'jit</span><span class="op">></span> <span class="ident">ResourceTracker</span><span class="op"><</span><span class="lifetime">'jit</span><span class="op">></span> { - <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">rt</span>: <span class="ident">LLVMOrcResourceTrackerRef</span>) -> <span class="ident">ResourceTracker</span><span class="op"><</span><span class="lifetime">'jit</span><span class="op">></span> { - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">rt</span>.<span class="ident">is_null</span>()); - <span class="ident">ResourceTracker</span>(<span class="ident">rt</span>, <span class="ident">PhantomData</span>) +<span class="kw">impl</span><<span class="lifetime">'jit</span>> ResourceTracker<<span class="lifetime">'jit</span>> { + <span class="kw">fn </span>new(rt: LLVMOrcResourceTrackerRef) -> ResourceTracker<<span class="lifetime">'jit</span>> { + <span class="macro">assert!</span>(!rt.is_null()); + ResourceTracker(rt, PhantomData) } } -<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">ResourceTracker</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span> { - <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="kw">unsafe</span> { - <span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">LLVMOrcResourceTrackerRemove</span>(<span class="self">self</span>.<span class="number">0</span>); +<span class="kw">impl </span>Drop <span class="kw">for </span>ResourceTracker<<span class="lifetime">'_</span>> { + <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ + <span class="kw">let </span>err = LLVMOrcResourceTrackerRemove(<span class="self">self</span>.<span class="number">0</span>); - <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">err</span>) <span class="op">=</span> <span class="ident">Error::from</span>(<span class="ident">err</span>) { - <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, <span class="ident">err</span>.<span class="ident">as_str</span>()); + <span class="kw">if let </span><span class="prelude-val">Some</span>(err) = Error::from(err) { + <span class="macro">panic!</span>(<span class="string">"Error: {}"</span>, err.as_str()); } - <span class="ident">LLVMOrcReleaseResourceTracker</span>(<span class="self">self</span>.<span class="number">0</span>); + LLVMOrcReleaseResourceTracker(<span class="self">self</span>.<span class="number">0</span>); }; } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/mod.rs.html b/src/llvm_kaleidoscope_rs/llvm/mod.rs.html index 3dca9c7..87756d4 100644 --- a/src/llvm_kaleidoscope_rs/llvm/mod.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/mod.rs.html @@ -1,10 +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/llvm/mod.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>mod.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/mod.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>mod.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -78,80 +72,79 @@ <span id="72">72</span> <span id="73">73</span> <span id="74">74</span> -</pre><pre class="rust"><code><span class="doccomment">//! Safe wrapper around the LLVM C API.</span> -<span class="doccomment">//!</span> -<span class="doccomment">//! References returned from the LLVM API are tied to the `'llvm` lifetime which is bound to the</span> -<span class="doccomment">//! context where the objects are created in.</span> -<span class="doccomment">//! We do not offer wrappers to remove or delete any objects in the context and therefore all the</span> -<span class="doccomment">//! references will be valid for the liftime of the context.</span> -<span class="doccomment">//!</span> -<span class="doccomment">//! For the scope of this tutorial we mainly use assertions to validate the results from the LLVM</span> -<span class="doccomment">//! API calls.</span> +</pre><pre class="rust"><code><span class="doccomment">//! Safe wrapper around the LLVM C API. +//! +//! References returned from the LLVM API are tied to the `'llvm` lifetime which is bound to the +//! context where the objects are created in. +//! We do not offer wrappers to remove or delete any objects in the context and therefore all the +//! references will be valid for the liftime of the context. +//! +//! For the scope of this tutorial we mainly use assertions to validate the results from the LLVM +//! API calls. -<span class="kw">use</span> <span class="ident">llvm_sys</span>::{ - <span class="ident">core::LLVMShutdown</span>, - <span class="ident">error</span>::{<span class="ident">LLVMDisposeErrorMessage</span>, <span class="ident">LLVMErrorRef</span>, <span class="ident">LLVMGetErrorMessage</span>}, - <span class="ident">target</span>::{ - <span class="ident">LLVM_InitializeNativeAsmParser</span>, <span class="ident">LLVM_InitializeNativeAsmPrinter</span>, - <span class="ident">LLVM_InitializeNativeTarget</span>, +</span><span class="kw">use </span>llvm_sys::{ + core::LLVMShutdown, + error::{LLVMDisposeErrorMessage, LLVMErrorRef, LLVMGetErrorMessage}, + target::{ + LLVM_InitializeNativeAsmParser, LLVM_InitializeNativeAsmPrinter, + LLVM_InitializeNativeTarget, }, }; -<span class="kw">use</span> <span class="ident">std::ffi::CStr</span>; +<span class="kw">use </span>std::ffi::CStr; -<span class="kw">mod</span> <span class="ident">basic_block</span>; -<span class="kw">mod</span> <span class="ident">builder</span>; -<span class="kw">mod</span> <span class="ident">lljit</span>; -<span class="kw">mod</span> <span class="ident">module</span>; -<span class="kw">mod</span> <span class="ident">pass_manager</span>; -<span class="kw">mod</span> <span class="ident">type_</span>; -<span class="kw">mod</span> <span class="ident">value</span>; +<span class="kw">mod </span>basic_block; +<span class="kw">mod </span>builder; +<span class="kw">mod </span>lljit; +<span class="kw">mod </span>module; +<span class="kw">mod </span>pass_manager; +<span class="kw">mod </span>type_; +<span class="kw">mod </span>value; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">basic_block::BasicBlock</span>; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">builder::IRBuilder</span>; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">lljit</span>::{<span class="ident">LLJit</span>, <span class="ident">ResourceTracker</span>}; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">module::Module</span>; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">pass_manager::FunctionPassManager</span>; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">type_::Type</span>; -<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">value</span>::{<span class="ident">FnValue</span>, <span class="ident">PhiValue</span>, <span class="ident">Value</span>}; +<span class="kw">pub use </span>basic_block::BasicBlock; +<span class="kw">pub use </span>builder::IRBuilder; +<span class="kw">pub use </span>lljit::{LLJit, ResourceTracker}; +<span class="kw">pub use </span>module::Module; +<span class="kw">pub use </span>pass_manager::FunctionPassManager; +<span class="kw">pub use </span>type_::Type; +<span class="kw">pub use </span>value::{FnValue, PhiValue, Value}; -<span class="kw">struct</span> <span class="ident">Error</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>(<span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="kw-2">mut</span> <span class="ident">libc::c_char</span>); +<span class="kw">struct </span>Error<<span class="lifetime">'llvm</span>>(<span class="kw-2">&</span><span class="lifetime">'llvm </span><span class="kw-2">mut </span>libc::c_char); -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">Error</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">fn</span> <span class="ident">from</span>(<span class="ident">err</span>: <span class="ident">LLVMErrorRef</span>) -> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">Error</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></span> { - (<span class="op">!</span><span class="ident">err</span>.<span class="ident">is_null</span>()).<span class="ident">then</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">Error</span>(<span class="kw">unsafe</span> { <span class="kw-2">&mut</span> <span class="kw-2">*</span><span class="ident">LLVMGetErrorMessage</span>(<span class="ident">err</span>) })) +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> Error<<span class="lifetime">'llvm</span>> { + <span class="kw">fn </span>from(err: LLVMErrorRef) -> <span class="prelude-ty">Option</span><Error<<span class="lifetime">'llvm</span>>> { + (!err.is_null()).then(|| Error(<span class="kw">unsafe </span>{ <span class="kw-2">&mut *</span>LLVMGetErrorMessage(err) })) } - <span class="kw">fn</span> <span class="ident">as_str</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="ident">str</span> { - <span class="kw">unsafe</span> { <span class="ident">CStr::from_ptr</span>(<span class="self">self</span>.<span class="number">0</span>) } - .<span class="ident">to_str</span>() - .<span class="ident">expect</span>(<span class="string">"Expected valid UTF8 string from LLVM API"</span>) + <span class="kw">fn </span>as_str(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span>str { + <span class="kw">unsafe </span>{ CStr::from_ptr(<span class="self">self</span>.<span class="number">0</span>) } + .to_str() + .expect(<span class="string">"Expected valid UTF8 string from LLVM API"</span>) } } -<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">Error</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span> { - <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="kw">unsafe</span> { - <span class="ident">LLVMDisposeErrorMessage</span>(<span class="self">self</span>.<span class="number">0</span> <span class="kw">as</span> <span class="kw-2">*mut</span> <span class="ident">libc::c_char</span>); +<span class="kw">impl </span>Drop <span class="kw">for </span>Error<<span class="lifetime">'_</span>> { + <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ + LLVMDisposeErrorMessage(<span class="self">self</span>.<span class="number">0 </span><span class="kw">as </span><span class="kw-2">*mut </span>libc::c_char); } } } -<span class="doccomment">/// Initialize native target for corresponding to host (useful for jitting).</span> -<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">initialize_native_taget</span>() { - <span class="kw">unsafe</span> { - <span class="macro">assert_eq!</span>(<span class="ident">LLVM_InitializeNativeTarget</span>(), <span class="number">0</span>); - <span class="macro">assert_eq!</span>(<span class="ident">LLVM_InitializeNativeAsmParser</span>(), <span class="number">0</span>); - <span class="macro">assert_eq!</span>(<span class="ident">LLVM_InitializeNativeAsmPrinter</span>(), <span class="number">0</span>); +<span class="doccomment">/// Initialize native target for corresponding to host (useful for jitting). +</span><span class="kw">pub fn </span>initialize_native_taget() { + <span class="kw">unsafe </span>{ + <span class="macro">assert_eq!</span>(LLVM_InitializeNativeTarget(), <span class="number">0</span>); + <span class="macro">assert_eq!</span>(LLVM_InitializeNativeAsmParser(), <span class="number">0</span>); + <span class="macro">assert_eq!</span>(LLVM_InitializeNativeAsmPrinter(), <span class="number">0</span>); } } -<span class="doccomment">/// Deallocate and destroy all "ManagedStatic" variables.</span> -<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">shutdown</span>() { - <span class="kw">unsafe</span> { - <span class="ident">LLVMShutdown</span>(); +<span class="doccomment">/// Deallocate and destroy all "ManagedStatic" variables. +</span><span class="kw">pub fn </span>shutdown() { + <span class="kw">unsafe </span>{ + LLVMShutdown(); }; } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/module.rs.html b/src/llvm_kaleidoscope_rs/llvm/module.rs.html index 99846f4..127ba39 100644 --- a/src/llvm_kaleidoscope_rs/llvm/module.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/module.rs.html @@ -1,10 +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/llvm/module.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>module.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/module.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>module.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -214,216 +208,215 @@ <span id="208">208</span> <span id="209">209</span> <span id="210">210</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">llvm_sys</span>::{ - <span class="ident">core</span>::{ - <span class="ident">LLVMAddFunction</span>, <span class="ident">LLVMAppendBasicBlockInContext</span>, <span class="ident">LLVMCreateBasicBlockInContext</span>, - <span class="ident">LLVMDisposeModule</span>, <span class="ident">LLVMDoubleTypeInContext</span>, <span class="ident">LLVMDumpModule</span>, <span class="ident">LLVMGetNamedFunction</span>, - <span class="ident">LLVMModuleCreateWithNameInContext</span>, +</pre><pre class="rust"><code><span class="kw">use </span>llvm_sys::{ + core::{ + LLVMAddFunction, LLVMAppendBasicBlockInContext, LLVMCreateBasicBlockInContext, + LLVMDisposeModule, LLVMDoubleTypeInContext, LLVMDumpModule, LLVMGetNamedFunction, + LLVMModuleCreateWithNameInContext, }, - <span class="ident">orc2</span>::{ - <span class="ident">LLVMOrcCreateNewThreadSafeContext</span>, <span class="ident">LLVMOrcCreateNewThreadSafeModule</span>, - <span class="ident">LLVMOrcDisposeThreadSafeContext</span>, <span class="ident">LLVMOrcThreadSafeContextGetContext</span>, - <span class="ident">LLVMOrcThreadSafeContextRef</span>, <span class="ident">LLVMOrcThreadSafeModuleRef</span>, + orc2::{ + LLVMOrcCreateNewThreadSafeContext, LLVMOrcCreateNewThreadSafeModule, + LLVMOrcDisposeThreadSafeContext, LLVMOrcThreadSafeContextGetContext, + LLVMOrcThreadSafeContextRef, LLVMOrcThreadSafeModuleRef, }, - <span class="ident">prelude</span>::{<span class="ident">LLVMBool</span>, <span class="ident">LLVMContextRef</span>, <span class="ident">LLVMModuleRef</span>, <span class="ident">LLVMTypeRef</span>}, - <span class="ident">LLVMTypeKind</span>, + prelude::{LLVMBool, LLVMContextRef, LLVMModuleRef, LLVMTypeRef}, + LLVMTypeKind, }; -<span class="kw">use</span> <span class="ident">std::convert::TryFrom</span>; +<span class="kw">use </span>std::convert::TryFrom; -<span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">BasicBlock</span>, <span class="ident">FnValue</span>, <span class="ident">Type</span>}; -<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::SmallCStr</span>; +<span class="kw">use super</span>::{BasicBlock, FnValue, Type}; +<span class="kw">use </span><span class="kw">crate</span>::SmallCStr; -<span class="comment">// Definition of LLVM C API functions using our `repr(transparent)` types.</span> -<span class="kw">extern</span> <span class="string">"C"</span> { - <span class="kw">fn</span> <span class="ident">LLVMFunctionType</span>( - <span class="ident">ReturnType</span>: <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span>, - <span class="ident">ParamTypes</span>: <span class="kw-2">*mut</span> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span>, - <span class="ident">ParamCount</span>: <span class="ident">::libc::c_uint</span>, - <span class="ident">IsVarArg</span>: <span class="ident">LLVMBool</span>, - ) -> <span class="ident">LLVMTypeRef</span>; +<span class="comment">// Definition of LLVM C API functions using our `repr(transparent)` types. +</span><span class="kw">extern </span><span class="string">"C" </span>{ + <span class="kw">fn </span>LLVMFunctionType( + ReturnType: Type<<span class="lifetime">'_</span>>, + ParamTypes: <span class="kw-2">*mut </span>Type<<span class="lifetime">'_</span>>, + ParamCount: ::libc::c_uint, + IsVarArg: LLVMBool, + ) -> LLVMTypeRef; } -<span class="doccomment">/// Wrapper for a LLVM Module with its own LLVM Context.</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Module</span> { - <span class="ident">tsctx</span>: <span class="ident">LLVMOrcThreadSafeContextRef</span>, - <span class="ident">ctx</span>: <span class="ident">LLVMContextRef</span>, - <span class="ident">module</span>: <span class="ident">LLVMModuleRef</span>, +<span class="doccomment">/// Wrapper for a LLVM Module with its own LLVM Context. +</span><span class="kw">pub struct </span>Module { + tsctx: LLVMOrcThreadSafeContextRef, + ctx: LLVMContextRef, + module: LLVMModuleRef, } -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">Module</span> { - <span class="doccomment">/// Create a new Module instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if creating the context or the module fails.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span>() -> <span class="self">Self</span> { - <span class="kw">let</span> (<span class="ident">tsctx</span>, <span class="ident">ctx</span>, <span class="ident">module</span>) <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="comment">// We generate a thread safe context because we are going to jit this IR module and</span> - <span class="comment">// there is no method to create a thread safe context wrapper from an existing context</span> - <span class="comment">// reference (at the time of writing this).</span> - <span class="comment">//</span> - <span class="comment">// ThreadSafeContext has shared ownership (start with ref count 1).</span> - <span class="comment">// We must explicitly dispose our reference (dec ref count).</span> - <span class="kw">let</span> <span class="ident">tc</span> <span class="op">=</span> <span class="ident">LLVMOrcCreateNewThreadSafeContext</span>(); - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">tc</span>.<span class="ident">is_null</span>()); +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> Module { + <span class="doccomment">/// Create a new Module instance. + /// + /// # Panics + /// + /// Panics if creating the context or the module fails. + </span><span class="kw">pub fn </span>new() -> <span class="self">Self </span>{ + <span class="kw">let </span>(tsctx, ctx, module) = <span class="kw">unsafe </span>{ + <span class="comment">// We generate a thread safe context because we are going to jit this IR module and + // there is no method to create a thread safe context wrapper from an existing context + // reference (at the time of writing this). + // + // ThreadSafeContext has shared ownership (start with ref count 1). + // We must explicitly dispose our reference (dec ref count). + </span><span class="kw">let </span>tc = LLVMOrcCreateNewThreadSafeContext(); + <span class="macro">assert!</span>(!tc.is_null()); - <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">LLVMOrcThreadSafeContextGetContext</span>(<span class="ident">tc</span>); - <span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="ident">LLVMModuleCreateWithNameInContext</span>(<span class="string">b"module\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), <span class="ident">c</span>); - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">c</span>.<span class="ident">is_null</span>() <span class="op">&&</span> <span class="op">!</span><span class="ident">m</span>.<span class="ident">is_null</span>()); - (<span class="ident">tc</span>, <span class="ident">c</span>, <span class="ident">m</span>) + <span class="kw">let </span>c = LLVMOrcThreadSafeContextGetContext(tc); + <span class="kw">let </span>m = LLVMModuleCreateWithNameInContext(<span class="string">b"module\0"</span>.as_ptr().cast(), c); + <span class="macro">assert!</span>(!c.is_null() && !m.is_null()); + (tc, c, m) }; - <span class="ident">Module</span> { <span class="ident">tsctx</span>, <span class="ident">ctx</span>, <span class="ident">module</span> } + Module { tsctx, ctx, module } } - <span class="doccomment">/// Get the raw LLVM context reference.</span> - <span class="attribute">#[<span class="ident">inline</span>]</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">ctx</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMContextRef</span> { - <span class="self">self</span>.<span class="ident">ctx</span> + <span class="doccomment">/// Get the raw LLVM context reference. + </span><span class="attribute">#[inline] + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>ctx(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMContextRef { + <span class="self">self</span>.ctx } - <span class="doccomment">/// Get the raw LLVM module reference.</span> - <span class="attribute">#[<span class="ident">inline</span>]</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">module</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMModuleRef</span> { - <span class="self">self</span>.<span class="ident">module</span> + <span class="doccomment">/// Get the raw LLVM module reference. + </span><span class="attribute">#[inline] + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>module(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMModuleRef { + <span class="self">self</span>.module } - <span class="doccomment">/// Consume the module and turn in into a raw LLVM ThreadSafeModule reference.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// If ownership of the raw reference is not transferred (eg to the JIT), memory will be leaked</span> - <span class="doccomment">/// in case the reference is disposed explicitly with LLVMOrcDisposeThreadSafeModule.</span> - <span class="attribute">#[<span class="ident">inline</span>]</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">into_raw_thread_safe_module</span>(<span class="kw-2">mut</span> <span class="self">self</span>) -> <span class="ident">LLVMOrcThreadSafeModuleRef</span> { - <span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="ident">std::mem::replace</span>(<span class="kw-2">&mut</span> <span class="self">self</span>.<span class="ident">module</span>, <span class="ident">std::ptr::null_mut</span>()); + <span class="doccomment">/// Consume the module and turn in into a raw LLVM ThreadSafeModule reference. + /// + /// If ownership of the raw reference is not transferred (eg to the JIT), memory will be leaked + /// in case the reference is disposed explicitly with LLVMOrcDisposeThreadSafeModule. + </span><span class="attribute">#[inline] + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>into_raw_thread_safe_module(<span class="kw-2">mut </span><span class="self">self</span>) -> LLVMOrcThreadSafeModuleRef { + <span class="kw">let </span>m = std::mem::replace(<span class="kw-2">&mut </span><span class="self">self</span>.module, std::ptr::null_mut()); - <span class="comment">// ThreadSafeModule has unique ownership.</span> - <span class="comment">// Takes ownership of module and increments ThreadSafeContext ref count.</span> - <span class="comment">//</span> - <span class="comment">// We must not reference/dispose `m` after this call, but we need to dispose our `tsctx`</span> - <span class="comment">// reference.</span> - <span class="kw">let</span> <span class="ident">tm</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMOrcCreateNewThreadSafeModule</span>(<span class="ident">m</span>, <span class="self">self</span>.<span class="ident">tsctx</span>) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">tm</span>.<span class="ident">is_null</span>()); + <span class="comment">// ThreadSafeModule has unique ownership. + // Takes ownership of module and increments ThreadSafeContext ref count. + // + // We must not reference/dispose `m` after this call, but we need to dispose our `tsctx` + // reference. + </span><span class="kw">let </span>tm = <span class="kw">unsafe </span>{ LLVMOrcCreateNewThreadSafeModule(m, <span class="self">self</span>.tsctx) }; + <span class="macro">assert!</span>(!tm.is_null()); - <span class="ident">tm</span> + tm } - <span class="doccomment">/// Dump LLVM IR emitted into the Module to stdout.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">dump</span>(<span class="kw-2">&</span><span class="self">self</span>) { - <span class="kw">unsafe</span> { <span class="ident">LLVMDumpModule</span>(<span class="self">self</span>.<span class="ident">module</span>) }; + <span class="doccomment">/// Dump LLVM IR emitted into the Module to stdout. + </span><span class="kw">pub fn </span>dump(<span class="kw-2">&</span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ LLVMDumpModule(<span class="self">self</span>.module) }; } - <span class="doccomment">/// Get a type reference representing a `f64` float.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">type_f64</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">type_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMDoubleTypeInContext</span>(<span class="self">self</span>.<span class="ident">ctx</span>) }; - <span class="ident">Type::new</span>(<span class="ident">type_ref</span>) + <span class="doccomment">/// Get a type reference representing a `f64` float. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>type_f64(<span class="kw-2">&</span><span class="self">self</span>) -> Type<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>type_ref = <span class="kw">unsafe </span>{ LLVMDoubleTypeInContext(<span class="self">self</span>.ctx) }; + Type::new(type_ref) } - <span class="doccomment">/// Get a type reference representing a `fn(args) -> ret` function.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">type_fn</span>(<span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="self">self</span>, <span class="ident">args</span>: <span class="kw-2">&mut</span> [<span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>], <span class="ident">ret</span>: <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">type_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMFunctionType</span>( - <span class="ident">ret</span>, - <span class="ident">args</span>.<span class="ident">as_mut_ptr</span>(), - <span class="ident">args</span>.<span class="ident">len</span>() <span class="kw">as</span> <span class="ident">libc::c_uint</span>, - <span class="number">0</span>, <span class="comment">/* IsVarArg */</span> - ) + <span class="doccomment">/// Get a type reference representing a `fn(args) -> ret` function. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>type_fn(<span class="kw-2">&</span><span class="lifetime">'llvm </span><span class="self">self</span>, args: <span class="kw-2">&mut </span>[Type<<span class="lifetime">'llvm</span>>], ret: Type<<span class="lifetime">'llvm</span>>) -> Type<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>type_ref = <span class="kw">unsafe </span>{ + LLVMFunctionType( + ret, + args.as_mut_ptr(), + args.len() <span class="kw">as </span>libc::c_uint, + <span class="number">0</span>, <span class="comment">/* IsVarArg */ + </span>) }; - <span class="ident">Type::new</span>(<span class="ident">type_ref</span>) + Type::new(type_ref) } - <span class="doccomment">/// Add a function with the given `name` and `fn_type` to the module and return a value</span> - <span class="doccomment">/// reference representing the function.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer or `name` could not be converted to a</span> - <span class="doccomment">/// [`SmallCStr`].</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">add_fn</span>(<span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="self">self</span>, <span class="ident">name</span>: <span class="kw-2">&</span><span class="ident">str</span>, <span class="ident">fn_type</span>: <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { + <span class="doccomment">/// Add a function with the given `name` and `fn_type` to the module and return a value + /// reference representing the function. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer or `name` could not be converted to a + /// [`SmallCStr`]. + </span><span class="kw">pub fn </span>add_fn(<span class="kw-2">&</span><span class="lifetime">'llvm </span><span class="self">self</span>, name: <span class="kw-2">&</span>str, fn_type: Type<<span class="lifetime">'llvm</span>>) -> FnValue<<span class="lifetime">'llvm</span>> { <span class="macro">debug_assert_eq!</span>( - <span class="ident">fn_type</span>.<span class="ident">kind</span>(), - <span class="ident">LLVMTypeKind::LLVMFunctionTypeKind</span>, - <span class="string">"Expected a function type when adding a function!"</span> - ); + fn_type.kind(), + LLVMTypeKind::LLVMFunctionTypeKind, + <span class="string">"Expected a function type when adding a function!" + </span>); - <span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="ident">SmallCStr::try_from</span>(<span class="ident">name</span>) - .<span class="ident">expect</span>(<span class="string">"Failed to convert 'name' argument to small C string!"</span>); + <span class="kw">let </span>name = SmallCStr::try_from(name) + .expect(<span class="string">"Failed to convert 'name' argument to small C string!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMAddFunction</span>(<span class="self">self</span>.<span class="ident">module</span>, <span class="ident">name</span>.<span class="ident">as_ptr</span>(), <span class="ident">fn_type</span>.<span class="ident">type_ref</span>()) }; - <span class="ident">FnValue::new</span>(<span class="ident">value_ref</span>) + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ LLVMAddFunction(<span class="self">self</span>.module, name.as_ptr(), fn_type.type_ref()) }; + FnValue::new(value_ref) } - <span class="doccomment">/// Get a function value reference to the function with the given `name` if it was previously</span> - <span class="doccomment">/// added to the module with [`add_fn`][Module::add_fn].</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if `name` could not be converted to a [`SmallCStr`].</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">get_fn</span>(<span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="self">self</span>, <span class="ident">name</span>: <span class="kw-2">&</span><span class="ident">str</span>) -> <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="ident">SmallCStr::try_from</span>(<span class="ident">name</span>) - .<span class="ident">expect</span>(<span class="string">"Failed to convert 'name' argument to small C string!"</span>); + <span class="doccomment">/// Get a function value reference to the function with the given `name` if it was previously + /// added to the module with [`add_fn`][Module::add_fn]. + /// + /// # Panics + /// + /// Panics if `name` could not be converted to a [`SmallCStr`]. + </span><span class="kw">pub fn </span>get_fn(<span class="kw-2">&</span><span class="lifetime">'llvm </span><span class="self">self</span>, name: <span class="kw-2">&</span>str) -> <span class="prelude-ty">Option</span><FnValue<<span class="lifetime">'llvm</span>>> { + <span class="kw">let </span>name = SmallCStr::try_from(name) + .expect(<span class="string">"Failed to convert 'name' argument to small C string!"</span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMGetNamedFunction</span>(<span class="self">self</span>.<span class="ident">module</span>, <span class="ident">name</span>.<span class="ident">as_ptr</span>()) }; + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ LLVMGetNamedFunction(<span class="self">self</span>.module, name.as_ptr()) }; - (<span class="op">!</span><span class="ident">value_ref</span>.<span class="ident">is_null</span>()).<span class="ident">then</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">FnValue::new</span>(<span class="ident">value_ref</span>)) + (!value_ref.is_null()).then(|| FnValue::new(value_ref)) } - <span class="doccomment">/// Append a Basic Block to the end of the function referenced by the value reference</span> - <span class="doccomment">/// `fn_value`.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">append_basic_block</span>(<span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="self">self</span>, <span class="ident">fn_value</span>: <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) -> <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">block</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="ident">LLVMAppendBasicBlockInContext</span>( - <span class="self">self</span>.<span class="ident">ctx</span>, - <span class="ident">fn_value</span>.<span class="ident">value_ref</span>(), - <span class="string">b"block\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), + <span class="doccomment">/// Append a Basic Block to the end of the function referenced by the value reference + /// `fn_value`. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>append_basic_block(<span class="kw-2">&</span><span class="lifetime">'llvm </span><span class="self">self</span>, fn_value: FnValue<<span class="lifetime">'llvm</span>>) -> BasicBlock<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>block = <span class="kw">unsafe </span>{ + LLVMAppendBasicBlockInContext( + <span class="self">self</span>.ctx, + fn_value.value_ref(), + <span class="string">b"block\0"</span>.as_ptr().cast(), ) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">block</span>.<span class="ident">is_null</span>()); + <span class="macro">assert!</span>(!block.is_null()); - <span class="ident">BasicBlock::new</span>(<span class="ident">block</span>) + BasicBlock::new(block) } - <span class="doccomment">/// Create a free-standing Basic Block without adding it to a function.</span> - <span class="doccomment">/// This can be added to a function at a later point in time with</span> - <span class="doccomment">/// [`FnValue::append_basic_block`].</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">create_basic_block</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">block</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMCreateBasicBlockInContext</span>(<span class="self">self</span>.<span class="ident">ctx</span>, <span class="string">b"block\0"</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">block</span>.<span class="ident">is_null</span>()); + <span class="doccomment">/// Create a free-standing Basic Block without adding it to a function. + /// This can be added to a function at a later point in time with + /// [`FnValue::append_basic_block`]. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>create_basic_block(<span class="kw-2">&</span><span class="self">self</span>) -> BasicBlock<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>block = <span class="kw">unsafe </span>{ LLVMCreateBasicBlockInContext(<span class="self">self</span>.ctx, <span class="string">b"block\0"</span>.as_ptr().cast()) }; + <span class="macro">assert!</span>(!block.is_null()); - <span class="ident">BasicBlock::new</span>(<span class="ident">block</span>) + BasicBlock::new(block) } } -<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">Module</span> { - <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="kw">unsafe</span> { - <span class="comment">// In case we turned the module into a ThreadSafeModule, we must not dispose the module</span> - <span class="comment">// reference because ThreadSafeModule took ownership!</span> - <span class="kw">if</span> <span class="op">!</span><span class="self">self</span>.<span class="ident">module</span>.<span class="ident">is_null</span>() { - <span class="ident">LLVMDisposeModule</span>(<span class="self">self</span>.<span class="ident">module</span>); +<span class="kw">impl </span>Drop <span class="kw">for </span>Module { + <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ + <span class="comment">// In case we turned the module into a ThreadSafeModule, we must not dispose the module + // reference because ThreadSafeModule took ownership! + </span><span class="kw">if </span>!<span class="self">self</span>.module.is_null() { + LLVMDisposeModule(<span class="self">self</span>.module); } - <span class="comment">// Dispose ThreadSafeContext reference (dec ref count) in any case.</span> - <span class="ident">LLVMOrcDisposeThreadSafeContext</span>(<span class="self">self</span>.<span class="ident">tsctx</span>); + <span class="comment">// Dispose ThreadSafeContext reference (dec ref count) in any case. + </span>LLVMOrcDisposeThreadSafeContext(<span class="self">self</span>.tsctx); } } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/pass_manager.rs.html b/src/llvm_kaleidoscope_rs/llvm/pass_manager.rs.html index ebf9b28..968856f 100644 --- a/src/llvm_kaleidoscope_rs/llvm/pass_manager.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/pass_manager.rs.html @@ -1,10 +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/llvm/pass_manager.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>pass_manager.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/pass_manager.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>pass_manager.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -79,81 +73,80 @@ <span id="73">73</span> <span id="74">74</span> <span id="75">75</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">llvm_sys</span>::{ - <span class="ident">core</span>::{ - <span class="ident">LLVMCreateFunctionPassManagerForModule</span>, <span class="ident">LLVMDisposePassManager</span>, - <span class="ident">LLVMInitializeFunctionPassManager</span>, <span class="ident">LLVMRunFunctionPassManager</span>, +</pre><pre class="rust"><code><span class="kw">use </span>llvm_sys::{ + core::{ + LLVMCreateFunctionPassManagerForModule, LLVMDisposePassManager, + LLVMInitializeFunctionPassManager, LLVMRunFunctionPassManager, }, - <span class="ident">prelude::LLVMPassManagerRef</span>, - <span class="ident">transforms</span>::{ - <span class="ident">instcombine::LLVMAddInstructionCombiningPass</span>, - <span class="ident">scalar</span>::{<span class="ident">LLVMAddCFGSimplificationPass</span>, <span class="ident">LLVMAddNewGVNPass</span>, <span class="ident">LLVMAddReassociatePass</span>}, + prelude::LLVMPassManagerRef, + transforms::{ + instcombine::LLVMAddInstructionCombiningPass, + scalar::{LLVMAddCFGSimplificationPass, LLVMAddNewGVNPass, LLVMAddReassociatePass}, }, }; -<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>; +<span class="kw">use </span>std::marker::PhantomData; -<span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">FnValue</span>, <span class="ident">Module</span>}; +<span class="kw">use super</span>::{FnValue, Module}; -<span class="doccomment">/// Wrapper for a LLVM Function PassManager (legacy).</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">FunctionPassManager</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="ident">fpm</span>: <span class="ident">LLVMPassManagerRef</span>, - <span class="ident">_ctx</span>: <span class="ident">PhantomData</span><span class="op"><</span><span class="kw-2">&</span><span class="lifetime">'llvm</span> ()<span class="op">></span>, +<span class="doccomment">/// Wrapper for a LLVM Function PassManager (legacy). +</span><span class="kw">pub struct </span>FunctionPassManager<<span class="lifetime">'llvm</span>> { + fpm: LLVMPassManagerRef, + _ctx: PhantomData<<span class="kw-2">&</span><span class="lifetime">'llvm </span>()>, } -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">FunctionPassManager</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new Function PassManager with the following optimization passes</span> - <span class="doccomment">/// - InstructionCombiningPass</span> - <span class="doccomment">/// - ReassociatePass</span> - <span class="doccomment">/// - NewGVNPass</span> - <span class="doccomment">/// - CFGSimplificationPass</span> - <span class="doccomment">///</span> - <span class="doccomment">/// The list of selected optimization passes is taken from the tutorial chapter [LLVM</span> - <span class="doccomment">/// Optimization Passes](https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.html#id3).</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">with_ctx</span>(<span class="ident">module</span>: <span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="ident">Module</span>) -> <span class="ident">FunctionPassManager</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">fpm</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="comment">// Borrows module reference.</span> - <span class="ident">LLVMCreateFunctionPassManagerForModule</span>(<span class="ident">module</span>.<span class="ident">module</span>()) +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> FunctionPassManager<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new Function PassManager with the following optimization passes + /// - InstructionCombiningPass + /// - ReassociatePass + /// - NewGVNPass + /// - CFGSimplificationPass + /// + /// The list of selected optimization passes is taken from the tutorial chapter [LLVM + /// Optimization Passes](https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.html#id3). + </span><span class="kw">pub fn </span>with_ctx(module: <span class="kw-2">&</span><span class="lifetime">'llvm </span>Module) -> FunctionPassManager<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>fpm = <span class="kw">unsafe </span>{ + <span class="comment">// Borrows module reference. + </span>LLVMCreateFunctionPassManagerForModule(module.module()) }; - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">fpm</span>.<span class="ident">is_null</span>()); + <span class="macro">assert!</span>(!fpm.is_null()); - <span class="kw">unsafe</span> { - <span class="comment">// Do simple "peephole" optimizations and bit-twiddling optzns.</span> - <span class="ident">LLVMAddInstructionCombiningPass</span>(<span class="ident">fpm</span>); - <span class="comment">// Reassociate expressions.</span> - <span class="ident">LLVMAddReassociatePass</span>(<span class="ident">fpm</span>); - <span class="comment">// Eliminate Common SubExpressions.</span> - <span class="ident">LLVMAddNewGVNPass</span>(<span class="ident">fpm</span>); - <span class="comment">// Simplify the control flow graph (deleting unreachable blocks, etc).</span> - <span class="ident">LLVMAddCFGSimplificationPass</span>(<span class="ident">fpm</span>); + <span class="kw">unsafe </span>{ + <span class="comment">// Do simple "peephole" optimizations and bit-twiddling optzns. + </span>LLVMAddInstructionCombiningPass(fpm); + <span class="comment">// Reassociate expressions. + </span>LLVMAddReassociatePass(fpm); + <span class="comment">// Eliminate Common SubExpressions. + </span>LLVMAddNewGVNPass(fpm); + <span class="comment">// Simplify the control flow graph (deleting unreachable blocks, etc). + </span>LLVMAddCFGSimplificationPass(fpm); - <span class="kw">let</span> <span class="ident">fail</span> <span class="op">=</span> <span class="ident">LLVMInitializeFunctionPassManager</span>(<span class="ident">fpm</span>); - <span class="macro">assert_eq!</span>(<span class="ident">fail</span>, <span class="number">0</span>); + <span class="kw">let </span>fail = LLVMInitializeFunctionPassManager(fpm); + <span class="macro">assert_eq!</span>(fail, <span class="number">0</span>); } - <span class="ident">FunctionPassManager</span> { - <span class="ident">fpm</span>, - <span class="ident">_ctx</span>: <span class="ident">PhantomData</span>, + FunctionPassManager { + fpm, + _ctx: PhantomData, } } - <span class="doccomment">/// Run the optimization passes registered with the Function PassManager on the function</span> - <span class="doccomment">/// referenced by `fn_value`.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">run</span>(<span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="self">self</span>, <span class="ident">fn_value</span>: <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { - <span class="kw">unsafe</span> { - <span class="comment">// Returns 1 if any of the passes modified the function, false otherwise.</span> - <span class="ident">LLVMRunFunctionPassManager</span>(<span class="self">self</span>.<span class="ident">fpm</span>, <span class="ident">fn_value</span>.<span class="ident">value_ref</span>()); + <span class="doccomment">/// Run the optimization passes registered with the Function PassManager on the function + /// referenced by `fn_value`. + </span><span class="kw">pub fn </span>run(<span class="kw-2">&</span><span class="lifetime">'llvm </span><span class="self">self</span>, fn_value: FnValue<<span class="lifetime">'llvm</span>>) { + <span class="kw">unsafe </span>{ + <span class="comment">// Returns 1 if any of the passes modified the function, false otherwise. + </span>LLVMRunFunctionPassManager(<span class="self">self</span>.fpm, fn_value.value_ref()); } } } -<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">FunctionPassManager</span><span class="op"><</span><span class="lifetime">'_</span><span class="op">></span> { - <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="kw">unsafe</span> { - <span class="ident">LLVMDisposePassManager</span>(<span class="self">self</span>.<span class="ident">fpm</span>); +<span class="kw">impl </span>Drop <span class="kw">for </span>FunctionPassManager<<span class="lifetime">'_</span>> { + <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ + LLVMDisposePassManager(<span class="self">self</span>.fpm); } } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/type_.rs.html b/src/llvm_kaleidoscope_rs/llvm/type_.rs.html index 98daee4..7df4f4d 100644 --- a/src/llvm_kaleidoscope_rs/llvm/type_.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/type_.rs.html @@ -1,10 +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/llvm/type_.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>type_.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/type_.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>type_.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -62,64 +56,63 @@ <span id="56">56</span> <span id="57">57</span> <span id="58">58</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">llvm_sys</span>::{ - <span class="ident">core</span>::{<span class="ident">LLVMConstReal</span>, <span class="ident">LLVMDumpType</span>, <span class="ident">LLVMGetTypeKind</span>}, - <span class="ident">prelude::LLVMTypeRef</span>, - <span class="ident">LLVMTypeKind</span>, +</pre><pre class="rust"><code><span class="kw">use </span>llvm_sys::{ + core::{LLVMConstReal, LLVMDumpType, LLVMGetTypeKind}, + prelude::LLVMTypeRef, + LLVMTypeKind, }; -<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>; +<span class="kw">use </span>std::marker::PhantomData; -<span class="kw">use</span> <span class="ident"><span class="kw">super</span>::Value</span>; +<span class="kw">use </span><span class="kw">super</span>::Value; -<span class="doccomment">/// Wrapper for a LLVM Type Reference.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Copy</span>, <span class="ident">Clone</span>)]</span> -<span class="attribute">#[<span class="ident">repr</span>(<span class="ident">transparent</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>(<span class="ident">LLVMTypeRef</span>, <span class="ident">PhantomData</span><span class="op"><</span><span class="kw-2">&</span><span class="lifetime">'llvm</span> ()<span class="op">></span>); +<span class="doccomment">/// Wrapper for a LLVM Type Reference. +</span><span class="attribute">#[derive(Copy, Clone)] +#[repr(transparent)] +</span><span class="kw">pub struct </span>Type<<span class="lifetime">'llvm</span>>(LLVMTypeRef, PhantomData<<span class="kw-2">&</span><span class="lifetime">'llvm </span>()>); -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new Type instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if `type_ref` is a null pointer.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">type_ref</span>: <span class="ident">LLVMTypeRef</span>) -> <span class="self">Self</span> { - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">type_ref</span>.<span class="ident">is_null</span>()); - <span class="ident">Type</span>(<span class="ident">type_ref</span>, <span class="ident">PhantomData</span>) +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> Type<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new Type instance. + /// + /// # Panics + /// + /// Panics if `type_ref` is a null pointer. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new(type_ref: LLVMTypeRef) -> <span class="self">Self </span>{ + <span class="macro">assert!</span>(!type_ref.is_null()); + Type(type_ref, PhantomData) } - <span class="doccomment">/// Get the raw LLVM type reference.</span> - <span class="attribute">#[<span class="ident">inline</span>]</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">type_ref</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMTypeRef</span> { - <span class="self">self</span>.<span class="number">0</span> - } + <span class="doccomment">/// Get the raw LLVM type reference. + </span><span class="attribute">#[inline] + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>type_ref(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMTypeRef { + <span class="self">self</span>.<span class="number">0 + </span>} - <span class="doccomment">/// Get the LLVM type kind for the given type reference.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">kind</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMTypeKind</span> { - <span class="kw">unsafe</span> { <span class="ident">LLVMGetTypeKind</span>(<span class="self">self</span>.<span class="ident">type_ref</span>()) } + <span class="doccomment">/// Get the LLVM type kind for the given type reference. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>kind(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMTypeKind { + <span class="kw">unsafe </span>{ LLVMGetTypeKind(<span class="self">self</span>.type_ref()) } } - <span class="doccomment">/// Dump the LLVM Type to stdout.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">dump</span>(<span class="kw-2">&</span><span class="self">self</span>) { - <span class="kw">unsafe</span> { <span class="ident">LLVMDumpType</span>(<span class="self">self</span>.<span class="ident">type_ref</span>()) }; + <span class="doccomment">/// Dump the LLVM Type to stdout. + </span><span class="kw">pub fn </span>dump(<span class="kw-2">&</span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ LLVMDumpType(<span class="self">self</span>.type_ref()) }; } - <span class="doccomment">/// Get a value reference representing the const `f64` value.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">const_f64</span>(<span class="self">self</span>, <span class="ident">n</span>: <span class="ident">f64</span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { + <span class="doccomment">/// Get a value reference representing the const `f64` value. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>const_f64(<span class="self">self</span>, n: f64) -> Value<<span class="lifetime">'llvm</span>> { <span class="macro">debug_assert_eq!</span>( - <span class="self">self</span>.<span class="ident">kind</span>(), - <span class="ident">LLVMTypeKind::LLVMDoubleTypeKind</span>, - <span class="string">"Expected a double type when creating const f64 value!"</span> - ); + <span class="self">self</span>.kind(), + LLVMTypeKind::LLVMDoubleTypeKind, + <span class="string">"Expected a double type when creating const f64 value!" + </span>); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMConstReal</span>(<span class="self">self</span>.<span class="ident">type_ref</span>(), <span class="ident">n</span>) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ LLVMConstReal(<span class="self">self</span>.type_ref(), n) }; + Value::new(value_ref) } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/llvm/value.rs.html b/src/llvm_kaleidoscope_rs/llvm/value.rs.html index eaea9c3..102c957 100644 --- a/src/llvm_kaleidoscope_rs/llvm/value.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/value.rs.html @@ -1,10 +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/llvm/value.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>value.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/llvm/value.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>value.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -241,243 +235,242 @@ <span id="235">235</span> <span id="236">236</span> <span id="237">237</span> -</pre><pre class="rust"><code><span class="attribute">#![<span class="ident">allow</span>(<span class="ident">unused</span>)]</span> - -<span class="kw">use</span> <span class="ident">llvm_sys</span>::{ - <span class="ident">analysis</span>::{<span class="ident">LLVMVerifierFailureAction</span>, <span class="ident">LLVMVerifyFunction</span>}, - <span class="ident">core</span>::{ - <span class="ident">LLVMAddIncoming</span>, <span class="ident">LLVMAppendExistingBasicBlock</span>, <span class="ident">LLVMCountBasicBlocks</span>, <span class="ident">LLVMCountParams</span>, - <span class="ident">LLVMDumpValue</span>, <span class="ident">LLVMGetParam</span>, <span class="ident">LLVMGetReturnType</span>, <span class="ident">LLVMGetValueKind</span>, <span class="ident">LLVMGetValueName2</span>, - <span class="ident">LLVMIsAFunction</span>, <span class="ident">LLVMIsAPHINode</span>, <span class="ident">LLVMSetValueName2</span>, <span class="ident">LLVMTypeOf</span>, +</pre><pre class="rust"><code><span class="attribute">#![allow(unused)] + +</span><span class="kw">use </span>llvm_sys::{ + analysis::{LLVMVerifierFailureAction, LLVMVerifyFunction}, + core::{ + LLVMAddIncoming, LLVMAppendExistingBasicBlock, LLVMCountBasicBlocks, LLVMCountParams, + LLVMDumpValue, LLVMGetParam, LLVMGetReturnType, LLVMGetValueKind, LLVMGetValueName2, + LLVMIsAFunction, LLVMIsAPHINode, LLVMSetValueName2, LLVMTypeOf, }, - <span class="ident">prelude::LLVMValueRef</span>, - <span class="ident">LLVMTypeKind</span>, <span class="ident">LLVMValueKind</span>, + prelude::LLVMValueRef, + LLVMTypeKind, LLVMValueKind, }; -<span class="kw">use</span> <span class="ident">std::ffi::CStr</span>; -<span class="kw">use</span> <span class="ident">std::marker::PhantomData</span>; -<span class="kw">use</span> <span class="ident">std::ops::Deref</span>; - -<span class="kw">use</span> <span class="ident"><span class="kw">super</span>::BasicBlock</span>; -<span class="kw">use</span> <span class="ident"><span class="kw">super</span>::Type</span>; - -<span class="doccomment">/// Wrapper for a LLVM Value Reference.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Copy</span>, <span class="ident">Clone</span>)]</span> -<span class="attribute">#[<span class="ident">repr</span>(<span class="ident">transparent</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>(<span class="ident">LLVMValueRef</span>, <span class="ident">PhantomData</span><span class="op"><</span><span class="kw-2">&</span><span class="lifetime">'llvm</span> ()<span class="op">></span>); - -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new Value instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if `value_ref` is a null pointer.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">value_ref</span>: <span class="ident">LLVMValueRef</span>) -> <span class="self">Self</span> { - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">value_ref</span>.<span class="ident">is_null</span>()); - <span class="ident">Value</span>(<span class="ident">value_ref</span>, <span class="ident">PhantomData</span>) +<span class="kw">use </span>std::ffi::CStr; +<span class="kw">use </span>std::marker::PhantomData; +<span class="kw">use </span>std::ops::Deref; + +<span class="kw">use </span><span class="kw">super</span>::BasicBlock; +<span class="kw">use </span><span class="kw">super</span>::Type; + +<span class="doccomment">/// Wrapper for a LLVM Value Reference. +</span><span class="attribute">#[derive(Copy, Clone)] +#[repr(transparent)] +</span><span class="kw">pub struct </span>Value<<span class="lifetime">'llvm</span>>(LLVMValueRef, PhantomData<<span class="kw-2">&</span><span class="lifetime">'llvm </span>()>); + +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> Value<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new Value instance. + /// + /// # Panics + /// + /// Panics if `value_ref` is a null pointer. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new(value_ref: LLVMValueRef) -> <span class="self">Self </span>{ + <span class="macro">assert!</span>(!value_ref.is_null()); + Value(value_ref, PhantomData) } - <span class="doccomment">/// Get the raw LLVM value reference.</span> - <span class="attribute">#[<span class="ident">inline</span>]</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">value_ref</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMValueRef</span> { - <span class="self">self</span>.<span class="number">0</span> - } + <span class="doccomment">/// Get the raw LLVM value reference. + </span><span class="attribute">#[inline] + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>value_ref(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMValueRef { + <span class="self">self</span>.<span class="number">0 + </span>} - <span class="doccomment">/// Get the LLVM value kind for the given value reference.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">kind</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">LLVMValueKind</span> { - <span class="kw">unsafe</span> { <span class="ident">LLVMGetValueKind</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) } + <span class="doccomment">/// Get the LLVM value kind for the given value reference. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>kind(<span class="kw-2">&</span><span class="self">self</span>) -> LLVMValueKind { + <span class="kw">unsafe </span>{ LLVMGetValueKind(<span class="self">self</span>.value_ref()) } } - <span class="doccomment">/// Check if value is `function` type.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">is_function</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">bool</span> { - <span class="kw">let</span> <span class="ident">cast</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMIsAFunction</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) }; - <span class="op">!</span><span class="ident">cast</span>.<span class="ident">is_null</span>() + <span class="doccomment">/// Check if value is `function` type. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>is_function(<span class="kw-2">&</span><span class="self">self</span>) -> bool { + <span class="kw">let </span>cast = <span class="kw">unsafe </span>{ LLVMIsAFunction(<span class="self">self</span>.value_ref()) }; + !cast.is_null() } - <span class="doccomment">/// Check if value is `phinode` type.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">is_phinode</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">bool</span> { - <span class="kw">let</span> <span class="ident">cast</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMIsAPHINode</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) }; - <span class="op">!</span><span class="ident">cast</span>.<span class="ident">is_null</span>() + <span class="doccomment">/// Check if value is `phinode` type. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>is_phinode(<span class="kw-2">&</span><span class="self">self</span>) -> bool { + <span class="kw">let </span>cast = <span class="kw">unsafe </span>{ LLVMIsAPHINode(<span class="self">self</span>.value_ref()) }; + !cast.is_null() } - <span class="doccomment">/// Dump the LLVM Value to stdout.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">dump</span>(<span class="kw-2">&</span><span class="self">self</span>) { - <span class="kw">unsafe</span> { <span class="ident">LLVMDumpValue</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) }; + <span class="doccomment">/// Dump the LLVM Value to stdout. + </span><span class="kw">pub fn </span>dump(<span class="kw-2">&</span><span class="self">self</span>) { + <span class="kw">unsafe </span>{ LLVMDumpValue(<span class="self">self</span>.value_ref()) }; } - <span class="doccomment">/// Get a type reference representing for the given value reference.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">type_of</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">type_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMTypeOf</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) }; - <span class="ident">Type::new</span>(<span class="ident">type_ref</span>) + <span class="doccomment">/// Get a type reference representing for the given value reference. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>type_of(<span class="kw-2">&</span><span class="self">self</span>) -> Type<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>type_ref = <span class="kw">unsafe </span>{ LLVMTypeOf(<span class="self">self</span>.value_ref()) }; + Type::new(type_ref) } - <span class="doccomment">/// Set the name for the given value reference.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">set_name</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">name</span>: <span class="kw-2">&</span><span class="ident">str</span>) { - <span class="kw">unsafe</span> { <span class="ident">LLVMSetValueName2</span>(<span class="self">self</span>.<span class="ident">value_ref</span>(), <span class="ident">name</span>.<span class="ident">as_ptr</span>().<span class="ident">cast</span>(), <span class="ident">name</span>.<span class="ident">len</span>()) }; + <span class="doccomment">/// Set the name for the given value reference. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>set_name(<span class="kw-2">&</span><span class="self">self</span>, name: <span class="kw-2">&</span>str) { + <span class="kw">unsafe </span>{ LLVMSetValueName2(<span class="self">self</span>.value_ref(), name.as_ptr().cast(), name.len()) }; } - <span class="doccomment">/// Get the name for the given value reference.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">get_name</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="lifetime">'llvm</span> <span class="ident">str</span> { - <span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="kw">unsafe</span> { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">len</span>: <span class="ident">libc::size_t</span> <span class="op">=</span> <span class="number">0</span>; - <span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="ident">LLVMGetValueName2</span>(<span class="self">self</span>.<span class="number">0</span>, <span class="kw-2">&mut</span> <span class="ident">len</span> <span class="kw">as</span> <span class="kw">_</span>); - <span class="macro">assert!</span>(<span class="op">!</span><span class="ident">name</span>.<span class="ident">is_null</span>()); - - <span class="ident">CStr::from_ptr</span>(<span class="ident">name</span>) + <span class="doccomment">/// Get the name for the given value reference. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>get_name(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="lifetime">'llvm </span>str { + <span class="kw">let </span>name = <span class="kw">unsafe </span>{ + <span class="kw">let </span><span class="kw-2">mut </span>len: libc::size_t = <span class="number">0</span>; + <span class="kw">let </span>name = LLVMGetValueName2(<span class="self">self</span>.<span class="number">0</span>, <span class="kw-2">&mut </span>len <span class="kw">as _</span>); + <span class="macro">assert!</span>(!name.is_null()); + + CStr::from_ptr(name) }; - <span class="comment">// TODO: Does this string live for the time of the LLVM context?!</span> - <span class="ident">name</span>.<span class="ident">to_str</span>() - .<span class="ident">expect</span>(<span class="string">"Expected valid UTF8 string from LLVM API"</span>) + <span class="comment">// TODO: Does this string live for the time of the LLVM context?! + </span>name.to_str() + .expect(<span class="string">"Expected valid UTF8 string from LLVM API"</span>) } - <span class="doccomment">/// Check if value is of `f64` type.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">is_f64</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">bool</span> { - <span class="self">self</span>.<span class="ident">type_of</span>().<span class="ident">kind</span>() <span class="op">==</span> <span class="ident">LLVMTypeKind::LLVMDoubleTypeKind</span> + <span class="doccomment">/// Check if value is of `f64` type. + </span><span class="kw">pub fn </span>is_f64(<span class="kw-2">&</span><span class="self">self</span>) -> bool { + <span class="self">self</span>.type_of().kind() == LLVMTypeKind::LLVMDoubleTypeKind } - <span class="doccomment">/// Check if value is of integer type.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">is_int</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">bool</span> { - <span class="self">self</span>.<span class="ident">type_of</span>().<span class="ident">kind</span>() <span class="op">==</span> <span class="ident">LLVMTypeKind::LLVMIntegerTypeKind</span> + <span class="doccomment">/// Check if value is of integer type. + </span><span class="kw">pub fn </span>is_int(<span class="kw-2">&</span><span class="self">self</span>) -> bool { + <span class="self">self</span>.type_of().kind() == LLVMTypeKind::LLVMIntegerTypeKind } } -<span class="doccomment">/// Wrapper for a LLVM Value Reference specialized for contexts where function values are needed.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Copy</span>, <span class="ident">Clone</span>)]</span> -<span class="attribute">#[<span class="ident">repr</span>(<span class="ident">transparent</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>(<span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>); +<span class="doccomment">/// Wrapper for a LLVM Value Reference specialized for contexts where function values are needed. +</span><span class="attribute">#[derive(Copy, Clone)] +#[repr(transparent)] +</span><span class="kw">pub struct </span>FnValue<<span class="lifetime">'llvm</span>>(Value<<span class="lifetime">'llvm</span>>); -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">Deref</span> <span class="kw">for</span> <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">type</span> <span class="ident">Target</span> <span class="op">=</span> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>; - <span class="kw">fn</span> <span class="ident">deref</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="ident"><span class="self">Self</span>::Target</span> { - <span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span> - } +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> Deref <span class="kw">for </span>FnValue<<span class="lifetime">'llvm</span>> { + <span class="kw">type </span>Target = Value<<span class="lifetime">'llvm</span>>; + <span class="kw">fn </span>deref(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="self">Self</span>::Target { + <span class="kw-2">&</span><span class="self">self</span>.<span class="number">0 + </span>} } -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">FnValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new FnValue instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if `value_ref` is a null pointer.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">value_ref</span>: <span class="ident">LLVMValueRef</span>) -> <span class="self">Self</span> { - <span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="ident">Value::new</span>(<span class="ident">value_ref</span>); +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> FnValue<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new FnValue instance. + /// + /// # Panics + /// + /// Panics if `value_ref` is a null pointer. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new(value_ref: LLVMValueRef) -> <span class="self">Self </span>{ + <span class="kw">let </span>value = Value::new(value_ref); <span class="macro">debug_assert!</span>( - <span class="ident">value</span>.<span class="ident">is_function</span>(), - <span class="string">"Expected a fn value when constructing FnValue!"</span> - ); + value.is_function(), + <span class="string">"Expected a fn value when constructing FnValue!" + </span>); - <span class="ident">FnValue</span>(<span class="ident">value</span>) + FnValue(value) } - <span class="doccomment">/// Get a type reference representing the return value of the given function value.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">ret_type</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">Type</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">type_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMGetReturnType</span>(<span class="ident">LLVMTypeOf</span>(<span class="self">self</span>.<span class="ident">value_ref</span>())) }; - <span class="ident">Type::new</span>(<span class="ident">type_ref</span>) + <span class="doccomment">/// Get a type reference representing the return value of the given function value. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer. + </span><span class="kw">pub fn </span>ret_type(<span class="kw-2">&</span><span class="self">self</span>) -> Type<<span class="lifetime">'llvm</span>> { + <span class="kw">let </span>type_ref = <span class="kw">unsafe </span>{ LLVMGetReturnType(LLVMTypeOf(<span class="self">self</span>.value_ref())) }; + Type::new(type_ref) } - <span class="doccomment">/// Get the number of function arguments for the given function value.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">args</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">usize</span> { - <span class="kw">unsafe</span> { <span class="ident">LLVMCountParams</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) <span class="kw">as</span> <span class="ident">usize</span> } + <span class="doccomment">/// Get the number of function arguments for the given function value. + </span><span class="kw">pub fn </span>args(<span class="kw-2">&</span><span class="self">self</span>) -> usize { + <span class="kw">unsafe </span>{ LLVMCountParams(<span class="self">self</span>.value_ref()) <span class="kw">as </span>usize } } - <span class="doccomment">/// Get a value reference for the function argument at index `idx`.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if LLVM API returns a `null` pointer or indexed out of bounds.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">arg</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">idx</span>: <span class="ident">usize</span>) -> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="macro">assert!</span>(<span class="ident">idx</span> <span class="op"><</span> <span class="self">self</span>.<span class="ident">args</span>()); + <span class="doccomment">/// Get a value reference for the function argument at index `idx`. + /// + /// # Panics + /// + /// Panics if LLVM API returns a `null` pointer or indexed out of bounds. + </span><span class="kw">pub fn </span>arg(<span class="kw-2">&</span><span class="self">self</span>, idx: usize) -> Value<<span class="lifetime">'llvm</span>> { + <span class="macro">assert!</span>(idx < <span class="self">self</span>.args()); - <span class="kw">let</span> <span class="ident">value_ref</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">LLVMGetParam</span>(<span class="self">self</span>.<span class="ident">value_ref</span>(), <span class="ident">idx</span> <span class="kw">as</span> <span class="ident">libc::c_uint</span>) }; - <span class="ident">Value::new</span>(<span class="ident">value_ref</span>) + <span class="kw">let </span>value_ref = <span class="kw">unsafe </span>{ LLVMGetParam(<span class="self">self</span>.value_ref(), idx <span class="kw">as </span>libc::c_uint) }; + Value::new(value_ref) } - <span class="doccomment">/// Get the number of Basic Blocks for the given function value.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">basic_blocks</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">usize</span> { - <span class="kw">unsafe</span> { <span class="ident">LLVMCountBasicBlocks</span>(<span class="self">self</span>.<span class="ident">value_ref</span>()) <span class="kw">as</span> <span class="ident">usize</span> } + <span class="doccomment">/// Get the number of Basic Blocks for the given function value. + </span><span class="kw">pub fn </span>basic_blocks(<span class="kw-2">&</span><span class="self">self</span>) -> usize { + <span class="kw">unsafe </span>{ LLVMCountBasicBlocks(<span class="self">self</span>.value_ref()) <span class="kw">as </span>usize } } - <span class="doccomment">/// Append a Basic Block to the end of the function value.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">append_basic_block</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">bb</span>: <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { - <span class="kw">unsafe</span> { - <span class="ident">LLVMAppendExistingBasicBlock</span>(<span class="self">self</span>.<span class="ident">value_ref</span>(), <span class="ident">bb</span>.<span class="ident">bb_ref</span>()); + <span class="doccomment">/// Append a Basic Block to the end of the function value. + </span><span class="kw">pub fn </span>append_basic_block(<span class="kw-2">&</span><span class="self">self</span>, bb: BasicBlock<<span class="lifetime">'llvm</span>>) { + <span class="kw">unsafe </span>{ + LLVMAppendExistingBasicBlock(<span class="self">self</span>.value_ref(), bb.bb_ref()); } } - <span class="doccomment">/// Verify that the given function is valid.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">verify</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">bool</span> { - <span class="kw">unsafe</span> { - <span class="ident">LLVMVerifyFunction</span>( - <span class="self">self</span>.<span class="ident">value_ref</span>(), - <span class="ident">LLVMVerifierFailureAction::LLVMPrintMessageAction</span>, - ) <span class="op">==</span> <span class="number">0</span> - } + <span class="doccomment">/// Verify that the given function is valid. + </span><span class="kw">pub fn </span>verify(<span class="kw-2">&</span><span class="self">self</span>) -> bool { + <span class="kw">unsafe </span>{ + LLVMVerifyFunction( + <span class="self">self</span>.value_ref(), + LLVMVerifierFailureAction::LLVMPrintMessageAction, + ) == <span class="number">0 + </span>} } } -<span class="doccomment">/// Wrapper for a LLVM Value Reference specialized for contexts where phi values are needed.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Copy</span>, <span class="ident">Clone</span>)]</span> -<span class="attribute">#[<span class="ident">repr</span>(<span class="ident">transparent</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">PhiValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>(<span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>); +<span class="doccomment">/// Wrapper for a LLVM Value Reference specialized for contexts where phi values are needed. +</span><span class="attribute">#[derive(Copy, Clone)] +#[repr(transparent)] +</span><span class="kw">pub struct </span>PhiValue<<span class="lifetime">'llvm</span>>(Value<<span class="lifetime">'llvm</span>>); -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">Deref</span> <span class="kw">for</span> <span class="ident">PhiValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="kw">type</span> <span class="ident">Target</span> <span class="op">=</span> <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>; - <span class="kw">fn</span> <span class="ident">deref</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="ident"><span class="self">Self</span>::Target</span> { - <span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span> - } +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> Deref <span class="kw">for </span>PhiValue<<span class="lifetime">'llvm</span>> { + <span class="kw">type </span>Target = Value<<span class="lifetime">'llvm</span>>; + <span class="kw">fn </span>deref(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="self">Self</span>::Target { + <span class="kw-2">&</span><span class="self">self</span>.<span class="number">0 + </span>} } -<span class="kw">impl</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> <span class="ident">PhiValue</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span> { - <span class="doccomment">/// Create a new PhiValue instance.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Panics if `value_ref` is a null pointer.</span> - <span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">value_ref</span>: <span class="ident">LLVMValueRef</span>) -> <span class="self">Self</span> { - <span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="ident">Value::new</span>(<span class="ident">value_ref</span>); +<span class="kw">impl</span><<span class="lifetime">'llvm</span>> PhiValue<<span class="lifetime">'llvm</span>> { + <span class="doccomment">/// Create a new PhiValue instance. + /// + /// # Panics + /// + /// Panics if `value_ref` is a null pointer. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new(value_ref: LLVMValueRef) -> <span class="self">Self </span>{ + <span class="kw">let </span>value = Value::new(value_ref); <span class="macro">debug_assert!</span>( - <span class="ident">value</span>.<span class="ident">is_phinode</span>(), - <span class="string">"Expected a phinode value when constructing PhiValue!"</span> - ); + value.is_phinode(), + <span class="string">"Expected a phinode value when constructing PhiValue!" + </span>); - <span class="ident">PhiValue</span>(<span class="ident">value</span>) + PhiValue(value) } - <span class="doccomment">/// Add an incoming value to the end of a PHI list.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">add_incoming</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">ival</span>: <span class="ident">Value</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>, <span class="ident">ibb</span>: <span class="ident">BasicBlock</span><span class="op"><</span><span class="lifetime">'llvm</span><span class="op">></span>) { + <span class="doccomment">/// Add an incoming value to the end of a PHI list. + </span><span class="kw">pub fn </span>add_incoming(<span class="kw-2">&</span><span class="self">self</span>, ival: Value<<span class="lifetime">'llvm</span>>, ibb: BasicBlock<<span class="lifetime">'llvm</span>>) { <span class="macro">debug_assert_eq!</span>( - <span class="ident">ival</span>.<span class="ident">type_of</span>().<span class="ident">kind</span>(), - <span class="self">self</span>.<span class="ident">type_of</span>().<span class="ident">kind</span>(), - <span class="string">"Type of incoming phi value must be the same as the type used to build the phi node."</span> - ); - - <span class="kw">unsafe</span> { - <span class="ident">LLVMAddIncoming</span>( - <span class="self">self</span>.<span class="ident">value_ref</span>(), - <span class="kw-2">&mut</span> <span class="ident">ival</span>.<span class="ident">value_ref</span>() <span class="kw">as</span> <span class="kw">_</span>, - <span class="kw-2">&mut</span> <span class="ident">ibb</span>.<span class="ident">bb_ref</span>() <span class="kw">as</span> <span class="kw">_</span>, + ival.type_of().kind(), + <span class="self">self</span>.type_of().kind(), + <span class="string">"Type of incoming phi value must be the same as the type used to build the phi node." + </span>); + + <span class="kw">unsafe </span>{ + LLVMAddIncoming( + <span class="self">self</span>.value_ref(), + <span class="kw-2">&mut </span>ival.value_ref() <span class="kw">as _</span>, + <span class="kw-2">&mut </span>ibb.bb_ref() <span class="kw">as _</span>, <span class="number">1</span>, ); } } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file diff --git a/src/llvm_kaleidoscope_rs/parser.rs.html b/src/llvm_kaleidoscope_rs/parser.rs.html index d3c9594..1751341 100644 --- a/src/llvm_kaleidoscope_rs/parser.rs.html +++ b/src/llvm_kaleidoscope_rs/parser.rs.html @@ -1,10 +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/parser.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>parser.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" 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/parser.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>parser.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" 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> @@ -658,660 +652,659 @@ <span id="652">652</span> <span id="653">653</span> <span id="654">654</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::lexer</span>::{<span class="ident">Lexer</span>, <span class="ident">Token</span>}; +</pre><pre class="rust"><code><span class="kw">use </span><span class="kw">crate</span>::lexer::{Lexer, Token}; -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span> -<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">ExprAST</span> { - <span class="doccomment">/// Number - Expression class for numeric literals like "1.0".</span> - <span class="ident">Number</span>(<span class="ident">f64</span>), +<span class="attribute">#[derive(Debug, PartialEq)] +</span><span class="kw">pub enum </span>ExprAST { + <span class="doccomment">/// Number - Expression class for numeric literals like "1.0". + </span>Number(f64), - <span class="doccomment">/// Variable - Expression class for referencing a variable, like "a".</span> - <span class="ident">Variable</span>(<span class="ident">String</span>), + <span class="doccomment">/// Variable - Expression class for referencing a variable, like "a". + </span>Variable(String), - <span class="doccomment">/// Binary - Expression class for a binary operator.</span> - <span class="ident">Binary</span>(<span class="ident">char</span>, <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>), + <span class="doccomment">/// Binary - Expression class for a binary operator. + </span>Binary(char, Box<ExprAST>, Box<ExprAST>), - <span class="doccomment">/// Call - Expression class for function calls.</span> - <span class="ident">Call</span>(<span class="ident">String</span>, <span class="ident">Vec</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>), + <span class="doccomment">/// Call - Expression class for function calls. + </span>Call(String, Vec<ExprAST>), - <span class="doccomment">/// If - Expression class for if/then/else.</span> - <span class="ident">If</span> { - <span class="ident">cond</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">then</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">else_</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, + <span class="doccomment">/// If - Expression class for if/then/else. + </span>If { + cond: Box<ExprAST>, + then: Box<ExprAST>, + else_: Box<ExprAST>, }, - <span class="doccomment">/// ForExprAST - Expression class for for/in.</span> - <span class="ident">For</span> { - <span class="ident">var</span>: <span class="ident">String</span>, - <span class="ident">start</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">end</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">step</span>: <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span><span class="op">></span>, - <span class="ident">body</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, + <span class="doccomment">/// ForExprAST - Expression class for for/in. + </span>For { + var: String, + start: Box<ExprAST>, + end: Box<ExprAST>, + step: <span class="prelude-ty">Option</span><Box<ExprAST>>, + body: Box<ExprAST>, }, } -<span class="doccomment">/// PrototypeAST - This class represents the "prototype" for a function,</span> -<span class="doccomment">/// which captures its name, and its argument names (thus implicitly the number</span> -<span class="doccomment">/// of arguments the function takes).</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>, <span class="ident">Clone</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">PrototypeAST</span>(<span class="kw">pub</span> <span class="ident">String</span>, <span class="kw">pub</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">String</span><span class="op">></span>); +<span class="doccomment">/// PrototypeAST - This class represents the "prototype" for a function, +/// which captures its name, and its argument names (thus implicitly the number +/// of arguments the function takes). +</span><span class="attribute">#[derive(Debug, PartialEq, Clone)] +</span><span class="kw">pub struct </span>PrototypeAST(<span class="kw">pub </span>String, <span class="kw">pub </span>Vec<String>); -<span class="doccomment">/// FunctionAST - This class represents a function definition itself.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">FunctionAST</span>(<span class="kw">pub</span> <span class="ident">PrototypeAST</span>, <span class="kw">pub</span> <span class="ident">ExprAST</span>); +<span class="doccomment">/// FunctionAST - This class represents a function definition itself. +</span><span class="attribute">#[derive(Debug, PartialEq)] +</span><span class="kw">pub struct </span>FunctionAST(<span class="kw">pub </span>PrototypeAST, <span class="kw">pub </span>ExprAST); -<span class="doccomment">/// Parse result with String as Error type (to be compliant with tutorial).</span> -<span class="kw">type</span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="op">=</span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">T</span>, <span class="ident">String</span><span class="op">></span>; +<span class="doccomment">/// Parse result with String as Error type (to be compliant with tutorial). +</span><span class="kw">type </span>ParseResult<T> = <span class="prelude-ty">Result</span><T, String>; -<span class="doccomment">/// Parser for the `kaleidoscope` language.</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Parser</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> -<span class="kw">where</span> - <span class="ident">I</span>: <span class="ident">Iterator</span><span class="op"><</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">char</span><span class="op">></span>, +<span class="doccomment">/// Parser for the `kaleidoscope` language. +</span><span class="kw">pub struct </span>Parser<I> +<span class="kw">where + </span>I: Iterator<Item = char>, { - <span class="ident">lexer</span>: <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span>, - <span class="ident">cur_tok</span>: <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">Token</span><span class="op">></span>, + lexer: Lexer<I>, + cur_tok: <span class="prelude-ty">Option</span><Token>, } -<span class="kw">impl</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> <span class="ident">Parser</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> -<span class="kw">where</span> - <span class="ident">I</span>: <span class="ident">Iterator</span><span class="op"><</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">char</span><span class="op">></span>, +<span class="kw">impl</span><I> Parser<I> +<span class="kw">where + </span>I: Iterator<Item = char>, { - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">lexer</span>: <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span>) -> <span class="self">Self</span> { - <span class="ident">Parser</span> { - <span class="ident">lexer</span>, - <span class="ident">cur_tok</span>: <span class="prelude-val">None</span>, + <span class="kw">pub fn </span>new(lexer: Lexer<I>) -> <span class="self">Self </span>{ + Parser { + lexer, + cur_tok: <span class="prelude-val">None</span>, } } - <span class="comment">// -----------------------</span> - <span class="comment">// Simple Token Buffer</span> - <span class="comment">// -----------------------</span> + <span class="comment">// ----------------------- + // Simple Token Buffer + // ----------------------- - <span class="doccomment">/// Implement the global variable `int CurTok;` from the tutorial.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">/// Panics if the parser doesn't have a current token.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">cur_tok</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="ident">Token</span> { - <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">as_ref</span>().<span class="ident">expect</span>(<span class="string">"Parser: Expected cur_token!"</span>) + </span><span class="doccomment">/// Implement the global variable `int CurTok;` from the tutorial. + /// + /// # Panics + /// Panics if the parser doesn't have a current token. + </span><span class="kw">pub fn </span>cur_tok(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span>Token { + <span class="self">self</span>.cur_tok.as_ref().expect(<span class="string">"Parser: Expected cur_token!"</span>) } - <span class="doccomment">/// Advance the `cur_tok` by getting the next token from the lexer.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement the fucntion `int getNextToken();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">get_next_token</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="self">self</span>.<span class="ident">cur_tok</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">lexer</span>.<span class="ident">gettok</span>()); + <span class="doccomment">/// Advance the `cur_tok` by getting the next token from the lexer. + /// + /// Implement the fucntion `int getNextToken();` from the tutorial. + </span><span class="kw">pub fn </span>get_next_token(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="self">self</span>.cur_tok = <span class="prelude-val">Some</span>(<span class="self">self</span>.lexer.gettok()); } - <span class="comment">// ----------------------------</span> - <span class="comment">// Basic Expression Parsing</span> - <span class="comment">// ----------------------------</span> - - <span class="doccomment">/// numberexpr ::= number</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseNumberExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_num_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() { - <span class="ident">Token::Number</span>(<span class="ident">num</span>) => { - <span class="comment">// Consume the number token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Number</span>(<span class="ident">num</span>)) + <span class="comment">// ---------------------------- + // Basic Expression Parsing + // ---------------------------- + + </span><span class="doccomment">/// numberexpr ::= number + /// + /// Implement `std::unique_ptr<ExprAST> ParseNumberExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_num_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">match </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() { + Token::Number(num) => { + <span class="comment">// Consume the number token. + </span><span class="self">self</span>.get_next_token(); + <span class="prelude-val">Ok</span>(ExprAST::Number(num)) } - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), } } - <span class="doccomment">/// parenexpr ::= '(' expression ')'</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseParenExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_paren_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="comment">// Eat '(' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Char</span>(<span class="string">'('</span>)); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - - <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; - - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">==</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { - <span class="comment">// Eat ')' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="prelude-val">Ok</span>(<span class="ident">v</span>) - } <span class="kw">else</span> { - <span class="prelude-val">Err</span>(<span class="string">"expected ')'"</span>.<span class="ident">into</span>()) + <span class="doccomment">/// parenexpr ::= '(' expression ')' + /// + /// Implement `std::unique_ptr<ExprAST> ParseParenExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_paren_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="comment">// Eat '(' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Char(<span class="string">'('</span>)); + <span class="self">self</span>.get_next_token(); + + <span class="kw">let </span>v = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; + + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() == Token::Char(<span class="string">')'</span>) { + <span class="comment">// Eat ')' token. + </span><span class="self">self</span>.get_next_token(); + <span class="prelude-val">Ok</span>(v) + } <span class="kw">else </span>{ + <span class="prelude-val">Err</span>(<span class="string">"expected ')'"</span>.into()) } } - <span class="doccomment">/// identifierexpr</span> - <span class="doccomment">/// ::= identifier</span> - <span class="doccomment">/// ::= identifier '(' expression* ')'</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseIdentifierExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_identifier_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">id_name</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Identifier</span>(<span class="ident">id</span>)) => { - <span class="comment">// Consume identifier.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="ident">id</span> + <span class="doccomment">/// identifierexpr + /// ::= identifier + /// ::= identifier '(' expression* ')' + /// + /// Implement `std::unique_ptr<ExprAST> ParseIdentifierExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_identifier_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">let </span>id_name = <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Identifier(id)) => { + <span class="comment">// Consume identifier. + </span><span class="self">self</span>.get_next_token(); + id } - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), }; - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">'('</span>) { - <span class="comment">// Simple variable reference.</span> - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Variable</span>(<span class="ident">id_name</span>)) - } <span class="kw">else</span> { - <span class="comment">// Call.</span> + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">'('</span>) { + <span class="comment">// Simple variable reference. + </span><span class="prelude-val">Ok</span>(ExprAST::Variable(id_name)) + } <span class="kw">else </span>{ + <span class="comment">// Call. - <span class="comment">// Eat '(' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + // Eat '(' token. + </span><span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">args</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> <span class="op">=</span> <span class="ident">Vec::new</span>(); + <span class="kw">let </span><span class="kw-2">mut </span>args: Vec<ExprAST> = Vec::new(); - <span class="comment">// If there are arguments collect them.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { - <span class="kw">loop</span> { - <span class="kw">let</span> <span class="ident">arg</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; - <span class="ident">args</span>.<span class="ident">push</span>(<span class="ident">arg</span>); + <span class="comment">// If there are arguments collect them. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">')'</span>) { + <span class="kw">loop </span>{ + <span class="kw">let </span>arg = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; + args.push(arg); - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">==</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() == Token::Char(<span class="string">')'</span>) { <span class="kw">break</span>; } - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">','</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected ')' or ',' in argument list"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">','</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected ')' or ',' in argument list"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); } } - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Char</span>(<span class="string">')'</span>)); - <span class="comment">// Eat ')' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Char(<span class="string">')'</span>)); + <span class="comment">// Eat ')' token. + </span><span class="self">self</span>.get_next_token(); - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Call</span>(<span class="ident">id_name</span>, <span class="ident">args</span>)) + <span class="prelude-val">Ok</span>(ExprAST::Call(id_name, args)) } } - <span class="doccomment">/// ifexpr ::= 'if' expression 'then' expression 'else' expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseIfExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_if_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="comment">// Consume 'if' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::If</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="doccomment">/// ifexpr ::= 'if' expression 'then' expression 'else' expression + /// + /// Implement `std::unique_ptr<ExprAST> ParseIfExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_if_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="comment">// Consume 'if' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::If); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">cond</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>cond = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="kw">if</span> <span class="kw-2">*</span><span class="macro">dbg!</span>(<span class="self">self</span>.<span class="ident">cur_tok</span>()) <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Then</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected 'then'"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="macro">dbg!</span>(<span class="self">self</span>.cur_tok()) != Token::Then { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected 'then'"</span>.into()); } - <span class="comment">// Consume 'then' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Consume 'then' token. + </span><span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">then</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>then = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Else</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected 'else'"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Else { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected 'else'"</span>.into()); } - <span class="comment">// Consume 'else' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Consume 'else' token. + </span><span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">else_</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>else_ = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { - <span class="ident">cond</span>: <span class="ident">Box::new</span>(<span class="ident">cond</span>), - <span class="ident">then</span>: <span class="ident">Box::new</span>(<span class="ident">then</span>), - <span class="ident">else_</span>: <span class="ident">Box::new</span>(<span class="ident">else_</span>), + <span class="prelude-val">Ok</span>(ExprAST::If { + cond: Box::new(cond), + then: Box::new(then), + else_: Box::new(else_), }) } - <span class="doccomment">/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseForExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_for_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="comment">// Consume the 'for' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::For</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - - <span class="kw">let</span> <span class="ident">var</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span> - .<span class="ident">parse_identifier_expr</span>() - .<span class="ident">map_err</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="ident">String::from</span>(<span class="string">"expected identifier after 'for'"</span>))<span class="question-mark">?</span> - { - <span class="ident">ExprAST::Variable</span>(<span class="ident">var</span>) => <span class="ident">var</span>, - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="doccomment">/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression + /// + /// Implement `std::unique_ptr<ExprAST> ParseForExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_for_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="comment">// Consume the 'for' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::For); + <span class="self">self</span>.get_next_token(); + + <span class="kw">let </span>var = <span class="kw">match </span><span class="self">self + </span>.parse_identifier_expr() + .map_err(|<span class="kw">_</span>| String::from(<span class="string">"expected identifier after 'for'"</span>))<span class="question-mark">? + </span>{ + ExprAST::Variable(var) => var, + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), }; - <span class="comment">// Consume the '=' token.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">'='</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"expected '=' after for"</span>.<span class="ident">into</span>()); + <span class="comment">// Consume the '=' token. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">'='</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"expected '=' after for"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>start = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="comment">// Consume the ',' token.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">','</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"expected ',' after for start value"</span>.<span class="ident">into</span>()); + <span class="comment">// Consume the ',' token. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">','</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"expected ',' after for start value"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">end</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>end = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">step</span> <span class="op">=</span> <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">==</span> <span class="ident">Token::Char</span>(<span class="string">','</span>) { - <span class="comment">// Consume the ',' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="kw">let </span>step = <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() == Token::Char(<span class="string">','</span>) { + <span class="comment">// Consume the ',' token. + </span><span class="self">self</span>.get_next_token(); - <span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>) - } <span class="kw">else</span> { - <span class="prelude-val">None</span> - }; + <span class="prelude-val">Some</span>(<span class="self">self</span>.parse_expression()<span class="question-mark">?</span>) + } <span class="kw">else </span>{ + <span class="prelude-val">None + </span>}; - <span class="comment">// Consume the 'in' token.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::In</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"expected 'in' after for"</span>.<span class="ident">into</span>()); + <span class="comment">// Consume the 'in' token. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::In { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"expected 'in' after for"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>body = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>: <span class="ident">Box::new</span>(<span class="ident">start</span>), - <span class="ident">end</span>: <span class="ident">Box::new</span>(<span class="ident">end</span>), - <span class="ident">step</span>: <span class="ident">step</span>.<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="ident">Box::new</span>(<span class="ident">s</span>)), - <span class="ident">body</span>: <span class="ident">Box::new</span>(<span class="ident">body</span>), + <span class="prelude-val">Ok</span>(ExprAST::For { + var, + start: Box::new(start), + end: Box::new(end), + step: step.map(|s| Box::new(s)), + body: Box::new(body), }) } - <span class="doccomment">/// primary</span> - <span class="doccomment">/// ::= identifierexpr</span> - <span class="doccomment">/// ::= numberexpr</span> - <span class="doccomment">/// ::= parenexpr</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParsePrimary();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_primary</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() { - <span class="ident">Token::Identifier</span>(<span class="kw">_</span>) => <span class="self">self</span>.<span class="ident">parse_identifier_expr</span>(), - <span class="ident">Token::Number</span>(<span class="kw">_</span>) => <span class="self">self</span>.<span class="ident">parse_num_expr</span>(), - <span class="ident">Token::Char</span>(<span class="string">'('</span>) => <span class="self">self</span>.<span class="ident">parse_paren_expr</span>(), - <span class="ident">Token::If</span> => <span class="self">self</span>.<span class="ident">parse_if_expr</span>(), - <span class="ident">Token::For</span> => <span class="self">self</span>.<span class="ident">parse_for_expr</span>(), - <span class="kw">_</span> => <span class="prelude-val">Err</span>(<span class="string">"unknown token when expecting an expression"</span>.<span class="ident">into</span>()), + <span class="doccomment">/// primary + /// ::= identifierexpr + /// ::= numberexpr + /// ::= parenexpr + /// + /// Implement `std::unique_ptr<ExprAST> ParsePrimary();` from the tutorial. + </span><span class="kw">fn </span>parse_primary(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">match </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() { + Token::Identifier(<span class="kw">_</span>) => <span class="self">self</span>.parse_identifier_expr(), + Token::Number(<span class="kw">_</span>) => <span class="self">self</span>.parse_num_expr(), + Token::Char(<span class="string">'('</span>) => <span class="self">self</span>.parse_paren_expr(), + Token::If => <span class="self">self</span>.parse_if_expr(), + Token::For => <span class="self">self</span>.parse_for_expr(), + <span class="kw">_ </span>=> <span class="prelude-val">Err</span>(<span class="string">"unknown token when expecting an expression"</span>.into()), } } - <span class="comment">// -----------------------------</span> - <span class="comment">// Binary Expression Parsing</span> - <span class="comment">// -----------------------------</span> - - <span class="doccomment">/// /// expression</span> - <span class="doccomment">/// ::= primary binoprhs</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseExpression();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_expression</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">lhs</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_primary</span>()<span class="question-mark">?</span>; - <span class="self">self</span>.<span class="ident">parse_bin_op_rhs</span>(<span class="number">0</span>, <span class="ident">lhs</span>) + <span class="comment">// ----------------------------- + // Binary Expression Parsing + // ----------------------------- + + </span><span class="doccomment">/// /// expression + /// ::= primary binoprhs + /// + /// Implement `std::unique_ptr<ExprAST> ParseExpression();` from the tutorial. + </span><span class="kw">fn </span>parse_expression(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">let </span>lhs = <span class="self">self</span>.parse_primary()<span class="question-mark">?</span>; + <span class="self">self</span>.parse_bin_op_rhs(<span class="number">0</span>, lhs) } - <span class="doccomment">/// binoprhs</span> - <span class="doccomment">/// ::= ('+' primary)*</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_bin_op_rhs</span>(<span class="kw-2">&mut</span> <span class="self">self</span>, <span class="ident">expr_prec</span>: <span class="ident">isize</span>, <span class="kw-2">mut</span> <span class="ident">lhs</span>: <span class="ident">ExprAST</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">loop</span> { - <span class="kw">let</span> <span class="ident">tok_prec</span> <span class="op">=</span> <span class="ident">get_tok_precedence</span>(<span class="self">self</span>.<span class="ident">cur_tok</span>()); - - <span class="comment">// Not a binary operator or precedence is too small.</span> - <span class="kw">if</span> <span class="ident">tok_prec</span> <span class="op"><</span> <span class="ident">expr_prec</span> { - <span class="kw">return</span> <span class="prelude-val">Ok</span>(<span class="ident">lhs</span>); + <span class="doccomment">/// binoprhs + /// ::= ('+' primary)* + /// + /// Implement `std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);` from the tutorial. + </span><span class="kw">fn </span>parse_bin_op_rhs(<span class="kw-2">&mut </span><span class="self">self</span>, expr_prec: isize, <span class="kw-2">mut </span>lhs: ExprAST) -> ParseResult<ExprAST> { + <span class="kw">loop </span>{ + <span class="kw">let </span>tok_prec = get_tok_precedence(<span class="self">self</span>.cur_tok()); + + <span class="comment">// Not a binary operator or precedence is too small. + </span><span class="kw">if </span>tok_prec < expr_prec { + <span class="kw">return </span><span class="prelude-val">Ok</span>(lhs); } - <span class="kw">let</span> <span class="ident">binop</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Char</span>(<span class="ident">c</span>)) => { - <span class="comment">// Eat binary operator.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="ident">c</span> + <span class="kw">let </span>binop = <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Char(c)) => { + <span class="comment">// Eat binary operator. + </span><span class="self">self</span>.get_next_token(); + c } - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), }; - <span class="comment">// lhs BINOP1 rhs BINOP2 remrhs</span> - <span class="comment">// ^^^^^^ ^^^^^^</span> - <span class="comment">// tok_prec next_prec</span> - <span class="comment">//</span> - <span class="comment">// In case BINOP1 has higher precedence, we are done here and can build a 'Binary' AST</span> - <span class="comment">// node between 'lhs' and 'rhs'.</span> - <span class="comment">//</span> - <span class="comment">// In case BINOP2 has higher precedence, we take 'rhs' as 'lhs' and recurse into the</span> - <span class="comment">// 'remrhs' expression first.</span> - - <span class="comment">// Parse primary expression after binary operator.</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rhs</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_primary</span>()<span class="question-mark">?</span>; - - <span class="kw">let</span> <span class="ident">next_prec</span> <span class="op">=</span> <span class="ident">get_tok_precedence</span>(<span class="self">self</span>.<span class="ident">cur_tok</span>()); - <span class="kw">if</span> <span class="ident">tok_prec</span> <span class="op"><</span> <span class="ident">next_prec</span> { - <span class="comment">// BINOP2 has higher precedence thatn BINOP1, recurse into 'remhs'.</span> - <span class="ident">rhs</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_bin_op_rhs</span>(<span class="ident">tok_prec</span> <span class="op">+</span> <span class="number">1</span>, <span class="ident">rhs</span>)<span class="question-mark">?</span> - } - - <span class="ident">lhs</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>(<span class="ident">binop</span>, <span class="ident">Box::new</span>(<span class="ident">lhs</span>), <span class="ident">Box::new</span>(<span class="ident">rhs</span>)); + <span class="comment">// lhs BINOP1 rhs BINOP2 remrhs + // ^^^^^^ ^^^^^^ + // tok_prec next_prec + // + // In case BINOP1 has higher precedence, we are done here and can build a 'Binary' AST + // node between 'lhs' and 'rhs'. + // + // In case BINOP2 has higher precedence, we take 'rhs' as 'lhs' and recurse into the + // 'remrhs' expression first. + + // Parse primary expression after binary operator. + </span><span class="kw">let </span><span class="kw-2">mut </span>rhs = <span class="self">self</span>.parse_primary()<span class="question-mark">?</span>; + + <span class="kw">let </span>next_prec = get_tok_precedence(<span class="self">self</span>.cur_tok()); + <span class="kw">if </span>tok_prec < next_prec { + <span class="comment">// BINOP2 has higher precedence thatn BINOP1, recurse into 'remhs'. + </span>rhs = <span class="self">self</span>.parse_bin_op_rhs(tok_prec + <span class="number">1</span>, rhs)<span class="question-mark">? + </span>} + + lhs = ExprAST::Binary(binop, Box::new(lhs), Box::new(rhs)); } } - <span class="comment">// --------------------</span> - <span class="comment">// Parsing the Rest</span> - <span class="comment">// --------------------</span> - - <span class="doccomment">/// prototype</span> - <span class="doccomment">/// ::= id '(' id* ')'</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<PrototypeAST> ParsePrototype();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_prototype</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">PrototypeAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">id_name</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Identifier</span>(<span class="ident">id</span>)) => { - <span class="comment">// Consume the identifier.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="ident">id</span> + <span class="comment">// -------------------- + // Parsing the Rest + // -------------------- + + </span><span class="doccomment">/// prototype + /// ::= id '(' id* ')' + /// + /// Implement `std::unique_ptr<PrototypeAST> ParsePrototype();` from the tutorial. + </span><span class="kw">fn </span>parse_prototype(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<PrototypeAST> { + <span class="kw">let </span>id_name = <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Identifier(id)) => { + <span class="comment">// Consume the identifier. + </span><span class="self">self</span>.get_next_token(); + id } - <span class="ident">other</span> => { - <span class="comment">// Plug back current token.</span> - <span class="self">self</span>.<span class="ident">cur_tok</span> <span class="op">=</span> <span class="ident">other</span>; - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected function name in prototype"</span>.<span class="ident">into</span>()); + other => { + <span class="comment">// Plug back current token. + </span><span class="self">self</span>.cur_tok = other; + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected function name in prototype"</span>.into()); } }; - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">'('</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected '(' in prototype"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">'('</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected '(' in prototype"</span>.into()); } - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">args</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">String</span><span class="op">></span> <span class="op">=</span> <span class="ident">Vec::new</span>(); - <span class="kw">loop</span> { - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="kw">let </span><span class="kw-2">mut </span>args: Vec<String> = Vec::new(); + <span class="kw">loop </span>{ + <span class="self">self</span>.get_next_token(); - <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Identifier</span>(<span class="ident">arg</span>)) => <span class="ident">args</span>.<span class="ident">push</span>(<span class="ident">arg</span>), - <span class="prelude-val">Some</span>(<span class="ident">Token::Char</span>(<span class="string">','</span>)) => {} - <span class="ident">other</span> => { - <span class="self">self</span>.<span class="ident">cur_tok</span> <span class="op">=</span> <span class="ident">other</span>; + <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Identifier(arg)) => args.push(arg), + <span class="prelude-val">Some</span>(Token::Char(<span class="string">','</span>)) => {} + other => { + <span class="self">self</span>.cur_tok = other; <span class="kw">break</span>; } } } - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected ')' in prototype"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">')'</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected ')' in prototype"</span>.into()); } - <span class="comment">// Consume ')'.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Consume ')'. + </span><span class="self">self</span>.get_next_token(); - <span class="prelude-val">Ok</span>(<span class="ident">PrototypeAST</span>(<span class="ident">id_name</span>, <span class="ident">args</span>)) + <span class="prelude-val">Ok</span>(PrototypeAST(id_name, args)) } - <span class="doccomment">/// definition ::= 'def' prototype expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<FunctionAST> ParseDefinition();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse_definition</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">FunctionAST</span><span class="op">></span> { - <span class="comment">// Consume 'def' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Def</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="doccomment">/// definition ::= 'def' prototype expression + /// + /// Implement `std::unique_ptr<FunctionAST> ParseDefinition();` from the tutorial. + </span><span class="kw">pub fn </span>parse_definition(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<FunctionAST> { + <span class="comment">// Consume 'def' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Def); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_prototype</span>()<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">expr</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>proto = <span class="self">self</span>.parse_prototype()<span class="question-mark">?</span>; + <span class="kw">let </span>expr = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">expr</span>)) + <span class="prelude-val">Ok</span>(FunctionAST(proto, expr)) } - <span class="doccomment">/// external ::= 'extern' prototype</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<PrototypeAST> ParseExtern();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse_extern</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">PrototypeAST</span><span class="op">></span> { - <span class="comment">// Consume 'extern' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Extern</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="doccomment">/// external ::= 'extern' prototype + /// + /// Implement `std::unique_ptr<PrototypeAST> ParseExtern();` from the tutorial. + </span><span class="kw">pub fn </span>parse_extern(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<PrototypeAST> { + <span class="comment">// Consume 'extern' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Extern); + <span class="self">self</span>.get_next_token(); - <span class="self">self</span>.<span class="ident">parse_prototype</span>() + <span class="self">self</span>.parse_prototype() } - <span class="doccomment">/// toplevelexpr ::= expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<FunctionAST> ParseTopLevelExpr();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse_top_level_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">FunctionAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"__anon_expr"</span>.<span class="ident">into</span>(), <span class="ident">Vec::new</span>()); - <span class="prelude-val">Ok</span>(<span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">e</span>)) + <span class="doccomment">/// toplevelexpr ::= expression + /// + /// Implement `std::unique_ptr<FunctionAST> ParseTopLevelExpr();` from the tutorial. + </span><span class="kw">pub fn </span>parse_top_level_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<FunctionAST> { + <span class="kw">let </span>e = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"__anon_expr"</span>.into(), Vec::new()); + <span class="prelude-val">Ok</span>(FunctionAST(proto, e)) } } -<span class="doccomment">/// Get the binary operator precedence.</span> -<span class="doccomment">///</span> -<span class="doccomment">/// Implement `int GetTokPrecedence();` from the tutorial.</span> -<span class="kw">fn</span> <span class="ident">get_tok_precedence</span>(<span class="ident">tok</span>: <span class="kw-2">&</span><span class="ident">Token</span>) -> <span class="ident">isize</span> { - <span class="kw">match</span> <span class="ident">tok</span> { - <span class="ident">Token::Char</span>(<span class="string">'<'</span>) => <span class="number">10</span>, - <span class="ident">Token::Char</span>(<span class="string">'+'</span>) => <span class="number">20</span>, - <span class="ident">Token::Char</span>(<span class="string">'-'</span>) => <span class="number">20</span>, - <span class="ident">Token::Char</span>(<span class="string">'*'</span>) => <span class="number">40</span>, - <span class="kw">_</span> => <span class="op">-</span><span class="number">1</span>, +<span class="doccomment">/// Get the binary operator precedence. +/// +/// Implement `int GetTokPrecedence();` from the tutorial. +</span><span class="kw">fn </span>get_tok_precedence(tok: <span class="kw-2">&</span>Token) -> isize { + <span class="kw">match </span>tok { + Token::Char(<span class="string">'<'</span>) => <span class="number">10</span>, + Token::Char(<span class="string">'+'</span>) => <span class="number">20</span>, + Token::Char(<span class="string">'-'</span>) => <span class="number">20</span>, + Token::Char(<span class="string">'*'</span>) => <span class="number">40</span>, + <span class="kw">_ </span>=> -<span class="number">1</span>, } } -<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span> -<span class="kw">mod</span> <span class="ident">test</span> { - <span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">ExprAST</span>, <span class="ident">FunctionAST</span>, <span class="ident">Parser</span>, <span class="ident">PrototypeAST</span>}; - <span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::lexer::Lexer</span>; +<span class="attribute">#[cfg(test)] +</span><span class="kw">mod </span>test { + <span class="kw">use super</span>::{ExprAST, FunctionAST, Parser, PrototypeAST}; + <span class="kw">use </span><span class="kw">crate</span>::lexer::Lexer; - <span class="kw">fn</span> <span class="ident">parser</span>(<span class="ident">input</span>: <span class="kw-2">&</span><span class="ident">str</span>) -> <span class="ident">Parser</span><span class="op"><</span><span class="ident">std::str::Chars</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">l</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="ident">input</span>.<span class="ident">chars</span>()); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">Parser::new</span>(<span class="ident">l</span>); + <span class="kw">fn </span>parser(input: <span class="kw-2">&</span>str) -> Parser<std::str::Chars> { + <span class="kw">let </span>l = Lexer::new(input.chars()); + <span class="kw">let </span><span class="kw-2">mut </span>p = Parser::new(l); - <span class="comment">// Drop initial coin, initialize cur_tok.</span> - <span class="ident">p</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Drop initial coin, initialize cur_tok. + </span>p.get_next_token(); - <span class="ident">p</span> + p } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_number</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"13.37"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_number() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"13.37"</span>); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_num_expr</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Number</span>(<span class="number">13.37f64</span>))); + <span class="macro">assert_eq!</span>(p.parse_num_expr(), <span class="prelude-val">Ok</span>(ExprAST::Number(<span class="number">13.37f64</span>))); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_variable</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"foop"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_variable() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"foop"</span>); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_identifier_expr</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"foop"</span>.<span class="ident">into</span>())) + p.parse_identifier_expr(), + <span class="prelude-val">Ok</span>(ExprAST::Variable(<span class="string">"foop"</span>.into())) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_if</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"if 1 then 2 else 3"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_if() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"if 1 then 2 else 3"</span>); - <span class="kw">let</span> <span class="ident">cond</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)); - <span class="kw">let</span> <span class="ident">then</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)); - <span class="kw">let</span> <span class="ident">else_</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>)); + <span class="kw">let </span>cond = Box::new(ExprAST::Number(<span class="number">1f64</span>)); + <span class="kw">let </span>then = Box::new(ExprAST::Number(<span class="number">2f64</span>)); + <span class="kw">let </span>else_ = Box::new(ExprAST::Number(<span class="number">3f64</span>)); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_if_expr</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { <span class="ident">cond</span>, <span class="ident">then</span>, <span class="ident">else_</span> })); + <span class="macro">assert_eq!</span>(p.parse_if_expr(), <span class="prelude-val">Ok</span>(ExprAST::If { cond, then, else_ })); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"if foo() then bar(2) else baz(3)"</span>); + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"if foo() then bar(2) else baz(3)"</span>); - <span class="kw">let</span> <span class="ident">cond</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"foo"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[])); - <span class="kw">let</span> <span class="ident">then</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"bar"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)])); - <span class="kw">let</span> <span class="ident">else_</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"baz"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>)])); + <span class="kw">let </span>cond = Box::new(ExprAST::Call(<span class="string">"foo"</span>.into(), <span class="macro">vec!</span>[])); + <span class="kw">let </span>then = Box::new(ExprAST::Call(<span class="string">"bar"</span>.into(), <span class="macro">vec!</span>[ExprAST::Number(<span class="number">2f64</span>)])); + <span class="kw">let </span>else_ = Box::new(ExprAST::Call(<span class="string">"baz"</span>.into(), <span class="macro">vec!</span>[ExprAST::Number(<span class="number">3f64</span>)])); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_if_expr</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { <span class="ident">cond</span>, <span class="ident">then</span>, <span class="ident">else_</span> })); + <span class="macro">assert_eq!</span>(p.parse_if_expr(), <span class="prelude-val">Ok</span>(ExprAST::If { cond, then, else_ })); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_for</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"for i = 1, 2, 3 in 4"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_for() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"for i = 1, 2, 3 in 4"</span>); - <span class="kw">let</span> <span class="ident">var</span> <span class="op">=</span> <span class="ident">String::from</span>(<span class="string">"i"</span>); - <span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)); - <span class="kw">let</span> <span class="ident">end</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)); - <span class="kw">let</span> <span class="ident">step</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>))); - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">4f64</span>)); + <span class="kw">let </span>var = String::from(<span class="string">"i"</span>); + <span class="kw">let </span>start = Box::new(ExprAST::Number(<span class="number">1f64</span>)); + <span class="kw">let </span>end = Box::new(ExprAST::Number(<span class="number">2f64</span>)); + <span class="kw">let </span>step = <span class="prelude-val">Some</span>(Box::new(ExprAST::Number(<span class="number">3f64</span>))); + <span class="kw">let </span>body = Box::new(ExprAST::Number(<span class="number">4f64</span>)); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_for_expr</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>, - <span class="ident">end</span>, - <span class="ident">step</span>, - <span class="ident">body</span> + p.parse_for_expr(), + <span class="prelude-val">Ok</span>(ExprAST::For { + var, + start, + end, + step, + body }) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_for_no_step</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"for i = 1, 2 in 4"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_for_no_step() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"for i = 1, 2 in 4"</span>); - <span class="kw">let</span> <span class="ident">var</span> <span class="op">=</span> <span class="ident">String::from</span>(<span class="string">"i"</span>); - <span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)); - <span class="kw">let</span> <span class="ident">end</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)); - <span class="kw">let</span> <span class="ident">step</span> <span class="op">=</span> <span class="prelude-val">None</span>; - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">4f64</span>)); + <span class="kw">let </span>var = String::from(<span class="string">"i"</span>); + <span class="kw">let </span>start = Box::new(ExprAST::Number(<span class="number">1f64</span>)); + <span class="kw">let </span>end = Box::new(ExprAST::Number(<span class="number">2f64</span>)); + <span class="kw">let </span>step = <span class="prelude-val">None</span>; + <span class="kw">let </span>body = Box::new(ExprAST::Number(<span class="number">4f64</span>)); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_for_expr</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>, - <span class="ident">end</span>, - <span class="ident">step</span>, - <span class="ident">body</span> + p.parse_for_expr(), + <span class="prelude-val">Ok</span>(ExprAST::For { + var, + start, + end, + step, + body }) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_primary</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"1337 foop \n bla(123) \n if a then b else c \n for x=1,2 in 3"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_primary() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"1337 foop \n bla(123) \n if a then b else c \n for x=1,2 in 3"</span>); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_primary</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1337f64</span>))); + <span class="macro">assert_eq!</span>(p.parse_primary(), <span class="prelude-val">Ok</span>(ExprAST::Number(<span class="number">1337f64</span>))); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_primary</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"foop"</span>.<span class="ident">into</span>()))); + <span class="macro">assert_eq!</span>(p.parse_primary(), <span class="prelude-val">Ok</span>(ExprAST::Variable(<span class="string">"foop"</span>.into()))); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_primary</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"bla"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="ident">ExprAST::Number</span>(<span class="number">123f64</span>)])) + p.parse_primary(), + <span class="prelude-val">Ok</span>(ExprAST::Call(<span class="string">"bla"</span>.into(), <span class="macro">vec!</span>[ExprAST::Number(<span class="number">123f64</span>)])) ); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_primary</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { - <span class="ident">cond</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"a"</span>.<span class="ident">into</span>())), - <span class="ident">then</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"b"</span>.<span class="ident">into</span>())), - <span class="ident">else_</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"c"</span>.<span class="ident">into</span>())), + p.parse_primary(), + <span class="prelude-val">Ok</span>(ExprAST::If { + cond: Box::new(ExprAST::Variable(<span class="string">"a"</span>.into())), + then: Box::new(ExprAST::Variable(<span class="string">"b"</span>.into())), + else_: Box::new(ExprAST::Variable(<span class="string">"c"</span>.into())), }) ); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_primary</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>: <span class="ident">String::from</span>(<span class="string">"x"</span>), - <span class="ident">start</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)), - <span class="ident">end</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)), - <span class="ident">step</span>: <span class="prelude-val">None</span>, - <span class="ident">body</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>)), + p.parse_primary(), + <span class="prelude-val">Ok</span>(ExprAST::For { + var: String::from(<span class="string">"x"</span>), + start: Box::new(ExprAST::Number(<span class="number">1f64</span>)), + end: Box::new(ExprAST::Number(<span class="number">2f64</span>)), + step: <span class="prelude-val">None</span>, + body: Box::new(ExprAST::Number(<span class="number">3f64</span>)), }) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_binary_op</span>() { - <span class="comment">// Operator before RHS has higher precedence, expected AST</span> - <span class="comment">//</span> - <span class="comment">// -</span> - <span class="comment">// / \</span> - <span class="comment">// + c</span> - <span class="comment">// / \</span> - <span class="comment">// a b</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"a + b - c"</span>); - - <span class="kw">let</span> <span class="ident">binexpr_ab</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_binary_op() { + <span class="comment">// Operator before RHS has higher precedence, expected AST + // + // - + // / \ + // + c + // / \ + // a b + </span><span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"a + b - c"</span>); + + <span class="kw">let </span>binexpr_ab = ExprAST::Binary( <span class="string">'+'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"a"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"b"</span>.<span class="ident">into</span>())), + Box::new(ExprAST::Variable(<span class="string">"a"</span>.into())), + Box::new(ExprAST::Variable(<span class="string">"b"</span>.into())), ); - <span class="kw">let</span> <span class="ident">binexpr_abc</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="kw">let </span>binexpr_abc = ExprAST::Binary( <span class="string">'-'</span>, - <span class="ident">Box::new</span>(<span class="ident">binexpr_ab</span>), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"c"</span>.<span class="ident">into</span>())), + Box::new(binexpr_ab), + Box::new(ExprAST::Variable(<span class="string">"c"</span>.into())), ); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_expression</span>(), <span class="prelude-val">Ok</span>(<span class="ident">binexpr_abc</span>)); + <span class="macro">assert_eq!</span>(p.parse_expression(), <span class="prelude-val">Ok</span>(binexpr_abc)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_binary_op2</span>() { - <span class="comment">// Operator after RHS has higher precedence, expected AST</span> - <span class="comment">//</span> - <span class="comment">// +</span> - <span class="comment">// / \</span> - <span class="comment">// a *</span> - <span class="comment">// / \</span> - <span class="comment">// b c</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"a + b * c"</span>); - - <span class="kw">let</span> <span class="ident">binexpr_bc</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_binary_op2() { + <span class="comment">// Operator after RHS has higher precedence, expected AST + // + // + + // / \ + // a * + // / \ + // b c + </span><span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"a + b * c"</span>); + + <span class="kw">let </span>binexpr_bc = ExprAST::Binary( <span class="string">'*'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"b"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"c"</span>.<span class="ident">into</span>())), + Box::new(ExprAST::Variable(<span class="string">"b"</span>.into())), + Box::new(ExprAST::Variable(<span class="string">"c"</span>.into())), ); - <span class="kw">let</span> <span class="ident">binexpr_abc</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="kw">let </span>binexpr_abc = ExprAST::Binary( <span class="string">'+'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"a"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">binexpr_bc</span>), + Box::new(ExprAST::Variable(<span class="string">"a"</span>.into())), + Box::new(binexpr_bc), ); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_expression</span>(), <span class="prelude-val">Ok</span>(<span class="ident">binexpr_abc</span>)); + <span class="macro">assert_eq!</span>(p.parse_expression(), <span class="prelude-val">Ok</span>(binexpr_abc)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_prototype</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"foo(a,b)"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_prototype() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"foo(a,b)"</span>); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"foo"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="string">"a"</span>.<span class="ident">into</span>(), <span class="string">"b"</span>.<span class="ident">into</span>()]); + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"foo"</span>.into(), <span class="macro">vec!</span>[<span class="string">"a"</span>.into(), <span class="string">"b"</span>.into()]); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_prototype</span>(), <span class="prelude-val">Ok</span>(<span class="ident">proto</span>)); + <span class="macro">assert_eq!</span>(p.parse_prototype(), <span class="prelude-val">Ok</span>(proto)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_definition</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"def bar( arg0 , arg1 ) arg0 + arg1"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_definition() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"def bar( arg0 , arg1 ) arg0 + arg1"</span>); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"bar"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="string">"arg0"</span>.<span class="ident">into</span>(), <span class="string">"arg1"</span>.<span class="ident">into</span>()]); + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"bar"</span>.into(), <span class="macro">vec!</span>[<span class="string">"arg0"</span>.into(), <span class="string">"arg1"</span>.into()]); - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="kw">let </span>body = ExprAST::Binary( <span class="string">'+'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"arg0"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"arg1"</span>.<span class="ident">into</span>())), + Box::new(ExprAST::Variable(<span class="string">"arg0"</span>.into())), + Box::new(ExprAST::Variable(<span class="string">"arg1"</span>.into())), ); - <span class="kw">let</span> <span class="ident">func</span> <span class="op">=</span> <span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">body</span>); + <span class="kw">let </span>func = FunctionAST(proto, body); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_definition</span>(), <span class="prelude-val">Ok</span>(<span class="ident">func</span>)); + <span class="macro">assert_eq!</span>(p.parse_definition(), <span class="prelude-val">Ok</span>(func)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_extern</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"extern baz()"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_extern() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"extern baz()"</span>); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"baz"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[]); + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"baz"</span>.into(), <span class="macro">vec!</span>[]); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_extern</span>(), <span class="prelude-val">Ok</span>(<span class="ident">proto</span>)); + <span class="macro">assert_eq!</span>(p.parse_extern(), <span class="prelude-val">Ok</span>(proto)); } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file |