From 22ee941ad66dc2a28f398ac877a7377417a2a340 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 21 Aug 2024 20:12:47 +0300 Subject: [PATCH] Move module creation to IRModule --- src/codegen/llvm.rs | 57 +++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/codegen/llvm.rs b/src/codegen/llvm.rs index 934719b..b00a3ad 100644 --- a/src/codegen/llvm.rs +++ b/src/codegen/llvm.rs @@ -24,34 +24,7 @@ impl IRContext { } pub fn module<'a>(&'a mut self, name: String) -> IRModule<'a> { - unsafe { - let module = - LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), self.context); - - // TODO, fix later! - - let t = LLVMInt32TypeInContext(self.context); - - let mut argts = []; - let func_type = LLVMFunctionType(t, argts.as_mut_ptr(), argts.len() as u32, 0); - - let anon_func = LLVMAddFunction(module, into_cstring("testfunc").as_ptr(), func_type); - - let blockref = - LLVMCreateBasicBlockInContext(self.context, into_cstring("entryblock").as_ptr()); - LLVMPositionBuilderAtEnd(self.builder, blockref); - - // What is the last 1 ? - let val = LLVMConstInt(t, mem::transmute(3 as i64), 1); - - LLVMAppendExistingBasicBlock(anon_func, blockref); - LLVMBuildRet(self.builder, val); - - IRModule { - context: self, - module, - } - } + IRModule::new(self, name) } } @@ -71,6 +44,34 @@ pub struct IRModule<'a> { } impl<'a> IRModule<'a> { + fn new(context: &'a mut IRContext, name: String) -> IRModule<'a> { + unsafe { + let module = + LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), context.context); + + // TODO, fix later! + + let t = LLVMInt32TypeInContext(context.context); + + let mut argts = []; + let func_type = LLVMFunctionType(t, argts.as_mut_ptr(), argts.len() as u32, 0); + + let anon_func = LLVMAddFunction(module, into_cstring("testfunc").as_ptr(), func_type); + + let blockref = + LLVMCreateBasicBlockInContext(context.context, into_cstring("entryblock").as_ptr()); + LLVMPositionBuilderAtEnd(context.builder, blockref); + + // What is the last 1 ? + let val = LLVMConstInt(t, mem::transmute(3 as i64), 1); + + LLVMAppendExistingBasicBlock(anon_func, blockref); + LLVMBuildRet(context.builder, val); + + IRModule { context, module } + } + } + pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> { unsafe { CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() } }