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
48 changes: 46 additions & 2 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub(crate) struct LoweredSvPortConnection {
pub formal: String,
pub actual: String,
pub actual_expr: Option<sv::ir::Expr>,
pub bound_pattern: Option<sv::ast::BoundPattern>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -3180,6 +3181,7 @@ fn lower_module_with_overrides(
formal: connection.formal().to_string(),
actual: connection.actual().to_string(),
actual_expr: connection.actual_expr().cloned(),
bound_pattern: connection.bound_pattern().cloned(),
})
.collect(),
array_element,
Expand Down Expand Up @@ -3454,6 +3456,7 @@ fn array_element_connections(
return Ok(LoweredSvPortConnection {
formal: connection.formal.clone(),
actual: connection.actual.clone(),
bound_pattern: None,
actual_expr: Some(sv::ir::Expr::Select {
expr: Box::new(actual_expr.clone()),
msb: sv::ir::ConstExpr::Literal((lsb + port_width - 1).to_string()),
Expand Down Expand Up @@ -3489,11 +3492,50 @@ fn array_element_connections(
formal: connection.formal.clone(),
actual: connection.actual.clone(),
actual_expr: Some(slice),
bound_pattern: None,
})
})
.collect()
}

fn bind_port_patterns(
connections: &mut [LoweredSvPortConnection],
child: &LoweredSvModule,
) -> Result<(), ParserError> {
for connection in connections {
let Some(pattern) = connection.bound_pattern.take() else {
continue;
};
let port = child
.source
.ports()
.iter()
.find(|port| port.name() == connection.formal)
.ok_or_else(|| {
ParserError::illegal_context(
"systemverilog port assignment pattern",
format!("unknown port `{}`", connection.formal),
None,
)
})?;
if port.direction() != sv::ir::PortDirection::Input {
return Err(ParserError::illegal_context(
"systemverilog port assignment pattern",
"only input port assignment patterns are supported",
None,
));
}
connection.actual_expr = Some(port.bind_pattern(&pattern).map_err(|error| {
ParserError::illegal_context(
"systemverilog port assignment pattern",
error.to_string(),
None,
)
})?);
}
Ok(())
}

pub(crate) fn attach_instance_glue(
module: &mut SimModule,
lowered: &LoweredSvModule,
Expand Down Expand Up @@ -3525,6 +3567,8 @@ pub(crate) fn attach_instance_glue(
let Some(child) = lowered_modules.get(&child_id) else {
return Err(unsupported_sv_instance(instance.module_name.clone()));
};
let mut authored_connections = instance.port_connections.clone();
bind_port_patterns(&mut authored_connections, child)?;
let connections = match instance.array_element {
Some((position, count)) => {
module
Expand All @@ -3536,7 +3580,7 @@ pub(crate) fn attach_instance_glue(
.insert(instance.instance_name.clone(), instance.array_index_base);
}
array_element_connections(
&instance.port_connections,
&authored_connections,
child,
position,
count,
Expand All @@ -3546,7 +3590,7 @@ pub(crate) fn attach_instance_glue(
&lowered.parameter_types,
)?
}
None => instance.port_connections.clone(),
None => authored_connections,
};
ensure_parent_output_signals(
module,
Expand Down
9 changes: 8 additions & 1 deletion crates/celox-sv-analyzer/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
),
port.is_net(),
)
.with_default(port.default().cloned().map(Into::into)),
.with_default(port.default().cloned().map(Into::into))
.with_pattern_target(port.r#type().pattern_target(
&ast::const_functions::Environment {
constants: &constants,
functions: &state.const_functions,
},
)),
);
}
let signals = module
Expand Down Expand Up @@ -172,6 +178,7 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
connection.formal().to_string(),
connection.actual().to_string(),
connection.actual_expr().cloned().map(Into::into),
connection.bound_pattern().cloned(),
)
})
.collect(),
Expand Down
38 changes: 32 additions & 6 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ use dimensions::{
use expressions::guard_zero_divisions;
use ff_process::ff_processes_from_scope;
use functions::{functions_from_scope, procedural_truth_condition};
use inlining::{
expand_expr_calls, expr_signedness, expr_signedness_with_return_types, substitute_expr_idents,
};
use inlining::{expand_expr_calls, expr_signedness_with_return_types, substitute_expr_idents};
pub(crate) use instances::normalize_identifier;
use instances::{collect_connected_nets, expr_ident_name, instances_from_scope};
use parameters::{
Expand Down Expand Up @@ -1054,6 +1052,7 @@ impl Module {
signed: function.return_signed,
is_2state: function.return_is_2state,
string: function.return_string,
pattern_type: function.return_type.clone(),
},
)
}));
Expand All @@ -1072,6 +1071,7 @@ impl Module {
signed: r#type.is_signed(),
is_2state: !r#type.is_4state(),
string: false,
pattern_type: None,
},
))
}));
Expand All @@ -1081,7 +1081,10 @@ impl Module {
packed_dimensions.expression_signedness = expression_signedness.clone().into();
for instance in &mut instances {
for connection in &mut instance.port_connections {
connection.actual_expr = connection.actual_expr.take().map(|expr| {
if let Some(pattern) = &mut connection.bound_pattern {
pattern.resolve_types(&packed_dimensions)?;
}
connection.map_expressions(|expr| {
let expr = expand_expr_calls(expr, &functions, &expression_signedness, 0, true);
substitute_expr_constants_with_parameter_literals(
&syntax_tree.state,
Expand Down Expand Up @@ -1214,6 +1217,11 @@ impl Module {
if let Some(expr) = connection.actual_expr() {
collect_connected_nets(expr, &mut connected_nets);
}
if let Some(pattern) = connection.bound_pattern() {
pattern.visit_expressions(&mut |expr| {
collect_connected_nets(expr, &mut connected_nets)
});
}
}
if let Some(signal) = signals.iter().find(|signal| {
!imported.declarations_only
Expand Down Expand Up @@ -1265,8 +1273,7 @@ impl Module {
}
for instance in &mut instances {
for connection in &mut instance.port_connections {
connection.actual_expr =
connection.actual_expr.take().map(|expr| rename.expr(expr));
connection.map_expressions(|expr| rename.expr(expr));
}
}
}
Expand Down Expand Up @@ -1890,6 +1897,7 @@ pub struct PortConnection {
formal: String,
actual: String,
actual_expr: Option<Expr>,
bound_pattern: Option<BoundPattern>,
}

impl PortConnection {
Expand All @@ -1898,6 +1906,20 @@ impl PortConnection {
formal,
actual,
actual_expr,
bound_pattern: None,
}
}

/// An untyped assignment pattern with caller-owned values. The receiving
/// port supplies its element types after parameter specialization.
pub fn bound_pattern(&self) -> Option<&BoundPattern> {
self.bound_pattern.as_ref()
}

fn map_expressions(&mut self, mut map: impl FnMut(Expr) -> Expr) {
self.actual_expr = self.actual_expr.take().map(&mut map);
if let Some(pattern) = &mut self.bound_pattern {
pattern.map_expressions(&mut map);
}
}

Expand Down Expand Up @@ -2435,6 +2457,8 @@ impl ConditionalAssignment {

#[derive(Debug, Clone, PartialEq, Eq)]
struct Function {
/// The aggregate return type, frozen in its declaration scope.
return_type: Option<Type>,
return_real: bool,
/// Whether the function returns a `string`.
return_string: bool,
Expand Down Expand Up @@ -2711,6 +2735,8 @@ struct FunctionReturnMetadata {
is_2state: bool,
/// The function returns a `string`.
string: bool,
/// Declared aggregate type, available before function bodies are inlined.
pattern_type: Option<Type>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
Expand Down
Loading
Loading