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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use crate::primitives::*;
use std::fs;
use indexmap::IndexMap;

type IResultStr<'a> = IResult<&'a str, &'a str>;

use nom::{
    bytes::complete::{is_not, tag, take_until},
    combinator::value,
    sequence::{pair, terminated},
    IResult,
};

fn take_until_or_end<'a>(tag: &'a str, istr: &'a str) -> IResultStr<'a> {
    let ret: IResult<&str, &str> = take_until(tag)(istr);
    match ret {
        Ok(x) => Ok(x),
        Err(_) => Ok(("", istr)),
    }
}

fn terminated_newline<'a>(istr: &'a str) -> IResultStr<'a> {
    let ret: IResult<&str, &str> =
        terminated(take_until("\n"), nom::character::complete::newline)(istr);
    match ret {
        Ok(x) => Ok(x),
        Err(_) => Ok(("", istr)),
    }
}

fn lut_table_parser<'a>(input: &'a str, table: &mut Vec<Vec<u8>>) -> IResultStr<'a> {
    let mut i = input;
    let mut li;
    let mut te;
    while i.len() > 0 {
        (i, li) = terminated_newline(i)?;

        let mut row: Vec<u8> = vec![];
        let (_, mut table_input) = take_until(" ")(li)?;
        while table_input.len() > 0 {
            (table_input, te) = nom::character::complete::one_of("01")(table_input)?;
            row.push(te.to_digit(10).unwrap() as u8);
        }
        table.push(row);
    }
    Ok(("", ""))
}

fn lut_body_parser<'a>(input: &'a str, luts: &mut Vec<ParsedPrimitive>) -> IResultStr<'a> {
    let (i, ioline) = terminated_newline(input)?;
    let mut io: Vec<&str> = ioline.split(' ').collect();

    let output = io.pop().unwrap_or("INVALID_OUTPUT").to_string();
    let mut inputs: Vec<String> = io.iter().map(|v| v.to_string()).collect();
    let (i, table) = take_until_or_end(".", i)?;

    let mut lut_table = vec![];
    let _ = lut_table_parser(table, &mut lut_table);

    // Check if the LUT has constant inputs
    let mut const_indices: Vec<usize> = vec![];
    for (idx, input) in inputs.iter().enumerate() {
        if input == "$false" || input == "$true" {
            const_indices.push(idx);
        }
    }

    // If the LUT has constant inputs, remove it
    const_indices.sort();
    for idx in const_indices.iter().rev() {
        lut_table.remove(*idx);
        inputs.remove(*idx);
    }

    luts.push(ParsedPrimitive::Lut {
        inputs: inputs,
        output: output,
        table: lut_table
    });

    Ok((i, ""))
}

fn subckt_parser<'a>(input: &'a str, subckts: &mut Vec<ParsedPrimitive>) -> IResultStr<'a> {
    let (i, sline) = terminated_newline(input)?;
    let conns_vec: Vec<&str> = sline.split(' ').collect();
    let name = conns_vec[0];

    let mut conns = IndexMap::new();
    conns_vec.iter().skip(1).for_each(|c| {
        let lr: Vec<&str> = c.split('=').collect();
        let lhs = lr[0];
        let rhs = lr[1];
        conns.insert(lhs.to_string(), rhs.to_string());
    });

    subckts.push(ParsedPrimitive::Subckt {
        name: name.to_string(),
        conns: conns,
    });

    Ok((i, ""))
}

// _SDFF_NP0_ : FF with reset C D Q R
// _DFFE_PN_  : FF with enables C D E Q
// _SDFFE_PP0N_ : FF with reset and enable C D E Q R
fn gate_parser<'a>(input: &'a str, gates: &mut Vec<ParsedPrimitive>) -> IResultStr<'a> {
    let (i, line) = terminated_newline(input)?;

    let signal_conns: Vec<&str> = line.split(' ').collect();
    let mut c = "".to_string();
    let mut d = "".to_string();
    let mut q = "".to_string();
    let mut r = None;
    let mut e = None;

    for sc in signal_conns.iter() {
        let x: Vec<&str> = sc.split('=').collect();
        if x.len() != 2 {
            continue;
        }
        match x[0] {
            "C" => {
                c = x[1].to_string();
            }
            "D" => {
                d = x[1].to_string();
            }
            "Q" => {
                q = x[1].to_string();
            }
            "R" => {
                r = Some(x[1].to_string());
            }
            "E" => {
                e = Some(x[1].to_string());
            }
            _ => {}
        }
    }

    gates.push(ParsedPrimitive::Gate { c: c, d: d, q: q, r: r, e: e });
    Ok((i, ""))
}

