veryl-analyzer 0.21.0

A modern hardware description language
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use super::*;

fn assert_interface_function_comb_loop(code: &str, expected: bool) {
    let errors = analyze(code);
    let detected = errors
        .iter()
        .any(|error| matches!(error, AnalyzerError::CombinationalLoop { .. }));
    assert_eq!(detected, expected, "{errors:#?}");
}

#[test]
fn comb_loop_interface_function_read_detects_member_feedback() {
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var value: logic;
            function read () -> logic {
                return value;
            }
        }
        module Top {
            inst bus: Bus;
            assign bus.value = bus.read();
        }
        "#,
        true,
    );
}

fn imported_interface_read_code(top_assignments: &str) -> String {
    format!(
        r#"
        interface Bus {{
            var observed : logic;
            var unrelated: logic;
            function read_observed () -> logic {{
                return observed;
            }}
            modport monitor {{
                read_observed: import,
            }}
        }}
        module Observer (
            bus: modport Bus::monitor,
            o  : output logic,
        ) {{
            assign o = bus.read_observed();
        }}
        module Top {{
            inst bus: Bus;
            var observed: logic;
            inst observer: Observer (
                bus: bus,
                o  : observed,
            );
            {top_assignments}
        }}
        "#
    )
}

#[test]
fn comb_loop_imported_interface_read_detects_member_feedback() {
    assert_interface_function_comb_loop(
        &imported_interface_read_code("assign bus.observed = observed; assign bus.unrelated = 0;"),
        true,
    );
}

#[test]
fn comb_loop_imported_interface_read_keeps_unrelated_member_independent() {
    assert_interface_function_comb_loop(
        &imported_interface_read_code("assign bus.observed = 0; assign bus.unrelated = observed;"),
        false,
    );
}

fn imported_interface_write_code(writer_input: &str) -> String {
    format!(
        r#"
        interface Bus {{
            var written  : logic;
            var unrelated: logic;
            function write_written (value: input logic) {{
                written = value;
            }}
            modport target {{
                write_written: import,
            }}
        }}
        module Writer (
            i  : input logic,
            bus: modport Bus::target,
        ) {{
            always_comb {{
                bus.write_written(i);
            }}
        }}
        module Top {{
            inst bus: Bus;
            inst writer: Writer (
                i  : {writer_input},
                bus: bus,
            );
            assign bus.unrelated = 0;
        }}
        "#
    )
}

#[test]
fn comb_loop_imported_interface_write_detects_member_feedback() {
    assert_interface_function_comb_loop(&imported_interface_write_code("bus.written"), true);
}

#[test]
fn comb_loop_imported_interface_write_keeps_unrelated_member_independent() {
    assert_interface_function_comb_loop(&imported_interface_write_code("bus.unrelated"), false);
}

const EXTERNAL_INTERFACE_API: &str = r#"
    interface Bus {
        var value: logic;
        function get () -> logic {
            return value;
        }
        function put (next: input logic) {
            value = next;
        }
        modport reader {
            get: import,
        }
        modport writer {
            put: import,
        }
    }
"#;

fn connected_interface_function_code(top_declarations: &str) -> String {
    format!(
        r#"
        interface Bus {{
            var forward : logic;
            var backward: logic;
            function get_forward () -> logic {{
                return forward;
            }}
            function put_backward (value: input logic) {{
                backward = value;
            }}
            modport source {{
                forward     : output,
                backward    : input,
                get_forward : import,
                put_backward: import,
            }}
            modport sink {{
                get_forward : import,
                put_backward: import,
                ..converse(source)
            }}
        }}
        module Top {{
            inst source: Bus;
            inst sink  : Bus;
            connect source.source <> sink.sink;
            {top_declarations}
        }}
        "#
    )
}

#[test]
fn comb_loop_connect_detects_interface_function_member_read_feedback() {
    assert_interface_function_comb_loop(
        &connected_interface_function_code(
            r#"assign sink.forward = source.get_forward();
            assign source.backward = 0;"#,
        ),
        true,
    );
}

#[test]
fn comb_loop_connect_keeps_interface_function_member_read_one_way() {
    assert_interface_function_comb_loop(
        &connected_interface_function_code(
            r#"var observed: logic;
            assign sink.forward = source.backward;
            assign source.backward = 0;
            assign observed = source.get_forward();"#,
        ),
        false,
    );
}

