Add bind directives
bind <target> [: inst, ...] <module> [#(...)] <inst> (...); reusing the instantiation AST and layout wholesale. Coverage loop round 1, cluster 3: 671 -> 699 / 820 (81.8% -> 85.2%). Feature README: docs/features/bind/README.md Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
8007a71932
commit
f7c66c79bf
9 files changed
+707
-38
No files matched your search
@@ -1054,6 +1054,7 @@ impl<'a> Parser<'a> {
|
||||
match t.kind {
|
||||
TokenKind::Directive => Ok(ItemKind::Directive(self.take_raw_text())),
|
||||
TokenKind::Keyword => self.parse_keyword_item(),
|
||||
TokenKind::Ident if self.at_bind_directive() => self.parse_bind().map(ItemKind::Bind),
|
||||
TokenKind::Ident => self.parse_ident_item(),
|
||||
TokenKind::SystemIdent if ELAB_TASKS.contains(&t.text.as_str()) => {
|
||||
let call = self.parse_expr()?;
|
||||
@@ -2135,6 +2136,58 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// True when the cursor opens a `bind` directive (IEEE 1800 §23.11).
|
||||
///
|
||||
/// `bind` is deliberately *not* in the lexer's keyword table — the same
|
||||
/// call vfmt makes for `new`/`this`/`super` — so that `obj.bind()` keeps
|
||||
/// parsing as the ordinary method call it is in existing testbenches.
|
||||
/// A directive is recognized from its shape instead: the word `bind`,
|
||||
/// then a target scope (a name, possibly a `.`-separated hierarchical
|
||||
/// path), then either the `:` of an instance list or the module name of
|
||||
/// the instantiation. A declaration of a variable called `bind` reaches
|
||||
/// `;`, `,`, `=` or a dimension there, so it never matches.
|
||||
fn at_bind_directive(&self) -> bool {
|
||||
let t = self.cur();
|
||||
if t.kind != TokenKind::Ident || t.text != "bind" {
|
||||
return false;
|
||||
}
|
||||
let mut p = self.pos + 1;
|
||||
if !self.tokens.get(p).is_some_and(|t| t.kind == TokenKind::Ident) {
|
||||
return false;
|
||||
}
|
||||
p += 1;
|
||||
while self.tokens.get(p).is_some_and(|t| t.is_sym("."))
|
||||
&& self.tokens.get(p + 1).is_some_and(|t| t.kind == TokenKind::Ident)
|
||||
{
|
||||
p += 2;
|
||||
}
|
||||
self.tokens.get(p).is_some_and(|t| t.is_sym(":") || t.kind == TokenKind::Ident)
|
||||
}
|
||||
|
||||
/// `bind <target> [: <inst>, ...] <module> [#(...)] <name> (...);`
|
||||
fn parse_bind(&mut self) -> PResult<BindDirective> {
|
||||
self.bump(); // `bind`
|
||||
let start = self.here();
|
||||
let mut target = self.expect_ident()?;
|
||||
while self.cur().is_sym(".") {
|
||||
self.bump();
|
||||
target.push('.');
|
||||
target.push_str(&self.expect_ident()?);
|
||||
}
|
||||
let target_span = self.span_from(start);
|
||||
let mut instances = Vec::new();
|
||||
if self.eat_sym(":") {
|
||||
loop {
|
||||
instances.push(self.expect_name()?);
|
||||
if !self.eat_sym(",") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let inst = self.parse_instantiation()?;
|
||||
Ok(BindDirective { target, target_span, instances, inst })
|
||||
}
|
||||
|
||||
fn parse_instantiation(&mut self) -> PResult<Instantiation> {
|
||||
let module_start = self.here();
|
||||
let mut module = self.expect_ident()?;
|
||||
@@ -3884,6 +3937,51 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bind_directive_forms() {
|
||||
parse_ok(
|
||||
"module top;
|
||||
bind target_mod checker u_chk (.a(x), .b(y));
|
||||
bind i_inst checker #(.W(8)) u_chk2 (.*);
|
||||
bind top.u_dut.u_core checker 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 checker 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, "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 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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expressions() {
|
||||
parse_ok(
|
||||
|
||||
Reference in new issue
Block a user