Skip to content

Commit

Permalink
upgrade wasmparser & wasm-encoder to v0.211.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Jun 25, 2024
1 parent 867641d commit b30aa65
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 297 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ leb128 = "0.2.4"
log = "0.4.8"
rayon = { version = "1.1.0", optional = true }
walrus-macro = { path = './crates/macro', version = '=0.19.0' }
wasm-encoder = "0.41.0"
wasmparser = "0.80.2"
wasm-encoder = "0.211.0"
wasmparser = "0.211.0"
gimli = "0.26.0"

[features]
Expand Down
9 changes: 5 additions & 4 deletions src/init_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum InitExpr {
}

impl InitExpr {
pub(crate) fn eval(init: &wasmparser::InitExpr, ids: &IndicesToIds) -> Result<InitExpr> {
pub(crate) fn eval(init: &wasmparser::ConstExpr, ids: &IndicesToIds) -> Result<InitExpr> {
use wasmparser::Operator::*;
let mut reader = init.get_operators_reader();
let val = match reader.read()? {
Expand All @@ -32,7 +32,8 @@ impl InitExpr {
F64Const { value } => InitExpr::Value(Value::F64(f64::from_bits(value.bits()))),
V128Const { value } => InitExpr::Value(Value::V128(v128_to_u128(&value))),
GlobalGet { global_index } => InitExpr::Global(ids.get_global(global_index)?),
RefNull { ty } => InitExpr::RefNull(ValType::parse(&ty)?),
// TODO: handle RefNull
// RefNull { hty } => InitExpr::RefNull(ValType::parse(&hty)?),
RefFunc { function_index } => InitExpr::RefFunc(ids.get_func(function_index)?),
_ => bail!("invalid constant expression"),
};
Expand All @@ -57,8 +58,8 @@ impl InitExpr {
wasm_encoder::ConstExpr::global_get(cx.indices.get_global_index(*g))
}
InitExpr::RefNull(ty) => wasm_encoder::ConstExpr::ref_null(match ty {
ValType::Externref => wasm_encoder::HeapType::Extern,
ValType::Funcref => wasm_encoder::HeapType::Func,
ValType::Externref => wasm_encoder::HeapType::Abstract { shared: false, ty: wasm_encoder::AbstractHeapType::Extern },
ValType::Funcref => wasm_encoder::HeapType::Abstract { shared: false, ty: wasm_encoder::AbstractHeapType::Func },
_ => unreachable!(),
}),
InitExpr::RefFunc(f) => {
Expand Down
4 changes: 2 additions & 2 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,8 @@ pub enum BinaryOp {
I8x16NarrowI16x8U,
I16x8NarrowI32x4S,
I16x8NarrowI32x4U,
I8x16RoundingAverageU,
I16x8RoundingAverageU,
I8x16AvgrU,
I16x8AvgrU,

I8x16MinS,
I8x16MinU,
Expand Down
4 changes: 2 additions & 2 deletions src/module/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ impl Module {
}
wasmparser::DataKind::Active {
memory_index,
init_expr,
offset_expr,
} => {
data.value = segment.data.to_vec();

let memory_id = ids.get_memory(memory_index)?;
let memory = self.memories.get_mut(memory_id);
memory.data_segments.insert(data.id);

let offset = InitExpr::eval(&init_expr, ids)
let offset = InitExpr::eval(&offset_expr, ids)
.with_context(|| format!("in segment {}", i))?;
data.kind = DataKind::Active(ActiveData {
memory: memory_id,
Expand Down
16 changes: 8 additions & 8 deletions src/module/debug/expression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{CodeTransform, Function, InstrLocId, ModuleFunctions};
use id_arena::Id;
use std::cmp::Ordering;
use std::{cmp::Ordering, ops::Range};

use super::dwarf::AddressSearchPreference;

Expand All @@ -22,7 +22,7 @@ pub(crate) enum CodeAddress {
/// Converts original code address to CodeAddress
pub(crate) struct CodeAddressGenerator {
/// Function range based convert table
address_convert_table: Vec<(wasmparser::Range, Id<Function>)>,
address_convert_table: Vec<(Range<usize>, Id<Function>)>,
/// Instrument based convert table
instrument_address_convert_table: Vec<(usize, InstrLocId)>,
}
Expand All @@ -31,7 +31,7 @@ impl CodeAddressGenerator {
pub(crate) fn new(funcs: &ModuleFunctions) -> Self {
let mut address_convert_table = funcs
.iter_local()
.filter_map(|(func_id, func)| func.original_range.map(|range| (range, func_id)))
.filter_map(|(func_id, func)| func.original_range.clone().map(|range| (range, func_id)))
.collect::<Vec<_>>();

let mut instrument_address_convert_table = funcs
Expand Down Expand Up @@ -75,7 +75,7 @@ impl CodeAddressGenerator {
};

// If the address is not mapped to any instruction, falling back to function-range-based comparison.
let inclusive_range_comparor = |range: &(wasmparser::Range, Id<Function>)| {
let inclusive_range_comparor = |range: &(Range<usize>, Id<Function>)| {
// range.start < address <= range.end
if range.0.end < address {
Ordering::Less
Expand All @@ -85,7 +85,7 @@ impl CodeAddressGenerator {
Ordering::Equal
}
};
let exclusive_range_comparor = |range: &(wasmparser::Range, Id<Function>)| {
let exclusive_range_comparor = |range: &(Range<usize>, Id<Function>)| {
// normal comparison: range.start <= address < range.end
if range.0.end <= address {
Ordering::Less
Expand Down Expand Up @@ -189,7 +189,7 @@ mod tests {
crate::FunctionBuilder::new(&mut module.types, &[], &[]),
);

func1.original_range = Some(wasmparser::Range { start: 20, end: 30 });
func1.original_range = Some(Range { start: 20, end: 30 });

let id1 = module.funcs.add_local(func1);

Expand All @@ -198,7 +198,7 @@ mod tests {
crate::FunctionBuilder::new(&mut module.types, &[], &[]),
);

func2.original_range = Some(wasmparser::Range { start: 30, end: 50 });
func2.original_range = Some(Range { start: 30, end: 50 });

let id2 = module.funcs.add_local(func2);

Expand Down Expand Up @@ -262,7 +262,7 @@ mod tests {
{
code_transform
.function_ranges
.push((id1, wasmparser::Range { start: 50, end: 80 }));
.push((id1, Range { start: 50, end: 80 }));
code_transform.instruction_map.push((instr_id1, 60));
code_transform.instruction_map.push((instr_id2, 65));
}
Expand Down
48 changes: 26 additions & 22 deletions src/module/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,35 @@ impl Module {
) -> Result<()> {
log::debug!("parse element section");
for (i, segment) in section.into_iter().enumerate() {
let segment = segment?;
let ty = ValType::parse(&segment.ty)?;
match ty {
ValType::Funcref => {}
_ => bail!("only funcref type allowed in element segments"),
}
let members = segment
.items
.get_items_reader()?
.into_iter()
.map(|e| -> Result<_> {
Ok(match e? {
wasmparser::ElementItem::Func(f) => Some(ids.get_func(f)?),
wasmparser::ElementItem::Null(_) => None,
let element = segment?;

let members = match element.items {
wasmparser::ElementItems::Functions(f) => f
.into_iter()
.map(|f| -> Result<_> {
match ids.get_func(f?) {
Ok(f) => Ok(Some(f)),
Err(_) => Ok(None),
}
})
})
.collect::<Result<_>>()?;
.collect::<Result<_>>()?,
wasmparser::ElementItems::Expressions(_, _) => todo!(),
};

let id = self.elements.arena.next_id();

let kind = match segment.kind {
let kind = match element.kind {
wasmparser::ElementKind::Passive => ElementKind::Passive,
wasmparser::ElementKind::Declared => ElementKind::Declared,
wasmparser::ElementKind::Active {
table_index,
init_expr,
offset_expr,
} => {
let table = ids.get_table(table_index)?;
// TODO: check if this is correct
let table = ids.get_table(table_index.unwrap_or_default())?;
self.tables.get_mut(table).elem_segments.insert(id);

let offset = InitExpr::eval(&init_expr, ids)
let offset = InitExpr::eval(&offset_expr, ids)
.with_context(|| format!("in segment {}", i))?;
match offset {
InitExpr::Value(Value::I32(_)) => {}
Expand All @@ -152,7 +151,7 @@ impl Module {
};
self.elements.arena.alloc(Element {
id,
ty,
ty: ValType::Funcref,
kind,
members,
name: None,
Expand Down Expand Up @@ -182,7 +181,12 @@ impl Emit for ModuleElements {
Some(func) => {
wasm_encoder::ConstExpr::ref_func(cx.indices.get_func_index(*func))
}
None => wasm_encoder::ConstExpr::ref_null(wasm_encoder::HeapType::Func),
None => {
wasm_encoder::ConstExpr::ref_null(wasm_encoder::HeapType::Abstract {
shared: false,
ty: wasm_encoder::AbstractHeapType::Func,
})
}
})
.collect();
let els = wasm_encoder::Elements::Expressions(
Expand Down
7 changes: 2 additions & 5 deletions src/module/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,17 @@ impl Module {
for entry in section {
let entry = entry?;
let item = match entry.kind {
Function => ExportItem::Function(ids.get_func(entry.index)?),
Func => ExportItem::Function(ids.get_func(entry.index)?),
Table => ExportItem::Table(ids.get_table(entry.index)?),
Memory => ExportItem::Memory(ids.get_memory(entry.index)?),
Global => ExportItem::Global(ids.get_global(entry.index)?),
Type | Module | Instance => {
unimplemented!("module linking not supported");
}
Tag => {
unimplemented!("exception handling not supported");
}
};
self.exports.arena.alloc_with_id(|id| Export {
id,
name: entry.field.to_string(),
name: entry.name.to_string(),
item,
});
}
Expand Down
14 changes: 10 additions & 4 deletions src/module/functions/local_function/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl<'instr> Visitor<'instr> for Emit<'_> {
I8x16MinU => Instruction::I8x16MinU,
I8x16MaxS => Instruction::I8x16MaxS,
I8x16MaxU => Instruction::I8x16MaxU,
I8x16RoundingAverageU => Instruction::I8x16AvgrU,
I8x16AvgrU => Instruction::I8x16AvgrU,

I16x8NarrowI32x4S => Instruction::I16x8NarrowI32x4S,
I16x8NarrowI32x4U => Instruction::I16x8NarrowI32x4U,
Expand All @@ -374,7 +374,7 @@ impl<'instr> Visitor<'instr> for Emit<'_> {
I16x8MinU => Instruction::I16x8MinU,
I16x8MaxS => Instruction::I16x8MaxS,
I16x8MaxU => Instruction::I16x8MaxU,
I16x8RoundingAverageU => Instruction::I16x8AvgrU,
I16x8AvgrU => Instruction::I16x8AvgrU,

I32x4Shl => Instruction::I32x4Shl,
I32x4ShrS => Instruction::I32x4ShrS,
Expand Down Expand Up @@ -772,8 +772,14 @@ impl<'instr> Visitor<'instr> for Emit<'_> {
TableSize(e) => Instruction::TableSize(self.indices.get_table_index(e.table)),
TableFill(e) => Instruction::TableFill(self.indices.get_table_index(e.table)),
RefNull(e) => Instruction::RefNull(match &e.ty {
crate::ValType::Externref => wasm_encoder::HeapType::Extern,
crate::ValType::Funcref => wasm_encoder::HeapType::Func,
crate::ValType::Externref => wasm_encoder::HeapType::Abstract {
shared: false,
ty: wasm_encoder::AbstractHeapType::Extern,
},
crate::ValType::Funcref => wasm_encoder::HeapType::Abstract {
shared: false,
ty: wasm_encoder::AbstractHeapType::Func,
},
_ => unreachable!(),
}),
RefIsNull(_) => Instruction::RefIsNull,
Expand Down
Loading

0 comments on commit b30aa65

Please sign in to comment.