Decomposition, lane A: vlog_parser's parser.rs and ast.rs become twenty-seven modules, a pure move
TODO section 28, lane A (the file-size rule in CLAUDE.md).
parser.rs (8 786 production + 5 044 test lines) becomes parser/{mod,
cursor, recover, directive, split, verbatim, item, module, decl, tf,
instance, stmt, keyword_stmt, expr, primary, class, covergroup,
assertion, sva}.rs with the tests regrouped into parser/tests/ (14
files, 309 tests, same names and bodies); ast.rs (3 313) becomes
ast/{mod, module, decl, stmt, expr, class, covergroup, sva}.rs, every
type re-exported so vlog_parser::ast::X is unchanged. Nothing over 800
production lines; the two keyword dispatch tables kept whole and
documented as such, parse_keyword_stmt in a file of its own. Carved by
a line-range manifest that had to concatenate back to the original
byte for byte before writing; 149 methods and 18 items pub(super) as
the compiler demanded. Proof: per-binary test counts identical
(workspace 3 215 / 48), 4 807 corpus + opentitan files byte-identical
to the v0.33.0 binary, cargo doc item list unchanged, and a multiset +
signature census done by the dev, redone by QA, re-run by the lead.
docs/features/decomposition-parser/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
159f6a0bd8
commit
9b93f33ae3
45 files changed
+17999
-17241
No files matched your search
@@ -0,0 +1,219 @@
|
||||
//! 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::<Vec<_>>(), ["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")
|
||||
};
|
||||
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<usize> = 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].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::<Vec<_>>(),
|
||||
["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::<Vec<_>>(), ["a.b", "plain"]);
|
||||
}
|
||||
Reference in new issue
Block a user