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
55 changes: 37 additions & 18 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@ pub(super) struct AnalysisState {
foreign_arguments: RefCell<Vec<(String, classes::specializations::BoundArguments)>>,
foreign_names: RefCell<Vec<(String, classes::specializations::BoundArguments, String)>>,
foreign_error: RefCell<Option<AnalyzerError>>,
interfaces: interface_ast::State,
dimensions: dimensions::State,
root_subroutines: std::cell::Cell<bool>,
inline_randomizations: RefCell<Vec<classes::constraints::Inline>>,
next_call_site: std::rc::Rc<std::cell::Cell<usize>>,
timescales: timescales::State,
diagnostics: RefCell<Vec<warnings::AnalyzerWarning>>,
/// Revision of the imported scope, the analyzed package, the active
/// interface instances and the root-subroutine mode.
revision: generate::Revision,
generate_items: generate::Memo,
/// Classes laid out so far, by name.
classes: std::cell::RefCell<HashMap<String, std::rc::Rc<classes::Class>>>,
Expand Down Expand Up @@ -85,6 +79,16 @@ pub(crate) struct AnalysisContext<'a> {
/// The constant functions that calls in constant expressions of the
/// scope call (IEEE 1800-2023 13.4.3).
pub(crate) functions: const_functions::ConstantFunctions,
/// Whether generate scopes elaborate only their items, and their
/// subroutines are collected later.
root_subroutines: bool,
/// The interface instances being analyzed, outermost first.
active_interfaces: Arc<Vec<interface_ast::ActiveInstance>>,
/// The time scopes that time queries of the scope read (IEEE 1800-2023
/// 3.14.2).
timescale: Arc<timescales::QueryContext>,
/// The hierarchical path of the interface instance being analyzed.
interface_instance: Option<Arc<str>>,
}

impl<'a> AnalysisContext<'a> {
Expand All @@ -100,6 +104,10 @@ impl<'a> AnalysisContext<'a> {
imported: Arc::default(),
package: None,
functions: const_functions::ConstantFunctions::default(),
root_subroutines: false,
active_interfaces: Arc::default(),
timescale: Arc::default(),
interface_instance: None,
}
}

Expand All @@ -111,6 +119,10 @@ impl<'a> AnalysisContext<'a> {
imported: Arc::default(),
package: None,
functions: const_functions::ConstantFunctions::default(),
root_subroutines: false,
active_interfaces: Arc::default(),
timescale: Arc::default(),
interface_instance: None,
}
}

Expand All @@ -128,6 +140,10 @@ impl<'a> AnalysisContext<'a> {
imported: Arc::default(),
package: None,
functions: const_functions::ConstantFunctions::default(),
root_subroutines: false,
active_interfaces: Arc::default(),
timescale: Arc::default(),
interface_instance: None,
}
}

Expand All @@ -143,6 +159,10 @@ impl<'a> AnalysisContext<'a> {
imported: self.imported.clone(),
package: self.package.clone(),
functions: self.functions.clone(),
root_subroutines: self.root_subroutines,
active_interfaces: self.active_interfaces.clone(),
timescale: self.timescale.clone(),
interface_instance: self.interface_instance.clone(),
}
}

