Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f59963d

Browse files
fix: call param order
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 0b2bd6c commit f59963d

File tree

9 files changed

+99
-193
lines changed

9 files changed

+99
-193
lines changed

crates/parser/src/conversion.rs

Lines changed: 36 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@ use crate::{module::CodeSection, Result};
1111
pub(crate) fn convert_module_elements<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Element<'a>>>>(
1212
elements: T,
1313
) -> Result<Vec<tinywasm_types::Element>> {
14-
let elements = elements
15-
.into_iter()
16-
.map(|element| convert_module_element(element?))
17-
.collect::<Result<Vec<_>>>()?;
14+
let elements = elements.into_iter().map(|element| convert_module_element(element?)).collect::<Result<Vec<_>>>()?;
1815
Ok(elements)
1916
}
2017

2118
pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result<tinywasm_types::Element> {
2219
let kind = match element.kind {
23-
wasmparser::ElementKind::Active {
24-
table_index,
25-
offset_expr,
26-
} => tinywasm_types::ElementKind::Active {
20+
wasmparser::ElementKind::Active { table_index, offset_expr } => tinywasm_types::ElementKind::Active {
2721
table: table_index,
2822
offset: process_const_operators(offset_expr.get_operators_reader())?,
2923
},
@@ -32,38 +26,24 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result
3226
};
3327

3428
let items = match element.items {
35-
wasmparser::ElementItems::Functions(funcs) => funcs
36-
.into_iter()
37-
.map(|func| Ok(ElementItem::Func(func?)))
38-
.collect::<Result<Vec<_>>>()?
39-
.into_boxed_slice(),
29+
wasmparser::ElementItems::Functions(funcs) => {
30+
funcs.into_iter().map(|func| Ok(ElementItem::Func(func?))).collect::<Result<Vec<_>>>()?.into_boxed_slice()
31+
}
4032

4133
wasmparser::ElementItems::Expressions(exprs) => exprs
4234
.into_iter()
43-
.map(|expr| {
44-
Ok(ElementItem::Expr(process_const_operators(
45-
expr?.get_operators_reader(),
46-
)?))
47-
})
35+
.map(|expr| Ok(ElementItem::Expr(process_const_operators(expr?.get_operators_reader())?)))
4836
.collect::<Result<Vec<_>>>()?
4937
.into_boxed_slice(),
5038
};
5139

52-
Ok(tinywasm_types::Element {
53-
kind,
54-
items,
55-
ty: convert_valtype(&element.ty),
56-
range: element.range,
57-
})
40+
Ok(tinywasm_types::Element { kind, items, ty: convert_valtype(&element.ty), range: element.range })
5841
}
5942

6043
pub(crate) fn convert_module_data_sections<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Data<'a>>>>(
6144
data_sections: T,
6245
) -> Result<Vec<tinywasm_types::Data>> {
63-
let data_sections = data_sections
64-
.into_iter()
65-
.map(|data| convert_module_data(data?))
66-
.collect::<Result<Vec<_>>>()?;
46+
let data_sections = data_sections.into_iter().map(|data| convert_module_data(data?)).collect::<Result<Vec<_>>>()?;
6747
Ok(data_sections)
6848
}
6949

@@ -72,15 +52,9 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm
7252
data: data.data.to_vec().into_boxed_slice(),
7353
range: data.range,
7454
kind: match data.kind {
75-
wasmparser::DataKind::Active {
76-
memory_index,
77-
offset_expr,
78-
} => {
55+
wasmparser::DataKind::Active { memory_index, offset_expr } => {
7956
let offset = process_const_operators(offset_expr.get_operators_reader())?;
80-
tinywasm_types::DataKind::Active {
81-
mem: memory_index,
82-
offset,
83-
}
57+
tinywasm_types::DataKind::Active { mem: memory_index, offset }
8458
}
8559
wasmparser::DataKind::Passive => tinywasm_types::DataKind::Passive,
8660
},
@@ -90,10 +64,7 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm
9064
pub(crate) fn convert_module_imports<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Import<'a>>>>(
9165
imports: T,
9266
) -> Result<Vec<Import>> {
93-
let imports = imports
94-
.into_iter()
95-
.map(|import| convert_module_import(import?))
96-
.collect::<Result<Vec<_>>>()?;
67+
let imports = imports.into_iter().map(|import| convert_module_import(import?)).collect::<Result<Vec<_>>>()?;
9768
Ok(imports)
9869
}
9970

