Modport method prototypes, unnamed DPI formals, and the const ref comment gap
TODO section 30 (coverage_gaps M13, corrected). A modport port may be a method prototype - import task drive(int x), export function int get() - parsed by Parser::parse_tf_header, the one header parse parse_function_decl and parse_task_decl now destructure, printed by Fmt::tf_header, bound as Target::InScope (IEEE 1800-2017 25.5: a modport names a member of its own interface) with the formals declared in a scope of their own; one prototype per import/export group, the LRM's comma list still a clean error (recorded narrowing). A DPI import's formals may be unnamed (35.5.5) behind parse_tf_ports(allow_unnamed), the empty-name convention documented on TfArg::name, every other caller unchanged and a non-DPI unnamed formal still an error at its position. M13's const half was misfiled - const ref already parsed - but the probe found the unowned gap in const /*c*/ ref, now TfArg::after_const. Proof: 3 223 tests / 48 binaries, corpus 814/820, 4 807 corpus + opentitan files byte-identical to 0.33.0 (zero population), QA's adversarial probes and consumer audit of the possibly-empty names. docs/features/interface-dpi-arms/README.md. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
d1821ff859
commit
ee0faa2e18
22 files changed
+1208
-156
No files matched your search
@@ -260,7 +260,7 @@ impl<'a> Parser<'a> {
|
||||
// `property p(sequence a, untyped b);` — the two formal type
|
||||
// spellings only a property or a sequence has.
|
||||
self.sva_formals = true;
|
||||
let out = self.parse_tf_ports();
|
||||
let out = self.parse_tf_ports(false);
|
||||
self.sva_formals = false;
|
||||
Some(out?)
|
||||
} else {
|
||||
|
||||
@@ -78,7 +78,8 @@ impl<'a> Parser<'a> {
|
||||
let before_name = self.take_gap();
|
||||
let name = self.expect_name()?;
|
||||
let before_args = self.take_before_open("(");
|
||||
let args = if self.cur().is_sym("(") { Some(self.parse_tf_ports()?.args) } else { None };
|
||||
let args =
|
||||
if self.cur().is_sym("(") { Some(self.parse_tf_ports(false)?.args) } else { None };
|
||||
let event = self.parse_coverage_event()?;
|
||||
let before_semi = self.gap_before(";");
|
||||
self.expect_sym(";")?;
|
||||
@@ -149,7 +150,7 @@ impl<'a> Parser<'a> {
|
||||
self.bump();
|
||||
let before_args = self.take_before_open("(");
|
||||
let has_parens = self.cur().is_sym("(");
|
||||
let args = self.parse_tf_ports()?.args;
|
||||
let args = self.parse_tf_ports(false)?.args;
|
||||
return Ok(Some(CoverageEvent::Sample(Box::new(SampleFn {
|
||||
before_with,
|
||||
after_with,
|
||||
|
||||
@@ -113,6 +113,9 @@ impl<'a> Parser<'a> {
|
||||
/// One direction group of a modport's port list. `input a, b, output c` is
|
||||
/// two groups: the comma keeps the group open only while a plain name
|
||||
/// follows it — a direction keyword starts the next group.
|
||||
///
|
||||
/// An `import` or `export` may write a method prototype instead of a name
|
||||
/// (IEEE 1800 §25.5.4); that is a group of its own.
|
||||
fn parse_modport_port(&mut self) -> PResult<ModportPort> {
|
||||
const MODPORT_KEYWORDS: &[&str] =
|
||||
&["input", "output", "inout", "ref", "import", "export", "clocking"];
|
||||
@@ -123,6 +126,10 @@ impl<'a> Parser<'a> {
|
||||
return self.error("expected a modport port direction");
|
||||
}
|
||||
let kw = self.bump().text;
|
||||
let after_kw = self.take_gap();
|
||||
if let Some(kind) = self.at_modport_prototype(&kw) {
|
||||
return self.parse_modport_prototype(meta, kw, after_kw, kind, start);
|
||||
}
|
||||
let mut names = vec![self.expect_name()?];
|
||||
let mut span = self.span_from(start);
|
||||
let mut comma = false;
|
||||
@@ -142,7 +149,52 @@ impl<'a> Parser<'a> {
|
||||
if meta.trailing.is_none() {
|
||||
meta.trailing = self.take_trailing();
|
||||
}
|
||||
Ok(ModportPort { meta, kw, names, comma, span })
|
||||
Ok(ModportPort { meta, kw, after_kw, names, prototype: None, comma, span })
|
||||
}
|
||||
|
||||
/// Which method prototype an `import`/`export` group is about to write, if
|
||||
/// it is writing one at all (IEEE 1800 §25.5.4).
|
||||
fn at_modport_prototype(&self, kw: &str) -> Option<TfKind> {
|
||||
if kw != "import" && kw != "export" {
|
||||
return None;
|
||||
}
|
||||
if self.cur().is_kw("task") {
|
||||
Some(TfKind::Task)
|
||||
} else if self.cur().is_kw("function") {
|
||||
Some(TfKind::Function)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// `import task drive(int x)` — one modport list element holding a whole
|
||||
/// subroutine header and nothing else. It ends where the modport list
|
||||
/// says: at the `,` that starts the next group or the `)` that closes the
|
||||
/// list, never at a `;`, because a prototype here has no body to open.
|
||||
fn parse_modport_prototype(
|
||||
&mut self,
|
||||
mut meta: Meta,
|
||||
kw: String,
|
||||
after_kw: Vec<Comment>,
|
||||
kind: TfKind,
|
||||
start: u32,
|
||||
) -> PResult<ModportPort> {
|
||||
let prototype = self.parse_tf_header(kind, false)?;
|
||||
let span = self.span_from(start);
|
||||
self.take_before_sep(&mut meta, ",");
|
||||
let comma = self.eat_sym(",");
|
||||
if meta.trailing.is_none() {
|
||||
meta.trailing = self.take_trailing();
|
||||
}
|
||||
Ok(ModportPort {
|
||||
meta,
|
||||
kw,
|
||||
after_kw,
|
||||
names: Vec::new(),
|
||||
prototype: Some(Box::new(prototype)),
|
||||
comma,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
/// `clocking cb @(posedge clk); ... endclocking` (IEEE 1800 §14.3). The
|
||||
|
||||
@@ -91,6 +91,39 @@ fn a_dpi_import_without_a_port_list_has_no_parens() {
|
||||
assert_eq!(g.name, "get");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dpi_import_formal_may_be_written_without_a_name() {
|
||||
// IEEE 1800-2017 §35.5.5: only an imported prototype's formal may name
|
||||
// its type and stop. The empty name is the crate's convention for one
|
||||
// that was written without a name; `name_span` is empty and sits just
|
||||
// past the type, inside the formal's own span.
|
||||
let src = "package p;\n import \"DPI-C\" function int f(int, input logic [7:0], int named);\nendpackage\n";
|
||||
let f = parse_ok(src);
|
||||
let ItemKind::Package { items, .. } = &f.items[0].kind else { panic!("expected package") };
|
||||
let ItemKind::Function(g) = &items[0].kind else { panic!("expected a function") };
|
||||
let names: Vec<&str> = g.args.iter().map(|a| a.name.as_str()).collect();
|
||||
assert_eq!(names, ["", "", "named"]);
|
||||
assert_eq!(g.args[1].dir.as_deref(), Some("input"));
|
||||
assert_eq!(g.args[1].ty.name, "logic");
|
||||
for a in &g.args[..2] {
|
||||
assert!(a.name_span.is_empty(), "an absent name spans nothing");
|
||||
assert!(a.span.start <= a.name_span.start && a.name_span.end <= a.span.end);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unnamed_formal_outside_a_dpi_import_is_an_error() {
|
||||
// The negative of the rule above: everywhere else the name is required,
|
||||
// and the error names the position it was expected at.
|
||||
let src = "package p;\n function int f(int, chandle);\n endfunction\nendpackage\n";
|
||||
let toks = tokenize(src).expect("lex error");
|
||||
let e = parse(toks, src).expect_err("an unnamed formal is an error here");
|
||||
assert!(e.message.contains("expected identifier"), "{}", e.message);
|
||||
assert_eq!((e.line, e.col), (2, 21), "the error points at the `,`");
|
||||
let msg = parse_err("module m;\n task t(int);\n endtask\nendmodule\n");
|
||||
assert!(msg.contains("expected identifier"), "{msg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn context_is_a_linkage_name_when_an_equals_sign_follows_it() {
|
||||
let f = parse_ok("package p;\n import \"DPI-C\" context = function void f();\nendpackage\n");
|
||||
|
||||
@@ -261,6 +261,68 @@ fn one_modport_declaration_may_name_several_modports() {
|
||||
assert_eq!(decls[1].name, "b");
|
||||
}
|
||||
|
||||
/// The modports of the first item of `src`, which must be an interface whose
|
||||
/// first item is a `modport` declaration.
|
||||
fn modports_of(f: &SourceFile) -> &[ModportDecl] {
|
||||
let ItemKind::Module(m) = &f.items[0].kind else { panic!("expected an interface") };
|
||||
let ItemKind::Modport(decls) = &m.items[0].kind else { panic!("expected a modport") };
|
||||
decls
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_modport_may_import_a_method_prototype() {
|
||||
// IEEE 1800-2017 §25.5.4: `modport_tf_port ::= method_prototype | tf_identifier`.
|
||||
let f = parse_ok(
|
||||
"interface i;\n modport mp (input a, import task drive(input int x), export function int peek());\nendinterface\n",
|
||||
);
|
||||
let ports = &modports_of(&f)[0].ports;
|
||||
assert_eq!(ports.len(), 3);
|
||||
assert!(ports[0].prototype.is_none(), "a name list is not a prototype");
|
||||
let drive = ports[1].prototype.as_ref().expect("an imported prototype");
|
||||
assert_eq!(ports[1].kw, "import");
|
||||
assert_eq!(drive.kind, TfKind::Task);
|
||||
assert_eq!(drive.name, "drive");
|
||||
assert!(drive.ret.is_none(), "a task has no return type");
|
||||
assert_eq!(drive.args.len(), 1);
|
||||
assert_eq!(drive.args[0].name, "x");
|
||||
assert_eq!(drive.args[0].dir.as_deref(), Some("input"));
|
||||
assert!(ports[1].names.is_empty(), "a prototype group names nothing else");
|
||||
let peek = ports[2].prototype.as_ref().expect("an exported prototype");
|
||||
assert_eq!(ports[2].kw, "export");
|
||||
assert_eq!(peek.kind, TfKind::Function);
|
||||
assert_eq!(peek.ret.as_ref().map(|r| r.name.as_str()), Some("int"));
|
||||
assert!(peek.has_parens && peek.args.is_empty(), "an empty formal list");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_modport_prototype_may_write_no_formals_at_all() {
|
||||
// `import task drive;` — the `tf_identifier` alternative and the
|
||||
// parenthesis-less prototype are two different things, and only the
|
||||
// second one is a prototype.
|
||||
let f =
|
||||
parse_ok("interface i;\n modport mp (import task drive, import drive2);\nendinterface\n");
|
||||
let ports = &modports_of(&f)[0].ports;
|
||||
assert_eq!(ports.len(), 2);
|
||||
let drive = ports[0].prototype.as_ref().expect("a prototype");
|
||||
assert!(!drive.has_parens, "no `()` was written");
|
||||
assert!(drive.args.is_empty());
|
||||
assert!(ports[1].prototype.is_none(), "a bare name is a tf_identifier");
|
||||
assert_eq!(ports[1].names[0].text, "drive2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_gap_of_a_modport_prototype_has_an_owner() {
|
||||
let f = parse_ok(
|
||||
"interface i;\n modport mp (import /*a*/ task /*b*/ drive /*c*/ (/*d*/ int x));\nendinterface\n",
|
||||
);
|
||||
let port = &modports_of(&f)[0].ports[0];
|
||||
assert_eq!(port.after_kw.len(), 1, "between `import` and `task`");
|
||||
let p = port.prototype.as_ref().expect("a prototype");
|
||||
assert_eq!(p.after_kw.len(), 1, "between `task` and the name");
|
||||
assert_eq!(p.before_args.len(), 1, "between the name and the `(`");
|
||||
assert_eq!(p.args[0].meta.leading.len(), 1, "inside the `(`");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_clocking_block_parses_its_items() {
|
||||
// corpus/common_cells/test/stream_register_tb.sv:75
|
||||
|
||||
@@ -19,34 +19,20 @@ impl<'a> Parser<'a> {
|
||||
// (in C for a DPI import, nowhere at all for a pure virtual).
|
||||
let is_prototype =
|
||||
dpi.is_some() || prefix.iter().any(|q| q.text == "extern" || q.text == "pure");
|
||||
self.expect_kw("function")?;
|
||||
let after_kw = self.take_gap();
|
||||
let mut qualifiers = Vec::new();
|
||||
while self.cur().is_kw("automatic") || self.cur().is_kw("static") {
|
||||
let text = self.bump().text;
|
||||
qualifiers.push(Qualifier { text, after: self.take_gap() });
|
||||
}
|
||||
// Return type: everything up to the identifier followed by `(` or `;`.
|
||||
let ret = {
|
||||
let t = self.cur();
|
||||
if t.kind == TokenKind::Ident && (self.nth(1).is_sym("(") || self.nth(1).is_sym(";")) {
|
||||
None
|
||||
} else {
|
||||
let ty = self.parse_opt_data_type()?;
|
||||
if ty.is_implicit() {
|
||||
None
|
||||
} else {
|
||||
Some(ty)
|
||||
}
|
||||
}
|
||||
};
|
||||
// Out-of-body definitions of an `extern` method write the class name
|
||||
// in front: `function void my_class::configure(...)`.
|
||||
let before_name = self.take_gap();
|
||||
let (scope, Name { text: name, span: name_span }) = self.parse_method_name()?;
|
||||
let has_parens = self.cur().is_sym("(");
|
||||
let before_args = self.take_before_open("(");
|
||||
let TfPorts { args, dangling: args_dangling } = self.parse_tf_ports()?;
|
||||
let TfPrototype {
|
||||
after_kw,
|
||||
qualifiers,
|
||||
ret,
|
||||
before_name,
|
||||
scope,
|
||||
name,
|
||||
name_span,
|
||||
has_parens,
|
||||
before_args,
|
||||
args,
|
||||
args_dangling,
|
||||
..
|
||||
} = self.parse_tf_header(TfKind::Function, dpi.is_some())?;
|
||||
let before_semi = self.gap_before(";");
|
||||
self.expect_sym(";")?;
|
||||
let (stmts, dangling, end_label_comments, end) = if is_prototype {
|
||||
@@ -92,18 +78,19 @@ impl<'a> Parser<'a> {
|
||||
let prefix = self.take_member_prefix();
|
||||
let is_prototype =
|
||||
dpi.is_some() || prefix.iter().any(|q| q.text == "extern" || q.text == "pure");
|
||||
self.expect_kw("task")?;
|
||||
let after_kw = self.take_gap();
|
||||
let mut qualifiers = Vec::new();
|
||||
while self.cur().is_kw("automatic") || self.cur().is_kw("static") {
|
||||
let text = self.bump().text;
|
||||
qualifiers.push(Qualifier { text, after: self.take_gap() });
|
||||
}
|
||||
let before_name = self.take_gap();
|
||||
let (scope, Name { text: name, span: name_span }) = self.parse_method_name()?;
|
||||
let has_parens = self.cur().is_sym("(");
|
||||
let before_args = self.take_before_open("(");
|
||||
let TfPorts { args, dangling: args_dangling } = self.parse_tf_ports()?;
|
||||
let TfPrototype {
|
||||
after_kw,
|
||||
qualifiers,
|
||||
before_name,
|
||||
scope,
|
||||
name,
|
||||
name_span,
|
||||
has_parens,
|
||||
before_args,
|
||||
args,
|
||||
args_dangling,
|
||||
..
|
||||
} = self.parse_tf_header(TfKind::Task, dpi.is_some())?;
|
||||
let before_semi = self.gap_before(";");
|
||||
self.expect_sym(";")?;
|
||||
let (stmts, dangling, end_label_comments, end) = if is_prototype {
|
||||
@@ -149,14 +136,79 @@ impl<'a> Parser<'a> {
|
||||
self.expect_kw("let")?;
|
||||
let Name { text: name, span: name_span } = self.expect_name()?;
|
||||
let has_parens = self.cur().is_sym("(");
|
||||
let ports = self.parse_tf_ports()?.args;
|
||||
let ports = self.parse_tf_ports(false)?.args;
|
||||
self.expect_sym("=")?;
|
||||
let value = self.parse_expr()?;
|
||||
self.expect_sym(";")?;
|
||||
Ok(LetDecl { name, name_span, has_parens, ports, value })
|
||||
}
|
||||
|
||||
pub(super) fn parse_tf_ports(&mut self) -> PResult<TfPorts> {
|
||||
/// The header a subroutine declaration and a `modport` method prototype
|
||||
/// (IEEE 1800 §25.5.4) are both written as: the keyword, the lifetime
|
||||
/// qualifiers, a function's return type, the name and the port list. It
|
||||
/// stops at the `)` of that list, so the caller owns whatever ends the
|
||||
/// construct — a `;` for a declaration, a `,` or `)` for a prototype.
|
||||
///
|
||||
/// `allow_unnamed` is the DPI rule (§35.5.5): only an imported prototype's
|
||||
/// formal may be written without a name.
|
||||
pub(super) fn parse_tf_header(
|
||||
&mut self,
|
||||
kind: TfKind,
|
||||
allow_unnamed: bool,
|
||||
) -> PResult<TfPrototype> {
|
||||
self.expect_kw(kind.keyword())?;
|
||||
let after_kw = self.take_gap();
|
||||
let mut qualifiers = Vec::new();
|
||||
while self.cur().is_kw("automatic") || self.cur().is_kw("static") {
|
||||
let text = self.bump().text;
|
||||
qualifiers.push(Qualifier { text, after: self.take_gap() });
|
||||
}
|
||||
// Return type: everything up to the identifier followed by `(` or `;`.
|
||||
// A task has none at all.
|
||||
let ret = match kind {
|
||||
TfKind::Task => None,
|
||||
TfKind::Function => {
|
||||
let t = self.cur();
|
||||
if t.kind == TokenKind::Ident
|
||||
&& (self.nth(1).is_sym("(") || self.nth(1).is_sym(";"))
|
||||
{
|
||||
None
|
||||
} else {
|
||||
let ty = self.parse_opt_data_type()?;
|
||||
if ty.is_implicit() {
|
||||
None
|
||||
} else {
|
||||
Some(ty)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// Out-of-body definitions of an `extern` method write the class name
|
||||
// in front: `function void my_class::configure(...)`.
|
||||
let before_name = self.take_gap();
|
||||
let (scope, Name { text: name, span: name_span }) = self.parse_method_name()?;
|
||||
let has_parens = self.cur().is_sym("(");
|
||||
let before_args = self.take_before_open("(");
|
||||
let TfPorts { args, dangling: args_dangling } = self.parse_tf_ports(allow_unnamed)?;
|
||||
Ok(TfPrototype {
|
||||
kind,
|
||||
after_kw,
|
||||
qualifiers,
|
||||
ret,
|
||||
before_name,
|
||||
scope,
|
||||
name,
|
||||
name_span,
|
||||
has_parens,
|
||||
before_args,
|
||||
args,
|
||||
args_dangling,
|
||||
})
|
||||
}
|
||||
|
||||
/// The `( ... )` of a subroutine's formals. `allow_unnamed` is the DPI
|
||||
/// rule; see [`Parser::parse_tf_header`] and [`TfArg::name`].
|
||||
pub(super) fn parse_tf_ports(&mut self, allow_unnamed: bool) -> PResult<TfPorts> {
|
||||
let mut args = Vec::new();
|
||||
if !self.cur().is_sym("(") {
|
||||
return Ok(TfPorts::default());
|
||||
@@ -172,12 +224,16 @@ impl<'a> Parser<'a> {
|
||||
loop {
|
||||
let start = self.here();
|
||||
let mut meta = self.take_meta();
|
||||
let mut after_const = Vec::new();
|
||||
let dir = {
|
||||
let t = self.cur();
|
||||
if t.is_kw("const") && self.nth(1).is_kw("ref") {
|
||||
// `const ref` (IEEE 1800 §13.5.2), corpus
|
||||
// `ibex/vendor/lowrisc_ip/dv/sv/dv_utils/dv_utils_pkg.sv:104`.
|
||||
// Two keywords, so the gap between them is a slot of its
|
||||
// own — no other direction has an inside.
|
||||
self.bump();
|
||||
after_const = self.take_gap();
|
||||
self.bump();
|
||||
Some("const ref".to_string())
|
||||
} else if t.is_kw("input")
|
||||
@@ -193,7 +249,15 @@ impl<'a> Parser<'a> {
|
||||
let after_dir = if dir.is_some() { self.take_gap() } else { Vec::new() };
|
||||
let ty = self.parse_opt_data_type()?;
|
||||
let before_name = self.take_gap();
|
||||
let Name { text: name, span: name_span } = self.expect_name()?;
|
||||
// A DPI import's formal may name its type and stop (§35.5.5);
|
||||
// every other formal must be named. The empty name is the crate's
|
||||
// convention for "written without one" — see [`TfArg::name`].
|
||||
let unnamed = allow_unnamed && (self.cur().is_sym(",") || self.cur().is_sym(")"));
|
||||
let Name { text: name, span: name_span } = if unnamed {
|
||||
Name { text: String::new(), span: TextRange::empty(self.span_from(start).end) }
|
||||
} else {
|
||||
self.expect_name()?
|
||||
};
|
||||
let unpacked = self.parse_dims()?;
|
||||
let mut eq_comments = BinaryComments::default();
|
||||
let default = if self.cur().is_sym("=") {
|
||||
@@ -211,6 +275,7 @@ impl<'a> Parser<'a> {
|
||||
args.push(TfArg {
|
||||
meta,
|
||||
dir,
|
||||
after_const,
|
||||
after_dir,
|
||||
ty,
|
||||
before_name,
|
||||
|
||||
Reference in new issue
Block a user