Skip to main content

vyre_libs/parsing/c/
source_bytes.rs

1use vyre_foundation::ir::Expr;
2
3/// Return an expression that loads one source byte from either resident-expanded
4/// or packed-byte haystack storage.
5#[must_use]
6pub fn load_source_byte(haystack: &str, byte_index: Expr, packed_haystack: bool) -> Expr {
7    if packed_haystack {
8        Expr::bitand(
9            Expr::shr(
10                Expr::load(haystack, Expr::shr(byte_index.clone(), Expr::u32(2))),
11                Expr::shl(Expr::bitand(byte_index, Expr::u32(3)), Expr::u32(3)),
12            ),
13            Expr::u32(0xff),
14        )
15    } else {
16        Expr::load(haystack, byte_index)
17    }
18}
19
20/// Number of `u32` storage elements required for a haystack of `source_len`
21/// logical source bytes.
22#[must_use]
23pub fn source_haystack_words(source_len: u32, packed_haystack: bool) -> u32 {
24    if packed_haystack {
25        source_len.max(1).div_ceil(4).max(1)
26    } else {
27        source_len.max(1)
28    }
29}