@@ -105,15 +76,11 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
10576
wasmparser::TypeRef::Func(ty) => ImportKind::Func(ty),
10677
wasmparser::TypeRef::Table(ty) => ImportKind::Table(convert_module_table(ty)?),
10778
wasmparser::TypeRef::Memory(ty) => ImportKind::Mem(convert_module_memory(ty)?),
108-
wasmparser::TypeRef::Global(ty) => ImportKind::Global(GlobalType {
109-
mutable: ty.mutable,
110-
ty: convert_valtype(&ty.content_type),
111-
}),
79+
wasmparser::TypeRef::Global(ty) => {
80+
ImportKind::Global(GlobalType { mutable: ty.mutable, ty: convert_valtype(&ty.content_type) })
81+
}
11282
wasmparser::TypeRef::Tag(ty) => {
113-
return Err(crate::ParseError::UnsupportedOperator(format!(
114-
"Unsupported import kind: {:?}",
115-
ty
116-
)))
83+
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {:?}", ty)))
11784
}
11885
},
11986
})
@@ -122,10 +89,8 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
12289
pub(crate) fn convert_module_memories<T: IntoIterator<Item = wasmparser::Result<wasmparser::MemoryType>>>(
12390
memory_types: T,
12491
) -> Result<Vec<MemoryType>> {
125-
let memory_type = memory_types
126-
.into_iter()
127-
.map(|memory| convert_module_memory(memory?))
128-
.collect::<Result<Vec<_>>>()?;
92+
let memory_type =
93+
memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::<Result<Vec<_>>>()?;
12994

13095
Ok(memory_type)
13196
}
@@ -144,21 +109,14 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<Me
144109
pub(crate) fn convert_module_tables<T: IntoIterator<Item = wasmparser::Result<wasmparser::TableType>>>(
145110
table_types: T,
146111
) -> Result<Vec<TableType>> {
147-
let table_type = table_types
148-
.into_iter()
149-
.map(|table| convert_module_table(table?))
150-
.collect::<Result<Vec<_>>>()?;
112+
let table_type = table_types.into_iter().map(|table| convert_module_table(table?)).collect::<Result<Vec<_>>>()?;
151113

152114
Ok(table_type)
153115
}
154116

155117
pub(crate) fn convert_module_table(table: wasmparser::TableType) -> Result<TableType> {
156118
let ty = convert_valtype(&table.element_type);
157-
Ok(TableType {
158-
element_type: ty,
159-
size_initial: table.initial,
160-
size_max: table.maximum,
161-
})
119+
Ok(TableType { element_type: ty, size_initial: table.initial, size_max: table.maximum })
162120
}
163121

164122
pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Global<'a>>>>(
@@ -171,13 +129,7 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Resu
171129
let ty = convert_valtype(&global.ty.content_type);
172130
let ops = global.init_expr.get_operators_reader();
173131

174-
Ok(Global {
175-
init: process_const_operators(ops)?,
176-
ty: GlobalType {
177-
mutable: global.ty.mutable,
178-
ty,
179-
},
180-
})
132+
Ok(Global { init: process_const_operators(ops)?, ty: GlobalType { mutable: global.ty.mutable, ty } })
181133
})
182134
.collect::<Result<Vec<_>>>()?;
183135
Ok(globals)
@@ -190,18 +142,11 @@ pub(crate) fn convert_module_export(export: wasmparser::Export) -> Result<Export
190142
wasmparser::ExternalKind::Memory => ExternalKind::Memory,
191143
wasmparser::ExternalKind::Global => ExternalKind::Global,
192144
wasmparser::ExternalKind::Tag => {
193-
return Err(crate::ParseError::UnsupportedOperator(format!(
194-
"Unsupported export kind: {:?}",
195-
export.kind
196-
)))
145+
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported export kind: {:?}", export.kind)))
197146
}
198147
};
199148

200-
Ok(Export {
201-
index: export.index,
202-
name: Box::from(export.name),
203-
kind,
204-
})
149+
Ok(Export { index: export.index, name: Box::from(export.name), kind })
205150
}
206151

