aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-04 09:00:27 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-04 09:00:27 +0100
commit2dc2b5624be8e4c51cd50bc77de6687c410e55bc (patch)
tree57d728bd07b222c5930dd7fdc54614cc4e2fa94d
parentc170166b8b56cbeab8c83da5e3ca8635e50d15f8 (diff)
Implements vars in patio-to-cranelift
-rw-r--r--patio-to-cranelift/src/context.rs28
-rw-r--r--patio-to-cranelift/src/lib.rs41
2 files changed, 68 insertions, 1 deletions
diff --git a/patio-to-cranelift/src/context.rs b/patio-to-cranelift/src/context.rs
index 7514e6f7..ecdecdc9 100644
--- a/patio-to-cranelift/src/context.rs
+++ b/patio-to-cranelift/src/context.rs
@@ -100,6 +100,8 @@ impl CompilerContext {
target_triple: self.target_triple.clone(),
var_name_to_variable: Default::default(),
var_name_to_type: Default::default(),
+ stack_slot_name_to_stack_slot: Default::default(),
+ stack_slot_name_to_type: Default::default(),
};
generate_function_body(&mut context);
@@ -119,6 +121,8 @@ pub(crate) struct CompilerFunctionContext<'a> {
pub(crate) target_triple: target_lexicon::Triple,
var_name_to_variable: HashMap<String, cranelift::prelude::Variable>,
var_name_to_type: HashMap<String, cranelift_codegen::ir::Type>,
+ stack_slot_name_to_stack_slot: HashMap<String, cranelift_codegen::ir::StackSlot>,
+ stack_slot_name_to_type: HashMap<String, cranelift_codegen::ir::Type>,
}
impl<'a> CompilerFunctionContext<'a> {
@@ -141,4 +145,28 @@ impl<'a> CompilerFunctionContext<'a> {
pub(crate) fn var_type(&mut self, name: &str) -> cranelift_codegen::ir::Type {
self.var_name_to_type[name]
}
+
+ pub(crate) fn create_stack_slot(&mut self, name: &str, type_: cranelift_codegen::ir::Type) {
+ let stack_slot =
+ self.function_builder
+ .create_sized_stack_slot(cranelift::prelude::StackSlotData {
+ kind: cranelift::prelude::StackSlotKind::ExplicitSlot,
+ size: type_.bytes(),
+ // TODO: alignment
+ align_shift: 0,
+ key: None,
+ });
+
+ self.stack_slot_name_to_stack_slot
+ .insert(name.to_string(), stack_slot);
+ self.stack_slot_name_to_type.insert(name.to_string(), type_);
+ }
+
+ pub(crate) fn stack_slot(&mut self, name: &str) -> cranelift_codegen::ir::StackSlot {
+ self.stack_slot_name_to_stack_slot[name]
+ }
+
+ pub(crate) fn stack_slot_type(&mut self, name: &str) -> cranelift_codegen::ir::Type {
+ self.stack_slot_name_to_type[name]
+ }
}
diff --git a/patio-to-cranelift/src/lib.rs b/patio-to-cranelift/src/lib.rs
index 3b33d097..430df6ff 100644
--- a/patio-to-cranelift/src/lib.rs
+++ b/patio-to-cranelift/src/lib.rs
@@ -12,7 +12,7 @@ use crate::{
PatioInstructionValue, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
PatioIntToBool, PatioIntValue, PatioIntValueValue, PatioLocal, PatioLocalRef,
PatioLocalStore, PatioLocalType, PatioOperand, PatioOperandValue, PatioReturn,
- PatioReturnVoid, PatioVisibility,
+ PatioReturnVoid, PatioVarLoad, PatioVarStore, PatioVisibility,
},
};
@@ -164,6 +164,14 @@ fn define_function_definition(
function_context.declare_var(local.name(), cranelift_type);
}
+ PatioLocalType::VAR => {
+ let cranelift_type = patio_type_to_cranelift_type(
+ local.type_().into(),
+ &function_context.target_triple,
+ );
+ function_context.create_stack_slot(local.name(), cranelift_type);
+ }
+
local_type => {
todo!(
"Unhandled local type: {:?} {}",
@@ -246,6 +254,16 @@ fn compile_instruction(
compile_return_void(return_void, context);
}
+ PatioInstructionValue::PatioVarLoad => {
+ let var_load = instruction.value_as_patio_var_load().unwrap();
+ compile_var_load(var_load, context);
+ }
+
+ PatioInstructionValue::PatioVarStore => {
+ let var_store = instruction.value_as_patio_var_store().unwrap();
+ compile_var_store(var_store, context);
+ }
+
instruction_type => {
todo!(
"Unhandled instruction type: {:?} {}",
@@ -429,6 +447,27 @@ fn compile_return_void(
context.function_builder.ins().return_(&[]);
}
+fn compile_var_load(var_load: PatioVarLoad<'_>, context: &mut CompilerFunctionContext<'_>) {
+ let stack_slot = context.stack_slot(var_load.var().name());
+ let type_ = context.stack_slot_type(var_load.var().name());
+ let value = context.function_builder.ins().stack_load(
+ type_,
+ stack_slot,
+ cranelift_codegen::ir::immediates::Offset32::new(0),
+ );
+ def_destination(var_load.destination(), value, context);
+}
+
+fn compile_var_store(var_store: PatioVarStore<'_>, context: &mut CompilerFunctionContext<'_>) {
+ let stack_slot = context.stack_slot(var_store.var().name());
+ let value = compile_operand(var_store.value(), context);
+ context.function_builder.ins().stack_store(
+ value,
+ stack_slot,
+ cranelift_codegen::ir::immediates::Offset32::new(0),
+ );
+}
+
fn compile_operands<'a>(
operands: impl Iterator<Item = PatioOperand<'a>>,
context: &mut CompilerFunctionContext<'_>,