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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#[macro_use] extern crate itertools;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate maplit;
#[macro_use] extern crate matches;
extern crate hoodlum_parser;
extern crate lalrpop_util;
extern crate regex;

pub mod sequence;
pub mod verilog;
pub mod walker;

pub use hoodlum_parser::{ParseError, ast, hdl_parser};
pub use verilog::ToVerilog;
pub use walker::*;
use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::fmt::Debug;
use regex::Regex;

pub fn codelist(code: &str) {
    for (i, line) in code.lines().enumerate() {
        println!("{:>3} | {}", i+1, line);
    }
}

pub fn code_error(code: &str, tok_pos: usize) {
    let code = format!("\n\n{}", code);
    let code = code.lines().collect::<Vec<_>>();
    let mut pos: isize = 0;
    for (i, lines) in (&code[..]).windows(3).enumerate() {
        if pos + lines[2].len() as isize >= tok_pos as isize {
            if i > 1 {
                println!("{:>3} | {}", i - 1, lines[0]);
            }
            if i > 0 {
                println!("{:>3} | {}", i, lines[1]);
            }
            println!("{:>3} | {}", i + 1, lines[2]);

            println!("{}^", (0..(tok_pos as isize) - (pos - 6)).map(|_| "~").collect::<String>());
            return;
        }
        pos += (lines[2].len() as isize) + 1;
    }
}

pub fn parse_results<C,T,E>(code: &str, res: Result<C, ParseError<usize,T,E>>) -> C
where C: Debug, T: Debug, E: Debug {
    match res {
        Ok(value) => {
            return value;
        }
        Err(ParseError::InvalidToken {
            location: loc
        }) => {
            println!("Error: Invalid token:");
            code_error(code, loc);
            panic!("{:?}", res);
        }
        Err(ParseError::UnrecognizedToken {
            token: Some((loc, _, _)),
            ..
        }) => {
            println!("Error: Unrecognized token:");
            code_error(code, loc);
            panic!("{:?}", res);
        }
        err => {
            panic!("{:?}", err);
        }
    }
}

#[macro_export]
macro_rules! hdl {
    ( $( $x:tt )* ) => {
        {
            let code = stringify!($($x)*);

            println!("Input");
            codelist(code);
            println!("");
            hoodlum::parse_results(&code, hoodlum::hdl_parser::parse_Code(&code))
        }
    };
}

pub struct TypeCollector {
    inner_types: BTreeMap<String, (Option<Vec<ast::Arg>>, Option<Vec<ast::Decl>>)>,
    literals: Vec<String>,
}

impl TypeCollector {
    pub fn new() -> TypeCollector {
        TypeCollector {
            inner_types: btreemap![],
            literals: vec![],
        }
    }

    // TODO make result
    pub fn validate(&self) {
        for (key, &(ref args, _)) in &self.inner_types {
            if args.is_none() {
                panic!("Declared {:?} without entity definition", key);
            }
        }
    }

    pub fn types(&self) -> BTreeMap<String, (Vec<ast::Arg>, Option<Vec<ast::Decl>>)> {
        self.inner_types.clone().into_iter().map(|x| {
            (x.0, ((x.1).0.unwrap(), (x.1).1))
        }).collect::<BTreeMap<_, _>>()
    }
}

impl Walker for TypeCollector {
    fn toplevel(&mut self, top: &ast::Toplevel) {
        match top {
            &ast::Toplevel::Entity(ref id, ref args) => {
                match self.inner_types.entry(id.0.clone()) {
                    Entry::Vacant(vacant) => {
                        vacant.insert((Some(args.clone()), None));
                    }
                    Entry::Occupied(mut occupied) => {
                        match occupied.get_mut() {
                            &mut (ref mut ref_args @ None, _) => {
                                *ref_args = Some(args.clone());
                            }
                            &mut (Some(_), _) => {
                                panic!("Repeated entity definition of {:?}", id.0);
                            }
                        }
                    }
                }
            }
            &ast::Toplevel::Impl(ref id, ref args) => {
                match self.inner_types.entry(id.0.clone()) {
                    Entry::Vacant(vacant) => {
                        vacant.insert((None, Some(args.clone())));
                    }
                    Entry::Occupied(mut occupied) => {
                        match occupied.get_mut() {
                            &mut (_, ref mut ref_args @ None) => {
                                *ref_args = Some(args.clone());
                            }
                            &mut (_, Some(_)) => {
                                panic!("Repeated impl definition of {:?}", id.0);
                            }
                        }
                    }
                }
            },
            &ast::Toplevel::VerilogLiteral(ref code) => {
                self.literals.push(code.clone());
            }
        }
    }
}

pub struct RefChecker {
    pub valid: Vec<String>,
}

impl RefChecker {
    pub fn new() -> Self {
        RefChecker {
            valid: vec![],
        }
    }
}

impl Walker for RefChecker {
    fn expr(&mut self, expr: &ast::Expr) {
        match expr {
            &ast::Expr::Ref(ref id) => {
                let id_str = &id.0;
                if self.valid.iter().position(|x| *x == *id_str).is_none() {
                    panic!("Invalid reference to undefined var {:?}", id_str);
                }
            }
            &ast::Expr::Slice(ref id, _, _) => {
                let id_str = &id.0;
                if self.valid.iter().position(|x| *x == *id_str).is_none() {
                    panic!("Invalid slice reference to undefined var {:?}", id_str);
                }
            }
            _ => { }
        }
    }

