vast 0.3.3

Verilog AST library
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use crate::v17::ast::*;
use std::rc::Rc;

impl Ty {
    pub fn new_int() -> Ty {
        Ty::Int
    }

    pub fn new_width(width: u64) -> Ty {
        assert!(width > 0, "Error: width must be greater than zero");
        Ty::Width(width)
    }

    pub fn width(&self) -> u64 {
        match self {
            Ty::Width(w) => *w,
            _ => panic!("Error: type does not support width"),
        }
    }
}

impl Port {
    pub fn new_input(name: &str, width: u64) -> Port {
        let ty = Ty::Width(width);
        let logic = Decl::Logic(name.to_string(), ty);
        Port::Input(logic)
    }

    pub fn new_input_int(name: &str) -> Port {
        let inp = Decl::Int(name.to_string(), Ty::Int);
        Port::Input(inp)
    }

    pub fn new_output(name: &str, width: u64) -> Port {
        let ty = Ty::Width(width);
        let logic = Decl::Logic(name.to_string(), ty);
        Port::Output(logic)
    }
}

impl CaseBranch {
    pub fn new<E>(cond: E) -> CaseBranch
    where
        E: Into<Expr>,
    {
        CaseBranch {
            cond: cond.into(),
            body: Vec::new(),
        }
    }

    pub fn add_seq<S>(&mut self, seq: S) -> &mut Self
    where
        S: Into<Sequential>,
    {
        self.body.push(seq.into());
        self
    }

    pub fn add_case(&mut self, case: Case) -> &mut Self {
        self.body.push(Sequential::new_case(case));
        self
    }

    pub fn body(&self) -> &Vec<Sequential> {
        &self.body
    }
}

impl CaseDefault {
    pub fn add_seq<S>(&mut self, seq: S) -> &mut Self
    where
        S: Into<Sequential>,
    {
        self.body.push(seq.into());
        self
    }

    pub fn body(&self) -> &Vec<Sequential> {
        &self.body
    }
}

impl Default for CaseDefault {
    fn default() -> CaseDefault {
        CaseDefault { body: Vec::new() }
    }
}

impl Case {
    pub fn new<E>(cond: E) -> Case
    where
        E: Into<Expr>,
    {
        Case {
            cond: cond.into(),
            branches: Vec::new(),
            default: None,
        }
    }

    pub fn add_branch(&mut self, branch: CaseBranch) -> &mut Self {
        self.branches.push(branch);
        self
    }

    pub fn set_default(&mut self, branch: CaseDefault) {
        self.default = Some(branch);
    }

    pub fn branches(&self) -> &Vec<CaseBranch> {
        &self.branches
    }

    pub fn default(&self) -> &CaseDefault {
        self.default
            .as_ref()
            .unwrap_or_else(|| panic!("Default branch has not been set"))
    }
}

impl Sequential {
    pub fn new_seqexpr<E>(expr: E) -> Sequential
    where
        E: Into<Expr>,
    {
        Sequential::SeqExpr(expr.into())
    }

    pub fn new_error(msg: &str) -> Sequential {
        Sequential::Error(msg.to_string())
    }

    pub fn new_display(msg: &str) -> Sequential {
        Sequential::Display(msg.to_string())
    }

    pub fn new_return<E>(expr: E) -> Sequential
    where
        E: Into<Expr>,
    {
        Sequential::Return(expr.into())
    }

    pub fn new_assert<E>(expr: E) -> Sequential
    where
        E: Into<Expr>,
    {
        Sequential::Assert(expr.into(), None)
    }

    pub fn new_assert_with_else<E, S>(expr: E, seq: S) -> Sequential
    where
        E: Into<Expr>,
        S: Into<Sequential>,
    {
        Sequential::Assert(expr.into(), Some(Rc::new(seq.into())))
    }