Expand Down Expand Up @@ -394,15 +414,15 @@ impl Source {
.map(|(name, value)| (name.clone(), const_expr_from_i128(*value)))
.collect();
let interfaces = Self::module_interfaces_from_syntax(syntax_tree)?;
let _timescales = timescales::install_for_tree(
&syntax_tree.state.timescales,
packages.timescales.clone(),
syntax_tree,
)?;
let timed = AnalysisContext {
timescale: timescales::for_tree(packages.timescales.clone(), syntax_tree)?,
..syntax_tree.clone()
};
let syntax_tree = &timed;
let mut modules = Vec::new();
for handle in syntax_tree.root_scopes()? {
let scoped = syntax_tree.scoped(handle)?;
let scoped_context = context.child(&scoped, &parameter_overrides);
let scoped_context = timed.child(&scoped, &parameter_overrides);
let syntax_tree = &scoped_context;
let unit = syntax_tree.root_unit()?;
if !unit.kind.is_module_like() {
Expand Down Expand Up @@ -504,11 +524,10 @@ impl Source {
) -> Result<Self, AnalyzerError> {
let context = AnalysisContext::with_packages(syntax_tree, packages);
let syntax_tree = &context;
let _timescales = timescales::install_for_tree(
&syntax_tree.state.timescales,
packages.timescales.clone(),
syntax_tree,
)?;
let timed = AnalysisContext {
timescale: timescales::for_tree(packages.timescales.clone(), syntax_tree)?,
..syntax_tree.clone()
};
let interfaces = InterfaceLookup {
local: &index.interfaces,
extra: extra_interfaces,
Expand All @@ -517,7 +536,7 @@ impl Source {
let interface_plan = packages.interface_plan.modules.get(module_name);
let original_name = interface_plan.map_or(module_name, |plan| plan.original.as_str());
for scope_tree in index.scopes(original_name) {
let scope_context = context.child(scope_tree, bindings.values);
let scope_context = timed.child(scope_tree, bindings.values);
let syntax_tree = &scope_context;
match syntax_tree.root_unit()?.port_style {
celox_sv_syntax::PortStyle::Ansi => {
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/ast/array_reductions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(super) fn bind(
"xor" => ContainerOp::ReduceXor,
_ => return Ok(None),
};
if timescales::analysis_options(&tree.state.timescales).legacy_empty_reductions {
if timescales::analysis_options(tree).legacy_empty_reductions {
op = match op {
ContainerOp::ReduceProduct => ContainerOp::LegacyReduceProduct,
ContainerOp::ReduceAnd => ContainerOp::LegacyReduceAnd,
Expand Down
7 changes: 5 additions & 2 deletions crates/celox-sv-analyzer/src/ast/classes/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,14 @@ impl Registry {
parameter_overrides: Cow::Owned(HashMap::default()),
imported: Arc::default(),
functions: const_functions::ConstantFunctions::default(),
root_subroutines: false,
active_interfaces: Arc::default(),
// Inside the package, `p::x` names its own item `x`.
package: Some(name.into()),
timescale: timescales::for_tree(owner.timescales.clone(), &owner.tree)?,
interface_instance: None,
};
let _timescales =
timescales::install_for_tree(&state.timescales, owner.timescales.clone(), &owner.tree)?;

let mut imports = owner.imports.clone();
if let Some(caller) = caller {
// Preserve the owner's existing bindings when caller metadata also
Expand Down
24 changes: 14 additions & 10 deletions crates/celox-sv-analyzer/src/ast/const_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ pub(super) fn symbolic_module_constant_functions(
if !has_functions {
return ConstantFunctions::default();
}
let needs_bootstrap = !generate::root_subroutines_only(&tree.state) && has_controls;
let needs_bootstrap = !tree.root_subroutines && has_controls;
if !needs_bootstrap {
return collect_module_constant_functions(
tree,
Expand All @@ -410,15 +410,19 @@ pub(super) fn symbolic_module_constant_functions(
symbolic_parameters,
);
}
let mut roots = generate::with_root_subroutines(&tree.state, || {
collect_module_constant_functions(
tree,
const_env,
type_aliases,
parameter_literals,
symbolic_parameters,
)
});
// Seed the module's functions before conditions need to call them:
// generate scopes still select their items, but their subroutines are
// left for the full pass.
let mut roots = collect_module_constant_functions(
&AnalysisContext {
root_subroutines: true,
..tree.clone()
},
const_env,
type_aliases,
parameter_literals,
symbolic_parameters,
);
roots.extend_missing(&scope::imported(tree).constant_functions);
let tree = &tree.with_functions(roots);
collect_module_constant_functions(
Expand Down
33 changes: 2 additions & 31 deletions crates/celox-sv-analyzer/src/ast/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::*;
use celox_sv_syntax::ExprId;
mod elaboration;
mod memo;
pub(super) use memo::{CallSites, Revision, memoize};
pub(super) use memo::{CallSites, memoize};

mod dependency_order;
use dependency_order::DependencyOrder;
Expand All @@ -17,35 +17,6 @@ pub(super) use scope_views::ScopeViews;
// Temporary identifiers protect definition-site bindings while functions are
// inlined into a generate scope. They are removed when that item's names are
// qualified, and never reach the public analyzer IR.
pub(super) fn root_subroutines_only(state: &AnalysisState) -> bool {
state.root_subroutines.get()
}

/// Seed module functions before conditions need to call them. The full pass
/// still selects generate scopes and reports every generate diagnostic.
pub(super) fn with_root_subroutines<T>(
state: &std::rc::Rc<AnalysisState>,
f: impl FnOnce() -> T,
) -> T {
struct Restore {
state: std::rc::Rc<AnalysisState>,
previous: bool,
revision: u64,
}
impl Drop for Restore {
fn drop(&mut self) {
self.state.root_subroutines.set(self.previous);
self.state.revision.restore(self.revision);
}
}
let _restore = Restore {
state: state.clone(),
revision: state.revision.change(),
previous: state.root_subroutines.replace(true),
};
f()
}

const OUTER_BINDING: &str = "\0generate_outer:";

// Whitespace terminates an escaped identifier (IEEE 1800-2023 5.6.1).
Expand Down Expand Up @@ -610,7 +581,7 @@ pub(super) fn subroutine_items(
tree,
env,
aliases,
if root_subroutines_only(&tree.state) {
if tree.root_subroutines {
LiteralUse::RootSubroutines
} else {
LiteralUse::Subroutines
Expand Down
61 changes: 14 additions & 47 deletions crates/celox-sv-analyzer/src/ast/generate/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,18 @@
//!
//! The collectors of a module each ask for the active items of the same
//! scope. A window opened for the module keeps each elaboration, keyed by its
//! arguments, the imports, package and constant functions of its context,
//! and the revisions of
//! the analysis state that elaboration reads (active interface instances,
//! time scopes), so a change of that state makes
//! earlier results unreachable.
//! arguments and by the parts of its context that elaboration reads: the
//! imports, package, constant functions, active interface instances and time
//! scopes, so a change of any of them makes earlier results unreachable.
//! A repeated elaboration allocates as many call sites as the first one did,
//! so later calls keep their sites.
use super::*;
#[cfg(test)]
use std::cell::Cell;

mod sites;
pub(in crate::ast) use sites::CallSites;

/// Names a value of analysis state that elaboration reads. Each change gives
/// a new revision; a guard that restores the previous value restores its
/// revision too, so equal revisions mean equal state.
#[derive(Default)]
pub(in crate::ast) struct Revision {
current: Cell<u64>,
last: Cell<u64>,
}

impl Revision {
/// Record a change, returning the revision to restore with the value.
pub(in crate::ast) fn change(&self) -> u64 {
self.last.set(self.last.get() + 1);
self.current.replace(self.last.get())
}

pub(in crate::ast) fn restore(&self, previous: u64) {
self.current.set(previous);
}

pub(in crate::ast) fn get(&self) -> u64 {
self.current.get()
}
}

#[derive(Default)]
pub(in crate::ast) struct State(RefCell<Option<Window>>);

Expand All @@ -50,11 +24,13 @@ struct Window {
}

struct Entry {
revisions: [u64; 2],
/// The symbols the scope imported and the package it was in.
imported: Arc<scope::ScopeSymbols>,
package: Option<Arc<str>>,
functions: const_functions::ConstantFunctions,
active_interfaces: Arc<Vec<interface_ast::ActiveInstance>>,
timescale: Arc<timescales::QueryContext>,
interface_instance: Option<Arc<str>>,
literal_use: LiteralUse,
overrides: HashMap<String, ConstExpr>,
env: HashMap<String, i128>,
Expand Down Expand Up @@ -96,13 +72,6 @@ fn address(tree: &SyntaxTree) -> usize {
tree as *const SyntaxTree as usize
}

fn revisions(state: &AnalysisState) -> [u64; 2] {
[
state.revision.get(),
timescales::revision(&state.timescales).get(),
]
}

/// The items `elaborate` gives for these arguments, elaborated once per window.
pub(super) fn items(
tree: &AnalysisContext<'_>,
Expand All @@ -128,12 +97,13 @@ pub(super) fn items(
drop(window);
return elaborate();
};
let current = revisions(state);
if let Some(entry) = window.entries.iter().find(|entry| {
entry.revisions == current
&& Arc::ptr_eq(&entry.imported, &tree.imported)
Arc::ptr_eq(&entry.imported, &tree.imported)
&& entry.package == tree.package
&& entry.functions.same(&tree.functions)
&& Arc::ptr_eq(&entry.active_interfaces, &tree.active_interfaces)
&& Arc::ptr_eq(&entry.timescale, &tree.timescale)
&& entry.interface_instance == tree.interface_instance
&& entry.literal_use == literal_use
&& entry.overrides == *tree.parameter_overrides
&& entry.env == *env
Expand All @@ -145,12 +115,7 @@ pub(super) fn items(
}
}
let first_site = state.next_call_site.get();
let current = revisions(state);
let items = elaborate()?;
// Elaboration that leaves the state changed is not repeatable.
if revisions(state) != current {
return Ok(items);
}
let sites = first_site..state.next_call_site.get();
// Effectful calls the items retain must get new sites when they are
// elaborated again.
Expand All @@ -160,10 +125,12 @@ pub(super) fn items(
let mut window = state.generate_items.0.borrow_mut();
if let Some(window) = window.as_mut().filter(|window| applies(window)) {
window.entries.push(Entry {
revisions: current,
imported: tree.imported.clone(),
package: tree.package.clone(),
functions: tree.functions.clone(),
active_interfaces: tree.active_interfaces.clone(),
timescale: tree.timescale.clone(),
interface_instance: tree.interface_instance.clone(),
literal_use,
overrides: tree.parameter_overrides.clone().into_owned(),
env: env.clone(),
Expand Down
Loading
Loading