1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use bitvec::prelude::*;
use enum_dispatch::enum_dispatch;
use nom::{branch::alt, combinator::map, IResult};

mod bit_at;
mod bits_at;
mod branch_high;
mod branch_low;
mod csr;
mod jal_form;
mod register;
pub use bit_at::BitAt;
pub use bits_at::BitsAt;
pub use branch_high::BranchHigh;
pub use branch_low::BranchLow;
pub use csr::Csr;
pub use jal_form::JalForm;
pub use register::Register;

use super::param::Param;

#[enum_dispatch]
pub trait IsParamTransformer {
    fn param_to_instruction_part(&self, offset: u64, argument: &Param) -> BitVec<u32>;
    fn update_param(&self, instruction_part: &BitSlice<u32>, param: &mut Param);
    fn default_param(&self) -> Param;
    fn bit_count(&self) -> usize;
}

#[enum_dispatch(IsParamTransformer)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ParamTransformer {
    BitAt,
    BitsAt,
    Register,
    Csr,
    JalForm,
    BranchHigh,
    BranchLow,
}

pub fn parse(code: &str) -> IResult<&str, ParamTransformer> {
    alt((
        map(bit_at::parse, ParamTransformer::BitAt),
        map(bits_at::parse, ParamTransformer::BitsAt),
        map(register::parse, ParamTransformer::Register),
        map(csr::parse, ParamTransformer::Csr),
        map(jal_form::parse, ParamTransformer::JalForm),
        map(branch_high::parse, ParamTransformer::BranchHigh),
        map(branch_low::parse, ParamTransformer::BranchLow),
    ))(code)
}