212 lines
11 KiB
Markdown
212 lines
11 KiB
Markdown
# dpi-export
|
||
|
||
## Goal
|
||
|
||
Parse and format the three constructs the `dpi-export` corpus bucket is made
|
||
of: DPI **imports** (`import "DPI-C" [context|pure] [c_name =] function|task
|
||
...;`, IEEE 1800 §35.5.1–2), DPI **exports** (`export "DPI-C" [c_name =]
|
||
function|task name;`, §35.5.4) and package **re-exports** (`export pkg::*;`,
|
||
§26.6). All three are one-line declarations that a real design writes at
|
||
package, module, generate-block or class scope; before this change every one
|
||
of them was a hard parse error, so 11 corpus files produced no output at all.
|
||
A DPI import is grammatically a function/task *prototype* with a linkage
|
||
prefix, so it is modelled as one — which means the argument list breaks with
|
||
the same rules as any other function header, and every consumer that already
|
||
understands `ItemKind::Function` sees a DPI-imported function without being
|
||
taught anything new.
|
||
|
||
## Assumptions
|
||
|
||
- **`import` followed by a string literal is always a DPI import.** No package
|
||
is named by a string, so the one-token lookahead in `parse_keyword_item` is
|
||
unambiguous. Any linkage spec string is accepted, not just `"DPI-C"` —
|
||
`"DPI-SV"` is legal and the LRM lets an implementation define others; the
|
||
string is stored and reprinted verbatim, never interpreted.
|
||
- **`context` is not promoted to a keyword.** It is a plain identifier
|
||
everywhere else, and adding it to the keyword table would break any signal or
|
||
port named `context`. It is recognised positionally, and only when an `=`
|
||
does *not* follow it — `import "DPI-C" context = function void f();` is a
|
||
linkage name spelled `context`, not the property. `pure` already is a
|
||
keyword (it is in `MEMBER_QUALIFIERS`), so the property check tests both
|
||
spellings.
|
||
- **`export` was already a keyword** (`token.rs`'s `is_keyword`), so nothing in
|
||
the lexer changed and no file's tokenization moved. Grepping the 820-file
|
||
corpus for a bare `export` used as an identifier found none: every hit is a
|
||
*suffix* of a longer name (`analysis_export`, `seq_item_export`), which lexes
|
||
as one identifier and is unaffected.
|
||
- **DPI arguments always carry a name.** The LRM allows an unnamed formal
|
||
(`function void f(input int);`); `parse_tf_ports` requires a name and this
|
||
work did not change that, so the unnamed form is still a parse error. Zero
|
||
corpus files write it. Fixing it is a `parse_tf_ports` change that would
|
||
affect ordinary functions too, which is outside this feature's scope.
|
||
- **Import and export lists are printed on one line.** `ItemKind::Import`
|
||
already does this, and no corpus `export` list is long enough for a breakable
|
||
group to earn its keep. A DPI import's *argument* list is a normal breakable
|
||
group, because those do get long.
|
||
- **A DPI import declares a prototype**, so `is_prototype` is true and there is
|
||
no `endfunction`/`endtask` — the same shape `extern` and `pure virtual`
|
||
already produced.
|
||
|
||
## Handshakes
|
||
|
||
**Consumed**
|
||
|
||
- `Parser::parse_function_decl` / `parse_task_decl` (renamed from
|
||
`parse_function` / `parse_task`, which now delegate) — the DPI import path
|
||
reuses ANSI argument parsing, the return-type sniffing and the prototype
|
||
handling wholesale.
|
||
- `Parser::parse_package_paths` — factored out of `parse_import_list` so
|
||
`export` can share the `pkg::item` / `pkg::*` loop. `import` calls it with
|
||
`allow_star_pkg: false`; only `export` admits `*::*`.
|
||
- `Fmt::tf_args` and `Fmt::delimited` for the argument group.
|
||
|
||
**Promised**
|
||
|
||
- `FunctionDecl` and `TaskDecl` gained `dpi: Option<DpiImport>`. Both are
|
||
constructed only in `parser.rs`, so nothing outside had to change; a consumer
|
||
that ignores the field keeps its old behavior exactly. `is_prototype` is now
|
||
true for DPI imports as well as `extern`/`pure virtual` — its documented
|
||
meaning ("body-less, no `end*` keyword") is unchanged.
|
||
- `ItemKind::Export(ExportDecl)` is a new variant. Exhaustive matches over
|
||
`ItemKind` live in `vlog_semantic/src/bind.rs` (one arm added, see below) and
|
||
`vlog_parser/tests/spans.rs` (arms added). `vlog_linter/src/visit.rs` has a
|
||
`_ => {}` fallback and needed no change.
|
||
|
||
**Requests to other modules** (not done here, deliberately)
|
||
|
||
- **vlog_semantic**: `ItemKind::Export` is bound as an opaque leaf. Two things
|
||
a `bind.rs` owner may want later: (a) a package re-export's `pkg::item`
|
||
halves are real references and would resolve for go-to-definition with the
|
||
same `bind_import_path` an `import` uses — but a re-export does *not* bring
|
||
the name into the exporting scope, so reusing `bind_import_path` as-is would
|
||
also push a scope import, which is a semantics change I did not want to make
|
||
unasked; (b) a DPI export's `name` refers to a function declared elsewhere in
|
||
the same scope and could be a `use_name`. Note the `*::*` form has `"*"` as
|
||
its **package** name, which no existing code expects.
|
||
- **vlog_semantic / vlog_linter**: DPI-imported functions and tasks now arrive
|
||
as ordinary `ItemKind::Function` / `ItemKind::Task` with `stmts` empty and
|
||
`is_prototype` set, so `bind_function` already declares them as symbols — a
|
||
DPI-imported function is a known name for go-to-definition with no further
|
||
work. If a rule ever wants to treat them differently, `decl.dpi.is_some()`
|
||
is the test.
|
||
- **dv-statements owner**: `corpus/ibex/vendor/lowrisc_ip/dv/sv/csr_utils/
|
||
csr_utils_pkg.sv` was in this bucket; its `export` at line 10 now parses and
|
||
the file advanced to `foreach (paths[0].slices[i])` at line 290 — a `foreach`
|
||
loop-variable list with an indexed expression, which is a dv-statements
|
||
blocker, not mine.
|
||
|
||
## Status / TODO
|
||
|
||
- [x] `import "DPI-C" function <ret> <name> (<ansi args>);`, including the
|
||
form split across lines (env_dpi.sv:7) and the no-parens form
|
||
(ibex_simple_system_cosim_checker.sv:29)
|
||
- [x] `context` and `pure` properties (ascon_model_dpi_pkg.sv:8,
|
||
crypto_dpi_prince_pkg.sv:14)
|
||
- [x] `chandle` and `longint unsigned` return types; arguments with no
|
||
direction keyword; packed multi-dimensional and unsized-unpacked
|
||
argument types (crypto_dpi_present_pkg.sv:12, ascon_model_dpi_pkg.sv:15)
|
||
- [x] DPI-imported `task`
|
||
- [x] `c_identifier =` linkage names on both import and export (zero corpus
|
||
hits; same grammar, so supported and tested)
|
||
- [x] `export "DPI-C" function|task name;` at module scope and inside a
|
||
generate block under an `` `ifdef`` (ibex_simple_system.sv:369,
|
||
ibex_if_stage.sv:378)
|
||
- [x] `export pkg::*;` and `export pkg::a, pkg::b;`
|
||
(ibex_mem_intf_agent_pkg.sv:28, csr_utils_pkg.sv:10)
|
||
- [x] `export *::*;` (LRM form, zero corpus hits)
|
||
- [x] Case table `crates/vlog_formatter/tests/cases/dpi_export.cases` (19
|
||
cases: one per corpus shape, plus comments, a `verilog_format: off`
|
||
fence, the 100/101-column boundary, a class body, compilation-unit
|
||
scope, and an ordinary package `import` next to a DPI one)
|
||
- [x] Parser unit tests and a `tests/spans.rs` source covering every shape
|
||
- [ ] Unnamed DPI formals (`function void f(input int);`) — see Assumptions;
|
||
needs a `parse_tf_ports` change that touches ordinary functions
|
||
|
||
## Work log
|
||
|
||
**2026-08-20** — Implemented the whole bucket.
|
||
|
||
- Baseline: `{"passing":661,"total":820,"pct":80.6,"failures_by_category":
|
||
{"interface":31,"bind":28,"dv-statements":25,"expressions":19,
|
||
"directive-split":14,"assertions":13,"dpi-export":11,"types-decls":11,
|
||
"invalid-or-fragment":3,"lex-macro-number":2,"not-verilog":1,"other":1}}`
|
||
- After: `{"passing":671,"total":820,"pct":81.8,"failures_by_category":
|
||
{"interface":31,"bind":28,"dv-statements":26,"expressions":19,
|
||
"directive-split":14,"assertions":13,"types-decls":11,
|
||
"invalid-or-fragment":3,"lex-macro-number":2,"not-verilog":1,"other":1}}`
|
||
— `dpi-export` is gone; the passing set is a strict superset of the
|
||
baseline's (checked with `comm` on the two TSVs: zero regressions, ten new
|
||
passes). `dv-statements` 25 → 26 is csr_utils_pkg.sv arriving from this
|
||
bucket at its next blocker, described in Handshakes.
|
||
|
||
**Key decision — modelling.** Two shapes were possible for a DPI import: a new
|
||
`ItemKind::DpiImport` node, or a `FunctionDecl`/`TaskDecl` with a linkage
|
||
field. I chose the field. A new item kind would have forced arms in every
|
||
exhaustive `ItemKind` match, and — worse — would have hidden DPI-imported
|
||
functions from `bind_function`, `visit.rs`'s function walk and every rule that
|
||
looks for functions, so a DPI-imported function would not be a symbol. The
|
||
field costs one `Option` on two structs, both built in one file, and makes the
|
||
formatter's job a two-line prefix. Export is genuinely a different construct
|
||
(it declares nothing and has no arguments), so it is a new `ItemKind`.
|
||
|
||
**Key decision — one `ExportDecl` enum, not two item kinds.** `export` reads
|
||
its next token to decide: a string literal is DPI, anything else starts a
|
||
package path list. Keeping both under one variant keeps the new-arm cost at one
|
||
per exhaustive match.
|
||
|
||
**Lexer.** Untouched. `export` was already a keyword; `context` deliberately is
|
||
not (see Assumptions). `chandle` was already a type keyword, so
|
||
`function chandle get;` needed nothing special — the ordinary return-type path
|
||
handles it.
|
||
|
||
**Formatting decisions.** The linkage prefix is emitted as flat text in front
|
||
of the existing function/task header (`Fmt::dpi_import_prefix`), so a DPI
|
||
import lays out exactly like the prototype it is: header on one line, argument
|
||
list a breakable group that goes one-per-line when it does not fit. The
|
||
multi-line `import "DPI-C"` / `function ...` spelling the ibex DV files use
|
||
collapses onto one line, which is what every other multi-line header in lucisv
|
||
does. Exports print flat.
|
||
|
||
**Safety net.** `verify.rs` was not touched and did not need to be: every one
|
||
of these constructs is printed by re-emitting exactly the tokens the source
|
||
wrote (the linkage string verbatim, the property, the linkage name, the
|
||
keyword, the names, the paths), so the equivalence check passes on token
|
||
identity, not on forgiveness. No new normalization, so no masked-corruption
|
||
attack tests are owed under CLAUDE.md invariant 4.
|
||
|
||
**Checks.** `cargo fmt --all` clean, `cargo clippy --all-targets` clean,
|
||
`cargo test` green (509 formatter cases, spans, verification, linter, server).
|
||
|
||
## QA log
|
||
|
||
### 2026-08-21 — round-1 adversarial QA
|
||
|
||
Verified: `cargo test --workspace`/clippy/fmt clean; corpus score 796/820;
|
||
whole-corpus idempotency + comment-preservation sweep clean (see item-misc log
|
||
for the shared methodology); 476-case mutation fuzz clean (0 panics/hangs/bad
|
||
exit codes/non-idempotent output).
|
||
|
||
Targeted attacks:
|
||
|
||
- **`context` recognized positionally, not as a keyword.** Tried `context`
|
||
used as an ordinary identifier in the position where it would be confused
|
||
with the DPI property: `import "DPI-C" function void context();` (function
|
||
*named* `context`) formats correctly and is not swallowed by the
|
||
positional check. Also exercised the real grammar order (`context`/`pure`
|
||
*before* the optional `c_identifier =`, IEEE 1800 §35.5.2) —
|
||
`import "DPI-C" context mycid = function void f1();` and the `pure` variant
|
||
both parse and round-trip.
|
||
|
||
- **`export *::*` → `ImportPath` with package `"*"`.** `package p; export
|
||
*::*; endpackage` formats unchanged and is idempotent
|
||
(`target/release/lucisv --stdout` twice, `cmp` clean).
|
||
|
||
- **`is_prototype` doubling as "DPI import".** No case found where this
|
||
conflation produces a wrong-but-plausible read; every DPI import/export
|
||
variant tried (`context`, `pure`, `c_identifier =`, task vs function,
|
||
package export lists, package export with comma-separated names) round-trips
|
||
or cleanly errors.
|
||
|
||
No BLOCKING findings for this cluster.
|
||
|