aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-06 11:01:41 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-06 11:01:41 +0100
commit5b40de4b578c136e5e3d567ada603540cb574ba2 (patch)
tree8b846ae1610d1747e2f450f837b16fcf09d9e1d1
parent893ff4f5d4c4d370b30eea0b5a1502852e02e4b7 (diff)
Don't use cranelift vars for Patio vals
-rw-r--r--patio-to-cranelift/src/codegen/context.rs24
-rw-r--r--patio-to-cranelift/src/codegen/mod.rs38
2 files changed, 32 insertions, 30 deletions
diff --git a/patio-to-cranelift/src/codegen/context.rs b/patio-to-cranelift/src/codegen/context.rs
index 4d87adcf..cf6bed84 100644
--- a/patio-to-cranelift/src/codegen/context.rs
+++ b/patio-to-cranelift/src/codegen/context.rs
@@ -98,8 +98,9 @@ impl CompilerContext {
function_builder,
module: &mut self.module,
target_triple: self.target_triple.clone(),
+ local_name_to_value: Default::default(),
+ local_name_to_type: Default::default(),
var_name_to_variable: Default::default(),
- var_name_to_type: Default::default(),
};
generate_function_body(&mut context);
@@ -154,8 +155,9 @@ pub(crate) struct CompilerFunctionContext<'a> {
pub(crate) function_builder: cranelift::prelude::FunctionBuilder<'a>,
module: &'a mut cranelift_object::ObjectModule,
pub(crate) target_triple: target_lexicon::Triple,
+ local_name_to_value: HashMap<String, cranelift_codegen::ir::Value>,
+ local_name_to_type: HashMap<String, cranelift_codegen::ir::Type>,
var_name_to_variable: HashMap<String, cranelift::prelude::Variable>,
- var_name_to_type: HashMap<String, cranelift_codegen::ir::Type>,
}
impl<'a> CompilerFunctionContext<'a> {
@@ -165,18 +167,30 @@ impl<'a> CompilerFunctionContext<'a> {
.declare_func_in_func(func_id, self.function_builder.func)
}
+ pub(crate) fn declare_local(&mut self, name: &str, type_: cranelift_codegen::ir::Type) {
+ self.local_name_to_type.insert(name.to_string(), type_);
+ }
+
+ pub(crate) fn local_store(&mut self, name: &str, value: cranelift_codegen::ir::Value) {
+ // TODO: error if already defined.
+ self.local_name_to_value.insert(name.to_string(), value);
+ }
+
+ pub(crate) fn local_load(&self, name: &str) -> cranelift_codegen::ir::Value {
+ self.local_name_to_value[name]
+ }
+
pub(crate) fn declare_var(&mut self, name: &str, type_: cranelift_codegen::ir::Type) {
let variable = self.function_builder.declare_var(type_);
self.var_name_to_variable.insert(name.to_string(), variable);
- self.var_name_to_type.insert(name.to_string(), type_);
}
pub(crate) fn var(&mut self, name: &str) -> cranelift::prelude::Variable {
self.var_name_to_variable[name]
}
- pub(crate) fn var_type(&mut self, name: &str) -> cranelift_codegen::ir::Type {
- self.var_name_to_type[name]
+ pub(crate) fn local_type(&mut self, name: &str) -> cranelift_codegen::ir::Type {
+ self.local_name_to_type[name]
}
pub(crate) fn data_ptr(&mut self, name: &str) -> cranelift_codegen::ir::GlobalValue {
diff --git a/patio-to-cranelift/src/codegen/mod.rs b/patio-to-cranelift/src/codegen/mod.rs
index aafa6faa..9b37e3b5 100644
--- a/patio-to-cranelift/src/codegen/mod.rs
+++ b/patio-to-cranelift/src/codegen/mod.rs
@@ -196,23 +196,17 @@ fn define_function_definition(
cranelift_signature,
|function_context| {
for local in &function_definition.locals {
+ let cranelift_type = types::patio_type_to_cranelift_type(
+ local.type_,
+ &function_context.target_triple,
+ );
+
match local.local_type {
- PatioLocalType::Param => {}
-
- PatioLocalType::Val => {
- let cranelift_type = types::patio_type_to_cranelift_type(
- local.type_,
- &function_context.target_triple,
- );
- // TODO: don't use Cranelift vars for Patio vals
- function_context.declare_var(&local.name, cranelift_type);
+ PatioLocalType::Param | PatioLocalType::Val => {
+ function_context.declare_local(&local.name, cranelift_type);
}
PatioLocalType::Var => {
- let cranelift_type = types::patio_type_to_cranelift_type(
- local.type_,
- &function_context.target_triple,
- );
function_context.declare_var(&local.name, cranelift_type);
}
}
@@ -369,7 +363,7 @@ fn compile_call_pointer(
cranelift_signature
.returns
.push(cranelift_codegen::ir::AbiParam::new(
- context.var_type(&call_pointer.destination.name),
+ context.local_type(&call_pointer.destination.name),
));
let cranelift_signature_ref = context
@@ -406,7 +400,7 @@ fn compile_if(if_: &PatioIf, context: &mut CompilerFunctionContext<'_>) {
.destinations
.iter()
.map(|destination| {
- let cranelift_type = context.var_type(&destination.name);
+ let cranelift_type = context.local_type(&destination.name);
context
.function_builder
.append_block_param(cranelift_end_block, cranelift_type)
@@ -597,7 +591,7 @@ fn compile_switch(switch: &PatioSwitch, context: &mut CompilerFunctionContext<'_
.destinations
.iter()
.map(|destination| {
- let cranelift_type = context.var_type(&destination.name);
+ let cranelift_type = context.local_type(&destination.name);
context
.function_builder
.append_block_param(end_block, cranelift_type)
@@ -703,10 +697,7 @@ fn compile_operand(
compile_int_value(&value, types::CRANELIFT_INT_32_TYPE, context)
}
- PatioOperand::LocalRef(local_ref) => {
- let cranelift_variable = context.var(&local_ref.name);
- context.function_builder.use_var(cranelift_variable)
- }
+ PatioOperand::LocalRef(local_ref) => context.local_load(&local_ref.name),
_ => {
todo!("Unhandled operand: {operand:?}",)
@@ -760,10 +751,7 @@ fn def_destination(
value: cranelift_codegen::ir::Value,
context: &mut CompilerFunctionContext<'_>,
) {
- let cranelift_destination = context.var(&destination.name);
- context
- .function_builder
- .def_var(cranelift_destination, value);
+ context.local_store(&destination.name, value);
}
fn new_cranelift_signature() -> cranelift::prelude::Signature {
@@ -825,6 +813,6 @@ fn patio_operand_to_cranelift_type(
PatioOperand::Int32Const(_) => {
patio_type_to_cranelift_type(PatioType::Int32, &context.target_triple)
}
- PatioOperand::LocalRef(local_ref) => context.var_type(&local_ref.name),
+ PatioOperand::LocalRef(local_ref) => context.local_type(&local_ref.name),
}
}