Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 --expose-gc |
| 29 | |
| 30 | // Limit the number of stress runs to reduce polymorphism it defeats some of the |
| 31 | // assumptions made about how elements transitions work because transition stubs |
| 32 | // end up going generic. |
| 33 | // Flags: --stress-runs=2 |
| 34 | |
| 35 | var elements_kind = { |
| 36 | fast_smi_only : 'fast smi only elements', |
| 37 | fast : 'fast elements', |
| 38 | fast_double : 'fast double elements', |
| 39 | dictionary : 'dictionary elements', |
| 40 | external_byte : 'external byte elements', |
| 41 | external_unsigned_byte : 'external unsigned byte elements', |
| 42 | external_short : 'external short elements', |
| 43 | external_unsigned_short : 'external unsigned short elements', |
| 44 | external_int : 'external int elements', |
| 45 | external_unsigned_int : 'external unsigned int elements', |
| 46 | external_float : 'external float elements', |
| 47 | external_double : 'external double elements', |
| 48 | external_pixel : 'external pixel elements' |
| 49 | } |
| 50 | |
| 51 | function getKind(obj) { |
| 52 | if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only; |
| 53 | if (%HasFastObjectElements(obj)) return elements_kind.fast; |
| 54 | if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; |
| 55 | if (%HasDictionaryElements(obj)) return elements_kind.dictionary; |
| 56 | // Every external kind is also an external array. |
| 57 | assertTrue(%HasExternalArrayElements(obj)); |
| 58 | if (%HasExternalByteElements(obj)) { |
| 59 | return elements_kind.external_byte; |
| 60 | } |
| 61 | if (%HasExternalUnsignedByteElements(obj)) { |
| 62 | return elements_kind.external_unsigned_byte; |
| 63 | } |
| 64 | if (%HasExternalShortElements(obj)) { |
| 65 | return elements_kind.external_short; |
| 66 | } |
| 67 | if (%HasExternalUnsignedShortElements(obj)) { |
| 68 | return elements_kind.external_unsigned_short; |
| 69 | } |
| 70 | if (%HasExternalIntElements(obj)) { |
| 71 | return elements_kind.external_int; |
| 72 | } |
| 73 | if (%HasExternalUnsignedIntElements(obj)) { |
| 74 | return elements_kind.external_unsigned_int; |
| 75 | } |
| 76 | if (%HasExternalFloatElements(obj)) { |
| 77 | return elements_kind.external_float; |
| 78 | } |
| 79 | if (%HasExternalDoubleElements(obj)) { |
| 80 | return elements_kind.external_double; |
| 81 | } |
| 82 | if (%HasExternalPixelElements(obj)) { |
| 83 | return elements_kind.external_pixel; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function assertKind(expected, obj, name_opt) { |
| 88 | assertEquals(expected, getKind(obj), name_opt); |
| 89 | } |
| 90 | |
| 91 | %NeverOptimizeFunction(construct_smis); |
| 92 | |
| 93 | // This code exists to eliminate the learning influence of AllocationSites |
| 94 | // on the following tests. |
| 95 | var __sequence = 0; |
| 96 | function make_array_string() { |
| 97 | this.__sequence = this.__sequence + 1; |
| 98 | return "/* " + this.__sequence + " */ [0, 0, 0];" |
| 99 | } |
| 100 | function make_array() { |
| 101 | return eval(make_array_string()); |
| 102 | } |
| 103 | |
| 104 | function construct_smis() { |
| 105 | var a = make_array(); |
| 106 | a[0] = 0; // Send the COW array map to the steak house. |
| 107 | assertKind(elements_kind.fast_smi_only, a); |
| 108 | return a; |
| 109 | } |
| 110 | |
| 111 | %NeverOptimizeFunction(construct_doubles); |
| 112 | function construct_doubles() { |
| 113 | var a = construct_smis(); |
| 114 | a[0] = 1.5; |
| 115 | assertKind(elements_kind.fast_double, a); |
| 116 | return a; |
| 117 | } |
| 118 | |
| 119 | %NeverOptimizeFunction(convert_mixed); |
| 120 | function convert_mixed(array, value, kind) { |
| 121 | array[1] = value; |
| 122 | assertKind(kind, array); |
| 123 | assertEquals(value, array[1]); |
| 124 | } |
| 125 | |
| 126 | function test1() { |
| 127 | // Test transition chain SMI->DOUBLE->FAST (crankshafted function will |
| 128 | // transition to FAST directly). |
| 129 | var smis = construct_smis(); |
| 130 | convert_mixed(smis, 1.5, elements_kind.fast_double); |
| 131 | |
| 132 | var doubles = construct_doubles(); |
| 133 | convert_mixed(doubles, "three", elements_kind.fast); |
| 134 | |
| 135 | convert_mixed(construct_smis(), "three", elements_kind.fast); |
| 136 | convert_mixed(construct_doubles(), "three", elements_kind.fast); |
| 137 | |
| 138 | smis = construct_smis(); |
| 139 | doubles = construct_doubles(); |
| 140 | convert_mixed(smis, 1, elements_kind.fast); |
| 141 | convert_mixed(doubles, 1, elements_kind.fast); |
| 142 | assertTrue(%HaveSameMap(smis, doubles)); |
| 143 | } |
| 144 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 145 | function clear_ic_state() { |
| 146 | %ClearFunctionTypeFeedback(construct_smis); |
| 147 | %ClearFunctionTypeFeedback(construct_doubles); |
| 148 | %ClearFunctionTypeFeedback(convert_mixed); |
| 149 | } |
| 150 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | test1(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 152 | clear_ic_state(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | test1(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 154 | clear_ic_state(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 155 | %OptimizeFunctionOnNextCall(test1); |
| 156 | test1(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 157 | clear_ic_state(); |