Home | History | Annotate | Download | only in mjsunit
      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   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 
     55 function 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 
    118 function assertKind(expected, obj, name_opt) {
    119   assertEquals(expected, getKind(obj), name_opt);
    120 }
    121 
    122 var me = {};
    123 assertKind(elements_kind.fast, me);
    124 me.dance = 0xD15C0;
    125 me.drink = 0xC0C0A;
    126 assertKind(elements_kind.fast, me);
    127 
    128 var too = [1,2,3];
    129 assertKind(elements_kind.fast_smi_only, too);
    130 too.dance = 0xD15C0;
    131 too.drink = 0xC0C0A;
    132 assertKind(elements_kind.fast_smi_only, too);
    133 
    134 // Make sure the element kind transitions from smi when a non-smi is stored.
    135 function 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.
    198 test_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.
    205 var __sequence = 0;
    206 function make_array_string() {
    207   this.__sequence = this.__sequence + 1;
    208   return "/* " + this.__sequence + " */  [0, 0, 0];"
    209 }
    210 function make_array() {
    211   return eval(make_array_string());
    212 }
    213 
    214 function 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);
    221 function 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);
    228 function 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);
    237 function convert_to_double(array) {
    238   array[1] = 2.5;
    239   assertKind(elements_kind.fast_double, array);
    240   assertEquals(2.5, array[1]);
    241 }
    242 var smis = construct_smis();
    243 for (var i = 0; i < 3; i++) convert_to_double(smis);
    244   %OptimizeFunctionOnNextCall(convert_to_double);
    245 smis = construct_smis();
    246 convert_to_double(smis);
    247 // Test crankshafted transitions SMI->FAST and DOUBLE->FAST.
    248   %NeverOptimizeFunction(convert_to_fast);
    249 function convert_to_fast(array) {
    250   array[1] = "two";
    251   assertKind(elements_kind.fast, array);
    252   assertEquals("two", array[1]);
    253 }
    254 smis = construct_smis();
    255 for (var i = 0; i < 3; i++) convert_to_fast(smis);
    256 var doubles = construct_doubles();
    257 for (var i = 0; i < 3; i++) convert_to_fast(doubles);
    258 smis = construct_smis();
    259 doubles = construct_doubles();
    260   %OptimizeFunctionOnNextCall(convert_to_fast);
    261 convert_to_fast(smis);
    262 convert_to_fast(doubles);
    263 // Test transition chain SMI->DOUBLE->FAST (crankshafted function will
    264 // transition to FAST directly).
    265   %NeverOptimizeFunction(convert_mixed);
    266 function convert_mixed(array, value, kind) {
    267   array[1] = value;
    268   assertKind(kind, array);
    269   assertEquals(value, array[1]);
    270 }
    271 smis = construct_smis();
    272 for (var i = 0; i < 3; i++) {
    273   convert_mixed(smis, 1.5, elements_kind.fast_double);
    274 }
    275 doubles = construct_doubles();
    276 for (var i = 0; i < 3; i++) {
    277   convert_mixed(doubles, "three", elements_kind.fast);
    278 }
    279 convert_mixed(construct_smis(), "three", elements_kind.fast);
    280 convert_mixed(construct_doubles(), "three", elements_kind.fast);
    281   %OptimizeFunctionOnNextCall(convert_mixed);
    282 smis = construct_smis();
    283 doubles = construct_doubles();
    284 convert_mixed(smis, 1, elements_kind.fast);
    285 convert_mixed(doubles, 1, elements_kind.fast);
    286 assertTrue(%HaveSameMap(smis, doubles));
    287 
    288 // Crankshaft support for smi-only elements in dynamic array literals.
    289 function get(foo) { return foo; }  // Used to generate dynamic values.
    290 
    291 function 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 }
    302 for (var i = 0; i < 3; i++) {
    303   crankshaft_test();
    304 }
    305 %OptimizeFunctionOnNextCall(crankshaft_test);
    306 crankshaft_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.
    315 var a = [1, 2, 3];
    316 var b = [1, 2, 3];
    317 assertTrue(%HaveSameMap(a, b));
    318 assertKind(elements_kind.fast_smi_only, a);
    319 var c = [1, 2, 3];
    320 c["case2"] = true;
    321 var d = [1, 2, 3];
    322 d["case2"] = true;
    323 assertTrue(%HaveSameMap(c, d));
    324 assertFalse(%HaveSameMap(a, c));
    325 assertKind(elements_kind.fast_smi_only, c);
    326 var e = [1, 2, 3];
    327 e["case3"] = true;
    328 var f = [1, 2, 3];
    329 f["case3"] = true;
    330 assertTrue(%HaveSameMap(e, f));
    331 assertFalse(%HaveSameMap(a, e));
    332 assertFalse(%HaveSameMap(c, e));
    333 assertKind(elements_kind.fast_smi_only, e);
    334 // Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT.
    335 a[0] = 1.5;
    336 assertKind(elements_kind.fast_double, a);
    337 a[0] = "foo";
    338 assertKind(elements_kind.fast, a);
    339 b[0] = "bar";
    340 assertTrue(%HaveSameMap(a, b));
    341 // Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT.
    342 c[0] = 1.5;
    343 assertKind(elements_kind.fast_double, c);
    344 assertFalse(%HaveSameMap(c, d));
    345 d[0] = "foo";
    346 assertKind(elements_kind.fast, d);
    347 assertFalse(%HaveSameMap(c, d));
    348 c[0] = "bar";
    349 assertTrue(%HaveSameMap(c, d));
    350 // Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT.
    351 e[0] = "foo";
    352 assertKind(elements_kind.fast, e);
    353 assertFalse(%HaveSameMap(e, f));
    354 f[0] = 1.5;
    355 assertKind(elements_kind.fast_double, f);
    356 assertFalse(%HaveSameMap(e, f));
    357 f[0] = "bar";
    358 assertKind(elements_kind.fast, f);
    359 assertTrue(%HaveSameMap(e, f));
    360 
    361 // Test if Array.concat() works correctly with DOUBLE elements.
    362 var a = [1, 2];
    363 assertKind(elements_kind.fast_smi_only, a);
    364 var b = [4.5, 5.5];
    365 assertKind(elements_kind.fast_double, b);
    366 var c = a.concat(b);
    367 assertEquals([1, 2, 4.5, 5.5], c);
    368 assertKind(elements_kind.fast_double, c);
    369 
    370 // Test that Array.push() correctly handles SMI elements.
    371 var a = [1, 2];
    372 assertKind(elements_kind.fast_smi_only, a);
    373 a.push(3, 4, 5);
    374 assertKind(elements_kind.fast_smi_only, a);
    375 assertEquals([1, 2, 3, 4, 5], a);
    376 
    377 // Test that Array.splice() and Array.slice() return correct ElementsKinds.
    378 var a = ["foo", "bar"];
    379 assertKind(elements_kind.fast, a);
    380 var b = a.splice(0, 1);
    381 assertKind(elements_kind.fast, b);
    382 var c = a.slice(0, 1);
    383 assertKind(elements_kind.fast, c);
    384 
    385 // Throw away type information in the ICs for next stress run.
    386 gc();
    387