Skip to main content

luaur_code_gen/methods/
ir_function_get_block_index.rs

1use crate::records::ir_block::IrBlock;
2use crate::records::ir_function::IrFunction;
3
4impl IrFunction {
5    pub fn get_block_index(&self, block: &IrBlock) -> u32 {
6        // Can only be called with blocks from our vector
7        let block_ptr = block as *const IrBlock as usize;
8        let base_ptr = self.blocks.as_ptr() as usize;
9        let end_ptr = unsafe { self.blocks.as_ptr().add(self.blocks.len()) } as usize;
10
11        if !(block_ptr >= base_ptr && block_ptr <= end_ptr) {
12            panic!("IrFunction::get_block_index: block not from this function");
13        }
14
15        let offset = block_ptr - base_ptr;
16        (offset / core::mem::size_of::<IrBlock>()) as u32
17    }
18}