Skip to main content

luaur_code_gen/functions/
cast_reg.rs

1use crate::enums::kind_a_64::KindA64;
2use crate::records::register_a_64::RegisterA64;
3
4pub const fn cast_reg(kind: KindA64, reg: RegisterA64) -> RegisterA64 {
5    // Since RegisterA64::kind() and RegisterA64::index() are not marked const in their dependency card,
6    // we must access the bits directly using the masks and shifts provided in the record's public API.
7    let reg_kind_bits = reg.bits & RegisterA64::KIND_MASK;
8    let kind_bits = kind as u8;
9
10    // CODEGEN_ASSERT is not usable in const fn contexts because it expands to
11    // non-const calls (assertCallHandler, intrinsics).
12    // We use a standard assert! which is supported in const fns since Rust 1.57.
13    assert!(kind_bits != reg_kind_bits);
14    assert!(kind_bits != KindA64::none as u8 && reg_kind_bits != KindA64::none as u8);
15    assert!(
16        (kind_bits == KindA64::w as u8 || kind_bits == KindA64::x as u8)
17            == (reg_kind_bits == KindA64::w as u8 || reg_kind_bits == KindA64::x as u8)
18    );
19
20    let reg_index_bits = reg.bits & RegisterA64::INDEX_MASK;
21
22    RegisterA64 {
23        bits: kind_bits | reg_index_bits,
24    }
25}