Skip to content

Commit 22b34d7

Browse files
committed
Refactor to prefer InstructionSink
1 parent 9076cae commit 22b34d7

File tree

17 files changed

+709
-748
lines changed

17 files changed

+709
-748
lines changed

crates/wasm-encoder/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ And then you can encode WebAssembly binaries via:
2626

2727
```rust
2828
use wasm_encoder::{
29-
CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction,
29+
CodeSection, ExportKind, ExportSection, Function, FunctionSection, LocalIdx,
3030
Module, TypeSection, ValType,
3131
};
3232

@@ -54,10 +54,11 @@ module.section(&exports);
5454
let mut codes = CodeSection::new();
5555
let locals = vec![];
5656
let mut f = Function::new(locals);
57-
f.instruction(&Instruction::LocalGet(0));
58-
f.instruction(&Instruction::LocalGet(1));
59-
f.instruction(&Instruction::I32Add);
60-
f.instruction(&Instruction::End);
57+
f.instructions()
58+
.local_get(LocalIdx(0))
59+
.local_get(LocalIdx(1))
60+
.i32_add()
61+
.end();
6162
codes.function(&f);
6263
module.section(&codes);
6364

crates/wasm-encoder/src/core/branch_hints.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ use alloc::vec::Vec;
2525
/// let mut code = CodeSection::new();
2626
/// let mut body = Function::new([]);
2727
///
28-
/// body.instruction(&Instruction::I32Const(1));
28+
/// body.instructions().i32_const(1);
2929
/// let if_offset = body.byte_len();
30-
/// body.instruction(&Instruction::If(BlockType::Empty));
31-
/// body.instruction(&Instruction::End);
32-
/// body.instruction(&Instruction::End);
30+
/// body.instructions()
31+
/// .if_(BlockType::Empty)
32+
/// .end()
33+
/// .end();
3334
/// code.function(&body);
3435
///
3536
/// let mut hints = BranchHints::new();

crates/wasm-encoder/src/core/code.rs

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use alloc::vec::Vec;
1313
///
1414
/// ```
1515
/// use wasm_encoder::{
16-
/// CodeSection, Function, FunctionSection, Instruction, Module,
16+
/// CodeSection, Function, FunctionSection, Module,
1717
/// TypeSection, ValType
1818
/// };
1919
///
@@ -26,7 +26,7 @@ use alloc::vec::Vec;
2626
///
2727
/// let locals = vec![];
2828
/// let mut func = Function::new(locals);
29-
/// func.instruction(&Instruction::I32Const(42));
29+
/// func.instructions().i32_const(42);
3030
/// let mut code = CodeSection::new();
3131
/// code.function(&func);
3232
///
@@ -126,7 +126,7 @@ impl Section for CodeSection {
126126
/// # Example
127127
///
128128
/// ```
129-
/// use wasm_encoder::{CodeSection, Function, Instruction};
129+
/// use wasm_encoder::{CodeSection, Function, LocalIdx};
130130
///
131131
/// // Define the function body for:
132132
/// //
@@ -136,9 +136,10 @@ impl Section for CodeSection {
136136
/// // i32.add)
137137
/// let locals = vec![];
138138
/// let mut func = Function::new(locals);
139-
/// func.instruction(&Instruction::LocalGet(0));
140-
/// func.instruction(&Instruction::LocalGet(1));
141-
/// func.instruction(&Instruction::I32Add);
139+
/// func.instructions()
140+
/// .local_get(LocalIdx(0))
141+
/// .local_get(LocalIdx(1))
142+
/// .i32_add();
142143
///
143144
/// // Add our function to the code section.
144145
/// let mut code = CodeSection::new();
@@ -271,9 +272,9 @@ impl Function {
271272
/// For example:
272273
///
273274
/// ```
274-
/// # use wasm_encoder::{CodeSection, Function, Instruction};
275+
/// # use wasm_encoder::{CodeSection, Function};
275276
/// let mut f = Function::new([]);
276-
/// f.instruction(&Instruction::End);
277+
/// f.instructions().end();
277278
/// let bytes = f.into_raw_body();
278279
/// // (save `bytes` somewhere for later use)
279280
/// let mut code = CodeSection::new();
@@ -2320,125 +2321,169 @@ impl ConstExpr {
23202321
Self { bytes }
23212322
}
23222323

2323-
fn new_insn(insn: Instruction) -> Self {
2324+
fn new(f: impl FnOnce(&mut InstructionSink)) -> Self {
23242325
let mut bytes = vec![];
2325-
insn.encode(&mut bytes);
2326+
f(&mut InstructionSink::new(&mut bytes));
23262327
Self { bytes }
23272328
}
23282329

2329-
fn with_insn(mut self, insn: Instruction) -> Self {
2330-
insn.encode(&mut self.bytes);
2330+
fn with(mut self, f: impl FnOnce(&mut InstructionSink)) -> Self {
2331+
f(&mut InstructionSink::new(&mut self.bytes));
23312332
self
23322333
}
23332334

23342335
/// Create a constant expression containing a single `global.get` instruction.
23352336
pub fn global_get(index: u32) -> Self {
2336-
Self::new_insn(Instruction::GlobalGet(index))
2337+
Self::new(|insn| {
2338+
insn.global_get(GlobalIdx(index));
2339+
})
23372340
}
23382341

23392342
/// Create a constant expression containing a single `ref.null` instruction.
23402343
pub fn ref_null(ty: HeapType) -> Self {
2341-
Self::new_insn(Instruction::RefNull(ty))
2344+
Self::new(|insn| {
2345+
insn.ref_null(ty);
2346+
})
23422347
}
23432348

23442349
/// Create a constant expression containing a single `ref.func` instruction.
23452350
pub fn ref_func(func: u32) -> Self {
2346-
Self::new_insn(Instruction::RefFunc(func))
2351+
Self::new(|insn| {
2352+
insn.ref_func(FuncIdx(func));
2353+
})
23472354
}
23482355

23492356
/// Create a constant expression containing a single `i32.const` instruction.
23502357
pub fn i32_const(value: i32) -> Self {
2351-
Self::new_insn(Instruction::I32Const(value))
2358+
Self::new(|insn| {
2359+
insn.i32_const(value);
2360+
})
23522361
}
23532362

23542363
/// Create a constant expression containing a single `i64.const` instruction.
23552364
pub fn i64_const(value: i64) -> Self {
2356-
Self::new_insn(Instruction::I64Const(value))
2365+
Self::new(|insn| {
2366+
insn.i64_const(value);
2367+
})
23572368
}
23582369

23592370
/// Create a constant expression containing a single `f32.const` instruction.
23602371
pub fn f32_const(value: f32) -> Self {
2361-
Self::new_insn(Instruction::F32Const(value))
2372+
Self::new(|insn| {
2373+
insn.f32_const(value);
2374+
})
23622375
}
23632376

23642377
/// Create a constant expression containing a single `f64.const` instruction.
23652378
pub fn f64_const(value: f64) -> Self {
2366-
Self::new_insn(Instruction::F64Const(value))
2379+
Self::new(|insn| {
2380+
insn.f64_const(value);
2381+
})
23672382
}
23682383

23692384
/// Create a constant expression containing a single `v128.const` instruction.
23702385
pub fn v128_const(value: i128) -> Self {
2371-
Self::new_insn(Instruction::V128Const(value))
2386+
Self::new(|insn| {
2387+
insn.v128_const(value);
2388+
})
23722389
}
23732390

23742391
/// Add a `global.get` instruction to this constant expression.
23752392
pub fn with_global_get(self, index: u32) -> Self {
2376-
self.with_insn(Instruction::GlobalGet(index))
2393+
self.with(|insn| {
2394+
insn.global_get(GlobalIdx(index));
2395+
})
23772396
}
23782397

23792398
/// Add a `ref.null` instruction to this constant expression.
23802399
pub fn with_ref_null(self, ty: HeapType) -> Self {
2381-
self.with_insn(Instruction::RefNull(ty))
2400+
self.with(|insn| {
2401+
insn.ref_null(ty);
2402+
})
23822403
}
23832404

23842405
/// Add a `ref.func` instruction to this constant expression.
23852406
pub fn with_ref_func(self, func: u32) -> Self {
2386-
self.with_insn(Instruction::RefFunc(func))
2407+
self.with(|insn| {
2408+
insn.ref_func(FuncIdx(func));
2409+
})
23872410
}
23882411

23892412
/// Add an `i32.const` instruction to this constant expression.
23902413
pub fn with_i32_const(self, value: i32) -> Self {
2391-
self.with_insn(Instruction::I32Const(value))
2414+
self.with(|insn| {
2415+
insn.i32_const(value);
2416+
})
23922417
}
23932418

23942419
/// Add an `i64.const` instruction to this constant expression.
23952420
pub fn with_i64_const(self, value: i64) -> Self {
2396-
self.with_insn(Instruction::I64Const(value))
2421+
self.with(|insn| {
2422+
insn.i64_const(value);
2423+
})
23972424
}
23982425

23992426
/// Add a `f32.const` instruction to this constant expression.
24002427
pub fn with_f32_const(self, value: f32) -> Self {
2401-
self.with_insn(Instruction::F32Const(value))
2428+
self.with(|insn| {
2429+
insn.f32_const(value);
2430+
})
24022431
}
24032432

24042433
/// Add a `f64.const` instruction to this constant expression.
24052434
pub fn with_f64_const(self, value: f64) -> Self {
2406-
self.with_insn(Instruction::F64Const(value))
2435+
self.with(|insn| {
2436+
insn.f64_const(value);
2437+
})
24072438
}
24082439

24092440
/// Add a `v128.const` instruction to this constant expression.
24102441
pub fn with_v128_const(self, value: i128) -> Self {
2411-
self.with_insn(Instruction::V128Const(value))
2442+
self.with(|insn| {
2443+
insn.v128_const(value);
2444+
})
24122445
}
24132446

24142447
/// Add an `i32.add` instruction to this constant expression.
24152448
pub fn with_i32_add(self) -> Self {
2416-
self.with_insn(Instruction::I32Add)
2449+
self.with(|insn| {
2450+
insn.i32_add();
2451+
})
24172452
}
24182453

24192454
/// Add an `i32.sub` instruction to this constant expression.
24202455
pub fn with_i32_sub(self) -> Self {
2421-
self.with_insn(Instruction::I32Sub)
2456+
self.with(|insn| {
2457+
insn.i32_sub();
2458+
})
24222459
}
24232460

24242461
/// Add an `i32.mul` instruction to this constant expression.
24252462
pub fn with_i32_mul(self) -> Self {
2426-
self.with_insn(Instruction::I32Mul)
2463+
self.with(|insn| {
2464+
insn.i32_mul();
2465+
})
24272466
}
24282467

24292468
/// Add an `i64.add` instruction to this constant expression.
24302469
pub fn with_i64_add(self) -> Self {
2431-
self.with_insn(Instruction::I64Add)
2470+
self.with(|insn| {
2471+
insn.i64_add();
2472+
})
24322473
}
24332474

24342475
/// Add an `i64.sub` instruction to this constant expression.
24352476
pub fn with_i64_sub(self) -> Self {
2436-
self.with_insn(Instruction::I64Sub)
2477+
self.with(|insn| {
2478+
insn.i64_sub();
2479+
})
24372480
}
24382481

24392482
/// Add an `i64.mul` instruction to this constant expression.
24402483
pub fn with_i64_mul(self) -> Self {
2441-
self.with_insn(Instruction::I64Mul)
2484+
self.with(|insn| {
2485+
insn.i64_mul();
2486+
})
24422487
}
24432488

24442489
/// Returns the function, if any, referenced by this global.
@@ -2456,7 +2501,7 @@ impl ConstExpr {
24562501
impl Encode for ConstExpr {
24572502
fn encode(&self, sink: &mut Vec<u8>) {
24582503
sink.extend(&self.bytes);
2459-
Instruction::End.encode(sink);
2504+
InstructionSink::new(sink).end();
24602505
}
24612506
}
24622507

crates/wasm-encoder/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//!
2727
//! ```
2828
//! use wasm_encoder::{
29-
//! CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction,
29+
//! CodeSection, ExportKind, ExportSection, Function, FunctionSection, LocalIdx,
3030
//! Module, TypeSection, ValType,
3131
//! };
3232
//!
@@ -54,10 +54,11 @@
5454
//! let mut codes = CodeSection::new();
5555
//! let locals = vec![];
5656
//! let mut f = Function::new(locals);
57-
//! f.instruction(&Instruction::LocalGet(0));
58-
//! f.instruction(&Instruction::LocalGet(1));
59-
//! f.instruction(&Instruction::I32Add);
60-
//! f.instruction(&Instruction::End);
57+
//! f.instructions()
58+
//! .local_get(LocalIdx(0))
59+
//! .local_get(LocalIdx(1))
60+
//! .i32_add()
61+
//! .end();
6162
//! codes.function(&f);
6263
//! module.section(&codes);
6364
//!

crates/wasm-mutate/src/mutators/add_function.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::Mutator;
44
use crate::module::{PrimitiveTypeInfo, TypeInfo};
55
use crate::{Result, WasmMutate};
66
use rand::Rng;
7-
use wasm_encoder::{AbstractHeapType, HeapType, Instruction, Module};
7+
use wasm_encoder::{AbstractHeapType, HeapType, Module};
88

99
/// Mutator that adds new, empty functions to a Wasm module.
1010
#[derive(Clone, Copy)]
@@ -47,36 +47,36 @@ impl Mutator for AddFunctionMutator {
4747
for ty in &func_ty.returns {
4848
match ty {
4949
PrimitiveTypeInfo::I32 => {
50-
func.instruction(&Instruction::I32Const(0));
50+
func.instructions().i32_const(0);
5151
}
5252
PrimitiveTypeInfo::I64 => {
53-
func.instruction(&Instruction::I64Const(0));
53+
func.instructions().i64_const(0);
5454
}
5555
PrimitiveTypeInfo::F32 => {
56-
func.instruction(&Instruction::F32Const(0.0));
56+
func.instructions().f32_const(0.0);
5757
}
5858
PrimitiveTypeInfo::F64 => {
59-
func.instruction(&Instruction::F64Const(0.0));
59+
func.instructions().f64_const(0.0);
6060
}
6161
PrimitiveTypeInfo::V128 => {
62-
func.instruction(&Instruction::V128Const(0));
62+
func.instructions().v128_const(0);
6363
}
6464
PrimitiveTypeInfo::FuncRef => {
65-
func.instruction(&Instruction::RefNull(HeapType::Abstract {
65+
func.instructions().ref_null(HeapType::Abstract {
6666
shared: false,
6767
ty: AbstractHeapType::Func,
68-
}));
68+
});
6969
}
7070
PrimitiveTypeInfo::ExternRef => {
71-
func.instruction(&Instruction::RefNull(HeapType::Abstract {
71+
func.instructions().ref_null(HeapType::Abstract {
7272
shared: false,
7373
ty: AbstractHeapType::Extern,
74-
}));
74+
});
7575
}
7676
PrimitiveTypeInfo::Empty => unreachable!(),
7777
}
7878
}
79-
func.instruction(&Instruction::End);
79+
func.instructions().end();
8080
code_sec_enc.function(&func);
8181

8282
let module = if config.info().functions.is_some() {

0 commit comments

Comments
 (0)