From cd62c3f8ccce0d834c333edafe2a825126c2bef6 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Sat, 18 Sep 2021 00:18:28 +0200 Subject: llvm: split into sub-modules --- src/codegen.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/codegen.rs') diff --git a/src/codegen.rs b/src/codegen.rs index 0a7893e..08c3039 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::llvm::{Builder, FnValue, FunctionPassManager, Module, Value}; +use crate::llvm::{FnValue, FunctionPassManager, IRBuilder, Module, Value}; use crate::parser::{ExprAST, FunctionAST, PrototypeAST}; use crate::Either; @@ -9,7 +9,7 @@ type CodegenResult = Result; /// Code generator from kaleidoscope AST to LLVM IR. pub struct Codegen<'llvm, 'a> { module: &'llvm Module, - builder: &'a Builder<'llvm>, + builder: &'a IRBuilder<'llvm>, fpm: &'a FunctionPassManager<'llvm>, } @@ -21,7 +21,7 @@ impl<'llvm, 'a> Codegen<'llvm, 'a> { ) -> CodegenResult> { let cg = Codegen { module, - builder: &Builder::with_ctx(module), + builder: &IRBuilder::with_ctx(module), fpm: &FunctionPassManager::with_ctx(module), }; let mut variables = HashMap::new(); @@ -129,7 +129,10 @@ impl<'llvm, 'a> Codegen<'llvm, 'a> { 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); + Ok(the_function) } else { todo!("Failed to codegen function body, erase from module!"); -- cgit v1.2.3