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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::{collections::HashMap, sync::OnceLock};

use crate::{
    backend::riscv::simple_instruction::template,
    binary_format::clef::PendingSymbol,
    utility::parsing::{self, in_multispace},
};
use bitvec::prelude::*;
use itertools::Itertools;
use nom::{
    branch::alt,
    bytes::complete::tag,
    combinator::map,
    multi::many1,
    sequence::{delimited, tuple},
    IResult,
};

use super::{
    param::Decided,
    param_transformer::{self, IsParamTransformer, ParamTransformer},
    Param,
};

/// A part of a template.
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Part {
    /// Several determined bits.
    BitPattern(BitVec<u32>),
    /// A parameter transformer for transform `Self.0`th param into expected form.
    ParamTransformer((usize, ParamTransformer)),
}

fn parse_param_transformer(code: &str) -> IResult<&str, (usize, ParamTransformer)> {
    map(
        delimited(
            tag("{{"),
            in_multispace(tuple((
                delimited(tag("params["), parsing::integer, tag("]")),
                in_multispace(tag("|")),
                param_transformer::parse,
            ))),
            tag("}}"),
        ),
        |(param_id, _, transformer)| (param_id, transformer),
    )(code)
}

fn parse_template_part(code: &str) -> IResult<&str, Part> {
    // for human beings, we prefer to write the MSB first
    // so we need to reverse the bits
    alt((
        map(many1(alt((tag("0"), tag("1")))), |char_bits| {
            Part::BitPattern(
                char_bits
                    .into_iter()
                    .rev()
                    .map(|char_bit| match char_bit {
                        "0" => false,
                        "1" => true,
                        _ => unreachable!(),
                    })
                    .collect(),
            )
        }),
        map(parse_param_transformer, Part::ParamTransformer),
    ))(code)
}

/// A template of an instruction.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Template {
    pub name: &'static str,
    pub parts: Vec<Part>,
}

impl Template {
    fn param_bit_count(&self, param_id: usize) -> usize {
        let mut current_result = 0;
        for part in &self.parts {
            match part {
                // todo: handle these fucking things better
                Part::ParamTransformer((id, ParamTransformer::BitsAt(bits_at)))
                    if param_id == *id =>
                {
                    if bits_at.end as usize > current_result {
                        current_result = bits_at.end as usize;
                    }
                }
                Part::ParamTransformer((id, ParamTransformer::JalForm(_))) if param_id == *id => {
                    if 20usize > current_result {
                        current_result = 20;
                    }
                }
                _ => (),
            }
        }
        current_result
    }
    pub fn bit_count(&self) -> usize {
        self.parts
            .iter()
            .map(|part| match part {
                Part::BitPattern(bit_pattern) => bit_pattern.len(),
                Part::ParamTransformer((_, transformer)) => transformer.bit_count(),
            })
            .sum()
    }
    /// Render an instruction into bits with param and offset.
    pub fn render(&self, params: &[Param], offset: u64) -> BitVec<u32> {
        let mut bits = BitVec::new();
        for part in &self.parts {
            match part {
                Part::BitPattern(bit_pattern) => bits.extend_from_bitslice(bit_pattern),
                Part::ParamTransformer((param_id, transformer)) => bits.extend_from_bitslice(
                    &transformer.param_to_instruction_part(offset, &params[*param_id]),
                ),
            }
        }
        bits
    }
    /// Parse the binary form of an instruction
    /// If matched success, return its params.
    pub fn parse_binary<'a>(
        &'a self,
        (mut bits, offset_bits): (&'a BitSlice<u32>, usize),
        pending_symbols: &[PendingSymbol],
    ) -> IResult<(&'a BitSlice<u32>, usize), Vec<Param>> {
        let mut params = Vec::new();
        for part in &self.parts {
            match part {
                Part::BitPattern(bit_pattern) => {
                    if bits.len() < bit_pattern.len() {
                        return Err(nom::Err::Error(nom::error::Error::new(
                            (bits, offset_bits),
                            nom::error::ErrorKind::Tag,
                        )));
                    }
                    if bits[0..bit_pattern.len()] != bit_pattern[..] {
                        return Err(nom::Err::Error(nom::error::Error::new(
                            (bits, offset_bits),
                            nom::error::ErrorKind::Tag,
                        )));
                    }
                    bits = &bits[bit_pattern.len()..];
                }
                Part::ParamTransformer((param_id, transformer)) => {
                    let param_id = *param_id;
                    while params.len() <= param_id {
                        params.push(None);
                    }
                    // the param is a symbol
                    let offset_bits = offset_bits as u32;
                    let offset_bytes = offset_bits / 8;
                    let symbol_param = if !matches!(
                        transformer,
                        ParamTransformer::Register(_) | ParamTransformer::Csr(_)
                    ) {
                        pending_symbols
                            .iter()
                            .find(|it| it.used_by_instruction_at_offset(offset_bytes))
                    } else {
                        None
                    };
                    if let Some(pending_symbol) = symbol_param {
                        let param = Param::Unresolved(pending_symbol.name.clone());
                        params[param_id] = Some(param);
                    } else {
                        let param = params
                            .get_mut(param_id)
                            .unwrap()
                            .get_or_insert(transformer.default_param());
                        transformer.update_param(&bits[0..transformer.bit_count()], param);
                        bits = &bits[transformer.bit_count()..];
                    }
                }
            }
        }
        let mut params = params.into_iter().map(|it| it.unwrap()).collect_vec();
        // extend the immediate values to 32 bits
        for (param_id, param) in params.iter_mut().enumerate() {
            if let Param::Decided(Decided::Immediate(imm))
            | Param::Resolved(_, Decided::Immediate(imm)) = param
            {
                let imm_bits = *imm as u32;
                let bits = imm_bits.view_bits::<Lsb0>();
                *imm = bits[0..self.param_bit_count(param_id)].load_le();
            }
        }
        Ok(((bits, offset_bits + self.bit_count()), params))
    }
}

