Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/ast/packed_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod selects;
#[cfg(test)]
mod tests;

pub(super) use layout::native_type;
pub(super) use layout::{AggregateKind, native_type};
pub(super) use selects::{select_member, whole_member};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
20 changes: 14 additions & 6 deletions crates/celox-sv-analyzer/src/ast/packed_structs/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

use super::*;

#[derive(Clone, Copy)]
pub(in crate::ast) enum AggregateKind {
Struct,
HardUnion,
SoftUnion,
}

/// Freeze member bounds in the declaration environment before generated scopes
/// can shadow their parameter names. Shared by both syntax owners.
pub(in crate::ast) fn capture_member_type(
Expand Down Expand Up @@ -30,7 +37,7 @@ pub(in crate::ast) fn capture_member_type(
/// Syntax-independent IEEE7.2.1/7.3.1 layout kernel.
pub(in crate::ast) fn native_type(
state: &AnalysisState,
union: bool,
kind: AggregateKind,
signed: bool,
owner: Arc<str>,
fields: Vec<(String, Type)>,
Expand All @@ -52,11 +59,11 @@ pub(in crate::ast) fn native_type(
})
})
.collect::<Option<Vec<_>>>()?;
finish_type(state, union, signed, members, ranges, env)
finish_type(state, kind, signed, members, ranges, env)
}
fn finish_type(
state: &AnalysisState,
union: bool,
kind: AggregateKind,
signed: bool,
mut members: Vec<PackedMember>,
ranges: Vec<PackedRange>,
Expand All @@ -66,13 +73,14 @@ fn finish_type(
// The first declared member occupies the most significant bits.
for member in members.iter_mut().rev() {
let member_width = expr_type_from_type(state, &member.r#type, const_env)?.width;
if union {
if !matches!(kind, AggregateKind::Struct) {
// IEEE 1800-2023 7.3.1: hard packed members share all bits
// and must have the same width.
if width != 0 && width != member_width {
if matches!(kind, AggregateKind::HardUnion) && width != 0 && width != member_width {
return None;
}
width = member_width;
// Soft members are right-justified in the largest member width.
width = width.max(member_width);
} else {
member.offset = width;
width = width.checked_add(member_width)?;
Expand Down
9 changes: 9 additions & 0 deletions crates/celox-sv-analyzer/src/ast/packed_structs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fn preserves_layout_signedness_and_state_kind() {
mixed_t mixed;
union packed signed { logic signed [7:0] a; bit [7:0] b; } mixed_union;
union packed { bit [129:0] a; bit [129:0] b; } wide_union;
union soft { bit [3:0] narrow_value; bit [15:0] wide_value; } soft_union;
union soft packed signed { logic [15:0] wide_value; bit [3:0] narrow_value; } signed_soft;
union soft { bit [2:0] narrow_value; union soft { bit [129:0] wide; bit flag; } nested; } nested_soft;
signed_t signed_value;
struct packed { byte a; shortint b; int c; longint d; } atoms;
endmodule
Expand All @@ -26,6 +29,9 @@ fn preserves_layout_signedness_and_state_kind() {
("atoms", 120, false, ir::TypeKind::Bit),
("mixed_union", 8, true, ir::TypeKind::Logic),
("wide_union", 130, false, ir::TypeKind::Bit),
("soft_union", 16, false, ir::TypeKind::Bit),
("signed_soft", 16, true, ir::TypeKind::Logic),
("nested_soft", 130, false, ir::TypeKind::Bit),
] {
let ty = signals
.iter()
Expand All @@ -44,6 +50,9 @@ fn rejects_invalid_or_unsupported_struct_shapes() {
"struct { logic a; } value;",
"union packed { logic a; logic [1:0] b; } value;",
"union tagged packed { logic a; logic b; } value;",
"union soft { real a; bit [63:0] b; } value;",
"union soft { logic a[2]; logic b; } value;",
"union soft { logic a; logic a; } value;",
"union packed { logic a; logic b; } value; always_comb value = '{a: 1'b1, b: 1'b0};",
"struct packed { real a; } value;",
"struct packed { logic a[2]; } value;",
Expand Down
14 changes: 12 additions & 2 deletions crates/celox-sv-analyzer/src/ast/type_binding/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(in crate::ast) fn bind_in(
Aggregate {
owner: Arc<str>,
union: bool,
soft: bool,
signed: bool,
names: Vec<String>,
ranges: Vec<PackedRange>,
Expand Down Expand Up @@ -89,11 +90,12 @@ pub(in crate::ast) fn bind_in(
}
DataTypeKind::Struct {
union,
soft,
tagged,
packed,
fields,
} => {
if !packed || *tagged {
if (!packed && !soft) || *tagged {
return Ok(None);
}
let mut names = Vec::new();
Expand Down Expand Up @@ -125,6 +127,7 @@ pub(in crate::ast) fn bind_in(
jobs.push(Job::Aggregate {
owner,
union: *union,
soft: *soft,
signed: data.signed.unwrap_or(false),
names,
ranges,
Expand Down Expand Up @@ -169,6 +172,7 @@ pub(in crate::ast) fn bind_in(
Job::Aggregate {
owner,
union,
soft,
signed,
names,
ranges,
Expand All @@ -180,7 +184,13 @@ pub(in crate::ast) fn bind_in(
let fields = names.into_iter().zip(values.split_off(start)).collect();
let Some(ty) = packed_structs::native_type(
&tree.state,
union,
if soft {
packed_structs::AggregateKind::SoftUnion
} else if union {
packed_structs::AggregateKind::HardUnion
} else {
packed_structs::AggregateKind::Struct
},
signed,
owner,
fields,
Expand Down
18 changes: 18 additions & 0 deletions crates/celox-sv-syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,12 @@ fn lex_with_context(
"::", "+:", "-:", "~&", "~|", "~^", "^~", "->", "=>", "*>", "##", "@@", ":=",
":/", ".*", "(*", "*)",
];
// `:` followed by a comment (`3'b0://`, `x:/*`) is not `:/`.
let comment_after_colon = source[at..].starts_with(":/")
&& matches!(bytes.get(at + 2), Some(b'/' | b'*'));
let length = OPERATORS
.iter()
.filter(|op| !(comment_after_colon && **op == ":/"))
.find(|op| source[at..].starts_with(**op))
.map_or(1, |op| op.len());
at += length;
Expand Down Expand Up @@ -403,6 +407,20 @@ mod tests {
}
}

#[test]
fn colon_before_comment_is_not_dist_weight() {
let text = "3'b0://c\n3'b1:/*c*/ x :/ 2";
let mut sources = SourceMap::default();
let file = sources.add("colon.sv", text);
let tokens = lex(&sources, file).unwrap();
let significant = tokens
.iter()
.filter(|token| !token.is_trivia())
.map(|token| token.text.as_str())
.collect::<Vec<_>>();
assert_eq!(significant, ["3'b0", ":", "3'b1", ":", "x", ":/", "2"]);
}

#[test]
fn strings_and_longest_operators_preserve_lexical_boundaries() {
let text = "\"continued\\\r\nstring\" \"continued\\\nstring\" \"\"\"multi\nline\"\"\" <<<= >>>= |-> |=> ->> <->";
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-syntax/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pub enum DataTypeKind {
TypeOf(ExprId),
Struct {
union: bool,
soft: bool,
tagged: bool,
packed: bool,
fields: Vec<Field>,
Expand Down
6 changes: 6 additions & 0 deletions crates/celox-sv-syntax/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,10 @@ fn datatype_contexts_reject_general_dimension_and_signing_grammar() {
"struct signed {int x;}",
"struct packed {int x;} unsigned",
"struct tagged {int x;}",
"struct soft {int x;}",
"union soft signed {int x;}",
"union soft tagged {int x;}",
"union tagged soft {int x;}",
"struct packed {}",
"union {}",
"enum real {A}",
Expand Down Expand Up @@ -2036,6 +2040,8 @@ fn datatype_contexts_preserve_enum_ranges_and_aggregate_qualifiers() {
"struct packed signed {logic[3:0] x;}",
"union packed unsigned {int x; bit[31:0] y;}",
"union tagged {int x; void nothing;}",
"union soft {int x; bit y;}",
"union soft packed signed {int x; bit y;}",
"enum {A}",
"enum int unsigned {A}",
"enum logic [3:0] {A}",
Expand Down
10 changes: 9 additions & 1 deletion crates/celox-sv-syntax/src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::*;
pub(super) struct Aggregate {
start: usize,
union: bool,
soft: bool,
tagged: bool,
packed: bool,
signed: Option<bool>,
Expand Down Expand Up @@ -166,7 +167,11 @@ impl Parser<'_> {
if !union {
self.expect("struct")?;
}
let tagged = self.eat("tagged");
let soft = self.eat("soft");
if soft && !union {
return Err(self.error("only a union can be soft"));
}
let tagged = !soft && self.eat("tagged");
let packed = self.eat("packed");
let signed = self.signing();
if tagged && !union {
Expand All @@ -179,6 +184,7 @@ impl Parser<'_> {
jobs.push(Job::Type(TypeJob::StructNext(Aggregate {
start,
union,
soft,
tagged,
packed,
signed,
Expand Down Expand Up @@ -424,6 +430,7 @@ impl Parser<'_> {
let Aggregate {
start,
union,
soft,
tagged,
packed,
signed,
Expand All @@ -434,6 +441,7 @@ impl Parser<'_> {
start,
kind: DataTypeKind::Struct {
union,
soft,
tagged,
packed,
fields,
Expand Down
24 changes: 24 additions & 0 deletions crates/celox-test-suite/LIMITATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,27 @@ Verilator 5.052 has two narrower exclusions, recorded in the
The setter's formal is `bit` (18.8), so the required conversion truncates 2 to
0. Generated C++ confirms that Verilator uses 1. The normal 0/1 mode control,
inline member selection and pre-hook control cases pass Verilator.

## SystemVerilog packed unions

The `unions` shared group covers hard and soft packed untagged unions. With
Verilator 5.052, `packed_overlay`, `soft_overlay`, and `soft_signed_ports` pass;
the two four-state cases report `unsupported` through the existing state-mode
check.

Icarus 13.0 rejects `union soft` declarations in `soft_overlay`,
`soft_signed_ports`, and `soft_wide_four_state` during compilation.
`packed_overlay` passes. `packed_mixed_state` fails when reading a `bit` member
from mixed `logic`/`bit` union storage: for `8'b1010xxxx`, it returns
`8'b1010xxxx` instead of `8'b10100000`. A direct procedural probe reproduces
this independently of the shared harness; writing through the `bit` member
converts X to zero correctly. IEEE 1800-2023 7.3.1 requires conversion from
four-state to two-state when reading a two-state member.

These cases remain enabled with their original assertions and no exclusions.
The [retained Icarus evidence](verification/sv/icarus/unions.json) records the
three compilation failures and the read mismatch with their exact diagnostics;
the full-suite gate accepts only those unchanged failures, never a passing
result in their place. Reproduce with `verify-sv-verilator` or
`verify-sv-icarus` and `--filter unions::`. Normal reports are written under
`target/sv-verilator/` and `target/sv-icarus/`.
4 changes: 4 additions & 0 deletions crates/celox-test-suite/src/sv/cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub(super) struct Group {
}

pub(super) const GROUPS: &[Group] = &[
Group {
file: "src/sv/cases/unions.vtest",
text: include_str!("unions.vtest"),
},
Group {
file: "src/sv/cases/real_extensions.vtest",
text: include_str!("real_extensions.vtest"),
Expand Down
Loading
Loading