tools/corpus_fetch.sh pins picorv32, ibex, common_cells and two
OpenTitan IPs by exact SHA; tools/corpus_check.sh buckets per-file
outcomes (parse/format/verification-error/crash) into a summary,
a deduped error histogram, and a TSV diffable between commits.
Baseline at 9f1a6cf: 35.2% parsed, zero crashes, zero corruption;
24 safety-net refusals reducing to four formatter bugs (event-list
comma -> or, dropped comments in port lists and always blocks,
merged header imports, dropped empty #()) — queued as the next
bug-fix round with corpus files as acceptance inputs.
The corpus proved the README roadmap order wrong: macro invocation
at item/statement position blocks ~60% of RTL parse errors (it
wasn't listed), interfaces (was #1) blocked one file. README
roadmap re-ordered by measured impact.
Dev: Opus agent; QA: Sonnet agent (all claims pass, re-run
byte-identical; fixed a TSV tab-splitting bug); review in
docs/features/corpus-check/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
132 lines
5.7 KiB
Markdown
132 lines
5.7 KiB
Markdown
# vfmt — a Verilog/SystemVerilog formatter in Rust
|
|
|
|
An opinionated, fast formatter for Verilog-2001 and SystemVerilog RTL, built
|
|
in the image of [ruff](https://github.com/astral-sh/ruff)'s formatter, with
|
|
layout conventions borrowed from
|
|
[Verible](https://github.com/chipsalliance/verible)'s `verible-verilog-format`.
|
|
|
|
```bash
|
|
# format files / directories in place (like `ruff format`)
|
|
vfmt rtl/ tb/top_tb.sv
|
|
|
|
# check mode for CI (exit 1 if anything would change)
|
|
vfmt --check rtl/
|
|
|
|
# print a unified diff instead of rewriting (also exit 1 if anything would change)
|
|
vfmt --diff rtl/
|
|
|
|
# skip generated files while walking directories (repeatable)
|
|
vfmt --exclude '*_gen.sv' --exclude build rtl/
|
|
|
|
# pipe through stdin/stdout; --stdin-filename names the buffer in messages
|
|
cat foo.sv | vfmt -
|
|
cat foo.sv | vfmt --stdin-filename rtl/foo.sv -
|
|
```
|
|
|
|
### Exit codes (frozen contract — CI may depend on it)
|
|
|
|
| Code | Meaning |
|
|
|---|---|
|
|
| 0 | Success: files were formatted, or `--check`/`--diff` found nothing to change |
|
|
| 1 | `--check`/`--diff` only: at least one file would be reformatted (nothing written) |
|
|
| 2 | An error occurred (bad arguments, I/O, or a file that could not be formatted); every file involved in an error is left untouched |
|
|
|
|
An error outranks "would reformat": a run with both exits 2.
|
|
|
|
## Architecture (the ruff mapping)
|
|
|
|
The workspace mirrors ruff's crate layout — a comment-preserving parser, a
|
|
generic pretty-printing IR, language formatting rules, and a thin CLI:
|
|
|
|
| This repo | ruff equivalent | Role |
|
|
|---|---|---|
|
|
| `crates/vlog_parser` | `ruff_python_parser` + `ruff_python_ast` | Hand-written lexer and recursive-descent parser producing a comment-preserving AST |
|
|
| `crates/vlog_formatter/src/{doc,printer}.rs` | `ruff_formatter` | Wadler/prettier-style document IR (`Text`/`Line`/`Group`/`Indent`) + width-aware printer |
|
|
| `crates/vlog_formatter/src/format.rs` | `ruff_python_formatter` | Per-node formatting rules |
|
|
| `crates/vfmt` | the `ruff` CLI | File discovery, in-place rewriting, `--check`, stdin mode |
|
|
|
|
Key ideas carried over from ruff/prettier:
|
|
|
|
- **Trivia attachment.** The lexer runs a second phase that folds comments
|
|
into neighboring tokens as *leading*/*trailing* trivia and records blank
|
|
lines; the parser then harvests them onto AST nodes (`Meta`). Comments are
|
|
never dropped — anything not harvested at a precise location is folded into
|
|
the next node's leading comments.
|
|
- **Fit-based layout.** Every breakable construct (port lists, connection
|
|
lists, expressions, concatenations) is a `Group`: it renders on one line if
|
|
it fits within the line limit, otherwise it breaks one-element-per-line.
|
|
A comment inside a group forces it to break.
|
|
- **Idempotency as a hard invariant.** The test suite formats every fixture
|
|
twice and asserts a fixed point.
|
|
|
|
## Style
|
|
|
|
Defaults follow Verible: **2-space indent**, **100-column lines** (both
|
|
configurable via `--indent` / `--line-length`).
|
|
|
|
- One port / parameter / connection per line once a header doesn't fit
|
|
- Cuddled `begin`/`end` (`end else begin`)
|
|
- Tight dimension expressions: `[WIDTH-1:0]`, `[base+:8]`
|
|
- Blank lines between items preserved (collapsed to at most one)
|
|
- `` `define``/`` `ifdef``/`` `include`` and `(* attributes *)` passed
|
|
through verbatim
|
|
|
|
## Supported syntax
|
|
|
|
Basic Verilog-2001 and the common synthesizable SystemVerilog subset:
|
|
|
|
- Modules (ANSI and non-ANSI headers), packages, parameter ports,
|
|
`import`, `timeunit`
|
|
- Net/variable declarations (`wire`/`reg`/`logic`/`int`/... , signing,
|
|
packed/unpacked dimensions, net delays), `parameter`/`localparam`,
|
|
`genvar`, `defparam`
|
|
- `assign` (with delays), `always`/`always_comb`/`always_ff`/`always_latch`,
|
|
`initial`/`final`
|
|
- Full statement set: `begin/end` (labeled), `if`/`else`, all `case` kinds
|
|
with `unique`/`priority`, `for`/`while`/`do`/`repeat`/`forever`/`foreach`,
|
|
`fork/join*`, timing controls (`#`, `@`), `wait`, `disable`, `force`/
|
|
`release`, event triggers, immediate assertions, `break`/`continue`/
|
|
`return`
|
|
- Module/gate instantiation: named and ordered connections, `.*`,
|
|
parameter overrides, instance arrays
|
|
- `generate` regions and module-level `if`/`for`/`case`
|
|
- Functions and tasks (ANSI and old-style port declarations)
|
|
- `typedef` (aliases, `enum`, packed `struct`/`union`)
|
|
- Expressions: the full operator precedence table, ternaries,
|
|
concatenation/replication, slices (`+:`/`-:`), casts (`int'(x)`),
|
|
assignment patterns (`'{...}`), `inside`, hierarchical and package-scoped
|
|
names, macro usages in expression position
|
|
|
|
Not yet supported (parse errors). Roadmap ordered by measured impact on
|
|
real RTL — see `docs/features/corpus-check/README.md` for the 820-file
|
|
corpus baseline this ordering comes from:
|
|
|
|
1. Macro invocation at module-item / statement position
|
|
(`` `ASSERT_INIT(...) `` style) — ≈60% of RTL parse errors in the corpus
|
|
2. `bind` directives
|
|
3. Classes/constraints (mostly blocks DV/UVM code, not RTL)
|
|
4. Dynamic arrays and queues
|
|
5. Declarations with identifier (typedef'd) types in some positions
|
|
(`my_t [N-1:0] x;`)
|
|
6. DPI, concurrent assertions (`assert property`), non-simple casts
|
|
7. Interfaces/modports, `case ... inside`, streaming operators, specify
|
|
blocks, `let`/`checker`, sequences — rare in the corpus RTL
|
|
|
|
Like ruff, `vfmt` refuses to output anything for a file it cannot fully
|
|
parse, so it can never corrupt code.
|
|
|
|
## Contributing / workflow
|
|
|
|
This is a solo project — committing directly to `master` is allowed and
|
|
expected; no PRs or review required.
|
|
|
|
## Building
|
|
|
|
```bash
|
|
cargo build --release # binary at target/release/vfmt
|
|
cargo test # unit + snapshot + idempotency tests
|
|
```
|
|
|
|
Snapshot tests live in `crates/vlog_formatter/tests/fixtures/`; regenerate
|
|
expected outputs with `UPDATE_SNAPSHOTS=1 cargo test -p vlog_formatter`.
|