Skip to content
Merged
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
7 changes: 4 additions & 3 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,8 @@ impl<'p, 'a> Ff<'p, 'a> {
}
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.m.subroutine(&name).cloned() else {
Expand Down Expand Up @@ -7744,8 +7745,8 @@ fn impure_call(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option<String> {
}
continue;
}
// A string operation is a value of its operands.
sv::ir::Callee::String(_) => continue,
// A string operation or lookup is a value of its operands.
sv::ir::Callee::String(_) | sv::ir::Callee::Lookup { .. } => continue,
// 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.
Expand Down
3 changes: 3 additions & 0 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ impl<'a> ProcModule<'a> {
}
continue;
}
// A lookup is a value of its operands.
sv::ir::Callee::Lookup { .. } => continue,
// String operations use the simulator's heap.
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
Expand Down Expand Up @@ -1147,6 +1149,7 @@ impl<'a> ProcModule<'a> {
pub fn is_callee(&self, callee: &sv::ir::Callee) -> bool {
match callee {
sv::ir::Callee::System(function) => function.reads_simulation_state(),
sv::ir::Callee::Lookup { .. } => false,
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => true,
Expand Down
27 changes: 22 additions & 5 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ mod constants;
mod declarations;
mod dimensions;
mod dpi;
mod enums;
mod expression_binding;
mod expressions;
mod ff_process;
Expand Down Expand Up @@ -1205,6 +1206,7 @@ impl Module {
is_2state: function.return_is_2state,
string: function.return_string,
pattern_type: function.return_type.clone(),
enum_domain: function.return_enum_domain.clone(),
},
)
}));
Expand All @@ -1224,6 +1226,7 @@ impl Module {
is_2state: !r#type.is_4state(),
string: false,
pattern_type: None,
enum_domain: None,
},
))
}));
Expand Down Expand Up @@ -1667,6 +1670,8 @@ pub struct Parameter {
is_local: bool,
/// See [`Type::signed_element_depth`].
signed_element_depth: Option<usize>,
/// The enumerated type the parameter is declared with.
enum_domain: Option<Arc<enums::EnumDomain>>,
}

impl Parameter {
Expand All @@ -1691,6 +1696,7 @@ impl Parameter {
has_declared_type,
is_local,
signed_element_depth: None,
enum_domain: None,
}
}

Expand Down Expand Up @@ -2160,8 +2166,11 @@ pub enum PortDirection {
pub struct Type {
container: Option<Arc<ContainerType>>,
is_enum: bool,
/// Resolved legal values for a scalar enum; None if its domain is unsupported.
enum_values: Option<Vec<i128>>,
/// Resolved members of a scalar enum; None if its domain is unsupported.
enum_domain: Option<Arc<enums::EnumDomain>>,
/// A packed array whose elements are of an enumerated type, whose
/// methods are not supported on its elements.
packed_enum: bool,
kind: TypeKind,
is_signed: bool,
packed_ranges: Vec<PackedRange>,
Expand Down Expand Up @@ -2207,7 +2216,8 @@ impl Type {
fn implicit() -> Self {
Self {
is_enum: false,
enum_values: None,
enum_domain: None,
packed_enum: false,
kind: TypeKind::Implicit,
is_signed: false,
packed_ranges: Vec::new(),
Expand All @@ -2224,7 +2234,8 @@ impl Type {
Self {
kind,
is_enum: false,
enum_values: None,
enum_domain: None,
packed_enum: false,
is_signed: false,
packed_ranges: Vec::new(),
unpacked_ranges: Vec::new(),
Expand Down Expand Up @@ -2787,6 +2798,8 @@ struct Function {
return_dimensions: Option<usize>,
return_signed: bool,
return_is_2state: bool,
/// The members of an enumerated return type, for its methods.
return_enum_domain: Option<Arc<enums::EnumDomain>>,
}

fn function_argument_metadata(params: &[FunctionParam]) -> Arc<[FunctionArgumentMetadata]> {
Expand Down Expand Up @@ -2998,7 +3011,9 @@ struct UnpackedDimension {
struct VariableDimensions {
container: Option<Arc<ContainerType>>,
randomizable: bool,
enum_values: Option<Vec<i128>>,
enum_domain: Option<Arc<enums::EnumDomain>>,
/// See [`Type::packed_enum`].
packed_enum: bool,
real: bool,
/// A `string`: each element is a heap handle, not packed bits.
string: bool,
Expand Down Expand Up @@ -3035,6 +3050,8 @@ struct FunctionReturnMetadata {
string: bool,
/// Declared aggregate type, available before function bodies are inlined.
pattern_type: Option<Type>,
/// The members of an enumerated return type, for its methods.
enum_domain: Option<Arc<enums::EnumDomain>>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
Expand Down
15 changes: 15 additions & 0 deletions crates/celox-sv-analyzer/src/ast/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ pub(super) fn expr_is_two_state(
callee: Callee::Checked { .. },
..
} => false,
// The selector is compared with case equality; the result is a value
// or the default.
Expr::Call {
callee: Callee::Lookup { .. },
args,
..
} => {
let key = |index: usize| index % 2 == 1 && index + 1 != args.len();
args.iter().enumerate().skip(1).all(|(index, arg)| {
key(index)
|| arg
.as_ref()
.is_some_and(|arg| expr_is_two_state(state, arg, packed_dimensions))
})
}
Expr::Call {
callee: Callee::Object(op),
..
Expand Down
5 changes: 3 additions & 2 deletions crates/celox-sv-analyzer/src/ast/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl Property {
return None;
}
if self.r#type.is_enum {
self.r#type.enum_values.as_ref().and_then(|values| {
self.r#type.enum_domain.as_ref().and_then(|domain| {
let values = &domain.members;
(1..=256)
.contains(&values.len())
.then_some(values.len() as u16)
Expand Down Expand Up @@ -167,7 +168,7 @@ impl Class {
}
if (property.rand || property.randc)
&& (property.field.handle.is_some()
|| (property.r#type.is_enum && property.r#type.enum_values.is_none()))
|| (property.r#type.is_enum && property.r#type.enum_domain.is_none()))
{
return Err(unsupported(
"class randomize requires integral rand properties with a supported enum domain",
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/ast/comb_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(super) fn expr_static_width(
..
} => Some(op.result_type().0),
Expr::Call {
callee: Callee::Checked { width, .. },
callee: Callee::Checked { width, .. } | Callee::Lookup { width, .. },
..
} => Some(*width),
Expr::Call {
Expand Down
3 changes: 2 additions & 1 deletion crates/celox-sv-analyzer/src/ast/const_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ fn simulation_state_readers(functions: &[Subroutine]) -> HashMap<String, Analyze
Callee::System(_)
| Callee::String(_)
| Callee::Object(_)
| Callee::Checked { .. } => {}
| Callee::Checked { .. }
| Callee::Lookup { .. } => {}
Callee::Subroutine(callee) => calls.push(callee.as_str()),
}
}
Expand Down
Loading
Loading