#[test]
fn comb_loop_connect_detects_interface_function_member_write_feedback() {
    assert_interface_function_comb_loop(
        &connected_interface_function_code(
            r#"always_comb {
                source.put_backward(sink.forward);
            }
            assign sink.forward = sink.backward;"#,
        ),
        true,
    );
}

#[test]
fn comb_loop_connect_keeps_interface_function_member_write_one_way() {
    assert_interface_function_comb_loop(
        &connected_interface_function_code(
            r#"always_comb {
                source.put_backward(sink.forward);
            }
            assign sink.forward = 0;"#,
        ),
        false,
    );
}

#[test]
fn comb_loop_external_interface_get_to_put_detects_same_receiver_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst bus: Bus;
            always_comb {{
                bus.put(bus.get());
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
#[ignore = "comb-loop follow-up: false negative; disjoint interface member writes require positional procedure SSA"]
fn comb_loop_external_interface_disjoint_followup_write_preserves_same_bit_feedback() {
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var value: logic[2];
            function get0 () -> logic { return value[0]; }
            function put0 (next: input logic) { value[0] = next; }
            function put1 (next: input logic) { value[1] = next; }
        }
        module Top {
            inst bus: Bus;
            always_comb {
                bus.put0(bus.get0());
                bus.put1(0);
            }
        }
        "#,
        true,
    );
}

#[test]
fn comb_loop_external_interface_get_to_put_detects_cross_receiver_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst first : Bus;
            inst second: Bus;
            always_comb {{
                first.put(second.get());
                second.put(first.get());
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
fn comb_loop_external_interface_get_to_put_keeps_one_way_receivers_loop_free() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst source     : Bus;
            inst destination: Bus;
            always_comb {{
                source.put(0);
                destination.put(source.get());
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, false);
}

#[test]
fn comb_loop_external_interface_read_preserves_receiver_identity() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst source     : Bus;
            inst destination: Bus;
            assign source.value      = 0;
            assign destination.value = source.get();
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, false);
}

#[test]
fn comb_loop_interface_function_array_detects_same_element_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst bus: Bus[2];
            always_comb {{
                bus[0].put(bus[0].get());
                bus[1].put(0);
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
fn comb_loop_interface_function_array_keeps_one_way_elements_loop_free() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst bus: Bus[2];
            always_comb {{
                bus[0].put(0);
                bus[1].put(bus[0].get());
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, false);
}

#[test]
fn comb_loop_interface_function_array_detects_cross_element_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst bus: Bus[2];
            always_comb {{
                bus[0].put(bus[1].get());
                bus[1].put(bus[0].get());
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
fn comb_loop_external_interface_function_control_detects_member_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst bus: Bus;
            always_comb {{
                if bus.get() {{
                    bus.value = 0;
                }} else {{
                    bus.value = 1;
                }}
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
fn comb_loop_external_interface_function_instance_actual_detects_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Pass (
            i: input  logic,
            o: output logic,
        ) {{
            assign o = i;
        }}
        module Top {{
            inst bus: Bus;
            inst pass: Pass (
                i: bus.get(),
                o: bus.value,
            );
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
fn comb_loop_modport_formal_get_to_put_detects_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Copy (
            source     : modport Bus::reader,
            destination: modport Bus::writer,
        ) {{
            always_comb {{
                destination.put(source.get());
            }}
        }}
        module Top {{
            inst source     : Bus;
            inst destination: Bus;
            inst copy: Copy (
                source     : source,
                destination: destination,
            );
            assign source.value = destination.value;
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, true);
}

#[test]
fn comb_loop_interface_function_output_detects_captured_member_feedback() {
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var value: logic;
            function sample (copy: output logic) -> logic {
                copy = value;
                return value;
            }
        }
        module Top {
            inst bus: Bus;
            var copy      : logic;
            var discarded : logic;
            always_comb {
                discarded = bus.sample(copy);
                bus.value = copy;
            }
        }
        "#,
        true,
    );
}

#[test]
fn comb_loop_later_member_write_kills_external_interface_call_feedback() {
    let code = format!(
        r#"
        {EXTERNAL_INTERFACE_API}
        module Top {{
            inst bus: Bus;
            always_comb {{
                bus.put(bus.get());
                bus.value = 0;
            }}
        }}
        "#
    );
    assert_interface_function_comb_loop(&code, false);
}

#[test]
fn comb_loop_uncalled_interface_function_adds_no_member_dependency() {
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var source     : logic;
            var destination: logic;
            function transfer () {
                destination = source;
            }
        }
        module Top {
            inst bus: Bus;
            assign bus.source = bus.destination;
        }
        "#,
        false,
    );
}

#[test]
fn comb_loop_mixed_interface_function_detects_inherited_member_feedback() {
    assert_interface_function_comb_loop(
        r#"
        interface BaseBus {
            var value: logic;
            function get () -> logic {
                return value;
            }
        }
        interface Bus {
            mixin BaseBus;
        }
        module Top {
            inst bus: Bus;
            assign bus.value = bus.get();
        }
        "#,
        true,
    );
}

#[test]
fn comb_loop_nested_interface_function_detects_transitive_member_read() {
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var value: logic;
            function raw () -> logic {
                return value;
            }
            function wrapped () -> logic {
                return raw();
            }
        }
        module Top {
            inst bus: Bus;
            assign bus.value = bus.wrapped();
        }
        "#,
        true,
    );
}

fn specialized_interface_function_code(enabled: bool) -> String {
    format!(
        r#"
        interface Bus::<ENABLED: bbool> {{
            var value: logic;
            function get () -> logic {{
                if ENABLED {{
                    return value;
                }} else {{
                    return 0;
                }}
            }}
        }}
        module Top {{
            inst bus: Bus::<{enabled}>;
            assign bus.value = bus.get();
        }}
        "#
    )
}

#[test]
fn comb_loop_enabled_interface_function_specialization_reads_member() {
    assert_interface_function_comb_loop(&specialized_interface_function_code(true), true);
}

#[test]
fn comb_loop_disabled_interface_function_specialization_ignores_member() {
    assert_interface_function_comb_loop(&specialized_interface_function_code(false), false);
}

#[test]
#[ignore = "whole dependencies widen a bit-specific child output to every actual output bit"]
fn comb_loop_false_positive_imported_interface_function_widens_output_region() {
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var a: logic;
            var b: logic;
            function choose (sel: input logic) -> logic<2> {
                return if sel ? {a, 1'b0} : {1'b0, b};
            }
            modport monitor {
                choose: import,
            }
        }
        module Observer (
            bus: modport Bus::monitor,
            sel: input  logic,
            o  : output logic<2>,
        ) {
            assign o = bus.choose(sel);
        }
        module Top (
            sel: input logic,
        ) {
            inst bus: Bus;
            var passed: logic<2>;
            inst observer: Observer (
                bus: bus,
                sel: sel,
                o  : passed,
            );
            assign bus.b = passed[1];
            assign bus.a = passed[0];
        }
        "#,
        false,
    );
}