    pub fn new_blk_assign<L, R>(lexpr: L, rexpr: R) -> Sequential
    where
        L: Into<Expr>,
        R: Into<Expr>,
    {
        Sequential::SeqAssign(lexpr.into(), rexpr.into(), AssignTy::Blocking)
    }

    pub fn new_nonblk_assign<L, R>(lexpr: L, rexpr: R) -> Sequential
    where
        L: Into<Expr>,
        R: Into<Expr>,
    {
        Sequential::SeqAssign(lexpr.into(), rexpr.into(), AssignTy::NonBlocking)
    }

    pub fn new_case(case: Case) -> Sequential {
        Sequential::SeqCase(case)
    }

    pub fn new_call<E>(call: E) -> Sequential
    where
        E: Into<Expr>,
    {
        Sequential::Call(call.into())
    }
}

impl SequentialIfElse {
    pub fn new<E>(cond: E) -> Self
    where
        E: Into<Expr>,
    {
        SequentialIfElse {
            cond: Some(cond.into()),
            body: vec![],
            else_branch: None,
            unique: false,
        }
    }

    /// Marks the branches of this if-else chain with the `unique` annotation
    /// in Verilog.
    pub fn set_unique(&mut self) -> &mut Self {
        self.unique = true;
        self
    }

    /// Add a new statement in to the body of the true statement for this
    /// conditional block.
    pub fn add_seq<S>(&mut self, seq: S) -> &mut Self
    where
        S: Into<Sequential>,
    {
        self.body.push(seq.into());
        self
    }

    /// Set the alternate branch for this conditional.
    pub fn set_else<S>(&mut self, seq: S)
    where
        S: Into<Sequential>,
    {
        self.else_branch = Some(Rc::new(seq.into()));
    }
}

impl ParallelProcess {
    pub fn new_always_comb() -> Self {
        ParallelProcess {
            ty: ProcessTy::AlwaysComb,
            event: None,
            body: Vec::new(),
        }
    }

    pub fn new_always_ff() -> Self {
        ParallelProcess {
            ty: ProcessTy::AlwaysFF,
            event: None,
            body: Vec::new(),
        }
    }

    pub fn new_initial() -> Self {
        ParallelProcess {
            ty: ProcessTy::Initial,
            event: None,
            body: Vec::new(),
        }
    }

    pub fn new_final() -> Self {
        ParallelProcess {
            ty: ProcessTy::Final,
            event: None,
            body: Vec::new(),
        }
    }

    pub fn ty(&self) -> &ProcessTy {
        &self.ty
    }

    pub fn body(&self) -> &Vec<Sequential> {
        &self.body
    }

    pub fn event(&self) -> Option<&Sequential> {
        self.event.as_ref()
    }

    pub fn add_seq<S>(&mut self, seq: S) -> &mut Self
    where
        S: Into<Sequential>,
    {
        self.body.push(seq.into());
        self
    }

    pub fn add_case(&mut self, case: Case) -> &mut Self {
        self.body.push(Sequential::new_case(case));
        self
    }

    pub fn set_event<S>(&mut self, seq: S)
    where
        S: Into<Sequential>,
    {
        self.event = Some(seq.into())
    }
}

impl Parallel {
    pub fn new_inst(inst: Instance) -> Parallel {
        Parallel::Inst(inst)
    }

    pub fn new_process(par_process: ParallelProcess) -> Parallel {
        Parallel::Process(par_process)
    }
}

impl Stmt {
    pub fn new_parallel<P>(par: P) -> Stmt
    where
        P: Into<Parallel>,
    {
        Stmt::Parallel(par.into())
    }

    pub fn new_decl(decl: Decl) -> Stmt {
        Stmt::Decl(decl)
    }

    pub fn new_rawstr(s: String) -> Stmt {
        Stmt::RawStr(s)
    }
}

impl Function {
    pub fn new(name: &str, ret: Ty) -> Function {
        Function {
            ty: FunctionTy::Default,
            name: name.to_string(),
            ports: Vec::new(),
            decls: Vec::new(),
            body: Vec::new(),
            ret,
        }
    }

