From 61dc72b00e586f5ff408c3f4caf257a577ef58f3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 30 Mar 2026 20:59:57 +0100 Subject: Reuse function context --- patio-to-cranelift/src/main.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/patio-to-cranelift/src/main.rs b/patio-to-cranelift/src/main.rs index 4778ce09..6fb81b61 100644 --- a/patio-to-cranelift/src/main.rs +++ b/patio-to-cranelift/src/main.rs @@ -115,9 +115,8 @@ fn compile_function_definition( &signature, ).unwrap(); - // TODO: reuse context - let mut func_context = cranelift_codegen::Context::for_function(func); - context.module.define_function(func_id, &mut func_context).unwrap(); + context.enter_function(func); + context.module.define_function(func_id, &mut context.cranelift_context).unwrap(); } fn cranelift_signature<'a>( @@ -192,6 +191,7 @@ fn patio_type_to_cranelift_type( } struct CompilerContext { + cranelift_context: cranelift_codegen::Context, module: ObjectModule, next_func_name_index: u32, target_triple: target_lexicon::Triple, @@ -200,6 +200,7 @@ struct CompilerContext { impl CompilerContext { fn new(module: ObjectModule, target_triple: target_lexicon::Triple) -> Self { CompilerContext { + cranelift_context: cranelift_codegen::Context::new(), module, next_func_name_index: 0, target_triple, @@ -214,6 +215,10 @@ impl CompilerContext { self.next_func_name_index += 1; name } + + fn enter_function(&mut self, cranelift_function: cranelift_codegen::ir::Function) { + self.cranelift_context.func = cranelift_function; + } } #[derive(Debug)] -- cgit v1.2.3