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