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
1 change: 1 addition & 0 deletions crates/celox-frontend-core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ pub fn lower_frontend_artifact(
reset_clock_map,
processes,
package_bindings: HashMap::default(),
hierarchical_bindings: HashMap::default(),
counter_slots,
pending_slots: Vec::new(),
comb_alternatives: Vec::new(),
Expand Down
27 changes: 27 additions & 0 deletions crates/celox-frontend-core/src/symbolic/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ pub struct SimModule {
/// 1800-2023 26.2). Every instance of the module refers to that one
/// object instead of a state object of its own.
pub package_bindings: HashMap<SourceVarId, PackageBinding>,
/// Variables of the module that denote a variable of another instance,
/// named by a hierarchical name (IEEE 1800-2023 23.6). Each instance of
/// the module refers to the object its binding locates from it.
pub hierarchical_bindings: HashMap<SourceVarId, HierarchicalBinding>,
/// Counter objects of the signals that a unit of the module may store
/// twice between two runtime checks (see
/// `crate::watch::multi_store_candidates`). Assembly counts a signal's
Expand Down Expand Up @@ -191,6 +195,28 @@ impl<A: Copy> NbaSwitch<A> {
}
}

/// The variable of another instance a module variable denotes.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct HierarchicalBinding {
pub start: HierarchicalStart,
/// The instance path segments from the start instance to the instance
/// that holds the target, as in [`crate::InstancePath`].
pub path: Vec<(String, usize)>,
/// The target variable, by the module of that instance: instances of
/// one module may reach instances of different modules.
pub targets: Vec<(ModuleId, SourceVarId)>,
}

/// Where the instance path of a [`HierarchicalBinding`] starts.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HierarchicalStart {
/// The top instance.
Root,
/// The ancestor that many levels above the instance holding the
/// variable; 0 is that instance.
Ancestor(usize),
}

/// The package variable a module variable denotes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PackageBinding {
Expand Down Expand Up @@ -225,6 +251,7 @@ impl fmt::Debug for SimModule {
.field("reset_clock_map", &self.reset_clock_map)
.field("processes", &self.processes)
.field("package_bindings", &self.package_bindings)
.field("hierarchical_bindings", &self.hierarchical_bindings)
.finish()
}
}
Expand Down
139 changes: 129 additions & 10 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use num_bigint::BigUint;
use std::collections::{BTreeMap, BTreeSet};