fn latch_parser<'a>(input: &'a str, latches: &mut Vec<ParsedPrimitive>) -> IResultStr<'a> {
    let (i, line) = terminated_newline(input)?;
    let latch_info: Vec<&str> = line.split(' ').collect();

    let mut input = "".to_string();
    let mut output = "".to_string();
    let mut control = "".to_string();
    let mut init = LatchInit::UNKNOWN;

    for (idx, li) in latch_info.iter().enumerate() {
        match idx {
            0 => {
                input = li.to_string();
            }
            1 => {
                output = li.to_string();
            }
            3 => {
                control = li.to_string();
            }
            4 => {
                init = LatchInit::to_enum(li);
            }
            _ => {}
        }
    }
    match init {
        LatchInit::ONE =>
            assert!(false, "Chisel RegInits changes the LUTs not the latch inputs"),
        _ =>
            ()
    }
    latches.push(ParsedPrimitive::Latch { input: input, output: output, control: control, init: init });
    Ok((i, ""))
}

fn module_body_parser<'a>(input: &'a str, modules: &mut Vec<ParsedPrimitive>) -> IResultStr<'a> {
    let body_end_marker = "\n.end\n";

    // Get module body
    let (i, _) = tag(".model ")(input)?;
    let (i, name) = terminated(take_until("\n"), nom::character::complete::newline)(i)?;
    let (mut i, body) = terminated(
        take_until(body_end_marker),
        nom::character::complete::newline,
    )(i)?;

    // Parse inputs
    let (bi, iline) = terminated(take_until("\n"), nom::character::complete::newline)(body)?;
    let inputs: Vec<String> = iline.split(' ').map(|v| v.to_string()).skip(1).collect();

    // Parse outputs
    let (bi, oline) = terminated(take_until("\n"), nom::character::complete::newline)(bi)?;
    let outputs: Vec<String> = oline.split(' ').map(|v| v.to_string()).skip(1).collect();

    let mut elems = vec![];
    let mut bi = bi;
    let mut tagstr;

    while bi.len() > 1 {
        (bi, tagstr) = terminated(take_until(" "), nom::character::complete::multispace0)(bi)?;
        if tagstr.eq(".names") {
            (bi, _) = lut_body_parser(bi, &mut elems)?;
        } else if tagstr.eq(".subckt") {
            (bi, _) = subckt_parser(bi, &mut elems)?;
        } else if tagstr.eq(".gate") {
            (bi, _) = gate_parser(bi, &mut elems)?;
        } else if tagstr.eq(".latch") {
            (bi, _) = latch_parser(bi, &mut elems)?;
        }
    }

    if i.len() > body_end_marker.to_string().len() {
        // Advance to the next .end
        (i, _) = take_until(".")(i)?;
    } else {
        // End of file
        (i, _) = take_until("\n")(i)?;
    }

    modules.push(ParsedPrimitive::Module {
        name: name.to_string(),
        inputs: inputs,
        outputs: outputs,
        elems: elems
    });

    Ok((i, ""))
}

fn parse_modules_from_blif_str<'a>(input: &'a str, circuit: &mut Vec<ParsedPrimitive>) -> IResultStr<'a> {
    // remove comment
    let (i, _) = value((), pair(tag("#"), is_not("\n")))(input)?;
    let (i, _) = take_until(".")(i)?;

    let mut i = i;
    while i.len() > 4 {
        (i, _) = module_body_parser(i, circuit)?;
        (i, _) = take_until_or_end("\n.model", i)?;
        (i, _) = terminated_newline(i)?;
    }

    Ok(("", ""))
}

fn parse_blif(input: &str) -> Result<Vec<ParsedPrimitive>, String> {
    let mut circuit = vec![];
    let res = parse_modules_from_blif_str(input, &mut circuit);
    match res {
        Ok(_) => {
            return Ok(circuit);
        }
        Err(e) => {
            return Err(format!("Error while parsing:\n{}", e).to_string());
        }
    }
}

pub fn parse_blif_file(input_file_path: &str) -> Result<Vec<ParsedPrimitive>, String> {
    let blif_file = fs::read_to_string(input_file_path);
    match blif_file {
        Ok(blif_str) => {
            return parse_blif(&blif_str);
        }
        Err(e) => {
            return Err(format!("Error while reading the file:\n{}", e).to_string());
        }
    }
}

#[cfg(test)]
pub mod parser_tests {
    use super::*;

    pub fn test_blif_parser(file_path: &str) -> bool {
        let res = parse_blif_file(&file_path);
        match res {
            Ok(_) => true,
            Err(err) => {
                println!("blif file parsing error:\n{}", err);
                false
            }
        }
    }

    #[test]
    pub fn test_adder_parse() {
        assert_eq!(test_blif_parser("./tests/Adder.lut.blif"), true);
    }

    #[test]
    pub fn test_gcd_parse() {
        assert_eq!(test_blif_parser("./tests/GCD.lut.blif"), true);
    }
}