//! Instantiations, bind directives, gates, drive strengths and delays. use super::*; #[test] fn instantiation_forms() { parse_ok( "module top; sub #(.W(8), .D(16)) u_sub (.clk(clk), .out(out), .unused()); sub2 u2 (a, b, c); and g1 (o, a, b); mem #(8) banks [3:0] (.*); endmodule", ); } #[test] fn bind_directive_forms() { parse_ok( "module top; bind target_mod chk_mod u_chk (.a(x), .b(y)); bind i_inst chk_mod #(.W(8)) u_chk2 (.*); bind top.u_dut.u_core chk_mod u_chk3 (.clk_i, .rst_ni); bind fifo_v3 : i_a, i_b fifo_props u_props (.*); endmodule", ); // Also at top level and inside a generate block. parse_ok("bind cpu cpu_assert u_a (.*);\n"); parse_ok( "module m; generate if (E) begin : g bind sub chk_mod u (.*); end endgenerate endmodule", ); } #[test] fn a_bind_directive_wraps_an_ordinary_instantiation() { let file = parse_ok("module m;\n bind top.u_dut : u_a, u_b chk #(.W(8)) u (.a(x));\nendmodule\n"); let m = module_of(&file); let ItemKind::Bind(b) = &m.items[0].kind else { panic!("expected a bind directive") }; assert_eq!(b.target_path(), "top.u_dut"); assert_eq!(b.instances.iter().map(|n| n.text.as_str()).collect::>(), ["u_a", "u_b"]); assert_eq!(b.inst.module, "chk"); assert_eq!(b.inst.params.as_ref().map(Vec::len), Some(1)); assert_eq!(b.inst.instances[0].name, "u"); } #[test] fn the_comments_around_a_binds_head_are_kept() { // QA round-1: the target is a bare string, so these two positions had // no node to attach to and the comments drifted onto the instance. let file = parse_ok("module m;\n bind /*c1*/ dut /*c2*/ chk u (.*);\nendmodule\n"); let ItemKind::Bind(b) = &module_of(&file).items[0].kind else { panic!("expected a bind directive") }; assert_eq!(texts(&b.target_comments), ["/*c1*/"]); assert_eq!(texts(&b.module_comments), ["/*c2*/"]); } #[test] fn an_indexed_bind_target_parses_as_path_segments() { // `docs/coverage_gaps.md` M6: binding SVA into a generate array is // the shape formal setups write, and until TODO §26 the path scan // stopped at the `[` and the file failed with `expected ';'`. let file = parse_ok("module m;\n bind top.gen[0].u_core chk c (.clk(clk));\nendmodule\n"); let ItemKind::Bind(b) = &module_of(&file).items[0].kind else { panic!("expected a bind directive") }; let names: Vec<&str> = b.target.iter().map(|s| s.name.as_str()).collect(); assert_eq!(names, ["top", "gen", "u_core"]); assert!(b.target[0].index.is_none()); assert!(matches!( b.target[1].index.as_ref().map(|e| &e.kind), Some(ExprKind::Number(n)) if n == "0" )); assert!(b.target[2].index.is_none()); assert_eq!(b.target_path(), "top.gen.u_core"); assert_eq!(b.inst.module, "chk"); // A non-constant selector is an ordinary expression, and a bare // indexed head needs no path at all. let file = parse_ok("module m;\n bind arr[N-1] chk c (.*);\nendmodule\n"); let ItemKind::Bind(b) = &module_of(&file).items[0].kind else { panic!("expected a bind directive") }; assert_eq!(b.target.len(), 1); assert!(matches!(b.target[0].index.as_ref().map(|e| &e.kind), Some(ExprKind::Binary { .. }))); } #[test] fn an_escaped_bind_path_segment_keeps_its_terminator() { // §2.9-21's rule for `::` chains, applied to the `.` ones: whitespace // is an escaped identifier's only terminator, so the segments are // kept apart rather than glued. let file = parse_ok("module m;\n bind \\my-inst .u_core chk c (.*);\nendmodule\n"); let ItemKind::Bind(b) = &module_of(&file).items[0].kind else { panic!("expected a bind directive") }; assert_eq!(b.target[0].name, "\\my-inst"); assert_eq!(b.target_path(), "\\my-inst .u_core"); // The `: instance, list` glues its paths into one name, so it carries // the same rule through `push_path_segment`. let file = parse_ok("module m;\n bind d : \\my-inst .u_core chk c (.*);\nendmodule\n"); let ItemKind::Bind(b) = &module_of(&file).items[0].kind else { panic!("expected a bind directive") }; assert_eq!(b.instances[0].text, "\\my-inst .u_core"); } #[test] fn bind_is_still_a_usable_identifier() { // `bind` is not in the lexer's keyword table, so a method called // `bind` — and a variable declared with a type of that name — keep // parsing exactly as they did before the directive was supported. parse_ok("module m;\n initial begin\n obj.bind();\n x = obj.bind;\n end\nendmodule\n"); parse_ok("module m;\n bind x;\n bind y = 1;\nendmodule\n"); // And the shapes the `[…]` scan could have swallowed: a packed // dimension on the type, and an unpacked one on the declarator. parse_ok("module m;\n bind [3:0] x;\n bind y [7:0];\nendmodule\n"); } #[test] fn a_gate_may_carry_a_drive_strength() { // OpenTitan hw/ip/spi_host/dv/tb.sv:101 — IEEE 1800 §28.3's // single-strength `pullup_strength`, beside the ordinary pair. let f = parse_ok( "module m;\n pullup (weak1) pd (x);\n and (strong0, strong1) g (o, a, b);\n buf b1 ((a & b), c);\nendmodule\n", ); let items = &module_of(&f).items; let ItemKind::GateInstantiation(pull) = &items[0].kind else { panic!("expected a gate") }; let s = pull.strength.as_ref().expect("the single strength was parsed"); assert_eq!((s.first.as_str(), s.second.as_deref()), ("weak1", None)); let ItemKind::GateInstantiation(and) = &items[1].kind else { panic!("expected a gate") }; let s = and.strength.as_ref().expect("the strength pair was parsed"); assert_eq!((s.first.as_str(), s.second.as_deref()), ("strong0", Some("strong1"))); // A `(` that does not open a strength still opens an argument list. let ItemKind::GateInstantiation(buf) = &items[2].kind else { panic!("expected a gate") }; assert!(buf.strength.is_none()); assert_eq!(buf.instances[0].args.len(), 2); } #[test] fn a_drive_strength_is_only_read_where_a_strength_keyword_follows_the_paren() { // corpus/ibex/vendor/lowrisc_ip/ip/prim_generic/rtl/prim_usb_diff_rx.sv:39. let f = parse_ok("module m;\n assign (weak0, pull1) a = b;\n assign c = d;\nendmodule\n"); let items = &module_of(&f).items; let ItemKind::ContAssign(a) = &items[0].kind else { panic!("expected an assign") }; let s = a.strength.as_ref().expect("the strength was parsed"); assert_eq!((s.first.as_str(), s.second.as_deref()), ("weak0", Some("pull1"))); let ItemKind::ContAssign(b) = &items[1].kind else { panic!("expected an assign") }; assert!(b.strength.is_none()); } #[test] fn an_attribute_may_precede_a_named_port_connection() { // corpus/common_cells/src/cdc_fifo_gray.sv:140. let f = parse_ok("module m;\n sub u (\n (* async *) .a(x),\n .b(y)\n );\nendmodule\n"); let ItemKind::Instantiation(inst) = &module_of(&f).items[0].kind else { panic!("expected an instantiation") }; let Connections::Named(conns) = &inst.instances[0].conns else { panic!("expected named connections") }; let conns = oks(conns); assert_eq!(conns[0].meta.attrs.len(), 1); assert_eq!(conns[0].meta.attrs[0].text, "(* async *)"); assert_eq!(conns[0].name, "a"); assert!(conns[1].meta.attrs.is_empty()); } #[test] fn a_gate_takes_a_multi_value_delay_list() { // hw/top_earlgrey/ip/ast/rtl/aon_osc.sv:41. let f = parse_ok( "module m;\n buf #(RDLY, 0) b0 (o, a);\n and #(1, 2, 3) g (o, a, b);\n \ buf #(5) b1 (o, a);\n buf #5 b2 (o, a);\n buf b3 (o, a);\nendmodule\n", ); let m = module_of(&f); let lens: Vec = m .items .iter() .map(|i| match &i.kind { ItemKind::GateInstantiation(g) => g.delay.len(), other => panic!("expected a gate, got {other:?}"), }) .collect(); assert_eq!(lens, [2, 3, 1, 1, 0]); // The one-value parenthesized spelling keeps its `Paren` node, which // is what makes `#(5)` print back as `#(5)` and not as `#5`. let ItemKind::GateInstantiation(g) = &m.items[2].kind else { panic!("expected a gate") }; assert!(matches!(g.delay[0].expr.kind, ExprKind::Paren { .. })); } #[test] fn a_multi_value_delay_is_refused_where_only_a_gate_may_write_one() { // A net declaration and a continuous assignment take one delay; the // message says which construct the list belongs to. assert!(parse_err("module m;\n wire #(1, 2) w;\nendmodule\n") .contains("only allowed on a gate instantiation")); assert!(parse_err("module m;\n assign #(1, 2) y = x;\nendmodule\n") .contains("only allowed on a gate instantiation")); } #[test] fn a_bind_instance_may_be_a_hierarchical_path() { // hw/ip/entropy_src/dv/tb/tb.sv:94. let f = parse_ok("bind prim_packer_fifo : dut.u_core.u_fifo fifo_if #(.P(1)) u (.clk_i);\n"); let ItemKind::Bind(b) = &f.items[0].kind else { panic!("expected a bind") }; assert_eq!(b.target_path(), "prim_packer_fifo"); assert_eq!( b.instances.iter().map(|i| i.text.as_str()).collect::>(), ["dut.u_core.u_fifo"] ); // A bare name and a path may share one list. let f = parse_ok("bind foo : a.b, plain bar u (.x);\n"); let ItemKind::Bind(b) = &f.items[0].kind else { panic!("expected a bind") }; assert_eq!(b.instances.iter().map(|i| i.text.as_str()).collect::>(), ["a.b", "plain"]); } #[test] fn an_inline_aggregate_is_a_parameter_override_argument() { // TODO §45.3: a `parameter type` defaulting to an anonymous aggregate is // overridden with one, and `parse_type_ref` has read that body all along — // only `parse_expr_or_type`'s door was shut (`struct`/`union`/`enum` are // not `TYPE_KEYWORDS`), so the value read as an expression and failed. for src in [ "module m;\n sub #(.T(struct packed { logic a; })) u ();\nendmodule\n", "module m;\n sub #(.T(union packed { logic [7:0] a; })) u ();\nendmodule\n", "module m;\n sub #(.T(enum logic [1:0] {A, B})) u ();\nendmodule\n", "module m;\n sub #(.T(struct packed { /* why */ logic a; })) u ();\nendmodule\n", ] { let f = parse_ok(src); let ItemKind::Instantiation(i) = &module_of(&f).items[0].kind else { panic!("expected an instantiation") }; let params = i.params.as_ref().expect("an override list"); let value = oks(params)[0].expr.as_ref().expect("a value"); assert!(matches!(value.kind, ExprKind::Type(_)), "{src:?} read as {:?}", value.kind); // Strict and tolerant must agree: the tolerant parser reports nothing // where the strict one succeeds. let (_, diags) = crate::parse_source_tolerant(src); assert!(diags.is_empty(), "{src:?} left {diags:?}"); } } #[test] fn an_ordered_parameter_override_takes_an_inline_aggregate_too() { // The bare-expression arm of `parse_connection` shares `parse_expr_or_type` // with the named one, so both doors opened at once. let f = parse_ok("module m;\n sub #(struct packed { logic a; }) u ();\nendmodule\n"); let ItemKind::Instantiation(i) = &module_of(&f).items[0].kind else { panic!("expected an instantiation") }; let params = i.params.as_ref().expect("an override list"); assert!(matches!(oks(params)[0].expr.as_ref().expect("a value").kind, ExprKind::Type(_))); }