207152
pub(crate) fn convert_module_code(
@@ -224,27 +169,16 @@ pub(crate) fn convert_module_code(
224169
let body_reader = func.get_operators_reader()?;
225170
let body = process_operators(body_reader.original_position(), body_reader.into_iter(), validator)?;
226171

227-
Ok(CodeSection {
228-
locals: locals.into_boxed_slice(),
229-
body,
230-
})
172+
Ok(CodeSection { locals: locals.into_boxed_slice(), body })
231173
}
232174

233175
pub(crate) fn convert_module_type(ty: wasmparser::Type) -> Result<FuncType> {
234176
let wasmparser::Type::Func(ty) = ty;
235-
let params = ty
236-
.params()
237-
.iter()
238-
.map(|p| Ok(convert_valtype(p)))
239-
.collect::<Result<Vec<ValType>>>()?
240-
.into_boxed_slice();
241-
242-
let results = ty
243-
.results()
244-
.iter()
245-
.map(|p| Ok(convert_valtype(p)))
246-
.collect::<Result<Vec<ValType>>>()?
247-
.into_boxed_slice();
177+
let params =
178+
ty.params().iter().map(|p| Ok(convert_valtype(p))).collect::<Result<Vec<ValType>>>()?.into_boxed_slice();
179+
180+
let results =
181+
ty.results().iter().map(|p| Ok(convert_valtype(p))).collect::<Result<Vec<ValType>>>()?.into_boxed_slice();
248182

249183
Ok(FuncType { params, results })
250184
}
@@ -270,19 +204,14 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType {
270204
I64 => ValType::I64,
271205
F32 => ValType::F32,
272206
F64 => ValType::F64,
273-
V128 => ValType::V128,
207+
V128 => unimplemented!("128-bit values are not supported yet"),
274208
FuncRef => ValType::FuncRef,
275209
ExternRef => ValType::ExternRef,
276210
}
277211
}
278212

279213
pub(crate) fn convert_memarg(memarg: wasmparser::MemArg) -> MemArg {
280-
MemArg {
281-
offset: memarg.offset,
282-
align: memarg.align,
283-
align_max: memarg.max_align,
284-
mem_addr: memarg.memory,
285-
}
214+
MemArg { offset: memarg.offset, align: memarg.align, align_max: memarg.max_align, mem_addr: memarg.memory }
286215
}
287216

288217
pub(crate) fn process_const_operators(ops: OperatorsReader) -> Result<ConstInstruction> {
@@ -306,10 +235,7 @@ pub fn process_const_operator(op: wasmparser::Operator) -> Result<ConstInstructi
306235
wasmparser::Operator::F32Const { value } => Ok(ConstInstruction::F32Const(f32::from_bits(value.bits()))), // TODO: check if this is correct
307236
wasmparser::Operator::F64Const { value } => Ok(ConstInstruction::F64Const(f64::from_bits(value.bits()))), // TODO: check if this is correct
308237
wasmparser::Operator::GlobalGet { global_index } => Ok(ConstInstruction::GlobalGet(global_index)),
309-
op => Err(crate::ParseError::UnsupportedOperator(format!(
310-
"Unsupported const instruction: {:?}",
311-
op
312-
))),
238+
op => Err(crate::ParseError::UnsupportedOperator(format!("Unsupported const instruction: {:?}", op))),
313239
}
314240
}
315241

