# bind ## Goal Parse and format the SystemVerilog `bind` directive (IEEE 1800-2017 §23.11) — the second-largest parse blocker in the corpus after `interface`, at 28 files. A directive is ``` bind [ : ] [ #( ) ] ( ); ``` where `` is a module identifier, an interface identifier or an instance name, and **everything after it is exactly an ordinary module instantiation**. That is the whole design point: `bind` reuses the existing `Instantiation` node and the existing `Instantiation` layout code unchanged, so a bound instantiation is laid out byte-for-byte the way the same instantiation written on its own would be — same `#( )` override group, same instance name, same connection-list group, same alignment inference for named connections. ## What is structural, and what is verbatim All structural — a real `ItemKind::Bind(BindDirective)` node with a canonical layout. Nothing is captured verbatim, no new normalization was needed, and `crates/vlog_formatter/src/verify.rs` is untouched. | shape | corpus evidence | |---|---| | target on one line, module + instance on the next, implicit named connections (`.clk_i,`) | `corpus/ibex/vendor/lowrisc_ip/ip/prim/fpv/tb/prim_secded_22_16_bind_fpv.sv:9` (+19 sibling `prim_secded_*_bind_fpv.sv` / `prim_alert_rxtx*_bind_fpv.sv` files) | | with `#( ... )` parameter overrides on the wrapped line | `corpus/ibex/vendor/lowrisc_ip/ip/prim/fpv/tb/prim_esc_rxtx_bind_fpv.sv:8` | | target is an **instance** name, directive inside a module | `corpus/ibex/vendor/lowrisc_ip/ip/prim/fpv/tb/prim_fifo_sync_bind_fpv.sv:17` (12 directives in that one file) | | `.*` connection list | `corpus/ibex/dv/uvm/core_ibex/fcov/core_ibex_fcov_bind.sv:6` | | `#(` glued to the module name (`iface#(`) | `corpus/ibex/dv/uvm/icache/dv/fcov/ibex_icache_fcov_bind.sv:8` | | **implicit named parameter overrides** (`.SecureIbex,` with no parens) + a hand-aligned connection table | `corpus/ibex/dv/verilator/simple_system_cosim/ibex_simple_system_cosim_checker_bind.sv:6` | | two directives at module scope, blank line kept between them | `corpus/opentitan/hw/ip/uart/dv/sva/uart_bind.sv:7,16` | | directive at **top level**, outside any module | `corpus/common_cells/formal/counter_properties.sv:96`, `fifo_v3_properties.sv:134`, `fall_through_register_properties.sv:84` (these three still fail on a *later* blocker, `assume` — category `assertions`, not mine) | **Verified already parsing, no work needed:** implicit named parameter overrides `#(.SecureIbex, .ICache)`, `#(` with no preceding space, and `.*` all went through the existing `parse_connection` / `parse_instantiation` path unchanged — checked against the pre-change release binary before writing any code, so the brief's "check whether the existing `#( )` override parser accepts `.Name` with no parens" is answered *yes*. A case pins it for a plain instantiation too (`bind.cases`, "implicit named *parameter* overrides"). ## Assumptions - **`bind` is deliberately NOT added to the lexer keyword table.** It is a reserved word in IEEE 1800 Table B.1, but promoting it would break `obj.bind()` and `x = obj.bind`, because member access after `.` requires a `TokenKind::Ident` (`parser.rs`, `self.nth(1).kind == TokenKind::Ident`). This follows the call the codebase already makes for `new`/`this`/`super` (see the comment in `token.rs::is_keyword`). A directive is recognized instead by shape, in `Parser::at_bind_directive`: the word `bind`, then a target scope (a name, optionally 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 at that point, so it never matches — pinned by `bind_is_still_a_usable_identifier` in `parser.rs`. A whole-word `grep` over the 820-file corpus found **no** use of `bind` as an identifier, so this is belt-and-braces rather than a live constraint. - **Recognition is item-position only.** `at_bind_directive` is consulted from `parse_item_kind`, so nothing in expression or statement position changes. - **The target is stored as one glued string** (`top.u_dut.u_core`), the same way `Instantiation::module` glues `pkg::mod`. `BindDirective::target_span` therefore slices back to the target only modulo whitespace written around the dots — the caveat `module_span` already documents, and the reason `tests/spans.rs` compares it despaced. - **The head is one flat run of names.** `bind `, then the instantiation. The head never breaks on its own: if the whole directive does not fit, the instantiation's own groups (`#( )` first, then the connection list) break exactly as they would without the `bind` prefix. Verified by formatting the identically wide standalone instantiation and diffing — same output, and the case comments say so. - **A `bind` inside a `` `ifdef `` arm needs nothing special**: directives are pass-through items, so the bind items around them parse normally (case: "an `` `ifdef `` inside the connection list of a bind" plus a manual check of a directive-split around whole directives). ## Non-goals (clean parse error, never wrong output) - **An indexed hierarchical target** — `bind top.u_arr[0].u_core chk u (.*);`. The path scan stops at `[`, so `at_bind_directive` declines and the file fails to parse with `expected ';'`. There is no corpus hit (`grep -rEn 'bind +[A-Za-z_][A-Za-z0-9_.]*\['` → zero), and supporting it means the target stops being a plain name and becomes an expression. Left out under "simplicity first"; the failure mode is an error with the input emitted unchanged, which is the hard invariant, not corruption. - **`bind` in the config-file/`bind_instantiation` sense of resolving the target scope.** See Handshakes. ## Handshakes ### Consumed - `Parser::parse_instantiation` (`crates/vlog_parser/src/parser.rs`) — the whole grammar after the target. `parse_bind` calls it and stores the result verbatim; no fork, no copy. - `Formatter::instantiation` (`crates/vlog_formatter/src/format.rs`) — the whole layout after the target, `;` included. `Formatter::bind` emits `text("bind [: ] ")` and then delegates. Every layout decision listed in the brief (module name, `#(` overrides group, instance name, connection-list group, alignment inference for named connections) applies unchanged because none of them is re-implemented. ### Promised / requested - **New AST variant `ItemKind::Bind(BindDirective)`** in `crates/vlog_parser/src/ast.rs`. Two exhaustive matches in other crates needed an arm; both got the minimal one: - `crates/vlog_formatter/src/format.rs::item_kind` — delegates as above. - `crates/vlog_semantic/src/bind.rs::bind_item` — **request for the vlog_semantic owner.** The arm added records *only the instantiated module name* (`bind_instantiation_module`), so a bind still makes the bound module a resolvable dependency of the file for the workspace index. It deliberately does **not** declare the instance or resolve the connections: a bind instantiates into the *target* scope, so the instance name does not belong to the enclosing scope and the connection expressions name signals in the target module, not here — resolving them against the enclosing scope would manufacture bogus unresolved-name results. Whether the target scope (`b.target`, with `b.target_span` already recorded for go-to-definition) and the connections should resolve through the workspace index is a vlog_semantic design decision, not a parser one. - `crates/vlog_linter/src/visit.rs`, `vlog_formatter/src/{range,fence}.rs` match with a wildcard arm and needed no change; a bind is a leaf item for all three, which is correct (fences and range formatting work at item granularity). - `crates/vlog_parser/tests/spans.rs` — the item walker is exhaustive, so a `Bind` arm was added. The `Instantiation` body was extracted into `check_instantiation` and is now shared by both arms rather than duplicated. ## Status / TODO - [x] Baseline score saved before any change (671/820, 81.8%). - [x] `ItemKind::Bind` + `BindDirective` in `ast.rs`. - [x] `Parser::at_bind_directive` (shape-based, no new keyword) and `Parser::parse_bind`. - [x] `Formatter::bind`, delegating wholesale to `Formatter::instantiation`. - [x] Minimal arms in `vlog_semantic/src/bind.rs` and `tests/spans.rs`. - [x] `crates/vlog_formatter/tests/cases/bind.cases` — 18 cases, one per corpus shape cited above plus the `: instance_list` form, a hierarchical target, top level, inside `generate`, attached comments, a `` `ifdef `` inside the connection list, a `verilog_format: off` fence, both sides of the 100-column boundary, and `obj.bind()`. - [x] Parser-level tests: `bind_directive_forms`, `a_bind_directive_wraps_an_ordinary_instantiation`, `bind_is_still_a_usable_identifier`. - [x] `cargo fmt --all`, `cargo clippy --all-targets`, `cargo test` all clean. - [x] Score after: 699/820 (85.2%), passing set a strict superset. - [x] `TODO.md` and `README.md` updated. ## Work log ### 2026-08-20 — dev - **Baseline.** `cargo build --release && scripts/score.sh` → `{"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}}`. TSV saved; all 28 `bind`-category rows are `expected ';' (found "")`, i.e. the parser was reading `bind ` as a data declaration. - **Keyword decision, made before writing code.** Grepped the whole corpus for a whole-word `bind`: 64 hits, every one of them a real directive or a comment — none is an identifier. Then checked the current binary on `obj.bind(); x = obj.bind;` — it parses today precisely *because* `bind` is an ordinary `Ident`, and member access after `.` demands `TokenKind::Ident`. Adding `bind` to `is_keyword` would have broken it, so recognition is shape-based instead. Recorded in Assumptions with the `new`/`this`/`super` precedent, and pinned by a parser test and a formatter case. - **Also checked before writing code:** whether `#(.SecureIbex, .ICache)` (implicit named parameter overrides, no parens) already parses. It does — `parse_connection` returns `expr: None` for a bare `.name`, and the pre-change binary formats it. So the brief's contingency ("if not, that is in scope for you") did not fire and no instantiation-grammar change was made at all. - **Model.** `ItemKind::Bind(BindDirective { target, target_span, instances, inst })`, with `inst: Instantiation` reused whole. `parse_bind` consumes `bind`, the dotted target, the optional `: a, b` instance list, then calls `parse_instantiation` — which also consumes the `;`. - **Layout.** `Formatter::bind` is four lines: build the head string, then `concat([head, self.instantiation(&b.inst, depth)])`. Confirmed by experiment that this is genuinely the same layout: `bind prim_esc_rxtx_tb prim_esc_rxtx_assert_fpv #(.TimeoutCntDw(6)) …` breaks its `#( )` group and keeps the connection list flat, and a standalone instantiation padded to the same total width makes the identical choice. Alignment inference is likewise untouched — the `ibex_simple_system_cosim_checker_bind` case keeps its hand-aligned connection table. - **After.** `{"passing":699,"total":820,"pct":85.2,"failures_by_category":{"interface":31,"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}}`. `bind` is gone from the histogram: **28 gained, 0 lost**, and `comm -23 ` is empty, so the passing set is a strict superset. - **Still failing in my category: none.** The three `common_cells/formal/*` files that contain a top-level `bind` are counted under `assertions`, not `bind`: they hit an unsupported `assume` (line 38 / 36 / 61) *before* the parser reaches the `bind` at the end of the file. Their directives were extracted and fed through the new binary on their own — all three format cleanly (`bind counter counter_properties #(...) i_counter_properties (.*);` from `counter_properties.sv:96`), so nothing in this category is left over; those files belong to the `assertions` owner. ## QA log ## Review