Skip to content

Commit

Permalink
wasm-encoder: Support extended constant expressions proposal (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePuzzlemaker authored Feb 1, 2024
1 parent 56d0d94 commit 8dc75f0
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions crates/wasm-encoder/src/core/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3188,6 +3188,11 @@ impl ConstExpr {
Self { bytes }
}

fn with_insn(mut self, insn: Instruction) -> Self {
insn.encode(&mut self.bytes);
self
}

/// Create a constant expression containing a single `global.get` instruction.
pub fn global_get(index: u32) -> Self {
Self::new_insn(Instruction::GlobalGet(index))
Expand Down Expand Up @@ -3227,6 +3232,76 @@ impl ConstExpr {
pub fn v128_const(value: i128) -> Self {
Self::new_insn(Instruction::V128Const(value))
}

/// Add a `global.get` instruction to this constant expression.
pub fn with_global_get(self, index: u32) -> Self {
self.with_insn(Instruction::GlobalGet(index))
}

/// Add a `ref.null` instruction to this constant expression.
pub fn with_ref_null(self, ty: HeapType) -> Self {
self.with_insn(Instruction::RefNull(ty))
}

/// Add a `ref.func` instruction to this constant expression.
pub fn with_ref_func(self, func: u32) -> Self {
self.with_insn(Instruction::RefFunc(func))
}

/// Add an `i32.const` instruction to this constant expression.
pub fn with_i32_const(self, value: i32) -> Self {
self.with_insn(Instruction::I32Const(value))
}

/// Add an `i64.const` instruction to this constant expression.
pub fn with_i64_const(self, value: i64) -> Self {
self.with_insn(Instruction::I64Const(value))
}

/// Add a `f32.const` instruction to this constant expression.
pub fn with_f32_const(self, value: f32) -> Self {
self.with_insn(Instruction::F32Const(value))
}

/// Add a `f64.const` instruction to this constant expression.
pub fn with_f64_const(self, value: f64) -> Self {
self.with_insn(Instruction::F64Const(value))
}

/// Add a `v128.const` instruction to this constant expression.
pub fn with_v128_const(self, value: i128) -> Self {
self.with_insn(Instruction::V128Const(value))
}

/// Add an `i32.add` instruction to this constant expression.
pub fn with_i32_add(self) -> Self {
self.with_insn(Instruction::I32Add)
}

/// Add an `i32.sub` instruction to this constant expression.
pub fn with_i32_sub(self) -> Self {
self.with_insn(Instruction::I32Sub)
}

/// Add an `i32.mul` instruction to this constant expression.
pub fn with_i32_mul(self) -> Self {
self.with_insn(Instruction::I32Mul)
}

/// Add an `i64.add` instruction to this constant expression.
pub fn with_i64_add(self) -> Self {
self.with_insn(Instruction::I64Add)
}

/// Add an `i64.sub` instruction to this constant expression.
pub fn with_i64_sub(self) -> Self {
self.with_insn(Instruction::I64Sub)
}

/// Add an `i64.mul` instruction to this constant expression.
pub fn with_i64_mul(self) -> Self {
self.with_insn(Instruction::I64Mul)
}
}

impl Encode for ConstExpr {
Expand Down

0 comments on commit 8dc75f0

Please sign in to comment.