diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-03-30 22:18:57 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-03-30 22:18:57 +0100 |
| commit | faade250d5adafb3fe9c8ab440e0a4d40a5e9f63 (patch) | |
| tree | cc2ec05ce106d0068b008ab676d5c8e3f5836a17 | |
| parent | e41262b82bcdfdb7857eab89f045cb917ab11305 (diff) | |
Add basic compilation of function body
| -rw-r--r-- | patio-to-cranelift/src/context.rs | 62 | ||||
| -rw-r--r-- | patio-to-cranelift/src/lib.rs | 125 |
2 files changed, 165 insertions, 22 deletions
diff --git a/patio-to-cranelift/src/context.rs b/patio-to-cranelift/src/context.rs index 65d7eb15..ca382be7 100644 --- a/patio-to-cranelift/src/context.rs +++ b/patio-to-cranelift/src/context.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::{collections::HashMap, path::Path}; use cranelift::prelude::Configurable; use cranelift_module::Module; @@ -6,9 +6,9 @@ use cranelift_module::Module; use crate::errors::CompileError; pub(crate) struct CompilerContext { - cranelift_context: cranelift_codegen::Context, + codegen_context: cranelift_codegen::Context, + func_name_to_func_id: HashMap<String, cranelift_module::FuncId>, module: cranelift_object::ObjectModule, - next_func_name_index: u32, pub(crate) target_triple: target_lexicon::Triple, } @@ -42,9 +42,9 @@ impl CompilerContext { fn new(module: cranelift_object::ObjectModule, target_triple: target_lexicon::Triple) -> Self { CompilerContext { - cranelift_context: cranelift_codegen::Context::new(), + codegen_context: module.make_context(), + func_name_to_func_id: Default::default(), module, - next_func_name_index: 0, target_triple, } } @@ -53,22 +53,50 @@ impl CompilerContext { self.module.finish() } - pub(crate) fn enter_function(&mut self, name: &str, signature: cranelift::prelude::Signature, linkage: cranelift_module::Linkage) { - let mut func = cranelift_codegen::ir::Function::with_name_signature(self.func_name(), signature); + pub(crate) fn declare_function(&mut self, function_name: &str, signature: &cranelift::prelude::Signature) { + let func_id = self.module.declare_function(function_name, cranelift_module::Linkage::Import, signature).unwrap(); + self.func_name_to_func_id.insert(function_name.to_string(), func_id); + } + + pub(crate) fn enter_function( + &mut self, + name: &str, + signature: cranelift::prelude::Signature, + linkage: cranelift_module::Linkage, + generate_function_body: impl Fn(&mut CompilerFunctionContext), + ) { + // TODO: clear context at start or end of function? + self.codegen_context.clear(); + let func_id = self.module.declare_function(name, linkage, &signature).unwrap(); + + // TODO: reuse function builder context let mut func_builder_context = cranelift::prelude::FunctionBuilderContext::new(); - let _function_builder = cranelift::prelude::FunctionBuilder::new(&mut func, &mut func_builder_context); + let mut function_builder = cranelift::prelude::FunctionBuilder::new(&mut self.codegen_context.func, &mut func_builder_context); + + let entry_block = function_builder.create_block(); + function_builder.append_block_params_for_function_params(entry_block); + function_builder.switch_to_block(entry_block); - let func_id = self.module.declare_function(name, linkage, &func.signature).unwrap(); - self.module.define_function(func_id, &mut self.cranelift_context).unwrap(); + generate_function_body(&mut CompilerFunctionContext { + func_name_to_func_id: &self.func_name_to_func_id, + function_builder, + module: &mut self.module, + }); + + self.module.define_function(func_id, &mut self.codegen_context).unwrap(); } +} + +pub(crate) struct CompilerFunctionContext<'a> { + func_name_to_func_id: &'a HashMap<String, cranelift_module::FuncId>, + pub(crate) function_builder: cranelift::prelude::FunctionBuilder<'a>, + module: &'a mut cranelift_object::ObjectModule, +} - /// Generate a function name for Cranelift. - fn func_name(&mut self) -> cranelift_codegen::ir::UserFuncName { - // Cranelift doesn't assign any particular meaning to the values, so we - // just use an incrementing index in a constant namespace. - let name = cranelift_codegen::ir::UserFuncName::user(0, self.next_func_name_index); - self.next_func_name_index += 1; - name +impl <'a> CompilerFunctionContext<'a> { + pub(crate) fn func_id(&mut self, func_name: &str) -> cranelift_codegen::ir::FuncRef { + let func_id = self.func_name_to_func_id[func_name]; + self.module.declare_func_in_func(func_id, self.function_builder.func) } } diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs index d3eeff8c..12097a9a 100644 --- a/patio-to-cranelift/src/lib.rs +++ b/patio-to-cranelift/src/lib.rs @@ -1,6 +1,8 @@ use std::{path::{Path}, str::FromStr}; -use crate::{context::CompilerContext, errors::CompileError, patio_generated::patio::{PatioCompilationUnitElement, PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioLocal, PatioLocalType, PatioType, PatioTypeValue, PatioVisibility}}; +use cranelift::prelude::InstBuilder; + +use crate::{context::{CompilerContext, CompilerFunctionContext}, errors::CompileError, patio_generated::patio::{PatioCall, PatioCompilationUnitElement, PatioCompilationUnitElementValue, PatioFunctionDefinition, PatioInstruction, PatioInstructionValue, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalType, PatioOperand, PatioOperandValue, PatioReturnVoid, PatioType, PatioTypeValue, PatioVisibility}}; #[allow(dead_code, unused_imports)] #[path = "./gen/patio_generated.rs"] @@ -25,6 +27,13 @@ pub fn compile(source_path: &Path, target_str: &str) -> Result<(), CompileError> let mut context = CompilerContext::for_object(source_path, target_triple)?; for element in compilation_unit.elements() { + if element.value_compilation_unit_element_value_type() == PatioCompilationUnitElementValue::PatioFunctionDeclaration { + let function_declaration = element.value_compilation_unit_element_value_as_patio_function_declaration().unwrap(); + compile_function_declaration(function_declaration, &mut context); + } + } + + for element in compilation_unit.elements() { compile_compilation_unit_element(&element, &mut context); } @@ -50,13 +59,34 @@ fn compile_compilation_unit_element( compile_function_definition(&function_definition, context); } - _ => todo!( - "Unsupported compilation unit element type: {}", - element.value_compilation_unit_element_value_type().0 + PatioCompilationUnitElementValue::PatioFunctionDeclaration => { + // Handled earlier. + } + + element_type => todo!( + "Unhandled compilation unit element type: {:?} {}", + element_type.variant_name(), + element_type.0, ), } } +fn compile_function_declaration( + function_declaration: patio_generated::patio::PatioFunctionDeclaration<'_>, + context: &mut CompilerContext +) { + let cranelift_signature = cranelift_signature( + function_declaration.params().iter(), + function_declaration.return_type(), + context, + ); + + context.declare_function( + function_declaration.name(), + &cranelift_signature, + ); +} + fn compile_function_definition( function_definition: &PatioFunctionDefinition, context: &mut CompilerContext, @@ -69,7 +99,92 @@ fn compile_function_definition( let cranelift_linkage = patio_visibility_to_cranelift_linkage(function_definition.visibility()); - context.enter_function(function_definition.name(), cranelift_signature, cranelift_linkage); + context.enter_function( + function_definition.name(), + cranelift_signature, + cranelift_linkage, + |function_context| { + for instruction in function_definition.body() { + compile_instruction(instruction, function_context) + } + } + ); +} + +fn compile_instruction( + instruction: PatioInstruction<'_>, + context: &mut CompilerFunctionContext<'_>, +) { + match instruction.value_instruction_value_type() { + PatioInstructionValue::PatioCall => { + let call = instruction.value_instruction_value_as_patio_call().unwrap(); + compile_call(call, context); + } + + PatioInstructionValue::PatioReturnVoid => { + let return_void = instruction.value_instruction_value_as_patio_return_void().unwrap(); + compile_return_void(return_void, context); + } + + instruction_type => { + todo!("Unhandled instruction type: {:?} {}", instruction_type.variant_name(), instruction_type.0) + } + } +} + +fn compile_call( + call: PatioCall<'_>, + context: &mut CompilerFunctionContext<'_>, +) { + let cranelift_args: Vec<_> = call.args().iter() + .map(|arg| compile_operand(arg, context)) + .collect(); + + let func_ref = context.func_id(call.function_name()); + context.function_builder.ins().call(func_ref, &cranelift_args); +} + +fn compile_return_void( + _return_void: PatioReturnVoid<'_>, + context: &mut CompilerFunctionContext<'_>, +) { + context.function_builder.ins().return_(&[]); +} + +fn compile_operand( + operand: PatioOperand<'_>, + context: &mut CompilerFunctionContext<'_>, +) -> cranelift::prelude::Value { + match operand.value_operand_value_type() { + PatioOperandValue::PatioIntConst => { + let int_const = operand.value_operand_value_as_patio_int_const().unwrap(); + // TODO: de-duplicate with patio_type_to_cranelift_type (and in so doing remove assumption about int size) + let cranelift_int_type = cranelift::prelude::Type::int(64).unwrap(); + compile_int_value(int_const.value(), cranelift_int_type, context) + } + + operand_type => { + todo!("Unhandled operand type: {:?} {}", operand_type.variant_name(), operand_type.0) + } + } +} + +fn compile_int_value( + int_value: PatioIntValue<'_>, + cranelift_type: cranelift::prelude::Type, + context: &mut CompilerFunctionContext<'_>, +) -> cranelift::prelude::Value { + match int_value.value_int_value_type() { + PatioIntValueValue::PatioIntValueLiteral => { + let literal = int_value.value_int_value_as_patio_int_value_literal().unwrap(); + + context.function_builder.ins().iconst(cranelift_type, i64::from(literal.value())) + } + + int_value_type => { + todo!("Unhandled int value type: {:?} {}", int_value_type.variant_name(), int_value_type.0) + } + } } fn cranelift_signature<'a>( |