#[test]
fn comb_loop_imported_interface_read_keeps_an_independent_loop_in_the_same_process() {
    // Why this case exists: if the procedure context cannot resolve the
    // interface member, the whole comb declaration is dropped -- taking the
    // unrelated, provable loop written next to it with it.
    assert_interface_function_comb_loop(
        r#"
        interface Bus {
            var a: logic;
            function f () -> logic {
                return a;
            }
            modport port {
                f: import,
            }
        }
        module Sub (
            bus: modport Bus::port,
            o  : output logic,
        ) {
            var p: logic;
            var q: logic;
            always_comb {
                p = q | bus.f();
                q = p;
                o = q;
            }
        }
        module Top {
            inst bus: Bus;
            var w: logic;
            inst sub: Sub (bus: bus, o: w);
        }
        "#,
        true,
    );
}

fn interface_array_receiver_code(element: usize) -> String {
    format!(
        r#"
        interface Bus {{
            var a: logic;
            var b: logic;
            function f () {{
                a = b;
            }}
            function g () {{
                b = a;
            }}
            modport port {{
                f: import,
                g: import,
            }}
        }}
        module Sub (
            bus: modport Bus::port[2],
        ) {{
            always_comb {{
                bus[{element}].f();
            }}
            always_comb {{
                bus[{element}].g();
            }}
        }}
        module Top {{
            inst bus: Bus[2];
            inst sub: Sub (bus: bus);
        }}
        "#
    )
}

#[test]
fn comb_loop_interface_array_receiver_detects_feedback_in_element_zero() {
    assert_interface_function_comb_loop(&interface_array_receiver_code(0), true);
}

#[test]
fn comb_loop_interface_array_receiver_detects_feedback_in_a_later_element() {
    // Why this case exists: element zero hides a receiver-index mismatch
    // between the bit-precise paths and `read_keys`/`write_keys`, because
    // every coordinate happens to be 0 there.
    assert_interface_function_comb_loop(&interface_array_receiver_code(1), true);
}