OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Test various setter situations, testing special cases in optimizing compiler. | 4 // Test various setter situations, testing special cases in optimizing compiler. |
5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-co
mpilation | 5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-co
mpilation |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 class A { | 9 class A { |
10 int field = 0; | 10 int field = 0; |
11 } | 11 } |
12 | 12 |
13 class B extends A { | 13 class B extends A {} |
14 } | |
15 | |
16 | 14 |
17 void sameImplicitSetter() { | 15 void sameImplicitSetter() { |
18 oneTarget(var a, var v) { | 16 oneTarget(var a, var v) { |
19 a.field = v; | 17 a.field = v; |
20 } | 18 } |
21 | 19 |
22 A a = new A(); | 20 A a = new A(); |
23 B b = new B(); | 21 B b = new B(); |
24 // Optimize 'oneTarget' for one class, one target. | 22 // Optimize 'oneTarget' for one class, one target. |
25 for (int i = 0; i < 20; i++) { | 23 for (int i = 0; i < 20; i++) { |
26 oneTarget(a, 5); | 24 oneTarget(a, 5); |
27 Expect.equals(5, a.field); | 25 Expect.equals(5, a.field); |
28 } | 26 } |
29 // Deoptimize 'oneTarget', since class B is not expected. | 27 // Deoptimize 'oneTarget', since class B is not expected. |
30 oneTarget(b, 6); | 28 oneTarget(b, 6); |
31 Expect.equals(6, b.field); | 29 Expect.equals(6, b.field); |
32 // Optimize 'oneTarget' for A and B classes, one target. | 30 // Optimize 'oneTarget' for A and B classes, one target. |
33 for (int i = 0; i < 20; i++) { | 31 for (int i = 0; i < 20; i++) { |
34 oneTarget(a, 7); | 32 oneTarget(a, 7); |
35 Expect.equals(7, a.field); | 33 Expect.equals(7, a.field); |
36 } | 34 } |
37 oneTarget(b, 8); | 35 oneTarget(b, 8); |
38 Expect.equals(8, b.field); | 36 Expect.equals(8, b.field); |
39 } | 37 } |
40 | 38 |
41 | |
42 // Deoptimize when no feedback exists. | 39 // Deoptimize when no feedback exists. |
43 void setterNoFeedback() { | 40 void setterNoFeedback() { |
44 maybeSet(var a, var v, bool set_it) { | 41 maybeSet(var a, var v, bool set_it) { |
45 if (set_it) { | 42 if (set_it) { |
46 return a.field = v; | 43 return a.field = v; |
47 } | 44 } |
48 return -1; | 45 return -1; |
49 } | 46 } |
50 | 47 |
51 A a = new A(); | 48 A a = new A(); |
52 for (int i = 0; i < 20; i++) { | 49 for (int i = 0; i < 20; i++) { |
53 var r = maybeSet(a, 5, false); | 50 var r = maybeSet(a, 5, false); |
54 Expect.equals(0, a.field); | 51 Expect.equals(0, a.field); |
55 Expect.equals(-1, r); | 52 Expect.equals(-1, r); |
56 } | 53 } |
57 var r = maybeSet(a, 5, true); | 54 var r = maybeSet(a, 5, true); |
58 Expect.equals(5, a.field); | 55 Expect.equals(5, a.field); |
59 Expect.equals(5, r); | 56 Expect.equals(5, r); |
60 for (int i = 0; i < 20; i++) { | 57 for (int i = 0; i < 20; i++) { |
61 var r = maybeSet(a, 6, true); | 58 var r = maybeSet(a, 6, true); |
62 Expect.equals(6, a.field); | 59 Expect.equals(6, a.field); |
63 Expect.equals(6, r); | 60 Expect.equals(6, r); |
64 } | 61 } |
65 } | 62 } |
66 | 63 |
67 // Check non-implicit setter | 64 // Check non-implicit setter |
68 class X { | 65 class X { |
69 int pField = 0; | 66 int pField = 0; |
70 set field(v) { pField = v; } | 67 set field(v) { |
| 68 pField = v; |
| 69 } |
| 70 |
71 get field => 10; | 71 get field => 10; |
72 } | 72 } |
73 | 73 |
74 void sameNotImplicitSetter() { | 74 void sameNotImplicitSetter() { |
75 oneTarget(var a, var v) { | 75 oneTarget(var a, var v) { |
76 return a.field = v; | 76 return a.field = v; |
77 } | 77 } |
78 | 78 |
79 incField(var a) { | 79 incField(var a) { |
80 a.field++; | 80 a.field++; |
81 } | 81 } |
82 | 82 |
83 X x = new X(); | 83 X x = new X(); |
84 for (int i = 0; i < 20; i++) { | 84 for (int i = 0; i < 20; i++) { |
85 var r = oneTarget(x, 3); | 85 var r = oneTarget(x, 3); |
86 Expect.equals(3, x.pField); | 86 Expect.equals(3, x.pField); |
87 Expect.equals(3, r); | 87 Expect.equals(3, r); |
88 } | 88 } |
89 oneTarget(x, 0); | 89 oneTarget(x, 0); |
90 for (int i = 0; i < 20; i++) { | 90 for (int i = 0; i < 20; i++) { |
91 incField(x); | 91 incField(x); |
92 } | 92 } |
93 Expect.equals(11, x.pField); | 93 Expect.equals(11, x.pField); |
94 } | 94 } |
95 | 95 |
96 class Y { | 96 class Y { |
97 int field = 0; | 97 int field = 0; |
98 } | 98 } |
99 | 99 |
100 | |
101 multiImplicitSetter() { | 100 multiImplicitSetter() { |
102 oneTarget(var a, var v) { | 101 oneTarget(var a, var v) { |
103 return a.field = v; | 102 return a.field = v; |
104 } | 103 } |
105 | 104 |
106 // Both classes 'Y' and 'A' have a distinct field getter. | 105 // Both classes 'Y' and 'A' have a distinct field getter. |
107 A a = new A(); | 106 A a = new A(); |
108 Y y = new Y(); | 107 Y y = new Y(); |
109 for (int i = 0; i < 20; i++) { | 108 for (int i = 0; i < 20; i++) { |
110 var r = oneTarget(a, 5); | 109 var r = oneTarget(a, 5); |
111 Expect.equals(5, a.field); | 110 Expect.equals(5, a.field); |
112 Expect.equals(5, r); | 111 Expect.equals(5, r); |
113 r = oneTarget(y, 6); | 112 r = oneTarget(y, 6); |
114 Expect.equals(6, y.field); | 113 Expect.equals(6, y.field); |
115 Expect.equals(6, r); | 114 Expect.equals(6, r); |
116 } | 115 } |
117 } | 116 } |
118 | 117 |
119 class Z { | 118 class Z { |
120 int pField = 0; | 119 int pField = 0; |
121 set field(v) { pField = v; } | 120 set field(v) { |
| 121 pField = v; |
| 122 } |
| 123 |
122 get field => 10; | 124 get field => 10; |
123 } | 125 } |
124 | 126 |
125 multiNotImplicitSetter() { | 127 multiNotImplicitSetter() { |
126 oneTarget(var a, var v) { | 128 oneTarget(var a, var v) { |
127 return a.field = v; | 129 return a.field = v; |
128 } | 130 } |
129 | 131 |
130 Y y = new Y(); | 132 Y y = new Y(); |
131 Z z = new Z(); | 133 Z z = new Z(); |
(...skipping 12 matching lines...) Expand all Loading... |
144 } | 146 } |
145 | 147 |
146 void main() { | 148 void main() { |
147 sameImplicitSetter(); | 149 sameImplicitSetter(); |
148 setterNoFeedback(); | 150 setterNoFeedback(); |
149 sameNotImplicitSetter(); | 151 sameNotImplicitSetter(); |
150 | 152 |
151 multiImplicitSetter(); | 153 multiImplicitSetter(); |
152 multiNotImplicitSetter(); | 154 multiNotImplicitSetter(); |
153 } | 155 } |
OLD | NEW |