Structs
A struct is a nominal, optionally parameterized product type packing named fields LSB-first, with optional read-only computed get getters.
Syntax
pub struct Word(w: Pos) {
lo: Int(w),
hi: Int(w),
get sum: Int(w) => lo + hi,
get same => this.lo == this.hi, // Omitted return type defaults to Bits(1)
}Bit Layout (LSB-First)
Fields are packed tightly in declaration order from LSB to MSB:
- Field 0 occupies
[w0 - 1 : 0]. - Field 1 occupies
[w0 + w1 - 1 : w0]. - Field
koccupies[offset_k + wk - 1 : offset_k]. - Total width
S.widthis the sum of all field widths.
Validation Rules
- Field access & partial assignment: Accessing
s.fieldprojects the struct'sAssignPath(AssignStep::Field), allowing disjoint fields of a struct to be assigned independently while rejecting double-writes (multi_assignment) and flagging undriven struct fields (undriven_output/undriven_logic). - Struct literals: Both explicit
MyStruct.{ a: x, b: y }and context-inferred.{ a: x, b: y }(plus field punning.{ a, b }) require every declared field exactly once (missing_struct_fields,duplicate_field,unknown_field). The literal's timing is the LUB of all field timings. - Getters (
get):- Inside a getter body,
this(of the struct type), all fields, and sibling getters are in scope. - Getters are read-only (
assign_path = None) and inherit the receiver's timing. - Direct and mutual recursion cycles between getters are statically rejected (
recursive_getter).
- Inside a getter body,
- Parameterization: Structs may take
UInt,Pos,Bool,LogicType, andTimingparameters withwhereinvariants. Two instantiations are equal iff they reference the same declaration and Z3 proves all parameter arguments equal.
Elaboration & Code Generation
In neo-ir and SystemVerilog (--format sv), a struct lowers to a single packed bit vector of width S.width with LSB-first field slicing and MSB-first concatenation ({hi, lo}).
Example
Source:
struct Pair {
a: Int(32),
b: Int(32),
}
@top
module PassPair(t: Timing) {
input in(t): Pair,
output out(t): Pair,
} {
assign out = .{ a: in.a, b: in.b };
}Generated SystemVerilog (combinational-only, so no __defaultClock port is emitted):
module PassPair (
input logic [63:0] in,
output logic [63:0] out
);
assign out = {in[63:32], in[31:0]};
endmoduleIn FIRRTL (--format firrtl), partial field assignments (e.g., assign out.a = ...; assign out.b = ...;) are automatically combined via neo-firrtl's partial-LValue bit splitter. In ROHD stubs (--format rohd-stub), neo-sv also emits Dart helper classes for LSB-first packing of struct ports.