    pub fn ports(&self) -> &Vec<Port> {
        &self.ports
    }

    pub fn decls(&self) -> &Vec<Decl> {
        &self.decls
    }

    pub fn body(&self) -> &Vec<Sequential> {
        &self.body
    }

    pub fn ty(&self) -> &FunctionTy {
        &self.ty
    }

    pub fn add_input(&mut self, name: &str, width: u64) -> &mut Self {
        self.ports.push(Port::new_input(name, width));
        self
    }

    pub fn add_input_int(&mut self, name: &str) -> &mut Self {
        self.ports.push(Port::new_input_int(name));
        self
    }

    pub fn add_output(&mut self, name: &str, width: u64) -> &mut Self {
        self.ports.push(Port::new_output(name, width));
        self
    }

    pub fn add_logic(&mut self, name: &str, width: u64) -> &mut Self {
        self.decls.push(Decl::new_logic(name, width));
        self
    }

    pub fn add_stmt<S>(&mut self, stmt: S) -> &mut Self
    where
        S: Into<Sequential>,
    {
        self.body.push(stmt.into());
        self
    }

    pub fn set_return_type(&mut self, ret: Ty) {
        self.ret = ret;
    }

    pub fn export(&mut self) {
        self.ty = FunctionTy::Export;
    }

    pub fn import(&mut self) {
        self.ty = FunctionTy::Import;
    }
}

impl Decl {
    pub fn new_param_uint(name: &str, value: u32) -> Decl {
        Decl::Param(
            name.to_string(),
            Ty::new_int(),
            Expr::new_ulit_dec(32, &value.to_string()),
        )
    }

    pub fn new_logic<S>(name: S, width: u64) -> Decl
    where
        S: AsRef<str>,
    {
        Decl::Logic(name.as_ref().to_string(), Ty::new_width(width))
    }

    pub fn new_int<S>(name: S) -> Decl
    where
        S: AsRef<str>,
    {
        Decl::Int(name.as_ref().to_string(), Ty::Int)
    }

    pub fn new_func(func: Function) -> Decl {
        Decl::Func(func)
    }
}

impl Module {
    pub fn new(name: &str) -> Module {
        Module {
            name: name.to_string(),
            params: Vec::new(),
            ports: Vec::new(),
            body: Vec::new(),
            attr: Attribute::default(),
        }
    }

    pub fn name(&self) -> String {
        self.name.to_string()
    }

    pub fn body(&self) -> &Vec<Stmt> {
        &self.body
    }

    pub fn ports(&self) -> &Vec<Port> {
        &self.ports
    }

    pub fn params(&self) -> &Vec<Decl> {
        &self.params
    }

    pub fn attr(&self) -> &Attribute {
        &self.attr
    }

    pub fn add_input(&mut self, name: &str, width: u64) -> &mut Self {
        self.ports.push(Port::new_input(name, width));
        self
    }

    pub fn add_output(&mut self, name: &str, width: u64) -> &mut Self {
        self.ports.push(Port::new_output(name, width));
        self
    }

    pub fn add_decl(&mut self, decl: Decl) -> &mut Self {
        self.body.push(Stmt::new_decl(decl));
        self
    }

    pub fn add_function(&mut self, func: Function) -> &mut Self {
        self.body.push(Stmt::new_decl(Decl::new_func(func)));
        self
    }

    pub fn add_instance(&mut self, inst: Instance) -> &mut Self {
        self.body.push(Stmt::new_parallel(Parallel::new_inst(inst)));
        self
    }

    pub fn add_process(&mut self, proc: ParallelProcess) -> &mut Self {
        self.body
            .push(Stmt::new_parallel(Parallel::new_process(proc)));
        self
    }

    pub fn add_stmt(&mut self, stmt: Stmt) -> &mut Self {
        self.body.push(stmt);
        self
    }

    pub fn set_attr(&mut self, attr: Attribute) {
        self.attr = attr;
    }
}