Skip to content

Commit

Permalink
Restore compatibility with wasm-tools. (bytecodealliance#18)
Browse files Browse the repository at this point in the history
* Restore compatibility with wasm-tools.

* An auxiliary function to get the arity of a continuation from an index

* Fetch wasmparser and wast dependencies from effect-handlers/wasm-tools#typed-continuations
  • Loading branch information
dhil committed Apr 11, 2023
1 parent 9479e26 commit 2673215
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 29 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 7 additions & 17 deletions cranelift/wasm/src/code_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
builder: &mut FunctionBuilder,
state: &mut FuncTranslationState,
environ: &mut FE,
ty: Option<wasmparser::ValType>,
) -> WasmResult<()> {
if !state.reachable {
translate_unreachable_operator(validator, &op, builder, state, environ)?;
Expand Down Expand Up @@ -2413,18 +2412,9 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let r = state.pop1();
state.push1(environ.translate_cont_new(builder.cursor(), r)?);
}
Operator::Resume { resumetable } => {
// let call_args : Vec<ir::Value> = match ty {
// None => panic!("Need type of resume operator"),
// Some(wasmparser::ValType::Ref(wasmparser::RefType { heap_type: wasmparser::HeapType::TypedFunc(i), .. })) => {
// let index = u32::from(i);
// println!("index: {:?}", index);
// let (fref, num_args) = state.get_direct_func(builder.func, index, environ)?;
// println!("fref: {:?}, num args: {:?}", fref, num_args);
// vec![]
// }
// Some(_) => panic!("Expected reference type"),
// };
Operator::Resume { type_index, resumetable } => {
let _arity = environ.continuation_arity(*type_index);
//println!("arity: {}", _arity);
let call_args = vec![];
let cont = state.pop1();
let jmpn = environ.translate_resume(builder.cursor(), cont, &call_args)?;
Expand Down Expand Up @@ -2463,20 +2453,20 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
builder,
state,
environ,
None,
)?;
translate_resume_table(builder, state, targets)?;
translate_operator(validator, &Operator::End, builder, state, environ, None)?;
translate_operator(validator, &Operator::End, builder, state, environ)?;
// We kept a continuation on the stack for the suspend cases, but
// on return we have no continuation. so drop that continuation that
// is now completely invalidated (something about deallocate?)
translate_operator(validator, &Operator::Drop, builder, state, environ, None)?;
translate_operator(validator, &Operator::Drop, builder, state, environ)?;
}
Operator::Suspend { tag_index } => {
environ.translate_suspend(builder.cursor(), *tag_index);
}
Operator::ContBind { type_index: _ }
Operator::ContBind { src_index: _, dst_index: _ }
| Operator::ResumeThrow {
type_index: _,
tag_index: _,
resumetable: _,
}
Expand Down
5 changes: 5 additions & 0 deletions cranelift/wasm/src/environ/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,11 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
fn translate_suspend(&mut self, _pos: FuncCursor, _tag_index: u32) {
todo!()
}

fn continuation_arity(&self, _type_index: u32) -> usize {
todo!()
}

}

impl TargetEnvironment for DummyEnvironment {
Expand Down
3 changes: 3 additions & 0 deletions cranelift/wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ pub trait FuncEnvironment: TargetEnvironment {

/// TODO(dhil): write documentation.
fn translate_suspend(&mut self, pos: FuncCursor, tag_index: u32);

/// TODO
fn continuation_arity(&self, type_index: u32) -> usize;
}

/// An object satisfying the `ModuleEnvironment` trait can be passed as argument to the
Expand Down
3 changes: 1 addition & 2 deletions cranelift/wasm/src/func_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(
let pos = reader.original_position();
builder.set_srcloc(cur_srcloc(&reader));
let op = reader.read_operator()?;
let ty = validator.get_operand_type(0).unwrap_or_default();
validator.op(pos, &op)?;
environ.before_translate_operator(&op, builder, state)?;
translate_operator(validator, &op, builder, state, environ, ty)?;
translate_operator(validator, &op, builder, state, environ)?;
environ.after_translate_operator(&op, builder, state)?;
}
environ.after_translate_function(builder, state)?;
Expand Down
5 changes: 5 additions & 0 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2236,4 +2236,9 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
pos.ins()
.call_indirect(builtin_sig, builtin_addr, &[vmctx, tag_index]);
}

fn continuation_arity(&self, index: u32) -> usize {
let idx = self.module.types[TypeIndex::from_u32(index)].unwrap_continuation();
self.types[idx].params().len()
}
}
11 changes: 10 additions & 1 deletion crates/environ/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ impl Default for TableInitialization {
#[allow(missing_docs)]
pub enum ModuleType {
Function(SignatureIndex),
Continuation(TypeIndex),
Continuation(SignatureIndex),
}

impl ModuleType {
Expand All @@ -767,6 +767,15 @@ impl ModuleType {
ModuleType::Continuation(_) => panic!("Attempt to unwrap non-function."),
}
}

/// Assert his is a `ModuleType::Continuation`, returning the
/// underlying `TypeIndex`.
pub fn unwrap_continuation(&self) -> SignatureIndex {
match self {
ModuleType::Continuation(f) => *f,
_ => panic!("Attempt to unwrap non-continuation."),
}
}
}

/// A translated WebAssembly module, excluding the function bodies and
Expand Down
3 changes: 2 additions & 1 deletion crates/environ/src/module_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,11 @@ and for re-adding support for interface types you can see this issue:
}

fn declare_type_cont(&mut self, index: u32) -> WasmResult<()> {
let sig_index = self.result.module.types[TypeIndex::from_u32(index)].unwrap_function();
self.result
.module
.types
.push(ModuleType::Continuation(TypeIndex::from_u32(index)));
.push(ModuleType::Continuation(sig_index));
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/misc_testsuite/typed-continuations/cont_new.wast
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(elem declare func $noop)

(func $make-cont (result (ref $ct))
(cont.new (type $ct) (ref.func $noop)))
(cont.new $ct (ref.func $noop)))

(func $f (export "f") (result i32)
(call $make-cont)
Expand Down
3 changes: 1 addition & 2 deletions tests/misc_testsuite/typed-continuations/cont_resume.wast
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

(func $f (export "f") (result i32)
(global.set $i (i32.const 99))
(resume (cont.new (type $ct)
(ref.func $g)))
(resume $ct (cont.new $ct (ref.func $g)))
(global.get $i))
)

Expand Down
4 changes: 2 additions & 2 deletions tests/misc_testsuite/typed-continuations/cont_suspend.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
(func (export "run") (result i32)
(call $print (i32.const 1))
(block $on_h (result (ref $ct))
(resume (tag $h $on_h)
(cont.new (type $ct) (ref.func $f)))
(resume $ct (tag $h $on_h)
(cont.new $ct (ref.func $f)))
(unreachable))
(drop)
(call $print (i32.const 3))
Expand Down

0 comments on commit 2673215

Please sign in to comment.