Every tracked file that can carry a comment (880 of them) now opens with "Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with "Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com", in the file's own comment syntax. No behaviour change: the diff outside the version bump and changelogs is comment lines only. - tools/stamp_author.py: writes the lines; idempotent, `--check` lists what is missing. Headers go under a shebang and under Markdown front matter. - Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the LICENSE files (already carry the copyright line). - tools/linux_check.sh: --help reads its header from line 3, now that the author line sits under the shebang. - tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
343 lines
15 KiB
Rust
343 lines
15 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! Hierarchical paths rooted at a top-level module (TODO §65.4, IEEE 1800
|
|
//! §23.6): `tb.dut.g_sw[0].buf_q` written in a file that does not declare
|
|
//! `tb`, and `$root.tb.…`. The head is a candidate for the workspace's unit
|
|
//! table, every segment after it a candidate on the hierarchical surface of
|
|
//! the unit before it — and a local of the head's name still wins.
|
|
//! `docs/features/top-rooted-paths/README.md`.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use vlog_parser::parse_source_tolerant;
|
|
use vlog_semantic::{
|
|
analyze, resolve_external, ExternalTarget, MemberSite, Resolution, Semantics, Surface,
|
|
WorkspaceIndex,
|
|
};
|
|
|
|
fn sem(src: &str) -> Semantics {
|
|
let (file, _) = parse_source_tolerant(src);
|
|
analyze(&file, src)
|
|
}
|
|
|
|
/// Offset of the `n`th (0-based) occurrence of `needle`, plus `skip` bytes.
|
|
fn at(src: &str, needle: &str, n: usize, skip: usize) -> u32 {
|
|
(src.match_indices(needle).nth(n).expect("occurrence must exist").0 + skip) as u32
|
|
}
|
|
|
|
fn resolution(s: &Semantics, offset: u32) -> Resolution {
|
|
s.reference_at(offset).unwrap_or_else(|| panic!("no reference at {offset}")).resolution.clone()
|
|
}
|
|
|
|
fn candidate(paths: &[&str]) -> Resolution {
|
|
Resolution::ExternalCandidate {
|
|
paths: paths.iter().map(|p| p.to_string()).collect(),
|
|
surface: Surface::Hierarchical,
|
|
}
|
|
}
|
|
|
|
const DUT: &str = "module dut_a;
|
|
for (genvar i = 0; i < 2; i++) begin : g_sw
|
|
logic buf_q;
|
|
end
|
|
endmodule
|
|
";
|
|
const TB: &str = "module tb;
|
|
dut_a dut ();
|
|
endmodule
|
|
";
|
|
const PROBE: &str = "module probe (output logic o);
|
|
assign o = tb.dut.g_sw[0].buf_q;
|
|
endmodule
|
|
";
|
|
|
|
fn index(extra: &[(&str, &str)]) -> WorkspaceIndex {
|
|
let mut files = vec![("dut_a.sv", DUT), ("tb.sv", TB), ("probe.sv", PROBE)];
|
|
files.extend_from_slice(extra);
|
|
WorkspaceIndex::build(files.into_iter().map(|(n, t)| (PathBuf::from("/rtl").join(n), t)))
|
|
}
|
|
|
|
/// What the workspace answers for the reference at `offset`: the kind and
|
|
/// name of the one declaration, or a description of a non-answer.
|
|
fn external(s: &Semantics, ws: &WorkspaceIndex, offset: u32) -> String {
|
|
let id = s.reference_id_at(offset).expect("a reference there");
|
|
match resolve_external(s, ws).get(id) {
|
|
Some(ExternalTarget::Resolved(def)) => format!("{:?}", def.kind),
|
|
Some(ExternalTarget::Ambiguous(defs)) => format!("ambiguous {}", defs.len()),
|
|
Some(ExternalTarget::StillUnresolved) => "unresolved".to_string(),
|
|
None => "not a candidate".to_string(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_head_and_every_segment_are_candidates_rooted_at_the_head() {
|
|
let s = sem(PROBE);
|
|
assert_eq!(resolution(&s, at(PROBE, "tb.", 0, 0)), candidate(&["tb"]));
|
|
assert_eq!(resolution(&s, at(PROBE, "dut.", 0, 0)), candidate(&["tb::dut"]));
|
|
assert_eq!(resolution(&s, at(PROBE, "g_sw", 0, 0)), candidate(&["tb::dut::g_sw"]));
|
|
assert_eq!(resolution(&s, at(PROBE, "buf_q", 0, 0)), candidate(&["tb::dut::g_sw::buf_q"]));
|
|
}
|
|
|
|
#[test]
|
|
fn the_workspace_walks_the_path_from_the_top_module() {
|
|
let (ws, s) = (index(&[]), sem(PROBE));
|
|
assert_eq!(external(&s, &ws, at(PROBE, "tb.", 0, 0)), "Module");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "dut.", 0, 0)), "Instance");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "g_sw", 0, 0)), "Block");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "buf_q", 0, 0)), "Var");
|
|
}
|
|
|
|
#[test]
|
|
fn a_root_the_workspace_does_not_declare_stays_unresolved() {
|
|
let ws = WorkspaceIndex::build(
|
|
[("dut_a.sv", DUT), ("probe.sv", PROBE)]
|
|
.into_iter()
|
|
.map(|(n, t)| (PathBuf::from("/rtl").join(n), t)),
|
|
);
|
|
let s = sem(PROBE);
|
|
assert_eq!(external(&s, &ws, at(PROBE, "tb.", 0, 0)), "unresolved");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "g_sw", 0, 0)), "unresolved");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "buf_q", 0, 0)), "unresolved");
|
|
}
|
|
|
|
/// Seventy opentitan testbenches are each `module tb`: `tb.dut` then names
|
|
/// as many instances, and nothing after it can be told apart.
|
|
#[test]
|
|
fn two_top_modules_of_one_name_leave_the_path_unresolved() {
|
|
let other_tb = "module tb;\n dut_b dut ();\nendmodule\n";
|
|
let ws = index(&[("tb2.sv", other_tb)]);
|
|
let s = sem(PROBE);
|
|
assert_eq!(external(&s, &ws, at(PROBE, "tb.", 0, 0)), "ambiguous 2");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "dut.", 0, 0)), "ambiguous 2");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "g_sw", 0, 0)), "unresolved");
|
|
}
|
|
|
|
/// A package or class is not a root a `.` can follow, and a name declared as
|
|
/// a module in one file and a package in another is no root either.
|
|
#[test]
|
|
fn only_a_module_or_an_interface_is_a_root() {
|
|
let pkg = "package tb;\n logic dut;\nendpackage\n";
|
|
let ws = WorkspaceIndex::build(
|
|
[("dut_a.sv", DUT), ("tb_pkg.sv", pkg), ("probe.sv", PROBE)]
|
|
.into_iter()
|
|
.map(|(n, t)| (PathBuf::from("/rtl").join(n), t)),
|
|
);
|
|
let s = sem(PROBE);
|
|
assert_eq!(external(&s, &ws, at(PROBE, "tb.", 0, 0)), "unresolved");
|
|
let ws = index(&[("tb_pkg.sv", pkg)]);
|
|
assert_eq!(external(&s, &ws, at(PROBE, "tb.", 0, 0)), "unresolved", "module and package");
|
|
let iface = "interface tb;\n dut_a dut ();\nendinterface\n";
|
|
let ws = WorkspaceIndex::build(
|
|
[("dut_a.sv", DUT), ("tb_if.sv", iface), ("probe.sv", PROBE)]
|
|
.into_iter()
|
|
.map(|(n, t)| (PathBuf::from("/rtl").join(n), t)),
|
|
);
|
|
assert_eq!(external(&s, &ws, at(PROBE, "tb.", 0, 0)), "Interface");
|
|
assert_eq!(external(&s, &ws, at(PROBE, "buf_q", 0, 0)), "Var");
|
|
}
|
|
|
|
/// The LRM's upward name resolution finds a local first: a `logic tb` makes
|
|
/// `tb.dut` a select of a net this crate models no members of, and an
|
|
/// instance named `tb` roots the path in *its* module.
|
|
#[test]
|
|
fn a_local_of_the_heads_name_wins() {
|
|
let net = "module probe (output logic o);\n logic tb;\n assign o = tb.dut.g_sw[0].buf_q;\nendmodule\n";
|
|
let s = sem(net);
|
|
let local = s.symbol_at(at(net, "logic tb", 0, 6)).expect("the net");
|
|
assert_eq!(resolution(&s, at(net, "tb.", 0, 0)), Resolution::Resolved(local));
|
|
assert!(s.reference_at(at(net, "dut.", 0, 0)).is_none(), "a net has no members");
|
|
let inst =
|
|
"module probe (output logic o);\n other tb ();\n assign o = tb.dut.x;\nendmodule\n";
|
|
let s = sem(inst);
|
|
let local = s.symbol_at(at(inst, "other tb", 0, 6)).expect("the instance");
|
|
assert_eq!(resolution(&s, at(inst, "tb.", 0, 0)), Resolution::Resolved(local));
|
|
assert_eq!(resolution(&s, at(inst, "dut.", 0, 0)), candidate(&["other::dut"]));
|
|
}
|
|
|
|
/// A module this file declares is found lexically, as it always was.
|
|
#[test]
|
|
fn a_top_module_declared_in_the_same_file_resolves_in_the_file() {
|
|
let src = "module tb;\n logic sig;\nendmodule\nmodule probe (output logic o);\n assign o = tb.sig;\nendmodule\n";
|
|
let s = sem(src);
|
|
let tb = s.symbol_at(at(src, "module tb", 0, 7)).expect("tb");
|
|
let sig = s.symbol_at(at(src, "logic sig", 0, 6)).expect("sig");
|
|
assert_eq!(resolution(&s, at(src, "tb.sig", 0, 0)), Resolution::Resolved(tb));
|
|
assert_eq!(resolution(&s, at(src, "tb.sig", 0, 3)), Resolution::Resolved(sig));
|
|
}
|
|
|
|
/// QA F2: an import that could supply the head makes it the imported name
|
|
/// (a local, by the LRM) whether or not the workspace can say so — doubt, so
|
|
/// nothing is rooted: the head keeps the import's candidate and no segment
|
|
/// after it is a reference.
|
|
#[test]
|
|
fn an_import_in_scope_wins_and_roots_nothing() {
|
|
let src = "module probe (output logic o);\n import p::*;\n assign o = tb.dut.x;\nendmodule\n";
|
|
let s = sem(src);
|
|
let imported = Resolution::ExternalCandidate {
|
|
paths: vec!["p::*".to_string()],
|
|
surface: Surface::Connection,
|
|
};
|
|
assert_eq!(resolution(&s, at(src, "tb.", 0, 0)), imported);
|
|
assert!(s.reference_at(at(src, "dut.", 0, 0)).is_none());
|
|
}
|
|
|
|
/// QA F4: a head written inside a class (any class: a member, inherited or
|
|
/// macro-declared, is never a path lucisv can prove), or in a scope where an
|
|
/// unexpanded macro stands as an item, roots nothing.
|
|
#[test]
|
|
fn a_head_inside_a_class_or_beside_a_macro_item_roots_nothing() {
|
|
for (src, head) in [
|
|
// A11: a class with no base, `cfg` declared by a macro.
|
|
("class env;\n `DECLARE_CFG\n task run(); if (cfg.en) $display(\"x\"); endtask\nendclass\n", "cfg."),
|
|
// D2: a class in a package writing a hierarchical path (illegal).
|
|
("package env_pkg;\n class seq;\n task body(); tb.dut.x = 1; endtask\n endclass\nendpackage\n", "tb."),
|
|
// A12: a module whose `st` comes from a macro.
|
|
("module user (output logic o);\n `DECL_STRUCT(st)\n assign o = st.a;\nendmodule\n", "st."),
|
|
// The macro one scope out still casts its doubt inward.
|
|
("module user (output logic o);\n `DECL_STRUCT(st)\n initial begin : b\n o = st.a;\n end\nendmodule\n", "st."),
|
|
] {
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, head, 0, 0)), Resolution::NotFound, "{src}");
|
|
let tail = at(src, head, 0, head.len());
|
|
assert!(s.reference_at(tail).is_none(), "{src}");
|
|
}
|
|
}
|
|
|
|
/// QA F1: a package sharing the root's name lends the path nothing — neither
|
|
/// the head nor any segment after it answers — and a package alone is never
|
|
/// a root (`pk.x` is illegal).
|
|
#[test]
|
|
fn a_package_never_lends_a_top_rooted_path_its_members() {
|
|
let pkg = "package tb;\n logic dut;\nendpackage\n";
|
|
let other = "module other;\n initial force tb.dut.x = 0;\nendmodule\n";
|
|
let dut = "module dut_a;\n logic x;\nendmodule\n";
|
|
let both = WorkspaceIndex::build(
|
|
[("dut_a.sv", dut), ("tb.sv", TB), ("tb_pkg.sv", pkg), ("other.sv", other)]
|
|
.into_iter()
|
|
.map(|(n, t)| (PathBuf::from("/rtl").join(n), t)),
|
|
);
|
|
let s = sem(other);
|
|
for needle in ["tb.", "dut.", ".x"] {
|
|
let skip = usize::from(needle.starts_with('.'));
|
|
assert_eq!(external(&s, &both, at(other, needle, 0, skip)), "unresolved", "{needle}");
|
|
}
|
|
let user = "module user (output logic o);\n assign o = pk.x;\nendmodule\n";
|
|
let only = WorkspaceIndex::build(
|
|
[("pk.sv", "package pk;\n logic x;\nendpackage\n"), ("user.sv", user)]
|
|
.into_iter()
|
|
.map(|(n, t)| (PathBuf::from("/rtl").join(n), t)),
|
|
);
|
|
let s = sem(user);
|
|
assert_eq!(external(&s, &only, at(user, "pk.", 0, 0)), "unresolved");
|
|
assert_eq!(external(&s, &only, at(user, ".x", 0, 1)), "unresolved");
|
|
}
|
|
|
|
/// Inside an extending class the base could supply the head, so it stays the
|
|
/// base's candidate and no path is rooted at a module.
|
|
#[test]
|
|
fn inside_an_extending_class_the_head_is_the_bases() {
|
|
let src = "class c extends base;\n function void f();\n cfg.dut.x = 1;\n endfunction\nendclass\n";
|
|
let s = sem(src);
|
|
assert!(matches!(resolution(&s, at(src, "cfg.", 0, 0)), Resolution::InheritedCandidate { .. }));
|
|
assert!(s.reference_at(at(src, "dut.", 0, 0)).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn dollar_root_names_the_top_module_and_skips_the_lexical_walk() {
|
|
let src = "module probe (output logic o);\n logic tb;\n assign o = $root.tb.dut.g_sw[0].buf_q;\nendmodule\n";
|
|
let s = sem(src);
|
|
// The local `tb` is not what `$root.tb` names.
|
|
assert_eq!(resolution(&s, at(src, "tb.dut", 0, 0)), candidate(&["tb"]));
|
|
assert_eq!(resolution(&s, at(src, "dut.", 0, 0)), candidate(&["tb::dut"]));
|
|
assert_eq!(resolution(&s, at(src, "buf_q", 0, 0)), candidate(&["tb::dut::g_sw::buf_q"]));
|
|
let ws = WorkspaceIndex::build(
|
|
[("dut_a.sv", DUT), ("tb.sv", TB), ("p.sv", src)]
|
|
.into_iter()
|
|
.map(|(n, t)| (PathBuf::from("/rtl").join(n), t)),
|
|
);
|
|
assert_eq!(external(&s, &ws, at(src, "tb.dut", 0, 0)), "Module");
|
|
assert_eq!(external(&s, &ws, at(src, "buf_q", 0, 0)), "Var");
|
|
}
|
|
|
|
#[test]
|
|
fn dollar_root_reaches_a_top_module_this_file_declares() {
|
|
let src = "module tb;\n logic sig;\nendmodule\nmodule probe (output logic o);\n logic tb;\n assign o = $root.tb.sig;\nendmodule\n";
|
|
let s = sem(src);
|
|
let tb = s.symbol_at(at(src, "module tb", 0, 7)).expect("tb");
|
|
let sig = s.symbol_at(at(src, "logic sig", 0, 6)).expect("sig");
|
|
assert_eq!(resolution(&s, at(src, "tb.sig", 0, 0)), Resolution::Resolved(tb));
|
|
assert_eq!(resolution(&s, at(src, "tb.sig", 0, 3)), Resolution::Resolved(sig));
|
|
}
|
|
|
|
/// The completion walk (`Semantics::member_site`) does not root an unknown
|
|
/// head at a top module — a popup after an unknown head answers nothing, so
|
|
/// the ordinary tiers run, until the language server's member list takes the
|
|
/// change knowingly (README, Handshakes). `$root.tb.` says what it is and is
|
|
/// rooted in both walks.
|
|
#[test]
|
|
fn the_completion_walk_roots_only_an_explicit_dollar_root() {
|
|
let s = sem(PROBE);
|
|
let scope = s.reference_at(at(PROBE, "tb.", 0, 0)).expect("tb").scope;
|
|
assert_eq!(s.member_site(scope, "tb", &["dut".to_string()]), None);
|
|
let rooted = s.member_site(scope, "$root", &["tb".to_string(), "dut".to_string()]);
|
|
assert_eq!(
|
|
rooted,
|
|
Some(MemberSite::Unit { path: "tb::dut".to_string(), surface: Surface::Hierarchical })
|
|
);
|
|
assert_eq!(s.member_site(scope, "$root", &[]), None);
|
|
assert_eq!(s.member_site(scope, "`MACRO", &[]), None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_system_name_or_a_macro_roots_nothing() {
|
|
let src = "module probe (output logic o);\n assign o = `TOP.dut.x;\n assign o = $unit.x;\nendmodule\n";
|
|
let s = sem(src);
|
|
assert!(s.reference_at(at(src, "dut.", 0, 0)).is_none());
|
|
assert!(s.reference_at(at(src, ".x;", 1, 1)).is_none());
|
|
}
|
|
|
|
/// QA R1: a macro standing where a declaration could stand — a statement in
|
|
/// a `begin`/`end`, a task or a function body, or an entry of the port list —
|
|
/// may declare the head, so nothing is rooted from that scope (or one inside
|
|
/// it).
|
|
#[test]
|
|
fn a_macro_where_a_declaration_could_stand_roots_nothing() {
|
|
for src in [
|
|
// E1
|
|
"module user;\n initial begin\n `DECL_LOCAL(st)\n st.a = 1;\n end\nendmodule\n",
|
|
// E2
|
|
"module user;\n task automatic t();\n `DECL_LOCAL(st)\n st.a = 1;\n endtask\nendmodule\n",
|
|
// E6
|
|
"module user;\n function automatic void f();\n `DECL_LOCAL(st)\n begin st.a = 1; end\n endfunction\nendmodule\n",
|
|
// E3, the parse is tolerant of the macro port
|
|
"module user (`MY_PORTS, output logic o);\n assign o = st.a;\nendmodule\n",
|
|
// E3, the macro after a port
|
|
"module user (input logic i, `MY_PORTS);\n initial st.a = i;\nendmodule\n",
|
|
] {
|
|
let s = sem(src);
|
|
let head = at(src, "st.a", 0, 0);
|
|
assert!(
|
|
matches!(s.reference_at(head).map(|r| &r.resolution), Some(Resolution::NotFound) | None),
|
|
"{src}"
|
|
);
|
|
assert!(s.reference_at(at(src, "st.a", 0, 3)).is_none(), "{src}");
|
|
}
|
|
}
|
|
|
|
/// …and the shapes that cannot declare the head keep rooting: a macro used
|
|
/// as a data type, and a macro in a *sibling* generate block (E7, E8).
|
|
#[test]
|
|
fn a_macro_that_cannot_declare_the_head_still_roots() {
|
|
for src in [
|
|
"module user;\n `MY_STRUCT_T other;\n initial st.a = 1;\nendmodule\n",
|
|
"module user;\n if (1) begin : g1\n `DECL_LOCAL(st)\n end\n if (1) begin : g2\n initial st.a = 1;\n end\nendmodule\n",
|
|
"module user;\n initial st.a = $bits(`WIDTH);\nendmodule\n",
|
|
] {
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "st.a", 0, 0)), candidate(&["st"]), "{src}");
|
|
assert_eq!(resolution(&s, at(src, "st.a", 0, 3)), candidate(&["st::a"]), "{src}");
|
|
}
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|