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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#[cfg(export_lexers)]
use std::io::Write;

use super::super::parser::parser::RISCVSymbolList;
pub use super::super::{
    super::{rv32f::constants::*, rv32i::constants::*},
    parser::parser::RISCVParser,
};
pub use crate::interface::parser::*;

pub const MAX_DATA_SIZE: usize = 0xf_ffff;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RISCV;

pub enum RISCVExtension {
    RV32I,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVInstOp {
    RV32I(RV32IInstruction),
    RV32F(RV32FInstruction),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVRegister {
    RV32I(RV32IRegister),
    RV32F(RV32FRegister),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVImmediate {
    Imm(RISCVImmediate),
    Lbl((ParserRISCVLabel, ParserRISCVLabelHandler)),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVCsr {
    RV32I(RV32ICsr),
    RV32F(RV32FCsr),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVInstOpd {
    Reg(ParserRISCVRegister),
    Imm(ParserRISCVImmediate),
    Lbl(ParserRISCVLabel),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVLabel {
    Text(usize),  // ParserResult<RISCV>::text[usize]
    Data(usize),  // ParserResult<RISCV>::data[usize]
    Unknown(Pos), // the label position in the code (mustn't exist in the output)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParserRISCVLabelHandler {
    Low,
    High,
    DeltaHigh,
    DeltaMinusOneLow,
}

impl ParserInstSet for RISCV {
    type Operator = ParserRISCVInstOp;
    type Operand = ParserRISCVInstOpd;
}

// ------------------------- Implementations -------------------------

impl RISCVExtension {
    pub fn get_symbol_parser(&self) -> &RISCVSymbolList {
        match self {
            RISCVExtension::RV32I => &super::super::super::rv32i::parser::parser::RV32I_SYMBOL_LIST,
        }
    }

    #[cfg(export_lexers)]
    pub fn export(&self, folder: &str) -> std::io::Result<()> {
        match self {
            RISCVExtension::RV32I => super::super::super::rv32i::parser::parser::export(folder),
        }
    }
}

#[cfg(export_lexers)]
pub fn export_pair<T, KFn, VFn, K, W>(
    pairs: &[T],
    key_fn: KFn,
    val_fn: VFn,
    prefix: [&str; 2],
    output: &mut std::io::BufWriter<W>,
) -> std::io::Result<()>
where
    KFn: Fn(&T) -> K,
    VFn: Fn(&T, &mut std::io::BufWriter<W>) -> std::io::Result<()>,
    K: std::fmt::Display,
    W: std::io::Write,
{
    if pairs.is_empty() {
        output.write("{}".as_bytes())?;
    } else {
        output.write("{\n".as_bytes())?;
        for data in &pairs[0..pairs.len() - 1] {
            let key = key_fn(data);
            output.write(format!("{}\"{}\": ", prefix[1], key).as_bytes())?;
            val_fn(data, output)?;
            output.write(",\n".as_bytes())?;
        }
        {
            let data = &pairs[pairs.len() - 1];
            let key = key_fn(data);
            output.write(format!("{}\"{}\": ", prefix[1], key).as_bytes())?;
            val_fn(data, output)?;
            output.write("\n".as_bytes())?;
        }
        output.write(format!("{}}}", prefix[0]).as_bytes())?;
    }
    Ok(())
}

#[cfg(export_lexers)]
pub fn export_list<T, F, V, W>(
    list: &[T],
    val_fn: F,
    prefix: [&str; 2],
    output: &mut std::io::BufWriter<W>,
) -> std::io::Result<()>
where
    F: Fn(&T) -> std::io::Result<V>,
    V: std::fmt::Display,
    W: std::io::Write,
{
    if list.is_empty() {
        output.write("[]".as_bytes())?;
    } else {
        output.write("[\n".as_bytes())?;
        for data in &list[0..list.len() - 1] {
            let val = val_fn(data)?;
            output.write(format!("{}\"{}\",\n", prefix[1], val).as_bytes())?;
        }
        {
            let data = &list[list.len() - 1];
            let val = val_fn(data)?;
            output.write(format!("{}\"{}\"\n", prefix[1], val).as_bytes())?;
        }
        output.write(format!("{}]", prefix[0]).as_bytes())?;
    }
    Ok(())
}