From 617f0d65b20b7b405b1acecea6f99ccd0a6d73f3 Mon Sep 17 00:00:00 2001 From: johannst Date: Thu, 5 Jan 2023 20:25:01 +0000 Subject: deploy: 802cca1963bc27f8ea4e4923029909e45464d3df --- src/llvm_kaleidoscope_rs/codegen.rs.html | 521 ++++++------ src/llvm_kaleidoscope_rs/lexer.rs.html | 307 ++++--- src/llvm_kaleidoscope_rs/lib.rs.html | 175 ++-- src/llvm_kaleidoscope_rs/llvm/basic_block.rs.html | 69 +- src/llvm_kaleidoscope_rs/llvm/builder.rs.html | 423 +++++----- src/llvm_kaleidoscope_rs/llvm/lljit.rs.html | 217 +++-- src/llvm_kaleidoscope_rs/llvm/mod.rs.html | 115 ++- src/llvm_kaleidoscope_rs/llvm/module.rs.html | 325 ++++---- src/llvm_kaleidoscope_rs/llvm/pass_manager.rs.html | 115 ++- src/llvm_kaleidoscope_rs/llvm/type_.rs.html | 95 +-- src/llvm_kaleidoscope_rs/llvm/value.rs.html | 361 ++++---- src/llvm_kaleidoscope_rs/parser.rs.html | 915 ++++++++++----------- 12 files changed, 1777 insertions(+), 1861 deletions(-) (limited to 'src') 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 @@ -codegen.rs - source -
1
+codegen.rs - source
1
 2
 3
 4
@@ -326,328 +320,327 @@
 320
 321
 322
-
use std::collections::HashMap;
+
use std::collections::HashMap;
 
-use crate::llvm::{FnValue, FunctionPassManager, IRBuilder, Module, Value};
-use crate::parser::{ExprAST, FunctionAST, PrototypeAST};
-use crate::Either;
+use crate::llvm::{FnValue, FunctionPassManager, IRBuilder, Module, Value};
+use crate::parser::{ExprAST, FunctionAST, PrototypeAST};
+use crate::Either;
 
-type CodegenResult<T> = Result<T, String>;
+type CodegenResult<T> = Result<T, String>;
 
