Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
linxuanm committed Oct 26, 2024
1 parent b0c977f commit 7ef1beb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/engine/Module.v3
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ class FuncDecl(sig_index: int) extends Decl {
def findExHandler(instance: Instance, tag: Tag, throw_pc: int) -> ExHandler {
return findHandler("findExHandler", handlers, instance, tag, throw_pc);
}
def findResumeHandler(instance: Instance, tag: Tag, throw_pc: int) -> ExHandler {
return findHandler("findResumeHandler", resume_handlers, instance, tag, throw_pc);
def findSuspendHandler(instance: Instance, tag: Tag, throw_pc: int) -> ExHandler {
return findHandler("findSuspendHandler", resume_handlers, instance, tag, throw_pc);
}
def findHandler(name: string, handlers: Array<ExHandlerEntry>, instance: Instance, tag: Tag, throw_pc: int) -> ExHandler {
var i = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/v3/V3Interpreter.v3
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ class V3Interpreter extends WasmStack {
}
// ext:typed-continuation
def gotoResumeHandlerAndConsume(tag: Tag) -> bool {
var handler = frame.func.decl.findResumeHandler(frame.func.instance, tag, prev_resume_pc);
var handler = frame.func.decl.findSuspendHandler(frame.func.instance, tag, prev_resume_pc);
prev_resume_pc = -1;
if (handler.handler_pc >= 0) {
var code = frame.func.decl.cur_bytecode;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/x86-64/X86_64Interpreter.v3
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,7 @@ class X86_64InterpreterGen(ic: X86_64InterpreterCode, w: DataWriter) {
// asm.movq_r_m(handlers, parent.plus(offsets.X86_64Stack_curr_resume_handler));

saveCallerIVars();
callRuntime(refRuntimeCall(RT.runtime_handle_suspend), [r_instance, tag_index], true);
callRuntime(refRuntimeCall(RT.runtime_handle_suspend), [r_instance, tag_index], false);
restoreCallerIVars();

var hentry = r_tmp4;
Expand Down
23 changes: 12 additions & 11 deletions src/engine/x86-64/X86_64Runtime.v3
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,33 @@ component X86_64Runtime {
var stack = to_stack.load<X86_64Stack>();
X86_64Stack.!(curStack).moveToStack(stack, n_vals);
}
def runtime_handle_suspend(instance: Instance, tag_index: int) -> Throwable {
// Returns the parent stack with a handler for the current suspension.
// For communication purposes, also pushes [cont] onto the current
// stack, where {cont} is the resulting continuation.
def runtime_handle_suspend(instance: Instance, tag_index: int) -> Pointer {
var stack = curStack.setRsp(CiRuntime.callerSp());
var tag = instance.tags[tag_index];

var prev = stack;
while (prev.parent != null) {
var parent = X86_64Stack.!(prev.parent);
var accessor = TargetFrame(parent.rsp).getFrameAccessor();
var lookup_pc = accessor.pc();
var func = accessor.func().decl;
var handler = func.findResumeHandler(instance, tag, lookup_pc);
if (handler.handler_pc >= 0) {
var frame = TargetFrame(parent.rsp);
if (!parent.findSuspendHandler(frame, tag)) { // handler found
var cont = Continuation.new(stack, prev);
prev.parent = null;
prev.parent_rsp_ptr = Pointer.NULL;



Trace.OUT.puts("FOUND").ln();
return null;
return Pointer.NULL;
}

prev = parent;
}

// var foo = 1 / 0;

// Not handled
return stack.trap(TrapReason.INVALID_SUSPEND);
stack.push(Values.REF_NULL);
return Pointer.NULL;
}

//=====================================================================
Expand Down
17 changes: 15 additions & 2 deletions src/engine/x86-64/X86_64Stack.v3
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,21 @@ class X86_64Stack extends WasmStack {
def setRsp(ptr: Pointer) -> this {
rsp = ptr;
}
def findSuspendHandler(tag: Tag) -> bool {
return false;
// This function returns TRUE if a handler IS NOT found.
// This is counter-intuitive but it is consistent with {findExHandler}.
// TODO[ss]: refractor a function similar to {walk} for stacks so that
// suspensions and exceptions up the stack chain can be handled similarly.
def findSuspendHandler(frame: TargetFrame, tag: Tag) -> bool {
var accessor = frame.getFrameAccessor();
var func = accessor.func();
var handler = func.decl.findSuspendHandler(func.instance, tag, accessor.pc());

if (handler.handler_pc >= 0) {
if (Trace.stack) Trace.OUT.put2(" found suspend handler: pc=%d, stp=%d", handler.handler_pc, handler.sidetable_pos).ln();

}

return true;
}
private def findExHandler(frame: TargetFrame, ex: Exception) -> bool {
var accessor = frame.getFrameAccessor(); // XXX: don't materialize accessor
Expand Down

0 comments on commit 7ef1beb

Please sign in to comment.