1 // Copyright 2012 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 --expose-gc --nostress-opt --typed-array-max-size-in-heap=2048 29 30 var elements_kind = { 31 fast_smi_only : 'fast smi only elements', 32 fast : 'fast elements', 33 fast_double : 'fast double elements', 34 dictionary : 'dictionary elements', 35 fixed_int32 : 'fixed int8 elements', 36 fixed_uint8 : 'fixed uint8 elements', 37 fixed_int16 : 'fixed int16 elements', 38 fixed_uint16 : 'fixed uint16 elements', 39 fixed_int32 : 'fixed int32 elements', 40 fixed_uint32 : 'fixed uint32 elements', 41 fixed_float32 : 'fixed float32 elements', 42 fixed_float64 : 'fixed float64 elements', 43 fixed_uint8_clamped : 'fixed uint8_clamped elements' 44 } 45 46 function getKind(obj) { 47 if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only; 48 if (%HasFastObjectElements(obj)) return elements_kind.fast; 49 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; 50 if (%HasDictionaryElements(obj)) return elements_kind.dictionary; 51 52 if (%HasFixedInt8Elements(obj)) { 53 return elements_kind.fixed_int8; 54 } 55 if (%HasFixedUint8Elements(obj)) { 56 return elements_kind.fixed_uint8; 57 } 58 if (%HasFixedInt16Elements(obj)) { 59 return elements_kind.fixed_int16; 60 } 61 if (%HasFixedUint16Elements(obj)) { 62 return elements_kind.fixed_uint16; 63 } 64 if (%HasFixedInt32Elements(obj)) { 65 return elements_kind.fixed_int32; 66 } 67 if (%HasFixedUint32Elements(obj)) { 68 return elements_kind.fixed_uint32; 69 } 70 if (%HasFixedFloat32Elements(obj)) { 71 return elements_kind.fixed_float32; 72 } 73 if (%HasFixedFloat64Elements(obj)) { 74 return elements_kind.fixed_float64; 75 } 76 if (%HasFixedUint8ClampedElements(obj)) { 77 return elements_kind.fixed_uint8_clamped; 78 } 79 } 80 81 function assertKind(expected, obj, name_opt) { 82 assertEquals(expected, getKind(obj), name_opt); 83 } 84 85 var me = {}; 86 assertKind(elements_kind.fast, me); 87 me.dance = 0xD15C0; 88 me.drink = 0xC0C0A; 89 assertKind(elements_kind.fast, me); 90 91 var too = [1,2,3]; 92 assertKind(elements_kind.fast_smi_only, too); 93 too.dance = 0xD15C0; 94 too.drink = 0xC0C0A; 95 assertKind(elements_kind.fast_smi_only, too); 96 97 // Make sure the element kind transitions from smi when a non-smi is stored. 98 function test_wrapper() { 99 var you = new Array(); 100 assertKind(elements_kind.fast_smi_only, you); 101 for (var i = 0; i < 1337; i++) { 102 var val = i; 103 if (i == 1336) { 104 assertKind(elements_kind.fast_smi_only, you); 105 val = new Object(); 106 } 107 you[i] = val; 108 } 109 assertKind(elements_kind.fast, you); 110 111 var temp = []; 112 temp[0xDECAF] = 0; 113 assertKind(elements_kind.dictionary, temp); 114 115 var fast_double_array = new Array(0xDECAF); 116 for (var i = 0; i < 0xDECAF; i++) fast_double_array[i] = i / 2; 117 assertKind(elements_kind.fast_double, fast_double_array); 118 119 assertKind(elements_kind.fixed_int8, new Int8Array(007)); 120 assertKind(elements_kind.fixed_uint8, new Uint8Array(007)); 121 assertKind(elements_kind.fixed_int16, new Int16Array(666)); 122 assertKind(elements_kind.fixed_uint16, new Uint16Array(42)); 123 assertKind(elements_kind.fixed_int32, new Int32Array(0xF)); 124 assertKind(elements_kind.fixed_uint32, new Uint32Array(23)); 125 assertKind(elements_kind.fixed_float32, new Float32Array(7)); 126 assertKind(elements_kind.fixed_float64, new Float64Array(0)); 127 assertKind(elements_kind.fixed_uint8_clamped, new Uint8ClampedArray(512)); 128 129 var ab = new ArrayBuffer(128); 130 assertKind(elements_kind.fixed_int8, new Int8Array(ab)); 131 assertKind(elements_kind.fixed_uint8, new Uint8Array(ab)); 132 assertKind(elements_kind.fixed_int16, new Int16Array(ab)); 133 assertKind(elements_kind.fixed_uint16, new Uint16Array(ab)); 134 assertKind(elements_kind.fixed_int32, new Int32Array(ab)); 135 assertKind(elements_kind.fixed_uint32, new Uint32Array(ab)); 136 assertKind(elements_kind.fixed_float32, new Float32Array(ab)); 137 assertKind(elements_kind.fixed_float64, new Float64Array(ab)); 138 assertKind(elements_kind.fixed_uint8_clamped, new Uint8ClampedArray(ab)); 139 140 // Crankshaft support for smi-only array elements. 141 function monomorphic(array) { 142 assertKind(elements_kind.fast_smi_only, array); 143 for (var i = 0; i < 3; i++) { 144 array[i] = i + 10; 145 } 146 assertKind(elements_kind.fast_smi_only, array); 147 for (var i = 0; i < 3; i++) { 148 var a = array[i]; 149 assertEquals(i + 10, a); 150 } 151 } 152 var smi_only = new Array(1, 2, 3); 153 assertKind(elements_kind.fast_smi_only, smi_only); 154 for (var i = 0; i < 3; i++) monomorphic(smi_only); 155 %OptimizeFunctionOnNextCall(monomorphic); 156 monomorphic(smi_only); 157 } 158 159 // The test is called in a wrapper function to eliminate the transition learning 160 // feedback of AllocationSites. 161 test_wrapper(); 162 %ClearFunctionTypeFeedback(test_wrapper); 163 164 %NeverOptimizeFunction(construct_smis); 165 166 // This code exists to eliminate the learning influence of AllocationSites 167 // on the following tests. 168 var __sequence = 0; 169 function make_array_string() { 170 this.__sequence = this.__sequence + 1; 171 return "/* " + this.__sequence + " */ [0, 0, 0];" 172 } 173 function make_array() { 174 return eval(make_array_string()); 175 } 176 177 function construct_smis() { 178 var a = make_array(); 179 a[0] = 0; // Send the COW array map to the steak house. 180 assertKind(elements_kind.fast_smi_only, a); 181 return a; 182 } 183 %NeverOptimizeFunction(construct_doubles); 184 function construct_doubles() { 185 var a = construct_smis(); 186 a[0] = 1.5; 187 assertKind(elements_kind.fast_double, a); 188 return a; 189 } 190 %NeverOptimizeFunction(construct_objects); 191 function construct_objects() { 192 var a = construct_smis(); 193 a[0] = "one"; 194 assertKind(elements_kind.fast, a); 195 return a; 196 } 197 198 // Test crankshafted transition SMI->DOUBLE. 199 %NeverOptimizeFunction(convert_to_double); 200 function convert_to_double(array) { 201 array[1] = 2.5; 202 assertKind(elements_kind.fast_double, array); 203 assertEquals(2.5, array[1]); 204 } 205 var smis = construct_smis(); 206 for (var i = 0; i < 3; i++) convert_to_double(smis); 207 %OptimizeFunctionOnNextCall(convert_to_double); 208 smis = construct_smis(); 209 convert_to_double(smis); 210 // Test crankshafted transitions SMI->FAST and DOUBLE->FAST. 211 %NeverOptimizeFunction(convert_to_fast); 212 function convert_to_fast(array) { 213 array[1] = "two"; 214 assertKind(elements_kind.fast, array); 215 assertEquals("two", array[1]); 216 } 217 smis = construct_smis(); 218 for (var i = 0; i < 3; i++) convert_to_fast(smis); 219 var doubles = construct_doubles(); 220 for (var i = 0; i < 3; i++) convert_to_fast(doubles); 221 smis = construct_smis(); 222 doubles = construct_doubles(); 223 %OptimizeFunctionOnNextCall(convert_to_fast); 224 convert_to_fast(smis); 225 convert_to_fast(doubles); 226 // Test transition chain SMI->DOUBLE->FAST (crankshafted function will 227 // transition to FAST directly). 228 %NeverOptimizeFunction(convert_mixed); 229 function convert_mixed(array, value, kind) { 230 array[1] = value; 231 assertKind(kind, array); 232 assertEquals(value, array[1]); 233 } 234 smis = construct_smis(); 235 for (var i = 0; i < 3; i++) { 236 convert_mixed(smis, 1.5, elements_kind.fast_double); 237 } 238 doubles = construct_doubles(); 239 for (var i = 0; i < 3; i++) { 240 convert_mixed(doubles, "three", elements_kind.fast); 241 } 242 convert_mixed(construct_smis(), "three", elements_kind.fast); 243 convert_mixed(construct_doubles(), "three", elements_kind.fast); 244 %OptimizeFunctionOnNextCall(convert_mixed); 245 smis = construct_smis(); 246 doubles = construct_doubles(); 247 convert_mixed(smis, 1, elements_kind.fast); 248 convert_mixed(doubles, 1, elements_kind.fast); 249 assertTrue(%HaveSameMap(smis, doubles)); 250 251 // Crankshaft support for smi-only elements in dynamic array literals. 252 function get(foo) { return foo; } // Used to generate dynamic values. 253 254 function crankshaft_test() { 255 var a1 = [get(1), get(2), get(3)]; 256 assertKind(elements_kind.fast_smi_only, a1); 257 258 var a2 = new Array(get(1), get(2), get(3)); 259 assertKind(elements_kind.fast_smi_only, a2); 260 var b = [get(1), get(2), get("three")]; 261 assertKind(elements_kind.fast, b); 262 var c = [get(1), get(2), get(3.5)]; 263 assertKind(elements_kind.fast_double, c); 264 } 265 for (var i = 0; i < 3; i++) { 266 crankshaft_test(); 267 } 268 %OptimizeFunctionOnNextCall(crankshaft_test); 269 crankshaft_test(); 270 271 // Elements_kind transitions for arrays. 272 273 // A map can have three different elements_kind transitions: SMI->DOUBLE, 274 // DOUBLE->OBJECT, and SMI->OBJECT. No matter in which order these three are 275 // created, they must always end up with the same FAST map. 276 277 // Preparation: create one pair of identical objects for each case. 278 var a = [1, 2, 3]; 279 var b = [1, 2, 3]; 280 assertTrue(%HaveSameMap(a, b)); 281 assertKind(elements_kind.fast_smi_only, a); 282 var c = [1, 2, 3]; 283 c["case2"] = true; 284 var d = [1, 2, 3]; 285 d["case2"] = true; 286 assertTrue(%HaveSameMap(c, d)); 287 assertFalse(%HaveSameMap(a, c)); 288 assertKind(elements_kind.fast_smi_only, c); 289 var e = [1, 2, 3]; 290 e["case3"] = true; 291 var f = [1, 2, 3]; 292 f["case3"] = true; 293 assertTrue(%HaveSameMap(e, f)); 294 assertFalse(%HaveSameMap(a, e)); 295 assertFalse(%HaveSameMap(c, e)); 296 assertKind(elements_kind.fast_smi_only, e); 297 // Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT. 298 a[0] = 1.5; 299 assertKind(elements_kind.fast_double, a); 300 a[0] = "foo"; 301 assertKind(elements_kind.fast, a); 302 b[0] = "bar"; 303 assertTrue(%HaveSameMap(a, b)); 304 // Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT. 305 c[0] = 1.5; 306 assertKind(elements_kind.fast_double, c); 307 assertFalse(%HaveSameMap(c, d)); 308 d[0] = "foo"; 309 assertKind(elements_kind.fast, d); 310 assertFalse(%HaveSameMap(c, d)); 311 c[0] = "bar"; 312 assertTrue(%HaveSameMap(c, d)); 313 // Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT. 314 e[0] = "foo"; 315 assertKind(elements_kind.fast, e); 316 assertFalse(%HaveSameMap(e, f)); 317 f[0] = 1.5; 318 assertKind(elements_kind.fast_double, f); 319 assertFalse(%HaveSameMap(e, f)); 320 f[0] = "bar"; 321 assertKind(elements_kind.fast, f); 322 assertTrue(%HaveSameMap(e, f)); 323 324 // Test if Array.concat() works correctly with DOUBLE elements. 325 var a = [1, 2]; 326 assertKind(elements_kind.fast_smi_only, a); 327 var b = [4.5, 5.5]; 328 assertKind(elements_kind.fast_double, b); 329 var c = a.concat(b); 330 assertEquals([1, 2, 4.5, 5.5], c); 331 assertKind(elements_kind.fast_double, c); 332 333 // Test that Array.push() correctly handles SMI elements. 334 var a = [1, 2]; 335 assertKind(elements_kind.fast_smi_only, a); 336 a.push(3, 4, 5); 337 assertKind(elements_kind.fast_smi_only, a); 338 assertEquals([1, 2, 3, 4, 5], a); 339 340 // Test that Array.splice() and Array.slice() return correct ElementsKinds. 341 var a = ["foo", "bar"]; 342 assertKind(elements_kind.fast, a); 343 var b = a.splice(0, 1); 344 assertKind(elements_kind.fast, b); 345 var c = a.slice(0, 1); 346 assertKind(elements_kind.fast, c); 347 348 // Throw away type information in the ICs for next stress run. 349 gc(); 350