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
20 changes: 10 additions & 10 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1177,16 +1177,16 @@ chapter-6/6.19.4--enum_numerical_expr_cast.sv 2state pass
chapter-6/6.19.4--enum_numerical_expr_cast.sv 4state pass
chapter-6/6.19.4--enum_numerical_expr_no_cast.sv 2state fail
chapter-6/6.19.4--enum_numerical_expr_no_cast.sv 4state fail
chapter-6/6.19.5.1--enum_first.sv 2state fail
chapter-6/6.19.5.1--enum_first.sv 4state fail
chapter-6/6.19.5.2--enum_last.sv 2state fail
chapter-6/6.19.5.2--enum_last.sv 4state fail
chapter-6/6.19.5.3--enum_next.sv 2state fail
chapter-6/6.19.5.3--enum_next.sv 4state fail
chapter-6/6.19.5.4--enum_prev.sv 2state fail
chapter-6/6.19.5.4--enum_prev.sv 4state fail
chapter-6/6.19.5.5--enum_num.sv 2state fail
chapter-6/6.19.5.5--enum_num.sv 4state fail
chapter-6/6.19.5.1--enum_first.sv 2state pass
chapter-6/6.19.5.1--enum_first.sv 4state pass
chapter-6/6.19.5.2--enum_last.sv 2state pass
chapter-6/6.19.5.2--enum_last.sv 4state pass
chapter-6/6.19.5.3--enum_next.sv 2state pass
chapter-6/6.19.5.3--enum_next.sv 4state pass
chapter-6/6.19.5.4--enum_prev.sv 2state pass
chapter-6/6.19.5.4--enum_prev.sv 4state pass
chapter-6/6.19.5.5--enum_num.sv 2state pass
chapter-6/6.19.5.5--enum_num.sv 4state pass
chapter-6/6.19.5.6--enum_name.sv 2state fail
chapter-6/6.19.5.6--enum_name.sv 4state fail
chapter-6/6.20.2--parameter.sv 2state pass
Expand Down
119 changes: 116 additions & 3 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,8 @@ fn kernel_written_names(
}
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => continue,
| sv::ir::Callee::Checked { .. }
| sv::ir::Callee::Lookup { .. } => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = subroutines
Expand Down Expand Up @@ -1671,7 +1672,8 @@ impl DriverScan<'_> {
}
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => continue,
| sv::ir::Callee::Checked { .. }
| sv::ir::Callee::Lookup { .. } => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = self
Expand Down Expand Up @@ -4944,6 +4946,56 @@ pub(crate) fn attach_instance_glue(
Ok(())
}

/// A [`sv::ir::Callee::Lookup`] call as nested multiplexers of case
/// equalities. The selector is lowered once and its node shared by every
/// comparison, so nested lookups lower in linear time. `lower` lowers an
/// operand with a context width and signedness.
fn lower_lookup<A: std::hash::Hash + Eq + Clone>(
args: &[Option<sv::ir::Expr>],
width: usize,
signed: bool,
arena: &mut SLTNodeArena<A>,
lower: &mut dyn FnMut(
&sv::ir::Expr,
&mut SLTNodeArena<A>,
Option<usize>,
Option<bool>,
) -> Option<NodeId>,
) -> Option<NodeId> {
let (selector, rest) = args.split_first()?;
let (default, pairs) = rest.split_last()?;
let (pairs, []) = pairs.as_chunks::<2>() else {
return None;
};
let selector = lower(selector.as_ref()?, arena, None, None)?;
let resized = |expr: &Option<sv::ir::Expr>| {
Some(sv::ir::Expr::Resize {
expr: Box::new(expr.clone()?),
width,
signed,
})
};
let mut result = lower(&resized(default)?, arena, Some(width), Some(signed))?;
for [key, value] in pairs.iter().rev() {
let key = lower(key.as_ref()?, arena, None, None)?;
let compared = celox_slt::get_width(selector, arena).max(celox_slt::get_width(key, arena));
let left = coerce_node_width(arena, selector, Some(compared), false).ok()?;
let right = coerce_node_width(arena, key, Some(compared), false).ok()?;
let cond = arena
.alloc(SLTNode::Binary(left, BinaryOp::EqCase, right))
.ok()?;
let then_expr = lower(&resized(value)?, arena, Some(width), Some(signed))?;
result = arena
.alloc(SLTNode::Mux {
cond,
then_expr,
else_expr: result,
})
.ok()?;
}
Some(result)
}

