diff --git a/conformance/sv-tests/README.md b/conformance/sv-tests/README.md
index 1c96cdab5ea..b1cb1cde9e6 100644
--- a/conformance/sv-tests/README.md
+++ b/conformance/sv-tests/README.md
@@ -107,6 +107,13 @@ stays a passing negative case because `par_cls::b` outside the class is
rejected. `8.5--properties_enum` keeps failing because enumeration constants
declared in a class are not supported.
+Shallow copies update two more rows: both state modes for `8.12--shallow_copy`
+change from fail to pass. The case was simulated and its displayed values
+checked: the property of the copy keeps the value it was copied with after the
+method call changes the original. Full official runs at the pinned revision
+report 1081 passing cases in 2state and 1092 in 4state, with no passing case
+regressing.
+
## Running locally
Use the locked development shell and install the pinned [`requirements.txt`](requirements.txt) in a Python environment.
diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv
index bf5592271d7..080686dec97 100644
--- a/conformance/sv-tests/expected.tsv
+++ b/conformance/sv-tests/expected.tsv
@@ -1469,8 +1469,8 @@ chapter-8/8.11--this.sv 2state pass
chapter-8/8.11--this.sv 4state pass
chapter-8/8.12--assignment.sv 2state pass
chapter-8/8.12--assignment.sv 4state pass
-chapter-8/8.12--shallow_copy.sv 2state fail
-chapter-8/8.12--shallow_copy.sv 4state fail
+chapter-8/8.12--shallow_copy.sv 2state pass
+chapter-8/8.12--shallow_copy.sv 4state pass
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
diff --git a/crates/celox-frontend-sv/src/lowering/ff/objects.rs b/crates/celox-frontend-sv/src/lowering/ff/objects.rs
index 9f2026bb1c5..4b83614aa47 100644
--- a/crates/celox-frontend-sv/src/lowering/ff/objects.rs
+++ b/crates/celox-frontend-sv/src/lowering/ff/objects.rs
@@ -115,13 +115,17 @@ impl Ff<'_, '_> {
let object = self.handle(&args[0])?;
self.heap_call(ops::OBJECT_CLASS, &[object])
}
+ ObjectOp::Copy => {
+ let object = self.handle(&args[0])?;
+ self.heap_call(ops::OBJECT_COPY, &[object])
+ }
};
let (width, signed) = op.result_type();
let value = self.b.alloc_bit(width, signed);
self.b.emit(SIRInstruction::Slice(value, result, 0, width));
let (temp, name) = self.m.temp("object", width, signed, false);
let kind = match op {
- ObjectOp::New { .. } => Some(celox_design::ValueKind::ObjRef),
+ ObjectOp::New { .. } | ObjectOp::Copy => Some(celox_design::ValueKind::ObjRef),
ObjectOp::Load(field) => field.handle.map(|handle| match handle {
HandleProperty::String => celox_design::ValueKind::StrRef,
HandleProperty::Object => celox_design::ValueKind::ObjRef,
diff --git a/crates/celox-heap/src/objects.rs b/crates/celox-heap/src/objects.rs
index bace65434d6..2683ca747b9 100644
--- a/crates/celox-heap/src/objects.rs
+++ b/crates/celox-heap/src/objects.rs
@@ -196,6 +196,18 @@ impl Heap {
true
}
+ /// A new object of the class of `object` with a copy of its properties:
+ /// the handles it holds are copied, not the objects they refer to (IEEE
+ /// 1800-2023 8.12). Copying `null` is a fault and gives `null`.
+ pub fn copy_object(&mut self, object: Handle) -> Handle {
+ let Some(copy) = self.object_mut(object).map(|object| object.clone()) else {
+ return NULL;
+ };
+ let handle = self.new_object(copy.class, 0);
+ self.objects.insert(handle, copy);
+ handle
+ }
+
/// Declare that the property of `object` at byte `offset` holds a handle
/// of `kind`.
pub fn declare_handle(&mut self, object: Handle, offset: u32, kind: HandleKind) {
@@ -297,6 +309,28 @@ mod tests {
assert_eq!(heap.class_of(NULL), 0);
}
+ #[test]
+ fn copies_are_shallow() {
+ let mut heap = Heap::new();
+ let inner = heap.new_object(2, 4);
+ let original = heap.new_object(1, 8);
+ heap.declare_handle(original, 0, HandleKind::Object);
+ heap.store(original, 0, 4, inner.into());
+ heap.store(original, 4, 4, 9);
+ let copy = heap.copy_object(original);
+ assert_ne!(copy, original);
+ assert_eq!(heap.class_of(copy), 1);
+ assert_eq!(heap.load(copy, 0, 4), u64::from(inner));
+ heap.store(copy, 4, 4, 5);
+ assert_eq!(heap.load(original, 4, 4), 9);
+ // The copy keeps what its handle properties refer to alive.
+ heap.collect([Root::object(copy)]);
+ assert!(heap.contains_object(inner));
+ assert!(!heap.contains_object(original));
+ assert_eq!(heap.copy_object(NULL), NULL);
+ assert_eq!(heap.take_fault(), Some(Fault::NullDereference));
+ }
+
#[test]
fn collection_follows_handle_properties() {
let mut heap = Heap::new();
diff --git a/crates/celox-heap/src/ops.rs b/crates/celox-heap/src/ops.rs
index d0485579576..1b725feabc3 100644
--- a/crates/celox-heap/src/ops.rs
+++ b/crates/celox-heap/src/ops.rs
@@ -81,6 +81,8 @@ pub const OBJECT_MODE_SELECT: u64 = 60;
pub const OBJECT_MODE_SELECTED: u64 = 61;
/// Draw a randc domain index; parameter is the property offset, b its domain size.
pub const OBJECT_RANDOM_CYCLIC: u64 = 62;
+/// `copy(object)`: a new object with a copy of the properties of `object`.
+pub const OBJECT_COPY: u64 = 63;
/// The opcode word of `op` with parameter `parameter`.
pub const fn encode(op: u64, parameter: u64) -> u64 {
@@ -222,6 +224,7 @@ impl Heap {
OBJECT_MODE_SELECTED => {
u64::from(self.selected_random_mode(a as Handle, parameter as u32))
}
+ OBJECT_COPY => self.copy_object(a as Handle).into(),
other => panic!("unknown heap operation {other}"),
}
}
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 1d4cfbf810c..3cd80786a50 100644
--- a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs
+++ b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs
@@ -233,6 +233,35 @@ pub(in crate::ast) fn static_property(
Ok(Some(property.variable.clone()))
}
+/// The class of the handle that the static property native expression `id`
+/// names holds, `h.p` or `C::p`.
+fn static_property_class(
+ tree: &AnalysisContext<'_>,
+ id: ExprId,
+ native: &Declarations,
+ dimensions: &PackedDimensions,
+) -> Converted