Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 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 | |
| 30 // Test element kind of objects. | |
| 31 // Since --smi-only-arrays affects builtins, its default setting at compile | |
| 32 // time sticks if built with snapshot. If --smi-only-arrays is deactivated | |
| 33 // by default, only a no-snapshot build actually has smi-only arrays enabled | |
| 34 // in this test case. Depending on whether smi-only arrays are actually | |
| 35 // enabled, this test takes the appropriate code path to check smi-only arrays. | |
| 36 | |
| 37 | |
| 38 support_smi_only_arrays = %HasFastSmiOnlyElements([]); | |
| 39 | |
| 40 if (support_smi_only_arrays) { | |
| 41 print("Tests include smi-only arrays."); | |
| 42 } else { | |
| 43 print("Tests do NOT include smi-only arrays."); | |
| 44 } | |
| 45 | |
| 46 var elements_kind = { | |
| 47 fast_smi_only : 'fast smi only elements', | |
| 48 fast : 'fast elements', | |
| 49 fast_double : 'fast double elements', | |
| 50 dictionary : 'dictionary elements', | |
| 51 external_byte : 'external byte elements', | |
| 52 external_unsigned_byte : 'external unsigned byte elements', | |
| 53 external_short : 'external short elements', | |
| 54 external_unsigned_short : 'external unsigned short elements', | |
| 55 external_int : 'external int elements', | |
| 56 external_unsigned_int : 'external unsigned int elements', | |
| 57 external_float : 'external float elements', | |
| 58 external_double : 'external double elements', | |
| 59 external_pixel : 'external pixel elements' | |
| 60 } | |
| 61 | |
| 62 function getKind(obj) { | |
| 63 if (%HasFastSmiOnlyElements(obj)) return elements_kind.fast_smi_only; | |
| 64 if (%HasFastElements(obj)) return elements_kind.fast; | |
| 65 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; | |
| 66 if (%HasDictionaryElements(obj)) return elements_kind.dictionary; | |
| 67 // Every external kind is also an external array. | |
| 68 assertTrue(%HasExternalArrayElements(obj)); | |
| 69 if (%HasExternalByteElements(obj)) { | |
| 70 return elements_kind.external_byte; | |
| 71 } | |
| 72 if (%HasExternalUnsignedByteElements(obj)) { | |
| 73 return elements_kind.external_unsigned_byte; | |
| 74 } | |
| 75 if (%HasExternalShortElements(obj)) { | |
| 76 return elements_kind.external_short; | |
| 77 } | |
| 78 if (%HasExternalUnsignedShortElements(obj)) { | |
| 79 return elements_kind.external_unsigned_short; | |
| 80 } | |
| 81 if (%HasExternalIntElements(obj)) { | |
| 82 return elements_kind.external_int; | |
| 83 } | |
| 84 if (%HasExternalUnsignedIntElements(obj)) { | |
| 85 return elements_kind.external_unsigned_int; | |
| 86 } | |
| 87 if (%HasExternalFloatElements(obj)) { | |
| 88 return elements_kind.external_float; | |
| 89 } | |
| 90 if (%HasExternalDoubleElements(obj)) { | |
| 91 return elements_kind.external_double; | |
| 92 } | |
| 93 if (%HasExternalPixelElements(obj)) { | |
| 94 return elements_kind.external_pixel; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 function assertKind(expected, obj, name_opt) { | |
| 99 if (!support_smi_only_arrays && | |
| 100 expected == elements_kind.fast_smi_only) { | |
| 101 expected = elements_kind.fast; | |
| 102 } | |
| 103 assertEquals(expected, getKind(obj), name_opt); | |
|
Jakob Kummerow
2011/10/13 09:15:59
Nice! I like this refactoring.
| |
| 104 } | |
| 105 | |
| 106 var me = {}; | |
| 107 assertKind(elements_kind.fast, me); | |
| 108 me.dance = 0xD15C0; | |
| 109 me.drink = 0xC0C0A; | |
| 110 assertKind(elements_kind.fast, me); | |
| 111 | |
| 112 var too = [1,2,3]; | |
| 113 assertKind(elements_kind.fast_smi_only, too); | |
| 114 too.dance = 0xD15C0; | |
| 115 too.drink = 0xC0C0A; | |
| 116 assertKind(elements_kind.fast_smi_only, too); | |
| 117 | |
| 118 // Make sure the element kind transitions from smionly when a non-smi is stored. | |
| 119 var you = new Array(); | |
| 120 assertKind(elements_kind.fast_smi_only, you); | |
| 121 for (var i = 0; i < 1337; i++) { | |
| 122 var val = i; | |
| 123 if (i == 1336) { | |
| 124 assertKind(elements_kind.fast_smi_only, you); | |
| 125 val = new Object(); | |
| 126 } | |
| 127 you[i] = val; | |
| 128 } | |
| 129 assertKind(elements_kind.fast, you); | |
| 130 | |
| 131 assertKind(elements_kind.dictionary, new Array(0xDECAF)); | |
| 132 | |
| 133 var fast_double_array = new Array(0xDECAF); | |
| 134 for (var i = 0; i < 0xDECAF; i++) fast_double_array[i] = i / 2; | |
| 135 assertKind(elements_kind.fast_double, fast_double_array); | |
| 136 | |
| 137 assertKind(elements_kind.external_byte, new Int8Array(9001)); | |
| 138 assertKind(elements_kind.external_unsigned_byte, new Uint8Array(007)); | |
| 139 assertKind(elements_kind.external_short, new Int16Array(666)); | |
| 140 assertKind(elements_kind.external_unsigned_short, new Uint16Array(42)); | |
| 141 assertKind(elements_kind.external_int, new Int32Array(0xF)); | |
| 142 assertKind(elements_kind.external_unsigned_int, new Uint32Array(23)); | |
| 143 assertKind(elements_kind.external_float, new Float32Array(7)); | |
| 144 assertKind(elements_kind.external_double, new Float64Array(0)); | |
| 145 assertKind(elements_kind.external_pixel, new PixelArray(512)); | |
| 146 | |
| 147 // Crankshaft support for smi-only array elements. | |
| 148 function monomorphic(array) { | |
| 149 for (var i = 0; i < 3; i++) { | |
| 150 array[i] = i + 10; | |
| 151 } | |
| 152 assertKind(elements_kind.fast_smi_only, array); | |
| 153 for (var i = 0; i < 3; i++) { | |
| 154 var a = array[i]; | |
| 155 assertEquals(i + 10, a); | |
| 156 } | |
| 157 } | |
| 158 var smi_only = [1, 2, 3]; | |
| 159 for (var i = 0; i < 3; i++) monomorphic(smi_only); | |
| 160 %OptimizeFunctionOnNextCall(monomorphic); | |
| 161 monomorphic(smi_only); | |
| 162 function polymorphic(array, expected_kind) { | |
| 163 array[1] = 42; | |
| 164 assertKind(expected_kind, array); | |
| 165 var a = array[1]; | |
| 166 assertEquals(42, a); | |
| 167 } | |
| 168 var smis = [1, 2, 3]; | |
| 169 var strings = [0, 0, 0]; strings[0] = "one"; | |
| 170 var doubles = [0, 0, 0]; doubles[0] = 1.5; | |
| 171 assertKind(support_smi_only_arrays | |
| 172 ? elements_kind.fast_double | |
| 173 : elements_kind.fast, | |
| 174 doubles); | |
| 175 for (var i = 0; i < 3; i++) { | |
| 176 polymorphic(smis, elements_kind.fast_smi_only); | |
| 177 } | |
| 178 for (var i = 0; i < 3; i++) { | |
| 179 polymorphic(strings, elements_kind.fast); | |
| 180 } | |
| 181 for (var i = 0; i < 3; i++) { | |
| 182 polymorphic(doubles, i == 0 | |
| 183 ? elements_kind.fast_double | |
| 184 : elements_kind.fast); | |
| 185 } | |
| 186 //%OptimizeFunctionOnNextCall(polymorphic); | |
|
Jakob Kummerow
2011/10/13 09:15:59
Please add a comment stating why this section is d
| |
| 187 //polymorphic(smis, elements_kind.fast_smi_only); | |
| 188 //polymorphic(strings, elements_kind.fast); | |
| 189 //polymorphic(doubles, elements_kind.fast); | |
| 190 // | |
| 191 //// Crankshaft support for smi-only elements in dynamic array literals. | |
| 192 //function get(foo) { return foo; } // Used to generate dynamic values. | |
| 193 // | |
| 194 //function crankshaft_test() { | |
| 195 // var a = [get(1), get(2), get(3)]; | |
| 196 // assertKind(elements_kind.fast_smi_only, a); | |
| 197 // var b = [get(1), get(2), get("three")]; | |
| 198 // assertKind(elements_kind.fast, b); | |
| 199 // var c = [get(1), get(2), get(3.5)]; | |
| 200 // // The full code generator doesn't support conversion to fast_double | |
| 201 // // yet. Crankshaft does, but only with --smi-only-arrays support. | |
| 202 // if ((%GetOptimizationStatus(crankshaft_test) & 1) && | |
| 203 // support_smi_only_arrays) { | |
| 204 // assertKind(elements_kind.fast_double, c); | |
| 205 // } else { | |
| 206 // assertKind(elements_kind.fast, c); | |
| 207 // } | |
| 208 //} | |
| 209 //for (var i = 0; i < 3; i++) { | |
| 210 // crankshaft_test(); | |
| 211 //} | |
| 212 //%OptimizeFunctionOnNextCall(crankshaft_test); | |
| 213 //crankshaft_test(); | |
| 214 | |
| 215 // Elements_kind transitions for arrays. | |
| 216 | |
| 217 // A map can have three different elements_kind transitions: SMI->DOUBLE, | |
| 218 // DOUBLE->OBJECT, and SMI->OBJECT. No matter in which order these three are | |
| 219 // created, they must always end up with the same FAST map. | |
| 220 | |
| 221 // This test is meaningless without FAST_SMI_ONLY_ELEMENTS. | |
| 222 if (support_smi_only_arrays) { | |
| 223 // Preparation: create one pair of identical objects for each case. | |
| 224 var a = [1, 2, 3]; | |
| 225 var b = [1, 2, 3]; | |
| 226 assertTrue(%HaveSameMap(a, b)); | |
| 227 assertKind(elements_kind.fast_smi_only, a); | |
| 228 var c = [1, 2, 3]; | |
| 229 c["case2"] = true; | |
| 230 var d = [1, 2, 3]; | |
| 231 d["case2"] = true; | |
| 232 assertTrue(%HaveSameMap(c, d)); | |
| 233 assertFalse(%HaveSameMap(a, c)); | |
| 234 assertKind(elements_kind.fast_smi_only, c); | |
| 235 var e = [1, 2, 3]; | |
| 236 e["case3"] = true; | |
| 237 var f = [1, 2, 3]; | |
| 238 f["case3"] = true; | |
| 239 assertTrue(%HaveSameMap(e, f)); | |
| 240 assertFalse(%HaveSameMap(a, e)); | |
| 241 assertFalse(%HaveSameMap(c, e)); | |
| 242 assertKind(elements_kind.fast_smi_only, e); | |
| 243 // Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT. | |
| 244 a[0] = 1.5; | |
| 245 assertKind(elements_kind.fast_double, a); | |
| 246 a[0] = "foo"; | |
| 247 assertKind(elements_kind.fast, a); | |
| 248 b[0] = "bar"; | |
| 249 assertTrue(%HaveSameMap(a, b)); | |
| 250 // Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT. | |
| 251 c[0] = 1.5; | |
| 252 assertKind(elements_kind.fast_double, c); | |
| 253 assertFalse(%HaveSameMap(c, d)); | |
| 254 d[0] = "foo"; | |
| 255 assertKind(elements_kind.fast, d); | |
| 256 assertFalse(%HaveSameMap(c, d)); | |
| 257 c[0] = "bar"; | |
| 258 assertTrue(%HaveSameMap(c, d)); | |
| 259 // Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT. | |
| 260 e[0] = "foo"; | |
| 261 assertKind(elements_kind.fast, e); | |
| 262 assertFalse(%HaveSameMap(e, f)); | |
| 263 f[0] = 1.5; | |
| 264 assertKind(elements_kind.fast_double, f); | |
| 265 assertFalse(%HaveSameMap(e, f)); | |
| 266 f[0] = "bar"; | |
| 267 assertKind(elements_kind.fast, f); | |
| 268 assertTrue(%HaveSameMap(e, f)); | |
| 269 } | |
| 270 | |
| 271 // Throw away type information in the ICs for next stress run. | |
| 272 gc(); | |
| OLD | NEW |