1// Copyright 2013 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 43support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8)); 44 45if (support_smi_only_arrays) { 46 print("Tests include smi-only arrays."); 47} else { 48 print("Tests do NOT include smi-only arrays."); 49} 50 51var 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 67function 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 103function 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%NeverOptimizeFunction(construct_smis); 112function construct_smis() { 113 var a = [0, 0, 0]; 114 a[0] = 0; // Send the COW array map to the steak house. 115 assertKind(elements_kind.fast_smi_only, a); 116 return a; 117} 118 119%NeverOptimizeFunction(construct_doubles); 120function construct_doubles() { 121 var a = construct_smis(); 122 a[0] = 1.5; 123 assertKind(elements_kind.fast_double, a); 124 return a; 125} 126 127%NeverOptimizeFunction(convert_mixed); 128function convert_mixed(array, value, kind) { 129 array[1] = value; 130 assertKind(kind, array); 131 assertEquals(value, array[1]); 132} 133 134function test1() { 135 if (!support_smi_only_arrays) return; 136 137 // Test transition chain SMI->DOUBLE->FAST (crankshafted function will 138 // transition to FAST directly). 139 var smis = construct_smis(); 140 convert_mixed(smis, 1.5, elements_kind.fast_double); 141 142 var doubles = construct_doubles(); 143 convert_mixed(doubles, "three", elements_kind.fast); 144 145 convert_mixed(construct_smis(), "three", elements_kind.fast); 146 convert_mixed(construct_doubles(), "three", elements_kind.fast); 147 148 smis = construct_smis(); 149 doubles = construct_doubles(); 150 convert_mixed(smis, 1, elements_kind.fast); 151 convert_mixed(doubles, 1, elements_kind.fast); 152 assertTrue(%HaveSameMap(smis, doubles)); 153} 154 155test1(); 156gc(); // clear IC state 157test1(); 158gc(); // clear IC state 159%OptimizeFunctionOnNextCall(test1); 160test1(); 161gc(); // clear IC state 162