OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 var myString; | |
6 | |
7 String ifBailout(test) { | |
8 if (test) { | |
9 // Share the same variable for the type inference. | |
10 var o = myString; | |
11 // Make sure the type inference wants an array. | |
12 if (false) o[1] = 2; | |
13 return '${o[0]} bailout'; | |
14 } | |
15 return '${myString[0]} no bailout'; | |
16 } | |
17 | |
18 String ifElseBailout(test) { | |
19 if (test) { | |
20 // Share the same variable for the type inference. | |
21 var o = myString; | |
22 // Make sure the type inference wants an array. | |
23 if (false) o[1] = 2; | |
24 return '${o[0]} if bailout'; | |
25 } else { | |
26 // Share the same variable for the type inference. | |
27 var o = myString; | |
28 // Make sure the type inference wants an array. | |
29 if (false) o[1] = 2; | |
30 return '${o[0]} else bailout'; | |
31 } | |
32 } | |
33 | |
34 void forBailout() { | |
35 var n = myString.length; | |
36 var res = ''; | |
37 for (int i = 0; i < n; i++) { | |
38 var o = myString; | |
39 if (false) o[1] = 2; | |
40 res += o[i]; | |
41 } | |
42 return res; | |
43 } | |
44 | |
45 void forInBailout() { | |
46 var n = myString.length; | |
47 var res = ''; | |
48 for (int i in myString.charCodes()) { | |
49 var o = myString; | |
50 if (false) o[1] = 2; | |
51 res += new String.fromCharCodes([i]); | |
52 } | |
53 return res; | |
54 } | |
55 | |
56 void innerForBailout() { | |
57 var n = myString.length; | |
58 var res = ''; | |
59 for (int i = 0; i < 2; i++) { | |
60 for (int j = 0; j < n; j++) { | |
61 var o = myString; | |
62 if (false) o[1] = 2; | |
63 res += o[j]; | |
64 } | |
65 } | |
66 return res; | |
67 } | |
68 | |
69 void whileBailout() { | |
70 var n = myString.length; | |
71 var res = ''; | |
72 var i = 0; | |
73 while (i < n) { | |
74 var o = myString; | |
75 if (false) o[1] = 2; | |
76 res += o[i]; | |
77 i++; | |
78 } | |
79 return res; | |
80 } | |
81 | |
82 void doWhileBailout() { | |
83 var n = myString.length; | |
84 var res = ''; | |
85 var i = 0; | |
86 do { | |
87 var o = myString; | |
88 if (false) o[1] = 2; | |
89 res += o[i]; | |
90 i++; | |
91 } while (i < n); | |
92 return res; | |
93 } | |
94 | |
95 void phiBailout() { | |
96 var prev = -1; | |
97 bool inside = false; | |
98 for (int i = 0; i < 2; i++) { | |
99 var o = myString; | |
100 // prev will be a phi converted to a local, and if we're not | |
101 // careful, the bailout target may not have the right value for | |
102 // it. | |
103 if (prev != -1) { | |
104 inside = true; | |
105 if (false) o[0] = 1; | |
106 print(o[0]); | |
107 Expect.equals(0, prev); | |
108 } | |
109 prev = i; | |
110 } | |
111 Expect.isTrue(inside); | |
112 } | |
113 | |
114 int fibonacci(int n) { | |
115 int a = 0, b = 1, i = 0; | |
116 // i, a, b will become phis, and then locals. The i++ creates a | |
117 // load/store sequence that we must be careful with. | |
118 while (i++ < n) { | |
119 a = a + b; | |
120 b = a - b; | |
121 var o = myString; | |
122 if (false) o[0] = 2; | |
123 print(o[0]); | |
124 } | |
125 return a; | |
126 } | |
127 | |
128 void ifPhiBailout1(int bailout) { | |
129 var a = 0; | |
130 var c = 0; | |
131 | |
132 if (a == 0) c = a++; | |
133 else c = a--; | |
134 | |
135 if (bailout == 1) { | |
136 var o = myString; | |
137 if (false) o[0] = 2; | |
138 print(o[0]); | |
139 } | |
140 | |
141 Expect.equals(1, a); | |
142 Expect.equals(0, c); | |
143 | |
144 if (a == 0) c = a++; | |
145 else c = a--; | |
146 | |
147 if (bailout == 2) { | |
148 var o = myString; | |
149 if (false) o[0] = 2; | |
150 print(o[0]); | |
151 } | |
152 | |
153 Expect.equals(0, a); | |
154 Expect.equals(1, c); | |
155 } | |
156 | |
157 void ifPhiBailout2(int bailout) { | |
158 var a = 0; | |
159 var c = 0; | |
160 | |
161 if (a == 0) { | |
162 c = a; | |
163 a = a + 1; | |
164 } else { | |
165 c = a; | |
166 a = a - 1; | |
167 } | |
168 | |
169 if (bailout == 1) { | |
170 var o = myString; | |
171 if (false) o[0] = 2; | |
172 print(o[0]); | |
173 } | |
174 | |
175 Expect.equals(1, a); | |
176 Expect.equals(0, c); | |
177 | |
178 if (a == 0) { | |
179 c = a; | |
180 a = a + 1; | |
181 } else { | |
182 c = a; | |
183 a = a - 1; | |
184 } | |
185 | |
186 if (bailout == 2) { | |
187 var o = myString; | |
188 if (false) o[0] = 2; | |
189 print(o[0]); | |
190 } | |
191 | |
192 Expect.equals(0, a); | |
193 Expect.equals(1, c); | |
194 } | |
195 | |
196 main() { | |
197 myString = '1'; | |
198 Expect.equals('1 no bailout', ifBailout(false)); | |
199 Expect.equals('1 bailout', ifBailout(true)); | |
200 | |
201 Expect.equals('1 else bailout', ifElseBailout(false)); | |
202 Expect.equals('1 if bailout', ifElseBailout(true)); | |
203 | |
204 myString = '1234'; | |
205 Expect.equals('1234', forBailout()); | |
206 Expect.equals('1234', forInBailout()); | |
207 Expect.equals('12341234', innerForBailout()); | |
208 | |
209 Expect.equals('1234', whileBailout()); | |
210 Expect.equals('1234', doWhileBailout()); | |
211 | |
212 Expect.equals(102334155, fibonacci(40)); | |
213 | |
214 phiBailout(); | |
215 ifPhiBailout1(1); | |
216 ifPhiBailout1(2); | |
217 ifPhiBailout2(1); | |
218 ifPhiBailout2(2); | |
219 } | |
OLD | NEW |