Commit eb69c7da authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Simplify exceptional continuation in interpreter.

This unifies how continuations after a control transfer are handled
within the interpreter loop. To avoid bumping the {pc} we can either
reset the opcode length to zero or just "continue" the loop. This just
unifies the approach for the entire {Execute} method.

R=clemensh@chromium.org

Change-Id: Ifc33c3a87cff69d417f61fa0bc234260c7fa502a
Reviewed-on: https://chromium-review.googlesource.com/c/1458216
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59434}
parent 4c65986a
...@@ -1412,13 +1412,12 @@ class ThreadImpl { ...@@ -1412,13 +1412,12 @@ class ThreadImpl {
} }
void ReloadFromFrameOnException(Decoder* decoder, InterpreterCode** code, void ReloadFromFrameOnException(Decoder* decoder, InterpreterCode** code,
pc_t* pc, pc_t* limit, int* len) { pc_t* pc, pc_t* limit) {
Frame* top = &frames_.back(); Frame* top = &frames_.back();
*code = top->code; *code = top->code;
*pc = top->pc; *pc = top->pc;
*limit = top->code->end - top->code->start; *limit = top->code->end - top->code->start;
decoder->Reset(top->code->start, top->code->end); decoder->Reset(top->code->start, top->code->end);
*len = 0; // The {pc} has already been set correctly.
} }
int LookupTargetDelta(InterpreterCode* code, pc_t pc) { int LookupTargetDelta(InterpreterCode* code, pc_t pc) {
...@@ -2377,13 +2376,12 @@ class ThreadImpl { ...@@ -2377,13 +2376,12 @@ class ThreadImpl {
#ifdef DEBUG #ifdef DEBUG
// Compute the stack effect of this opcode, and verify later that the // Compute the stack effect of this opcode, and verify later that the
// stack was modified accordingly (unless an exception was thrown). // stack was modified accordingly.
std::pair<uint32_t, uint32_t> stack_effect = std::pair<uint32_t, uint32_t> stack_effect =
StackEffect(codemap_->module(), frames_.back().code->function->sig, StackEffect(codemap_->module(), frames_.back().code->function->sig,
code->orig_start + pc, code->orig_end); code->orig_start + pc, code->orig_end);
sp_t expected_new_stack_height = sp_t expected_new_stack_height =
StackHeight() - stack_effect.first + stack_effect.second; StackHeight() - stack_effect.first + stack_effect.second;
bool exception_was_thrown = false;
#endif #endif
switch (orig) { switch (orig) {
...@@ -2424,15 +2422,15 @@ class ThreadImpl { ...@@ -2424,15 +2422,15 @@ class ThreadImpl {
CommitPc(pc); // Needed for local unwinding. CommitPc(pc); // Needed for local unwinding.
const WasmException* exception = &module()->exceptions[imm.index]; const WasmException* exception = &module()->exceptions[imm.index];
if (!DoThrowException(exception, imm.index)) return; if (!DoThrowException(exception, imm.index)) return;
ReloadFromFrameOnException(&decoder, &code, &pc, &limit, &len); ReloadFromFrameOnException(&decoder, &code, &pc, &limit);
break; continue; // Do not bump pc.
} }
case kExprRethrow: { case kExprRethrow: {
WasmValue ex = Pop(); WasmValue ex = Pop();
CommitPc(pc); // Needed for local unwinding. CommitPc(pc); // Needed for local unwinding.
if (!DoRethrowException(&ex)) return; if (!DoRethrowException(&ex)) return;
ReloadFromFrameOnException(&decoder, &code, &pc, &limit, &len); ReloadFromFrameOnException(&decoder, &code, &pc, &limit);
break; continue; // Do not bump pc.
} }
case kExprSelect: { case kExprSelect: {
WasmValue cond = Pop(); WasmValue cond = Pop();
...@@ -2481,7 +2479,7 @@ class ThreadImpl { ...@@ -2481,7 +2479,7 @@ class ThreadImpl {
size_t arity = code->function->sig->return_count(); size_t arity = code->function->sig->return_count();
if (!DoReturn(&decoder, &code, &pc, &limit, arity)) return; if (!DoReturn(&decoder, &code, &pc, &limit, arity)) return;
PAUSE_IF_BREAK_FLAG(AfterReturn); PAUSE_IF_BREAK_FLAG(AfterReturn);
continue; continue; // Do not bump pc.
} }
case kExprUnreachable: { case kExprUnreachable: {
return DoTrap(kTrapUnreachable, pc); return DoTrap(kTrapUnreachable, pc);
...@@ -2563,9 +2561,8 @@ class ThreadImpl { ...@@ -2563,9 +2561,8 @@ class ThreadImpl {
case ExternalCallResult::EXTERNAL_UNWOUND: case ExternalCallResult::EXTERNAL_UNWOUND:
return; return;
case ExternalCallResult::EXTERNAL_CAUGHT: case ExternalCallResult::EXTERNAL_CAUGHT:
ReloadFromFrameOnException(&decoder, &code, &pc, &limit, &len); ReloadFromFrameOnException(&decoder, &code, &pc, &limit);
DCHECK(exception_was_thrown = true); continue; // Do not bump pc.
break;
} }
if (result.type != ExternalCallResult::INTERNAL) break; if (result.type != ExternalCallResult::INTERNAL) break;
} }
...@@ -2573,7 +2570,7 @@ class ThreadImpl { ...@@ -2573,7 +2570,7 @@ class ThreadImpl {
if (!DoCall(&decoder, target, &pc, &limit)) return; if (!DoCall(&decoder, target, &pc, &limit)) return;
code = target; code = target;
PAUSE_IF_BREAK_FLAG(AfterCall); PAUSE_IF_BREAK_FLAG(AfterCall);
continue; // don't bump pc continue; // Do not bump pc.
} break; } break;
case kExprCallIndirect: { case kExprCallIndirect: {
CallIndirectImmediate<Decoder::kNoValidate> imm(&decoder, CallIndirectImmediate<Decoder::kNoValidate> imm(&decoder,
...@@ -2591,7 +2588,7 @@ class ThreadImpl { ...@@ -2591,7 +2588,7 @@ class ThreadImpl {
return; return;
code = result.interpreter_code; code = result.interpreter_code;
PAUSE_IF_BREAK_FLAG(AfterCall); PAUSE_IF_BREAK_FLAG(AfterCall);
continue; // don't bump pc continue; // Do not bump pc.
case ExternalCallResult::INVALID_FUNC: case ExternalCallResult::INVALID_FUNC:
return DoTrap(kTrapFuncInvalid, pc); return DoTrap(kTrapFuncInvalid, pc);
case ExternalCallResult::SIGNATURE_MISMATCH: case ExternalCallResult::SIGNATURE_MISMATCH:
...@@ -2603,9 +2600,8 @@ class ThreadImpl { ...@@ -2603,9 +2600,8 @@ class ThreadImpl {
case ExternalCallResult::EXTERNAL_UNWOUND: case ExternalCallResult::EXTERNAL_UNWOUND:
return; return;
case ExternalCallResult::EXTERNAL_CAUGHT: case ExternalCallResult::EXTERNAL_CAUGHT:
ReloadFromFrameOnException(&decoder, &code, &pc, &limit, &len); ReloadFromFrameOnException(&decoder, &code, &pc, &limit);
DCHECK(exception_was_thrown = true); continue; // Do not bump pc.
break;
} }
} break; } break;
case kExprGetGlobal: { case kExprGetGlobal: {
...@@ -2853,7 +2849,7 @@ class ThreadImpl { ...@@ -2853,7 +2849,7 @@ class ThreadImpl {
} }
#ifdef DEBUG #ifdef DEBUG
if (!WasmOpcodes::IsControlOpcode(opcode) && !exception_was_thrown) { if (!WasmOpcodes::IsControlOpcode(opcode)) {
DCHECK_EQ(expected_new_stack_height, StackHeight()); DCHECK_EQ(expected_new_stack_height, StackHeight());
} }
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment