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
8 changes: 4 additions & 4 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,8 @@ chapter-7/arrays/associative/arguments.sv 2state fail
chapter-7/arrays/associative/arguments.sv 4state fail
chapter-7/arrays/associative/assignment.sv 2state pass
chapter-7/arrays/associative/assignment.sv 4state pass
chapter-7/arrays/associative/class.sv 2state fail
chapter-7/arrays/associative/class.sv 4state fail
chapter-7/arrays/associative/class.sv 2state pass
chapter-7/arrays/associative/class.sv 4state pass
chapter-7/arrays/associative/integral.sv 2state pass
chapter-7/arrays/associative/integral.sv 4state pass
chapter-7/arrays/associative/literals.sv 2state fail
Expand Down Expand Up @@ -1451,8 +1451,8 @@ chapter-7/structures/packed/signed.sv 2state pass
chapter-7/structures/packed/signed.sv 4state pass
chapter-7/structures/packed/unsigned.sv 2state pass
chapter-7/structures/packed/unsigned.sv 4state pass
chapter-7/structures/unpacked/basic.sv 2state fail
chapter-7/structures/unpacked/basic.sv 4state fail
chapter-7/structures/unpacked/basic.sv 2state pass
chapter-7/structures/unpacked/basic.sv 4state pass
chapter-7/structures/unpacked/default-value.sv 2state fail
chapter-7/structures/unpacked/default-value.sv 4state fail
chapter-7/unions/packed/basic.sv 2state pass
Expand Down
25 changes: 25 additions & 0 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3446,6 +3446,31 @@ impl<'p, 'a> Comb<'p, 'a> {
.cloned()
.flatten()
.or_else(|| param.default.clone());
if param.direction.is_reference() && !param.leaves.is_empty() {
// Each member of a formal of unpacked structure type names
// the matching member variable of the actual.
let targets = match &actual {
Some(sv::ir::Expr::Concat(parts)) if parts.len() == param.leaves.len() => parts
.iter()
.map(|part| match part {
sv::ir::Expr::Ident(actual) => self.m.id(actual),
_ => None,
})
.collect::<Option<Vec<_>>>(),
_ => None,
}
.ok_or_else(|| {
unsupported(format!("ref argument `{}` of `{name}`", param.source_name))
})?;
for (leaf, target) in param.leaves.iter().zip(targets) {
let id = self
.m
.id(leaf)
.ok_or_else(|| unsupported(format!("argument `{}`", param.source_name)))?;
references.push((leaf.clone(), id, target));
}
continue;
}
if param.direction.is_reference() {
// A reference formal names the actual variable itself
// (IEEE 1800-2023 13.5.2); other actuals fall back to Ff.
Expand Down
206 changes: 179 additions & 27 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3138,11 +3138,24 @@ impl<'p, 'a> Ff<'p, 'a> {
&mut self,
lhs: &sv::ir::LValue,
rhs: &sv::ir::Expr,
) -> Result<(), sv::AnalyzerError> {
self.assign_value(lhs, rhs, true)
}

/// Assign `rhs` to `lhs`; `convert` turns an integral value assigned to
/// a string variable into its bytes. The parts of a concatenation target
/// take the bits of the value instead: a part that is a string is a
/// member of an unpacked structure, and its bits are a handle.
fn assign_value(
&mut self,
lhs: &sv::ir::LValue,
rhs: &sv::ir::Expr,
convert: bool,
) -> Result<(), sv::AnalyzerError> {
let lhs = self.propagate_lvalue(lhs);
let lhs = self.hoist_lvalue(&lhs)?;
let converted = match self.m.id(lhs.name()) {
Some(id) if matches!(lhs, sv::ir::LValue::Ident(_)) => {
Some(id) if convert && matches!(lhs, sv::ir::LValue::Ident(_)) => {
self.string_conversion(id, rhs.clone())
}
_ => rhs.clone(),
Expand Down Expand Up @@ -3609,9 +3622,11 @@ impl<'p, 'a> Ff<'p, 'a> {
let mut arena = SLTNodeArena::new();
let node = self.expr_slt(rhs, Some((total, signed)), &mut arena)?;
let node = coerce_node_width(&mut arena, node, Some(total), signed).map_err(slt_error)?;
// Capture the value before any part is written.
let (temp, temp_name) = self.m.temp("concat", total, false, true);
// Capture the value before any part is written, in a temporary of
// the value's state kind.
let value = self.lower_slt(&arena, node)?;
let four_state = matches!(self.b.register(&value), RegisterType::Logic { .. });
let (temp, temp_name) = self.m.temp("concat", total, false, four_state);
self.store(temp, SIROffset::Static(0), total, value);
let mut lsb = total;
for (part, width) in parts.iter().zip(widths) {
Expand All @@ -3622,7 +3637,7 @@ impl<'p, 'a> Ff<'p, 'a> {
lsb: sv::ir::ConstExpr::Literal(lsb.to_string()),
signed: false,
};
self.assign(part, &slice)?;
self.assign_value(part, &slice, false)?;
}
Ok(())
}
Expand Down Expand Up @@ -4613,12 +4628,12 @@ impl<'p, 'a> Ff<'p, 'a> {
let mut activation = Vec::new();
if self.kernel.is_some() && (subroutine.is_task || self.forked) {
for name in &subroutine.automatic_locals {
// A reference formal has no storage of its own.
if subroutine
.params
.iter()
.any(|param| &param.name == name && param.direction.is_reference())
{
// A reference formal, or a member of one, has no storage of
// its own.
if subroutine.params.iter().any(|param| {
param.direction.is_reference()
&& (&param.name == name || param.leaves.contains(name))
}) {
continue;
}
let original = self
Expand Down Expand Up @@ -4686,11 +4701,42 @@ impl<'p, 'a> Ff<'p, 'a> {
.cloned()
.flatten()
.or_else(|| param.default.clone());
match self.reference(name, param, actual)? {
Reference::Variable(target) => {
references.push((param.name.clone(), id, target));
if param.leaves.is_empty() {
match self.reference(name, param, actual)? {
Reference::Variable(target) => {
references.push((param.name.clone(), id, target));
}
Reference::Alias(alias) => aliases.push(*alias),
}
continue;
}
// Each member of a formal of unpacked structure type refers to
// the matching member of the actual, which the analyzer binds as
// the concatenation of its members.
let parts = match actual {
Some(sv::ir::Expr::Concat(parts)) if parts.len() == param.leaves.len() => parts,
_ => {
return Err(unsupported(format!(
"ref argument `{}` of `{name}`",
param.source_name
)));
}
};
for (leaf, part) in param.leaves.iter().zip(parts) {
let id = self
.m
.id(leaf)
.ok_or_else(|| unsupported(format!("argument `{}`", param.source_name)))?;
let member = sv::ir::SubroutineParam {
name: leaf.clone(),
leaves: Vec::new(),
default: None,
..param.clone()
};
match self.reference(name, &member, Some(part))? {
Reference::Variable(target) => references.push((leaf.clone(), id, target)),
Reference::Alias(alias) => aliases.push(*alias),
}
Reference::Alias(alias) => aliases.push(*alias),
}
}
// An input evaluates into a register, which a host request in a later
Expand Down Expand Up @@ -4733,8 +4779,12 @@ impl<'p, 'a> Ff<'p, 'a> {
.ok_or_else(|| unsupported("missing output argument"))?;
// A reference formal bound to a class property, forwarded:
// its object was evaluated when the reference was made.
if let Some(store) = forwarded_property(reference, &param.name) {
if let Some(store) =
forwarded_property(reference, sv::ir::Expr::Ident(param.name.clone()))
{
property_outputs.push(store);
} else if let Some(stores) = forwarded_members(reference, &param.name) {
property_outputs.extend(stores);
} else {
let lvalues = lvalue_from_expr(reference).ok_or_else(|| {
unsupported(format!(
Expand All @@ -4744,7 +4794,10 @@ impl<'p, 'a> Ff<'p, 'a> {
})?;
let guards = tagged_actual_guards(reference);
self.check_tagged_guards(&guards)?;
outputs.push((param.name.clone(), id, lvalues, guards));
// The members of an unpacked structure take the formal's
// bits, even a single string or handle member.
let concat = matches!(reference, sv::ir::Expr::Concat(_));
outputs.push((param.name.clone(), id, lvalues, guards, concat));
}
}
if param.direction.is_read() {
Expand Down Expand Up @@ -4841,6 +4894,7 @@ impl<'p, 'a> Ff<'p, 'a> {
let constant = |position: &sv::ir::ConstExpr| {
sv::typecheck::eval_const_expr_with_types(position, constants, parameter_types)
};
self.split_property_concats(&mut body, &aliases);
for alias in &aliases {
body = alias.rewrite(body, &constant)?;
}
Expand All @@ -4855,10 +4909,12 @@ impl<'p, 'a> Ff<'p, 'a> {
self.jump(return_block);
}
self.b.switch_to_block(return_block);
for (formal, id, lvalues, guards) in outputs {
for (formal, id, lvalues, guards, concat) in outputs {
self.check_tagged_guards(&guards)?;
let signed = self.m.var(id).signed;
if let [lvalue] = lvalues.as_slice() {
if let [lvalue] = lvalues.as_slice()
&& !concat
{
self.assign(lvalue, &sv::ir::Expr::Ident(formal))?;
} else {
let _ = signed;
Expand All @@ -4882,6 +4938,67 @@ impl<'p, 'a> Ff<'p, 'a> {
Ok(result)
}

/// Rewrite each assignment in `body` to a concatenation that has members
/// of a structure reference formal bound to a class property, which
/// [`references::Alias::rewrite`] stores one property at a time: the
/// concatenation writes temporaries in their place, so every position
/// of its other parts is taken before any write, and each property is
/// then stored from its temporary.
fn split_property_concats(&mut self, body: &mut [sv::ir::Stmt], aliases: &[references::Alias]) {
let properties: HashSet<&str> = aliases
.iter()
.filter(|alias| matches!(alias.target, references::Target::Property { .. }))
.map(|alias| alias.formal.as_str())
.collect();
if properties.is_empty() {
return;
}
for stmt in body {
stmt.walk_mut(&mut |stmt| {
let sv::ir::Stmt::AssignConcat {
parts,
nonblocking,
..
} = stmt
else {
return;
};
if !parts.iter().any(|part| properties.contains(part.name())) {
return;
}
let nonblocking = *nonblocking;
let mut stores = Vec::new();
for part in parts.iter_mut() {
if !matches!(part, sv::ir::LValue::Ident(name) if properties.contains(name.as_str()))
{
continue;
}
// A select of a member is left to the rewrite to reject.
let Ok(width) = self.lvalue_width(part) else {
continue;
};
let (_, temp) = self.m.temp("ref_member", width, false, self.m.four_state);
let member = std::mem::replace(part, sv::ir::LValue::Ident(temp.clone()));
stores.push(sv::ir::Stmt::Assign {
lhs: member,
rhs: sv::ir::Expr::Ident(temp),
nonblocking,
});
}
if stores.is_empty() {
return;
}
let assignment = std::mem::replace(stmt, sv::ir::Stmt::Break);
stores.insert(0, assignment);
*stmt = sv::ir::Stmt::If {
condition: sv::ir::Expr::Literal("1'b1".into()),
then_body: stores,
else_body: Vec::new(),
};
});
}
}

/// What reference formal `param` of `name` refers to for a call with
/// `actual`. The object of a property is evaluated here, at the call.
fn reference(
Expand Down Expand Up @@ -7820,14 +7937,15 @@ fn impure_call(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option<String> {
// Container contents are immutable: reading an element is a
// value of the container handle and index, unlike a class
// property that can change without its handle changing.
// A record of a structure element is immutable as well.
sv::ir::Callee::Object(op)
if matches!(
*op,
sv::ir::ObjectOp::Container {
op: sv::ir::ContainerOp::Get,
..
}
) =>
} | sv::ir::ObjectOp::Unbox(_)
) || matches!(*op, sv::ir::ObjectOp::Load(field) if field.nullable) =>
{
continue;
}
Expand Down Expand Up @@ -8359,10 +8477,10 @@ enum Reference {
Alias(Box<references::Alias>),
}

/// The store that copies formal `formal` back into the class property that
/// `actual` reads through the object of a reference formal (see
/// The store that copies `value` back into the class property that `actual`
/// reads through the object of a reference formal (see
/// [`references::Alias`]), when it is such a read.
fn forwarded_property(actual: &sv::ir::Expr, formal: &str) -> Option<sv::ir::Stmt> {
fn forwarded_property(actual: &sv::ir::Expr, value: sv::ir::Expr) -> Option<sv::ir::Stmt> {
let sv::ir::Expr::Call {
callee: sv::ir::Callee::Object(op),
args,
Expand All @@ -8379,11 +8497,45 @@ fn forwarded_property(actual: &sv::ir::Expr, formal: &str) -> Option<sv::ir::Stm
handle.starts_with(references::OBJECT_TEMP).then(|| {
sv::ir::Stmt::Eval(sv::ir::Expr::Call {
callee: sv::ir::Callee::Object(Box::new(sv::ir::ObjectOp::Store(*field))),
args: vec![
Some(sv::ir::Expr::Ident(handle.clone())),
Some(sv::ir::Expr::Ident(formal.to_owned())),
],
args: vec![Some(sv::ir::Expr::Ident(handle.clone())), Some(value)],
arg_order: Vec::new(),
})
})
}

/// The stores that copy the members of formal `formal` of unpacked
/// structure type back into the class properties that `actual`, the members
/// of a structure reference formal bound to a property, read.
fn forwarded_members(actual: &sv::ir::Expr, formal: &str) -> Option<Vec<sv::ir::Stmt>> {
let sv::ir::Expr::Concat(parts) = actual else {
return None;
};
let widths = parts
.iter()
.map(|part| match part {
sv::ir::Expr::Call {
callee: sv::ir::Callee::Object(op),
..
} => match **op {
sv::ir::ObjectOp::Load(field) => Some(field.width as usize),
_ => None,
},
_ => None,
})
.collect::<Option<Vec<_>>>()?;
let mut lsb = widths.iter().sum::<usize>();
parts
.iter()
.zip(widths)
.map(|(part, width)| {
lsb -= width;
let bits = sv::ir::Expr::Select {
expr: Box::new(sv::ir::Expr::Ident(formal.to_owned())),
msb: sv::ir::ConstExpr::Literal((lsb + width - 1).to_string()),
lsb: sv::ir::ConstExpr::Literal(lsb.to_string()),
signed: false,
};
forwarded_property(part, bits)
})
.collect()
}
Loading
Loading