Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 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 | |
| 28 | // Flags: --allow-natives-syntax --smi-only-arrays --expose-gc |
| 29 | |
| 30 | // Test element kind of objects. |
| 31 | // Since --smi-only-arrays affects builtins, its default setting at compile |
| 32 | // time sticks if built with snapshot. If --smi-only-arrays is deactivated |
| 33 | // by default, only a no-snapshot build actually has smi-only arrays enabled |
| 34 | // in this test case. Depending on whether smi-only arrays are actually |
| 35 | // enabled, this test takes the appropriate code path to check smi-only arrays. |
| 36 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 37 | support_smi_only_arrays = %HasFastSmiOnlyElements([1,2,3,4,5,6,7,8,9,10]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 38 | |
| 39 | if (support_smi_only_arrays) { |
| 40 | print("Tests include smi-only arrays."); |
| 41 | } else { |
| 42 | print("Tests do NOT include smi-only arrays."); |
| 43 | } |
| 44 | |
| 45 | var elements_kind = { |
| 46 | fast_smi_only : 'fast smi only elements', |
| 47 | fast : 'fast elements', |
| 48 | fast_double : 'fast double elements', |
| 49 | dictionary : 'dictionary elements', |
| 50 | external_byte : 'external byte elements', |
| 51 | external_unsigned_byte : 'external unsigned byte elements', |
| 52 | external_short : 'external short elements', |
| 53 | external_unsigned_short : 'external unsigned short elements', |
| 54 | external_int : 'external int elements', |
| 55 | external_unsigned_int : 'external unsigned int elements', |
| 56 | external_float : 'external float elements', |
| 57 | external_double : 'external double elements', |
| 58 | external_pixel : 'external pixel elements' |
| 59 | } |
| 60 | |
| 61 | function getKind(obj) { |
| 62 | if (%HasFastSmiOnlyElements(obj)) return elements_kind.fast_smi_only; |
| 63 | if (%HasFastElements(obj)) return elements_kind.fast; |
| 64 | if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; |
| 65 | if (%HasDictionaryElements(obj)) return elements_kind.dictionary; |
| 66 | // Every external kind is also an external array. |
| 67 | assertTrue(%HasExternalArrayElements(obj)); |
| 68 | if (%HasExternalByteElements(obj)) { |
| 69 | return elements_kind.external_byte; |
| 70 | } |
| 71 | if (%HasExternalUnsignedByteElements(obj)) { |
| 72 | return elements_kind.external_unsigned_byte; |
| 73 | } |
| 74 | if (%HasExternalShortElements(obj)) { |
| 75 | return elements_kind.external_short; |
| 76 | } |
| 77 | if (%HasExternalUnsignedShortElements(obj)) { |
| 78 | return elements_kind.external_unsigned_short; |
| 79 | } |
| 80 | if (%HasExternalIntElements(obj)) { |
| 81 | return elements_kind.external_int; |
| 82 | } |
| 83 | if (%HasExternalUnsignedIntElements(obj)) { |
| 84 | return elements_kind.external_unsigned_int; |
| 85 | } |
| 86 | if (%HasExternalFloatElements(obj)) { |
| 87 | return elements_kind.external_float; |
| 88 | } |
| 89 | if (%HasExternalDoubleElements(obj)) { |
| 90 | return elements_kind.external_double; |
| 91 | } |
| 92 | if (%HasExternalPixelElements(obj)) { |
| 93 | return elements_kind.external_pixel; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | function assertKind(expected, obj, name_opt) { |
| 98 | if (!support_smi_only_arrays && |
| 99 | expected == elements_kind.fast_smi_only) { |
| 100 | expected = elements_kind.fast; |
| 101 | } |
| 102 | assertEquals(expected, getKind(obj), name_opt); |
| 103 | } |
| 104 | |
| 105 | var me = {}; |
| 106 | assertKind(elements_kind.fast, me); |
| 107 | me.dance = 0xD15C0; |
| 108 | me.drink = 0xC0C0A; |
| 109 | assertKind(elements_kind.fast, me); |
| 110 | |
| 111 | var too = [1,2,3]; |
| 112 | assertKind(elements_kind.fast_smi_only, too); |
| 113 | too.dance = 0xD15C0; |
| 114 | too.drink = 0xC0C0A; |
| 115 | assertKind(elements_kind.fast_smi_only, too); |
| 116 | |
| 117 | // Make sure the element kind transitions from smionly when a non-smi is stored. |
| 118 | var you = new Array(); |
| 119 | assertKind(elements_kind.fast_smi_only, you); |
| 120 | for (var i = 0; i < 1337; i++) { |
| 121 | var val = i; |
| 122 | if (i == 1336) { |
| 123 | assertKind(elements_kind.fast_smi_only, you); |
| 124 | val = new Object(); |
| 125 | } |
| 126 | you[i] = val; |
| 127 | } |
| 128 | assertKind(elements_kind.fast, you); |
| 129 | |
| 130 | assertKind(elements_kind.dictionary, new Array(0xDECAF)); |
| 131 | |
| 132 | var fast_double_array = new Array(0xDECAF); |
| 133 | for (var i = 0; i < 0xDECAF; i++) fast_double_array[i] = i / 2; |
| 134 | assertKind(elements_kind.fast_double, fast_double_array); |
| 135 | |
| 136 | assertKind(elements_kind.external_byte, new Int8Array(9001)); |
| 137 | assertKind(elements_kind.external_unsigned_byte, new Uint8Array(007)); |
| 138 | assertKind(elements_kind.external_short, new Int16Array(666)); |
| 139 | assertKind(elements_kind.external_unsigned_short, new Uint16Array(42)); |
| 140 | assertKind(elements_kind.external_int, new Int32Array(0xF)); |
| 141 | assertKind(elements_kind.external_unsigned_int, new Uint32Array(23)); |
| 142 | assertKind(elements_kind.external_float, new Float32Array(7)); |
| 143 | assertKind(elements_kind.external_double, new Float64Array(0)); |
| 144 | assertKind(elements_kind.external_pixel, new PixelArray(512)); |
| 145 | |
| 146 | // Crankshaft support for smi-only array elements. |
| 147 | function monomorphic(array) { |
| 148 | for (var i = 0; i < 3; i++) { |
| 149 | array[i] = i + 10; |
| 150 | } |
| 151 | assertKind(elements_kind.fast_smi_only, array); |
| 152 | for (var i = 0; i < 3; i++) { |
| 153 | var a = array[i]; |
| 154 | assertEquals(i + 10, a); |
| 155 | } |
| 156 | } |
| 157 | var smi_only = [1, 2, 3]; |
| 158 | for (var i = 0; i < 3; i++) monomorphic(smi_only); |
| 159 | %OptimizeFunctionOnNextCall(monomorphic); |
| 160 | monomorphic(smi_only); |
| 161 | |
| 162 | if (support_smi_only_arrays) { |
| 163 | function construct_smis() { |
| 164 | var a = [0, 0, 0]; |
| 165 | a[0] = 0; // Send the COW array map to the steak house. |
| 166 | assertKind(elements_kind.fast_smi_only, a); |
| 167 | return a; |
| 168 | } |
| 169 | function construct_doubles() { |
| 170 | var a = construct_smis(); |
| 171 | a[0] = 1.5; |
| 172 | assertKind(elements_kind.fast_double, a); |
| 173 | return a; |
| 174 | } |
| 175 | function construct_objects() { |
| 176 | var a = construct_smis(); |
| 177 | a[0] = "one"; |
| 178 | assertKind(elements_kind.fast, a); |
| 179 | return a; |
| 180 | } |
| 181 | |
| 182 | // Test crankshafted transition SMI->DOUBLE. |
| 183 | function convert_to_double(array) { |
| 184 | array[1] = 2.5; |
| 185 | assertKind(elements_kind.fast_double, array); |
| 186 | assertEquals(2.5, array[1]); |
| 187 | } |
| 188 | var smis = construct_smis(); |
| 189 | for (var i = 0; i < 3; i++) convert_to_double(smis); |
| 190 | %OptimizeFunctionOnNextCall(convert_to_double); |
| 191 | smis = construct_smis(); |
| 192 | convert_to_double(smis); |
| 193 | // Test crankshafted transitions SMI->FAST and DOUBLE->FAST. |
| 194 | function convert_to_fast(array) { |
| 195 | array[1] = "two"; |
| 196 | assertKind(elements_kind.fast, array); |
| 197 | assertEquals("two", array[1]); |
| 198 | } |
| 199 | smis = construct_smis(); |
| 200 | for (var i = 0; i < 3; i++) convert_to_fast(smis); |
| 201 | var doubles = construct_doubles(); |
| 202 | for (var i = 0; i < 3; i++) convert_to_fast(doubles); |
| 203 | smis = construct_smis(); |
| 204 | doubles = construct_doubles(); |
| 205 | %OptimizeFunctionOnNextCall(convert_to_fast); |
| 206 | convert_to_fast(smis); |
| 207 | convert_to_fast(doubles); |
| 208 | // Test transition chain SMI->DOUBLE->FAST (crankshafted function will |
| 209 | // transition to FAST directly). |
| 210 | function convert_mixed(array, value, kind) { |
| 211 | array[1] = value; |
| 212 | assertKind(kind, array); |
| 213 | assertEquals(value, array[1]); |
| 214 | } |
| 215 | smis = construct_smis(); |
| 216 | for (var i = 0; i < 3; i++) { |
| 217 | convert_mixed(smis, 1.5, elements_kind.fast_double); |
| 218 | } |
| 219 | doubles = construct_doubles(); |
| 220 | for (var i = 0; i < 3; i++) { |
| 221 | convert_mixed(doubles, "three", elements_kind.fast); |
| 222 | } |
| 223 | smis = construct_smis(); |
| 224 | doubles = construct_doubles(); |
| 225 | %OptimizeFunctionOnNextCall(convert_mixed); |
| 226 | convert_mixed(smis, 1, elements_kind.fast); |
| 227 | convert_mixed(doubles, 1, elements_kind.fast); |
| 228 | assertTrue(%HaveSameMap(smis, doubles)); |
| 229 | } |
| 230 | |
| 231 | // Crankshaft support for smi-only elements in dynamic array literals. |
| 232 | function get(foo) { return foo; } // Used to generate dynamic values. |
| 233 | |
| 234 | function crankshaft_test() { |
| 235 | var a = [get(1), get(2), get(3)]; |
| 236 | assertKind(elements_kind.fast_smi_only, a); |
| 237 | var b = [get(1), get(2), get("three")]; |
| 238 | assertKind(elements_kind.fast, b); |
| 239 | var c = [get(1), get(2), get(3.5)]; |
| 240 | if (support_smi_only_arrays) { |
| 241 | assertKind(elements_kind.fast_double, c); |
| 242 | } else { |
| 243 | assertKind(elements_kind.fast, c); |
| 244 | } |
| 245 | } |
| 246 | for (var i = 0; i < 3; i++) { |
| 247 | crankshaft_test(); |
| 248 | } |
| 249 | %OptimizeFunctionOnNextCall(crankshaft_test); |
| 250 | crankshaft_test(); |
| 251 | |
| 252 | // Elements_kind transitions for arrays. |
| 253 | |
| 254 | // A map can have three different elements_kind transitions: SMI->DOUBLE, |
| 255 | // DOUBLE->OBJECT, and SMI->OBJECT. No matter in which order these three are |
| 256 | // created, they must always end up with the same FAST map. |
| 257 | |
| 258 | // This test is meaningless without FAST_SMI_ONLY_ELEMENTS. |
| 259 | if (support_smi_only_arrays) { |
| 260 | // Preparation: create one pair of identical objects for each case. |
| 261 | var a = [1, 2, 3]; |
| 262 | var b = [1, 2, 3]; |
| 263 | assertTrue(%HaveSameMap(a, b)); |
| 264 | assertKind(elements_kind.fast_smi_only, a); |
| 265 | var c = [1, 2, 3]; |
| 266 | c["case2"] = true; |
| 267 | var d = [1, 2, 3]; |
| 268 | d["case2"] = true; |
| 269 | assertTrue(%HaveSameMap(c, d)); |
| 270 | assertFalse(%HaveSameMap(a, c)); |
| 271 | assertKind(elements_kind.fast_smi_only, c); |
| 272 | var e = [1, 2, 3]; |
| 273 | e["case3"] = true; |
| 274 | var f = [1, 2, 3]; |
| 275 | f["case3"] = true; |
| 276 | assertTrue(%HaveSameMap(e, f)); |
| 277 | assertFalse(%HaveSameMap(a, e)); |
| 278 | assertFalse(%HaveSameMap(c, e)); |
| 279 | assertKind(elements_kind.fast_smi_only, e); |
| 280 | // Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT. |
| 281 | a[0] = 1.5; |
| 282 | assertKind(elements_kind.fast_double, a); |
| 283 | a[0] = "foo"; |
| 284 | assertKind(elements_kind.fast, a); |
| 285 | b[0] = "bar"; |
| 286 | assertTrue(%HaveSameMap(a, b)); |
| 287 | // Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT. |
| 288 | c[0] = 1.5; |
| 289 | assertKind(elements_kind.fast_double, c); |
| 290 | assertFalse(%HaveSameMap(c, d)); |
| 291 | d[0] = "foo"; |
| 292 | assertKind(elements_kind.fast, d); |
| 293 | assertFalse(%HaveSameMap(c, d)); |
| 294 | c[0] = "bar"; |
| 295 | assertTrue(%HaveSameMap(c, d)); |
| 296 | // Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT. |
| 297 | e[0] = "foo"; |
| 298 | assertKind(elements_kind.fast, e); |
| 299 | assertFalse(%HaveSameMap(e, f)); |
| 300 | f[0] = 1.5; |
| 301 | assertKind(elements_kind.fast_double, f); |
| 302 | assertFalse(%HaveSameMap(e, f)); |
| 303 | f[0] = "bar"; |
| 304 | assertKind(elements_kind.fast, f); |
| 305 | assertTrue(%HaveSameMap(e, f)); |
| 306 | } |
| 307 | |
| 308 | // Test if Array.concat() works correctly with DOUBLE elements. |
| 309 | if (support_smi_only_arrays) { |
| 310 | var a = [1, 2]; |
| 311 | assertKind(elements_kind.fast_smi_only, a); |
| 312 | var b = [4.5, 5.5]; |
| 313 | assertKind(elements_kind.fast_double, b); |
| 314 | var c = a.concat(b); |
| 315 | assertEquals([1, 2, 4.5, 5.5], c); |
| 316 | // TODO(1810): Change implementation so that we get DOUBLE elements here? |
| 317 | assertKind(elements_kind.fast, c); |
| 318 | } |
| 319 | |
| 320 | // Test that Array.push() correctly handles SMI elements. |
| 321 | if (support_smi_only_arrays) { |
| 322 | var a = [1, 2]; |
| 323 | assertKind(elements_kind.fast_smi_only, a); |
| 324 | a.push(3, 4, 5); |
| 325 | assertKind(elements_kind.fast_smi_only, a); |
| 326 | assertEquals([1, 2, 3, 4, 5], a); |
| 327 | } |
| 328 | |
| 329 | // Test that Array.splice() and Array.slice() return correct ElementsKinds. |
| 330 | if (support_smi_only_arrays) { |
| 331 | var a = ["foo", "bar"]; |
| 332 | assertKind(elements_kind.fast, a); |
| 333 | var b = a.splice(0, 1); |
| 334 | assertKind(elements_kind.fast, b); |
| 335 | var c = a.slice(0, 1); |
| 336 | assertKind(elements_kind.fast, c); |
| 337 | } |
| 338 | |
| 339 | // Throw away type information in the ICs for next stress run. |
| 340 | gc(); |