use crate::symbolic::artifact::{
FfPart, NbaSwitch, RelocationModule, SimModule, SplitCommit, SymbolicGlueAddr as GlueAddr,
SymbolicRtl,
FfPart, HierarchicalStart, NbaSwitch, RelocationModule, SimModule, SplitCommit,
SymbolicGlueAddr as GlueAddr, SymbolicRtl,
};
use crate::{
FrontendLookup, FrontendTrace, FrontendTraceOptions, FusedSirOptimizationHints, HashMap,
Expand Down Expand Up @@ -186,6 +186,7 @@ pub(crate) struct PackageBindings {

impl PackageBindings {
fn new(
expanded: &HashMap<InstancePath, InstanceId>,
instance_modules: &HashMap<InstanceId, ModuleId>,
modules: &HashMap<ModuleId, SimModule>,
package_instances: &HashMap<ModuleId, InstanceId>,
Expand Down Expand Up @@ -244,6 +245,98 @@ impl PackageBindings {
);
}
}
let mut paths: Option<HashMap<InstanceId, &InstancePath>> = None;
for (&instance_id, module_id) in instance_modules {
let module = &modules[module_id];
for (&var_id, binding) in &module.hierarchical_bindings {
let paths = paths
.get_or_insert_with(|| expanded.iter().map(|(path, &id)| (id, path)).collect());
let mut path = match binding.start {
HierarchicalStart::Root => Vec::new(),
HierarchicalStart::Ancestor(levels) => {
let own = &paths[&instance_id].0;
own[..own.len().saturating_sub(levels)].to_vec()
}
};
path.extend(binding.path.iter().cloned());
let error = |reason: &str| {
ParserError::illegal_context(
"hierarchical reference",
format!(
"module `{}` binds `{}` {reason}",
module.name,
module
.variables
.get(&var_id)
.map_or_else(String::new, |variable| variable.path.join(".")),
),
None,
)
};
let target_instance = *expanded
.get(&InstancePath(path))
.ok_or_else(|| error("to an instance that does not exist"))?;
let target_module_id = instance_modules[&target_instance];
let target_module = &modules[&target_module_id];
let target_var = binding
.targets
.iter()
.find(|(module, _)| *module == target_module_id)
.map(|&(_, var)| var)
.ok_or_else(|| error("to an instance of another module"))?;
let (Some(variable), Some(target)) = (
module.variables.get(&var_id),
target_module.variables.get(&target_var),
) else {
return Err(error("to an unknown variable"));
};
let (variable, target) = (&variable.metadata, &target.metadata);
if (
variable.width,
variable.is_4state,
&variable.array_dims,
variable.value_kind,
) != (
target.width,
target.is_4state,
&target.array_dims,
target.value_kind,
) {
return Err(error("to a variable of another type"));
}
targets.insert(
AbsoluteAddr {
instance_id,
var_id,
},
AbsoluteAddr {
instance_id: target_instance,
var_id: target_var,
},
);
}
}
// A hierarchical name may denote a variable that itself denotes a
// package variable or another instance's variable.
if paths.is_some() {
let sources: Vec<_> = targets.keys().copied().collect();
for source in sources {
let mut target = targets[&source];
let mut steps = 0;
while let Some(&next) = targets.get(&target) {
steps += 1;
if next == source || steps > targets.len() {
return Err(ParserError::illegal_context(
"hierarchical reference",
"a hierarchical name denotes itself".to_string(),
None,
));
}
target = next;
}
targets.insert(source, target);
}
}
Ok(Self { targets })
}

Expand Down Expand Up @@ -288,7 +381,13 @@ fn check_package_variable_drivers(
package_instances: &HashMap<ModuleId, InstanceId>,
comb_blocks: &[celox_slt::LogicPath<AbsoluteAddr>],
) -> Result<(), ParserError> {
let packages: HashSet<AbsoluteAddr> = bindings.targets.values().copied().collect();
let package_instance_ids: HashSet<InstanceId> = package_instances.values().copied().collect();
let packages: HashSet<AbsoluteAddr> = bindings
.targets
.values()
.copied()
.filter(|target| package_instance_ids.contains(&target.instance_id))
.collect();
if packages.is_empty() {
return Ok(());
}
Expand Down Expand Up @@ -663,7 +762,8 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.iter()
.map(|(&instance, module)| (instance, modules[module].time_scale))
.collect::<HashMap<_, _>>();
let bindings = &PackageBindings::new(&instance_modules, &modules, &package_instances)?;
let bindings =
&PackageBindings::new(&expanded, &instance_modules, &modules, &package_instances)?;
let global_boundaries = timed_sub!(
"propagate_boundaries",
propagate_boundaries(bindings, &expanded, &instance_modules, &modules)
Expand Down Expand Up @@ -1644,17 +1744,33 @@ pub fn schedule_symbolic_rtl_reporting_loops(
})
})
.collect();
let mut initial_memory_values: Vec<InitialStateValue<AbsoluteAddr>> = initial_instances
// A write through a binding, such as a folded `initial` assignment
// through a hierarchical name, follows the declaration values of every
// instance, its target's among them.
let (mut initial_memory_values, bound_initials): (Vec<_>, Vec<_>) = initial_instances
.into_iter()
.flat_map(|(&instance_id, module_id)| {
modules[module_id]
.initial_memory_values
.iter()
.map(move |init| InitialStateValue {
address: bindings.locate(instance_id, init.address),
data: init.data.clone(),
.map(move |init| {
(
bindings.is_bound(&AbsoluteAddr {
instance_id,
var_id: init.address,
}),
InitialStateValue {
address: bindings.locate(instance_id, init.address),
data: init.data.clone(),
},
)
})
})
.partition(|(bound, _)| !bound);
let mut initial_memory_values: Vec<InitialStateValue<AbsoluteAddr>> = initial_memory_values
.drain(..)
.chain(bound_initials)
.map(|(_, init)| init)
.collect();
let state_objects: HashMap<AbsoluteAddr, VariableMetadata> = instance_modules
.iter()
Expand Down Expand Up @@ -2299,8 +2415,11 @@ fn module_variables(
let mut variables = HashMap::default();
let mut paths: HashMap<Vec<String>, Option<SourceVarId>> = HashMap::default();
for (&source_id, variable) in &module.variables {
// A package variable belongs to the package's instance.
if module.package_bindings.contains_key(&source_id) {
// A package variable belongs to the package's instance, and a
// hierarchically named one to its own instance.
if module.package_bindings.contains_key(&source_id)
|| module.hierarchical_bindings.contains_key(&source_id)
{
continue;
}
// Only module-scope variables are externally addressable. Locals
Expand Down
Loading
Loading