plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
bcb9c6c
— Peter John
2026-11-10T21:13:58+05:30
Codegen: a trait downcast's tag mask ignores tags of 64 and more
compiler/wasm-codegen/src/traits.rs
CHANGED
|
@@ -388,6 +388,11 @@ impl AsTarget {
|
|
|
388
388
|
/// of a mask, else a compare per tag.
|
|
389
389
|
pub(crate) fn emitTest(tags: &[i32], reg: &GcTypeRegistry, body: &mut Vec<u8>, load: &mut dyn FnMut(&mut Vec<u8>)) {
|
|
390
390
|
let obj = reg.obj_idx.expect("a downcast's subject is a trait object");
|
|
391
|
+
// The range test below counts the tags, so each is counted once.
|
|
392
|
+
let mut tags = tags.to_vec();
|
|
393
|
+
tags.sort_unstable();
|
|
394
|
+
tags.dedup();
|
|
395
|
+
let tags = &tags[..];
|
|
391
396
|
let tag = |body: &mut Vec<u8>, load: &mut dyn FnMut(&mut Vec<u8>)| {
|
|
392
397
|
load(body);
|
|
393
398
|
Instruction::StructGet { struct_type_index: obj, field_index: 0 }.encode(body);
|
|
@@ -406,7 +411,12 @@ impl AsTarget {
|
|
|
406
411
|
return;
|
|
407
412
|
}
|
|
408
413
|
if tags.len() > 2 && tags.iter().all(|t| (0..64).contains(t)) {
|
|
414
|
+
// `i64.shr_u` takes its count modulo 64, so a subject whose own tag is 64 or more
|
|
415
|
+
// would read the bit of its tag minus 64: the tag is checked to be under 64 too.
|
|
409
416
|
let mask = tags.iter().fold(0u64, |m, t| m | (1u64 << t));
|
|
417
|
+
tag(body, load);
|
|
418
|
+
Instruction::I32Const(64).encode(body);
|
|
419
|
+
Instruction::I32LtU.encode(body);
|
|
410
420
|
Instruction::I64Const(mask as i64).encode(body);
|
|
411
421
|
tag(body, load);
|
|
412
422
|
Instruction::I64ExtendI32U.encode(body);
|
|
@@ -414,6 +424,7 @@ impl AsTarget {
|
|
|
414
424
|
Instruction::I32WrapI64.encode(body);
|
|
415
425
|
Instruction::I32Const(1).encode(body);
|
|
416
426
|
Instruction::I32And.encode(body);
|
|
427
|
+
Instruction::I32And.encode(body);
|
|
417
428
|
return;
|
|
418
429
|
}
|
|
419
430
|
for (i, t) in tags.iter().enumerate() {
|
compiler/wasm-codegen/tests/wasm_codegen/dispatch_test.rs
CHANGED
|
@@ -251,3 +251,42 @@ fun main() -> Int =
|
|
|
251
251
|
assert_eq!(news(©ing, "Ball.react"), 1, "--no-fbip copies");
|
|
252
252
|
assert_eq!(runInt(©ing), 500 * 100000 + 6000 + 302);
|
|
253
253
|
}
|
|
254
|
+
|
|
255
|
+
/// A downcast to a trait whose implementers' tags have gaps but are all under 64 tests a
|
|
256
|
+
/// bit of a 64-bit mask. `i64.shr_u` takes its count modulo 64, so without its own check a
|
|
257
|
+
/// subject tagged 65 read bit 1 and passed as a `Zz`, and `z.zz()` then had no method for it.
|
|
258
|
+
#[test]
|
|
259
|
+
fn trait_downcast_mask_ignores_tags_of_64_and_more() {
|
|
260
|
+
let mut s = String::from(
|
|
261
|
+
"trait Actor =\n fun key(self) -> Int\n\ntrait Mid =\n fun mid(self) -> Int = 1\n\ntrait N =\n fun nn(self) -> Int = 2\n\n\
|
|
262
|
+
trait Zz =\n fun zz(self) -> Int\n\ntrait Zzzz =\n fun z4(self) -> Int = 4\n\n",
|
|
263
|
+
);
|
|
264
|
+
// Tags follow the sorted trait lists: M0 0, Z1 1, N2 2, Z3 3, Z4 4, then P5.. 5 to 69.
|
|
265
|
+
let mut names = Vec::new();
|
|
266
|
+
let mut st = |name: String, traits: &str, k: usize, zz: bool| {
|
|
267
|
+
s += &format!("struct {name}[{traits}](id: Int) =\n fun key(self) -> Int = {k}\n");
|
|
268
|
+
if zz {
|
|
269
|
+
s += &format!(" fun zz(self) -> Int = {}\n", 100 + k);
|
|
270
|
+
}
|
|
271
|
+
s += "\n";
|
|
272
|
+
names.push(name);
|
|
273
|
+
};
|
|
274
|
+
st("M0".into(), "Actor, Mid", 0, false);
|
|
275
|
+
st("Z1".into(), "Actor, Mid, Zz", 1, true);
|
|
276
|
+
st("N2".into(), "Actor, N", 2, false);
|
|
277
|
+
st("Z3".into(), "Actor, Zz", 3, true);
|
|
278
|
+
st("Z4".into(), "Actor, Zz", 4, true);
|
|
279
|
+
for i in 5..70 {
|
|
280
|
+
st(format!("P{i}"), "Actor, Zzzz", i, false);
|
|
281
|
+
}
|
|
282
|
+
s += "fun isZ(a: Actor) -> Int = match a\n Zz as z => z.zz()\n _ => 0\n\n";
|
|
283
|
+
s += &format!(
|
|
284
|
+
"fun main() -> Int =\n xs: List<Actor> := [{}]\n total := xs.fold(0, |acc, a| acc + isZ(a))\n hits := xs.fold(0, |acc, a| if isZ(a) > 0 then acc + 1 else acc)\n total * 100 + hits\n",
|
|
285
|
+
names.iter().map(|n| format!("{n}(id: 0)")).collect::<Vec<_>>().join(", ")
|
|
286
|
+
);
|
|
287
|
+
let wasm = compile("dispatch_mask64", &format!("{PRE}{s}"), true);
|
|
288
|
+
let ops = operators(&wasm);
|
|
289
|
+
let o = ops.get("fn.isZ").unwrap_or_else(|| panic!("no fn isZ: {:?}", ops.keys().collect::<Vec<_>>()));
|
|
290
|
+
assert_eq!(count(o, "I64ShrU"), 1, "the mask test: {o:?}");
|
|
291
|
+
assert_eq!(runInt(&wasm), (101 + 103 + 104) * 100 + 3);
|
|
292
|
+
}
|