elements-kind.js 12.5 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --allow-natives-syntax --smi-only-arrays --expose-gc
29
// Flags: --notrack_allocation_sites
30

31 32 33 34
// Limit the number of stress runs to reduce polymorphism it defeats some of the
// assumptions made about how elements transitions work because transition stubs
// end up going generic.
// Flags: --stress-runs=2
35

36 37 38 39 40 41 42
// Test element kind of objects.
// Since --smi-only-arrays affects builtins, its default setting at compile
// time sticks if built with snapshot.  If --smi-only-arrays is deactivated
// by default, only a no-snapshot build actually has smi-only arrays enabled
// in this test case.  Depending on whether smi-only arrays are actually
// enabled, this test takes the appropriate code path to check smi-only arrays.

43
support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8));
44

45 46 47 48 49 50
if (support_smi_only_arrays) {
  print("Tests include smi-only arrays.");
} else {
  print("Tests do NOT include smi-only arrays.");
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64
var elements_kind = {
  fast_smi_only            :  'fast smi only elements',
  fast                     :  'fast elements',
  fast_double              :  'fast double elements',
  dictionary               :  'dictionary elements',
  external_byte            :  'external byte elements',
  external_unsigned_byte   :  'external unsigned byte elements',
  external_short           :  'external short elements',
  external_unsigned_short  :  'external unsigned short elements',
  external_int             :  'external int elements',
  external_unsigned_int    :  'external unsigned int elements',
  external_float           :  'external float elements',
  external_double          :  'external double elements',
  external_pixel           :  'external pixel elements'
65 66
}

67
function getKind(obj) {
68 69
  if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only;
  if (%HasFastObjectElements(obj)) return elements_kind.fast;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  if (%HasFastDoubleElements(obj)) return elements_kind.fast_double;
  if (%HasDictionaryElements(obj)) return elements_kind.dictionary;
  // Every external kind is also an external array.
  assertTrue(%HasExternalArrayElements(obj));
  if (%HasExternalByteElements(obj)) {
    return elements_kind.external_byte;
  }
  if (%HasExternalUnsignedByteElements(obj)) {
    return elements_kind.external_unsigned_byte;
  }
  if (%HasExternalShortElements(obj)) {
    return elements_kind.external_short;
  }
  if (%HasExternalUnsignedShortElements(obj)) {
    return elements_kind.external_unsigned_short;
  }
  if (%HasExternalIntElements(obj)) {
    return elements_kind.external_int;
  }
  if (%HasExternalUnsignedIntElements(obj)) {
    return elements_kind.external_unsigned_int;
  }
  if (%HasExternalFloatElements(obj)) {
    return elements_kind.external_float;
  }
  if (%HasExternalDoubleElements(obj)) {
    return elements_kind.external_double;
  }
  if (%HasExternalPixelElements(obj)) {
    return elements_kind.external_pixel;
100
  }
101 102 103 104 105 106 107 108
}

function assertKind(expected, obj, name_opt) {
  if (!support_smi_only_arrays &&
      expected == elements_kind.fast_smi_only) {
    expected = elements_kind.fast;
  }
  assertEquals(expected, getKind(obj), name_opt);
109 110 111
}

var me = {};
112
assertKind(elements_kind.fast, me);
113 114
me.dance = 0xD15C0;
me.drink = 0xC0C0A;
115
assertKind(elements_kind.fast, me);
116

117 118 119 120 121 122 123
if (support_smi_only_arrays) {
  var too = [1,2,3];
  assertKind(elements_kind.fast_smi_only, too);
  too.dance = 0xD15C0;
  too.drink = 0xC0C0A;
  assertKind(elements_kind.fast_smi_only, too);
}
124

125
// Make sure the element kind transitions from smi when a non-smi is stored.
126
var you = new Array();
127
assertKind(elements_kind.fast_smi_only, you);
128
for (var i = 0; i < 1337; i++) {
129 130
  var val = i;
  if (i == 1336) {
131
    assertKind(elements_kind.fast_smi_only, you);
132 133 134
    val = new Object();
  }
  you[i] = val;
135
}
136
assertKind(elements_kind.fast, you);
137

138
assertKind(elements_kind.dictionary, new Array(0xDECAF));
139

140 141
var fast_double_array = new Array(0xDECAF);
for (var i = 0; i < 0xDECAF; i++) fast_double_array[i] = i / 2;
142
assertKind(elements_kind.fast_double, fast_double_array);
143

144 145 146 147 148 149 150 151
assertKind(elements_kind.external_byte,           new Int8Array(9001));
assertKind(elements_kind.external_unsigned_byte,  new Uint8Array(007));
assertKind(elements_kind.external_short,          new Int16Array(666));
assertKind(elements_kind.external_unsigned_short, new Uint16Array(42));
assertKind(elements_kind.external_int,            new Int32Array(0xF));
assertKind(elements_kind.external_unsigned_int,   new Uint32Array(23));
assertKind(elements_kind.external_float,          new Float32Array(7));
assertKind(elements_kind.external_double,         new Float64Array(0));
152
assertKind(elements_kind.external_pixel,          new Uint8ClampedArray(512));
153 154 155

// Crankshaft support for smi-only array elements.
function monomorphic(array) {
156
  assertKind(elements_kind.fast_smi_only, array);
157 158 159
  for (var i = 0; i < 3; i++) {
    array[i] = i + 10;
  }
160
  assertKind(elements_kind.fast_smi_only, array);
161 162 163 164 165
  for (var i = 0; i < 3; i++) {
    var a = array[i];
    assertEquals(i + 10, a);
  }
}
166
var smi_only = new Array(1, 2, 3);
167
assertKind(elements_kind.fast_smi_only, smi_only);
168 169 170
for (var i = 0; i < 3; i++) monomorphic(smi_only);
%OptimizeFunctionOnNextCall(monomorphic);
monomorphic(smi_only);
171 172

if (support_smi_only_arrays) {
173
  %NeverOptimizeFunction(construct_smis);
174 175 176 177 178 179
  function construct_smis() {
    var a = [0, 0, 0];
    a[0] = 0;  // Send the COW array map to the steak house.
    assertKind(elements_kind.fast_smi_only, a);
    return a;
  }
180
  %NeverOptimizeFunction(construct_doubles);
181 182 183 184 185 186
  function construct_doubles() {
    var a = construct_smis();
    a[0] = 1.5;
    assertKind(elements_kind.fast_double, a);
    return a;
  }
187
  %NeverOptimizeFunction(construct_objects);
188 189 190 191 192 193 194 195
  function construct_objects() {
    var a = construct_smis();
    a[0] = "one";
    assertKind(elements_kind.fast, a);
    return a;
  }

  // Test crankshafted transition SMI->DOUBLE.
196
  %NeverOptimizeFunction(convert_to_double);
197 198 199 200 201 202 203 204 205 206 207
  function convert_to_double(array) {
    array[1] = 2.5;
    assertKind(elements_kind.fast_double, array);
    assertEquals(2.5, array[1]);
  }
  var smis = construct_smis();
  for (var i = 0; i < 3; i++) convert_to_double(smis);
  %OptimizeFunctionOnNextCall(convert_to_double);
  smis = construct_smis();
  convert_to_double(smis);
  // Test crankshafted transitions SMI->FAST and DOUBLE->FAST.
208
  %NeverOptimizeFunction(convert_to_fast);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  function convert_to_fast(array) {
    array[1] = "two";
    assertKind(elements_kind.fast, array);
    assertEquals("two", array[1]);
  }
  smis = construct_smis();
  for (var i = 0; i < 3; i++) convert_to_fast(smis);
  var doubles = construct_doubles();
  for (var i = 0; i < 3; i++) convert_to_fast(doubles);
  smis = construct_smis();
  doubles = construct_doubles();
  %OptimizeFunctionOnNextCall(convert_to_fast);
  convert_to_fast(smis);
  convert_to_fast(doubles);
  // Test transition chain SMI->DOUBLE->FAST (crankshafted function will
  // transition to FAST directly).
225
  %NeverOptimizeFunction(convert_mixed);
226 227 228 229 230 231 232 233 234 235 236 237 238
  function convert_mixed(array, value, kind) {
    array[1] = value;
    assertKind(kind, array);
    assertEquals(value, array[1]);
  }
  smis = construct_smis();
  for (var i = 0; i < 3; i++) {
    convert_mixed(smis, 1.5, elements_kind.fast_double);
  }
  doubles = construct_doubles();
  for (var i = 0; i < 3; i++) {
    convert_mixed(doubles, "three", elements_kind.fast);
  }
239 240 241
  convert_mixed(construct_smis(), "three", elements_kind.fast);
  convert_mixed(construct_doubles(), "three", elements_kind.fast);
  %OptimizeFunctionOnNextCall(convert_mixed);
242 243 244 245 246
  smis = construct_smis();
  doubles = construct_doubles();
  convert_mixed(smis, 1, elements_kind.fast);
  convert_mixed(doubles, 1, elements_kind.fast);
  assertTrue(%HaveSameMap(smis, doubles));
247
}
248 249 250 251 252

// Crankshaft support for smi-only elements in dynamic array literals.
function get(foo) { return foo; }  // Used to generate dynamic values.

function crankshaft_test() {
253 254 255 256 257 258
  if (support_smi_only_arrays) {
    var a1 = [get(1), get(2), get(3)];
    assertKind(elements_kind.fast_smi_only, a1);
  }
  var a2 = new Array(get(1), get(2), get(3));
  assertKind(elements_kind.fast_smi_only, a2);
259 260 261
  var b = [get(1), get(2), get("three")];
  assertKind(elements_kind.fast, b);
  var c = [get(1), get(2), get(3.5)];
262 263 264
  if (support_smi_only_arrays) {
    assertKind(elements_kind.fast_double, c);
  }
265 266
}
for (var i = 0; i < 3; i++) {
267
  crankshaft_test();
268
}
269 270
%OptimizeFunctionOnNextCall(crankshaft_test);
crankshaft_test();
271 272 273 274 275 276

// Elements_kind transitions for arrays.

// A map can have three different elements_kind transitions: SMI->DOUBLE,
// DOUBLE->OBJECT, and SMI->OBJECT. No matter in which order these three are
// created, they must always end up with the same FAST map.
277 278 279 280 281 282 283

// This test is meaningless without FAST_SMI_ONLY_ELEMENTS.
if (support_smi_only_arrays) {
  // Preparation: create one pair of identical objects for each case.
  var a = [1, 2, 3];
  var b = [1, 2, 3];
  assertTrue(%HaveSameMap(a, b));
284
  assertKind(elements_kind.fast_smi_only, a);
285 286 287 288 289 290
  var c = [1, 2, 3];
  c["case2"] = true;
  var d = [1, 2, 3];
  d["case2"] = true;
  assertTrue(%HaveSameMap(c, d));
  assertFalse(%HaveSameMap(a, c));
291
  assertKind(elements_kind.fast_smi_only, c);
292 293 294 295 296 297 298
  var e = [1, 2, 3];
  e["case3"] = true;
  var f = [1, 2, 3];
  f["case3"] = true;
  assertTrue(%HaveSameMap(e, f));
  assertFalse(%HaveSameMap(a, e));
  assertFalse(%HaveSameMap(c, e));
299
  assertKind(elements_kind.fast_smi_only, e);
300 301
  // Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT.
  a[0] = 1.5;
302
  assertKind(elements_kind.fast_double, a);
303
  a[0] = "foo";
304
  assertKind(elements_kind.fast, a);
305 306 307 308
  b[0] = "bar";
  assertTrue(%HaveSameMap(a, b));
  // Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT.
  c[0] = 1.5;
309
  assertKind(elements_kind.fast_double, c);
310 311
  assertFalse(%HaveSameMap(c, d));
  d[0] = "foo";
312
  assertKind(elements_kind.fast, d);
313 314 315 316 317
  assertFalse(%HaveSameMap(c, d));
  c[0] = "bar";
  assertTrue(%HaveSameMap(c, d));
  // Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT.
  e[0] = "foo";
318
  assertKind(elements_kind.fast, e);
319 320
  assertFalse(%HaveSameMap(e, f));
  f[0] = 1.5;
321
  assertKind(elements_kind.fast_double, f);
322 323
  assertFalse(%HaveSameMap(e, f));
  f[0] = "bar";
324
  assertKind(elements_kind.fast, f);
325 326
  assertTrue(%HaveSameMap(e, f));
}
327

328 329 330 331 332 333 334 335
// Test if Array.concat() works correctly with DOUBLE elements.
if (support_smi_only_arrays) {
  var a = [1, 2];
  assertKind(elements_kind.fast_smi_only, a);
  var b = [4.5, 5.5];
  assertKind(elements_kind.fast_double, b);
  var c = a.concat(b);
  assertEquals([1, 2, 4.5, 5.5], c);
336
  assertKind(elements_kind.fast_double, c);
337 338
}

339 340 341 342 343 344 345 346 347
// Test that Array.push() correctly handles SMI elements.
if (support_smi_only_arrays) {
  var a = [1, 2];
  assertKind(elements_kind.fast_smi_only, a);
  a.push(3, 4, 5);
  assertKind(elements_kind.fast_smi_only, a);
  assertEquals([1, 2, 3, 4, 5], a);
}

348 349 350 351 352 353 354 355 356 357
// Test that Array.splice() and Array.slice() return correct ElementsKinds.
if (support_smi_only_arrays) {
  var a = ["foo", "bar"];
  assertKind(elements_kind.fast, a);
  var b = a.splice(0, 1);
  assertKind(elements_kind.fast, b);
  var c = a.slice(0, 1);
  assertKind(elements_kind.fast, c);
}

358 359
// Throw away type information in the ICs for next stress run.
gc();