Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax --expose-gc |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 30 | // This is a regression test for overlapping key and value registers. |
| 31 | function f(a) { |
| 32 | a[0] = 0; |
| 33 | a[1] = 0; |
| 34 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 36 | var a = new Int32Array(2); |
| 37 | for (var i = 0; i < 5; i++) { |
| 38 | f(a); |
| 39 | } |
| 40 | %OptimizeFunctionOnNextCall(f); |
| 41 | f(a); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 43 | assertEquals(0, a[0]); |
| 44 | assertEquals(0, a[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 46 | // Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is |
| 47 | // "constant", but not read-only). |
| 48 | a = new Int32Array(2); |
| 49 | assertEquals(4, a.BYTES_PER_ELEMENT); |
| 50 | a.BYTES_PER_ELEMENT = 42; |
| 51 | assertEquals(42, a.BYTES_PER_ELEMENT); |
| 52 | a = new Uint8Array(2); |
| 53 | assertEquals(1, a.BYTES_PER_ELEMENT); |
| 54 | a = new Int16Array(2); |
| 55 | assertEquals(2, a.BYTES_PER_ELEMENT); |
| 56 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 57 | // Test Float64Arrays. |
| 58 | function get(a, index) { |
| 59 | return a[index]; |
| 60 | } |
| 61 | function set(a, index, value) { |
| 62 | a[index] = value; |
| 63 | } |
| 64 | function temp() { |
| 65 | var array = new Float64Array(2); |
| 66 | for (var i = 0; i < 5; i++) { |
| 67 | set(array, 0, 2.5); |
| 68 | assertEquals(2.5, array[0]); |
| 69 | } |
| 70 | %OptimizeFunctionOnNextCall(set); |
| 71 | set(array, 0, 2.5); |
| 72 | assertEquals(2.5, array[0]); |
| 73 | set(array, 1, 3.5); |
| 74 | assertEquals(3.5, array[1]); |
| 75 | for (var i = 0; i < 5; i++) { |
| 76 | assertEquals(2.5, get(array, 0)); |
| 77 | assertEquals(3.5, array[1]); |
| 78 | } |
| 79 | %OptimizeFunctionOnNextCall(get); |
| 80 | assertEquals(2.5, get(array, 0)); |
| 81 | assertEquals(3.5, get(array, 1)); |
| 82 | } |
| 83 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 84 | // Test non-number parameters. |
| 85 | var array_with_length_from_non_number = new Int32Array("2"); |
| 86 | assertEquals(2, array_with_length_from_non_number.length); |
| 87 | array_with_length_from_non_number = new Int32Array(undefined); |
| 88 | assertEquals(0, array_with_length_from_non_number.length); |
| 89 | var foo = { valueOf: function() { return 3; } }; |
| 90 | array_with_length_from_non_number = new Int32Array(foo); |
| 91 | assertEquals(3, array_with_length_from_non_number.length); |
| 92 | foo = { toString: function() { return "4"; } }; |
| 93 | array_with_length_from_non_number = new Int32Array(foo); |
| 94 | assertEquals(4, array_with_length_from_non_number.length); |
| 95 | |
| 96 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 97 | // Test loads and stores. |
| 98 | types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, |
| 99 | Uint32Array, PixelArray, Float32Array, Float64Array]; |
| 100 | |
| 101 | test_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN]; |
| 102 | test_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1]; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 103 | test_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -1.25]; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 104 | test_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75]; |
| 105 | test_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256]; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 106 | test_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25]; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 107 | |
| 108 | const kElementCount = 40; |
| 109 | |
| 110 | function test_load(array, sum) { |
| 111 | for (var i = 0; i < kElementCount; i++) { |
| 112 | sum += array[i]; |
| 113 | } |
| 114 | return sum; |
| 115 | } |
| 116 | |
| 117 | function test_load_const_key(array, sum) { |
| 118 | sum += array[0]; |
| 119 | sum += array[1]; |
| 120 | sum += array[2]; |
| 121 | return sum; |
| 122 | } |
| 123 | |
| 124 | function test_store(array, sum) { |
| 125 | for (var i = 0; i < kElementCount; i++) { |
| 126 | sum += array[i] = i+1; |
| 127 | } |
| 128 | return sum; |
| 129 | } |
| 130 | |
| 131 | function test_store_const_key(array, sum) { |
| 132 | sum += array[0] = 1; |
| 133 | sum += array[1] = 2; |
| 134 | sum += array[2] = 3; |
| 135 | return sum; |
| 136 | } |
| 137 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 138 | function zero() { |
| 139 | return 0.0; |
| 140 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 141 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 142 | function test_store_middle_tagged(array, sum) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 143 | array[0] = 253.75; |
| 144 | return array[0]; |
| 145 | } |
| 146 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 147 | function test_store_high_tagged(array, sum) { |
| 148 | array[0] = 256.25; |
| 149 | return array[0]; |
| 150 | } |
| 151 | |
| 152 | function test_store_middle_double(array, sum) { |
| 153 | array[0] = 253.75 + zero(); // + forces double type feedback |
| 154 | return array[0]; |
| 155 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 156 | |
| 157 | function test_store_high_double(array, sum) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 158 | array[0] = 256.25 + zero(); // + forces double type feedback |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 159 | return array[0]; |
| 160 | } |
| 161 | |
| 162 | function test_store_high_double(array, sum) { |
| 163 | array[0] = 256.25; |
| 164 | return array[0]; |
| 165 | } |
| 166 | |
| 167 | function test_store_low_int(array, sum) { |
| 168 | array[0] = -1; |
| 169 | return array[0]; |
| 170 | } |
| 171 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 172 | function test_store_low_tagged(array, sum) { |
| 173 | array[0] = -1.25; |
| 174 | return array[0]; |
| 175 | } |
| 176 | |
| 177 | function test_store_low_double(array, sum) { |
| 178 | array[0] = -1.25 + zero(); // + forces double type feedback |
| 179 | return array[0]; |
| 180 | } |
| 181 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 182 | function test_store_high_int(array, sum) { |
| 183 | array[0] = 256; |
| 184 | return array[0]; |
| 185 | } |
| 186 | |
| 187 | function test_store_nan(array, sum) { |
| 188 | array[0] = NaN; |
| 189 | return array[0]; |
| 190 | } |
| 191 | |
| 192 | const kRuns = 10; |
| 193 | |
| 194 | function run_test(test_func, array, expected_result) { |
| 195 | for (var i = 0; i < 5; i++) test_func(array, 0); |
| 196 | %OptimizeFunctionOnNextCall(test_func); |
| 197 | var sum = 0; |
| 198 | for (var i = 0; i < kRuns; i++) { |
| 199 | sum = test_func(array, sum); |
| 200 | } |
| 201 | assertEquals(expected_result, sum); |
| 202 | %DeoptimizeFunction(test_func); |
| 203 | gc(); // Makes V8 forget about type information for test_func. |
| 204 | } |
| 205 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 206 | function run_bounds_test(test_func, array, expected_result) { |
| 207 | assertEquals(undefined, a[kElementCount]); |
| 208 | a[kElementCount] = 456; |
| 209 | assertEquals(undefined, a[kElementCount]); |
| 210 | assertEquals(undefined, a[kElementCount+1]); |
| 211 | a[kElementCount+1] = 456; |
| 212 | assertEquals(undefined, a[kElementCount+1]); |
| 213 | } |
| 214 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 215 | for (var t = 0; t < types.length; t++) { |
| 216 | var type = types[t]; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 217 | var a = new type(kElementCount); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 218 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 219 | for (var i = 0; i < kElementCount; i++) { |
| 220 | a[i] = i; |
| 221 | } |
| 222 | |
| 223 | // Run test functions defined above. |
| 224 | run_test(test_load, a, 780 * kRuns); |
| 225 | run_test(test_load_const_key, a, 3 * kRuns); |
| 226 | run_test(test_store, a, 820 * kRuns); |
| 227 | run_test(test_store_const_key, a, 6 * kRuns); |
| 228 | run_test(test_store_low_int, a, test_result_low_int[t]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 229 | run_test(test_store_low_double, a, test_result_low_double[t]); |
| 230 | run_test(test_store_low_tagged, a, test_result_low_double[t]); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 231 | run_test(test_store_high_int, a, test_result_high_int[t]); |
| 232 | run_test(test_store_nan, a, test_result_nan[t]); |
| 233 | run_test(test_store_middle_double, a, test_result_middle[t]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 234 | run_test(test_store_middle_tagged, a, test_result_middle[t]); |
| 235 | run_test(test_store_high_double, a, test_result_high_double[t]); |
| 236 | run_test(test_store_high_tagged, a, test_result_high_double[t]); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 237 | |
| 238 | // Test the correct behavior of the |length| property (which is read-only). |
| 239 | if (t != 0) { |
| 240 | assertEquals(kElementCount, a.length); |
| 241 | a.length = 2; |
| 242 | assertEquals(kElementCount, a.length); |
| 243 | assertTrue(delete a.length); |
| 244 | a.length = 2; |
| 245 | assertEquals(2, a.length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 246 | |
| 247 | // Make sure bounds checks are handled correctly for external arrays. |
| 248 | run_bounds_test(a); |
| 249 | run_bounds_test(a); |
| 250 | run_bounds_test(a); |
| 251 | %OptimizeFunctionOnNextCall(run_bounds_test); |
| 252 | run_bounds_test(a); |
| 253 | %DeoptimizeFunction(run_bounds_test); |
| 254 | gc(); // Makes V8 forget about type information for test_func. |
| 255 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | function array_load_set_smi_check(a) { |
| 259 | return a[0] = a[0] = 1; |
| 260 | } |
| 261 | |
| 262 | array_load_set_smi_check(a); |
| 263 | array_load_set_smi_check(0); |
| 264 | |
| 265 | function array_load_set_smi_check2(a) { |
| 266 | return a[0] = a[0] = 1; |
| 267 | } |
| 268 | |
| 269 | array_load_set_smi_check2(a); |
| 270 | %OptimizeFunctionOnNextCall(array_load_set_smi_check2); |
| 271 | array_load_set_smi_check2(a); |
| 272 | array_load_set_smi_check2(0); |
| 273 | %DeoptimizeFunction(array_load_set_smi_check2); |
| 274 | gc(); // Makes V8 forget about type information for array_load_set_smi_check. |
| 275 | } |