/// Parse the string form of a template to get a [`Template`] object.
fn parse<'a>(code: &'a str, name: &'static str) -> IResult<&'a str, Template> {
    // for human beings, we prefer to write the MSB first
    // so we need to reverse the parts
    map(many1(parse_template_part), |mut parts| {
        parts.reverse();
        Template { name, parts }
    })(code)
}

// todo: make it an object?
/// All [`Template`]s for simple instructions in RISC-V
pub fn templates() -> &'static HashMap<&'static str, Template> {
    static TEMPLATE_MAPPING: OnceLock<HashMap<&'static str, Template>> = OnceLock::new();
    TEMPLATE_MAPPING.get_or_init(|| {
        let mut mapping = HashMap::new();
        let templates_str = include_str!("../spec/instructions.spec");
        let templates = templates_str
            .split('\n')
            .map(|it| it.trim())
            .filter(|it| !it.is_empty());
        for template in templates {
            let (name, template) = template.split_once(' ').unwrap();
            mapping.insert(
                name.trim(),
                template::parse(template.trim(), name).unwrap().1,
            );
        }
        mapping
    })
}

#[cfg(test)]
mod tests {

    use crate::backend::riscv::simple_instruction::param_transformer::{BitsAt, Register};

    use super::*;

    #[test]
    fn test_parse() {
        assert_eq!(
            parse("{{ params[0] | bits_at(0,5) }}100", "test"),
            Ok((
                "",
                Template {
                    name: "test",
                    parts: vec![
                        Part::BitPattern(bitvec![u32, Lsb0; 0, 0, 1]),
                        Part::ParamTransformer((0, BitsAt::new(0, 5).into())),
                    ]
                }
            ))
        );
    }

    #[test]
    fn test_render() {
        let template = Template {
            name: "test",
            parts: vec![
                Part::BitPattern(bitvec![u32, Lsb0; 0, 0, 1]),
                Part::ParamTransformer((0, BitsAt::new(0, 5).into())),
            ],
        };
        assert_eq!(
            template.render(&[Param::Decided(Decided::Immediate(0b11101))], 0),
            bits![0, 0, 1, 1, 0, 1, 1, 1]
        );

        let template = Template {
            parts: vec![
                Part::BitPattern(bitvec![u32, Lsb0; 1, 0, 1, 1, 0]),
                Part::ParamTransformer((1, BitsAt::new(5, 8).into())),
                Part::BitPattern(bitvec![u32, Lsb0; 0, 0, 1]),
                Part::ParamTransformer((0, Register.into())),
            ],
            name: "test",
        };
        assert_eq!(
            template.render(
                &[
                    Param::Decided(Decided::Register(0b11101)),
                    Param::Decided(Decided::Immediate(0b0010_0000))
                ],
                0
            ),
            bits![1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1]
        );
    }
}