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 --smi-only-arrays --expose-gc
     29 // Flags: --notrack_allocation_sites
     30 
     31 // Limit the number of stress runs to reduce polymorphism it defeats some of the
     32 // assumptions made about how elements transitions work because transition stubs
     33 // end up going generic.
     34 // Flags: --stress-runs=2
     35 
     36 // Test element kind of objects.
     37 // Since --smi-only-arrays affects builtins, its default setting at compile
     38 // time sticks if built with snapshot.  If --smi-only-arrays is deactivated
     39 // by default, only a no-snapshot build actually has smi-only arrays enabled
     40 // in this test case.  Depending on whether smi-only arrays are actually
     41 // enabled, this test takes the appropriate code path to check smi-only arrays.
     42 
     43 support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8));
     44 
     45 if (support_smi_only_arrays) {
     46   print("Tests include smi-only arrays.");
     47 } else {
     48   print("Tests do NOT include smi-only arrays.");
     49 }
     50 
     51 var elements_kind = {
     52   fast_smi_only            :  'fast smi only elements',
     53   fast                     :  'fast elements',
     54   fast_double              :  'fast double elements',
     55   dictionary               :  'dictionary elements',
     56   external_byte            :  'external byte elements',
     57   external_unsigned_byte   :  'external unsigned byte elements',
     58   external_short           :  'external short elements',
     59   external_unsigned_short  :  'external unsigned short elements',
     60   external_int             :  'external int elements',
     61   external_unsigned_int    :  'external unsigned int elements',
     62   external_float           :  'external float elements',
     63   external_double          :  'external double elements',
     64   external_pixel           :  'external pixel elements'
     65 }
     66 
     67 function getKind(obj) {
     68   if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only;
     69   if (%HasFastObjectElements(obj)) return elements_kind.fast;
     70   if (%HasFastDoubleElements(obj)) return elements_kind.fast_double;
     71   if (%HasDictionaryElements(obj)) return elements_kind.dictionary;
     72   // Every external kind is also an external array.
     73   assertTrue(%HasExternalArrayElements(obj));
     74   if (%HasExternalByteElements(obj)) {
     75     return elements_kind.external_byte;
     76   }
     77   if (%HasExternalUnsignedByteElements(obj)) {
     78     return elements_kind.external_unsigned_byte;
     79   }
     80   if (%HasExternalShortElements(obj)) {
     81     return elements_kind.external_short;
     82   }
     83   if (%HasExternalUnsignedShortElements(obj)) {
     84     return elements_kind.external_unsigned_short;
     85   }
     86   if (%HasExternalIntElements(obj)) {
     87     return elements_kind.external_int;
     88   }
     89   if (%HasExternalUnsignedIntElements(obj)) {
     90     return elements_kind.external_unsigned_int;
     91   }
     92   if (%HasExternalFloatElements(obj)) {
     93     return elements_kind.external_float;
     94   }
     95   if (%HasExternalDoubleElements(obj)) {
     96     return elements_kind.external_double;
     97   }
     98   if (%HasExternalPixelElements(obj)) {
     99     return elements_kind.external_pixel;
    100   }
    101 }
    102 
    103 function assertKind(expected, obj, name_opt) {
    104   if (!support_smi_only_arrays &&
    105       expected == elements_kind.fast_smi_only) {
    106     expected = elements_kind.fast;
    107   }
    108   assertEquals(expected, getKind(obj), name_opt);
    109 }
    110 
    111 // long-running loop forces OSR.
    112 %NeverOptimizeFunction(construct_smis);
    113 %NeverOptimizeFunction(construct_doubles);
    114 %NeverOptimizeFunction(convert_mixed);
    115 for (var i = 0; i < 1000000; i++) { }
    116 
    117 if (support_smi_only_arrays) {
    118   function construct_smis() {
    119     var a = [0, 0, 0];
    120     a[0] = 0;  // Send the COW array map to the steak house.
    121     assertKind(elements_kind.fast_smi_only, a);
    122     return a;
    123   }
    124   function construct_doubles() {
    125     var a = construct_smis();
    126     a[0] = 1.5;
    127     assertKind(elements_kind.fast_double, a);
    128     return a;
    129   }
    130 
    131   // Test transition chain SMI->DOUBLE->FAST (crankshafted function will
    132   // transition to FAST directly).
    133   function convert_mixed(array, value, kind) {
    134     array[1] = value;
    135     assertKind(kind, array);
    136     assertEquals(value, array[1]);
    137   }
    138   smis = construct_smis();
    139   convert_mixed(smis, 1.5, elements_kind.fast_double);
    140 
    141   doubles = construct_doubles();
    142   convert_mixed(doubles, "three", elements_kind.fast);
    143 
    144   convert_mixed(construct_smis(), "three", elements_kind.fast);
    145   convert_mixed(construct_doubles(), "three", elements_kind.fast);
    146 
    147   smis = construct_smis();
    148   doubles = construct_doubles();
    149   convert_mixed(smis, 1, elements_kind.fast);
    150   convert_mixed(doubles, 1, elements_kind.fast);
    151   assertTrue(%HaveSameMap(smis, doubles));
    152 }
    153 
    154 // Throw away type information in the ICs for next stress run.
    155 gc();
    156