svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 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 | #include "elements-kind.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "elements.h" |
| 32 | #include "objects.h" |
| 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 38 | const char* ElementsKindToString(ElementsKind kind) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 39 | ElementsAccessor* accessor = ElementsAccessor::ForKind(kind); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 40 | return accessor->name(); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void PrintElementsKind(FILE* out, ElementsKind kind) { |
| 45 | PrintF(out, "%s", ElementsKindToString(kind)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | ElementsKind GetInitialFastElementsKind() { |
| 50 | if (FLAG_packed_arrays) { |
| 51 | return FAST_SMI_ELEMENTS; |
| 52 | } else { |
| 53 | return FAST_HOLEY_SMI_ELEMENTS; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | struct InitializeFastElementsKindSequence { |
| 59 | static void Construct( |
| 60 | ElementsKind** fast_elements_kind_sequence_ptr) { |
| 61 | ElementsKind* fast_elements_kind_sequence = |
| 62 | new ElementsKind[kFastElementsKindCount]; |
| 63 | *fast_elements_kind_sequence_ptr = fast_elements_kind_sequence; |
| 64 | STATIC_ASSERT(FAST_SMI_ELEMENTS == FIRST_FAST_ELEMENTS_KIND); |
| 65 | fast_elements_kind_sequence[0] = FAST_SMI_ELEMENTS; |
| 66 | fast_elements_kind_sequence[1] = FAST_HOLEY_SMI_ELEMENTS; |
| 67 | fast_elements_kind_sequence[2] = FAST_DOUBLE_ELEMENTS; |
| 68 | fast_elements_kind_sequence[3] = FAST_HOLEY_DOUBLE_ELEMENTS; |
| 69 | fast_elements_kind_sequence[4] = FAST_ELEMENTS; |
| 70 | fast_elements_kind_sequence[5] = FAST_HOLEY_ELEMENTS; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | static LazyInstance<ElementsKind*, |
| 76 | InitializeFastElementsKindSequence>::type |
| 77 | fast_elements_kind_sequence = LAZY_INSTANCE_INITIALIZER; |
| 78 | |
| 79 | |
| 80 | ElementsKind GetFastElementsKindFromSequenceIndex(int sequence_number) { |
| 81 | ASSERT(sequence_number >= 0 && |
| 82 | sequence_number < kFastElementsKindCount); |
| 83 | return fast_elements_kind_sequence.Get()[sequence_number]; |
| 84 | } |
| 85 | |
| 86 | int GetSequenceIndexFromFastElementsKind(ElementsKind elements_kind) { |
| 87 | for (int i = 0; i < kFastElementsKindCount; ++i) { |
| 88 | if (fast_elements_kind_sequence.Get()[i] == elements_kind) { |
| 89 | return i; |
| 90 | } |
| 91 | } |
| 92 | UNREACHABLE(); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | ElementsKind GetNextMoreGeneralFastElementsKind(ElementsKind elements_kind, |
| 98 | bool allow_only_packed) { |
| 99 | ASSERT(IsFastElementsKind(elements_kind)); |
| 100 | ASSERT(elements_kind != TERMINAL_FAST_ELEMENTS_KIND); |
| 101 | while (true) { |
| 102 | int index = |
| 103 | GetSequenceIndexFromFastElementsKind(elements_kind) + 1; |
| 104 | elements_kind = GetFastElementsKindFromSequenceIndex(index); |
| 105 | if (!IsFastHoleyElementsKind(elements_kind) || !allow_only_packed) { |
| 106 | return elements_kind; |
| 107 | } |
| 108 | } |
| 109 | UNREACHABLE(); |
| 110 | return TERMINAL_FAST_ELEMENTS_KIND; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | bool IsMoreGeneralElementsKindTransition(ElementsKind from_kind, |
| 115 | ElementsKind to_kind) { |
| 116 | switch (from_kind) { |
| 117 | case FAST_SMI_ELEMENTS: |
| 118 | return to_kind != FAST_SMI_ELEMENTS; |
| 119 | case FAST_HOLEY_SMI_ELEMENTS: |
| 120 | return to_kind != FAST_SMI_ELEMENTS && |
| 121 | to_kind != FAST_HOLEY_SMI_ELEMENTS; |
| 122 | case FAST_DOUBLE_ELEMENTS: |
| 123 | return to_kind != FAST_SMI_ELEMENTS && |
| 124 | to_kind != FAST_HOLEY_SMI_ELEMENTS && |
| 125 | to_kind != FAST_DOUBLE_ELEMENTS; |
| 126 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 127 | return to_kind == FAST_ELEMENTS || |
| 128 | to_kind == FAST_HOLEY_ELEMENTS; |
| 129 | case FAST_ELEMENTS: |
| 130 | return to_kind == FAST_HOLEY_ELEMENTS; |
| 131 | case FAST_HOLEY_ELEMENTS: |
| 132 | return false; |
| 133 | default: |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | } } // namespace v8::internal |