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 43// Reset the GC stress mode to be off. Needed because AllocationMementos only 44// live for one gc, so a gc that happens in certain fragile areas of the test 45// can break assumptions. 46%SetFlags("--gc-interval=-1") 47 48support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8)); 49 50if (support_smi_only_arrays) { 51 print("Tests include smi-only arrays."); 52} else { 53 print("Tests do NOT include smi-only arrays."); 54} 55 56var elements_kind = { 57 fast_smi_only : 'fast smi only elements', 58 fast : 'fast elements', 59 fast_double : 'fast double elements', 60 dictionary : 'dictionary elements', 61 external_byte : 'external byte elements', 62 external_unsigned_byte : 'external unsigned byte elements', 63 external_short : 'external short elements', 64 external_unsigned_short : 'external unsigned short elements', 65 external_int : 'external int elements', 66 external_unsigned_int : 'external unsigned int elements', 67 external_float : 'external float elements', 68 external_double : 'external double elements', 69 external_pixel : 'external pixel elements' 70} 71 72function getKind(obj) { 73 if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only; 74 if (%HasFastObjectElements(obj)) return elements_kind.fast; 75 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; 76 if (%HasDictionaryElements(obj)) return elements_kind.dictionary; 77 // Every external kind is also an external array. 78 assertTrue(%HasExternalArrayElements(obj)); 79 if (%HasExternalByteElements(obj)) { 80 return elements_kind.external_byte; 81 } 82 if (%HasExternalUnsignedByteElements(obj)) { 83 return elements_kind.external_unsigned_byte; 84 } 85 if (%HasExternalShortElements(obj)) { 86 return elements_kind.external_short; 87 } 88 if (%HasExternalUnsignedShortElements(obj)) { 89 return elements_kind.external_unsigned_short; 90 } 91 if (%HasExternalIntElements(obj)) { 92 return elements_kind.external_int; 93 } 94 if (%HasExternalUnsignedIntElements(obj)) { 95 return elements_kind.external_unsigned_int; 96 } 97 if (%HasExternalFloatElements(obj)) { 98 return elements_kind.external_float; 99 } 100 if (%HasExternalDoubleElements(obj)) { 101 return elements_kind.external_double; 102 } 103 if (%HasExternalPixelElements(obj)) { 104 return elements_kind.external_pixel; 105 } 106} 107 108function assertKind(expected, obj, name_opt) { 109 if (!support_smi_only_arrays && 110 expected == elements_kind.fast_smi_only) { 111 expected = elements_kind.fast; 112 } 113 assertEquals(expected, getKind(obj), name_opt); 114} 115 116%NeverOptimizeFunction(construct_smis); 117function construct_smis() { 118 var a = [0, 0, 0]; 119 a[0] = 0; // Send the COW array map to the steak house. 120 assertKind(elements_kind.fast_smi_only, a); 121 return a; 122} 123 124%NeverOptimizeFunction(construct_doubles); 125function construct_doubles() { 126 var a = construct_smis(); 127 a[0] = 1.5; 128 assertKind(elements_kind.fast_double, a); 129 return a; 130} 131 132%NeverOptimizeFunction(convert_mixed); 133function convert_mixed(array, value, kind) { 134 array[1] = value; 135 assertKind(kind, array); 136 assertEquals(value, array[1]); 137} 138 139function test1() { 140 if (!support_smi_only_arrays) return; 141 142 // Test transition chain SMI->DOUBLE->FAST (crankshafted function will 143 // transition to FAST directly). 144 var smis = construct_smis(); 145 convert_mixed(smis, 1.5, elements_kind.fast_double); 146 147 var doubles = construct_doubles(); 148 convert_mixed(doubles, "three", elements_kind.fast); 149 150 convert_mixed(construct_smis(), "three", elements_kind.fast); 151 convert_mixed(construct_doubles(), "three", elements_kind.fast); 152 153 smis = construct_smis(); 154 doubles = construct_doubles(); 155 convert_mixed(smis, 1, elements_kind.fast); 156 convert_mixed(doubles, 1, elements_kind.fast); 157 assertTrue(%HaveSameMap(smis, doubles)); 158} 159 160test1(); 161gc(); // clear IC state 162test1(); 163gc(); // clear IC state 164%OptimizeFunctionOnNextCall(test1); 165test1(); 166gc(); // clear IC state 167