/// `expr inside { items }` as the comparisons the language defines: a value
/// matches by wildcard equality, a range by an inclusive bounds check.
fn inside_as_comparisons(expr: &sv::ir::Expr, items: &[sv::ir::InsideItem]) -> sv::ir::Expr {
Expand Down Expand Up @@ -7231,6 +7283,38 @@ fn lower_glue_parent_expr(
source_ids,
))
}
sv::ir::Expr::Call {
callee: sv::ir::Callee::Lookup { width, signed },
args,
..
} => {
let mut sources = HashSet::default();
let mut source_ids = Vec::new();
let node = lower_lookup(
args,
*width,
*signed,
arena,
&mut |expr, arena, context_width, context_signed| {
let (node, expr_sources, expr_source_ids) = lower_glue_parent_expr(
expr,
variables,
name_to_id,
constants,
parameter_types,
arena,
context_width,
context_signed,
)?;
sources.extend(expr_sources);
source_ids.extend(expr_source_ids);
Some(node)
},
)?;
source_ids.sort();
source_ids.dedup();
Some((node, sources, source_ids))
}
sv::ir::Expr::Inside { expr, items } => lower_glue_parent_expr(
&inside_as_comparisons(expr, items),
variables,
Expand Down Expand Up @@ -8634,6 +8718,34 @@ fn lower_expr_with_context(
sources,
))
}
sv::ir::Expr::Call {
callee: sv::ir::Callee::Lookup { width, signed },
args,
..
} => {
let mut sources = HashSet::default();
let node = lower_lookup(
args,
*width,
*signed,
arena,
&mut |expr, arena, context_width, context_signed| {
let (node, expr_sources) = lower_expr_with_context(
expr,
variables,
name_to_id,
constants,
parameter_types,
arena,
context_width,
context_signed,
)?;
sources.extend(expr_sources);
Some(node)
},
)?;
Some((node, sources))
}
sv::ir::Expr::Inside { expr, items } => lower_expr_with_context(
&inside_as_comparisons(expr, items),
variables,
Expand Down Expand Up @@ -10856,7 +10968,8 @@ mod tests {
}
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => continue,
| sv::ir::Callee::Checked { .. }
| sv::ir::Callee::Lookup { .. } => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = self
Expand Down
20 changes: 20 additions & 0 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,7 @@ impl<'p, 'a> Comb<'p, 'a> {
// Copy the inputs in, left to right, in the caller's scope.
let mut outputs = Vec::new();
let mut copied = Vec::new();
let mut references = Vec::new();
let positions: Vec<_> = if arg_order.is_empty() {
(0..subroutine.params.len()).collect()
} else {
Expand All @@ -3445,6 +3446,19 @@ impl<'p, 'a> Comb<'p, 'a> {
.cloned()
.flatten()
.or_else(|| param.default.clone());
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.
let target = match &actual {
Some(sv::ir::Expr::Ident(actual)) => self.m.id(actual),
_ => None,
}
.ok_or_else(|| {
unsupported(format!("ref argument `{}` of `{name}`", param.source_name))
})?;
references.push((param.name.clone(), id, target));
continue;
}
if param.direction.is_written() {
let actual = actual.as_ref().and_then(lvalue_from_expr).ok_or_else(|| {
unsupported(format!(
Expand Down Expand Up @@ -3510,6 +3524,9 @@ impl<'p, 'a> Comb<'p, 'a> {
let width = self.m.var(id).width;
self.write(store, id, full(width), value)?;
}
for (formal, _, target) in &references {
self.m.name_to_id.insert(formal.clone(), *target);
}
let return_var = match &subroutine.return_var {
Some(name) => {
let id = self
Expand All @@ -3533,6 +3550,9 @@ impl<'p, 'a> Comb<'p, 'a> {
self.active_calls.push(name.to_string());
let result = self.exec_block(store.fork(), &call_frames, &subroutine.body);
self.active_calls.pop();
for (formal, original, _) in references {
self.m.name_to_id.insert(formal, original);
}
*store = result?;
// Copy the outputs back.
for (id, lvalues) in outputs {
Expand Down
Loading
Loading