@@ -332,9 +258,7 @@ pub fn process_operators<'a>(
332258
let res = match op {
333259
BrTable { targets } => {
334260
let def = targets.default();
335-
let targets = targets
336-
.targets()
337-
.collect::<Result<Vec<u32>, wasmparser::BinaryReaderError>>()?;
261+
let targets = targets.targets().collect::<Result<Vec<u32>, wasmparser::BinaryReaderError>>()?;
338262
instructions.push(Instruction::BrTable(def, targets.len()));
339263
instructions.extend(targets.into_iter().map(Instruction::BrLabel));
340264
continue;
@@ -406,11 +330,7 @@ pub fn process_operators<'a>(
406330
BrIf { relative_depth } => Instruction::BrIf(relative_depth),
407331
Return => Instruction::Return,
408332
Call { function_index } => Instruction::Call(function_index),
409-
CallIndirect {
410-
type_index,
411-
table_index,
412-
..
413-
} => Instruction::CallIndirect(type_index, table_index),
333+
CallIndirect { type_index, table_index, .. } => Instruction::CallIndirect(type_index, table_index),
414334
Drop => Instruction::Drop,
415335
Select => Instruction::Select(None),
416336
TypedSelect { ty } => Instruction::Select(Some(convert_valtype(&ty))),
@@ -590,30 +510,21 @@ pub fn process_operators<'a>(
590510
TableGet { table } => Instruction::TableGet(table),
591511
TableSet { table } => Instruction::TableSet(table),
592512
TableInit { table, elem_index } => Instruction::TableInit(table, elem_index),
593-
TableCopy { src_table, dst_table } => Instruction::TableCopy {
594-
from: src_table,
595-
to: dst_table,
596-
},
513+
TableCopy { src_table, dst_table } => Instruction::TableCopy { from: src_table, to: dst_table },
597514
TableGrow { table } => Instruction::TableGrow(table),
598515
TableSize { table } => Instruction::TableSize(table),
599516
TableFill { table } => Instruction::TableFill(table),
600517
op => {
601518
log::error!("Unsupported instruction: {:?}", op);
602-
return Err(crate::ParseError::UnsupportedOperator(format!(
603-
"Unsupported instruction: {:?}",
604-
op
605-
)));
519+
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported instruction: {:?}", op)));
606520
}
607521
};
608522

609523
instructions.push(res);
610524
}
611525

612526
if !labels_ptrs.is_empty() {
613-
panic!(
614-
"last_label_pointer should be None after processing all instructions: {:?}",
615-
labels_ptrs
616-
);
527+
panic!("last_label_pointer should be None after processing all instructions: {:?}", labels_ptrs);
617528
}
618529

619530
validator.finish(offset)?;

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn exec_one(
111111
Nop => { /* do nothing */ }
112112
Unreachable => return Ok(ExecResult::Trap(crate::Trap::Unreachable)), // we don't need to include the call frame here because it's already on the stack
113113
Drop => stack.values.pop().map(|_| ())?,
114-
Select(_) => {
114+
Select(t) => {
115115
// due to validation, we know that the type of the values on the stack
116116
let cond: i32 = stack.values.pop()?.into();
117117
let val2 = stack.values.pop()?;
@@ -143,8 +143,7 @@ fn exec_one(
143143

144144
debug!("params: {:?}", func_ty.params);
145145
debug!("stack: {:?}", stack.values);
146-
let params = stack.values.pop_n(func_ty.params.len())?;
147-
146+
let params = stack.values.pop_n_rev(func_ty.params.len())?;
148147
let call_frame = CallFrame::new_raw(func_idx as usize, &params, func.locals.to_vec());
149148

150149
// push the call frame
@@ -163,10 +162,11 @@ fn exec_one(
163162
let call_ty = module.func_ty(*type_addr);
164163

165164
let func_idx = stack.values.pop_t::<u32>()?;
166-
let func_addr = table.borrow().get(func_idx as usize)?;
165+
let actual_func_addr = table.borrow().get(func_idx as usize)?;
166+
let resolved_func_addr = module.resolve_func_addr(actual_func_addr);
167167

168168
// prepare the call frame
169-
let func_inst = store.get_func(func_addr as usize)?;
169+
let func_inst = store.get_func(resolved_func_addr as usize)?;
170170
let func = match &func_inst.func {
171171
crate::Function::Wasm(ref f) => f,
172172
crate::Function::Host(host_func) => {
@@ -185,9 +185,8 @@ fn exec_one(
185185
);
186186
}
187187

188-
let params = stack.values.pop_n(func_ty.params.len())?;
189-
190-
let call_frame = CallFrame::new_raw(func_addr as usize, &params, func.locals.to_vec());
188+
let params = stack.values.pop_n_rev(func_ty.params.len())?;
189+
let call_frame = CallFrame::new_raw(resolved_func_addr as usize, &params, func.locals.to_vec());
191190

192191
// push the call frame
193192
cf.instr_ptr += 1; // skip the call instruction
@@ -218,7 +217,7 @@ fn exec_one(
218217
cf.instr_ptr += *end_offset
219218
}
220219
} else {
221-
log::info!("entering then");
220+
log::trace!("entering then");
222221
cf.enter_label(
223222
LabelFrame {
224223
instr_ptr: cf.instr_ptr,

0 commit comments

Comments
 (0)