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
4 changes: 2 additions & 2 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,8 @@ chapter-23/23.2--module-definition.sv 2state pass
chapter-23/23.2--module-definition.sv 4state pass
chapter-23/23.2--module-label.sv 2state pass
chapter-23/23.2--module-label.sv 4state pass
chapter-24/24.3--program.sv 2state fail
chapter-24/24.3--program.sv 4state fail
chapter-24/24.3--program.sv 2state pass
chapter-24/24.3--program.sv 4state pass
chapter-25/25.3-interface.sv 2state pass
chapter-25/25.3-interface.sv 4state pass
chapter-26/26.2--package-decl.sv 2state pass
Expand Down
23 changes: 20 additions & 3 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4108,8 +4108,10 @@ fn lower_initial_processes(
.copied()
.unwrap_or(0x4d59_5df4);
// An `always` process never finishes, so it is never folded into
// initial state.
if process.repeats() {
// initial state. A program's `initial` procedure runs in the reactive
// region set (IEEE 1800-2023 24.3.1), after the design's processes of
// time zero, so it is not folded either.
if process.repeats() || (module.is_program() && !process.is_initializer()) {
runtime.push((process, random_seed));
continue;
}
Expand Down Expand Up @@ -4154,7 +4156,7 @@ fn lower_initial_processes(
.max()
.unwrap_or_default(),
);
let slots: Vec<_> = runtime
let mut slots: Vec<_> = runtime
.iter()
.enumerate()
.map(|(index, (process, _))| {
Expand Down Expand Up @@ -4195,6 +4197,21 @@ fn lower_initial_processes(
}
})
.collect();
// Every `initial` kernel of a program instance names the status slot of
// the first, the instance's identity for `$exit` and its end (IEEE
// 1800-2023 24.3, 24.7).
if module.is_program() {
let program = slots
.iter()
.zip(&runtime)
.find(|(_, (process, _))| !process.is_initializer())
.map(|(slots, _)| slots.status);
for (slots, (process, _)) in slots.iter_mut().zip(&runtime) {
if !process.is_initializer() {
slots.program = program;
}
}
}
// The kernels add their event sites, runtime errors and extern functions
// after those of the sequential processes. Their hidden variables are
// state the kernels keep.
Expand Down
9 changes: 7 additions & 2 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2869,9 +2869,14 @@ impl<'p, 'a> Ff<'p, 'a> {
}
return Ok(true);
}
// IEEE 1800-2023 24.7: `$exit` outside a program block is ignored,
// with a warning.
// IEEE 1800-2023 24.7: `$exit` in a thread of a program's `initial`
// procedure ends the program's initial procedures; elsewhere it is
// ignored, with a warning.
if task == sv::ir::SystemTask::Exit {
if self.m.program && self.kernel.is_some() {
self.with_kernel(ProcessKernelBuilder::exit);
return Ok(false);
}
self.m
.warn(crate::FrontendWarningKind::ExitOutsideProgram, site);
return Ok(true);
Expand Down
4 changes: 4 additions & 0 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub(super) struct ProcModule<'a> {
/// Number of extern calls lowered so far.
pub extern_calls: usize,
pub four_state: bool,
/// The module is a program (IEEE 1800-2023 24.3): its kernels honour
/// `$exit` (24.7).
pub program: bool,
pub forceable: HashMap<SourceVarId, (SourceVarId, SourceVarId)>,
/// Hidden variables created while lowering, for the caller to publish.
pub created: Vec<SourceVarId>,
Expand Down Expand Up @@ -244,6 +247,7 @@ impl<'a> ProcModule<'a> {
extern_functions: Vec::new(),
extern_calls: 0,
four_state,
program: module.is_program(),
created: Vec::new(),
temp_counter: 0,
capture_counter: 0,
Expand Down
4 changes: 2 additions & 2 deletions crates/celox-frontend-sv/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub enum FrontendWarningKind {
file: String,
detail: String,
},
/// `$exit` outside a program block, which IEEE 1800-2023 24.7 ignores.
/// Celox has no program blocks, so every call is ignored.
/// `$exit` outside a thread of a program's `initial` procedure, which
/// IEEE 1800-2023 24.7 ignores.
#[error("`$exit` outside a program block is ignored")]
ExitOutsideProgram,
}
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-analyzer/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
.collect(),
)
.with_dpi_imports(module.dpi_imports().to_vec())
.with_program(module.is_program())
.with_imported_parameters(imported_parameters)
.with_declaration_subroutines(module.declaration_subroutines().to_vec()),
);
Expand Down
22 changes: 15 additions & 7 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Source {
let scoped_context = context.child(&scoped, &parameter_overrides);
let syntax_tree = &scoped_context;
let unit = syntax_tree.root_unit()?;
if unit.kind != celox_sv_syntax::UnitKind::Module {
if !unit.kind.is_module_like() {
continue;
}
match unit.port_style {
Expand Down Expand Up @@ -477,8 +477,7 @@ impl Source {
let scoped = syntax_tree.scoped(handle)?;
let syntax_tree = &scoped;
let unit = syntax_tree.root_unit()?;
if unit.kind == celox_sv_syntax::UnitKind::Module
&& unit.port_style != celox_sv_syntax::PortStyle::Wildcard
if unit.kind.is_module_like() && unit.port_style != celox_sv_syntax::PortStyle::Wildcard
{
names.push(syntax_tree.root_name()?);
}
Expand All @@ -495,9 +494,7 @@ impl Source {
let scoped = syntax_tree.scoped(handle)?;
let syntax_tree = &scoped;
let unit = syntax_tree.root_unit()?;
if unit.kind == celox_sv_syntax::UnitKind::Module
&& unit.port_style == celox_sv_syntax::PortStyle::Ansi
{
if unit.kind.is_module_like() && unit.port_style == celox_sv_syntax::PortStyle::Ansi {
let name = syntax_tree.root_name()?;
let context = AnalysisContext::unspecialized(syntax_tree);
interfaces.insert(name, module_interface_from_scope(&context)?);
Expand All @@ -512,7 +509,8 @@ impl Source {

pub(crate) fn warnings(&self) -> Vec<warnings::AnalyzerWarning> {
let mut warnings = self.diagnostics.clone();
for module in &self.modules {
// A program's functions run in its threads, where `$exit` applies.
for module in self.modules.iter().filter(|module| !module.program) {
warnings::append(&mut warnings, module.constant_functions.warnings());
}
warnings
Expand All @@ -522,6 +520,9 @@ impl Source {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Module {
name: String,
/// A program (IEEE 1800-2023 24.3), whose processes run in the reactive
/// region set.
program: bool,
parameters: Vec<Parameter>,
ports: Vec<Port>,
signals: Vec<Signal>,
Expand Down Expand Up @@ -625,6 +626,7 @@ impl Module {
) -> Result<Self, AnalyzerError> {
let unit = syntax_tree.scope_handle().unit(syntax_tree.declarations());
let is_package = unit.is_none_or(|unit| unit.kind == celox_sv_syntax::UnitKind::Package);
let program = unit.is_some_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Program);
let name = unit.map_or_else(
|| packages::UNIT.to_string(),
|unit| instances::normalize_identifier(&unit.name.spelling),
Expand Down Expand Up @@ -1417,6 +1419,7 @@ impl Module {

Ok(Self {
name,
program,
parameters,
ports,
signals,
Expand Down Expand Up @@ -1488,6 +1491,11 @@ impl Module {
pub fn dpi_imports(&self) -> &[crate::ir::DpiImport] {
&self.dpi_imports
}

/// Whether this is a program (IEEE 1800-2023 24.3).
pub fn is_program(&self) -> bool {
self.program
}
}

pub(crate) fn generated_time_declarations(
Expand Down
6 changes: 3 additions & 3 deletions crates/celox-sv-analyzer/src/ast/module_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl ModuleIndex {
for handle in tree.root_scopes()? {
let scoped = tree.scoped(handle)?;
let unit = scoped.root_unit()?;
if unit.kind != celox_sv_syntax::UnitKind::Module
if !unit.kind.is_module_like()
|| unit.port_style == celox_sv_syntax::PortStyle::Wildcard
{
continue;
Expand All @@ -51,11 +51,11 @@ impl ModuleIndex {
}
}
if let Some(declarations) = declarations {
use celox_sv_syntax::{PortKind, PortStyle, UnitKind};
use celox_sv_syntax::{PortKind, PortStyle};
let units = declarations
.all_units()
.filter(|unit| {
unit.kind == UnitKind::Module
unit.kind.is_module_like()
&& !unit.external
&& unit.port_style != PortStyle::Wildcard
})
Expand Down
76 changes: 74 additions & 2 deletions crates/celox-sv-analyzer/src/ast/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,16 @@ pub(super) fn reject_silently_ignored_constructs(
parameter_dimensions: &ScopedMap<VariableDimensions>,
parameter_values: &HashMap<String, Expr>,
) -> Converted<()> {
if let Ok(unit) = syntax_tree.root_unit() {
validate_program_structure(unit, syntax_tree.declarations())?;
}
let mut dimensions =
PackedDimensions::new(parameter_dimensions.clone(), const_env, type_aliases);
dimensions.parameter_values = parameter_values.clone().into();
validate_scope(None, syntax_tree, &dimensions)?;
if syntax_tree
.root_unit()
.is_ok_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Module)
.is_ok_and(|unit| unit.kind.is_module_like())
{
let active = generate::items(syntax_tree, const_env, type_aliases)?;
let mut views = generate::ScopeViews::new(&dimensions);
Expand All @@ -236,6 +239,75 @@ pub(super) fn reject_silently_ignored_constructs(
Ok(())
}

/// A program is a leaf: it shall not contain `always` procedures or
/// declarations or instances of modules, interfaces or other programs (IEEE
/// 1800-2023 24.3), generate blocks included. A program nested in a module
/// is implicitly instantiated there, which Celox does not do.
fn validate_program_structure(
unit: &celox_sv_syntax::Unit,
native: &celox_sv_syntax::Declarations,
) -> Converted<()> {
use celox_sv_syntax::{BodyItemKind, ProcedureKind, UnitKind};
let program = unit.kind == UnitKind::Program;
let illegal = |item: String| {
Err(AnalyzerError::IllegalProgramItem {
program: instances::normalize_identifier(&unit.name.spelling),
item,
})
};
let mut pending = unit.items.iter().rev().copied().collect::<Vec<_>>();
while let Some(id) = pending.pop() {
match &native.body_item(id).kind {
BodyItemKind::Procedure(procedure) if program => {
let keyword = match native.procedure(*procedure).kind {
ProcedureKind::Always => "always",
ProcedureKind::AlwaysComb => "always_comb",
ProcedureKind::AlwaysLatch => "always_latch",
ProcedureKind::AlwaysFf => "always_ff",
ProcedureKind::Initial | ProcedureKind::Final => continue,
};
return illegal(format!("`{keyword}` procedure"));
}
BodyItemKind::Instance(_) if program => return illegal("an instance".into()),
BodyItemKind::Unit(nested) => {
let nested = native.unit(*nested);
if program {
let kind = match nested.kind {
UnitKind::Module => "module",
UnitKind::Interface => "interface",
UnitKind::Package => "package",
UnitKind::Program => "program",
};
return illegal(format!(
"{kind} declaration `{}`",
instances::normalize_identifier(&nested.name.spelling)
));
}
if nested.kind == UnitKind::Program {
return Err(unsupported("program declaration nested in a module"));
}
}
BodyItemKind::Region(items) | BodyItemKind::Block { items, .. } => {
pending.extend(items.iter().rev().copied());
}
BodyItemKind::Conditional {
then_body,
otherwise,
..
} => {
pending.extend(otherwise);
pending.push(*then_body);
}
BodyItemKind::Case { arms, .. } => {
pending.extend(arms.iter().rev().map(|arm| arm.body));
}
BodyItemKind::Loop { body, .. } => pending.push(*body),
_ => {}
}
}
Ok(())
}

fn contains(outer: celox_sv_syntax::TokenRange, inner: celox_sv_syntax::TokenRange) -> bool {
outer.start <= inner.start && inner.end <= outer.end
}
Expand All @@ -253,7 +325,7 @@ fn validate_scope(
let is_module = owner.is_none()
&& syntax_tree
.root_unit()
.is_ok_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Module);
.is_ok_and(|unit| unit.kind.is_module_like());
let items = owner.map_or_else(|| syntax_tree.body_membership(), |id| Ok(vec![id]))?;
validate_body_items(&items, native, !is_module)?;
parameters::skipped::validate_scope(owner, is_module, syntax_tree, indexed_dimensions)?;
Expand Down
4 changes: 3 additions & 1 deletion crates/celox-sv-analyzer/src/ast/warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ pub(crate) fn components(tree: &SyntaxTree) -> HashMap<String, Vec<AnalyzerWarni
for unit in tree.declarations().all_units() {
if matches!(
unit.kind,
celox_sv_syntax::UnitKind::Module | celox_sv_syntax::UnitKind::Interface
celox_sv_syntax::UnitKind::Module
| celox_sv_syntax::UnitKind::Program
| celox_sv_syntax::UnitKind::Interface
) {
append(
warnings
Expand Down
13 changes: 13 additions & 0 deletions crates/celox-sv-analyzer/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct Module {
declaration_subroutines: Vec<Subroutine>,
dpi_imports: Vec<DpiImport>,
imported_parameters: Vec<Parameter>,
program: bool,
}

impl Module {
Expand Down Expand Up @@ -194,9 +195,21 @@ impl Module {
declaration_subroutines: Vec::new(),
dpi_imports: Vec::new(),
imported_parameters: Vec::new(),
program: false,
}
}

pub(crate) fn with_program(mut self, program: bool) -> Self {
self.program = program;
self
}

/// Whether this is a program (IEEE 1800-2023 24.3): its `initial`
/// procedures run in the reactive region set (24.3.1).
pub fn is_program(&self) -> bool {
self.program
}

pub(crate) fn with_imported_parameters(mut self, parameters: Vec<Parameter>) -> Self {
self.imported_parameters = parameters;
self
Expand Down
5 changes: 5 additions & 0 deletions crates/celox-sv-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ pub enum AnalyzerError {
/// least significant bit to judge the edge on (IEEE 1800-2023 6.12).
#[error("`{edge}` of a real expression: edge event controls do not apply to reals")]
RealEdgeEvent { edge: &'static str },
/// An item a program shall not contain: an `always` procedure, or a
/// declaration or instance of a module, interface or program (IEEE
/// 1800-2023 24.3).
#[error("{item} in program `{program}`: a program shall not contain it (IEEE 1800-2023 24.3)")]
IllegalProgramItem { program: String, item: String },
}

impl miette::Diagnostic for AnalyzerError {}
Expand Down
8 changes: 6 additions & 2 deletions crates/celox-sv-analyzer/src/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ impl ParsedSource {
.filter(|unit| {
matches!(
unit.kind,
celox_sv_syntax::UnitKind::Module | celox_sv_syntax::UnitKind::Interface
celox_sv_syntax::UnitKind::Module
| celox_sv_syntax::UnitKind::Program
| celox_sv_syntax::UnitKind::Interface
) && !unit.external
&& unit.port_style != celox_sv_syntax::PortStyle::Wildcard
})
Expand Down Expand Up @@ -118,7 +120,9 @@ impl ParsedSource {
};
if !matches!(
unit.kind,
celox_sv_syntax::UnitKind::Module | celox_sv_syntax::UnitKind::Interface
celox_sv_syntax::UnitKind::Module
| celox_sv_syntax::UnitKind::Program
| celox_sv_syntax::UnitKind::Interface
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/source_tree/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(super) fn handles(native: &Declarations) -> Converted<Vec<ScopeHandle>> {
if !unit.external
&& matches!(
unit.kind,
UnitKind::Module | UnitKind::Interface | UnitKind::Package
UnitKind::Module | UnitKind::Program | UnitKind::Interface | UnitKind::Package
)
{
result.push(handle);
Expand Down
Loading
Loading