Commit e175e39f authored by mstarzinger's avatar mstarzinger Committed by Commit bot

[interpreter] Implement handling of try-finally constructs.

This models function local control flow through try-finally constructs
using a token dispatch mechanism. All paths through the finally block
are assigned a token, at the end of the finally block a switch construct
dispatches according to this token.

R=oth@chromium.org,rmcilroy@chromium.org
BUG=v8:4674
LOG=n

Review URL: https://codereview.chromium.org/1613443002

Cr-Commit-Position: refs/heads/master@{#33465}
parent 847ac580
This diff is collapsed.
......@@ -32,6 +32,9 @@ class BytecodeGenerator final : public AstVisitor {
class ControlScope;
class ControlScopeForBreakable;
class ControlScopeForIteration;
class ControlScopeForTopLevel;
class ControlScopeForTryCatch;
class ControlScopeForTryFinally;
class ExpressionResultScope;
class EffectResultScope;
class AccumulatorResultScope;
......
......@@ -159,13 +159,31 @@ void TryFinallyBuilder::BeginTry(Register context) {
}
void TryFinallyBuilder::LeaveTry() {
finalization_sites_.push_back(BytecodeLabel());
builder()->Jump(&finalization_sites_.back());
}
void TryFinallyBuilder::EndTry() {
builder()->MarkTryEnd(handler_id_);
}
void TryFinallyBuilder::BeginHandler() {
builder()->Bind(&handler_);
builder()->MarkHandler(handler_id_, false);
}
void TryFinallyBuilder::BeginFinally() {
for (size_t i = 0; i < finalization_sites_.size(); i++) {
BytecodeLabel& site = finalization_sites_.at(i);
builder()->Bind(&site);
}
}
void TryFinallyBuilder::EndFinally() {
// Nothing to be done here.
}
......
......@@ -166,15 +166,23 @@ class TryCatchBuilder final : public ControlFlowBuilder {
class TryFinallyBuilder final : public ControlFlowBuilder {
public:
explicit TryFinallyBuilder(BytecodeArrayBuilder* builder)
: ControlFlowBuilder(builder), handler_id_(builder->NewHandlerEntry()) {}
: ControlFlowBuilder(builder),
handler_id_(builder->NewHandlerEntry()),
finalization_sites_(builder->zone()) {}
void BeginTry(Register context);
void LeaveTry();
void EndTry();
void BeginHandler();
void BeginFinally();
void EndFinally();
private:
int handler_id_;
BytecodeLabel handler_;
// Unbound labels that identify jumps to the finally block in the code.
ZoneVector<BytecodeLabel> finalization_sites_;
};
} // namespace interpreter
......
......@@ -4350,92 +4350,128 @@ TEST(TryFinally) {
ExpectedSnippet<const char*> snippets[] = {
{"var a = 1; try { a = 2; } finally { a = 3; }",
2 * kPointerSize,
4 * kPointerSize,
1,
14,
35,
{
B(LdaSmi8), U8(1), //
B(Star), R(0), //
B(LdaSmi8), U8(2), //
B(Star), R(0), //
B(LdaSmi8), U8(3), //
B(Star), R(0), //
B(LdaUndefined), //
B(Return), //
B(LdaSmi8), U8(1), //
B(Star), R(0), //
B(LdaSmi8), U8(2), //
B(Star), R(0), //
B(LdaSmi8), U8(-1), //
B(Star), R(1), //
B(Jump), U8(7), //
B(Star), R(2), //
B(LdaZero), //
B(Star), R(1), //
B(LdaSmi8), U8(3), //
B(Star), R(0), //
B(LdaZero), //
B(TestEqualStrict), R(1), //
B(JumpIfTrue), U8(4), //
B(Jump), U8(5), //
B(Ldar), R(2), //
B(Throw), //
B(LdaUndefined), //
B(Return), //
},
0,
{},
1,
{{4, 8, 8}}},
{{4, 8, 14}}},
{"var a = 1; try { a = 2; } catch(e) { a = 20 } finally { a = 3; }",
7 * kPointerSize,
9 * kPointerSize,
1,
39,
60,
{
B(LdaSmi8), U8(1), //
B(Star), R(0), //
B(LdaSmi8), U8(2), //
B(Star), R(0), //
B(Jump), U8(25), //
B(Star), R(5), //
B(Star), R(7), //
B(LdaConstant), U8(0), //
B(Star), R(4), //
B(Ldar), R(closure), //
B(Star), R(6), //
B(CallRuntime), U16(Runtime::kPushCatchContext), R(4), U8(3), //
B(Ldar), R(closure), //
B(Star), R(8), //
B(CallRuntime), U16(Runtime::kPushCatchContext), R(6), U8(3), //
B(PushContext), R(1), //
B(LdaSmi8), U8(20), //
B(Star), R(0), //
B(PopContext), R(context), //
B(LdaSmi8), U8(-1), //
B(Star), R(2), //
B(Jump), U8(7), //
B(Star), R(3), //
B(LdaZero), //
B(Star), R(2), //
B(LdaSmi8), U8(3), //
B(Star), R(0), //
B(LdaZero), //
B(TestEqualStrict), R(2), //
B(JumpIfTrue), U8(4), //
B(Jump), U8(5), //
B(Ldar), R(3), //
B(Throw), //
B(LdaUndefined), //
B(Return), //
},
1,
{"e"},
2,
{{4, 33, 33}, {4, 8, 10}}},
{{4, 33, 39}, {4, 8, 10}}},
{"var a; try {"
" try { a = 1 } catch(e) { a = 2 }"
"} catch(e) { a = 20 } finally { a = 3; }",
8 * kPointerSize,
10 * kPointerSize,
1,
60,
81,
{
B(LdaSmi8), U8(1), //
B(Star), R(0), //
B(Jump), U8(25), //
B(Star), R(6), //
B(Star), R(8), //
B(LdaConstant), U8(0), //
B(Star), R(5), //
B(Ldar), R(closure), //
B(Star), R(7), //
B(CallRuntime), U16(Runtime::kPushCatchContext), R(5), U8(3), //
B(Ldar), R(closure), //
B(Star), R(9), //
B(CallRuntime), U16(Runtime::kPushCatchContext), R(7), U8(3), //
B(PushContext), R(1), //
B(LdaSmi8), U8(2), //
B(Star), R(0), //
B(PopContext), R(context), //
B(Jump), U8(25), //
B(Star), R(5), //
B(Star), R(7), //
B(LdaConstant), U8(0), //
B(Star), R(4), //
B(Ldar), R(closure), //
B(Star), R(6), //
B(CallRuntime), U16(Runtime::kPushCatchContext), R(4), U8(3), //
B(Ldar), R(closure), //
B(Star), R(8), //
B(CallRuntime), U16(Runtime::kPushCatchContext), R(6), U8(3), //
B(PushContext), R(1), //
B(LdaSmi8), U8(20), //
B(Star), R(0), //
B(PopContext), R(context), //
B(LdaSmi8), U8(-1), //
B(Star), R(2), //
B(Jump), U8(7), //
B(Star), R(3), //
B(LdaZero), //
B(Star), R(2), //
B(LdaSmi8), U8(3), //
B(Star), R(0), //
B(LdaZero), //
B(TestEqualStrict), R(2), //
B(JumpIfTrue), U8(4), //
B(Jump), U8(5), //
B(Ldar), R(3), //
B(Throw), //
B(LdaUndefined), //
B(Return), //
},
1,
{"e"},
3,
{{0, 54, 54}, {0, 29, 31}, {0, 4, 6}}},
{{0, 54, 60}, {0, 29, 31}, {0, 4, 6}}},
};
for (size_t i = 0; i < arraysize(snippets); i++) {
......
......@@ -2048,15 +2048,45 @@ TEST(InterpreterTryCatch) {
TEST(InterpreterTryFinally) {
HandleAndZoneScope handles;
i::Isolate* isolate = handles.main_isolate();
i::Factory* factory = isolate->factory();
// TODO(rmcilroy): modify tests when we have real try finally support.
std::string source(InterpreterTester::SourceForBody(
"var a = 1; try { a = a + 1; } finally { a = a + 2; }; return a;"));
InterpreterTester tester(handles.main_isolate(), source.c_str());
auto callable = tester.GetCallable<>();
std::pair<const char*, Handle<Object>> finallies[] = {
std::make_pair(
"var a = 1; try { a = a + 1; } finally { a = a + 2; }; return a;",
factory->NewStringFromStaticChars("R4")),
std::make_pair(
"var a = 1; try { a = 2; return 23; } finally { a = 3 }; return a;",
factory->NewStringFromStaticChars("R23")),
std::make_pair(
"var a = 1; try { a = 2; throw 23; } finally { a = 3 }; return a;",
factory->NewStringFromStaticChars("E23")),
std::make_pair(
"var a = 1; try { a = 2; throw 23; } finally { return a; };",
factory->NewStringFromStaticChars("R2")),
std::make_pair(
"var a = 1; try { a = 2; throw 23; } finally { throw 42; };",
factory->NewStringFromStaticChars("E42")),
std::make_pair("var a = 1; for (var i = 10; i < 20; i += 5) {"
" try { a = 2; break; } finally { a = 3; }"
"} return a + i;",
factory->NewStringFromStaticChars("R13")),
std::make_pair("var a = 1; for (var i = 10; i < 20; i += 5) {"
" try { a = 2; continue; } finally { a = 3; }"
"} return a + i;",
factory->NewStringFromStaticChars("R23")),
};
Handle<Object> return_val = callable().ToHandleChecked();
CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(4));
const char* try_wrapper =
"(function() { try { return 'R' + f() } catch(e) { return 'E' + e }})()";
for (size_t i = 0; i < arraysize(finallies); i++) {
std::string source(InterpreterTester::SourceForBody(finallies[i].first));
InterpreterTester tester(handles.main_isolate(), source.c_str());
tester.GetCallable<>();
Handle<Object> wrapped = v8::Utils::OpenHandle(*CompileRun(try_wrapper));
CHECK(wrapped->SameValue(*finallies[i].second));
}
}
......
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