    fn edgeref(&mut self, expr: &ast::EdgeRef) {
        let id_str = &(expr.0).0;
        if self.valid.iter().position(|x| *x == *id_str).is_none() {
            panic!("Invalid clock reference to undefined var {:?}", id_str);
        }
    }

    fn seq(&mut self, expr: &ast::Seq) {
        match expr {
            &ast::Seq::Set(_, ref id, _) => {
                let id_str = &id.0;
                if self.valid.iter().position(|x| *x == *id_str).is_none() {
                    panic!("Invalid assignment to undefined var {:?}", id_str);
                }
            }
            _ => { }
        }
    }
}


pub struct DefMutChecker {
    pub valid: Vec<String>,
}

impl DefMutChecker {
    pub fn new() -> Self {
        DefMutChecker {
            valid: vec![],
        }
    }
}

impl Walker for DefMutChecker {
    fn seq(&mut self, expr: &ast::Seq) {
        match expr {
            &ast::Seq::Set(ast::BlockType::Blocking, ref id, _) |
            &ast::Seq::Set(ast::BlockType::NonBlocking, ref id, _) => {
                let id_str = &id.0;
                if self.valid.iter().position(|x| *x == *id_str).is_none() {
                    panic!("Invalid assignment to non-mutable var {:?}", id_str);
                }
            }
            _ => { }
        }
    }
}


pub struct DefChecker {
    pub valid: Vec<String>,
}

impl DefChecker {
    pub fn new() -> Self {
        DefChecker {
            valid: vec![],
        }
    }
}

impl Walker for DefChecker {
    fn seq(&mut self, expr: &ast::Seq) {
        match expr {
            &ast::Seq::Set(ast::BlockType::Static, ref id, _) => {
                let id_str = &id.0;
                if self.valid.iter().position(|x| *x == *id_str).is_none() {
                    panic!("Invalid static assignment to mutable var {:?}", id_str);
                }
            }
            _ => { }
        }
    }
}

//TODO not abort
pub fn typecheck(code: &ast::Code) {
    let mut types = TypeCollector::new();
    code.walk(&mut types);
    types.validate();

    for (_, entity) in types.types() {
        // Extract consts.
        let mut inner_consts = vec![];
        for item in entity.1.clone().unwrap_or(vec![]) {
            //TODO if shadow an entity arg, panic
            match item {
                ast::Decl::Const(id, _) => {
                    inner_consts.push(id.0.clone());
                }
                _ => { }
            }
        }

        // Extract defs.
        let mut inner_defs = vec![];
        for item in entity.1.clone().unwrap_or(vec![]) {
            //TODO if shadow an entity arg, panic
            match item {
                ast::Decl::Let(id, _, _) => {
                    inner_defs.push(id.0.clone());
                    //println!("obj def {:?}", id);
                }
                ast::Decl::Reg(id, _, _) => {
                    inner_defs.push(id.0.clone());
                    //println!("reg {:?}", id);
                }
                ast::Decl::Const(id, _) => {
                    inner_consts.push(id.0.clone());
                }
                _ => { }
            }
        }

        // Extract def muts.
        let mut inner_def_muts = vec![];
        for item in entity.1.clone().unwrap_or(vec![]) {
            //TODO if shadow an entity arg, panic
            match item {
                ast::Decl::Latch(id, _) => {
                    inner_def_muts.push(id.0.clone());
                    //println!("latch {:?}", id);
                }
                _ => { }
            }
        }

        // Check that all inner refs are declared.
        let mut checker = RefChecker::new();
        checker.valid.extend(entity.0.clone().iter().map(|x| (x.0).0.clone()));
        checker.valid.extend(inner_consts.clone());
        checker.valid.extend(inner_defs.clone());
        checker.valid.extend(inner_def_muts.clone());
        for decl in entity.1.clone().unwrap_or(vec![]) {
            decl.walk(&mut checker);
        }

        // Check that all static assignments are non-mutable.
        //TODO only include non-mutable arguments
        let mut checker = DefChecker::new();
        checker.valid.extend(entity.0.clone().iter().map(|x| (x.0).0.clone()));
        checker.valid.extend(inner_consts.clone());
        checker.valid.extend(inner_defs.clone());
        for decl in entity.1.clone().unwrap_or(vec![]) {
            decl.walk(&mut checker);
        }

        // Check that all latch assignments are mutable.
        //TODO only include mutable arguments
        let mut checker = DefMutChecker::new();
        checker.valid.extend(entity.0.clone().iter().map(|x| (x.0).0.clone()));
        checker.valid.extend(inner_consts.clone());
        checker.valid.extend(inner_def_muts.clone());
        for decl in entity.1.clone().unwrap_or(vec![]) {
            decl.walk(&mut checker);
        }
    }

    // TODO iterate through code, identify type decls. Then check AST for
    // incorrect references.
    //panic!("okay");
}

// Easy invocation of Verilog compilation.
pub fn compile(code: &str) -> String {
    // Removes comments.
    let re = Regex::new(r"(?m)//.*").unwrap();
    let code = re.replace_all(&code, "");

    let code = parse_results(&code, hdl_parser::parse_Code(&code));
    typecheck(&code);

    // Collect into types list.
    let mut types = TypeCollector::new();
    code.walk(&mut types);
    types.validate();

    // Convert typeset to code.
    types.to_verilog(&Default::default())
}