aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-06 17:40:29 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-06 17:40:29 +0100
commit1bfac23fac8b749cdcbd0232253a57b29ec91142 (patch)
tree7aa537345d74020c3f16dd34a999b2cb7471caf8
parentb7d9a653d63d7aecd56bd5e684ed08eef7262409 (diff)
Support unreachable instruction in patio-to-cranelift
-rw-r--r--patio-to-cranelift/src/codegen/mod.rs16
-rw-r--r--patio-to-cranelift/src/patio/flatbuffers.rs7
-rw-r--r--patio-to-cranelift/src/patio/instructions.rs3
3 files changed, 24 insertions, 2 deletions
diff --git a/patio-to-cranelift/src/codegen/mod.rs b/patio-to-cranelift/src/codegen/mod.rs
index 1e898123..29a8d949 100644
--- a/patio-to-cranelift/src/codegen/mod.rs
+++ b/patio-to-cranelift/src/codegen/mod.rs
@@ -20,7 +20,7 @@ use crate::{
PatioInstruction, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak,
PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn,
- PatioReturnVoid, PatioSwitch, PatioVarLoad, PatioVarStore,
+ PatioReturnVoid, PatioSwitch, PatioUnreachable, PatioVarLoad, PatioVarStore,
},
operands::{PatioLocalRef, PatioOperand},
types::PatioType,
@@ -372,6 +372,10 @@ fn compile_instruction(instruction: &PatioInstruction, context: &mut CompilerFun
compile_switch(switch, context);
}
+ PatioInstruction::Unreachable(unreachable) => {
+ compile_unreachable(unreachable, context);
+ }
+
PatioInstruction::VarLoad(var_load) => {
compile_var_load(var_load, context);
}
@@ -764,6 +768,11 @@ fn compile_switch(switch: &PatioSwitch, context: &mut CompilerFunctionContext<'_
}
}
+fn compile_unreachable(_: &PatioUnreachable, context: &mut CompilerFunctionContext<'_>) {
+ context.function_builder.ins().trap(trap_codes::UNREACHABLE);
+ context.mark_unreachable();
+}
+
fn compile_var_load(var_load: &PatioVarLoad, context: &mut CompilerFunctionContext<'_>) {
let cranelift_variable = context.var(&var_load.var.name);
let cranelift_value = context.function_builder.use_var(cranelift_variable);
@@ -943,3 +952,8 @@ fn patio_operand_to_cranelift_type(
PatioOperand::LocalRef(local_ref) => context.local_type(&local_ref.name),
}
}
+
+mod trap_codes {
+ pub(super) const UNREACHABLE: cranelift_codegen::ir::TrapCode =
+ cranelift_codegen::ir::TrapCode::unwrap_user(1);
+}
diff --git a/patio-to-cranelift/src/patio/flatbuffers.rs b/patio-to-cranelift/src/patio/flatbuffers.rs
index 34e0a1a1..4e391b66 100644
--- a/patio-to-cranelift/src/patio/flatbuffers.rs
+++ b/patio-to-cranelift/src/patio/flatbuffers.rs
@@ -11,7 +11,8 @@ use crate::{
PatioInstruction, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak,
PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn,
- PatioReturnVoid, PatioSwitch, PatioSwitchCase, PatioVarLoad, PatioVarStore,
+ PatioReturnVoid, PatioSwitch, PatioSwitchCase, PatioUnreachable, PatioVarLoad,
+ PatioVarStore,
},
operands::{PatioLocalRef, PatioOperand},
types::{PatioDataLayout, PatioType},
@@ -341,6 +342,10 @@ impl From<patio_generated::patio::PatioInstruction<'_>> for PatioInstruction {
})
}
+ patio_generated::patio::PatioInstructionValue::PatioUnreachable => {
+ Self::Unreachable(PatioUnreachable)
+ }
+
patio_generated::patio::PatioInstructionValue::PatioVarLoad => {
let var_load = value.value_as_patio_var_load().unwrap();
diff --git a/patio-to-cranelift/src/patio/instructions.rs b/patio-to-cranelift/src/patio/instructions.rs
index e9a3824e..8d05f6a2 100644
--- a/patio-to-cranelift/src/patio/instructions.rs
+++ b/patio-to-cranelift/src/patio/instructions.rs
@@ -26,6 +26,7 @@ pub(crate) enum PatioInstruction {
Return(PatioReturn),
ReturnVoid(PatioReturnVoid),
Switch(PatioSwitch),
+ Unreachable(PatioUnreachable),
VarLoad(PatioVarLoad),
VarStore(PatioVarStore),
}
@@ -147,6 +148,8 @@ pub(crate) struct PatioSwitchCase {
pub(crate) body: PatioBlock,
}
+pub(crate) struct PatioUnreachable;
+
pub(crate) struct PatioVarLoad {
pub(crate) destination: PatioLocalRef,
pub(crate) var: PatioLocalRef,