From a95ad5eadbdf22a0267d68eaa64308c0457063c7 Mon Sep 17 00:00:00 2001 From: tignear Date: Thu, 22 Oct 2026 10:56:11 +0900 Subject: [PATCH] feat(sv): support virtual methods, abstract classes and interface class implementation A virtual method gets a dispatcher subroutine that calls the override for the class of `this` (IEEE 1800-2023 8.20); calls through handles and unqualified calls inside methods go through it, `super.m` calls the base's implementation directly. A call through `null` is a runtime error. `virtual class` cannot be constructed and a class that is not abstract must implement every pure virtual method (8.21). A class that `implements` interface classes implements each of their methods with the same return type, or re-declares it pure when it is abstract (8.26); the parser now keeps the `implements` list. Methods called on elements of arrays of class handles and `new` assigned to such an element are bound with the element's class. Change-Id: I1f9a009a084e52b208ecd286d6d89ebe23ac40fd --- conformance/sv-tests/README.md | 15 ++ conformance/sv-tests/expected.tsv | 36 +-- crates/celox-sv-analyzer/src/ast/classes.rs | 187 ++++++++++++++- .../src/ast/dimensions/discovery.rs | 10 + .../src/ast/expression_binding.rs | 17 +- .../src/ast/expression_binding/objects.rs | 49 +++- crates/celox-sv-analyzer/src/ast/functions.rs | 1 + .../celox-sv-analyzer/src/ast/procedural.rs | 135 ++++++++++- crates/celox-sv-syntax/src/nodes.rs | 3 + crates/celox-sv-syntax/src/parser/classes.rs | 9 +- crates/celox-test-suite/LIMITATIONS.md | 2 + .../celox-test-suite/src/sv/cases/class.vtest | 214 ++++++++++++++++++ .../verification/repros/virtual_dispatch.sv | 41 ++++ .../repros/virtual_dispatch_icarus.json | 42 ++++ .../verification/sv/icarus/class.json | 93 ++++++++ .../verification/sv/limitations.json | 37 +++ .../verification/sv/limitations/icarus.json | 36 ++- .../verification/sv/verilator/class.json | 44 ++++ .../tests/frontends/systemverilog/class.rs | 43 +++- docs/guide/systemverilog-reference.md | 14 +- docs/ja/guide/systemverilog-reference.md | 6 +- 21 files changed, 972 insertions(+), 62 deletions(-) create mode 100644 crates/celox-test-suite/verification/repros/virtual_dispatch.sv create mode 100644 crates/celox-test-suite/verification/repros/virtual_dispatch_icarus.json diff --git a/conformance/sv-tests/README.md b/conformance/sv-tests/README.md index 731745d973d..a0c9b0c88e3 100644 --- a/conformance/sv-tests/README.md +++ b/conformance/sv-tests/README.md @@ -172,6 +172,21 @@ runs at the pinned revision report 534 passing cases in 2state and 536 in interface classes, classes in packages, parameterized classes and shallow copies stay unsupported. +Virtual methods, abstract classes and classes implementing interface classes +update eighteen more rows: both state modes for `8.15--super-default-new`, +`8.20--virtual_method`, `8.21--abstract_class`, `8.22--dynamic_method_lookup`, +`8.26.2--implements`, `8.26.2--implements_extends`, +`8.26.2--implements_multiple`, `8.26.6.1--name_conflict_resolved` and +`8.26.7--partial_implementation` change from fail to pass. Each was simulated +and its displayed values checked: the override of the object's class runs +through base-class handles and an array of them, and the shadowed property of +`8.20--virtual_method` reads the derived class's value. Full official runs at +the pinned revision report 543 passing cases in 2state and 545 in 4state, with +no passing case regressing. `8.26.6.1--name_conflict_unresolved` stays a +passing negative case: its return types conflict, which is rejected. Calls +through interface class handles, `$cast`, parameterized classes and shallow +copies stay unsupported. + The report contains each case’s state mode, verdict, selected execution mode, tags, tool return code and path to the official log. The output directory retains both the official log and runner diagnostics for each case and state mode. diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index c63919786c8..df6b904eb10 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -1473,8 +1473,8 @@ chapter-8/8.13--inheritance.sv 2state pass chapter-8/8.13--inheritance.sv 4state pass chapter-8/8.14--override_member.sv 2state pass chapter-8/8.14--override_member.sv 4state pass -chapter-8/8.15--super-default-new.sv 2state fail -chapter-8/8.15--super-default-new.sv 4state fail +chapter-8/8.15--super-default-new.sv 2state pass +chapter-8/8.15--super-default-new.sv 4state pass chapter-8/8.15--super.sv 2state pass chapter-8/8.15--super.sv 4state pass chapter-8/8.16--cast_func.sv 2state fail @@ -1489,14 +1489,14 @@ chapter-8/8.19--global_constant.sv 2state pass chapter-8/8.19--global_constant.sv 4state pass chapter-8/8.19--instance_constant.sv 2state pass chapter-8/8.19--instance_constant.sv 4state pass -chapter-8/8.20--virtual_method.sv 2state fail -chapter-8/8.20--virtual_method.sv 4state fail -chapter-8/8.21--abstract_class.sv 2state fail -chapter-8/8.21--abstract_class.sv 4state fail +chapter-8/8.20--virtual_method.sv 2state pass +chapter-8/8.20--virtual_method.sv 4state pass +chapter-8/8.21--abstract_class.sv 2state pass +chapter-8/8.21--abstract_class.sv 4state pass chapter-8/8.21--abstract_class_inst.sv 2state pass chapter-8/8.21--abstract_class_inst.sv 4state pass -chapter-8/8.22--dynamic_method_lookup.sv 2state fail -chapter-8/8.22--dynamic_method_lookup.sv 4state fail +chapter-8/8.22--dynamic_method_lookup.sv 2state pass +chapter-8/8.22--dynamic_method_lookup.sv 4state pass chapter-8/8.23--scope_resolution.sv 2state pass chapter-8/8.23--scope_resolution.sv 4state pass chapter-8/8.24--out_of_block_methods.sv 2state pass @@ -1507,12 +1507,12 @@ chapter-8/8.25.1--parametrized_class_invalid_scope_resolution.sv 2state pass chapter-8/8.25.1--parametrized_class_invalid_scope_resolution.sv 4state pass chapter-8/8.25.1--parametrized_class_scope_resolution.sv 2state fail chapter-8/8.25.1--parametrized_class_scope_resolution.sv 4state fail -chapter-8/8.26.2--implements.sv 2state fail -chapter-8/8.26.2--implements.sv 4state fail -chapter-8/8.26.2--implements_extends.sv 2state fail -chapter-8/8.26.2--implements_extends.sv 4state fail -chapter-8/8.26.2--implements_multiple.sv 2state fail -chapter-8/8.26.2--implements_multiple.sv 4state fail +chapter-8/8.26.2--implements.sv 2state pass +chapter-8/8.26.2--implements.sv 4state pass +chapter-8/8.26.2--implements_extends.sv 2state pass +chapter-8/8.26.2--implements_extends.sv 4state pass +chapter-8/8.26.2--implements_multiple.sv 2state pass +chapter-8/8.26.2--implements_multiple.sv 4state pass chapter-8/8.26.3--type_access_extends.sv 2state pass chapter-8/8.26.3--type_access_extends.sv 4state pass chapter-8/8.26.3--type_access_implements.sv 2state fail @@ -1529,8 +1529,8 @@ chapter-8/8.26.5--implemented_class_handle.sv 2state pass chapter-8/8.26.5--implemented_class_handle.sv 4state pass chapter-8/8.26.5--invalid_interface_instantiation.sv 2state pass chapter-8/8.26.5--invalid_interface_instantiation.sv 4state pass -chapter-8/8.26.6.1--name_conflict_resolved.sv 2state fail -chapter-8/8.26.6.1--name_conflict_resolved.sv 4state fail +chapter-8/8.26.6.1--name_conflict_resolved.sv 2state pass +chapter-8/8.26.6.1--name_conflict_resolved.sv 4state pass chapter-8/8.26.6.1--name_conflict_unresolved.sv 2state pass chapter-8/8.26.6.1--name_conflict_unresolved.sv 4state pass chapter-8/8.26.6.2--parameter_type_conflict.sv 2state pass @@ -1541,8 +1541,8 @@ chapter-8/8.26.6.3--diamond_relationship.sv 2state pass chapter-8/8.26.6.3--diamond_relationship.sv 4state pass chapter-8/8.26.6.3--diamond_relationship_parametrized.sv 2state fail chapter-8/8.26.6.3--diamond_relationship_parametrized.sv 4state fail -chapter-8/8.26.7--partial_implementation.sv 2state fail -chapter-8/8.26.7--partial_implementation.sv 4state fail +chapter-8/8.26.7--partial_implementation.sv 2state pass +chapter-8/8.26.7--partial_implementation.sv 4state pass chapter-8/8.27--forward_declaration.sv 2state pass chapter-8/8.27--forward_declaration.sv 4state pass chapter-8/8.4--instantiation.sv 2state pass diff --git a/crates/celox-sv-analyzer/src/ast/classes.rs b/crates/celox-sv-analyzer/src/ast/classes.rs index c4d9aafde87..55eff2a48e9 100644 --- a/crates/celox-sv-analyzer/src/ast/classes.rs +++ b/crates/celox-sv-analyzer/src/ast/classes.rs @@ -47,8 +47,19 @@ pub(super) struct Method { pub name: String, /// The class that declares the method. pub owner: String, + /// The definition, or the prototype of a pure virtual method. pub native: celox_sv_syntax::SubroutineId, pub qualifiers: celox_sv_syntax::ClassQualifiers, + /// Declared `virtual` here or in a base: a call through a handle runs + /// the override of the object's class (IEEE 1800-2023 8.20). + pub is_virtual: bool, +} + +impl Method { + /// A pure virtual method, which has no implementation (8.21). + pub fn is_pure(&self) -> bool { + self.qualifiers.is_pure + } } /// A class with the layout of its objects. @@ -58,6 +69,8 @@ pub(super) struct Class { /// Identifies the class at run time; stable across modules and runs. pub id: u32, pub base: Option, + /// A `virtual class`, which cannot be constructed (IEEE 1800-2023 8.21). + pub is_abstract: bool, /// Properties of the class and its bases, bases first. pub properties: Vec, /// How many of `properties` the bases declare. @@ -111,6 +124,40 @@ impl Class { format!("{owner}::{method}") } + /// The subroutine that calls the override of virtual method `method` + /// of `owner` for the class of its `this` (IEEE 1800-2023 8.20). + pub fn dispatcher_name(owner: &str, method: &str) -> String { + format!("{owner}::{method}$virtual") + } + + /// The virtual methods the class itself declares, each with a + /// dispatcher. + pub fn dispatched_methods(&self) -> impl Iterator { + self.methods + .iter() + .filter(|method| method.owner == self.name && method.is_virtual) + } + + /// Whether the class is `ancestor` or derives from it. + pub fn derives_from( + &self, + tree: &AnalysisContext<'_>, + ancestor: &str, + env: &HashMap, + aliases: &HashMap, + ) -> bool { + let mut class = Some(self.name.clone()); + while let Some(name) = class { + if name == ancestor { + return true; + } + class = self::class(tree, &name, env, aliases) + .ok() + .and_then(|class| class.base.clone()); + } + false + } + /// An allocation of an uninitialized object of the class. pub fn allocation(&self) -> Expr { let handles = self @@ -330,12 +377,14 @@ pub(super) fn class( for method in &declaration.methods { let subroutine = native.subroutine(method.subroutine); let method_name = instances::normalize_identifier(&subroutine.name.spelling); - // A pure virtual method has no implementation to call; calls of - // virtual methods are rejected where they are bound. - if method.qualifiers.is_pure { - continue; - } - let native_id = if subroutine.prototype { + // A method overriding a virtual method is virtual (8.20). + let is_virtual = method.qualifiers.is_virtual + || methods + .iter() + .any(|known| known.name == method_name && known.is_virtual); + let native_id = if method.qualifiers.is_pure { + method.subroutine + } else if subroutine.prototype { out_of_block_definition(tree, name, &method_name).ok_or_else(|| { unsupported(format!( "extern method `{name}::{method_name}` without a body" @@ -350,12 +399,55 @@ pub(super) fn class( owner: name.to_owned(), native: native_id, qualifiers: method.qualifiers, + is_virtual, }); } + // A method of an implemented interface class is virtual, and the class + // implements it with the same return type (8.26.2, 8.26.6.1). An + // abstract class may instead re-declare it pure (8.26.7). + let mut required = Vec::new(); + for &interface in &declaration.interfaces { + interface_methods(tree, interface, env, aliases, &mut required)?; + } + for (method_name, prototype) in required { + match methods.iter_mut().find(|method| method.name == method_name) { + Some(method) => { + if !method.is_virtual && method.owner != name { + return Err(unsupported(format!( + "interface class method `{method_name}` implemented by a non-virtual method of a base class" + ))); + } + if return_type(tree, method.native, env, aliases)? + != return_type(tree, prototype, env, aliases)? + { + return Err(unsupported(format!( + "method `{method_name}` of class `{name}` whose return type differs from its interface class" + ))); + } + method.is_virtual = true; + } + None => { + return Err(unsupported(format!( + "class `{name}` that does not implement interface class method `{method_name}`" + ))); + } + } + } + // A class that is not abstract implements every pure virtual method + // (8.21). + if !declaration.virtual_class + && let Some(method) = methods.iter().find(|method| method.is_pure()) + { + return Err(unsupported(format!( + "class `{name}` that does not implement pure virtual method `{}`", + method.name + ))); + } let class = Rc::new(Class { name: name.to_owned(), id: class_id(name), base: base.map(|base| base.name.clone()), + is_abstract: declaration.virtual_class, properties, base_properties, statics, @@ -369,6 +461,48 @@ pub(super) fn class( Ok(class) } +/// The methods that interface class type `interface` declares or inherits, +/// with their prototypes, appended to `methods` (IEEE 1800-2023 8.26). A +/// name that several interface classes declare appears once for each. +fn interface_methods( + tree: &AnalysisContext<'_>, + interface: celox_sv_syntax::TypeId, + env: &HashMap, + aliases: &HashMap, + methods: &mut Vec<(String, celox_sv_syntax::SubroutineId)>, +) -> Converted<()> { + let native = tree.declarations(); + let name = type_binding::procedural_type(interface, native, tree, env, aliases)? + .and_then(|ty| ty.class.clone()) + .ok_or_else(|| unsupported("implemented interface class type"))?; + let declaration = declaration(tree, &name) + .filter(|declaration| declaration.interface) + .ok_or_else(|| unsupported(format!("`implements` of `{name}`, not an interface class")))?; + for method in &declaration.methods { + let method_name = + instances::normalize_identifier(&native.subroutine(method.subroutine).name.spelling); + methods.push((method_name, method.subroutine)); + } + for &base in &declaration.interfaces { + interface_methods(tree, base, env, aliases, methods)?; + } + Ok(()) +} + +/// The return type of subroutine `id`, `None` for a task or `void`. +fn return_type( + tree: &AnalysisContext<'_>, + id: celox_sv_syntax::SubroutineId, + env: &HashMap, + aliases: &HashMap, +) -> Converted> { + let native = tree.declarations(); + match native.subroutine(id).return_type { + Some(ty) => type_binding::procedural_type(ty, native, tree, env, aliases), + None => Ok(None), + } +} + /// The definition `function ... class::method` outside class `class`. fn out_of_block_definition( tree: &AnalysisContext<'_>, @@ -428,6 +562,47 @@ fn class_id(name: &str) -> u32 { hash.max(1) } +/// The overrides that dispatcher `Class::dispatcher_name(owner, method)` +/// calls: each implementation of the method with the classes, deriving from +/// `owner` and not abstract, whose objects run it. +pub(super) fn dispatch_targets( + tree: &AnalysisContext<'_>, + owner: &str, + method: &str, + env: &HashMap, + aliases: &HashMap, +) -> Converted)>> { + let mut targets: Vec<(String, Vec)> = Vec::new(); + let mut ids = HashMap::default(); + for declaration in &tree.declarations().classes { + if declaration.interface || declaration.virtual_class { + continue; + } + let name = instances::normalize_identifier(&declaration.name.spelling); + // A class that cannot be laid out has no objects. + let Ok(class) = class(tree, &name, env, aliases) else { + continue; + }; + if !class.derives_from(tree, owner, env, aliases) { + continue; + } + if let Some(other) = ids.insert(class.id, name.clone()) { + return Err(unsupported(format!( + "classes `{other}` and `{name}` with the same run-time identifier" + ))); + } + let Some(implementation) = class.method(method) else { + continue; + }; + let target = Class::subroutine_name(&implementation.owner, method); + match targets.iter_mut().find(|(known, _)| *known == target) { + Some((_, classes)) => classes.push(class.id), + None => targets.push((target, vec![class.id])), + } + } + Ok(targets) +} + /// A call of object operation `op`. pub(super) fn object_call(op: ObjectOp, args: Vec) -> Expr { Expr::Call { diff --git a/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs b/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs index 775b26fc042..d15c8400ea1 100644 --- a/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs +++ b/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs @@ -439,6 +439,16 @@ fn class_signatures( } else { return_type(subroutine, tree, context)? }; + // A method that may be virtual also has a dispatcher (8.20). + if !method.qualifiers.is_static && subroutine.kind != SubroutineKind::Constructor { + publish_signature( + classes::Class::dispatcher_name(&class_name, &name(&subroutine.name)), + formals.clone(), + result.clone(), + tree, + context, + ); + } publish_signature( classes::Class::subroutine_name(&class_name, &name(&subroutine.name)), formals, diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding.rs b/crates/celox-sv-analyzer/src/ast/expression_binding.rs index 79354194004..065187f34ae 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding.rs @@ -42,8 +42,21 @@ pub(super) fn expression( LValue::Ident(name) => dimensions.get(name), _ => None, }; - // `new` creates an object of the variable's class (IEEE 1800-2023 8.7). - if let Some(class) = shape.and_then(|shape| shape.class.as_deref()) + // `new` creates an object of the class of the variable or of the + // element of an array of handles (IEEE 1800-2023 8.7). + let class = match lhs { + LValue::Ident(_) => shape.and_then(|shape| shape.class.as_deref()), + LValue::Select { + name, + array_slice_width: None, + .. + } => dimensions + .get(name) + .filter(|shape| !shape.unpacked.is_empty()) + .and_then(|shape| shape.class.as_deref()), + LValue::Select { .. } => None, + }; + if let Some(class) = class && let Some(call) = objects::constructor(tree, root, native, dimensions, class)? { return Ok(call); diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs index 5a23baf7cb6..7665af7a0b4 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs @@ -21,14 +21,26 @@ pub(in crate::ast) fn class( } /// The class of native expression `id` when it is an object handle, judged -/// before it is bound: a handle variable, or a handle property. +/// before it is bound: a handle variable, an element of an array of +/// handles, or a handle property. pub(in crate::ast) fn native_class( tree: &AnalysisContext<'_>, id: ExprId, native: &Declarations, dimensions: &PackedDimensions, ) -> Option { - if let ExprKind::Name(name) = &native.expression(ungroup(id, native)).kind { + let expression = native.expression(ungroup(id, native)); + if let ExprKind::Select { + expression, + dimension, + } = &expression.kind + && matches!(dimension.kind, celox_sv_syntax::DimensionKind::Index(_)) + && let ExprKind::Name(name) = &native.expression(ungroup(*expression, native)).kind + { + let ty = dimensions.get(&instances::normalize_identifier(&name.spelling))?; + return ty.class.clone().filter(|_| !ty.unpacked.is_empty()); + } + if let ExprKind::Name(name) = &expression.kind { let name = instances::normalize_identifier(&name.spelling); if let Some(ty) = dimensions.get(&name) { return ty.class.clone(); @@ -214,20 +226,26 @@ pub(in crate::ast) enum Receiver { /// The method that native callee expression `id` names: through a handle /// (`h.m`), `this.m`, `super.m`, `C::m`, or unqualified inside a method. /// Returns the method's subroutine, its number of formals, and the object -/// it is called on. +/// it is called on. A virtual method is called through its dispatcher, +/// except as `super.m`, which names the base's implementation (IEEE +/// 1800-2023 8.15, 8.20). pub(in crate::ast) fn method( tree: &AnalysisContext<'_>, id: ExprId, native: &Declarations, dimensions: &PackedDimensions, ) -> Converted> { + let mut through_super = false; let (class_name, method_name, receiver) = match &native.expression(ungroup(id, native)).kind { ExprKind::Member { expression, separator, name, } if separator == "." => match native_class(tree, *expression, native, dimensions) { - Some(class) if is_super(*expression, native) => (class, name, Receiver::This), + Some(class) if is_super(*expression, native) => { + through_super = true; + (class, name, Receiver::This) + } Some(class) => (class, name, Receiver::Handle(*expression)), None => return Ok(None), }, @@ -265,8 +283,11 @@ pub(in crate::ast) fn method( let Some(method) = class.method(&method_name) else { return Ok(None); }; - if method.qualifiers.is_virtual { - return Err(unsupported(format!("virtual method `{}`", method.name))); + if through_super && method.is_pure() { + return Err(unsupported(format!( + "pure virtual method `{}` called through `super`", + method.name + ))); } let receiver = if method.qualifiers.is_static { Receiver::Static @@ -292,11 +313,12 @@ pub(in crate::ast) fn method( .ports .as_ref() .map_or(0, Vec::len); - Ok(Some(( - Class::subroutine_name(&method.owner, &method.name), - formals, - receiver, - ))) + let subroutine = if method.is_virtual && !through_super && receiver != Receiver::Static { + Class::dispatcher_name(&method.owner, &method.name) + } else { + Class::subroutine_name(&method.owner, &method.name) + }; + Ok(Some((subroutine, formals, receiver))) } /// The receiver of an unqualified method call inside a method. @@ -339,6 +361,11 @@ pub(in crate::ast) fn constructor( _ => return Ok(None), }; let class = class(tree, dimensions, &class_name)?; + if class.is_abstract { + return Err(unsupported(format!( + "object of abstract class `{class_name}`" + ))); + } let formals = class.constructor_formals(native); let mut args = vec![Some(class.allocation())]; for argument in arguments { diff --git a/crates/celox-sv-analyzer/src/ast/functions.rs b/crates/celox-sv-analyzer/src/ast/functions.rs index 2112b88a286..65393bef9c8 100644 --- a/crates/celox-sv-analyzer/src/ast/functions.rs +++ b/crates/celox-sv-analyzer/src/ast/functions.rs @@ -100,6 +100,7 @@ pub(super) fn native_functions( &mut state, item, None, + None, ) { Ok(subroutine) => { let has_real = subroutine.return_type.as_ref().is_some_and(|ty| { diff --git a/crates/celox-sv-analyzer/src/ast/procedural.rs b/crates/celox-sv-analyzer/src/ast/procedural.rs index 0f1ff9d0e7c..6d1661eb256 100644 --- a/crates/celox-sv-analyzer/src/ast/procedural.rs +++ b/crates/celox-sv-analyzer/src/ast/procedural.rs @@ -1541,17 +1541,38 @@ pub(super) fn subroutine_argument_names( handle.clone(), None, ); - for (native_id, is_static) in classes::declared_methods(tree, &class)? { + let mut methods = classes::declared_methods(tree, &class)? + .into_iter() + .map(|(native_id, is_static)| { + let name = instances::normalize_identifier( + &tree.declarations().subroutine(native_id).name.spelling, + ); + ( + classes::Class::subroutine_name(&class, &name), + native_id, + is_static, + ) + }) + .collect::>(); + // A class that cannot be laid out reports why where it is + // lowered. + if let Ok(layout) = classes::class(tree, &class, &item.env, &aliases) { + methods.extend(layout.dispatched_methods().map(|method| { + ( + classes::Class::dispatcher_name(&class, &method.name), + method.native, + false, + ) + })); + } + for (name, native_id, is_static) in methods { let native = tree.declarations().subroutine(native_id); let mut params = subroutine_param_declarations(native, tree, &item.env, &aliases)?; constructor |= native.kind == celox_sv_syntax::SubroutineKind::Constructor; if !is_static { params.insert(0, this.clone()); } - let qualified = item.name(&classes::Class::subroutine_name( - &class, - &instances::normalize_identifier(&native.name.spelling), - )); + let qualified = item.name(&name); directions.insert( qualified.clone(), params.iter().map(|(_, direction, ..)| *direction).collect(), @@ -1685,6 +1706,7 @@ pub(super) fn subroutines_with_symbolic_parameters( state, item, None, + None, ); let subroutine = match (lowered, rejected.as_deref_mut()) { (Ok(subroutine), _) => subroutine, @@ -1747,16 +1769,18 @@ pub(super) fn lower_subroutine( state: &mut BodyState<'_>, item: &generate::Item, class: Option<&classes::Class>, + dispatch: Option<&[(String, Vec)]>, ) -> Converted { let native = tree.declarations().subroutine(native_id); let name = instances::normalize_identifier(&native.name.spelling); let constructor = native.kind == celox_sv_syntax::SubroutineKind::Constructor; - let is_static = class.is_some_and(|class| { - class - .methods - .iter() - .any(|method| method.native == native_id && method.qualifiers.is_static) - }); + let is_static = dispatch.is_none() + && class.is_some_and(|class| { + class + .methods + .iter() + .any(|method| method.native == native_id && method.qualifiers.is_static) + }); let handle = class .map(|class| { classes::handle_type(tree, &class.name) @@ -1902,7 +1926,14 @@ pub(super) fn lower_subroutine( prologue = builder.constructor_prologue(class, &this.name, result, arguments)?; } let mut body = prologue; - body.extend(builder.native_body(&items, &native.authored_statements)?); + match dispatch { + Some(targets) => body.push(dispatch_body( + targets, + &lowered_params, + return_var.as_deref(), + )), + None => body.extend(builder.native_body(&items, &native.authored_statements)?), + } builder.pop_scope(); let static_types: HashMap = builder .state @@ -1923,6 +1954,9 @@ pub(super) fn lower_subroutine( } let mut subroutine = Subroutine { name: item.name(&match class { + Some(class) if dispatch.is_some() => { + classes::Class::dispatcher_name(&class.name, &name) + } Some(class) => classes::Class::subroutine_name(&class.name, &name), None => name, }), @@ -2055,6 +2089,66 @@ pub(super) fn lower_subroutine( Ok(subroutine) } +/// The body of a dispatcher of a virtual method: a call of the override in +/// `targets` for the class of `this`, the first of `params`, passing the +/// formals on (IEEE 1800-2023 8.20). Every class an object can have is in +/// `targets`, so any other class is that of `null`, whose dereference is an +/// error (8.4). +fn dispatch_body( + targets: &[(String, Vec)], + params: &[SubroutineParam], + result: Option<&str>, +) -> Stmt { + let this = Expr::Ident(params[0].name.clone()); + let args = params + .iter() + .map(|param| Some(Expr::Ident(param.name.clone()))) + .collect::>(); + let items = targets + .iter() + .map(|(target, classes)| { + let call = match result { + Some(result) => Stmt::Assign { + lhs: LValue::Ident(result.to_owned()), + rhs: Expr::Call { + callee: Callee::Subroutine(target.clone()), + args: args.clone(), + arg_order: Vec::new(), + }, + nonblocking: false, + }, + None => Stmt::Call { + name: target.clone(), + args: args.clone(), + arg_order: Vec::new(), + }, + }; + CaseItemBase { + labels: classes + .iter() + .map(|id| CaseLabel::Value(Expr::Literal(format!("32'd{id}")))) + .collect(), + body: vec![call], + } + }) + .collect(); + let null_dereference = classes::object_call( + system_functions::ObjectOp::Load(system_functions::ObjectField { + offset: 0, + width: 8, + signed: false, + handle: None, + }), + vec![this.clone()], + ); + Stmt::Case { + kind: CaseKind::Exact, + selector: classes::object_call(system_functions::ObjectOp::ClassOf, vec![this]), + items, + default: Some(vec![Stmt::Eval(null_dereference)]), + } +} + /// The `super.new(args)` call that starts constructor body `items`, as its /// index in `items` and its arguments (IEEE 1800-2023 8.15, 8.17). fn super_new<'a>( @@ -2217,6 +2311,23 @@ fn lower_class( state, item, Some(&class), + None, + )?); + } + for method in class.dispatched_methods() { + let targets = + classes::dispatch_targets(tree, name, &method.name, declaration_env, type_aliases)?; + subroutines.push(lower_subroutine( + method.native, + tree, + declaration_env, + type_aliases, + item_dimensions, + literals, + state, + item, + Some(&class), + Some(&targets), )?); } if !constructor { diff --git a/crates/celox-sv-syntax/src/nodes.rs b/crates/celox-sv-syntax/src/nodes.rs index f857274c2ca..e72c9d6fcee 100644 --- a/crates/celox-sv-syntax/src/nodes.rs +++ b/crates/celox-sv-syntax/src/nodes.rs @@ -954,6 +954,9 @@ pub struct ClassDeclaration { pub interface: bool, /// `extends` of a class, not of an interface class. pub base: Option, + /// The interface classes of `implements`, or those an interface class + /// extends (8.26). + pub interfaces: Vec, pub properties: Vec, pub methods: Vec, pub tokens: TokenRange, diff --git a/crates/celox-sv-syntax/src/parser/classes.rs b/crates/celox-sv-syntax/src/parser/classes.rs index 0de82cbbff9..e9ea1f3df5f 100644 --- a/crates/celox-sv-syntax/src/parser/classes.rs +++ b/crates/celox-sv-syntax/src/parser/classes.rs @@ -155,6 +155,7 @@ impl Parser<'_> { } let name = self.name()?; let mut base = None; + let mut interfaces = Vec::new(); if self.eat("#") { let start = self.at; self.expect("(")?; @@ -173,8 +174,9 @@ impl Parser<'_> { if self.eat("extends") { let data_type = self.class_base(interface)?; if interface { + interfaces.push(data_type); while self.eat(",") { - self.class_base(true)?; + interfaces.push(self.class_base(true)?); } } else { let arguments = if self.peek() == "(" { @@ -192,9 +194,9 @@ impl Parser<'_> { } } if !interface && self.eat("implements") { - self.class_base(true)?; + interfaces.push(self.class_base(true)?); while self.eat(",") { - self.class_base(true)?; + interfaces.push(self.class_base(true)?); } } self.expect(";")?; @@ -203,6 +205,7 @@ impl Parser<'_> { virtual_class, interface, base, + interfaces, properties: Vec::new(), methods: Vec::new(), tokens: TokenRange { start, end: start }, diff --git a/crates/celox-test-suite/LIMITATIONS.md b/crates/celox-test-suite/LIMITATIONS.md index 5f9554fe29c..287bdccd714 100644 --- a/crates/celox-test-suite/LIMITATIONS.md +++ b/crates/celox-test-suite/LIMITATIONS.md @@ -493,6 +493,8 @@ four-state expectations; the assertions remain enabled on all Celox variants. | `sv_icarus_classes` | compile | icarus | 5 | Icarus 13.0 does not support extern methods ("sorry: External methods are not yet supported") or string methods called on a class property ("Can't find task itoa in class Named") or comparisons of class handles ("SORRY: Compare class handles not implemented"), aborts on a failed assertion in dup_expr.cc while compiling objects_referring_to_objects, and crashes with a segmentation fault while compiling implicit_constructors_and_this. IEEE 1800-2023 8.4, 8.7, 8.8 and 8.24 define these constructs. Verilator passes all but the string-method case, see sv_verilator_class_property_string_methods. | | `sv_icarus_nested_class_members` | execute | icarus | 1 | Icarus 13.0 reads an empty string through a nested property path (kept.inner.text) after reporting "sorry: Nested member path not yet supported for class properties" for its assignment. IEEE 1800-2023 8.5 permits accessing a property of an object referred to by a property. Verilator passes the case. | | `sv_icarus_class_scope_resolution` | compile | icarus | 1 | Icarus 13.0 rejects class scope resolution with a syntax error: reading a static property (Counter::created), calling a static method (Counter::next()) and assigning a static property (Counter::last = "set"). IEEE 1800-2023 8.9, 8.10 and 8.23 define these accesses. Verilator passes the case. | +| `sv_icarus_virtual_dispatch` | execute | icarus | 1 | Icarus 13.0 binds a virtual function called through a base-class handle to the handle's class instead of the object's: the shared case prints area=0 for every shape, and the adapter-free source prints 1 1 1 3 where IEEE 1800-2023 8.20 requires 2 3 4 4. Verilator and Celox print 2 3 4 4 and pass the case. | +| `sv_icarus_abstract_and_interface_classes` | compile | icarus | 3 | Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors ("Invalid class item", "Invalid module item") and fails code generation without a diagnostic ("Code generation had 2 error(s)") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases. | | `sv_verilator_class_property_string_methods` | compile | verilator | 1 | Verilator 5.052 stops with an internal error ("AstNode is not of expected type, but instead has type 'MEMBERSEL'") on a string method called on a class property, n.name.itoa(42), which IEEE 1800-2023 6.16 and 8.5 permit. Icarus rejects the case too, see sv_icarus_classes. | | `sv_icarus_unpacked_array_patterns` | compile | icarus | 1 | Icarus 13.0 reports that an assignment pattern with unpacked array items assigned to a multidimensional unpacked array is not yet supported ("Procedural assignment of array or array slice"), so the run cannot count it as a language rejection. The design is invalid (IEEE 1800-2023 7.6, 10.8). | | `sv_icarus_display_field_width` | execute | icarus | 1 | Icarus 13.0 zero-pads a value wider than an explicit field width of %h or %o to the argument's full width (%3h of 32'h1234 prints 00001234); IEEE 1800-2023 21.2.1.2 expands the field to the value without leading zeros (1234). Verilator agrees with the expected value. | diff --git a/crates/celox-test-suite/src/sv/cases/class.vtest b/crates/celox-test-suite/src/sv/cases/class.vtest index 98c2d18e311..c7470fd436a 100644 --- a/crates/celox-test-suite/src/sv/cases/class.vtest +++ b/crates/celox-test-suite/src/sv/cases/class.vtest @@ -312,3 +312,217 @@ endmodule "#) (top Top) (run_until 0) (assert_eq done 1) (expect_output "a=1 b=7 c=103\nderived=8110 base=8\nfixed=41 default=3\n")) + +(case virtual_methods_dispatch_on_object_class + (source "class.sv" #" +module Top(output bit done); + class Shape; + int id; + function new(int id); + this.id = id; + endfunction + virtual function int area(); + return 0; + endfunction + function int describe(); + return id * 1000 + area(); + endfunction + virtual task scale(input int by, output int result); + result = by; + endtask + function int plain(); + return 1; + endfunction + endclass + class Square extends Shape; + int side; + function new(int id, int side); + super.new(id); + this.side = side; + endfunction + function int area(); + return side * side; + endfunction + task scale(input int by, output int result); + result = area() * by; + endtask + function int plain(); + return 2; + endfunction + endclass + class Cube extends Square; + function new(int id, int side); + super.new(id, side); + endfunction + function int area(); + return super.area() * 6; + endfunction + endclass + class Unchanged extends Shape; + function new(); + super.new(9); + endfunction + endclass + Shape shapes[4]; + Shape shape; + Square square; + Cube cube; + Unchanged unchanged; + int scaled; + initial begin + square = new(1, 3); + cube = new(2, 2); + unchanged = new; + shapes[0] = square; + shapes[1] = cube; + shapes[2] = unchanged; + shapes[3] = new(4); + foreach (shapes[i]) begin + shape = shapes[i]; + shape.scale(10, scaled); + $display("%0d area=%0d describe=%0d scaled=%0d plain=%0d", i, + shape.area(), shape.describe(), scaled, shape.plain()); + end + square = cube; + $display("square area=%0d plain=%0d", square.area(), square.plain()); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1) + (expect_output "0 area=9 describe=1009 scaled=90 plain=1\n1 area=24 describe=2024 scaled=240 plain=1\n2 area=0 describe=9000 scaled=10 plain=1\n3 area=0 describe=4000 scaled=10 plain=1\nsquare area=24 plain=2\n")) + +(case abstract_classes_and_pure_virtual_methods + (source "class.sv" #" +module Top(output bit done); + virtual class Animal; + string name; + function new(string name); + this.name = name; + endfunction + pure virtual function string sound(); + function string speak(); + return {name, " says ", sound()}; + endfunction + endclass + class Dog extends Animal; + function new(); + super.new("dog"); + endfunction + virtual function string sound(); + return "woof"; + endfunction + endclass + virtual class Bird extends Animal; + function new(string name); + super.new(name); + endfunction + endclass + class Crow extends Bird; + function new(); + super.new("crow"); + endfunction + function string sound(); + return "caw"; + endfunction + endclass + Animal animal; + Dog dog; + Crow crow; + initial begin + dog = new; + crow = new; + animal = dog; + $display("%s", animal.speak()); + animal = crow; + $display("%s / %s", animal.speak(), animal.sound()); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1) + (expect_output "dog says woof\ncrow says caw / caw\n")) + +(case methods_of_handle_array_elements + (source "class.sv" #" +module Top(output bit done); + class Item; + int value; + function new(int value); + this.value = value; + endfunction + virtual function int get(); + return value; + endfunction + endclass + class Twice extends Item; + function new(int value); + super.new(value); + endfunction + function int get(); + return value * 2; + endfunction + endclass + Item items[3]; + Twice twice; + int sum; + initial begin + items[0] = new(1); + twice = new(5); + items[1] = twice; + items[2] = new(7); + foreach (items[i]) sum = sum * 100 + items[i].get(); + $display("%0d %0d %0d", items[1].get(), items[1].value, sum); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1) + (expect_output "10 5 11007\n")) + +(case classes_implementing_interface_classes + (source "class.sv" #" +module Top(output bit done); + interface class Named; + pure virtual function int code(); + endclass + interface class Sized extends Named; + pure virtual function int size(); + endclass + virtual class Partial implements Sized; + pure virtual function int code(); + function int size(); + return code() * 2; + endfunction + endclass + class Box extends Partial; + int n; + function new(int n); + this.n = n; + endfunction + function int code(); + return n; + endfunction + endclass + class Crate extends Box; + function new(); + super.new(4); + endfunction + function int code(); + return 100; + endfunction + endclass + Box box; + Crate crate; + initial begin + box = new(3); + crate = new; + $display("%0d %0d", box.code(), box.size()); + box = crate; + $display("%0d %0d", box.code(), box.size()); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1) + (expect_output "3 6\n100 200\n")) diff --git a/crates/celox-test-suite/verification/repros/virtual_dispatch.sv b/crates/celox-test-suite/verification/repros/virtual_dispatch.sv new file mode 100644 index 00000000000..1826c5f1756 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/virtual_dispatch.sv @@ -0,0 +1,41 @@ +// A virtual function called through a base-class handle runs the override of +// the object's class (IEEE 1800-2023 8.20). Expected output: 2 3 4 4. +module Top; + class A; + virtual function int f(); + return 1; + endfunction + endclass + class B extends A; + function int f(); + return 2; + endfunction + endclass + class C extends A; + virtual function int f(); + return 3; + endfunction + endclass + class D extends C; + function int f(); + return 4; + endfunction + endclass + A a; + B b; + C c; + D d; + initial begin + b = new; + c = new; + d = new; + a = b; + $write("%0d ", a.f()); + a = c; + $write("%0d ", a.f()); + a = d; + $write("%0d ", a.f()); + c = d; + $display("%0d", c.f()); + end +endmodule diff --git a/crates/celox-test-suite/verification/repros/virtual_dispatch_icarus.json b/crates/celox-test-suite/verification/repros/virtual_dispatch_icarus.json new file mode 100644 index 00000000000..691c18acbf1 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/virtual_dispatch_icarus.json @@ -0,0 +1,42 @@ +{ + "schema_version": 3, + "suite_version": "0.11.0", + "tool": "icarus", + "version": "Icarus Verilog version 13.0", + "cases": [ + { + "category": "Types", + "detail": "class::virtual_methods_dispatch_on_object_class at 393:3: expect_output: expected [48, 32, 97, 114, 101, 97, 61, 57, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 49, 48, 48, 57, 32, 115, 99, 97, 108, 101, 100, 61, 57, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 49, 32, 97, 114, 101, 97, 61, 50, 52, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 50, 48, 50, 52, 32, 115, 99, 97, 108, 101, 100, 61, 50, 52, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 50, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 57, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 51, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 52, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 115, 113, 117, 97, 114, 101, 32, 97, 114, 101, 97, 61, 50, 52, 32, 112, 108, 97, 105, 110, 61, 50, 10], got [48, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 49, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 49, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 50, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 50, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 57, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 51, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 52, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 115, 113, 117, 97, 114, 101, 32, 97, 114, 101, 97, 61, 52, 32, 112, 108, 97, 105, 110, 61, 50, 10]", + "expectation": "Simulation", + "name": "class::virtual_methods_dispatch_on_object_class", + "phase": "execute", + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + } + ], + "counts": { + "passed": 0, + "mismatch": 1, + "compile_error": 0, + "emission_error": 0, + "ignored": 0, + "rejected": 0, + "runtime_error": 0, + "unexpected_accept": 0, + "unsupported": 0 + }, + "include_ignored": true, + "exclude_stronger_than_sv": false, + "standard": "IEEE 1800-2023 8.20: a virtual method called through a base-class handle runs the override of the object's class; an override is virtual whether or not it repeats the keyword.", + "adapter_free": { + "source": "verification/repros/virtual_dispatch.sv", + "icarus_command": "iverilog -g2012 -s Top -o virtual_dispatch.sv; vvp ", + "icarus_version": "Icarus Verilog version 13.0", + "icarus_output": "1 1 1 3", + "verilator_command": "verilator --binary --timing -Wno-fatal --top-module Top --Mdir virtual_dispatch.sv", + "verilator_output": "2 3 4 4", + "expected_output": "2 3 4 4" + } +} diff --git a/crates/celox-test-suite/verification/sv/icarus/class.json b/crates/celox-test-suite/verification/sv/icarus/class.json index 02c99aaf1a5..2c488fd85ce 100644 --- a/crates/celox-test-suite/verification/sv/icarus/class.json +++ b/crates/celox-test-suite/verification/sv/icarus/class.json @@ -1,5 +1,51 @@ { "cases": [ + { + "category": "Types", + "detail": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_compile_limitation", + "evidence": [ + "verification/sv/limitations/icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "id": "sv_icarus_abstract_and_interface_classes", + "observed_version": "Celox SV frontend; Verilator 5.052 / Icarus 13.0", + "phase": "compile", + "reason": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "upstream": [] + }, + "name": "class::abstract_classes_and_pure_virtual_methods", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Types", + "detail": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_compile_limitation", + "evidence": [ + "verification/sv/limitations/icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "id": "sv_icarus_abstract_and_interface_classes", + "observed_version": "Celox SV frontend; Verilator 5.052 / Icarus 13.0", + "phase": "compile", + "reason": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "upstream": [] + }, + "name": "class::classes_implementing_interface_classes", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Types", "detail": "", @@ -91,6 +137,29 @@ "tag_reasons": {}, "tags": [] }, + { + "category": "Types", + "detail": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_compile_limitation", + "evidence": [ + "verification/sv/limitations/icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "id": "sv_icarus_abstract_and_interface_classes", + "observed_version": "Celox SV frontend; Verilator 5.052 / Icarus 13.0", + "phase": "compile", + "reason": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "upstream": [] + }, + "name": "class::methods_of_handle_array_elements", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Types", "detail": "", @@ -193,6 +262,30 @@ "stronger_than_sv": false, "tag_reasons": {}, "tags": [] + }, + { + "category": "Types", + "detail": "Icarus 13.0 binds a virtual function called through a base-class handle to the handle's class instead of the object's: the shared case prints area=0 for every shape, and the adapter-free source prints 1 1 1 3 where IEEE 1800-2023 8.20 requires 2 3 4 4. Verilator and Celox print 2 3 4 4 and pass the case.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_disagreement", + "evidence": [ + "verification/repros/virtual_dispatch_icarus.json", + "verification/repros/virtual_dispatch.sv", + "LIMITATIONS.md#systemverilog-suite" + ], + "id": "sv_icarus_virtual_dispatch", + "observed_version": "Celox SV frontend; Verilator 5.052 / Icarus 13.0", + "phase": "execute", + "reason": "Icarus 13.0 binds a virtual function called through a base-class handle to the handle's class instead of the object's: the shared case prints area=0 for every shape, and the adapter-free source prints 1 1 1 3 where IEEE 1800-2023 8.20 requires 2 3 4 4. Verilator and Celox print 2 3 4 4 and pass the case.", + "upstream": [] + }, + "name": "class::virtual_methods_dispatch_on_object_class", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] } ] } diff --git a/crates/celox-test-suite/verification/sv/limitations.json b/crates/celox-test-suite/verification/sv/limitations.json index f2c69d158b0..cd5e9eb793d 100644 --- a/crates/celox-test-suite/verification/sv/limitations.json +++ b/crates/celox-test-suite/verification/sv/limitations.json @@ -1881,6 +1881,43 @@ ] } }, + { + "id": "sv_icarus_virtual_dispatch", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Celox SV frontend; Verilator 5.052 / Icarus 13.0", + "reason": "Icarus 13.0 binds a virtual function called through a base-class handle to the handle's class instead of the object's: the shared case prints area=0 for every shape, and the adapter-free source prints 1 1 1 3 where IEEE 1800-2023 8.20 requires 2 3 4 4. Verilator and Celox print 2 3 4 4 and pass the case.", + "upstream": [], + "evidence": [ + "verification/repros/virtual_dispatch_icarus.json", + "verification/repros/virtual_dispatch.sv", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "icarus": [ + "class::virtual_methods_dispatch_on_object_class" + ] + } + }, + { + "id": "sv_icarus_abstract_and_interface_classes", + "category": "simulator_compile_limitation", + "phase": "compile", + "observed_version": "Celox SV frontend; Verilator 5.052 / Icarus 13.0", + "reason": "Icarus 13.0 rejects pure virtual method declarations and interface classes with syntax errors (\"Invalid class item\", \"Invalid module item\") and fails code generation without a diagnostic (\"Code generation had 2 error(s)\") for a method called on an element of an array of class handles. IEEE 1800-2023 8.21 and 8.26 define pure virtual methods and interface classes, and 7.4 and 8.4 permit arrays of class handles. Verilator passes all three cases.", + "upstream": [], + "evidence": [ + "verification/sv/limitations/icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "icarus": [ + "class::abstract_classes_and_pure_virtual_methods", + "class::methods_of_handle_array_elements", + "class::classes_implementing_interface_classes" + ] + } + }, { "id": "sv_verilator_class_property_string_methods", "category": "simulator_compile_limitation", diff --git a/crates/celox-test-suite/verification/sv/limitations/icarus.json b/crates/celox-test-suite/verification/sv/limitations/icarus.json index 2d569e682de..bcde8372c4e 100644 --- a/crates/celox-test-suite/verification/sv/limitations/icarus.json +++ b/crates/celox-test-suite/verification/sv/limitations/icarus.json @@ -7,9 +7,9 @@ "passed": 0, "rejected": 0, "unexpected_accept": 1, - "mismatch": 15, + "mismatch": 16, "emission_error": 0, - "compile_error": 113, + "compile_error": 116, "runtime_error": 7, "unsupported": 0, "ignored": 0 @@ -1279,6 +1279,38 @@ "name": "class::static_properties_and_methods", "phase": "compile", "status": "compile_error" + }, + { + "category": "Types", + "detail": "class::virtual_methods_dispatch_on_object_class at 393:3: expect_output: expected [48, 32, 97, 114, 101, 97, 61, 57, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 49, 48, 48, 57, 32, 115, 99, 97, 108, 101, 100, 61, 57, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 49, 32, 97, 114, 101, 97, 61, 50, 52, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 50, 48, 50, 52, 32, 115, 99, 97, 108, 101, 100, 61, 50, 52, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 50, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 57, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 51, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 52, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 115, 113, 117, 97, 114, 101, 32, 97, 114, 101, 97, 61, 50, 52, 32, 112, 108, 97, 105, 110, 61, 50, 10], got [48, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 49, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 49, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 50, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 50, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 57, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 51, 32, 97, 114, 101, 97, 61, 48, 32, 100, 101, 115, 99, 114, 105, 98, 101, 61, 52, 48, 48, 48, 32, 115, 99, 97, 108, 101, 100, 61, 49, 48, 32, 112, 108, 97, 105, 110, 61, 49, 10, 115, 113, 117, 97, 114, 101, 32, 97, 114, 101, 97, 61, 52, 32, 112, 108, 97, 105, 110, 61, 50, 10]", + "expectation": "Simulation", + "name": "class::virtual_methods_dispatch_on_object_class", + "phase": "execute", + "status": "mismatch" + }, + { + "category": "Types", + "detail": "Icarus build exit status: 2; see /build.log\n/source_0.sv:8: syntax error\n/source_0.sv:8: error: Invalid class item.\n", + "expectation": "Simulation", + "name": "class::abstract_classes_and_pure_virtual_methods", + "phase": "compile", + "status": "compile_error" + }, + { + "category": "Types", + "detail": "Icarus build exit status: 1; see /build.log\nerror: Code generation had 2 error(s).\n", + "expectation": "Simulation", + "name": "class::methods_of_handle_array_elements", + "phase": "compile", + "status": "compile_error" + }, + { + "category": "Types", + "detail": "Icarus build exit status: 25; see /build.log\n/source_0.sv:3: syntax error\n/source_0.sv:3: error: Invalid module item.\n/source_0.sv:4: syntax error\n/source_0.sv:4: error: Invalid module item.\n/source_0.sv:5: syntax error\n/source_0.sv:6: error: Invalid module item.\n/source_0.sv:7: syntax error\n/source_0.sv:7: error: Invalid module item.\n/source_0.sv:8: syntax error\n/source_0.sv:9: error: Invalid module item.\n/source_0.sv:10: syntax error\n/source_0.sv:10: error: Invalid module item.\n/source_0.sv:14: syntax error\n/source_0.sv:15: error: Invalid module item.\n/source_0.sv:17: syntax error\n/source_0.sv:17: error: I give up on this function definition.\n/source_0.sv:23: syntax error\n/source_0.sv:24: error: Invalid module item.\n/source_0.sv:25: syntax error\n/source_0.sv:25: error: I give up on this function definition.\n/source_0.sv:28: error: 'code' has already been declared in this scope.\n/source_0.sv:20: : It was declared here as a function.\n/source_0.sv:31: syntax error\n/source_0.sv:32: error: Invalid module item.\n/source_0.sv:33: syntax error\n/source_0.sv:33: error: Invalid module instantiation\n", + "expectation": "Simulation", + "name": "class::classes_implementing_interface_classes", + "phase": "compile", + "status": "compile_error" } ] } diff --git a/crates/celox-test-suite/verification/sv/verilator/class.json b/crates/celox-test-suite/verification/sv/verilator/class.json index 72774285008..736c8fff273 100644 --- a/crates/celox-test-suite/verification/sv/verilator/class.json +++ b/crates/celox-test-suite/verification/sv/verilator/class.json @@ -1,5 +1,27 @@ { "cases": [ + { + "category": "Types", + "detail": "", + "expectation": "Simulation", + "name": "class::abstract_classes_and_pure_virtual_methods", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Types", + "detail": "", + "expectation": "Simulation", + "name": "class::classes_implementing_interface_classes", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Types", "detail": "", @@ -55,6 +77,17 @@ "tag_reasons": {}, "tags": [] }, + { + "category": "Types", + "detail": "", + "expectation": "Simulation", + "name": "class::methods_of_handle_array_elements", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Types", "detail": "", @@ -121,6 +154,17 @@ "stronger_than_sv": false, "tag_reasons": {}, "tags": [] + }, + { + "category": "Types", + "detail": "", + "expectation": "Simulation", + "name": "class::virtual_methods_dispatch_on_object_class", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] } ] } diff --git a/crates/celox/tests/frontends/systemverilog/class.rs b/crates/celox/tests/frontends/systemverilog/class.rs index 4d2e3a2f413..a70bfda6206 100644 --- a/crates/celox/tests/frontends/systemverilog/class.rs +++ b/crates/celox/tests/frontends/systemverilog/class.rs @@ -16,6 +16,10 @@ sv_backends! { fn class_inherited_properties_and_methods(sim) { @case "class::inherited_properties_and_methods"; } fn class_static_properties_and_methods(sim) { @case "class::static_properties_and_methods"; } fn class_constructors_chain_to_base(sim) { @case "class::constructors_chain_to_base"; } + fn class_virtual_methods_dispatch_on_object_class(sim) { @case "class::virtual_methods_dispatch_on_object_class"; } + fn class_abstract_classes_and_pure_virtual_methods(sim) { @case "class::abstract_classes_and_pure_virtual_methods"; } + fn class_methods_of_handle_array_elements(sim) { @case "class::methods_of_handle_array_elements"; } + fn class_classes_implementing_interface_classes(sim) { @case "class::classes_implementing_interface_classes"; } } sv_backends! { @@ -37,6 +41,26 @@ sv_backends! { assert!(error.contains("null object handle"), "{error}"); } + fn virtual_calls_through_null_are_runtime_errors(sim) { + @setup { + let source = r#"module Top(output int value, output bit done); + class C; + virtual function int f(); + return 1; + endfunction + endclass + C c; + initial begin + value = c.f(); + done = 1; + end + endmodule"#; + } + @build celox::Simulation::from_sv_sources(vec![(source, Path::new("null.sv"))], "Top"); + let error = sim.run_until(0).unwrap_err().to_string(); + assert!(error.contains("null object handle"), "{error}"); + } + fn checkpoints_keep_objects_that_state_files_cannot_record(sim) { @setup { let source = r#"module Top(output int value); @@ -91,7 +115,7 @@ fn four_state_properties_are_rejected() { } #[test] -fn misplaced_super_new_and_static_misuse_are_rejected() { +fn misplaced_super_new_static_misuse_and_unimplemented_methods_are_rejected() { for (body, want) in [ ( "class B; endclass class C extends B; int x; function new(); x = 1; super.new(); endfunction endclass", @@ -105,6 +129,23 @@ fn misplaced_super_new_and_static_misuse_are_rejected() { "class C; function int g(); return 1; endfunction static function int f(); return g(); endfunction endclass", "non-static method `g` called from a static method", ), + ( + "virtual class C; pure virtual function int f(); endclass class D extends C; endclass", + "does not implement pure virtual method `f`", + ), + ( + "virtual class B; pure virtual function int f(); endclass class C extends B; function int f(); return super.f(); endfunction endclass", + "pure virtual method `f` called through `super`", + ), + ("virtual class C; endclass", "object of abstract class `C`"), + ( + "interface class I; pure virtual function int f(); endclass virtual class B implements I; endclass class C extends B; endclass", + "does not implement interface class method `f`", + ), + ( + "interface class I; pure virtual function int f(); endclass interface class J; pure virtual function void f(); endclass class C implements I, J; function int f(); return 1; endfunction endclass", + "whose return type differs from its interface class", + ), ] { let source = format!("module Top; {body} C c; initial c = new; endmodule"); let Err(error) = diff --git a/docs/guide/systemverilog-reference.md b/docs/guide/systemverilog-reference.md index 6bf041d4ce4..d3358f3d560 100644 --- a/docs/guide/systemverilog-reference.md +++ b/docs/guide/systemverilog-reference.md @@ -30,7 +30,7 @@ synthesis and is tested at the design level. | Selects | constant and run-time bit selects and indexed part-selects (`[i]`, `[i +: W]`, `[i -: W]`), in reads and writes, in either declaration direction | | Patterns | assignment patterns for packed structs, packed arrays and unpacked arrays (`'{a, b}`, `'{x: a, default: 0}`, `'{n{a}}`, `T'{...}`) | | Strings | `string` variables, function arguments and results: assignment of strings, string literals and casts (`string'(x)`), `==`, `!=` and ordering comparisons, concatenation and replication (with a run-time count), character reads and writes (`s[i]`), and the methods `len`, `getc`, `putc`, `toupper`, `tolower`, `compare`, `icompare`, `substr`, `atoi` / `atohex` / `atooct` / `atobin` and `itoa` / `hextoa` / `octtoa` / `bintoa`; `%s` and format-less display of string values. See [strings](#strings) | -| Classes | classes declared in a module: properties of 2-state integral types up to 64 bits, `string` and class handle types, with initializers; `new` with and without arguments, implicit and explicit constructors; methods (`function` and `task`, also `extern` with an out-of-block body) called through a handle or unqualified inside the class; `this`, `null`, handle assignment and comparison; inheritance of properties and non-virtual methods, with base constructors called by `super.new(...)` or `extends Base(...)`; `super.` members; static properties and methods; `C::` scope resolution of static members and `C::new`. See [classes](#classes) | +| Classes | classes declared in a module: properties of 2-state integral types up to 64 bits, `string` and class handle types, with initializers; `new` with and without arguments, implicit and explicit constructors; methods (`function` and `task`, also `extern` with an out-of-block body) called through a handle or unqualified inside the class; `this`, `null`, handle assignment and comparison; inheritance of properties and methods, with base constructors called by `super.new(...)` or `extends Base(...)`; `super.` members; virtual methods, abstract (`virtual`) classes and pure virtual methods; classes that implement interface classes; static properties and methods; `C::` scope resolution of static members and `C::new`; arrays of handles. See [classes](#classes) | | Parameters | integral parameters and overrides preserving widths beyond 128 bits and X/Z values, and parameters of unpacked array or packed struct type given by an assignment pattern (constant tables), including unpacked-array pattern overrides | ### Processes and control flow @@ -128,7 +128,7 @@ Celox reports an `Unsupported` error naming the construct instead of ignoring it. The error carries the number of the issue that tracks the construct; constructs without a dedicated issue point to the frontend roadmap, [#88](https://github.com/celox-sim/celox/issues/88). -- Class features beyond those [listed](#classes): virtual method calls, abstract and interface classes, classes declared in packages, parameterized classes, named constructor arguments, shallow copies (`new h`), and properties that are 4-state, `real`, arrays or wider than 64 bits. Timed tasks with static local declarations, recursive calls and `ref` arguments remain unsupported. +- Class features beyond those [listed](#classes): method calls through an interface class handle, `$cast`, classes declared in packages, parameterized classes, named constructor arguments, shallow copies (`new h`), and properties that are 4-state, `real`, arrays or wider than 64 bits. Timed tasks with static local declarations, recursive calls and `ref` arguments remain unsupported. - Behavioral and verification constructs: `final`, intra-assignment timing controls (`a = #1 b`), delayed continuous assignments ([#444](https://github.com/celox-sim/celox/issues/444)), named events and `->`, `fork` / `join`, `wait fork` / `disable fork`, concurrent assertions, `force` / `release`. @@ -229,8 +229,14 @@ object call into the runtime. Objects that no variable or other object refers to are collected between time slots and after each `tick`. A method is called with the object it was called on, so it can be a function or a task, and calls are bound to the method of the variable's class (IEEE 1800-2023 8.6). -Reading or writing a property or calling a method through `null` is a runtime -error. +A call of a virtual method runs the override of the object's class instead, +except `super.m(...)`, which runs the base's implementation (8.15, 8.20); it +selects the override by the class of the object, so it costs one more call +into the runtime. Reading or writing a property or calling a method through +`null` is a runtime error. Constructing an abstract class and a class that +leaves a pure virtual method unimplemented are rejected (8.21). A class that +implements an interface class implements each of its methods with the same +return type, or, if it is abstract, re-declares it pure (8.26). A constructor calls the constructor of the base class first, with the arguments of `super.new(...)` or of the `extends` clause, then runs the diff --git a/docs/ja/guide/systemverilog-reference.md b/docs/ja/guide/systemverilog-reference.md index b67333b1c30..7667500252d 100644 --- a/docs/ja/guide/systemverilog-reference.md +++ b/docs/ja/guide/systemverilog-reference.md @@ -28,7 +28,7 @@ outline: [2, 3] | 式 | `**` を含む算術、論理、シフト、比較、リダクション、連結・複製、`?:`、`inside`、`==?` / `!=?`、キャスト(`N'(x)`、`signed'(x)`、`T'(x)`)、`$signed` / `$unsigned` | | 選択 | 定数・実行時のビット選択と indexed part-select(`[i]`、`[i +: W]`、`[i -: W]`)。読み書きの両方、宣言の向きによらず | | パターン | packed 構造体・packed 配列・unpacked 配列の assignment pattern(`'{a, b}`、`'{x: a, default: 0}`、`'{n{a}}`、`T'{...}`) | -| クラス | モジュール内で宣言したクラス。64 ビット以下の 2 値整数型・`string`・クラスハンドル型のプロパティ(初期値つきも可)、引数あり・なしの `new`、暗黙と明示のコンストラクタ、ハンドル経由またはクラス内から修飾なしで呼ぶメソッド(`function` と `task`、本体をクラス外に書く `extern` も可)、`this`、`null`、ハンドルの代入と比較、プロパティと非 virtual メソッドの継承(基底クラスのコンストラクタは `super.new(...)` か `extends Base(...)` で呼ぶ)、`super.` によるメンバ参照、static プロパティとメソッド、static メンバと `C::new` の `C::` によるスコープ解決。[クラス](#classes)を参照 | +| クラス | モジュール内で宣言したクラス。64 ビット以下の 2 値整数型・`string`・クラスハンドル型のプロパティ(初期値つきも可)、引数あり・なしの `new`、暗黙と明示のコンストラクタ、ハンドル経由またはクラス内から修飾なしで呼ぶメソッド(`function` と `task`、本体をクラス外に書く `extern` も可)、`this`、`null`、ハンドルの代入と比較、プロパティとメソッドの継承(基底クラスのコンストラクタは `super.new(...)` か `extends Base(...)` で呼ぶ)、`super.` によるメンバ参照、virtual メソッド、抽象クラス(`virtual class`)と pure virtual メソッド、interface クラスを実装するクラス、static プロパティとメソッド、static メンバと `C::new` の `C::` によるスコープ解決、ハンドルの配列。[クラス](#classes)を参照 | | 文字列 | `string` 型の変数、関数の引数と戻り値。文字列・文字列リテラル・キャスト(`string'(x)`)の代入、`==` / `!=` と大小比較、連結と複製(実行時の回数も可)、文字の読み書き(`s[i]`)、メソッド `len`、`getc`、`putc`、`toupper`、`tolower`、`compare`、`icompare`、`substr`、`atoi` / `atohex` / `atooct` / `atobin`、`itoa` / `hextoa` / `octtoa` / `bintoa`。`%s` と書式なしの表示。[文字列](#strings)を参照 | | パラメータ | 整数のパラメータ、128ビットを超える幅と X/Z を保持するパラメータ上書き、assignment pattern で与える unpacked 配列型・packed 構造体型のパラメータ(定数テーブル)と unpacked 配列のパターンによる上書き | @@ -118,7 +118,7 @@ SystemVerilog に対しても、共有の Veryl 適合性スイートを実行 その構文を追跡する issue の番号が含まれます。専用の issue がない構文は、フロントエンドの ロードマップ [#88](https://github.com/celox-sim/celox/issues/88) を指します。 -- [対応範囲](#classes)外のクラスの機能:virtual メソッドの呼び出し、抽象クラスと interface クラス、package 内で宣言したクラス、パラメータ付きクラス、名前付きのコンストラクタ引数、浅いコピー(`new h`)、4 値・`real`・配列・64 ビットを超えるプロパティ。時間待ちを含む task の static ローカル宣言、再帰呼び出し、`ref` 引数。 +- [対応範囲](#classes)外のクラスの機能:interface クラスのハンドルを通したメソッド呼び出し、`$cast`、package 内で宣言したクラス、パラメータ付きクラス、名前付きのコンストラクタ引数、浅いコピー(`new h`)、4 値・`real`・配列・64 ビットを超えるプロパティ。時間待ちを含む task の static ローカル宣言、再帰呼び出し、`ref` 引数。 - interface のポート、`inout` / `ref` / 式を使う modport、virtual interface など。 interface 自体と入れ子の interface は対応しています。型パラメータはインスタンスごとに解決し、modport ポート経由でも伝搬します。 再帰する特殊化と64段を超える入れ子は拒否します。細かな制約は[英語版の一覧](/guide/systemverilog-reference#interface-restrictions)を参照してください。 @@ -167,7 +167,7 @@ SystemVerilog に対しても、共有の Veryl 適合性スイートを実行 ### クラス {#classes} -クラス型の変数はシミュレータのヒープ上のオブジェクトへのハンドルを持ち、コンストラクタを呼ぶまでは `null` です。文字列と同じく、ハンドルのコピーと比較は 32 ビット整数と同じコストで、プロパティの読み書きとオブジェクトの生成だけがランタイムを呼び出します。どの変数・オブジェクトからも参照されないオブジェクトは、タイムスロットの間と各 `tick` の後に回収されます。メソッドは呼び出したオブジェクトを受け取るので function でも task でもよく、呼び出しは変数のクラスのメソッドに結び付きます(IEEE 1800-2023 8.6)。`null` を通したプロパティの読み書きやメソッド呼び出しは実行時エラーです。 +クラス型の変数はシミュレータのヒープ上のオブジェクトへのハンドルを持ち、コンストラクタを呼ぶまでは `null` です。文字列と同じく、ハンドルのコピーと比較は 32 ビット整数と同じコストで、プロパティの読み書きとオブジェクトの生成だけがランタイムを呼び出します。どの変数・オブジェクトからも参照されないオブジェクトは、タイムスロットの間と各 `tick` の後に回収されます。メソッドは呼び出したオブジェクトを受け取るので function でも task でもよく、呼び出しは変数のクラスのメソッドに結び付きます(IEEE 1800-2023 8.6)。virtual メソッドの呼び出しは、代わりにオブジェクトのクラスの override を実行します。ただし `super.m(...)` は基底クラスの実装を実行します(8.15、8.20)。override はオブジェクトのクラスで選ぶので、ランタイムの呼び出しが 1 回増えます。`null` を通したプロパティの読み書きやメソッド呼び出しは実行時エラーです。抽象クラスの生成と、pure virtual メソッドを実装しないクラスはエラーになります(8.21)。interface クラスを実装するクラスは、そのメソッドをすべて同じ戻り値型で実装するか、抽象クラスなら pure として宣言し直します(8.26)。 コンストラクタは、まず `super.new(...)` または `extends` 節の引数で基底クラスのコンストラクタを呼び、次に自クラスが宣言するプロパティの初期値を設定し、最後に本体を実行します(IEEE 1800-2023 8.17)。static プロパティはクラスを宣言したモジュールの変数なので、モジュールのインスタンスごとに 1 つずつあります。