ruka_codegen_wasm/
tail.rs

1use super::*;
2
3pub(crate) fn runtime_function(
4    runtime: &RuntimeFunctions,
5    symbol: &'static str,
6) -> Result<linker::RuntimeFunction, LowerError> {
7    runtime
8        .resolve(symbol)
9        .ok_or_else(|| LowerError::UnknownRuntimeSymbol(symbol.to_owned()))
10}
11
12/// Return the tracked runtime allocator descriptor used by generated code.
13pub(crate) fn runtime_tracked_alloc_function(
14    runtime: &RuntimeFunctions,
15) -> Result<linker::RuntimeFunction, LowerError> {
16    runtime_function(runtime, wasm_api::RT_ALLOC_BYTES_TRACKED_SITE_SYMBOL)
17}
18
19/// Compose runtime allocation site metadata for one allocation event.
20pub(crate) fn alloc_site_parts(
21    kind_id: i32,
22    source_pos: Option<ruka_mir::MirSourcePos>,
23    fallback_line: i32,
24) -> (i32, i32, i32, i32) {
25    if let Some(source_pos) = source_pos {
26        let file_id = i32::try_from(source_pos.file_id).unwrap_or(i32::MAX);
27        let line = i32::try_from(source_pos.line).unwrap_or(i32::MAX).max(0);
28        let column = i32::try_from(source_pos.column).unwrap_or(i32::MAX).max(0);
29        (kind_id, file_id, line, column)
30    } else {
31        (kind_id, 0, fallback_line.max(0), 0)
32    }
33}
34
35/// Return whether one place local is represented directly as a value local.
36pub(crate) fn is_passthrough_place_local(ctx: &LowerCtx<'_>, local: ruka_mir::MirLocalId) -> bool {
37    ctx.passthrough_place_locals.contains(&local.as_u32())
38}
39
40pub(crate) fn is_shadow_stack_local(ctx: &LowerCtx<'_>, local: ruka_mir::MirLocalId) -> bool {
41    ctx.shadow_stack_offsets.contains_key(&local.as_u32())
42}
43
44/// Return a stable MIR instruction label for diagnostics.
45pub(crate) fn instr_name(instr: &ruka_mir::MirInstr) -> &'static str {
46    match instr {
47        ruka_mir::MirInstr::ConstUnit { .. } => "ConstUnit",
48        ruka_mir::MirInstr::ConstInt { .. } => "ConstInt",
49        ruka_mir::MirInstr::ConstFloat { .. } => "ConstFloat",
50        ruka_mir::MirInstr::ConstString { .. } => "ConstString",
51        ruka_mir::MirInstr::ConstBool { .. } => "ConstBool",
52        ruka_mir::MirInstr::ConstNull { .. } => "ConstNull",
53        ruka_mir::MirInstr::ReleaseHeap { .. } => "ReleaseHeap",
54        ruka_mir::MirInstr::PointerNew { .. } => "PointerNew",
55        ruka_mir::MirInstr::MakeArray { .. } => "MakeArray",
56        ruka_mir::MirInstr::MakeTuple { .. } => "MakeTuple",
57        ruka_mir::MirInstr::MakeStruct { .. } => "MakeStruct",
58        ruka_mir::MirInstr::MakeEnum { .. } => "MakeEnum",
59        ruka_mir::MirInstr::ReadField { .. } => "ReadField",
60        ruka_mir::MirInstr::EnumIsVariant { .. } => "EnumIsVariant",
61        ruka_mir::MirInstr::EnumGetField { .. } => "EnumGetField",
62        ruka_mir::MirInstr::Copy { .. } => "Copy",
63        ruka_mir::MirInstr::Move { .. } => "Move",
64        ruka_mir::MirInstr::AssignLocal { .. } => "AssignLocal",
65        ruka_mir::MirInstr::StoreRef { .. } => "StoreRef",
66        ruka_mir::MirInstr::AssignFieldPath { .. } => "AssignFieldPath",
67        ruka_mir::MirInstr::Call { .. } => "Call",
68        ruka_mir::MirInstr::CallExtern { .. } => "CallExtern",
69        ruka_mir::MirInstr::CallIntrinsic { .. } => "CallIntrinsic",
70        ruka_mir::MirInstr::CollectionLen { .. } => "CollectionLen",
71        ruka_mir::MirInstr::IndexBorrowRo { .. } => "IndexBorrowRo",
72        ruka_mir::MirInstr::IndexBorrowMut { .. } => "IndexBorrowMut",
73        ruka_mir::MirInstr::SliceBorrowRo { .. } => "SliceBorrowRo",
74        ruka_mir::MirInstr::SliceBorrowMut { .. } => "SliceBorrowMut",
75        ruka_mir::MirInstr::FieldBorrowRo { .. } => "FieldBorrowRo",
76        ruka_mir::MirInstr::FieldBorrowMut { .. } => "FieldBorrowMut",
77        ruka_mir::MirInstr::PointerIsSome { .. } => "PointerIsSome",
78        ruka_mir::MirInstr::PointerBorrowRo { .. } => "PointerBorrowRo",
79        ruka_mir::MirInstr::PointerBorrowMut { .. } => "PointerBorrowMut",
80        ruka_mir::MirInstr::DerefCopy { .. } => "DerefCopy",
81        ruka_mir::MirInstr::NumCast { .. } => "NumCast",
82        ruka_mir::MirInstr::CheckedIntCast { .. } => "CheckedIntCast",
83        ruka_mir::MirInstr::IntLt { .. } => "IntLt",
84        ruka_mir::MirInstr::IntGt { .. } => "IntGt",
85        ruka_mir::MirInstr::IntLtEq { .. } => "IntLtEq",
86        ruka_mir::MirInstr::IntGtEq { .. } => "IntGtEq",
87        ruka_mir::MirInstr::IntEq { .. } => "IntEq",
88        ruka_mir::MirInstr::IntNeq { .. } => "IntNeq",
89        ruka_mir::MirInstr::IntAdd { .. } => "IntAdd",
90        ruka_mir::MirInstr::IntSub { .. } => "IntSub",
91        ruka_mir::MirInstr::IntMul { .. } => "IntMul",
92        ruka_mir::MirInstr::IntDiv { .. } => "IntDiv",
93        ruka_mir::MirInstr::IntMod { .. } => "IntMod",
94    }
95}