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
20 changes: 10 additions & 10 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1833,22 +1833,22 @@ generated/binary_op/11.4.8--binary_op_bit_xor.sv 2state pass
generated/binary_op/11.4.8--binary_op_bit_xor.sv 4state pass
generated/easyUVM/top.sv 2state fail
generated/easyUVM/top.sv 4state fail
generated/encapsulation/8.18--inherited_local_from_inside.sv 2state fail
generated/encapsulation/8.18--inherited_local_from_inside.sv 4state fail
generated/encapsulation/8.18--inherited_local_from_outside.sv 2state fail
generated/encapsulation/8.18--inherited_local_from_outside.sv 4state fail
generated/encapsulation/8.18--inherited_local_from_inside.sv 2state pass
generated/encapsulation/8.18--inherited_local_from_inside.sv 4state pass
generated/encapsulation/8.18--inherited_local_from_outside.sv 2state pass
generated/encapsulation/8.18--inherited_local_from_outside.sv 4state pass
generated/encapsulation/8.18--inherited_prot_from_inside.sv 2state pass
generated/encapsulation/8.18--inherited_prot_from_inside.sv 4state pass
generated/encapsulation/8.18--inherited_prot_from_outside.sv 2state fail
generated/encapsulation/8.18--inherited_prot_from_outside.sv 4state fail
generated/encapsulation/8.18--inherited_prot_from_outside.sv 2state pass
generated/encapsulation/8.18--inherited_prot_from_outside.sv 4state pass
generated/encapsulation/8.18--local_from_inside.sv 2state pass
generated/encapsulation/8.18--local_from_inside.sv 4state pass
generated/encapsulation/8.18--local_from_outside.sv 2state fail
generated/encapsulation/8.18--local_from_outside.sv 4state fail
generated/encapsulation/8.18--local_from_outside.sv 2state pass
generated/encapsulation/8.18--local_from_outside.sv 4state pass
generated/encapsulation/8.18--prot_from_inside.sv 2state pass
generated/encapsulation/8.18--prot_from_inside.sv 4state pass
generated/encapsulation/8.18--prot_from_outside.sv 2state fail
generated/encapsulation/8.18--prot_from_outside.sv 4state fail
generated/encapsulation/8.18--prot_from_outside.sv 2state pass
generated/encapsulation/8.18--prot_from_outside.sv 4state pass
generated/equality_operators_sim/11.4.5--equality_operator_0.sv 2state fail
generated/equality_operators_sim/11.4.5--equality_operator_0.sv 4state fail
generated/equality_operators_sim/11.4.5--equality_operator_1.sv 2state fail
Expand Down
35 changes: 30 additions & 5 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5092,11 +5092,36 @@ fn lower_initial_processes(
.zip(slots)
.enumerate()
.map(|(index, ((process, random_seed), slots))| {
let kernel = ff::Ff::new(&mut pm, random_seed).lower_initial_kernel(
process.body(),
slots,
index,
)?;
// `always @(list) statement` waits for a change (IEEE 1800-2023
// 9.4.2). In two-state mode its outputs would then start as a
// real 0, so it runs once at time zero, after the time-zero
// statements of the initial blocks, before it waits, as
// Verilator settles it: `#0; forever begin statement; @(list); end`.
let settled;
let body = match process.level_sensitive().filter(|_| !four_state) {
Some((items, statements)) => {
let mut looped = statements.to_vec();
looped.push(sv::ir::Stmt::WaitEvent(items.to_vec()));
settled = [
sv::ir::Stmt::Delay {
amount: sv::ir::Expr::Literal("0".to_string()),
ticks_per_unit: 1,
ticks_per_precision: 1,
},
sv::ir::Stmt::Loop {
kind: sv::ir::LoopKind::Forever,
init: Vec::new(),
condition: None,
step: Vec::new(),
body: looped,
},
];
&settled[..]
}
None => process.body(),
};
let kernel =
ff::Ff::new(&mut pm, random_seed).lower_initial_kernel(body, slots, index)?;
wait_sites.extend(kernel.wait_sites);
nba_sites.extend(kernel.nba_sites);
nba_switches.extend(kernel.nba_switches);
Expand Down
12 changes: 9 additions & 3 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,11 @@ impl<'p, 'a> Ff<'p, 'a> {
arena: &mut SLTNodeArena<SourceVarId>,
) -> Result<Vec<LogicPath<SourceVarId>>, sv::AnalyzerError> {
let always_comb = kind != sv::ir::CombProcessKind::AlwaysStar;
// `always @*` and a sensitivity list wait for a change (IEEE
// 1800-2023 9.2.2.2). In two-state mode their outputs would then
// start as a real 0, so they run once at time zero, after the
// `initial` blocks, like `always_comb`, as Verilator settles them.
let runs_at_time_zero = always_comb || !self.m.four_state;
self.direct = true;
let mut reads = self.m.implicit_reads(body, always_comb);
reads.extend(self.m.implicit_reads(sensitivity_body, always_comb));
Expand Down Expand Up @@ -4113,7 +4118,7 @@ impl<'p, 'a> Ff<'p, 'a> {
counter_snapshots.push((counter, saved_counter));
}
let run = self.b.alloc_bit(1, false);
if always_comb {
if runs_at_time_zero {
let first = self.b.alloc_bit(1, false);
self.b
.emit(SIRInstruction::Unary(first, UnaryOp::LogicNot, init));
Expand Down Expand Up @@ -4227,7 +4232,7 @@ impl<'p, 'a> Ff<'p, 'a> {
target: LogicPathTarget::Process {
process: initialized,
sensitivity_counters: sensitivity_counters.clone(),
startup: (!always_comb).then(|| celox_slt::CombStartup {
startup: (!runs_at_time_zero).then(|| celox_slt::CombStartup {
initialized,
counters: counter_snapshots.clone(),
inputs: sources
Expand All @@ -4239,7 +4244,8 @@ impl<'p, 'a> Ff<'p, 'a> {
after_initials: matches!(
kind,
sv::ir::CombProcessKind::AlwaysComb | sv::ir::CombProcessKind::AlwaysLatch
),
) || (kind == sv::ir::CombProcessKind::AlwaysStar
&& !self.m.four_state),
target,
unit: Box::new(unit.clone()),
},
Expand Down
4 changes: 4 additions & 0 deletions crates/celox-sv-analyzer/src/ast/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl Property {
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct StaticProperty {
pub name: String,
pub local: bool,
pub protected: bool,
/// The class that declares the property.
pub owner: String,
pub r#type: Type,
Expand Down Expand Up @@ -460,6 +462,8 @@ pub(super) fn class(
if property.qualifiers.is_static {
statics.retain(|known| known.name != property_name);
statics.push(StaticProperty {
local: property.qualifiers.local,
protected: property.qualifiers.protected,
variable: Class::subroutine_name(name, &property_name),
name: property_name,
owner: name.to_owned(),
Expand Down
6 changes: 3 additions & 3 deletions crates/celox-sv-analyzer/src/ast/expression_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub(super) use direct::{
pub(super) use lvalues::{net_lvalue, variable_lvalue};
pub(super) use objects::{
Cast as ObjectCast, Receiver as ObjectReceiver, cast as object_cast,
check_assignment as check_handle_assignment, constructor as object_constructor,
method as object_method, property as object_property, rand_mode_call as object_rand_mode_call,
receiver as object_receiver, this as object_this,
check_access as check_member_access, check_assignment as check_handle_assignment,
constructor as object_constructor, method as object_method, property as object_property,
rand_mode_call as object_rand_mode_call, receiver as object_receiver, this as object_this,
};
pub(super) use strings::{
call as string_call, method as string_method, native_string, to_string as string_operand,
Expand Down
138 changes: 110 additions & 28 deletions crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ pub(in crate::ast) fn native_class(
property.r#type.class().map(str::to_owned)
}

/// Access depends on the lexical class, not the class of the receiver (8.18).
pub(in crate::ast) fn check_access(
tree: &AnalysisContext<'_>,
dimensions: &PackedDimensions,
owner: &str,
name: &str,
local: bool,
protected: bool,
) -> Converted<()> {
if !local && !protected {
return Ok(());
}
if let Some(scope) = dimensions.class_scope.as_deref() {
if scope == owner
|| (!local
&& class(tree, dimensions, scope)?.derives_from(
tree,
owner,
&dimensions.const_env,
&dimensions.type_aliases,
))
{
return Ok(());
}
}
Err(AnalyzerError::InaccessibleClassMember {
owner: owner.to_owned(),
name: name.to_owned(),
visibility: if local { "local" } else { "protected" },
})
}

/// The class whose method is being bound, whose properties and methods
/// unqualified names may refer to.
pub(in crate::ast) fn scope_class(dimensions: &PackedDimensions) -> Option<String> {
Expand Down Expand Up @@ -115,9 +147,20 @@ pub(in crate::ast) fn static_property(
let Some(class_name) = class_name else {
return Ok(None);
};
Ok(class(tree, dimensions, &class_name)?
.static_property(&instances::normalize_identifier(&name.spelling))
.map(|property| property.variable.clone()))
let class = class(tree, dimensions, &class_name)?;
let Some(property) = class.static_property(&instances::normalize_identifier(&name.spelling))
else {
return Ok(None);
};
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
Ok(Some(property.variable.clone()))
}

/// The property that native expression `id` names, `h.p` or an unqualified
Expand Down Expand Up @@ -157,9 +200,28 @@ pub(in crate::ast) fn property(
_ => return Ok(None),
};
let class = class(tree, dimensions, &class_name)?;
let Some(property) = class.property(&instances::normalize_identifier(&name.spelling)) else {
let name = instances::normalize_identifier(&name.spelling);
let Some(property) = class.property(&name) else {
if let Some(property) = class.static_property(&name) {
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
}
return Ok(None);
};
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
if receiver.is_none() && dimensions.get(THIS).is_none() {
return Err(unsupported(format!(
"non-static property `{}` in a static method",
Expand Down Expand Up @@ -202,8 +264,26 @@ pub(in crate::ast) fn unqualified_property(
};
let class = class(tree, dimensions, &class_name)?;
let Some(property) = class.property(name) else {
if let Some(property) = class.static_property(name) {
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
}
return Ok(None);
};
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
if dimensions.get(THIS).is_none() {
return Err(unsupported(format!(
"non-static property `{name}` in a static method"
Expand Down Expand Up @@ -300,6 +380,14 @@ pub(in crate::ast) fn method(
let Some(method) = class.method(&method_name) else {
return Ok(None);
};
check_access(
tree,
dimensions,
&method.owner,
&method.name,
method.qualifiers.local,
method.qualifiers.protected,
)?;
if through_super && method.is_pure() {
return Err(unsupported(format!(
"pure virtual method `{}` called through `super`",
Expand Down Expand Up @@ -485,6 +573,16 @@ pub(in crate::ast) fn constructor(
_ => return Ok(None),
};
let class = class(tree, dimensions, &class_name)?;
if let Some(method) = class.method("new") {
check_access(
tree,
dimensions,
&method.owner,
&method.name,
method.qualifiers.local,
method.qualifiers.protected,
)?;
}
if class.is_abstract {
return Err(unsupported(format!(
"object of abstract class `{class_name}`"
Expand Down Expand Up @@ -674,28 +772,12 @@ fn check_random_access(
dimensions: &PackedDimensions,
property: &classes::Property,
) -> Converted<()> {
let scope = scope_class(dimensions);
let accessible = if property.local {
scope.as_deref() == Some(&property.owner)
} else if property.protected {
scope
.and_then(|name| class(tree, dimensions, &name).ok())
.is_some_and(|class| {
class.derives_from(
tree,
&property.owner,
&dimensions.const_env,
&dimensions.type_aliases,
)
})
} else {
true
};
if !accessible {
return Err(unsupported(format!(
"randomization access to non-public property `{}`",
property.name
)));
}
Ok(())
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)
}
15 changes: 15 additions & 0 deletions crates/celox-sv-analyzer/src/ast/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ impl<'s, 't, 'a> BodyBuilder<'s, 't, 'a> {
.push(entry);
}
for property in &class.statics {
// A base's local static is not visible in the derived class (8.18).
// Keep it out of the alias table so binding can diagnose its use.
if property.local && property.owner != class.name {
continue;
}
let entry = self.take_binding(
&property.name,
Some(property.variable.clone()),
Expand Down Expand Up @@ -2468,6 +2473,16 @@ impl BodyBuilder<'_, '_, '_> {
&self.dims.const_env,
&self.dims.type_aliases,
)?;
if let Some(method) = base.method("new") {
expression_binding::check_member_access(
self.tree,
&self.dims,
&method.owner,
&method.name,
method.qualifiers.local,
method.qualifiers.protected,
)?;
}
let formals = base.constructor_formals(native);
if arguments.len() > formals {
return Err(unsupported(format!(
Expand Down
7 changes: 7 additions & 0 deletions crates/celox-sv-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ pub enum AnalyzerError {
Parse(String),
#[error("Unsupported SystemVerilog construct: {0}")]
Unsupported(String),
/// A class member is inaccessible from the lexical scope (IEEE 1800-2023 8.18).
#[error("{visibility} class member `{owner}::{name}` is not accessible from this scope")]
InaccessibleClassMember {
owner: String,
name: String,
visibility: &'static str,
},
/// A dotted name whose first component names no variable or interface
/// of the scope: a hierarchical name (IEEE 1800-2023 23.6) that the
/// caller did not resolve to a signal of another instance.
Expand Down
Loading
Loading