Add interfaces, modport, clocking blocks and virtual interface types
interface...endinterface shares the module header/body machinery (ItemKind::Module with kw == "interface"); modport and clocking are structural items; virtual <iface>[#(...)] is accepted in every type position. module-filename now applies to interfaces. Coverage loop round 1, cluster 5: 712 -> 740 / 820 (86.8% -> 90.2%). Feature README: docs/features/interfaces/README.md Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
e24d33afab
commit
2a63faf9a2
11 files changed
+1204
-14
No files matched your search
@@ -0,0 +1,220 @@
|
||||
# interfaces
|
||||
|
||||
## Goal
|
||||
|
||||
Make `interface … endinterface` and the two constructs that only live
|
||||
inside one — `modport` and `clocking` — first-class, structurally parsed
|
||||
and canonically laid out, and make a `virtual <iface>` type reference
|
||||
legal in every type position rather than only as a class property. An
|
||||
interface header is the module header's grammar (IEEE 1800-2017 §25.3
|
||||
vs §23.2.1) and an interface body is the module body's item list plus
|
||||
those two extra items, so the whole feature is mostly a matter of
|
||||
*sharing* the module machinery rather than writing a second copy of it.
|
||||
|
||||
At the start of this round `interface` was the corpus's top blocker at
|
||||
31 of 820 files.
|
||||
|
||||
## Assumptions
|
||||
|
||||
- **An interface is a `Module` node with a different keyword.** There is
|
||||
no `ItemKind::Interface`: `ItemKind::Module` carries `kw` (`module` /
|
||||
`macromodule` / `interface`) and `Module::end_kw()` derives
|
||||
`endmodule` / `endinterface` from it. Everything that already walks a
|
||||
module — the formatter, `fence.rs`, `range.rs`, `vlog_linter::visit`,
|
||||
`vlog_semantic::bind` — therefore handles interfaces unchanged, and
|
||||
the parser's header and body code is literally the same code. The cost
|
||||
is that a consumer that wants to tell the two apart must read `kw`;
|
||||
that is the same thing it already had to do for `macromodule`.
|
||||
- **`interface class` is not an interface** (IEEE 1800-2017 §8.26). It
|
||||
is a class-like construct with a different body grammar and is
|
||||
deliberately left as an "unsupported construct" parse error. No corpus
|
||||
file uses it (0 of 820).
|
||||
- **`program … endprogram` is out of scope** for this round even though
|
||||
it shares the same header/body grammar and would be a one-line
|
||||
addition here. No corpus file uses it (0 of 820), so it buys nothing
|
||||
measurable; see Handshakes.
|
||||
- A modport port is a **direction keyword plus the names it governs**.
|
||||
The source's grouping is preserved (`input valid, data` is one group,
|
||||
not two ports), because regrouping it would be a token-order change
|
||||
for no gain. The explicit modport port (`.name(expr)`) and the modport
|
||||
method *prototype* (`import task drive(int x);` with a signature) are
|
||||
not supported — a bare `import drive_task, check_task;` is. Neither
|
||||
prototype form occurs in the corpus.
|
||||
- A clocking skew is kept as **text as written** (`#2`, `#1step`,
|
||||
`negedge`), not as an expression: it is a delay or an edge and vfmt
|
||||
never re-lays-out either. The `#(min:typ:max)` skew spelling is not
|
||||
supported (0 corpus uses).
|
||||
- A clocking item's optional `= expression` (`input data = top.dut.d;`,
|
||||
IEEE 1800-2017 §14.4) is not supported. 0 corpus uses.
|
||||
- `virtual` / `virtual interface` in a **type position** is recorded on
|
||||
`DataType::virt` as a one-byte `VirtualKw`, not as a `String`: every
|
||||
`Expr` carries a `DataType` through `ExprKind::Type`, and a `String`
|
||||
there pushes `DimKind` past clippy's `large_enum_variant` threshold
|
||||
(measured: `DimKind::Range` 192 → 208 bytes). A *declaration* keeps
|
||||
the prefix where it always did, in `DataDecl::qualifiers`, so the
|
||||
class-property path is untouched.
|
||||
- A `// verilog_format: off` fence inside a **modport or clocking body**
|
||||
is not a fence scope, exactly as a fence inside a class body or a
|
||||
statement body is not (`fence.rs::scan_nested` has no arm for either).
|
||||
A fence in an *interface* body works, because that is a module body.
|
||||
|
||||
## Handshakes
|
||||
|
||||
Consumed:
|
||||
|
||||
- `Parser::parse_module`, `parse_param_port_list`, `parse_port_list`,
|
||||
`parse_sens_list`, and the whole `Fmt::module`/`module_head` layout —
|
||||
all reused verbatim for interfaces.
|
||||
- `Fmt::delimited_dangling` for a modport's port list, so it breaks and
|
||||
aligns exactly like a header port list.
|
||||
|
||||
Promised / changed for others:
|
||||
|
||||
- **`ast::Module` now also models an interface.** `Module::kw` may be
|
||||
`"interface"`; `Module::end_kw()` is the closing keyword. Any consumer
|
||||
that assumed `ItemKind::Module` means `module` must read `kw`.
|
||||
- **`ast::DataType` has a new field, `virt: Option<VirtualKw>`.**
|
||||
`DataType::is_implicit()` now returns `false` when it is set.
|
||||
`DataType::default()` is unchanged, so constructing one still
|
||||
compiles.
|
||||
- **Two new `ItemKind` variants**, `Modport(Vec<ModportDecl>)` and
|
||||
`Clocking(ClockingDecl)`. Minimal arms were added where the match had
|
||||
to stay exhaustive: `vlog_semantic::bind::bind_item` (both are opaque
|
||||
leaves) and `vlog_parser/tests/spans.rs`. `vlog_linter::visit`,
|
||||
`fence.rs`, `range.rs` and `vfmt_server` all have catch-all arms and
|
||||
needed no change.
|
||||
- **`clocking` and `endclocking` are now reserved words** (`token.rs`).
|
||||
A design that used either as an identifier would change meaning; no
|
||||
corpus file does, and the passing set was verified to be a strict
|
||||
superset.
|
||||
- **`module-filename` now applies to interfaces** and its message names
|
||||
the construct (`interface \`bus_if\` does not match file name …`)
|
||||
instead of hard-coding "module". This is the intended behaviour: a
|
||||
one-interface file is looked up by its interface name exactly the way
|
||||
a one-module file is.
|
||||
|
||||
Requests to other owners (not done here):
|
||||
|
||||
- **`vlog_semantic`**: an interface is currently declared as
|
||||
`SymbolKind::Module`, which is what makes go-to-definition on an
|
||||
interface *instantiation* work today for free (an instantiation
|
||||
resolves its module name against the same index). Whether interfaces
|
||||
deserve their own `SymbolKind::Interface` — and with it
|
||||
`vfmt_server`'s `LspKind::INTERFACE` in `symbols.rs` instead of
|
||||
`LspKind::CLASS` — is the semantic owner's call; it is a mechanical
|
||||
change across `model.rs`, `symbols.rs`, `hover.rs` and `workspace.rs`,
|
||||
which is why it is not made here. Likewise, a `modport` name and a
|
||||
`clocking` block name are plausible symbols, and the signal names
|
||||
inside both are *references* to nets declared in the enclosing
|
||||
interface; neither is bound today.
|
||||
- **expressions owner**: `corpus/ibex/dv/formal/check/top.sv:432` now
|
||||
parses past its interface/`default clocking` blockers and fails on
|
||||
`(data_we_o -> …)`, the implication operator in an ordinary
|
||||
expression. `corpus/ibex/vendor/lowrisc_ip/dv/sv/common_ifs/pins_if.sv:97`
|
||||
likewise fails on the drive-strength spec `assign (pull0, pull1) x = …`.
|
||||
Both are classified `expressions` by `scripts/score.sh`.
|
||||
- **whoever owns the `other` bucket**:
|
||||
`corpus/ibex/vendor/lowrisc_ip/dv/sv/dv_utils/dv_utils_pkg.sv:104`
|
||||
now fails on `function automatic int max(const ref int int_q[$]);` —
|
||||
the `const` qualifier on a task/function argument, which
|
||||
`Parser::parse_tf_ports` does not accept.
|
||||
- **`program … endprogram`**: whoever picks it up gets it for two lines
|
||||
in `parse_keyword_item` plus one in `Module::end_kw`, since the
|
||||
grammar is the one already implemented here.
|
||||
|
||||
## Status / TODO
|
||||
|
||||
- [x] `interface [import pkg::*;] [#(params)] [(ports)]; … endinterface
|
||||
[: label]`, sharing the module header and body parse
|
||||
- [x] `import pkg::*;` between the name and the `#(`/`(` — verified it
|
||||
already worked for modules too (`Module::imports`), so nothing new
|
||||
was needed there; a module case was added to the case table
|
||||
- [x] ANSI ports with no type (`inout clk`, `input rst_n`), string
|
||||
parameters, multi-parameter headers
|
||||
- [x] `endinterface : label`
|
||||
- [x] `modport a (…), b (…);` — `input`/`output`/`inout`/`ref`/`import`/
|
||||
`export`/`clocking` groups, several modports per declaration
|
||||
- [x] `clocking name @(event); … endclocking [: label]`, `default
|
||||
clocking`, anonymous `default clocking @(…)`, body-less `default
|
||||
clocking cb;`
|
||||
- [x] clocking items: `default input #s output #s;`, `input [skew] a, b;`,
|
||||
`output [skew] c;`, `inout d;`
|
||||
- [x] `virtual [interface] <iface>[#(…)]` in a typedef base type and in a
|
||||
task/function argument (the declaration position already worked)
|
||||
- [x] `module-filename` generalized to interfaces, with a test
|
||||
- [x] 22 formatter cases in `crates/vlog_formatter/tests/cases/interfaces.cases`
|
||||
plus 8 parser unit tests, covering every corpus shape cited above,
|
||||
comments in every position, a `verilog_format: off` fence and the
|
||||
line-length boundary
|
||||
- [ ] Not done, and not planned here: `interface class`, `program`,
|
||||
explicit modport ports, modport method prototypes, clocking
|
||||
`= expr` items, `#(min:typ:max)` skews (see Assumptions)
|
||||
|
||||
## Work log
|
||||
|
||||
### 2026-08-20 — dev
|
||||
|
||||
Baseline (`cargo build --release && scripts/score.sh`, at `d488f0a`):
|
||||
|
||||
```
|
||||
{"passing":712,"total":820,"pct":86.8,"failures_by_category":{"interface":31,"dv-statements":26,"expressions":19,"directive-split":14,"types-decls":11,"invalid-or-fragment":3,"lex-macro-number":2,"not-verilog":1,"other":1}}
|
||||
```
|
||||
|
||||
After:
|
||||
|
||||
```
|
||||
{"passing":740,"total":820,"pct":90.2,"failures_by_category":{"dv-statements":26,"expressions":21,"directive-split":14,"types-decls":11,"invalid-or-fragment":3,"lex-macro-number":2,"other":2,"not-verilog":1}}
|
||||
```
|
||||
|
||||
+28 files, 0 lost — the passing set was verified to be a strict superset
|
||||
by diffing the before/after `.score-results.tsv` by path. The
|
||||
`interface` bucket is empty; the 3 files that did not reach `pass`
|
||||
advanced to blockers owned by other categories (listed in Handshakes).
|
||||
All 28 newly-formatting files were additionally round-tripped twice to
|
||||
confirm idempotency on the real sources.
|
||||
|
||||
Key decisions:
|
||||
|
||||
1. **Reuse `Module` instead of adding an `Interface` node.** The header
|
||||
grammar and the body item list are the same grammar; the only
|
||||
difference is the closing keyword. `parse_module` took an `end_kw`
|
||||
parameter and `parse_keyword_item` gained one arm. This is what made
|
||||
the header `import` (the interesting bit of §25.3) work with no new
|
||||
code at all: `Module::imports` already existed for
|
||||
`module m import pkg::*; (…)`, so both §23.2.1 and §25.3 are the same
|
||||
code path. A case for the module spelling was added to lock that in.
|
||||
2. **`modport` port lists reuse `delimited_dangling`**, so they collapse
|
||||
onto one line when they fit and break one direction group per line
|
||||
when they do not — the same rule every other bracketed list in vfmt
|
||||
follows. `interface STREAM_DV`'s three modports collapse; a 100-column
|
||||
one breaks.
|
||||
3. **Clocking blocks are parsed structurally**, not kept verbatim: the
|
||||
item grammar is small and closed (`default input/output <skew>`, and
|
||||
direction groups with an optional skew), so there was no reason to
|
||||
hand it to the verbatim path. One item per line, indented, closing
|
||||
keyword aligned with the opener.
|
||||
4. **`virtual` moved into `parse_opt_data_type`.** That one place is
|
||||
reached by the typedef base type, task/function arguments, struct
|
||||
members, ports and parameter ports, so a single change generalized it
|
||||
to every type position. The declaration path (`parse_data_decl`) is
|
||||
untouched: it consumes `virtual` into `qualifiers` before ever
|
||||
reaching `parse_opt_data_type`, so the class-property behaviour and
|
||||
its tests are bit-identical.
|
||||
5. **`DataType::virt` is a one-byte enum, not a `String`.** The first
|
||||
cut used `Option<String>` and made clippy's `large_enum_variant` fire
|
||||
on `DimKind` (`Range` grew 192 → 208 bytes, past the 200-byte
|
||||
threshold) — every `Expr` carries a `DataType`. `Option<Box<str>>`
|
||||
was still 208. `VirtualKw` (1 byte, with a `text()` accessor) fits in
|
||||
the existing padding and is honest: the prefix is one of exactly two
|
||||
spellings.
|
||||
|
||||
Deviations from the brief: none, except that `default disable iff (…)`
|
||||
was looked for in the corpus (`grep -rn 'default disable' corpus`) and
|
||||
does not occur in any `.sv`/`.v` file, so it was not implemented.
|
||||
|
||||
`verify.rs` was not touched: nothing here widens the safety net's
|
||||
forgiveness, so no masked-corruption attack tests are owed.
|
||||
|
||||
## QA log
|
||||
|
||||
## Review
|
||||
Reference in new issue
Block a user