From f1be72f9d393593a00eb508a3e8365853d7e372d Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Sat, 14 Dec 2024 13:49:58 -0500 Subject: [PATCH] [wasm][ast] Support `ref.cast` instruction --- crates/samlang-ast/src/wasm.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/samlang-ast/src/wasm.rs b/crates/samlang-ast/src/wasm.rs index 25c55f25..1d213c11 100644 --- a/crates/samlang-ast/src/wasm.rs +++ b/crates/samlang-ast/src/wasm.rs @@ -10,6 +10,10 @@ pub enum InlineInstruction { pointer_type: String, value: Box, }, + Cast { + pointer_type: String, + value: Box, + }, Binary(Box, hir::BinaryOperator, Box), Load { index: usize, @@ -60,6 +64,13 @@ impl InlineInstruction { value.pretty_print(collector, heap, table); collector.push(')'); } + InlineInstruction::Cast { pointer_type, value } => { + collector.push_str("(ref.cast $"); + collector.push_str(pointer_type); + collector.push(' '); + value.pretty_print(collector, heap, table); + collector.push(')'); + } InlineInstruction::Binary(v1, op, v2) => { let op_s = match op { hir::BinaryOperator::MUL => "mul", @@ -377,6 +388,10 @@ mod tests { pointer_type: "Foo".to_string(), value: Box::new(InlineInstruction::Const(0)), }), + Instruction::Inline(InlineInstruction::Cast { + pointer_type: "Foo".to_string(), + value: Box::new(InlineInstruction::Const(0)), + }), ], s2: vec![ Instruction::Inline(InlineInstruction::Binary( @@ -520,6 +535,7 @@ mod tests { (local.get $a) (local.set $b (i32.const 0)) (ref.test $Foo (i32.const 0)) + (ref.cast $Foo (i32.const 0)) ) (else (i32.add (i32.const 0) (i32.const 0)) (i32.sub (i32.const 0) (i32.const 0))