-/// Code generator from kaleidoscope AST to LLVM IR.
-pub struct Codegen<'llvm, 'a> {
-    module: &'llvm Module,
-    builder: &'a IRBuilder<'llvm>,
-    fpm: &'a FunctionPassManager<'llvm>,
-    fn_protos: &'a mut HashMap<String, PrototypeAST>,
+/// Code generator from kaleidoscope AST to LLVM IR.
+pub struct Codegen<'llvm, 'a> {
+    module: &'llvm Module,
+    builder: &'a IRBuilder<'llvm>,
+    fpm: &'a FunctionPassManager<'llvm>,
+    fn_protos: &'a mut HashMap<String, PrototypeAST>,
 }
 
-impl<'llvm, 'a> Codegen<'llvm, 'a> {
-    /// Compile either a [`PrototypeAST`] or a [`FunctionAST`] into the LLVM `module`.
-    pub fn compile(
-        module: &'llvm Module,
-        fn_protos: &mut HashMap<String, PrototypeAST>,
-        compilee: Either<&PrototypeAST, &FunctionAST>,
-    ) -> CodegenResult<FnValue<'llvm>> {
-        let mut cg = Codegen {
-            module,
-            builder: &IRBuilder::with_ctx(module),
-            fpm: &FunctionPassManager::with_ctx(module),
-            fn_protos,
+impl<'llvm, 'a> Codegen<'llvm, 'a> {
+    /// Compile either a [`PrototypeAST`] or a [`FunctionAST`] into the LLVM `module`.
+    pub fn compile(
+        module: &'llvm Module,
+        fn_protos: &mut HashMap<String, PrototypeAST>,
+        compilee: Either<&PrototypeAST, &FunctionAST>,
+    ) -> CodegenResult<FnValue<'llvm>> {
+        let mut cg = Codegen {
+            module,
+            builder: &IRBuilder::with_ctx(module),
+            fpm: &FunctionPassManager::with_ctx(module),
+            fn_protos,
         };
-        let mut variables = HashMap::new();
+        let mut variables = HashMap::new();
 
-        match compilee {
-            Either::A(proto) => Ok(cg.codegen_prototype(proto)),
-            Either::B(func) => cg.codegen_function(func, &mut variables),
+        match compilee {
+            Either::A(proto) => Ok(cg.codegen_prototype(proto)),
+            Either::B(func) => cg.codegen_function(func, &mut variables),
         }
     }
 
-    fn codegen_expr(
+    fn codegen_expr(
         &self,
-        expr: &ExprAST,
-        named_values: &mut HashMap<String, Value<'llvm>>,
-    ) -> CodegenResult<Value<'llvm>> {
-        match expr {
-            ExprAST::Number(num) => Ok(self.module.type_f64().const_f64(*num)),
-            ExprAST::Variable(name) => match named_values.get(name.as_str()) {
-                Some(value) => Ok(*value),
-                None => Err("Unknown variable name".into()),
+        expr: &ExprAST,
+        named_values: &mut HashMap<String, Value<'llvm>>,
+    ) -> CodegenResult<Value<'llvm>> {
+        match expr {
+            ExprAST::Number(num) => Ok(self.module.type_f64().const_f64(*num)),
+            ExprAST::Variable(name) => match named_values.get(name.as_str()) {
+                Some(value) => Ok(*value),
+                None => Err("Unknown variable name".into()),
             },
-            ExprAST::Binary(binop, lhs, rhs) => {
-                let l = self.codegen_expr(lhs, named_values)?;
-                let r = self.codegen_expr(rhs, named_values)?;
-
-                match binop {
-                    '+' => Ok(self.builder.fadd(l, r)),
-                    '-' => Ok(self.builder.fsub(l, r)),
-                    '*' => Ok(self.builder.fmul(l, r)),
-                    '<' => {
-                        let res = self.builder.fcmpult(l, r);
-                        // Turn bool into f64.
-                        Ok(self.builder.uitofp(res, self.module.type_f64()))
+            ExprAST::Binary(binop, lhs, rhs) => {
+                let l = self.codegen_expr(lhs, named_values)?;
+                let r = self.codegen_expr(rhs, named_values)?;
+
+                match binop {
+                    '+' => Ok(self.builder.fadd(l, r)),
+                    '-' => Ok(self.builder.fsub(l, r)),
+                    '*' => Ok(self.builder.fmul(l, r)),
+                    '<' => {
+                        let res = self.builder.fcmpult(l, r);
+                        // Turn bool into f64.
+                        Ok(self.builder.uitofp(res, self.module.type_f64()))
                     }
-                    _ => Err("invalid binary operator".into()),
+                    _ => Err("invalid binary operator".into()),
                 }
             }
-            ExprAST::Call(callee, args) => match self.get_function(callee) {
-                Some(callee) => {
-                    if callee.args() != args.len() {
-                        return Err("Incorrect # arguments passed".into());
+            ExprAST::Call(callee, args) => match self.get_function(callee) {
+                Some(callee) => {
+                    if callee.args() != args.len() {
+                        return Err("Incorrect # arguments passed".into());
                     }
 
-                    // Generate code for function argument expressions.
-                    let mut args: Vec<Value<'_>> = args
-                        .iter()
-                        .map(|arg| self.codegen_expr(arg, named_values))
-                        .collect::<CodegenResult<_>>()?;
+                    // Generate code for function argument expressions.
+                    let mut args: Vec<Value<'_>> = args
+                        .iter()
+                        .map(|arg| self.codegen_expr(arg, named_values))
+                        .collect::<CodegenResult<_>>()?;
 
-                    Ok(self.builder.call(callee, &mut args))
+                    Ok(self.builder.call(callee, &mut args))
                 }
-                None => Err("Unknown function referenced".into()),
+                None => Err("Unknown function referenced".into()),
             },
-            ExprAST::If { cond, then, else_ } => {
-                // For 'if' expressions we are building the following CFG.
-                //
-                //         ; cond
-                //         br
-                //          |
-                //    +-----+------+
-                //    v            v
-                //  ; then       ; else
-                //    |            |
-                //    +-----+------+
-                //          v
-                //        ; merge
-                //        phi then, else
-                //        ret phi
-
-                let cond_v = {
-                    // Codgen 'cond' expression.
-                    let v = self.codegen_expr(cond, named_values)?;
-                    // Compare 'v' against '0' as 'one = ordered not equal'.
-                    self.builder
-                        .fcmpone(v, self.module.type_f64().const_f64(0f64))
+            ExprAST::If { cond, then, else_ } => {
+                // For 'if' expressions we are building the following CFG.
+                //
+                //         ; cond
+                //         br
+                //          |
+                //    +-----+------+
+                //    v            v
+                //  ; then       ; else
+                //    |            |
+                //    +-----+------+
+                //          v
+                //        ; merge
+                //        phi then, else
+                //        ret phi
+
+                let cond_v = {
+                    // Codgen 'cond' expression.
+                    let v = self.codegen_expr(cond, named_values)?;
+                    // Compare 'v' against '0' as 'one = ordered not equal'.
+                    self.builder
+                        .fcmpone(v, self.module.type_f64().const_f64(0f64))
                 };
 
-                // Get the function we are currently inserting into.
-                let the_function = self.builder.get_insert_block().get_parent();
-
-                // 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.
-                let then_bb = self.module.append_basic_block(the_function);
-                let else_bb = self.module.create_basic_block();
-                let merge_bb = self.module.create_basic_block();
-
-                // Create a conditional branch based on the result of the 'cond' expression.
-                self.builder.cond_br(cond_v, then_bb, else_bb);
-
-                // Move to 'then' basic block and codgen the 'then' expression.
-                self.builder.pos_at_end(then_bb);
-                let then_v = self.codegen_expr(then, named_values)?;
-                // Create unconditional branch to 'merge' block.
-                self.builder.br(merge_bb);
-                // Update reference to current basic block (in case the 'then' expression added new
-                // basic blocks).
-                let then_bb = self.builder.get_insert_block();
-
-                // Now append the 'else' basic block to the function.
-                the_function.append_basic_block(else_bb);
-                // Move to 'else' basic block and codgen the 'else' expression.
-                self.builder.pos_at_end(else_bb);
-                let else_v = self.codegen_expr(else_, named_values)?;
-                // Create unconditional branch to 'merge' block.
-                self.builder.br(merge_bb);
-                // Update reference to current basic block (in case the 'else' expression added new
-                // basic blocks).
-                let else_bb = self.builder.get_insert_block();
-
-                // Now append the 'merge' basic block to the function.
-                the_function.append_basic_block(merge_bb);
-                // Move to 'merge' basic block.
-                self.builder.pos_at_end(merge_bb);
-                // Codegen the phi node returning the appropriate value depending on the branch
-                // condition.
-                let phi = self.builder.phi(
-                    self.module.type_f64(),
-                    &[(then_v, then_bb), (else_v, else_bb)],
+                // Get the function we are currently inserting into.
+                let the_function = self.builder.get_insert_block().get_parent();
+
+                // 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.
+                let then_bb = self.module.append_basic_block(the_function);
+                let else_bb = self.module.create_basic_block();
+                let merge_bb = self.module.create_basic_block();
+
+                // Create a conditional branch based on the result of the 'cond' expression.
+                self.builder.cond_br(cond_v, then_bb, else_bb);
+
+                // Move to 'then' basic block and codgen the 'then' expression.
+                self.builder.pos_at_end(then_bb);
+                let then_v = self.codegen_expr(then, named_values)?;
+                // Create unconditional branch to 'merge' block.
+                self.builder.br(merge_bb);
+                // Update reference to current basic block (in case the 'then' expression added new
+                // basic blocks).
+                let then_bb = self.builder.get_insert_block();
+
+                // Now append the 'else' basic block to the function.
+                the_function.append_basic_block(else_bb);
+                // Move to 'else' basic block and codgen the 'else' expression.
+                self.builder.pos_at_end(else_bb);
+                let else_v = self.codegen_expr(else_, named_values)?;
+                // Create unconditional branch to 'merge' block.
+                self.builder.br(merge_bb);
+                // Update reference to current basic block (in case the 'else' expression added new
+                // basic blocks).
+                let else_bb = self.builder.get_insert_block();
+
+                // Now append the 'merge' basic block to the function.
+                the_function.append_basic_block(merge_bb);
+                // Move to 'merge' basic block.
+                self.builder.pos_at_end(merge_bb);
+                // Codegen the phi node returning the appropriate value depending on the branch
+                // condition.
+                let phi = self.builder.phi(
+                    self.module.type_f64(),
+                    &[(then_v, then_bb), (else_v, else_bb)],
                 );
 
-                Ok(*phi)
+                Ok(*phi)
             }
-            ExprAST::For {
-                var,
-                start,
-                end,
-                step,
-                body,
+            ExprAST::For {
+                var,
+                start,
+                end,
+                step,
+                body,
             } => {
-                // 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.
-                let start_val = self.codegen_expr(start, named_values)?;
-
-                let the_function = self.builder.get_insert_block().get_parent();
-                // Get current basic block (used in the loop variable phi node).
-                let entry_bb = self.builder.get_insert_block();
-                // Add new basic block to emit loop body.
-                let loop_bb = self.module.append_basic_block(the_function);
-
-                self.builder.br(loop_bb);
-                self.builder.pos_at_end(loop_bb);
-
-                // 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.
-                let variable = self
-                    .builder
-                    .phi(self.module.type_f64(), &[(start_val, entry_bb)]);
-
-                // 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.
-                let old_val = named_values.insert(var.into(), *variable);
-
-                // Generate the loop body.
-                self.codegen_expr(body, named_values)?;
-
-                // Generate step value expression if available else use '1'.
-                let step_val = if let Some(step) = step {
-                    self.codegen_expr(step, named_values)?
-                } else {
-                    self.module.type_f64().const_f64(1f64)
+                // 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.
+                let start_val = self.codegen_expr(start, named_values)?;
+
+                let the_function = self.builder.get_insert_block().get_parent();
+                // Get current basic block (used in the loop variable phi node).
+                let entry_bb = self.builder.get_insert_block();
+                // Add new basic block to emit loop body.
+                let loop_bb = self.module.append_basic_block(the_function);
+
+                self.builder.br(loop_bb);
+                self.builder.pos_at_end(loop_bb);
+
+                // 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.
+                let variable = self
+                    .builder
+                    .phi(self.module.type_f64(), &[(start_val, entry_bb)]);
+
+                // 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.
+                let old_val = named_values.insert(var.into(), *variable);
+
+                // Generate the loop body.
+                self.codegen_expr(body, named_values)?;
+
+                // Generate step value expression if available else use '1'.
+                let step_val = if let Some(step) = step {
+                    self.codegen_expr(step, named_values)?
+                } else {
+                    self.module.type_f64().const_f64(1f64)
                 };
 
-                // Increment loop variable.
-                let next_var = self.builder.fadd(*variable, step_val);
+                // Increment loop variable.
+                let next_var = self.builder.fadd(*variable, step_val);
 
-                // Generate the loop end condition.
-                let end_cond = self.codegen_expr(end, named_values)?;
-                let end_cond = self
-                    .builder
-                    .fcmpone(end_cond, self.module.type_f64().const_f64(0f64));
+                // Generate the loop end condition.
+                let end_cond = self.codegen_expr(end, named_values)?;
+                let end_cond = self
+                    .builder
+                    .fcmpone(end_cond, self.module.type_f64().const_f64(0f64));
 
-                // Get current basic block.
-                let loop_end_bb = self.builder.get_insert_block();
-                // Add new basic block following the loop.
-                let after_bb = self.module.append_basic_block(the_function);
+                // Get current basic block.
+                let loop_end_bb = self.builder.get_insert_block();
+                // Add new basic block following the loop.
+                let after_bb = self.module.append_basic_block(the_function);
 
-                // Register additional incoming value for the loop variable. This will choose the
-                // updated loop variable if we are iterating in the loop.
-                variable.add_incoming(next_var, loop_end_bb);
+                // Register additional incoming value for the loop variable. This will choose the
+                // updated loop variable if we are iterating in the loop.
+                variable.add_incoming(next_var, loop_end_bb);
 
-                // Branch depending on the loop end condition.
-                self.builder.cond_br(end_cond, loop_bb, after_bb);
+                // Branch depending on the loop end condition.
+                self.builder.cond_br(end_cond, loop_bb, after_bb);
 
-                self.builder.pos_at_end(after_bb);
+                self.builder.pos_at_end(after_bb);
 
-                // Restore the shadowed variable if there was one.
-                if let Some(old_val) = old_val {
-                    // We inserted 'var' above so it must exist.
-                    *named_values.get_mut(var).unwrap() = old_val;
-                } else {
-                    named_values.remove(var);
+                // Restore the shadowed variable if there was one.
+                if let Some(old_val) = old_val {
+                    // We inserted 'var' above so it must exist.
+                    *named_values.get_mut(var).unwrap() = old_val;
+                } else {
+                    named_values.remove(var);
                 }
 
-                // Loops just always return 0.
-                Ok(self.module.type_f64().const_f64(0f64))
+                // Loops just always return 0.
+                Ok(self.module.type_f64().const_f64(0f64))
             }
         }
     }
 
-    fn codegen_prototype(&self, PrototypeAST(name, args): &PrototypeAST) -> FnValue<'llvm> {
-        let type_f64 = self.module.type_f64();
+    fn codegen_prototype(&self, PrototypeAST(name, args): &PrototypeAST) -> FnValue<'llvm> {
+        let type_f64 = self.module.type_f64();
 
-        let mut doubles = Vec::new();
-        doubles.resize(args.len(), type_f64);
+        let mut doubles = Vec::new();
+        doubles.resize(args.len(), type_f64);
 
-        // Build the function type: fn(f64, f64, ..) -> f64
-        let ft = self.module.type_fn(&mut doubles, type_f64);
+        // Build the function type: fn(f64, f64, ..) -> f64
+        let ft = self.module.type_fn(&mut doubles, type_f64);
 
-        // Create the function declaration.
-        let f = self.module.add_fn(name, ft);
+        // Create the function declaration.
+        let f = self.module.add_fn(name, ft);
 
-        // Set the names of the function arguments.
-        for idx in 0..f.args() {
-            f.arg(idx).set_name(&args[idx]);
+        // Set the names of the function arguments.
+        for idx in 0..f.args() {
+            f.arg(idx).set_name(&args[idx]);
         }
 
-        f
+        f
     }
 
-    fn codegen_function(
-        &mut self,
-        FunctionAST(proto, body): &FunctionAST,
-        named_values: &mut HashMap<String, Value<'llvm>>,
-    ) -> CodegenResult<FnValue<'llvm>> {
-        // Insert the function prototype into the `fn_protos` map to keep track for re-generating
-        // declarations in other modules.
-        self.fn_protos.insert(proto.0.clone(), proto.clone());
+    fn codegen_function(
+        &mut self,
+        FunctionAST(proto, body): &FunctionAST,
+        named_values: &mut HashMap<String, Value<'llvm>>,
+    ) -> CodegenResult<FnValue<'llvm>> {
+        // Insert the function prototype into the `fn_protos` map to keep track for re-generating
+        // declarations in other modules.
+        self.fn_protos.insert(proto.0.clone(), proto.clone());
 
-        let the_function = self.get_function(&proto.0)
-            .expect("If proto not already generated, get_function will do for us since we updated fn_protos before-hand!");
+        let the_function = self.get_function(&proto.0)
+            .expect("If proto not already generated, get_function will do for us since we updated fn_protos before-hand!");
 
-        if the_function.basic_blocks() > 0 {
-            return Err("Function cannot be redefined.".into());
+        if the_function.basic_blocks() > 0 {
+            return Err("Function cannot be redefined.".into());
         }
 
-        // Create entry basic block to insert code.
-        let bb = self.module.append_basic_block(the_function);
-        self.builder.pos_at_end(bb);
+        // Create entry basic block to insert code.
+        let bb = self.module.append_basic_block(the_function);
+        self.builder.pos_at_end(bb);
 
-        // New scope, clear the map with the function args.
-        named_values.clear();
+        // New scope, clear the map with the function args.
+        named_values.clear();
 
-        // Update the map with the current functions args.
-        for idx in 0..the_function.args() {
-            let arg = the_function.arg(idx);
-            named_values.insert(arg.get_name().into(), arg);
+        // Update the map with the current functions args.
+        for idx in 0..the_function.args() {
+            let arg = the_function.arg(idx);
+            named_values.insert(arg.get_name().into(), arg);
         }
 
-        // Codegen function body.
-        if let Ok(ret) = self.codegen_expr(body, named_values) {
-            self.builder.ret(ret);
-            assert!(the_function.verify());
+        // Codegen function body.
+        if let Ok(ret) = self.codegen_expr(body, named_values) {
+            self.builder.ret(ret);
+            assert!(the_function.verify());
 
-            // Run the optimization passes on the function.
-            self.fpm.run(the_function);
+            // Run the optimization passes on the function.
+            self.fpm.run(the_function);
 
-            Ok(the_function)
-        } else {
+            Ok(the_function)
+        } else {
             todo!("Failed to codegen function body, erase from module!");
         }
     }
 
-    /// 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.
-    fn get_function(&self, name: &str) -> Option<FnValue<'llvm>> {
-        let callee = match self.module.get_fn(name) {
-            Some(callee) => callee,
-            None => {
-                let proto = self.fn_protos.get(name)?;
-                self.codegen_prototype(proto)
+    /// 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.
+    fn get_function(&self, name: &str) -> Option<FnValue<'llvm>> {
+        let callee = match self.module.get_fn(name) {
+            Some(callee) => callee,
+            None => {
+                let proto = self.fn_protos.get(name)?;
+                self.codegen_prototype(proto)
             }
         };
 
-        Some(callee)
+        Some(callee)
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -lexer.rs - source -
1
+lexer.rs - source
1
 2
 3
 4
@@ -210,212 +204,211 @@
 204
 205
 206
-
#[derive(Debug, PartialEq)]
-pub enum Token {
-    Eof,
-    Def,
-    Extern,
-    Identifier(String),
-    Number(f64),
-    Char(char),
-    If,
-    Then,
-    Else,
-    For,
-    In,
+
#[derive(Debug, PartialEq)]
+pub enum Token {
+    Eof,
+    Def,
+    Extern,
+    Identifier(String),
+    Number(f64),
+    Char(char),
+    If,
+    Then,
+    Else,
+    For,
+    In,
 }
 
-pub struct Lexer<I>
-where
-    I: Iterator<Item = char>,
+pub struct Lexer<I>
+where
+    I: Iterator<Item = char>,
 {
-    input: I,
-    last_char: Option<char>,
+    input: I,
+    last_char: Option<char>,
 }
 
-impl<I> Lexer<I>
-where
-    I: Iterator<Item = char>,
+impl<I> Lexer<I>
+where
+    I: Iterator<Item = char>,
 {
-    pub fn new(mut input: I) -> Lexer<I> {
-        let last_char = input.next();
-        Lexer { input, last_char }
+    pub fn new(mut input: I) -> Lexer<I> {
+        let last_char = input.next();
+        Lexer { input, last_char }
     }
 
-    fn step(&mut self) -> Option<char> {
-        self.last_char = self.input.next();
-        self.last_char
+    fn step(&mut self) -> Option<char> {
+        self.last_char = self.input.next();
+        self.last_char
     }
 
-    /// Lex and return the next token.
-    ///
-    /// Implement `int gettok();` from the tutorial.
-    pub fn gettok(&mut self) -> Token {
-        // Eat up whitespaces.
-        while matches!(self.last_char, Some(c) if c.is_ascii_whitespace()) {
-            self.step();
+    /// Lex and return the next token.
+    ///
+    /// Implement `int gettok();` from the tutorial.
+    pub fn gettok(&mut self) -> Token {
+        // Eat up whitespaces.
+        while matches!(self.last_char, Some(c) if c.is_ascii_whitespace()) {
+            self.step();
         }
 
-        // Unpack last char or return EOF.
-        let last_char = if let Some(c) = self.last_char {
-            c
-        } else {
-            return Token::Eof;
+        // Unpack last char or return EOF.
+        let last_char = if let Some(c) = self.last_char {
+            c
+        } else {
+            return Token::Eof;
         };
 
-        // Identifier: [a-zA-Z][a-zA-Z0-9]*
-        if last_char.is_ascii_alphabetic() {
-            let mut ident = String::new();
-            ident.push(last_char);
+        // Identifier: [a-zA-Z][a-zA-Z0-9]*
+        if last_char.is_ascii_alphabetic() {
+            let mut ident = String::new();
+            ident.push(last_char);
 
-            while let Some(c) = self.step() {
-                if c.is_ascii_alphanumeric() {
-                    ident.push(c)
-                } else {
+            while let Some(c) = self.step() {
+                if c.is_ascii_alphanumeric() {
+                    ident.push(c)
+                } else {
                     break;
                 }
             }
 
-            match ident.as_ref() {
-                "def" => return Token::Def,
-                "extern" => return Token::Extern,
-                "if" => return Token::If,
-                "then" => return Token::Then,
-                "else" => return Token::Else,
-                "for" => return Token::For,
-                "in" => return Token::In,
-                _ => {}
+            match ident.as_ref() {
+                "def" => return Token::Def,
+                "extern" => return Token::Extern,
+                "if" => return Token::If,
+                "then" => return Token::Then,
+                "else" => return Token::Else,
+                "for" => return Token::For,
+                "in" => return Token::In,
+                _ => {}
             }
 
-            return Token::Identifier(ident);
+            return Token::Identifier(ident);
         }
 
-        // Number: [0-9.]+
-        if last_char.is_ascii_digit() || last_char == '.' {
-            let mut num = String::new();
-            num.push(last_char);
+        // Number: [0-9.]+
+        if last_char.is_ascii_digit() || last_char == '.' {
+            let mut num = String::new();
+            num.push(last_char);
 
-            while let Some(c) = self.step() {
-                if c.is_ascii_digit() || c == '.' {
-                    num.push(c)
-                } else {
+            while let Some(c) = self.step() {
+                if c.is_ascii_digit() || c == '.' {
+                    num.push(c)
+                } else {
                     break;
                 }
             }
 
-            let num: f64 = num.parse().unwrap_or_default();
-            return Token::Number(num);
+            let num: f64 = num.parse().unwrap_or_default();
+            return Token::Number(num);
         }
 
-        // Eat up comment.
-        if last_char == '#' {
-            loop {
-                match self.step() {
-                    Some(c) if c == '\r' || c == '\n' => return self.gettok(),
-                    None => return Token::Eof,
-                    _ => { /* consume comment */ }
+        // Eat up comment.
+        if last_char == '#' {
+            loop {
+                match self.step() {
+                    Some(c) if c == '\r' || c == '\n' => return self.gettok(),
+                    None => return Token::Eof,
+                    _ => { /* consume comment */ }
                 }
             }
         }
 
-        // Advance last char and return currently last char.
-        self.step();
-        Token::Char(last_char)
+        // Advance last char and return currently last char.
+        self.step();
+        Token::Char(last_char)
     }
 }
 
-#[cfg(test)]
-mod test {
-    use super::{Lexer, Token};
+#[cfg(test)]
+mod test {
+    use super::{Lexer, Token};
 
-    #[test]
-    fn test_identifier() {
-        let mut lex = Lexer::new("a b c".chars());
-        assert_eq!(Token::Identifier("a".into()), lex.gettok());
-        assert_eq!(Token::Identifier("b".into()), lex.gettok());
-        assert_eq!(Token::Identifier("c".into()), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+    #[test]
+    fn test_identifier() {
+        let mut lex = Lexer::new("a b c".chars());
+        assert_eq!(Token::Identifier("a".into()), lex.gettok());
+        assert_eq!(Token::Identifier("b".into()), lex.gettok());
+        assert_eq!(Token::Identifier("c".into()), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
     }
 
-    #[test]
-    fn test_keyword() {
-        let mut lex = Lexer::new("def extern".chars());
-        assert_eq!(Token::Def, lex.gettok());
-        assert_eq!(Token::Extern, lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+    #[test]
+    fn test_keyword() {
+        let mut lex = Lexer::new("def extern".chars());
+        assert_eq!(Token::Def, lex.gettok());
+        assert_eq!(Token::Extern, lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
     }
 
-    #[test]
-    fn test_number() {
-        let mut lex = Lexer::new("12.34".chars());
-        assert_eq!(Token::Number(12.34f64), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+    #[test]
+    fn test_number() {
+        let mut lex = Lexer::new("12.34".chars());
+        assert_eq!(Token::Number(12.34f64), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
 
-        let mut lex = Lexer::new(" 1.0   2.0 3.0".chars());
-        assert_eq!(Token::Number(1.0f64), lex.gettok());
-        assert_eq!(Token::Number(2.0f64), lex.gettok());
-        assert_eq!(Token::Number(3.0f64), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+        let mut lex = Lexer::new(" 1.0   2.0 3.0".chars());
+        assert_eq!(Token::Number(1.0f64), lex.gettok());
+        assert_eq!(Token::Number(2.0f64), lex.gettok());
+        assert_eq!(Token::Number(3.0f64), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
 
-        let mut lex = Lexer::new("12.34.56".chars());
-        assert_eq!(Token::Number(0f64), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+        let mut lex = Lexer::new("12.34.56".chars());
+        assert_eq!(Token::Number(0f64), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
     }
 
-    #[test]
-    fn test_comment() {
-        let mut lex = Lexer::new("# some comment".chars());
-        assert_eq!(Token::Eof, lex.gettok());
+    #[test]
+    fn test_comment() {
+        let mut lex = Lexer::new("# some comment".chars());
+        assert_eq!(Token::Eof, lex.gettok());
 
-        let mut lex = Lexer::new("abc # some comment \n xyz".chars());
-        assert_eq!(Token::Identifier("abc".into()), lex.gettok());
-        assert_eq!(Token::Identifier("xyz".into()), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+        let mut lex = Lexer::new("abc # some comment \n xyz".chars());
+        assert_eq!(Token::Identifier("abc".into()), lex.gettok());
+        assert_eq!(Token::Identifier("xyz".into()), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
     }
 
-    #[test]
-    fn test_chars() {
-        let mut lex = Lexer::new("a+b-c".chars());
-        assert_eq!(Token::Identifier("a".into()), lex.gettok());
-        assert_eq!(Token::Char('+'), lex.gettok());
-        assert_eq!(Token::Identifier("b".into()), lex.gettok());
-        assert_eq!(Token::Char('-'), lex.gettok());
-        assert_eq!(Token::Identifier("c".into()), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+    #[test]
+    fn test_chars() {
+        let mut lex = Lexer::new("a+b-c".chars());
+        assert_eq!(Token::Identifier("a".into()), lex.gettok());
+        assert_eq!(Token::Char('+'), lex.gettok());
+        assert_eq!(Token::Identifier("b".into()), lex.gettok());
+        assert_eq!(Token::Char('-'), lex.gettok());
+        assert_eq!(Token::Identifier("c".into()), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
     }
 
-    #[test]
-    fn test_whitespaces() {
-        let mut lex = Lexer::new("    +a  b      c!    ".chars());
-        assert_eq!(Token::Char('+'), lex.gettok());
-        assert_eq!(Token::Identifier("a".into()), lex.gettok());
-        assert_eq!(Token::Identifier("b".into()), lex.gettok());
-        assert_eq!(Token::Identifier("c".into()), lex.gettok());
-        assert_eq!(Token::Char('!'), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+    #[test]
+    fn test_whitespaces() {
+        let mut lex = Lexer::new("    +a  b      c!    ".chars());
+        assert_eq!(Token::Char('+'), lex.gettok());
+        assert_eq!(Token::Identifier("a".into()), lex.gettok());
+        assert_eq!(Token::Identifier("b".into()), lex.gettok());
+        assert_eq!(Token::Identifier("c".into()), lex.gettok());
+        assert_eq!(Token::Char('!'), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
 
-        let mut lex = Lexer::new("\n    a \n\r  b \r \n     c \r\r  \n   ".chars());
-        assert_eq!(Token::Identifier("a".into()), lex.gettok());
-        assert_eq!(Token::Identifier("b".into()), lex.gettok());
-        assert_eq!(Token::Identifier("c".into()), lex.gettok());
-        assert_eq!(Token::Eof, lex.gettok());
+        let mut lex = Lexer::new("\n    a \n\r  b \r \n     c \r\r  \n   ".chars());
+        assert_eq!(Token::Identifier("a".into()), lex.gettok());
+        assert_eq!(Token::Identifier("b".into()), lex.gettok());
+        assert_eq!(Token::Identifier("c".into()), lex.gettok());
+        assert_eq!(Token::Eof, lex.gettok());
     }
 
-    #[test]
-    fn test_ite() {
-        let mut lex = Lexer::new("if then else".chars());
-        assert_eq!(Token::If, lex.gettok());
-        assert_eq!(Token::Then, lex.gettok());
-        assert_eq!(Token::Else, lex.gettok());
+    #[test]
+    fn test_ite() {
+        let mut lex = Lexer::new("if then else".chars());
+        assert_eq!(Token::If, lex.gettok());
+        assert_eq!(Token::Then, lex.gettok());
+        assert_eq!(Token::Else, lex.gettok());
     }
 
-    #[test]
-    fn test_for() {
-        let mut lex = Lexer::new("for in".chars());
-        assert_eq!(Token::For, lex.gettok());
-        assert_eq!(Token::In, lex.gettok());
+    #[test]
+    fn test_for() {
+        let mut lex = Lexer::new("for in".chars());
+        assert_eq!(Token::For, lex.gettok());
+        assert_eq!(Token::In, lex.gettok());
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -lib.rs - source -
1
+lib.rs - source
1
 2
 3
 4
@@ -115,117 +109,116 @@
 109
 110
 111
-
use std::convert::TryFrom;
-
-pub mod codegen;
-pub mod lexer;
-pub mod llvm;
-pub mod parser;
-
-/// Fixed size of [`SmallCStr`] including the trailing `\0` byte.
-pub const SMALL_STR_SIZE: usize = 16;
-
-/// 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.
-#[derive(Debug, PartialEq)]
-pub struct SmallCStr([u8; SMALL_STR_SIZE]);
-
-impl SmallCStr {
-    /// Create a new C string from `src`.
-    /// Returns [`None`] if `src` exceeds the fixed size or contains any `\0` bytes.
-    pub fn new<T: AsRef<[u8]>>(src: &T) -> Option<SmallCStr> {
-        let src = src.as_ref();
-        let len = src.len();
-
-        // Check for \0 bytes.
-        let contains_null = unsafe { !libc::memchr(src.as_ptr().cast(), 0, len).is_null() };
-
-        if contains_null || len > SMALL_STR_SIZE - 1 {
-            None
-        } else {
-            let mut dest = [0; SMALL_STR_SIZE];
-            dest[..len].copy_from_slice(src);
-            Some(SmallCStr(dest))
+
use std::convert::TryFrom;
+
+pub mod codegen;
+pub mod lexer;
+pub mod llvm;
+pub mod parser;
+
+/// Fixed size of [`SmallCStr`] including the trailing `\0` byte.
+pub const SMALL_STR_SIZE: usize = 16;
+
+/// 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.
+#[derive(Debug, PartialEq)]
+pub struct SmallCStr([u8; SMALL_STR_SIZE]);
+
+impl SmallCStr {
+    /// Create a new C string from `src`.
+    /// Returns [`None`] if `src` exceeds the fixed size or contains any `\0` bytes.
+    pub fn new<T: AsRef<[u8]>>(src: &T) -> Option<SmallCStr> {
+        let src = src.as_ref();
+        let len = src.len();
+
+        // Check for \0 bytes.
+        let contains_null = unsafe { !libc::memchr(src.as_ptr().cast(), 0, len).is_null() };
+
+        if contains_null || len > SMALL_STR_SIZE - 1 {
+            None
+        } else {
+            let mut dest = [0; SMALL_STR_SIZE];
+            dest[..len].copy_from_slice(src);
+            Some(SmallCStr(dest))
         }
     }
 
-    /// Return pointer to C string.
-    pub const fn as_ptr(&self) -> *const libc::c_char {
-        self.0.as_ptr().cast()
+    /// Return pointer to C string.
+    pub const fn as_ptr(&self) -> *const libc::c_char {
+        self.0.as_ptr().cast()
     }
 }
 
-impl TryFrom<&str> for SmallCStr {
-    type Error = ();
+impl TryFrom<&str> for SmallCStr {
+    type Error = ();
 
-    fn try_from(value: &str) -> Result<Self, Self::Error> {
-        SmallCStr::new(&value).ok_or(())
+    fn try_from(value: &str) -> Result<Self, Self::Error> {
+        SmallCStr::new(&value).ok_or(())
     }
 }
 
-/// Either type, for APIs accepting two types.
-pub enum Either<A, B> {
-    A(A),
-    B(B),
+/// Either type, for APIs accepting two types.
+pub enum Either<A, B> {
+    A(A),
+    B(B),
 }
 
-#[cfg(test)]
-mod test {
-    use super::{SmallCStr, SMALL_STR_SIZE};
-    use std::convert::TryInto;
+#[cfg(test)]
+mod test {
+    use super::{SmallCStr, SMALL_STR_SIZE};
+    use std::convert::TryInto;
 
-    #[test]
-    fn test_create() {
-        let src = "\x30\x31\x32\x33";
-        let scs = SmallCStr::new(&src).unwrap();
-        assert_eq!(&scs.0[..5], &[0x30, 0x31, 0x32, 0x33, 0x00]);
+    #[test]
+    fn test_create() {
+        let src = "\x30\x31\x32\x33";
+        let scs = SmallCStr::new(&src).unwrap();
+        assert_eq!(&scs.0[..5], &[0x30, 0x31, 0x32, 0x33, 0x00]);
 
-        let src = b"abcd1234";
-        let scs = SmallCStr::new(&src).unwrap();
+        let src = b"abcd1234";
+        let scs = SmallCStr::new(&src).unwrap();
         assert_eq!(
-            &scs.0[..9],
+            &scs.0[..9],
             &[0x61, 0x62, 0x63, 0x64, 0x31, 0x32, 0x33, 0x34, 0x00]
         );
     }
 
-    #[test]
-    fn test_contain_null() {
-        let src = "\x30\x00\x32\x33";
-        let scs = SmallCStr::new(&src);
-        assert_eq!(scs, None);
+    #[test]
+    fn test_contain_null() {
+        let src = "\x30\x00\x32\x33";
+        let scs = SmallCStr::new(&src);
+        assert_eq!(scs, None);
 
-        let src = "\x30\x31\x32\x33\x00";
-        let scs = SmallCStr::new(&src);
-        assert_eq!(scs, None);
+        let src = "\x30\x31\x32\x33\x00";
+        let scs = SmallCStr::new(&src);
+        assert_eq!(scs, None);
     }
 
-    #[test]
-    fn test_too_large() {
-        let src = (0..SMALL_STR_SIZE).map(|_| 'a').collect::<String>();
-        let scs = SmallCStr::new(&src);
-        assert_eq!(scs, None);
+    #[test]
+    fn test_too_large() {
+        let src = (0..SMALL_STR_SIZE).map(|_| 'a').collect::<String>();
+        let scs = SmallCStr::new(&src);
+        assert_eq!(scs, None);
 
-        let src = (0..SMALL_STR_SIZE + 10).map(|_| 'a').collect::<String>();
-        let scs = SmallCStr::new(&src);
-        assert_eq!(scs, None);
+        let src = (0..SMALL_STR_SIZE + 10).map(|_| 'a').collect::<String>();
+        let scs = SmallCStr::new(&src);
+        assert_eq!(scs, None);
     }
 
-    #[test]
-    fn test_try_into() {
-        let src = "\x30\x31\x32\x33";
-        let scs: Result<SmallCStr, ()> = src.try_into();
-        assert!(scs.is_ok());
+    #[test]
+    fn test_try_into() {
+        let src = "\x30\x31\x32\x33";
+        let scs: Result<SmallCStr, ()> = src.try_into();
+        assert!(scs.is_ok());
 
-        let src = (0..SMALL_STR_SIZE).map(|_| 'a').collect::<String>();
-        let scs: Result<SmallCStr, ()> = src.as_str().try_into();
-        assert!(scs.is_err());
+        let src = (0..SMALL_STR_SIZE).map(|_| 'a').collect::<String>();
+        let scs: Result<SmallCStr, ()> = src.as_str().try_into();
+        assert!(scs.is_err());
 
-        let src = (0..SMALL_STR_SIZE + 10).map(|_| 'a').collect::<String>();
-        let scs: Result<SmallCStr, ()> = src.as_str().try_into();
-        assert!(scs.is_err());
+        let src = (0..SMALL_STR_SIZE + 10).map(|_| 'a').collect::<String>();
+        let scs: Result<SmallCStr, ()> = src.as_str().try_into();
+        assert!(scs.is_err());
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -basic_block.rs - source -
1
+basic_block.rs - source
1
 2
 3
 4
@@ -43,45 +37,44 @@
 37
 38
 39
-
use llvm_sys::{core::LLVMGetBasicBlockParent, prelude::LLVMBasicBlockRef};
+
use llvm_sys::{core::LLVMGetBasicBlockParent, prelude::LLVMBasicBlockRef};
 
-use std::marker::PhantomData;
+use std::marker::PhantomData;
 
-use super::FnValue;
+use super::FnValue;
 
-/// Wrapper for a LLVM Basic Block.
-#[derive(Copy, Clone)]
-pub struct BasicBlock<'llvm>(LLVMBasicBlockRef, PhantomData<&'llvm ()>);
+/// Wrapper for a LLVM Basic Block.
+#[derive(Copy, Clone)]
+pub struct BasicBlock<'llvm>(LLVMBasicBlockRef, PhantomData<&'llvm ()>);
 
-impl<'llvm> BasicBlock<'llvm> {
-    /// Create a new BasicBlock instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if `bb_ref` is a null pointer.
-    pub(super) fn new(bb_ref: LLVMBasicBlockRef) -> BasicBlock<'llvm> {
-        assert!(!bb_ref.is_null());
-        BasicBlock(bb_ref, PhantomData)
+impl<'llvm> BasicBlock<'llvm> {
+    /// Create a new BasicBlock instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `bb_ref` is a null pointer.
+    pub(super) fn new(bb_ref: LLVMBasicBlockRef) -> BasicBlock<'llvm> {
+        assert!(!bb_ref.is_null());
+        BasicBlock(bb_ref, PhantomData)
     }
 
-    /// Get the raw LLVM value reference.
-    #[inline]
-    pub(super) fn bb_ref(&self) -> LLVMBasicBlockRef {
-        self.0
-    }
+    /// Get the raw LLVM value reference.
+    #[inline]
+    pub(super) fn bb_ref(&self) -> LLVMBasicBlockRef {
+        self.0
+    }
 
-    /// Get the function to which the basic block belongs.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn get_parent(&self) -> FnValue<'llvm> {
-        let value_ref = unsafe { LLVMGetBasicBlockParent(self.bb_ref()) };
-        assert!(!value_ref.is_null());
+    /// Get the function to which the basic block belongs.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn get_parent(&self) -> FnValue<'llvm> {
+        let value_ref = unsafe { LLVMGetBasicBlockParent(self.bb_ref()) };
+        assert!(!value_ref.is_null());
 
-        FnValue::new(value_ref)
+        FnValue::new(value_ref)
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -builder.rs - source -
1
+builder.rs - source
1
 2
 3
 4
@@ -283,285 +277,284 @@
 277
 278
 279
-
use llvm_sys::{
-    core::{
-        LLVMAddIncoming, LLVMBuildBr, LLVMBuildCondBr, LLVMBuildFAdd, LLVMBuildFCmp, LLVMBuildFMul,
-        LLVMBuildFSub, LLVMBuildPhi, LLVMBuildRet, LLVMBuildUIToFP, LLVMCreateBuilderInContext,
-        LLVMDisposeBuilder, LLVMGetInsertBlock, LLVMPositionBuilderAtEnd,
+
use llvm_sys::{
+    core::{
+        LLVMAddIncoming, LLVMBuildBr, LLVMBuildCondBr, LLVMBuildFAdd, LLVMBuildFCmp, LLVMBuildFMul,
+        LLVMBuildFSub, LLVMBuildPhi, LLVMBuildRet, LLVMBuildUIToFP, LLVMCreateBuilderInContext,
+        LLVMDisposeBuilder, LLVMGetInsertBlock, LLVMPositionBuilderAtEnd,
     },
-    prelude::{LLVMBuilderRef, LLVMValueRef},
-    LLVMRealPredicate,
+    prelude::{LLVMBuilderRef, LLVMValueRef},
+    LLVMRealPredicate,
 };
 
-use std::marker::PhantomData;
+use std::marker::PhantomData;
 
-use super::{BasicBlock, FnValue, Module, PhiValue, Type, Value};
+use super::{BasicBlock, FnValue, Module, PhiValue, Type, Value};
 
-// Definition of LLVM C API functions using our `repr(transparent)` types.
-extern "C" {
-    fn LLVMBuildCall2(
-        arg1: LLVMBuilderRef,
-        arg2: Type<'_>,
-        Fn: FnValue<'_>,
-        Args: *mut Value<'_>,
-        NumArgs: ::libc::c_uint,
-        Name: *const ::libc::c_char,
-    ) -> LLVMValueRef;
+// Definition of LLVM C API functions using our `repr(transparent)` types.
+extern "C" {
+    fn LLVMBuildCall2(
+        arg1: LLVMBuilderRef,
+        arg2: Type<'_>,
+        Fn: FnValue<'_>,
+        Args: *mut Value<'_>,
+        NumArgs: ::libc::c_uint,
+        Name: *const ::libc::c_char,
+    ) -> LLVMValueRef;
 }
 
-/// Wrapper for a LLVM IR Builder.
-pub struct IRBuilder<'llvm> {
-    builder: LLVMBuilderRef,
-    _ctx: PhantomData<&'llvm ()>,
+/// Wrapper for a LLVM IR Builder.
+pub struct IRBuilder<'llvm> {
+    builder: LLVMBuilderRef,
+    _ctx: PhantomData<&'llvm ()>,
 }
 
-impl<'llvm> IRBuilder<'llvm> {
-    /// Create a new LLVM IR Builder with the `module`s context.
-    ///
-    /// # Panics
-    ///
-    /// Panics if creating the IR Builder fails.
-    pub fn with_ctx(module: &'llvm Module) -> IRBuilder<'llvm> {
-        let builder = unsafe { LLVMCreateBuilderInContext(module.ctx()) };
-        assert!(!builder.is_null());
+impl<'llvm> IRBuilder<'llvm> {
+    /// Create a new LLVM IR Builder with the `module`s context.
+    ///
+    /// # Panics
+    ///
+    /// Panics if creating the IR Builder fails.
+    pub fn with_ctx(module: &'llvm Module) -> IRBuilder<'llvm> {
+        let builder = unsafe { LLVMCreateBuilderInContext(module.ctx()) };
+        assert!(!builder.is_null());
 
-        IRBuilder {
-            builder,
-            _ctx: PhantomData,
+        IRBuilder {
+            builder,
+            _ctx: PhantomData,
         }
     }
 
-    /// Position the IR Builder at the end of the given Basic Block.
-    pub fn pos_at_end(&self, bb: BasicBlock<'llvm>) {
-        unsafe {
-            LLVMPositionBuilderAtEnd(self.builder, bb.bb_ref());
+    /// Position the IR Builder at the end of the given Basic Block.
+    pub fn pos_at_end(&self, bb: BasicBlock<'llvm>) {
+        unsafe {
+            LLVMPositionBuilderAtEnd(self.builder, bb.bb_ref());
         }
     }
 
-    /// Get the BasicBlock the IRBuilder currently inputs into.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn get_insert_block(&self) -> BasicBlock<'llvm> {
-        let bb_ref = unsafe { LLVMGetInsertBlock(self.builder) };
-        assert!(!bb_ref.is_null());
+    /// Get the BasicBlock the IRBuilder currently inputs into.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn get_insert_block(&self) -> BasicBlock<'llvm> {
+        let bb_ref = unsafe { LLVMGetInsertBlock(self.builder) };
+        assert!(!bb_ref.is_null());
 
-        BasicBlock::new(bb_ref)
+        BasicBlock::new(bb_ref)
     }
 
-    /// Emit a [fadd](https://llvm.org/docs/LangRef.html#fadd-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn fadd(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
-        debug_assert!(lhs.is_f64(), "fadd: Expected f64 as lhs operand!");
-        debug_assert!(rhs.is_f64(), "fadd: Expected f64 as rhs operand!");
+    /// Emit a [fadd](https://llvm.org/docs/LangRef.html#fadd-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn fadd(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
+        debug_assert!(lhs.is_f64(), "fadd: Expected f64 as lhs operand!");
+        debug_assert!(rhs.is_f64(), "fadd: Expected f64 as rhs operand!");
 
-        let value_ref = unsafe {
-            LLVMBuildFAdd(
-                self.builder,
-                lhs.value_ref(),
-                rhs.value_ref(),
-                b"fadd\0".as_ptr().cast(),
+        let value_ref = unsafe {
+            LLVMBuildFAdd(
+                self.builder,
+                lhs.value_ref(),
+                rhs.value_ref(),
+                b"fadd\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [fsub](https://llvm.org/docs/LangRef.html#fsub-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn fsub(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
-        debug_assert!(lhs.is_f64(), "fsub: Expected f64 as lhs operand!");
-        debug_assert!(rhs.is_f64(), "fsub: Expected f64 as rhs operand!");
+    /// Emit a [fsub](https://llvm.org/docs/LangRef.html#fsub-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn fsub(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
+        debug_assert!(lhs.is_f64(), "fsub: Expected f64 as lhs operand!");
+        debug_assert!(rhs.is_f64(), "fsub: Expected f64 as rhs operand!");
 
-        let value_ref = unsafe {
-            LLVMBuildFSub(
-                self.builder,
-                lhs.value_ref(),
-                rhs.value_ref(),
-                b"fsub\0".as_ptr().cast(),
+        let value_ref = unsafe {
+            LLVMBuildFSub(
+                self.builder,
+                lhs.value_ref(),
+                rhs.value_ref(),
+                b"fsub\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [fmul](https://llvm.org/docs/LangRef.html#fmul-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn fmul(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
-        debug_assert!(lhs.is_f64(), "fmul: Expected f64 as lhs operand!");
-        debug_assert!(rhs.is_f64(), "fmul: Expected f64 as rhs operand!");
+    /// Emit a [fmul](https://llvm.org/docs/LangRef.html#fmul-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn fmul(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
+        debug_assert!(lhs.is_f64(), "fmul: Expected f64 as lhs operand!");
+        debug_assert!(rhs.is_f64(), "fmul: Expected f64 as rhs operand!");
 
-        let value_ref = unsafe {
-            LLVMBuildFMul(
-                self.builder,
-                lhs.value_ref(),
-                rhs.value_ref(),
-                b"fmul\0".as_ptr().cast(),
+        let value_ref = unsafe {
+            LLVMBuildFMul(
+                self.builder,
+                lhs.value_ref(),
+                rhs.value_ref(),
+                b"fmul\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [fcmpult](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn fcmpult(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
-        debug_assert!(lhs.is_f64(), "fcmpult: Expected f64 as lhs operand!");
-        debug_assert!(rhs.is_f64(), "fcmpult: Expected f64 as rhs operand!");
+    /// Emit a [fcmpult](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn fcmpult(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
+        debug_assert!(lhs.is_f64(), "fcmpult: Expected f64 as lhs operand!");
+        debug_assert!(rhs.is_f64(), "fcmpult: Expected f64 as rhs operand!");
 
-        let value_ref = unsafe {
-            LLVMBuildFCmp(
-                self.builder,
-                LLVMRealPredicate::LLVMRealULT,
-                lhs.value_ref(),
-                rhs.value_ref(),
-                b"fcmpult\0".as_ptr().cast(),
+        let value_ref = unsafe {
+            LLVMBuildFCmp(
+                self.builder,
+                LLVMRealPredicate::LLVMRealULT,
+                lhs.value_ref(),
+                rhs.value_ref(),
+                b"fcmpult\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [fcmpone](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn fcmpone(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
-        debug_assert!(lhs.is_f64(), "fcmone: Expected f64 as lhs operand!");
-        debug_assert!(rhs.is_f64(), "fcmone: Expected f64 as rhs operand!");
+    /// Emit a [fcmpone](https://llvm.org/docs/LangRef.html#fcmp-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn fcmpone(&self, lhs: Value<'llvm>, rhs: Value<'llvm>) -> Value<'llvm> {
+        debug_assert!(lhs.is_f64(), "fcmone: Expected f64 as lhs operand!");
+        debug_assert!(rhs.is_f64(), "fcmone: Expected f64 as rhs operand!");
 
-        let value_ref = unsafe {
-            LLVMBuildFCmp(
-                self.builder,
-                LLVMRealPredicate::LLVMRealONE,
-                lhs.value_ref(),
-                rhs.value_ref(),
-                b"fcmpone\0".as_ptr().cast(),
+        let value_ref = unsafe {
+            LLVMBuildFCmp(
+                self.builder,
+                LLVMRealPredicate::LLVMRealONE,
+                lhs.value_ref(),
+                rhs.value_ref(),
+                b"fcmpone\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [uitofp](https://llvm.org/docs/LangRef.html#uitofp-to-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn uitofp(&self, val: Value<'llvm>, dest_type: Type<'llvm>) -> Value<'llvm> {
-        debug_assert!(val.is_int(), "uitofp: Expected integer operand!");
+    /// Emit a [uitofp](https://llvm.org/docs/LangRef.html#uitofp-to-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn uitofp(&self, val: Value<'llvm>, dest_type: Type<'llvm>) -> Value<'llvm> {
+        debug_assert!(val.is_int(), "uitofp: Expected integer operand!");
 
-        let value_ref = unsafe {
-            LLVMBuildUIToFP(
-                self.builder,
-                val.value_ref(),
-                dest_type.type_ref(),
-                b"uitofp\0".as_ptr().cast(),
+        let value_ref = unsafe {
+            LLVMBuildUIToFP(
+                self.builder,
+                val.value_ref(),
+                dest_type.type_ref(),
+                b"uitofp\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [call](https://llvm.org/docs/LangRef.html#call-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn call(&self, fn_value: FnValue<'llvm>, args: &mut [Value<'llvm>]) -> Value<'llvm> {
-        let value_ref = unsafe {
-            LLVMBuildCall2(
-                self.builder,
-                fn_value.ret_type(),
-                fn_value,
-                args.as_mut_ptr(),
-                args.len() as libc::c_uint,
-                b"call\0".as_ptr().cast(),
+    /// Emit a [call](https://llvm.org/docs/LangRef.html#call-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn call(&self, fn_value: FnValue<'llvm>, args: &mut [Value<'llvm>]) -> Value<'llvm> {
+        let value_ref = unsafe {
+            LLVMBuildCall2(
+                self.builder,
+                fn_value.ret_type(),
+                fn_value,
+                args.as_mut_ptr(),
+                args.len() as libc::c_uint,
+                b"call\0".as_ptr().cast(),
             )
         };
-        Value::new(value_ref)
+        Value::new(value_ref)
     }
 
-    /// Emit a [ret](https://llvm.org/docs/LangRef.html#ret-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn ret(&self, ret: Value<'llvm>) {
-        let ret = unsafe { LLVMBuildRet(self.builder, ret.value_ref()) };
-        assert!(!ret.is_null());
+    /// Emit a [ret](https://llvm.org/docs/LangRef.html#ret-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn ret(&self, ret: Value<'llvm>) {
+        let ret = unsafe { LLVMBuildRet(self.builder, ret.value_ref()) };
+        assert!(!ret.is_null());
     }
 
-    /// Emit an unconditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn br(&self, dest: BasicBlock<'llvm>) {
-        let br_ref = unsafe { LLVMBuildBr(self.builder, dest.bb_ref()) };
-        assert!(!br_ref.is_null());
+    /// Emit an unconditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn br(&self, dest: BasicBlock<'llvm>) {
+        let br_ref = unsafe { LLVMBuildBr(self.builder, dest.bb_ref()) };
+        assert!(!br_ref.is_null());
     }
 
-    /// Emit a conditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn cond_br(&self, cond: Value<'llvm>, then: BasicBlock<'llvm>, else_: BasicBlock<'llvm>) {
-        let br_ref = unsafe {
-            LLVMBuildCondBr(
-                self.builder,
-                cond.value_ref(),
-                then.bb_ref(),
-                else_.bb_ref(),
+    /// Emit a conditional [br](https://llvm.org/docs/LangRef.html#br-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn cond_br(&self, cond: Value<'llvm>, then: BasicBlock<'llvm>, else_: BasicBlock<'llvm>) {
+        let br_ref = unsafe {
+            LLVMBuildCondBr(
+                self.builder,
+                cond.value_ref(),
+                then.bb_ref(),
+                else_.bb_ref(),
             )
         };
-        assert!(!br_ref.is_null());
+        assert!(!br_ref.is_null());
     }
 
-    /// Emit a [phi](https://llvm.org/docs/LangRef.html#phi-instruction) instruction.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn phi(
+    /// Emit a [phi](https://llvm.org/docs/LangRef.html#phi-instruction) instruction.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn phi(
         &self,
-        phi_type: Type<'llvm>,
-        incoming: &[(Value<'llvm>, BasicBlock<'llvm>)],
-    ) -> PhiValue<'llvm> {
-        let phi_ref =
-            unsafe { LLVMBuildPhi(self.builder, phi_type.type_ref(), b"phi\0".as_ptr().cast()) };
-        assert!(!phi_ref.is_null());
+        phi_type: Type<'llvm>,
+        incoming: &[(Value<'llvm>, BasicBlock<'llvm>)],
+    ) -> PhiValue<'llvm> {
+        let phi_ref =
+            unsafe { LLVMBuildPhi(self.builder, phi_type.type_ref(), b"phi\0".as_ptr().cast()) };
+        assert!(!phi_ref.is_null());
 
-        for (val, bb) in incoming {
+        for (val, bb) in incoming {
             debug_assert_eq!(
-                val.type_of().kind(),
-                phi_type.kind(),
-                "Type of incoming phi value must be the same as the type used to build the phi node."
-            );
+                val.type_of().kind(),
+                phi_type.kind(),
+                "Type of incoming phi value must be the same as the type used to build the phi node."
+            );
 
-            unsafe {
-                LLVMAddIncoming(phi_ref, &mut val.value_ref() as _, &mut bb.bb_ref() as _, 1);
+            unsafe {
+                LLVMAddIncoming(phi_ref, &mut val.value_ref() as _, &mut bb.bb_ref() as _, 1);
             }
         }
 
-        PhiValue::new(phi_ref)
+        PhiValue::new(phi_ref)
     }
 }
 
-impl Drop for IRBuilder<'_> {
-    fn drop(&mut self) {
-        unsafe { LLVMDisposeBuilder(self.builder) }
+impl Drop for IRBuilder<'_> {
+    fn drop(&mut self) {
+        unsafe { LLVMDisposeBuilder(self.builder) }
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -lljit.rs - source -
1
+lljit.rs - source
1
 2
 3
 4
@@ -158,160 +152,159 @@
 152
 153
 154
-
use llvm_sys::orc2::{
-    lljit::{
-        LLVMOrcCreateLLJIT, LLVMOrcLLJITAddLLVMIRModuleWithRT, LLVMOrcLLJITGetGlobalPrefix,
-        LLVMOrcLLJITGetMainJITDylib, LLVMOrcLLJITLookup, LLVMOrcLLJITRef,
+
use llvm_sys::orc2::{
+    lljit::{
+        LLVMOrcCreateLLJIT, LLVMOrcLLJITAddLLVMIRModuleWithRT, LLVMOrcLLJITGetGlobalPrefix,
+        LLVMOrcLLJITGetMainJITDylib, LLVMOrcLLJITLookup, LLVMOrcLLJITRef,
     },
-    LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess, LLVMOrcDefinitionGeneratorRef,
-    LLVMOrcJITDylibAddGenerator, LLVMOrcJITDylibCreateResourceTracker, LLVMOrcJITDylibRef,
-    LLVMOrcReleaseResourceTracker, LLVMOrcResourceTrackerRef, LLVMOrcResourceTrackerRemove,
+    LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess, LLVMOrcDefinitionGeneratorRef,
+    LLVMOrcJITDylibAddGenerator, LLVMOrcJITDylibCreateResourceTracker, LLVMOrcJITDylibRef,
+    LLVMOrcReleaseResourceTracker, LLVMOrcResourceTrackerRef, LLVMOrcResourceTrackerRemove,
 };
 
-use std::convert::TryFrom;
-use std::marker::PhantomData;
+use std::convert::TryFrom;
+use std::marker::PhantomData;
 
-use super::{Error, Module};
-use crate::SmallCStr;
+use super::{Error, Module};
+use crate::SmallCStr;
 
-/// Marker trait to constrain function signatures that can be looked up in the JIT.
-pub trait JitFn {}
+/// Marker trait to constrain function signatures that can be looked up in the JIT.
+pub trait JitFn {}
 
-impl JitFn for unsafe extern "C" fn() -> f64 {}
+impl JitFn for unsafe extern "C" fn() -> f64 {}
 
-/// Wrapper for a LLVM [LLJIT](https://www.llvm.org/docs/ORCv2.html#lljit-and-lllazyjit).
-pub struct LLJit {
-    jit: LLVMOrcLLJITRef,
-    dylib: LLVMOrcJITDylibRef,
+/// Wrapper for a LLVM [LLJIT](https://www.llvm.org/docs/ORCv2.html#lljit-and-lllazyjit).
+pub struct LLJit {
+    jit: LLVMOrcLLJITRef,
+    dylib: LLVMOrcJITDylibRef,
 }
 
-impl LLJit {
-    /// Create a new LLJit instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer or an error.
-    pub fn new() -> LLJit {
-        let (jit, dylib) = unsafe {
-            let mut jit = std::ptr::null_mut();
-            let err = LLVMOrcCreateLLJIT(
-                &mut jit as _,
-                std::ptr::null_mut(), /* builder: nullptr -> default */
-            );
+impl LLJit {
+    /// Create a new LLJit instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer or an error.
+    pub fn new() -> LLJit {
+        let (jit, dylib) = unsafe {
+            let mut jit = std::ptr::null_mut();
+            let err = LLVMOrcCreateLLJIT(
+                &mut jit as _,
+                std::ptr::null_mut(), /* builder: nullptr -> default */
+            );
 
-            if let Some(err) = Error::from(err) {
-                panic!("Error: {}", err.as_str());
+            if let Some(err) = Error::from(err) {
+                panic!("Error: {}", err.as_str());
             }
 
-            let dylib = LLVMOrcLLJITGetMainJITDylib(jit);
-            assert!(!dylib.is_null());
+            let dylib = LLVMOrcLLJITGetMainJITDylib(jit);
+            assert!(!dylib.is_null());
 
-            (jit, dylib)
+            (jit, dylib)
         };
 
-        LLJit { jit, dylib }
+        LLJit { jit, dylib }
     }
 
-    /// 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.
-    pub fn add_module(&self, module: Module) -> ResourceTracker<'_> {
-        let tsmod = module.into_raw_thread_safe_module();
+    /// 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.
+    pub fn add_module(&self, module: Module) -> ResourceTracker<'_> {
+        let tsmod = module.into_raw_thread_safe_module();
 
-        let rt = unsafe {
-            let rt = LLVMOrcJITDylibCreateResourceTracker(self.dylib);
-            let err = LLVMOrcLLJITAddLLVMIRModuleWithRT(self.jit, rt, tsmod);
+        let rt = unsafe {
+            let rt = LLVMOrcJITDylibCreateResourceTracker(self.dylib);
+            let err = LLVMOrcLLJITAddLLVMIRModuleWithRT(self.jit, rt, tsmod);
 
-            if let Some(err) = Error::from(err) {
-                panic!("Error: {}", err.as_str());
+            if let Some(err) = Error::from(err) {
+                panic!("Error: {}", err.as_str());
             }
 
-            rt
+            rt
         };
 
-        ResourceTracker::new(rt)
+        ResourceTracker::new(rt)
     }
 
-    /// Find the symbol with the name `sym` in the JIT.
-    ///
-    /// # Panics
-    ///
-    /// Panics if the symbol is not found in the JIT.
-    pub fn find_symbol<F: JitFn>(&self, sym: &str) -> F {
-        let sym =
-            SmallCStr::try_from(sym).expect("Failed to convert 'sym' argument to small C string!");
+    /// Find the symbol with the name `sym` in the JIT.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the symbol is not found in the JIT.
+    pub fn find_symbol<F: JitFn>(&self, sym: &str) -> F {
+        let sym =
+            SmallCStr::try_from(sym).expect("Failed to convert 'sym' argument to small C string!");
 
-        unsafe {
-            let mut addr = 0u64;
-            let err = LLVMOrcLLJITLookup(self.jit, &mut addr as _, sym.as_ptr());
+        unsafe {
+            let mut addr = 0u64;
+            let err = LLVMOrcLLJITLookup(self.jit, &mut addr as _, sym.as_ptr());
 
-            if let Some(err) = Error::from(err) {
-                panic!("Error: {}", err.as_str());
+            if let Some(err) = Error::from(err) {
+                panic!("Error: {}", err.as_str());
             }
 
-            debug_assert_eq!(core::mem::size_of_val(&addr), core::mem::size_of::<F>());
-            std::mem::transmute_copy(&addr)
+            debug_assert_eq!(core::mem::size_of_val(&addr), core::mem::size_of::<F>());
+            std::mem::transmute_copy(&addr)
         }
     }
 
-    /// Enable lookup of dynamic symbols available in the current process from the JIT.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns an error.
-    pub fn enable_process_symbols(&self) {
-        unsafe {
-            let mut proc_syms_gen: LLVMOrcDefinitionGeneratorRef = std::ptr::null_mut();
-            let err = LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
-                &mut proc_syms_gen as _,
-                self.global_prefix(),
-                None,                 /* filter */
-                std::ptr::null_mut(), /* filter ctx */
-            );
+    /// Enable lookup of dynamic symbols available in the current process from the JIT.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns an error.
+    pub fn enable_process_symbols(&self) {
+        unsafe {
+            let mut proc_syms_gen: LLVMOrcDefinitionGeneratorRef = std::ptr::null_mut();
+            let err = LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
+                &mut proc_syms_gen as _,
+                self.global_prefix(),
+                None,                 /* filter */
+                std::ptr::null_mut(), /* filter ctx */
+            );
 
-            if let Some(err) = Error::from(err) {
-                panic!("Error: {}", err.as_str());
+            if let Some(err) = Error::from(err) {
+                panic!("Error: {}", err.as_str());
             }
 
-            LLVMOrcJITDylibAddGenerator(self.dylib, proc_syms_gen);
+            LLVMOrcJITDylibAddGenerator(self.dylib, proc_syms_gen);
         }
     }
 
-    /// Return the global prefix character according to the LLJITs data layout.
-    fn global_prefix(&self) -> libc::c_char {
-        unsafe { LLVMOrcLLJITGetGlobalPrefix(self.jit) }
+    /// Return the global prefix character according to the LLJITs data layout.
+    fn global_prefix(&self) -> libc::c_char {
+        unsafe { LLVMOrcLLJITGetGlobalPrefix(self.jit) }
     }
 }
 
-/// 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.
-pub struct ResourceTracker<'jit>(LLVMOrcResourceTrackerRef, PhantomData<&'jit ()>);
+/// 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.
+pub struct ResourceTracker<'jit>(LLVMOrcResourceTrackerRef, PhantomData<&'jit ()>);
 
-impl<'jit> ResourceTracker<'jit> {
-    fn new(rt: LLVMOrcResourceTrackerRef) -> ResourceTracker<'jit> {
-        assert!(!rt.is_null());
-        ResourceTracker(rt, PhantomData)
+impl<'jit> ResourceTracker<'jit> {
+    fn new(rt: LLVMOrcResourceTrackerRef) -> ResourceTracker<'jit> {
+        assert!(!rt.is_null());
+        ResourceTracker(rt, PhantomData)
     }
 }
 
-impl Drop for ResourceTracker<'_> {
-    fn drop(&mut self) {
-        unsafe {
-            let err = LLVMOrcResourceTrackerRemove(self.0);
+impl Drop for ResourceTracker<'_> {
+    fn drop(&mut self) {
+        unsafe {
+            let err = LLVMOrcResourceTrackerRemove(self.0);
 
-            if let Some(err) = Error::from(err) {
-                panic!("Error: {}", err.as_str());
+            if let Some(err) = Error::from(err) {
+                panic!("Error: {}", err.as_str());
             }
 
-            LLVMOrcReleaseResourceTracker(self.0);
+            LLVMOrcReleaseResourceTracker(self.0);
         };
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -mod.rs - source -
1
+mod.rs - source
1
 2
 3
 4
@@ -78,80 +72,79 @@
 72
 73
 74
-
//! 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.
+
//! 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.
 
-use llvm_sys::{
-    core::LLVMShutdown,
-    error::{LLVMDisposeErrorMessage, LLVMErrorRef, LLVMGetErrorMessage},
-    target::{
-        LLVM_InitializeNativeAsmParser, LLVM_InitializeNativeAsmPrinter,
-        LLVM_InitializeNativeTarget,
+use llvm_sys::{
+    core::LLVMShutdown,
+    error::{LLVMDisposeErrorMessage, LLVMErrorRef, LLVMGetErrorMessage},
+    target::{
+        LLVM_InitializeNativeAsmParser, LLVM_InitializeNativeAsmPrinter,
+        LLVM_InitializeNativeTarget,
     },
 };
 
-use std::ffi::CStr;
+use std::ffi::CStr;
 
-mod basic_block;
-mod builder;
-mod lljit;
-mod module;
-mod pass_manager;
-mod type_;
-mod value;
+mod basic_block;
+mod builder;
+mod lljit;
+mod module;
+mod pass_manager;
+mod type_;
+mod value;
 
-pub use basic_block::BasicBlock;
-pub use builder::IRBuilder;
-pub use lljit::{LLJit, ResourceTracker};
-pub use module::Module;
-pub use pass_manager::FunctionPassManager;
-pub use type_::Type;
-pub use value::{FnValue, PhiValue, Value};
+pub use basic_block::BasicBlock;
+pub use builder::IRBuilder;
+pub use lljit::{LLJit, ResourceTracker};
+pub use module::Module;
+pub use pass_manager::FunctionPassManager;
+pub use type_::Type;
+pub use value::{FnValue, PhiValue, Value};
 
-struct Error<'llvm>(&'llvm mut libc::c_char);
+struct Error<'llvm>(&'llvm mut libc::c_char);
 
-impl<'llvm> Error<'llvm> {
-    fn from(err: LLVMErrorRef) -> Option<Error<'llvm>> {
-        (!err.is_null()).then(|| Error(unsafe { &mut *LLVMGetErrorMessage(err) }))
+impl<'llvm> Error<'llvm> {
+    fn from(err: LLVMErrorRef) -> Option<Error<'llvm>> {
+        (!err.is_null()).then(|| Error(unsafe { &mut *LLVMGetErrorMessage(err) }))
     }
 
-    fn as_str(&self) -> &str {
-        unsafe { CStr::from_ptr(self.0) }
-            .to_str()
-            .expect("Expected valid UTF8 string from LLVM API")
+    fn as_str(&self) -> &str {
+        unsafe { CStr::from_ptr(self.0) }
+            .to_str()
+            .expect("Expected valid UTF8 string from LLVM API")
     }
 }
 
-impl Drop for Error<'_> {
-    fn drop(&mut self) {
-        unsafe {
-            LLVMDisposeErrorMessage(self.0 as *mut libc::c_char);
+impl Drop for Error<'_> {
+    fn drop(&mut self) {
+        unsafe {
+            LLVMDisposeErrorMessage(self.0 as *mut libc::c_char);
         }
     }
 }
 
-/// Initialize native target for corresponding to host (useful for jitting).
-pub fn initialize_native_taget() {
-    unsafe {
-        assert_eq!(LLVM_InitializeNativeTarget(), 0);
-        assert_eq!(LLVM_InitializeNativeAsmParser(), 0);
-        assert_eq!(LLVM_InitializeNativeAsmPrinter(), 0);
+/// Initialize native target for corresponding to host (useful for jitting).
+pub fn initialize_native_taget() {
+    unsafe {
+        assert_eq!(LLVM_InitializeNativeTarget(), 0);
+        assert_eq!(LLVM_InitializeNativeAsmParser(), 0);
+        assert_eq!(LLVM_InitializeNativeAsmPrinter(), 0);
     }
 }
 
-/// Deallocate and destroy all "ManagedStatic" variables.
-pub fn shutdown() {
-    unsafe {
-        LLVMShutdown();
+/// Deallocate and destroy all "ManagedStatic" variables.
+pub fn shutdown() {
+    unsafe {
+        LLVMShutdown();
     };
 }
 
-
- \ No newline at end of file +
\ 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 @@ -module.rs - source -
1
+module.rs - source
1
 2
 3
 4
@@ -214,216 +208,215 @@
 208
 209
 210
-
use llvm_sys::{
-    core::{
-        LLVMAddFunction, LLVMAppendBasicBlockInContext, LLVMCreateBasicBlockInContext,
-        LLVMDisposeModule, LLVMDoubleTypeInContext, LLVMDumpModule, LLVMGetNamedFunction,
-        LLVMModuleCreateWithNameInContext,
+
use llvm_sys::{
+    core::{
+        LLVMAddFunction, LLVMAppendBasicBlockInContext, LLVMCreateBasicBlockInContext,
+        LLVMDisposeModule, LLVMDoubleTypeInContext, LLVMDumpModule, LLVMGetNamedFunction,
+        LLVMModuleCreateWithNameInContext,
     },
-    orc2::{
-        LLVMOrcCreateNewThreadSafeContext, LLVMOrcCreateNewThreadSafeModule,
-        LLVMOrcDisposeThreadSafeContext, LLVMOrcThreadSafeContextGetContext,
-        LLVMOrcThreadSafeContextRef, LLVMOrcThreadSafeModuleRef,
+    orc2::{
+        LLVMOrcCreateNewThreadSafeContext, LLVMOrcCreateNewThreadSafeModule,
+        LLVMOrcDisposeThreadSafeContext, LLVMOrcThreadSafeContextGetContext,
+        LLVMOrcThreadSafeContextRef, LLVMOrcThreadSafeModuleRef,
     },
-    prelude::{LLVMBool, LLVMContextRef, LLVMModuleRef, LLVMTypeRef},
-    LLVMTypeKind,
+    prelude::{LLVMBool, LLVMContextRef, LLVMModuleRef, LLVMTypeRef},
+    LLVMTypeKind,
 };
 
-use std::convert::TryFrom;
+use std::convert::TryFrom;
 
-use super::{BasicBlock, FnValue, Type};
-use crate::SmallCStr;
+use super::{BasicBlock, FnValue, Type};
+use crate::SmallCStr;
 
-// Definition of LLVM C API functions using our `repr(transparent)` types.
-extern "C" {
-    fn LLVMFunctionType(
-        ReturnType: Type<'_>,
-        ParamTypes: *mut Type<'_>,
-        ParamCount: ::libc::c_uint,
-        IsVarArg: LLVMBool,
-    ) -> LLVMTypeRef;
+// Definition of LLVM C API functions using our `repr(transparent)` types.
+extern "C" {
+    fn LLVMFunctionType(
+        ReturnType: Type<'_>,
+        ParamTypes: *mut Type<'_>,
+        ParamCount: ::libc::c_uint,
+        IsVarArg: LLVMBool,
+    ) -> LLVMTypeRef;
 }
 
-/// Wrapper for a LLVM Module with its own LLVM Context.
-pub struct Module {
-    tsctx: LLVMOrcThreadSafeContextRef,
-    ctx: LLVMContextRef,
-    module: LLVMModuleRef,
+/// Wrapper for a LLVM Module with its own LLVM Context.
+pub struct Module {
+    tsctx: LLVMOrcThreadSafeContextRef,
+    ctx: LLVMContextRef,
+    module: LLVMModuleRef,
 }
 
-impl<'llvm> Module {
-    /// Create a new Module instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if creating the context or the module fails.
-    pub fn new() -> Self {
-        let (tsctx, ctx, module) = unsafe {
-            // 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).
-            let tc = LLVMOrcCreateNewThreadSafeContext();
-            assert!(!tc.is_null());
+impl<'llvm> Module {
+    /// Create a new Module instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if creating the context or the module fails.
+    pub fn new() -> Self {
+        let (tsctx, ctx, module) = unsafe {
+            // 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).
+            let tc = LLVMOrcCreateNewThreadSafeContext();
+            assert!(!tc.is_null());
 
-            let c = LLVMOrcThreadSafeContextGetContext(tc);
-            let m = LLVMModuleCreateWithNameInContext(b"module\0".as_ptr().cast(), c);
-            assert!(!c.is_null() && !m.is_null());
-            (tc, c, m)
+            let c = LLVMOrcThreadSafeContextGetContext(tc);
+            let m = LLVMModuleCreateWithNameInContext(b"module\0".as_ptr().cast(), c);
+            assert!(!c.is_null() && !m.is_null());
+            (tc, c, m)
         };
 
-        Module { tsctx, ctx, module }
+        Module { tsctx, ctx, module }
     }
 
-    /// Get the raw LLVM context reference.
-    #[inline]
-    pub(super) fn ctx(&self) -> LLVMContextRef {
-        self.ctx
+    /// Get the raw LLVM context reference.
+    #[inline]
+    pub(super) fn ctx(&self) -> LLVMContextRef {
+        self.ctx
     }
 
-    /// Get the raw LLVM module reference.
-    #[inline]
-    pub(super) fn module(&self) -> LLVMModuleRef {
-        self.module
+    /// Get the raw LLVM module reference.
+    #[inline]
+    pub(super) fn module(&self) -> LLVMModuleRef {
+        self.module
     }
 
-    /// 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.
-    #[inline]
-    pub(super) fn into_raw_thread_safe_module(mut self) -> LLVMOrcThreadSafeModuleRef {
-        let m = std::mem::replace(&mut self.module, std::ptr::null_mut());
+    /// 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.
+    #[inline]
+    pub(super) fn into_raw_thread_safe_module(mut self) -> LLVMOrcThreadSafeModuleRef {
+        let m = std::mem::replace(&mut self.module, std::ptr::null_mut());
 
-        // 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.
-        let tm = unsafe { LLVMOrcCreateNewThreadSafeModule(m, self.tsctx) };
-        assert!(!tm.is_null());
+        // 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.
+        let tm = unsafe { LLVMOrcCreateNewThreadSafeModule(m, self.tsctx) };
+        assert!(!tm.is_null());
 
-        tm
+        tm
     }
 
-    /// Dump LLVM IR emitted into the Module to stdout.
-    pub fn dump(&self) {
-        unsafe { LLVMDumpModule(self.module) };
+    /// Dump LLVM IR emitted into the Module to stdout.
+    pub fn dump(&self) {
+        unsafe { LLVMDumpModule(self.module) };
     }
 
-    /// Get a type reference representing a `f64` float.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn type_f64(&self) -> Type<'llvm> {
-        let type_ref = unsafe { LLVMDoubleTypeInContext(self.ctx) };
-        Type::new(type_ref)
+    /// Get a type reference representing a `f64` float.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn type_f64(&self) -> Type<'llvm> {
+        let type_ref = unsafe { LLVMDoubleTypeInContext(self.ctx) };
+        Type::new(type_ref)
     }
 
-    /// Get a type reference representing a `fn(args) -> ret` function.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn type_fn(&'llvm self, args: &mut [Type<'llvm>], ret: Type<'llvm>) -> Type<'llvm> {
-        let type_ref = unsafe {
-            LLVMFunctionType(
-                ret,
-                args.as_mut_ptr(),
-                args.len() as libc::c_uint,
-                0, /* IsVarArg */
-            )
+    /// Get a type reference representing a `fn(args) -> ret` function.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn type_fn(&'llvm self, args: &mut [Type<'llvm>], ret: Type<'llvm>) -> Type<'llvm> {
+        let type_ref = unsafe {
+            LLVMFunctionType(
+                ret,
+                args.as_mut_ptr(),
+                args.len() as libc::c_uint,
+                0, /* IsVarArg */
+            )
         };
-        Type::new(type_ref)
+        Type::new(type_ref)
     }
 
-    /// 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`].
-    pub fn add_fn(&'llvm self, name: &str, fn_type: Type<'llvm>) -> FnValue<'llvm> {
+    /// 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`].
+    pub fn add_fn(&'llvm self, name: &str, fn_type: Type<'llvm>) -> FnValue<'llvm> {
         debug_assert_eq!(
-            fn_type.kind(),
-            LLVMTypeKind::LLVMFunctionTypeKind,
-            "Expected a function type when adding a function!"
-        );
+            fn_type.kind(),
+            LLVMTypeKind::LLVMFunctionTypeKind,
+            "Expected a function type when adding a function!"
+        );
 
-        let name = SmallCStr::try_from(name)
-            .expect("Failed to convert 'name' argument to small C string!");
+        let name = SmallCStr::try_from(name)
+            .expect("Failed to convert 'name' argument to small C string!");
 
-        let value_ref = unsafe { LLVMAddFunction(self.module, name.as_ptr(), fn_type.type_ref()) };
-        FnValue::new(value_ref)
+        let value_ref = unsafe { LLVMAddFunction(self.module, name.as_ptr(), fn_type.type_ref()) };
+        FnValue::new(value_ref)
     }
 
-    /// 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`].
-    pub fn get_fn(&'llvm self, name: &str) -> Option<FnValue<'llvm>> {
-        let name = SmallCStr::try_from(name)
-            .expect("Failed to convert 'name' argument to small C string!");
+    /// 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`].
+    pub fn get_fn(&'llvm self, name: &str) -> Option<FnValue<'llvm>> {
+        let name = SmallCStr::try_from(name)
+            .expect("Failed to convert 'name' argument to small C string!");
 
-        let value_ref = unsafe { LLVMGetNamedFunction(self.module, name.as_ptr()) };
+        let value_ref = unsafe { LLVMGetNamedFunction(self.module, name.as_ptr()) };
 
-        (!value_ref.is_null()).then(|| FnValue::new(value_ref))
+        (!value_ref.is_null()).then(|| FnValue::new(value_ref))
     }
 
-    /// 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.
-    pub fn append_basic_block(&'llvm self, fn_value: FnValue<'llvm>) -> BasicBlock<'llvm> {
-        let block = unsafe {
-            LLVMAppendBasicBlockInContext(
-                self.ctx,
-                fn_value.value_ref(),
-                b"block\0".as_ptr().cast(),
+    /// 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.
+    pub fn append_basic_block(&'llvm self, fn_value: FnValue<'llvm>) -> BasicBlock<'llvm> {
+        let block = unsafe {
+            LLVMAppendBasicBlockInContext(
+                self.ctx,
+                fn_value.value_ref(),
+                b"block\0".as_ptr().cast(),
             )
         };
-        assert!(!block.is_null());
+        assert!(!block.is_null());
 
-        BasicBlock::new(block)
+        BasicBlock::new(block)
     }
 
-    /// 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.
-    pub fn create_basic_block(&self) -> BasicBlock<'llvm> {
-        let block = unsafe { LLVMCreateBasicBlockInContext(self.ctx, b"block\0".as_ptr().cast()) };
-        assert!(!block.is_null());
+    /// 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.
+    pub fn create_basic_block(&self) -> BasicBlock<'llvm> {
+        let block = unsafe { LLVMCreateBasicBlockInContext(self.ctx, b"block\0".as_ptr().cast()) };
+        assert!(!block.is_null());
 
-        BasicBlock::new(block)
+        BasicBlock::new(block)
     }
 }
 
-impl Drop for Module {
-    fn drop(&mut self) {
-        unsafe {
-            // In case we turned the module into a ThreadSafeModule, we must not dispose the module
-            // reference because ThreadSafeModule took ownership!
-            if !self.module.is_null() {
-                LLVMDisposeModule(self.module);
+impl Drop for Module {
+    fn drop(&mut self) {
+        unsafe {
+            // In case we turned the module into a ThreadSafeModule, we must not dispose the module
+            // reference because ThreadSafeModule took ownership!
+            if !self.module.is_null() {
+                LLVMDisposeModule(self.module);
             }
 
-            // Dispose ThreadSafeContext reference (dec ref count) in any case.
-            LLVMOrcDisposeThreadSafeContext(self.tsctx);
+            // Dispose ThreadSafeContext reference (dec ref count) in any case.
+            LLVMOrcDisposeThreadSafeContext(self.tsctx);
         }
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -pass_manager.rs - source -
1
+pass_manager.rs - source
1
 2
 3
 4
@@ -79,81 +73,80 @@
 73
 74
 75
-
use llvm_sys::{
-    core::{
-        LLVMCreateFunctionPassManagerForModule, LLVMDisposePassManager,
-        LLVMInitializeFunctionPassManager, LLVMRunFunctionPassManager,
+
use llvm_sys::{
+    core::{
+        LLVMCreateFunctionPassManagerForModule, LLVMDisposePassManager,
+        LLVMInitializeFunctionPassManager, LLVMRunFunctionPassManager,
     },
-    prelude::LLVMPassManagerRef,
-    transforms::{
-        instcombine::LLVMAddInstructionCombiningPass,
-        scalar::{LLVMAddCFGSimplificationPass, LLVMAddNewGVNPass, LLVMAddReassociatePass},
+    prelude::LLVMPassManagerRef,
+    transforms::{
+        instcombine::LLVMAddInstructionCombiningPass,
+        scalar::{LLVMAddCFGSimplificationPass, LLVMAddNewGVNPass, LLVMAddReassociatePass},
     },
 };
 
-use std::marker::PhantomData;
+use std::marker::PhantomData;
 
-use super::{FnValue, Module};
+use super::{FnValue, Module};
 
-/// Wrapper for a LLVM Function PassManager (legacy).
-pub struct FunctionPassManager<'llvm> {
-    fpm: LLVMPassManagerRef,
-    _ctx: PhantomData<&'llvm ()>,
+/// Wrapper for a LLVM Function PassManager (legacy).
+pub struct FunctionPassManager<'llvm> {
+    fpm: LLVMPassManagerRef,
+    _ctx: PhantomData<&'llvm ()>,
 }
 
-impl<'llvm> FunctionPassManager<'llvm> {
-    /// 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).
-    pub fn with_ctx(module: &'llvm Module) -> FunctionPassManager<'llvm> {
-        let fpm = unsafe {
-            // Borrows module reference.
-            LLVMCreateFunctionPassManagerForModule(module.module())
+impl<'llvm> FunctionPassManager<'llvm> {
+    /// 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).
+    pub fn with_ctx(module: &'llvm Module) -> FunctionPassManager<'llvm> {
+        let fpm = unsafe {
+            // Borrows module reference.
+            LLVMCreateFunctionPassManagerForModule(module.module())
         };
-        assert!(!fpm.is_null());
+        assert!(!fpm.is_null());
 
-        unsafe {
-            // Do simple "peephole" optimizations and bit-twiddling optzns.
-            LLVMAddInstructionCombiningPass(fpm);
-            // Reassociate expressions.
-            LLVMAddReassociatePass(fpm);
-            // Eliminate Common SubExpressions.
-            LLVMAddNewGVNPass(fpm);
-            // Simplify the control flow graph (deleting unreachable blocks, etc).
-            LLVMAddCFGSimplificationPass(fpm);
+        unsafe {
+            // Do simple "peephole" optimizations and bit-twiddling optzns.
+            LLVMAddInstructionCombiningPass(fpm);
+            // Reassociate expressions.
+            LLVMAddReassociatePass(fpm);
+            // Eliminate Common SubExpressions.
+            LLVMAddNewGVNPass(fpm);
+            // Simplify the control flow graph (deleting unreachable blocks, etc).
+            LLVMAddCFGSimplificationPass(fpm);
 
-            let fail = LLVMInitializeFunctionPassManager(fpm);
-            assert_eq!(fail, 0);
+            let fail = LLVMInitializeFunctionPassManager(fpm);
+            assert_eq!(fail, 0);
         }
 
-        FunctionPassManager {
-            fpm,
-            _ctx: PhantomData,
+        FunctionPassManager {
+            fpm,
+            _ctx: PhantomData,
         }
     }
 
-    /// Run the optimization passes registered with the Function PassManager on the function
-    /// referenced by `fn_value`.
-    pub fn run(&'llvm self, fn_value: FnValue<'llvm>) {
-        unsafe {
-            // Returns 1 if any of the passes modified the function, false otherwise.
-            LLVMRunFunctionPassManager(self.fpm, fn_value.value_ref());
+    /// Run the optimization passes registered with the Function PassManager on the function
+    /// referenced by `fn_value`.
+    pub fn run(&'llvm self, fn_value: FnValue<'llvm>) {
+        unsafe {
+            // Returns 1 if any of the passes modified the function, false otherwise.
+            LLVMRunFunctionPassManager(self.fpm, fn_value.value_ref());
         }
     }
 }
 
-impl Drop for FunctionPassManager<'_> {
-    fn drop(&mut self) {
-        unsafe {
-            LLVMDisposePassManager(self.fpm);
+impl Drop for FunctionPassManager<'_> {
+    fn drop(&mut self) {
+        unsafe {
+            LLVMDisposePassManager(self.fpm);
         }
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -type_.rs - source -
1
+type_.rs - source
1
 2
 3
 4
@@ -62,64 +56,63 @@
 56
 57
 58
-
use llvm_sys::{
-    core::{LLVMConstReal, LLVMDumpType, LLVMGetTypeKind},
-    prelude::LLVMTypeRef,
-    LLVMTypeKind,
+
use llvm_sys::{
+    core::{LLVMConstReal, LLVMDumpType, LLVMGetTypeKind},
+    prelude::LLVMTypeRef,
+    LLVMTypeKind,
 };
 
-use std::marker::PhantomData;
+use std::marker::PhantomData;
 
-use super::Value;
+use super::Value;
 
-/// Wrapper for a LLVM Type Reference.
-#[derive(Copy, Clone)]
-#[repr(transparent)]
-pub struct Type<'llvm>(LLVMTypeRef, PhantomData<&'llvm ()>);
+/// Wrapper for a LLVM Type Reference.
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+pub struct Type<'llvm>(LLVMTypeRef, PhantomData<&'llvm ()>);
 
-impl<'llvm> Type<'llvm> {
-    /// Create a new Type instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if `type_ref` is a null pointer.
-    pub(super) fn new(type_ref: LLVMTypeRef) -> Self {
-        assert!(!type_ref.is_null());
-        Type(type_ref, PhantomData)
+impl<'llvm> Type<'llvm> {
+    /// Create a new Type instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `type_ref` is a null pointer.
+    pub(super) fn new(type_ref: LLVMTypeRef) -> Self {
+        assert!(!type_ref.is_null());
+        Type(type_ref, PhantomData)
     }
 
-    /// Get the raw LLVM type reference.
-    #[inline]
-    pub(super) fn type_ref(&self) -> LLVMTypeRef {
-        self.0
-    }
+    /// Get the raw LLVM type reference.
+    #[inline]
+    pub(super) fn type_ref(&self) -> LLVMTypeRef {
+        self.0
+    }
 
-    /// Get the LLVM type kind for the given type reference.
-    pub(super) fn kind(&self) -> LLVMTypeKind {
-        unsafe { LLVMGetTypeKind(self.type_ref()) }
+    /// Get the LLVM type kind for the given type reference.
+    pub(super) fn kind(&self) -> LLVMTypeKind {
+        unsafe { LLVMGetTypeKind(self.type_ref()) }
     }
 
-    /// Dump the LLVM Type to stdout.
-    pub fn dump(&self) {
-        unsafe { LLVMDumpType(self.type_ref()) };
+    /// Dump the LLVM Type to stdout.
+    pub fn dump(&self) {
+        unsafe { LLVMDumpType(self.type_ref()) };
     }
 
-    /// Get a value reference representing the const `f64` value.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn const_f64(self, n: f64) -> Value<'llvm> {
+    /// Get a value reference representing the const `f64` value.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn const_f64(self, n: f64) -> Value<'llvm> {
         debug_assert_eq!(
-            self.kind(),
-            LLVMTypeKind::LLVMDoubleTypeKind,
-            "Expected a double type when creating const f64 value!"
-        );
+            self.kind(),
+            LLVMTypeKind::LLVMDoubleTypeKind,
+            "Expected a double type when creating const f64 value!"
+        );
 
-        let value_ref = unsafe { LLVMConstReal(self.type_ref(), n) };
-        Value::new(value_ref)
+        let value_ref = unsafe { LLVMConstReal(self.type_ref(), n) };
+        Value::new(value_ref)
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -value.rs - source -
1
+value.rs - source
1
 2
 3
 4
@@ -241,243 +235,242 @@
 235
 236
 237
-
#![allow(unused)]
-
-use llvm_sys::{
-    analysis::{LLVMVerifierFailureAction, LLVMVerifyFunction},
-    core::{
-        LLVMAddIncoming, LLVMAppendExistingBasicBlock, LLVMCountBasicBlocks, LLVMCountParams,
-        LLVMDumpValue, LLVMGetParam, LLVMGetReturnType, LLVMGetValueKind, LLVMGetValueName2,
-        LLVMIsAFunction, LLVMIsAPHINode, LLVMSetValueName2, LLVMTypeOf,
+
#![allow(unused)]
+
+use llvm_sys::{
+    analysis::{LLVMVerifierFailureAction, LLVMVerifyFunction},
+    core::{
+        LLVMAddIncoming, LLVMAppendExistingBasicBlock, LLVMCountBasicBlocks, LLVMCountParams,
+        LLVMDumpValue, LLVMGetParam, LLVMGetReturnType, LLVMGetValueKind, LLVMGetValueName2,
+        LLVMIsAFunction, LLVMIsAPHINode, LLVMSetValueName2, LLVMTypeOf,
     },
-    prelude::LLVMValueRef,
-    LLVMTypeKind, LLVMValueKind,
+    prelude::LLVMValueRef,
+    LLVMTypeKind, LLVMValueKind,
 };
 
-use std::ffi::CStr;
-use std::marker::PhantomData;
-use std::ops::Deref;
-
-use super::BasicBlock;
-use super::Type;
-
-/// Wrapper for a LLVM Value Reference.
-#[derive(Copy, Clone)]
-#[repr(transparent)]
-pub struct Value<'llvm>(LLVMValueRef, PhantomData<&'llvm ()>);
-
-impl<'llvm> Value<'llvm> {
-    /// Create a new Value instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if `value_ref` is a null pointer.
-    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
-        assert!(!value_ref.is_null());
-        Value(value_ref, PhantomData)
+use std::ffi::CStr;
+use std::marker::PhantomData;
+use std::ops::Deref;
+
+use super::BasicBlock;
+use super::Type;
+
+/// Wrapper for a LLVM Value Reference.
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+pub struct Value<'llvm>(LLVMValueRef, PhantomData<&'llvm ()>);
+
+impl<'llvm> Value<'llvm> {
+    /// Create a new Value instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `value_ref` is a null pointer.
+    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
+        assert!(!value_ref.is_null());
+        Value(value_ref, PhantomData)
     }
 
-    /// Get the raw LLVM value reference.
-    #[inline]
-    pub(super) fn value_ref(&self) -> LLVMValueRef {
-        self.0
-    }
+    /// Get the raw LLVM value reference.
+    #[inline]
+    pub(super) fn value_ref(&self) -> LLVMValueRef {
+        self.0
+    }
 
-    /// Get the LLVM value kind for the given value reference.
-    pub(super) fn kind(&self) -> LLVMValueKind {
-        unsafe { LLVMGetValueKind(self.value_ref()) }
+    /// Get the LLVM value kind for the given value reference.
+    pub(super) fn kind(&self) -> LLVMValueKind {
+        unsafe { LLVMGetValueKind(self.value_ref()) }
     }
 
-    /// Check if value is `function` type.
-    pub(super) fn is_function(&self) -> bool {
-        let cast = unsafe { LLVMIsAFunction(self.value_ref()) };
-        !cast.is_null()
+    /// Check if value is `function` type.
+    pub(super) fn is_function(&self) -> bool {
+        let cast = unsafe { LLVMIsAFunction(self.value_ref()) };
+        !cast.is_null()
     }
 
-    /// Check if value is `phinode` type.
-    pub(super) fn is_phinode(&self) -> bool {
-        let cast = unsafe { LLVMIsAPHINode(self.value_ref()) };
-        !cast.is_null()
+    /// Check if value is `phinode` type.
+    pub(super) fn is_phinode(&self) -> bool {
+        let cast = unsafe { LLVMIsAPHINode(self.value_ref()) };
+        !cast.is_null()
     }
 
-    /// Dump the LLVM Value to stdout.
-    pub fn dump(&self) {
-        unsafe { LLVMDumpValue(self.value_ref()) };
+    /// Dump the LLVM Value to stdout.
+    pub fn dump(&self) {
+        unsafe { LLVMDumpValue(self.value_ref()) };
     }
 
-    /// Get a type reference representing for the given value reference.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn type_of(&self) -> Type<'llvm> {
-        let type_ref = unsafe { LLVMTypeOf(self.value_ref()) };
-        Type::new(type_ref)
+    /// Get a type reference representing for the given value reference.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn type_of(&self) -> Type<'llvm> {
+        let type_ref = unsafe { LLVMTypeOf(self.value_ref()) };
+        Type::new(type_ref)
     }
 
-    /// Set the name for the given value reference.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn set_name(&self, name: &str) {
-        unsafe { LLVMSetValueName2(self.value_ref(), name.as_ptr().cast(), name.len()) };
+    /// Set the name for the given value reference.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn set_name(&self, name: &str) {
+        unsafe { LLVMSetValueName2(self.value_ref(), name.as_ptr().cast(), name.len()) };
     }
 
-    /// Get the name for the given value reference.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn get_name(&self) -> &'llvm str {
-        let name = unsafe {
-            let mut len: libc::size_t = 0;
-            let name = LLVMGetValueName2(self.0, &mut len as _);
-            assert!(!name.is_null());
-
-            CStr::from_ptr(name)
+    /// Get the name for the given value reference.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn get_name(&self) -> &'llvm str {
+        let name = unsafe {
+            let mut len: libc::size_t = 0;
+            let name = LLVMGetValueName2(self.0, &mut len as _);
+            assert!(!name.is_null());
+
+            CStr::from_ptr(name)
         };
 
-        // TODO: Does this string live for the time of the LLVM context?!
-        name.to_str()
-            .expect("Expected valid UTF8 string from LLVM API")
+        // TODO: Does this string live for the time of the LLVM context?!
+        name.to_str()
+            .expect("Expected valid UTF8 string from LLVM API")
     }
 
-    /// Check if value is of `f64` type.
-    pub fn is_f64(&self) -> bool {
-        self.type_of().kind() == LLVMTypeKind::LLVMDoubleTypeKind
+    /// Check if value is of `f64` type.
+    pub fn is_f64(&self) -> bool {
+        self.type_of().kind() == LLVMTypeKind::LLVMDoubleTypeKind
     }
 
-    /// Check if value is of integer type.
-    pub fn is_int(&self) -> bool {
-        self.type_of().kind() == LLVMTypeKind::LLVMIntegerTypeKind
+    /// Check if value is of integer type.
+    pub fn is_int(&self) -> bool {
+        self.type_of().kind() == LLVMTypeKind::LLVMIntegerTypeKind
     }
 }
 
-/// Wrapper for a LLVM Value Reference specialized for contexts where function values are needed.
-#[derive(Copy, Clone)]
-#[repr(transparent)]
-pub struct FnValue<'llvm>(Value<'llvm>);
+/// Wrapper for a LLVM Value Reference specialized for contexts where function values are needed.
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+pub struct FnValue<'llvm>(Value<'llvm>);
 
-impl<'llvm> Deref for FnValue<'llvm> {
-    type Target = Value<'llvm>;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
+impl<'llvm> Deref for FnValue<'llvm> {
+    type Target = Value<'llvm>;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
 }
 
-impl<'llvm> FnValue<'llvm> {
-    /// Create a new FnValue instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if `value_ref` is a null pointer.
-    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
-        let value = Value::new(value_ref);
+impl<'llvm> FnValue<'llvm> {
+    /// Create a new FnValue instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `value_ref` is a null pointer.
+    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
+        let value = Value::new(value_ref);
         debug_assert!(
-            value.is_function(),
-            "Expected a fn value when constructing FnValue!"
-        );
+            value.is_function(),
+            "Expected a fn value when constructing FnValue!"
+        );
 
-        FnValue(value)
+        FnValue(value)
     }
 
-    /// Get a type reference representing the return value of the given function value.
-    ///
-    /// # Panics
-    ///
-    /// Panics if LLVM API returns a `null` pointer.
-    pub fn ret_type(&self) -> Type<'llvm> {
-        let type_ref = unsafe { LLVMGetReturnType(LLVMTypeOf(self.value_ref())) };
-        Type::new(type_ref)
+    /// Get a type reference representing the return value of the given function value.
+    ///
+    /// # Panics
+    ///
+    /// Panics if LLVM API returns a `null` pointer.
+    pub fn ret_type(&self) -> Type<'llvm> {
+        let type_ref = unsafe { LLVMGetReturnType(LLVMTypeOf(self.value_ref())) };
+        Type::new(type_ref)
     }
 
-    /// Get the number of function arguments for the given function value.
-    pub fn args(&self) -> usize {
-        unsafe { LLVMCountParams(self.value_ref()) as usize }
+    /// Get the number of function arguments for the given function value.
+    pub fn args(&self) -> usize {
+        unsafe { LLVMCountParams(self.value_ref()) as usize }
     }
 
-    /// 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.
-    pub fn arg(&self, idx: usize) -> Value<'llvm> {
-        assert!(idx < self.args());
+    /// 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.
+    pub fn arg(&self, idx: usize) -> Value<'llvm> {
+        assert!(idx < self.args());
 
-        let value_ref = unsafe { LLVMGetParam(self.value_ref(), idx as libc::c_uint) };
-        Value::new(value_ref)
+        let value_ref = unsafe { LLVMGetParam(self.value_ref(), idx as libc::c_uint) };
+        Value::new(value_ref)
     }
 
-    /// Get the number of Basic Blocks for the given function value.
-    pub fn basic_blocks(&self) -> usize {
-        unsafe { LLVMCountBasicBlocks(self.value_ref()) as usize }
+    /// Get the number of Basic Blocks for the given function value.
+    pub fn basic_blocks(&self) -> usize {
+        unsafe { LLVMCountBasicBlocks(self.value_ref()) as usize }
     }
 
-    /// Append a Basic Block to the end of the function value.
-    pub fn append_basic_block(&self, bb: BasicBlock<'llvm>) {
-        unsafe {
-            LLVMAppendExistingBasicBlock(self.value_ref(), bb.bb_ref());
+    /// Append a Basic Block to the end of the function value.
+    pub fn append_basic_block(&self, bb: BasicBlock<'llvm>) {
+        unsafe {
+            LLVMAppendExistingBasicBlock(self.value_ref(), bb.bb_ref());
         }
     }
 
-    /// Verify that the given function is valid.
-    pub fn verify(&self) -> bool {
-        unsafe {
-            LLVMVerifyFunction(
-                self.value_ref(),
-                LLVMVerifierFailureAction::LLVMPrintMessageAction,
-            ) == 0
-        }
+    /// Verify that the given function is valid.
+    pub fn verify(&self) -> bool {
+        unsafe {
+            LLVMVerifyFunction(
+                self.value_ref(),
+                LLVMVerifierFailureAction::LLVMPrintMessageAction,
+            ) == 0
+        }
     }
 }
 
-/// Wrapper for a LLVM Value Reference specialized for contexts where phi values are needed.
-#[derive(Copy, Clone)]
-#[repr(transparent)]
-pub struct PhiValue<'llvm>(Value<'llvm>);
+/// Wrapper for a LLVM Value Reference specialized for contexts where phi values are needed.
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+pub struct PhiValue<'llvm>(Value<'llvm>);
 
-impl<'llvm> Deref for PhiValue<'llvm> {
-    type Target = Value<'llvm>;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
+impl<'llvm> Deref for PhiValue<'llvm> {
+    type Target = Value<'llvm>;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
 }
 
-impl<'llvm> PhiValue<'llvm> {
-    /// Create a new PhiValue instance.
-    ///
-    /// # Panics
-    ///
-    /// Panics if `value_ref` is a null pointer.
-    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
-        let value = Value::new(value_ref);
+impl<'llvm> PhiValue<'llvm> {
+    /// Create a new PhiValue instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `value_ref` is a null pointer.
+    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
+        let value = Value::new(value_ref);
         debug_assert!(
-            value.is_phinode(),
-            "Expected a phinode value when constructing PhiValue!"
-        );
+            value.is_phinode(),
+            "Expected a phinode value when constructing PhiValue!"
+        );
 
-        PhiValue(value)
+        PhiValue(value)
     }
 
-    /// Add an incoming value to the end of a PHI list.
-    pub fn add_incoming(&self, ival: Value<'llvm>, ibb: BasicBlock<'llvm>) {
+    /// Add an incoming value to the end of a PHI list.
+    pub fn add_incoming(&self, ival: Value<'llvm>, ibb: BasicBlock<'llvm>) {
         debug_assert_eq!(
-            ival.type_of().kind(),
-            self.type_of().kind(),
-            "Type of incoming phi value must be the same as the type used to build the phi node."
-        );
-
-        unsafe {
-            LLVMAddIncoming(
-                self.value_ref(),
-                &mut ival.value_ref() as _,
-                &mut ibb.bb_ref() as _,
+            ival.type_of().kind(),
+            self.type_of().kind(),
+            "Type of incoming phi value must be the same as the type used to build the phi node."
+        );
+
+        unsafe {
+            LLVMAddIncoming(
+                self.value_ref(),
+                &mut ival.value_ref() as _,
+                &mut ibb.bb_ref() as _,
                 1,
             );
         }
     }
 }
 
-
- \ No newline at end of file +
\ 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 @@ -parser.rs - source -
1
+parser.rs - source
1
 2
 3
 4
@@ -658,660 +652,659 @@
 652
 653
 654
-
use crate::lexer::{Lexer, Token};
+
use crate::lexer::{Lexer, Token};
 
-#[derive(Debug, PartialEq)]
-pub enum ExprAST {
-    /// Number - Expression class for numeric literals like "1.0".
-    Number(f64),
+#[derive(Debug, PartialEq)]
+pub enum ExprAST {
+    /// Number - Expression class for numeric literals like "1.0".
+    Number(f64),
 
-    /// Variable - Expression class for referencing a variable, like "a".
-    Variable(String),
+    /// Variable - Expression class for referencing a variable, like "a".
+    Variable(String),
 
-    /// Binary - Expression class for a binary operator.
-    Binary(char, Box<ExprAST>, Box<ExprAST>),
+    /// Binary - Expression class for a binary operator.
+    Binary(char, Box<ExprAST>, Box<ExprAST>),
 
-    /// Call - Expression class for function calls.
-    Call(String, Vec<ExprAST>),
+    /// Call - Expression class for function calls.
+    Call(String, Vec<ExprAST>),
 
-    /// If - Expression class for if/then/else.
-    If {
-        cond: Box<ExprAST>,
-        then: Box<ExprAST>,
-        else_: Box<ExprAST>,
+    /// If - Expression class for if/then/else.
+    If {
+        cond: Box<ExprAST>,
+        then: Box<ExprAST>,
+        else_: Box<ExprAST>,
     },
 
-    /// ForExprAST - Expression class for for/in.
-    For {
-        var: String,
-        start: Box<ExprAST>,
-        end: Box<ExprAST>,
-        step: Option<Box<ExprAST>>,
-        body: Box<ExprAST>,
+    /// ForExprAST - Expression class for for/in.
+    For {
+        var: String,
+        start: Box<ExprAST>,
+        end: Box<ExprAST>,
+        step: Option<Box<ExprAST>>,
+        body: Box<ExprAST>,
     },
 }
 
-/// 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).
-#[derive(Debug, PartialEq, Clone)]
-pub struct PrototypeAST(pub String, pub Vec<String>);
+/// 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).
+#[derive(Debug, PartialEq, Clone)]
+pub struct PrototypeAST(pub String, pub Vec<String>);
 
-/// FunctionAST - This class represents a function definition itself.
-#[derive(Debug, PartialEq)]
-pub struct FunctionAST(pub PrototypeAST, pub ExprAST);
+/// FunctionAST - This class represents a function definition itself.
+#[derive(Debug, PartialEq)]
+pub struct FunctionAST(pub PrototypeAST, pub ExprAST);
 
-/// Parse result with String as Error type (to be compliant with tutorial).
-type ParseResult<T> = Result<T, String>;
+/// Parse result with String as Error type (to be compliant with tutorial).
+type ParseResult<T> = Result<T, String>;
 
-/// Parser for the `kaleidoscope` language.
-pub struct Parser<I>
-where
-    I: Iterator<Item = char>,
+/// Parser for the `kaleidoscope` language.
+pub struct Parser<I>
+where
+    I: Iterator<Item = char>,
 {
-    lexer: Lexer<I>,
-    cur_tok: Option<Token>,
+    lexer: Lexer<I>,
+    cur_tok: Option<Token>,
 }
 
-impl<I> Parser<I>
-where
-    I: Iterator<Item = char>,
+impl<I> Parser<I>
+where
+    I: Iterator<Item = char>,
 {
-    pub fn new(lexer: Lexer<I>) -> Self {
-        Parser {
-            lexer,
-            cur_tok: None,
+    pub fn new(lexer: Lexer<I>) -> Self {
+        Parser {
+            lexer,
+            cur_tok: None,
         }
     }
 
-    // -----------------------
-    //   Simple Token Buffer
-    // -----------------------
+    // -----------------------
+    //   Simple Token Buffer
+    // -----------------------
 
-    /// Implement the global variable `int CurTok;` from the tutorial.
-    ///
-    /// # Panics
-    /// Panics if the parser doesn't have a current token.
-    pub fn cur_tok(&self) -> &Token {
-        self.cur_tok.as_ref().expect("Parser: Expected cur_token!")
+    /// Implement the global variable `int CurTok;` from the tutorial.
+    ///
+    /// # Panics
+    /// Panics if the parser doesn't have a current token.
+    pub fn cur_tok(&self) -> &Token {
+        self.cur_tok.as_ref().expect("Parser: Expected cur_token!")
     }
 
-    /// Advance the `cur_tok` by getting the next token from the lexer.
-    ///
-    /// Implement the fucntion `int getNextToken();` from the tutorial.
-    pub fn get_next_token(&mut self) {
-        self.cur_tok = Some(self.lexer.gettok());
+    /// Advance the `cur_tok` by getting the next token from the lexer.
+    ///
+    /// Implement the fucntion `int getNextToken();` from the tutorial.
+    pub fn get_next_token(&mut self) {
+        self.cur_tok = Some(self.lexer.gettok());
     }
 
-    // ----------------------------
-    //   Basic Expression Parsing
-    // ----------------------------
-
-    /// numberexpr ::= number
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseNumberExpr();` from the tutorial.
-    fn parse_num_expr(&mut self) -> ParseResult<ExprAST> {
-        match *self.cur_tok() {
-            Token::Number(num) => {
-                // Consume the number token.
-                self.get_next_token();
-                Ok(ExprAST::Number(num))
+    // ----------------------------
+    //   Basic Expression Parsing
+    // ----------------------------
+
+    /// numberexpr ::= number
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseNumberExpr();` from the tutorial.
+    fn parse_num_expr(&mut self) -> ParseResult<ExprAST> {
+        match *self.cur_tok() {
+            Token::Number(num) => {
+                // Consume the number token.
+                self.get_next_token();
+                Ok(ExprAST::Number(num))
             }
-            _ => unreachable!(),
+            _ => unreachable!(),
         }
     }
 
-    /// parenexpr ::= '(' expression ')'
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseParenExpr();` from the tutorial.
-    fn parse_paren_expr(&mut self) -> ParseResult<ExprAST> {
-        // Eat '(' token.
-        assert_eq!(*self.cur_tok(), Token::Char('('));
-        self.get_next_token();
-
-        let v = self.parse_expression()?;
-
-        if *self.cur_tok() == Token::Char(')') {
-            // Eat ')' token.
-            self.get_next_token();
-            Ok(v)
-        } else {
-            Err("expected ')'".into())
+    /// parenexpr ::= '(' expression ')'
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseParenExpr();` from the tutorial.
+    fn parse_paren_expr(&mut self) -> ParseResult<ExprAST> {
+        // Eat '(' token.
+        assert_eq!(*self.cur_tok(), Token::Char('('));
+        self.get_next_token();
+
+        let v = self.parse_expression()?;
+
+        if *self.cur_tok() == Token::Char(')') {
+            // Eat ')' token.
+            self.get_next_token();
+            Ok(v)
+        } else {
+            Err("expected ')'".into())
         }
     }
 
-    /// identifierexpr
-    ///   ::= identifier
-    ///   ::= identifier '(' expression* ')'
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseIdentifierExpr();` from the tutorial.
-    fn parse_identifier_expr(&mut self) -> ParseResult<ExprAST> {
-        let id_name = match self.cur_tok.take() {
-            Some(Token::Identifier(id)) => {
-                // Consume identifier.
-                self.get_next_token();
-                id
+    /// identifierexpr
+    ///   ::= identifier
+    ///   ::= identifier '(' expression* ')'
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseIdentifierExpr();` from the tutorial.
+    fn parse_identifier_expr(&mut self) -> ParseResult<ExprAST> {
+        let id_name = match self.cur_tok.take() {
+            Some(Token::Identifier(id)) => {
+                // Consume identifier.
+                self.get_next_token();
+                id
             }
-            _ => unreachable!(),
+            _ => unreachable!(),
         };
 
-        if *self.cur_tok() != Token::Char('(') {
-            // Simple variable reference.
-            Ok(ExprAST::Variable(id_name))
-        } else {
-            // Call.
+        if *self.cur_tok() != Token::Char('(') {
+            // Simple variable reference.
+            Ok(ExprAST::Variable(id_name))
+        } else {
+            // Call.
 
-            // Eat '(' token.
-            self.get_next_token();
+            // Eat '(' token.
+            self.get_next_token();
 
-            let mut args: Vec<ExprAST> = Vec::new();
+            let mut args: Vec<ExprAST> = Vec::new();
 
-            // If there are arguments collect them.
-            if *self.cur_tok() != Token::Char(')') {
-                loop {
-                    let arg = self.parse_expression()?;
-                    args.push(arg);
+            // If there are arguments collect them.
+            if *self.cur_tok() != Token::Char(')') {
+                loop {
+                    let arg = self.parse_expression()?;
+                    args.push(arg);
 
-                    if *self.cur_tok() == Token::Char(')') {
+                    if *self.cur_tok() == Token::Char(')') {
                         break;
                     }
 
-                    if *self.cur_tok() != Token::Char(',') {
-                        return Err("Expected ')' or ',' in argument list".into());
+                    if *self.cur_tok() != Token::Char(',') {
+                        return Err("Expected ')' or ',' in argument list".into());
                     }
 
-                    self.get_next_token();
+                    self.get_next_token();
                 }
             }
 
-            assert_eq!(*self.cur_tok(), Token::Char(')'));
-            // Eat ')' token.
-            self.get_next_token();
+            assert_eq!(*self.cur_tok(), Token::Char(')'));
+            // Eat ')' token.
+            self.get_next_token();
 
-            Ok(ExprAST::Call(id_name, args))
+            Ok(ExprAST::Call(id_name, args))
         }
     }
 
-    /// ifexpr ::= 'if' expression 'then' expression 'else' expression
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseIfExpr();` from the tutorial.
-    fn parse_if_expr(&mut self) -> ParseResult<ExprAST> {
-        // Consume 'if' token.
-        assert_eq!(*self.cur_tok(), Token::If);
-        self.get_next_token();
+    /// ifexpr ::= 'if' expression 'then' expression 'else' expression
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseIfExpr();` from the tutorial.
+    fn parse_if_expr(&mut self) -> ParseResult<ExprAST> {
+        // Consume 'if' token.
+        assert_eq!(*self.cur_tok(), Token::If);
+        self.get_next_token();
 
-        let cond = self.parse_expression()?;
+        let cond = self.parse_expression()?;
 
-        if *dbg!(self.cur_tok()) != Token::Then {
-            return Err("Expected 'then'".into());
+        if *dbg!(self.cur_tok()) != Token::Then {
+            return Err("Expected 'then'".into());
         }
-        // Consume 'then' token.
-        self.get_next_token();
+        // Consume 'then' token.
+        self.get_next_token();
 
-        let then = self.parse_expression()?;
+        let then = self.parse_expression()?;
 
-        if *self.cur_tok() != Token::Else {
-            return Err("Expected 'else'".into());
+        if *self.cur_tok() != Token::Else {
+            return Err("Expected 'else'".into());
         }
-        // Consume 'else' token.
-        self.get_next_token();
+        // Consume 'else' token.
+        self.get_next_token();
 
-        let else_ = self.parse_expression()?;
+        let else_ = self.parse_expression()?;
 
-        Ok(ExprAST::If {
-            cond: Box::new(cond),
-            then: Box::new(then),
-            else_: Box::new(else_),
+        Ok(ExprAST::If {
+            cond: Box::new(cond),
+            then: Box::new(then),
+            else_: Box::new(else_),
         })
     }
 
-    /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseForExpr();` from the tutorial.
-    fn parse_for_expr(&mut self) -> ParseResult<ExprAST> {
-        // Consume the 'for' token.
-        assert_eq!(*self.cur_tok(), Token::For);
-        self.get_next_token();
-
-        let var = match self
-            .parse_identifier_expr()
-            .map_err(|_| String::from("expected identifier after 'for'"))?
-        {
-            ExprAST::Variable(var) => var,
-            _ => unreachable!(),
+    /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseForExpr();` from the tutorial.
+    fn parse_for_expr(&mut self) -> ParseResult<ExprAST> {
+        // Consume the 'for' token.
+        assert_eq!(*self.cur_tok(), Token::For);
+        self.get_next_token();
+
+        let var = match self
+            .parse_identifier_expr()
+            .map_err(|_| String::from("expected identifier after 'for'"))?
+        {
+            ExprAST::Variable(var) => var,
+            _ => unreachable!(),
         };
 
-        // Consume the '=' token.
-        if *self.cur_tok() != Token::Char('=') {
-            return Err("expected '=' after for".into());
+        // Consume the '=' token.
+        if *self.cur_tok() != Token::Char('=') {
+            return Err("expected '=' after for".into());
         }
-        self.get_next_token();
+        self.get_next_token();
 
-        let start = self.parse_expression()?;
+        let start = self.parse_expression()?;
 
-        // Consume the ',' token.
-        if *self.cur_tok() != Token::Char(',') {
-            return Err("expected ',' after for start value".into());
+        // Consume the ',' token.
+        if *self.cur_tok() != Token::Char(',') {
+            return Err("expected ',' after for start value".into());
         }
-        self.get_next_token();
+        self.get_next_token();
 
-        let end = self.parse_expression()?;
+        let end = self.parse_expression()?;
 
-        let step = if *self.cur_tok() == Token::Char(',') {
-            // Consume the ',' token.
-            self.get_next_token();
+        let step = if *self.cur_tok() == Token::Char(',') {
+            // Consume the ',' token.
+            self.get_next_token();
 
-            Some(self.parse_expression()?)
-        } else {
-            None
-        };
+            Some(self.parse_expression()?)
+        } else {
+            None
+        };
 
-        // Consume the 'in' token.
-        if *self.cur_tok() != Token::In {
-            return Err("expected 'in' after for".into());
+        // Consume the 'in' token.
+        if *self.cur_tok() != Token::In {
+            return Err("expected 'in' after for".into());
         }
-        self.get_next_token();
+        self.get_next_token();
 
-        let body = self.parse_expression()?;
+        let body = self.parse_expression()?;
 
-        Ok(ExprAST::For {
-            var,
-            start: Box::new(start),
-            end: Box::new(end),
-            step: step.map(|s| Box::new(s)),
-            body: Box::new(body),
+        Ok(ExprAST::For {
+            var,
+            start: Box::new(start),
+            end: Box::new(end),
+            step: step.map(|s| Box::new(s)),
+            body: Box::new(body),
         })
     }
 
-    /// primary
-    ///   ::= identifierexpr
-    ///   ::= numberexpr
-    ///   ::= parenexpr
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParsePrimary();` from the tutorial.
-    fn parse_primary(&mut self) -> ParseResult<ExprAST> {
-        match *self.cur_tok() {
-            Token::Identifier(_) => self.parse_identifier_expr(),
-            Token::Number(_) => self.parse_num_expr(),
-            Token::Char('(') => self.parse_paren_expr(),
-            Token::If => self.parse_if_expr(),
-            Token::For => self.parse_for_expr(),
-            _ => Err("unknown token when expecting an expression".into()),
+    /// primary
+    ///   ::= identifierexpr
+    ///   ::= numberexpr
+    ///   ::= parenexpr
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParsePrimary();` from the tutorial.
+    fn parse_primary(&mut self) -> ParseResult<ExprAST> {
+        match *self.cur_tok() {
+            Token::Identifier(_) => self.parse_identifier_expr(),
+            Token::Number(_) => self.parse_num_expr(),
+            Token::Char('(') => self.parse_paren_expr(),
+            Token::If => self.parse_if_expr(),
+            Token::For => self.parse_for_expr(),
+            _ => Err("unknown token when expecting an expression".into()),
         }
     }
 
-    // -----------------------------
-    //   Binary Expression Parsing
-    // -----------------------------
-
-    /// /// expression
-    ///   ::= primary binoprhs
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseExpression();` from the tutorial.
-    fn parse_expression(&mut self) -> ParseResult<ExprAST> {
-        let lhs = self.parse_primary()?;
-        self.parse_bin_op_rhs(0, lhs)
+    // -----------------------------
+    //   Binary Expression Parsing
+    // -----------------------------
+
+    /// /// expression
+    ///   ::= primary binoprhs
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseExpression();` from the tutorial.
+    fn parse_expression(&mut self) -> ParseResult<ExprAST> {
+        let lhs = self.parse_primary()?;
+        self.parse_bin_op_rhs(0, lhs)
     }
 
-    /// binoprhs
-    ///   ::= ('+' primary)*
-    ///
-    /// Implement `std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);` from the tutorial.
-    fn parse_bin_op_rhs(&mut self, expr_prec: isize, mut lhs: ExprAST) -> ParseResult<ExprAST> {
-        loop {
-            let tok_prec = get_tok_precedence(self.cur_tok());
-
-            // Not a binary operator or precedence is too small.
-            if tok_prec < expr_prec {
-                return Ok(lhs);
+    /// binoprhs
+    ///   ::= ('+' primary)*
+    ///
+    /// Implement `std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);` from the tutorial.
+    fn parse_bin_op_rhs(&mut self, expr_prec: isize, mut lhs: ExprAST) -> ParseResult<ExprAST> {
+        loop {
+            let tok_prec = get_tok_precedence(self.cur_tok());
+
+            // Not a binary operator or precedence is too small.
+            if tok_prec < expr_prec {
+                return Ok(lhs);
             }
 
-            let binop = match self.cur_tok.take() {
-                Some(Token::Char(c)) => {
-                    // Eat binary operator.
-                    self.get_next_token();
-                    c
+            let binop = match self.cur_tok.take() {
+                Some(Token::Char(c)) => {
+                    // Eat binary operator.
+                    self.get_next_token();
+                    c
                 }
-                _ => unreachable!(),
+                _ => unreachable!(),
             };
 
-            // 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.
-            let mut rhs = self.parse_primary()?;
-
-            let next_prec = get_tok_precedence(self.cur_tok());
-            if tok_prec < next_prec {
-                // BINOP2 has higher precedence thatn BINOP1, recurse into 'remhs'.
-                rhs = self.parse_bin_op_rhs(tok_prec + 1, rhs)?
-            }
-
-            lhs = ExprAST::Binary(binop, Box::new(lhs), Box::new(rhs));
+            // 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.
+            let mut rhs = self.parse_primary()?;
+
+            let next_prec = get_tok_precedence(self.cur_tok());
+            if tok_prec < next_prec {
+                // BINOP2 has higher precedence thatn BINOP1, recurse into 'remhs'.
+                rhs = self.parse_bin_op_rhs(tok_prec + 1, rhs)?
+            }
+
+            lhs = ExprAST::Binary(binop, Box::new(lhs), Box::new(rhs));
         }
     }
 
-    // --------------------
-    //   Parsing the Rest
-    // --------------------
-
-    /// prototype
-    ///   ::= id '(' id* ')'
-    ///
-    /// Implement `std::unique_ptr<PrototypeAST> ParsePrototype();` from the tutorial.
-    fn parse_prototype(&mut self) -> ParseResult<PrototypeAST> {
-        let id_name = match self.cur_tok.take() {
-            Some(Token::Identifier(id)) => {
-                // Consume the identifier.
-                self.get_next_token();
-                id
+    // --------------------
+    //   Parsing the Rest
+    // --------------------
+
+    /// prototype
+    ///   ::= id '(' id* ')'
+    ///
+    /// Implement `std::unique_ptr<PrototypeAST> ParsePrototype();` from the tutorial.
+    fn parse_prototype(&mut self) -> ParseResult<PrototypeAST> {
+        let id_name = match self.cur_tok.take() {
+            Some(Token::Identifier(id)) => {
+                // Consume the identifier.
+                self.get_next_token();
+                id
             }
-            other => {
-                // Plug back current token.
-                self.cur_tok = other;
-                return Err("Expected function name in prototype".into());
+            other => {
+                // Plug back current token.
+                self.cur_tok = other;
+                return Err("Expected function name in prototype".into());
             }
         };
 
-        if *self.cur_tok() != Token::Char('(') {
-            return Err("Expected '(' in prototype".into());
+        if *self.cur_tok() != Token::Char('(') {
+            return Err("Expected '(' in prototype".into());
         }
 
-        let mut args: Vec<String> = Vec::new();
-        loop {
-            self.get_next_token();
+        let mut args: Vec<String> = Vec::new();
+        loop {
+            self.get_next_token();
 
-            match self.cur_tok.take() {
-                Some(Token::Identifier(arg)) => args.push(arg),
-                Some(Token::Char(',')) => {}
-                other => {
-                    self.cur_tok = other;
+            match self.cur_tok.take() {
+                Some(Token::Identifier(arg)) => args.push(arg),
+                Some(Token::Char(',')) => {}
+                other => {
+                    self.cur_tok = other;
                     break;
                 }
             }
         }
 
-        if *self.cur_tok() != Token::Char(')') {
-            return Err("Expected ')' in prototype".into());
+        if *self.cur_tok() != Token::Char(')') {
+            return Err("Expected ')' in prototype".into());
         }
 
-        // Consume ')'.
-        self.get_next_token();
+        // Consume ')'.
+        self.get_next_token();
 
-        Ok(PrototypeAST(id_name, args))
+        Ok(PrototypeAST(id_name, args))
     }
 
-    /// definition ::= 'def' prototype expression
-    ///
-    /// Implement `std::unique_ptr<FunctionAST> ParseDefinition();` from the tutorial.
-    pub fn parse_definition(&mut self) -> ParseResult<FunctionAST> {
-        // Consume 'def' token.
-        assert_eq!(*self.cur_tok(), Token::Def);
-        self.get_next_token();
+    /// definition ::= 'def' prototype expression
+    ///
+    /// Implement `std::unique_ptr<FunctionAST> ParseDefinition();` from the tutorial.
+    pub fn parse_definition(&mut self) -> ParseResult<FunctionAST> {
+        // Consume 'def' token.
+        assert_eq!(*self.cur_tok(), Token::Def);
+        self.get_next_token();
 
-        let proto = self.parse_prototype()?;
-        let expr = self.parse_expression()?;
+        let proto = self.parse_prototype()?;
+        let expr = self.parse_expression()?;
 
-        Ok(FunctionAST(proto, expr))
+        Ok(FunctionAST(proto, expr))
     }
 
-    /// external ::= 'extern' prototype
-    ///
-    /// Implement `std::unique_ptr<PrototypeAST> ParseExtern();` from the tutorial.
-    pub fn parse_extern(&mut self) -> ParseResult<PrototypeAST> {
-        // Consume 'extern' token.
-        assert_eq!(*self.cur_tok(), Token::Extern);
-        self.get_next_token();
+    /// external ::= 'extern' prototype
+    ///
+    /// Implement `std::unique_ptr<PrototypeAST> ParseExtern();` from the tutorial.
+    pub fn parse_extern(&mut self) -> ParseResult<PrototypeAST> {
+        // Consume 'extern' token.
+        assert_eq!(*self.cur_tok(), Token::Extern);
+        self.get_next_token();
 
-        self.parse_prototype()
+        self.parse_prototype()
     }
 
-    /// toplevelexpr ::= expression
-    ///
-    /// Implement `std::unique_ptr<FunctionAST> ParseTopLevelExpr();` from the tutorial.
-    pub fn parse_top_level_expr(&mut self) -> ParseResult<FunctionAST> {
-        let e = self.parse_expression()?;
-        let proto = PrototypeAST("__anon_expr".into(), Vec::new());
-        Ok(FunctionAST(proto, e))
+    /// toplevelexpr ::= expression
+    ///
+    /// Implement `std::unique_ptr<FunctionAST> ParseTopLevelExpr();` from the tutorial.
+    pub fn parse_top_level_expr(&mut self) -> ParseResult<FunctionAST> {
+        let e = self.parse_expression()?;
+        let proto = PrototypeAST("__anon_expr".into(), Vec::new());
+        Ok(FunctionAST(proto, e))
     }
 }
 
-/// Get the binary operator precedence.
-///
-/// Implement `int GetTokPrecedence();` from the tutorial.
-fn get_tok_precedence(tok: &Token) -> isize {
-    match tok {
-        Token::Char('<') => 10,
-        Token::Char('+') => 20,
-        Token::Char('-') => 20,
-        Token::Char('*') => 40,
-        _ => -1,
+/// Get the binary operator precedence.
+///
+/// Implement `int GetTokPrecedence();` from the tutorial.
+fn get_tok_precedence(tok: &Token) -> isize {
+    match tok {
+        Token::Char('<') => 10,
+        Token::Char('+') => 20,
+        Token::Char('-') => 20,
+        Token::Char('*') => 40,
+        _ => -1,
     }
 }
 
-#[cfg(test)]
-mod test {
-    use super::{ExprAST, FunctionAST, Parser, PrototypeAST};
-    use crate::lexer::Lexer;
+#[cfg(test)]
+mod test {
+    use super::{ExprAST, FunctionAST, Parser, PrototypeAST};
+    use crate::lexer::Lexer;
 
-    fn parser(input: &str) -> Parser<std::str::Chars> {
-        let l = Lexer::new(input.chars());
-        let mut p = Parser::new(l);
+    fn parser(input: &str) -> Parser<std::str::Chars> {
+        let l = Lexer::new(input.chars());
+        let mut p = Parser::new(l);
 
-        // Drop initial coin, initialize cur_tok.
-        p.get_next_token();
+        // Drop initial coin, initialize cur_tok.
+        p.get_next_token();
 
-        p
+        p
     }
 
-    #[test]
-    fn parse_number() {
-        let mut p = parser("13.37");
+    #[test]
+    fn parse_number() {
+        let mut p = parser("13.37");
 
-        assert_eq!(p.parse_num_expr(), Ok(ExprAST::Number(13.37f64)));
+        assert_eq!(p.parse_num_expr(), Ok(ExprAST::Number(13.37f64)));
     }
 
-    #[test]
-    fn parse_variable() {
-        let mut p = parser("foop");
+    #[test]
+    fn parse_variable() {
+        let mut p = parser("foop");
 
         assert_eq!(
-            p.parse_identifier_expr(),
-            Ok(ExprAST::Variable("foop".into()))
+            p.parse_identifier_expr(),
+            Ok(ExprAST::Variable("foop".into()))
         );
     }
 
-    #[test]
-    fn parse_if() {
-        let mut p = parser("if 1 then 2 else 3");
+    #[test]
+    fn parse_if() {
+        let mut p = parser("if 1 then 2 else 3");
 
-        let cond = Box::new(ExprAST::Number(1f64));
-        let then = Box::new(ExprAST::Number(2f64));
-        let else_ = Box::new(ExprAST::Number(3f64));
+        let cond = Box::new(ExprAST::Number(1f64));
+        let then = Box::new(ExprAST::Number(2f64));
+        let else_ = Box::new(ExprAST::Number(3f64));
 
-        assert_eq!(p.parse_if_expr(), Ok(ExprAST::If { cond, then, else_ }));
+        assert_eq!(p.parse_if_expr(), Ok(ExprAST::If { cond, then, else_ }));
 
-        let mut p = parser("if foo() then bar(2) else baz(3)");
+        let mut p = parser("if foo() then bar(2) else baz(3)");
 
-        let cond = Box::new(ExprAST::Call("foo".into(), vec![]));
-        let then = Box::new(ExprAST::Call("bar".into(), vec![ExprAST::Number(2f64)]));
-        let else_ = Box::new(ExprAST::Call("baz".into(), vec![ExprAST::Number(3f64)]));
+        let cond = Box::new(ExprAST::Call("foo".into(), vec![]));
+        let then = Box::new(ExprAST::Call("bar".into(), vec![ExprAST::Number(2f64)]));
+        let else_ = Box::new(ExprAST::Call("baz".into(), vec![ExprAST::Number(3f64)]));
 
-        assert_eq!(p.parse_if_expr(), Ok(ExprAST::If { cond, then, else_ }));
+        assert_eq!(p.parse_if_expr(), Ok(ExprAST::If { cond, then, else_ }));
     }
 
-    #[test]
-    fn parse_for() {
-        let mut p = parser("for i = 1, 2, 3 in 4");
+    #[test]
+    fn parse_for() {
+        let mut p = parser("for i = 1, 2, 3 in 4");
 
-        let var = String::from("i");
-        let start = Box::new(ExprAST::Number(1f64));
-        let end = Box::new(ExprAST::Number(2f64));
-        let step = Some(Box::new(ExprAST::Number(3f64)));
-        let body = Box::new(ExprAST::Number(4f64));
+        let var = String::from("i");
+        let start = Box::new(ExprAST::Number(1f64));
+        let end = Box::new(ExprAST::Number(2f64));
+        let step = Some(Box::new(ExprAST::Number(3f64)));
+        let body = Box::new(ExprAST::Number(4f64));
 
         assert_eq!(
-            p.parse_for_expr(),
-            Ok(ExprAST::For {
-                var,
-                start,
-                end,
-                step,
-                body
+            p.parse_for_expr(),
+            Ok(ExprAST::For {
+                var,
+                start,
+                end,
+                step,
+                body
             })
         );
     }
 
-    #[test]
-    fn parse_for_no_step() {
-        let mut p = parser("for i = 1, 2 in 4");
+    #[test]
+    fn parse_for_no_step() {
+        let mut p = parser("for i = 1, 2 in 4");
 
-        let var = String::from("i");
-        let start = Box::new(ExprAST::Number(1f64));
-        let end = Box::new(ExprAST::Number(2f64));
-        let step = None;
-        let body = Box::new(ExprAST::Number(4f64));
+        let var = String::from("i");
+        let start = Box::new(ExprAST::Number(1f64));
+        let end = Box::new(ExprAST::Number(2f64));
+        let step = None;
+        let body = Box::new(ExprAST::Number(4f64));
 
         assert_eq!(
-            p.parse_for_expr(),
-            Ok(ExprAST::For {
-                var,
-                start,
-                end,
-                step,
-                body
+            p.parse_for_expr(),
+            Ok(ExprAST::For {
+                var,
+                start,
+                end,
+                step,
+                body
             })
         );
     }
 
-    #[test]
-    fn parse_primary() {
-        let mut p = parser("1337 foop \n bla(123) \n if a then b else c \n for x=1,2 in 3");
+    #[test]
+    fn parse_primary() {
+        let mut p = parser("1337 foop \n bla(123) \n if a then b else c \n for x=1,2 in 3");
 
-        assert_eq!(p.parse_primary(), Ok(ExprAST::Number(1337f64)));
+        assert_eq!(p.parse_primary(), Ok(ExprAST::Number(1337f64)));
 
-        assert_eq!(p.parse_primary(), Ok(ExprAST::Variable("foop".into())));
+        assert_eq!(p.parse_primary(), Ok(ExprAST::Variable("foop".into())));
 
         assert_eq!(
-            p.parse_primary(),
-            Ok(ExprAST::Call("bla".into(), vec![ExprAST::Number(123f64)]))
+            p.parse_primary(),
+            Ok(ExprAST::Call("bla".into(), vec![ExprAST::Number(123f64)]))
         );
 
         assert_eq!(
-            p.parse_primary(),
-            Ok(ExprAST::If {
-                cond: Box::new(ExprAST::Variable("a".into())),
-                then: Box::new(ExprAST::Variable("b".into())),
-                else_: Box::new(ExprAST::Variable("c".into())),
+            p.parse_primary(),
+            Ok(ExprAST::If {
+                cond: Box::new(ExprAST::Variable("a".into())),
+                then: Box::new(ExprAST::Variable("b".into())),
+                else_: Box::new(ExprAST::Variable("c".into())),
             })
         );
 
         assert_eq!(
-            p.parse_primary(),
-            Ok(ExprAST::For {
-                var: String::from("x"),
-                start: Box::new(ExprAST::Number(1f64)),
-                end: Box::new(ExprAST::Number(2f64)),
-                step: None,
-                body: Box::new(ExprAST::Number(3f64)),
+            p.parse_primary(),
+            Ok(ExprAST::For {
+                var: String::from("x"),
+                start: Box::new(ExprAST::Number(1f64)),
+                end: Box::new(ExprAST::Number(2f64)),
+                step: None,
+                body: Box::new(ExprAST::Number(3f64)),
             })
         );
     }
 
-    #[test]
-    fn parse_binary_op() {
-        // Operator before RHS has higher precedence, expected AST
-        //
-        //       -
-        //      / \
-        //     +     c
-        //    / \
-        //   a   b
-        let mut p = parser("a + b - c");
-
-        let binexpr_ab = ExprAST::Binary(
+    #[test]
+    fn parse_binary_op() {
+        // Operator before RHS has higher precedence, expected AST
+        //
+        //       -
+        //      / \
+        //     +     c
+        //    / \
+        //   a   b
+        let mut p = parser("a + b - c");
+
+        let binexpr_ab = ExprAST::Binary(
             '+',
-            Box::new(ExprAST::Variable("a".into())),
-            Box::new(ExprAST::Variable("b".into())),
+            Box::new(ExprAST::Variable("a".into())),
+            Box::new(ExprAST::Variable("b".into())),
         );
 
-        let binexpr_abc = ExprAST::Binary(
+        let binexpr_abc = ExprAST::Binary(
             '-',
-            Box::new(binexpr_ab),
-            Box::new(ExprAST::Variable("c".into())),
+            Box::new(binexpr_ab),
+            Box::new(ExprAST::Variable("c".into())),
         );
 
-        assert_eq!(p.parse_expression(), Ok(binexpr_abc));
+        assert_eq!(p.parse_expression(), Ok(binexpr_abc));
     }
 
-    #[test]
-    fn parse_binary_op2() {
-        // Operator after RHS has higher precedence, expected AST
-        //
-        //       +
-        //      / \
-        //     a   *
-        //        / \
-        //       b   c
-        let mut p = parser("a + b * c");
-
-        let binexpr_bc = ExprAST::Binary(
+    #[test]
+    fn parse_binary_op2() {
+        // Operator after RHS has higher precedence, expected AST
+        //
+        //       +
+        //      / \
+        //     a   *
+        //        / \
+        //       b   c
+        let mut p = parser("a + b * c");
+
+        let binexpr_bc = ExprAST::Binary(
             '*',
-            Box::new(ExprAST::Variable("b".into())),
-            Box::new(ExprAST::Variable("c".into())),
+            Box::new(ExprAST::Variable("b".into())),
+            Box::new(ExprAST::Variable("c".into())),
         );
 
-        let binexpr_abc = ExprAST::Binary(
+        let binexpr_abc = ExprAST::Binary(
             '+',
-            Box::new(ExprAST::Variable("a".into())),
-            Box::new(binexpr_bc),
+            Box::new(ExprAST::Variable("a".into())),
+            Box::new(binexpr_bc),
         );
 
-        assert_eq!(p.parse_expression(), Ok(binexpr_abc));
+        assert_eq!(p.parse_expression(), Ok(binexpr_abc));
     }
 
-    #[test]
-    fn parse_prototype() {
-        let mut p = parser("foo(a,b)");
+    #[test]
+    fn parse_prototype() {
+        let mut p = parser("foo(a,b)");
 
-        let proto = PrototypeAST("foo".into(), vec!["a".into(), "b".into()]);
+        let proto = PrototypeAST("foo".into(), vec!["a".into(), "b".into()]);
 
-        assert_eq!(p.parse_prototype(), Ok(proto));
+        assert_eq!(p.parse_prototype(), Ok(proto));
     }
 
-    #[test]
-    fn parse_definition() {
-        let mut p = parser("def bar( arg0 , arg1 ) arg0 + arg1");
+    #[test]
+    fn parse_definition() {
+        let mut p = parser("def bar( arg0 , arg1 ) arg0 + arg1");
 
-        let proto = PrototypeAST("bar".into(), vec!["arg0".into(), "arg1".into()]);
+        let proto = PrototypeAST("bar".into(), vec!["arg0".into(), "arg1".into()]);
 
-        let body = ExprAST::Binary(
+        let body = ExprAST::Binary(
             '+',
-            Box::new(ExprAST::Variable("arg0".into())),
-            Box::new(ExprAST::Variable("arg1".into())),
+            Box::new(ExprAST::Variable("arg0".into())),
+            Box::new(ExprAST::Variable("arg1".into())),
         );
 
-        let func = FunctionAST(proto, body);
+        let func = FunctionAST(proto, body);
 
-        assert_eq!(p.parse_definition(), Ok(func));
+        assert_eq!(p.parse_definition(), Ok(func));
     }
 
-    #[test]
-    fn parse_extern() {
-        let mut p = parser("extern baz()");
+    #[test]
+    fn parse_extern() {
+        let mut p = parser("extern baz()");
 
-        let proto = PrototypeAST("baz".into(), vec![]);
+        let proto = PrototypeAST("baz".into(), vec![]);
 
-        assert_eq!(p.parse_extern(), Ok(proto));
+        assert_eq!(p.parse_extern(), Ok(proto));
     }
 }
 
-
- \ No newline at end of file +
\ No newline at end of file -- cgit v1.2.3