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 30var 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 46function 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 81function assertKind(expected, obj, name_opt) { 82 assertEquals(expected, getKind(obj), name_opt); 83} 84 85var me = {}; 86assertKind(elements_kind.fast, me); 87me.dance = 0xD15C0; 88me.drink = 0xC0C0A; 89assertKind(elements_kind.fast, me); 90 91var too = [1,2,3]; 92assertKind(elements_kind.fast_smi_only, too); 93too.dance = 0xD15C0; 94too.drink = 0xC0C0A; 95assertKind(elements_kind.fast_smi_only, too); 96 97// Make sure the element kind transitions from smi when a non-smi is stored. 98function 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. 161test_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. 168var __sequence = 0; 169function make_array_string() { 170 this.__sequence = this.__sequence + 1; 171 return "/* " + this.__sequence + " */ [0, 0, 0];" 172} 173function make_array() { 174 return eval(make_array_string()); 175} 176 177function 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); 184function 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); 191function 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); 200function convert_to_double(array) { 201 array[1] = 2.5; 202 assertKind(elements_kind.fast_double, array); 203 assertEquals(2.5, array[1]); 204} 205var smis = construct_smis(); 206for (var i = 0; i < 3; i++) convert_to_double(smis); 207 %OptimizeFunctionOnNextCall(convert_to_double); 208smis = construct_smis(); 209convert_to_double(smis); 210// Test crankshafted transitions SMI->FAST and DOUBLE->FAST. 211 %NeverOptimizeFunction(convert_to_fast); 212function convert_to_fast(array) { 213 array[1] = "two"; 214 assertKind(elements_kind.fast, array); 215 assertEquals("two", array[1]); 216} 217smis = construct_smis(); 218for (var i = 0; i < 3; i++) convert_to_fast(smis); 219var doubles = construct_doubles(); 220for (var i = 0; i < 3; i++) convert_to_fast(doubles); 221smis = construct_smis(); 222doubles = construct_doubles(); 223 %OptimizeFunctionOnNextCall(convert_to_fast); 224convert_to_fast(smis); 225convert_to_fast(doubles); 226// Test transition chain SMI->DOUBLE->FAST (crankshafted function will 227// transition to FAST directly). 228 %NeverOptimizeFunction(convert_mixed); 229function convert_mixed(array, value, kind) { 230 array[1] = value; 231 assertKind(kind, array); 232 assertEquals(value, array[1]); 233} 234smis = construct_smis(); 235for (var i = 0; i < 3; i++) { 236 convert_mixed(smis, 1.5, elements_kind.fast_double); 237} 238doubles = construct_doubles(); 239for (var i = 0; i < 3; i++) { 240 convert_mixed(doubles, "three", elements_kind.fast); 241} 242convert_mixed(construct_smis(), "three", elements_kind.fast); 243convert_mixed(construct_doubles(), "three", elements_kind.fast); 244 %OptimizeFunctionOnNextCall(convert_mixed); 245smis = construct_smis(); 246doubles = construct_doubles(); 247convert_mixed(smis, 1, elements_kind.fast); 248convert_mixed(doubles, 1, elements_kind.fast); 249assertTrue(%HaveSameMap(smis, doubles)); 250 251// Crankshaft support for smi-only elements in dynamic array literals. 252function get(foo) { return foo; } // Used to generate dynamic values. 253 254function 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} 265for (var i = 0; i < 3; i++) { 266 crankshaft_test(); 267} 268%OptimizeFunctionOnNextCall(crankshaft_test); 269crankshaft_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. 278var a = [1, 2, 3]; 279var b = [1, 2, 3]; 280assertTrue(%HaveSameMap(a, b)); 281assertKind(elements_kind.fast_smi_only, a); 282var c = [1, 2, 3]; 283c["case2"] = true; 284var d = [1, 2, 3]; 285d["case2"] = true; 286assertTrue(%HaveSameMap(c, d)); 287assertFalse(%HaveSameMap(a, c)); 288assertKind(elements_kind.fast_smi_only, c); 289var e = [1, 2, 3]; 290e["case3"] = true; 291var f = [1, 2, 3]; 292f["case3"] = true; 293assertTrue(%HaveSameMap(e, f)); 294assertFalse(%HaveSameMap(a, e)); 295assertFalse(%HaveSameMap(c, e)); 296assertKind(elements_kind.fast_smi_only, e); 297// Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT. 298a[0] = 1.5; 299assertKind(elements_kind.fast_double, a); 300a[0] = "foo"; 301assertKind(elements_kind.fast, a); 302b[0] = "bar"; 303assertTrue(%HaveSameMap(a, b)); 304// Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT. 305c[0] = 1.5; 306assertKind(elements_kind.fast_double, c); 307assertFalse(%HaveSameMap(c, d)); 308d[0] = "foo"; 309assertKind(elements_kind.fast, d); 310assertFalse(%HaveSameMap(c, d)); 311c[0] = "bar"; 312assertTrue(%HaveSameMap(c, d)); 313// Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT. 314e[0] = "foo"; 315assertKind(elements_kind.fast, e); 316assertFalse(%HaveSameMap(e, f)); 317f[0] = 1.5; 318assertKind(elements_kind.fast_double, f); 319assertFalse(%HaveSameMap(e, f)); 320f[0] = "bar"; 321assertKind(elements_kind.fast, f); 322assertTrue(%HaveSameMap(e, f)); 323 324// Test if Array.concat() works correctly with DOUBLE elements. 325var a = [1, 2]; 326assertKind(elements_kind.fast_smi_only, a); 327var b = [4.5, 5.5]; 328assertKind(elements_kind.fast_double, b); 329var c = a.concat(b); 330assertEquals([1, 2, 4.5, 5.5], c); 331assertKind(elements_kind.fast_double, c); 332 333// Test that Array.push() correctly handles SMI elements. 334var a = [1, 2]; 335assertKind(elements_kind.fast_smi_only, a); 336a.push(3, 4, 5); 337assertKind(elements_kind.fast_smi_only, a); 338assertEquals([1, 2, 3, 4, 5], a); 339 340// Test that Array.splice() and Array.slice() return correct ElementsKinds. 341var a = ["foo", "bar"]; 342assertKind(elements_kind.fast, a); 343var b = a.splice(0, 1); 344assertKind(elements_kind.fast, b); 345var c = a.slice(0, 1); 346assertKind(elements_kind.fast, c); 347 348// Throw away type information in the ICs for next stress run. 349gc(); 350