Skip to main content

luaur_code_gen/functions/
try_get_next_block_in_chain.rs

1use crate::enums::ir_cmd::IrCmd;
2use crate::enums::ir_op_kind::IrOpKind;
3use crate::macros::op_a::op_a;
4use crate::records::ir_block::IrBlock;
5use crate::records::ir_function::IrFunction;
6use crate::records::ir_inst::IrInst;
7
8pub fn try_get_next_block_in_chain(function: &mut IrFunction, block: &mut IrBlock) -> *mut IrBlock {
9    let (is_jump, jump_op) = {
10        let term_inst: &mut IrInst = &mut function.instructions[block.finish as usize];
11        (term_inst.cmd == IrCmd::JUMP, op_a(term_inst))
12    };
13
14    // Follow the strict block chain
15    if is_jump && jump_op.kind() == IrOpKind::Block {
16        let target: &mut IrBlock = function.block_op(jump_op);
17
18        // Has to have the same sorting key and a consecutive chain key
19        if target.sortkey == block.sortkey && target.chainkey == block.chainkey + 1 {
20            return target as *mut IrBlock;
21        }
22    }
23
24    core::ptr::null_mut()
25}