Skip to content

Commit 522a143

Browse files
committed
add basic instruction encoder
1 parent e34f636 commit 522a143

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use super::{Instr, Reset};
2+
use crate::ir::Instruction;
3+
use alloc::vec::Vec;
4+
5+
/// Creates and encodes the list of [`Instruction`]s for a function.
6+
#[derive(Debug, Default)]
7+
pub struct InstrEncoder {
8+
/// The list of constructed instructions and their parameters.
9+
instrs: Vec<Instruction>,
10+
}
11+
12+
impl Reset for InstrEncoder {
13+
fn reset(&mut self) {
14+
self.instrs.clear();
15+
}
16+
}
17+
18+
impl InstrEncoder {
19+
/// Returns the next [`Instr`].
20+
#[must_use]
21+
fn next_instr(&self) -> Instr {
22+
Instr::from_usize(self.instrs.len())
23+
}
24+
25+
/// Pushes an [`Instruction`] to the [`InstrEncoder`].
26+
///
27+
/// Returns an [`Instr`] that refers to the pushed [`Instruction`].
28+
#[must_use]
29+
pub fn push_instr(&mut self, instruction: Instruction) -> Instr {
30+
let instr = self.next_instr();
31+
self.instrs.push(instruction);
32+
instr
33+
}
34+
35+
/// Pushes an [`Instruction`] parameter to the [`InstrEncoder`].
36+
///
37+
/// The parameter is associated to the last pushed [`Instruction`].
38+
pub fn push_param(&mut self, instruction: Instruction) {
39+
self.instrs.push(instruction);
40+
}
41+
42+
/// Returns a shared reference to the [`Instruction`] associated to [`Instr`].
43+
///
44+
/// # Panics
45+
///
46+
/// If `instr` is out of bounds for `self`.
47+
pub fn get(&self, instr: Instr) -> &Instruction {
48+
&self.instrs[instr.into_usize()]
49+
}
50+
}

crates/wasmi/src/engine/translator/translator2/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![expect(dead_code, unused_imports, unused_variables)]
22

3+
mod instrs;
34
mod layout;
45
#[cfg(feature = "simd")]
56
mod simd;
@@ -8,14 +9,15 @@ mod utils;
89
mod visit;
910

1011
use self::{
12+
instrs::InstrEncoder,
1113
layout::{StackLayout, StackSpace},
1214
stack::{LocalIdx, Operand, OperandIdx, Stack},
1315
utils::Reset,
1416
};
1517
use crate::{
1618
core::FuelCostsProvider,
1719
engine::{
18-
translator::{LabelRegistry, WasmTranslator},
20+
translator::{Instr, LabelRegistry, WasmTranslator},
1921
CompiledFuncEntity,
2022
},
2123
module::{FuncIdx, ModuleHeader, WasmiValueType},
@@ -59,6 +61,8 @@ pub struct FuncTranslator {
5961
layout: StackLayout,
6062
/// Registers and pins labels and tracks their users.
6163
labels: LabelRegistry,
64+
/// Constructs and encodes function instructions.
65+
instrs: InstrEncoder,
6266
}
6367

6468
/// Heap allocated data structured used by the [`FuncTranslator`].
@@ -70,6 +74,8 @@ pub struct FuncTranslatorAllocations {
7074
layout: StackLayout,
7175
/// Registers and pins labels and tracks their users.
7276
labels: LabelRegistry,
77+
/// Constructs and encodes function instructions.
78+
instrs: InstrEncoder,
7379
}
7480

7581
impl WasmTranslator<'_> for FuncTranslator {
@@ -141,6 +147,7 @@ impl FuncTranslator {
141147
stack,
142148
layout,
143149
labels,
150+
instrs,
144151
})
145152
}
146153

@@ -150,6 +157,7 @@ impl FuncTranslator {
150157
stack: self.stack,
151158
layout: self.layout,
152159
labels: self.labels,
160+
instrs: self.instrs,
153161
}
154162
}
155163

0 commit comments

Comments
 (0)