blob: 94105ec9d4b76d271605bd066b3593b33db48987 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Ben Murdoch257744e2011-11-30 15:57:28 +000028// Flags: --allow-natives-syntax --expose-gc
Steve Blocka7e24c12009-10-30 11:49:00 +000029
Ben Murdoch8b112d22011-06-08 16:22:53 +010030// This is a regression test for overlapping key and value registers.
31function f(a) {
32 a[0] = 0;
33 a[1] = 0;
34}
Steve Blocka7e24c12009-10-30 11:49:00 +000035
Ben Murdoch8b112d22011-06-08 16:22:53 +010036var a = new Int32Array(2);
37for (var i = 0; i < 5; i++) {
38 f(a);
39}
40%OptimizeFunctionOnNextCall(f);
41f(a);
Steve Blocka7e24c12009-10-30 11:49:00 +000042
Ben Murdoch8b112d22011-06-08 16:22:53 +010043assertEquals(0, a[0]);
44assertEquals(0, a[1]);
Steve Blocka7e24c12009-10-30 11:49:00 +000045
Ben Murdoch8b112d22011-06-08 16:22:53 +010046// Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is
47// "constant", but not read-only).
48a = new Int32Array(2);
49assertEquals(4, a.BYTES_PER_ELEMENT);
50a.BYTES_PER_ELEMENT = 42;
51assertEquals(42, a.BYTES_PER_ELEMENT);
52a = new Uint8Array(2);
53assertEquals(1, a.BYTES_PER_ELEMENT);
54a = new Int16Array(2);
55assertEquals(2, a.BYTES_PER_ELEMENT);
56
Ben Murdoch257744e2011-11-30 15:57:28 +000057// Test Float64Arrays.
58function get(a, index) {
59 return a[index];
60}
61function set(a, index, value) {
62 a[index] = value;
63}
64function temp() {
65var array = new Float64Array(2);
66for (var i = 0; i < 5; i++) {
67 set(array, 0, 2.5);
68 assertEquals(2.5, array[0]);
69}
70%OptimizeFunctionOnNextCall(set);
71set(array, 0, 2.5);
72assertEquals(2.5, array[0]);
73set(array, 1, 3.5);
74assertEquals(3.5, array[1]);
75for (var i = 0; i < 5; i++) {
76 assertEquals(2.5, get(array, 0));
77 assertEquals(3.5, array[1]);
78}
79%OptimizeFunctionOnNextCall(get);
80assertEquals(2.5, get(array, 0));
81assertEquals(3.5, get(array, 1));
82}
83
84// Test loads and stores.
85types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array,
86 Uint32Array, PixelArray, Float32Array, Float64Array];
87
88test_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN];
89test_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1];
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090test_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -1.25];
Ben Murdoch257744e2011-11-30 15:57:28 +000091test_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75];
92test_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256];
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000093test_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25];
Ben Murdoch257744e2011-11-30 15:57:28 +000094
95const kElementCount = 40;
96
97function test_load(array, sum) {
98 for (var i = 0; i < kElementCount; i++) {
99 sum += array[i];
100 }
101 return sum;
102}
103
104function test_load_const_key(array, sum) {
105 sum += array[0];
106 sum += array[1];
107 sum += array[2];
108 return sum;
109}
110
111function test_store(array, sum) {
112 for (var i = 0; i < kElementCount; i++) {
113 sum += array[i] = i+1;
114 }
115 return sum;
116}
117
118function test_store_const_key(array, sum) {
119 sum += array[0] = 1;
120 sum += array[1] = 2;
121 sum += array[2] = 3;
122 return sum;
123}
124
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125function zero() {
126 return 0.0;
127}
Ben Murdoch257744e2011-11-30 15:57:28 +0000128
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000129function test_store_middle_tagged(array, sum) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000130 array[0] = 253.75;
131 return array[0];
132}
133
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000134function test_store_high_tagged(array, sum) {
135 array[0] = 256.25;
136 return array[0];
137}
138
139function test_store_middle_double(array, sum) {
140 array[0] = 253.75 + zero(); // + forces double type feedback
141 return array[0];
142}
Ben Murdoch257744e2011-11-30 15:57:28 +0000143
144function test_store_high_double(array, sum) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000145 array[0] = 256.25 + zero(); // + forces double type feedback
Ben Murdoch257744e2011-11-30 15:57:28 +0000146 return array[0];
147}
148
149function test_store_high_double(array, sum) {
150 array[0] = 256.25;
151 return array[0];
152}
153
154function test_store_low_int(array, sum) {
155 array[0] = -1;
156 return array[0];
157}
158
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000159function test_store_low_tagged(array, sum) {
160 array[0] = -1.25;
161 return array[0];
162}
163
164function test_store_low_double(array, sum) {
165 array[0] = -1.25 + zero(); // + forces double type feedback
166 return array[0];
167}
168
Ben Murdoch257744e2011-11-30 15:57:28 +0000169function test_store_high_int(array, sum) {
170 array[0] = 256;
171 return array[0];
172}
173
174function test_store_nan(array, sum) {
175 array[0] = NaN;
176 return array[0];
177}
178
179const kRuns = 10;
180
181function run_test(test_func, array, expected_result) {
182 for (var i = 0; i < 5; i++) test_func(array, 0);
183 %OptimizeFunctionOnNextCall(test_func);
184 var sum = 0;
185 for (var i = 0; i < kRuns; i++) {
186 sum = test_func(array, sum);
187 }
188 assertEquals(expected_result, sum);
189 %DeoptimizeFunction(test_func);
190 gc(); // Makes V8 forget about type information for test_func.
191}
192
193for (var t = 0; t < types.length; t++) {
194 var type = types[t];
Ben Murdoch257744e2011-11-30 15:57:28 +0000195 var a = new type(kElementCount);
196 for (var i = 0; i < kElementCount; i++) {
197 a[i] = i;
198 }
199
200 // Run test functions defined above.
201 run_test(test_load, a, 780 * kRuns);
202 run_test(test_load_const_key, a, 3 * kRuns);
203 run_test(test_store, a, 820 * kRuns);
204 run_test(test_store_const_key, a, 6 * kRuns);
205 run_test(test_store_low_int, a, test_result_low_int[t]);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000206 run_test(test_store_low_double, a, test_result_low_double[t]);
207 run_test(test_store_low_tagged, a, test_result_low_double[t]);
Ben Murdoch257744e2011-11-30 15:57:28 +0000208 run_test(test_store_high_int, a, test_result_high_int[t]);
209 run_test(test_store_nan, a, test_result_nan[t]);
210 run_test(test_store_middle_double, a, test_result_middle[t]);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000211 run_test(test_store_middle_tagged, a, test_result_middle[t]);
212 run_test(test_store_high_double, a, test_result_high_double[t]);
213 run_test(test_store_high_tagged, a, test_result_high_double[t]);
Ben Murdoch257744e2011-11-30 15:57:28 +0000214
215 // Test the correct behavior of the |length| property (which is read-only).
216 if (t != 0) {
217 assertEquals(kElementCount, a.length);
218 a.length = 2;
219 assertEquals(kElementCount, a.length);
220 assertTrue(delete a.length);
221 a.length = 2;
222 assertEquals(2, a.length);
223 }
224
225 function array_load_set_smi_check(a) {
226 return a[0] = a[0] = 1;
227 }
228
229 array_load_set_smi_check(a);
230 array_load_set_smi_check(0);
231
232 function array_load_set_smi_check2(a) {
233 return a[0] = a[0] = 1;
234 }
235
236 array_load_set_smi_check2(a);
237 %OptimizeFunctionOnNextCall(array_load_set_smi_check2);
238 array_load_set_smi_check2(a);
239 array_load_set_smi_check2(0);
240 %DeoptimizeFunction(array_load_set_smi_check2);
241 gc(); // Makes V8 forget about type information for array_load_set_smi_check.
242}