wahgex_core/compile/
instructions.rs

1//! This module contains type for emitting WASM instructions specifically for
2//! the regex machine.
3
4use std::alloc::Layout;
5
6use wasm_encoder::{InstructionSink, MemArg};
7
8pub trait InstructionSinkExt {
9    fn state_id_load(&mut self, offset: u64, state_id_layout: &Layout) -> &mut Self;
10
11    fn state_id_store(&mut self, offset: u64, state_id_layout: &Layout) -> &mut Self;
12}
13
14impl InstructionSinkExt for InstructionSink<'_> {
15    fn state_id_load(&mut self, offset: u64, state_id_layout: &Layout) -> &mut Self {
16        let state_id_size = state_id_layout.size();
17        if state_id_size == 1 {
18            self.i32_load8_u(MemArg {
19                offset,
20                align: state_id_layout.align().ilog2(),
21                memory_index: 1, // states are always stored in the state memory
22            })
23        } else if state_id_size == 2 {
24            self.i32_load16_u(MemArg {
25                offset,
26                align: state_id_layout.align().ilog2(),
27                memory_index: 1, // states are always stored in the state memory
28            })
29        } else {
30            self.i32_load(MemArg {
31                offset,
32                align: state_id_layout.align().ilog2(),
33                memory_index: 1, // states are always stored in the state memory
34            })
35        }
36    }
37
38    fn state_id_store(&mut self, offset: u64, state_id_layout: &Layout) -> &mut Self {
39        let state_id_size = state_id_layout.size();
40        let align = state_id_layout.align().ilog2();
41        if state_id_size == 1 {
42            self.i32_store8(MemArg {
43                offset,
44                align,
45                memory_index: 1, // states are always stored in the state memory
46            })
47        } else if state_id_size == 2 {
48            self.i32_store16(MemArg {
49                offset,
50                align,
51                memory_index: 1, // states are always stored in the state memory
52            })
53        } else {
54            self.i32_store(MemArg {
55                offset,
56                align,
57                memory_index: 1, // states are always stored in the state memory
58            })
59        }
60    }
61}