Files
sdk/tests/language/try_catch_test.dart
T
fschneider@google.com 8ad125f607 Optimize functions containing try-catch.
This is a first step towards fully optimizing try-catch-finally.

At a catch entry, all local variables and parameters are
expected at a fixed stack location. There is a list of
initial definitions at the catch entry block, similar to
the initial definitions at graph entry.

Inside every try-block there is a special prologue code before each
call (instruction that may throw) inside the try-block. This prologue
is similar to a parallel move instruction: It moves all locals+parameters
to the locations expected by the catch-entry block. The stack frame
is extended with the corresponding number of fixed slots right below
the normal spill slots.

Every function containing try-catch has additional compiler-
generated local variables to pass the context, the exception and
the stack trace.

Variable liveness analysis is adapted to treat locals inside try{} blocks
specially: Every call has an implicit LoadLocal of every local variable.
This CL uses a safe approximiation of liveness which can be optimized further.

Current restrictions which are planned for future CLs:
 * No inlining inside try-blocks.
 * No inlining of functions containing try-catch.
 * No try-finally yet.

R=kmillikin@google.com

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

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@22615 260f80e4-7a28-3924-810f-c04153c831b5
2013-05-13 10:33:24 +00:00

158 lines
2.8 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class MyException { }
class MyException1 extends MyException { }
class MyException2 extends MyException { }
class TryCatchTest {
static void test1() {
var foo = 0;
try {
throw new MyException1();
} on MyException2 catch (e) {
foo = 1;
} on MyException1 catch (e) {
foo = 2;
} on MyException catch (e) {
foo = 3;
}
Expect.equals(2, foo);
}
static void test2() {
var foo = 0;
try {
throw new MyException1();
} on MyException2 catch (e) {
foo = 1;
} on MyException catch (e) {
foo = 2;
} on MyException1 catch (e) {
foo = 3;
}
Expect.equals(2, foo);
}
static void test3() {
var foo = 0;
try {
throw new MyException();
} on MyException2 catch (e) {
foo = 1;
} on MyException1 catch (e) {
foo = 2;
} on MyException catch (e) {
foo = 3;
}
Expect.equals(3, foo);
}
static void test4() {
var foo = 0;
try {
try {
throw new MyException();
} on MyException2 catch (e) {
foo = 1;
} on MyException1 catch (e) {
foo = 2;
}
} on MyException catch (e) {
Expect.equals(0, foo);
foo = 3;
}
Expect.equals(3, foo);
}
static void test5() {
var foo = 0;
try {
throw new MyException1();
} on MyException2 catch (e) {
foo = 1;
} catch (e) {
foo = 2;
}
Expect.equals(2, foo);
}
static void test6() {
var foo = 0;
try {
throw new MyException();
} on MyException2 catch (e) {
foo = 1;
} on MyException1 catch (e) {
foo = 2;
} catch (e) {
foo = 3;
}
Expect.equals(3, foo);
}
static void test7() {
var foo = 0;
try {
try {
throw new MyException();
} on MyException2 catch (e) {
foo = 1;
} on MyException1 catch (e) {
foo = 2;
}
} catch (e) {
Expect.equals(0, foo);
foo = 3;
}
Expect.equals(3, foo);
}
static void test8() {
var e = 3;
var caught = false;
try {
throw new MyException();
} catch (exc) {
caught = true;
}
Expect.equals(true, caught);
Expect.equals(3, e);
}
static void test9() {
var e = 6;
try {
throw "up";
} on String {
e = "s";
} on int {
e = "i";
}
Expect.equals("s", e);
}
static void testMain() {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
}
}
main() {
for (var i = 0; i < 2000; i++) {
TryCatchTest.testMain();
}
}