luaur_code_gen/methods/
ir_function_get_inst_index.rs1use crate::records::ir_inst::IrInst;
2
3macro_rules! CODEGEN_ASSERT {
4 ($expr:expr) => {
5 assert!($expr);
6 };
7}
8
9impl crate::records::ir_function::IrFunction {
10 pub fn get_inst_index(&self, inst: &IrInst) -> u32 {
11 let inst_ptr = inst as *const IrInst as usize;
13 let base_ptr = self.instructions.as_ptr() as usize;
14 let end_ptr = unsafe { self.instructions.as_ptr().add(self.instructions.len()) } as usize;
15
16 CODEGEN_ASSERT!(inst_ptr >= base_ptr && inst_ptr <= end_ptr);
17
18 let offset = inst_ptr - base_ptr;
19 (offset / core::mem::size_of::<IrInst>()) as u32
20 }
21}