Skip to content

Commit

Permalink
[wasm][ast] Support ref.cast instruction (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored Dec 16, 2024
1 parent 6c826d0 commit 38b2cf5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/samlang-ast/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub enum InlineInstruction {
pointer_type: String,
value: Box<InlineInstruction>,
},
Cast {
pointer_type: String,
value: Box<InlineInstruction>,
},
Binary(Box<InlineInstruction>, hir::BinaryOperator, Box<InlineInstruction>),
Load {
index: usize,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 38b2cf5

Please sign in to comment.