OLD | NEW |
(Empty) | |
| 1 // Copyright 2009 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 #ifndef V8_FRAME_ELEMENT_H_ |
| 29 #define V8_FRAME_ELEMENT_H_ |
| 30 |
| 31 #include "register-allocator-inl.h" |
| 32 |
| 33 namespace v8 { namespace internal { |
| 34 |
| 35 // ------------------------------------------------------------------------- |
| 36 // Virtual frame elements |
| 37 // |
| 38 // The internal elements of the virtual frames. There are several kinds of |
| 39 // elements: |
| 40 // * Invalid: elements that are uninitialized or not actually part |
| 41 // of the virtual frame. They should not be read. |
| 42 // * Memory: an element that resides in the actual frame. Its address is |
| 43 // given by its position in the virtual frame. |
| 44 // * Register: an element that resides in a register. |
| 45 // * Constant: an element whose value is known at compile time. |
| 46 |
| 47 class FrameElement BASE_EMBEDDED { |
| 48 public: |
| 49 enum SyncFlag { |
| 50 NOT_SYNCED, |
| 51 SYNCED |
| 52 }; |
| 53 |
| 54 // The default constructor creates an invalid frame element. |
| 55 FrameElement() { |
| 56 value_ = StaticTypeField::encode(StaticType::UNKNOWN_TYPE) |
| 57 | TypeField::encode(INVALID) |
| 58 | CopiedField::encode(false) |
| 59 | SyncedField::encode(false) |
| 60 | DataField::encode(0); |
| 61 } |
| 62 |
| 63 // Factory function to construct an invalid frame element. |
| 64 static FrameElement InvalidElement() { |
| 65 FrameElement result; |
| 66 return result; |
| 67 } |
| 68 |
| 69 // Factory function to construct an in-memory frame element. |
| 70 static FrameElement MemoryElement() { |
| 71 FrameElement result(MEMORY, no_reg, SYNCED); |
| 72 return result; |
| 73 } |
| 74 |
| 75 // Factory function to construct an in-register frame element. |
| 76 static FrameElement RegisterElement(Register reg, |
| 77 SyncFlag is_synced, |
| 78 StaticType static_type = StaticType()) { |
| 79 return FrameElement(REGISTER, reg, is_synced, static_type); |
| 80 } |
| 81 |
| 82 // Factory function to construct a frame element whose value is known at |
| 83 // compile time. |
| 84 static FrameElement ConstantElement(Handle<Object> value, |
| 85 SyncFlag is_synced) { |
| 86 FrameElement result(value, is_synced); |
| 87 return result; |
| 88 } |
| 89 |
| 90 // Static indirection table for handles to constants. If a frame |
| 91 // element represents a constant, the data contains an index into |
| 92 // this table of handles to the actual constants. |
| 93 typedef ZoneList<Handle<Object> > ZoneObjectList; |
| 94 |
| 95 static ZoneObjectList* ConstantList() { |
| 96 static ZoneObjectList list(10); |
| 97 return &list; |
| 98 } |
| 99 |
| 100 // Clear the constants indirection table. |
| 101 static void ClearConstantList() { |
| 102 ConstantList()->Clear(); |
| 103 } |
| 104 |
| 105 bool is_synced() const { return SyncedField::decode(value_); } |
| 106 |
| 107 void set_sync() { |
| 108 ASSERT(type() != MEMORY); |
| 109 value_ = value_ | SyncedField::encode(true); |
| 110 } |
| 111 |
| 112 void clear_sync() { |
| 113 ASSERT(type() != MEMORY); |
| 114 value_ = value_ & ~SyncedField::mask(); |
| 115 } |
| 116 |
| 117 bool is_valid() const { return type() != INVALID; } |
| 118 bool is_memory() const { return type() == MEMORY; } |
| 119 bool is_register() const { return type() == REGISTER; } |
| 120 bool is_constant() const { return type() == CONSTANT; } |
| 121 bool is_copy() const { return type() == COPY; } |
| 122 |
| 123 bool is_copied() const { return CopiedField::decode(value_); } |
| 124 void set_copied() { value_ = value_ | CopiedField::encode(true); } |
| 125 void clear_copied() { value_ = value_ & ~CopiedField::mask(); } |
| 126 |
| 127 Register reg() const { |
| 128 ASSERT(is_register()); |
| 129 uint32_t reg = DataField::decode(value_); |
| 130 Register result; |
| 131 result.code_ = reg; |
| 132 return result; |
| 133 } |
| 134 |
| 135 Handle<Object> handle() const { |
| 136 ASSERT(is_constant()); |
| 137 return ConstantList()->at(DataField::decode(value_)); |
| 138 } |
| 139 |
| 140 int index() const { |
| 141 ASSERT(is_copy()); |
| 142 return DataField::decode(value_); |
| 143 } |
| 144 |
| 145 StaticType static_type() { return StaticType(StaticTypeField::decode(value_));
} |
| 146 |
| 147 void set_static_type(StaticType static_type) { |
| 148 value_ = value_ & ~StaticTypeField::mask(); |
| 149 value_ = value_ | StaticTypeField::encode(static_type.static_type_); |
| 150 } |
| 151 |
| 152 bool Equals(FrameElement other) { |
| 153 if (value_ == other.value_) return true; |
| 154 |
| 155 if (type() != other.type() || |
| 156 is_copied() != other.is_copied() || |
| 157 is_synced() != other.is_synced()) return false; |
| 158 |
| 159 if (is_register()) { |
| 160 if (!reg().is(other.reg())) return false; |
| 161 } else if (is_constant()) { |
| 162 if (!handle().is_identical_to(other.handle())) return false; |
| 163 } else if (is_copy()) { |
| 164 if (index() != other.index()) return false; |
| 165 } |
| 166 |
| 167 return true; |
| 168 } |
| 169 |
| 170 // Given a pair of non-null frame element pointers, return one of them |
| 171 // as an entry frame candidate or null if they are incompatible. |
| 172 FrameElement* Combine(FrameElement* other) { |
| 173 // If either is invalid, the result is. |
| 174 if (!is_valid()) return this; |
| 175 if (!other->is_valid()) return other; |
| 176 |
| 177 // If they do not have the exact same location we reallocate. |
| 178 bool not_same_location = |
| 179 (type() != other->type()) || |
| 180 (is_register() && !reg().is(other->reg())) || |
| 181 (is_constant() && !handle().is_identical_to(other->handle())) || |
| 182 (is_copy() && index() != other->index()); |
| 183 if (not_same_location) return NULL; |
| 184 |
| 185 // If either is unsynced, the result is. The result static type is |
| 186 // the merge of the static types. It's safe to set it on one of the |
| 187 // frame elements, and harmless too (because we are only going to |
| 188 // merge the reaching frames and will ensure that the types are |
| 189 // coherent, and changing the static type does not emit code). |
| 190 FrameElement* result = is_synced() ? other : this; |
| 191 result->set_static_type(static_type().merge(other->static_type())); |
| 192 return result; |
| 193 } |
| 194 |
| 195 private: |
| 196 enum Type { |
| 197 INVALID, |
| 198 MEMORY, |
| 199 REGISTER, |
| 200 CONSTANT, |
| 201 COPY |
| 202 }; |
| 203 |
| 204 // Used to construct memory and register elements. |
| 205 FrameElement(Type type, Register reg, SyncFlag is_synced) { |
| 206 value_ = StaticTypeField::encode(StaticType::UNKNOWN_TYPE) |
| 207 | TypeField::encode(type) |
| 208 | CopiedField::encode(false) |
| 209 | SyncedField::encode(is_synced != NOT_SYNCED) |
| 210 | DataField::encode(reg.code_ > 0 ? reg.code_ : 0); |
| 211 } |
| 212 |
| 213 FrameElement(Type type, Register reg, SyncFlag is_synced, StaticType stype) { |
| 214 value_ = StaticTypeField::encode(stype.static_type_) |
| 215 | TypeField::encode(type) |
| 216 | CopiedField::encode(false) |
| 217 | SyncedField::encode(is_synced != NOT_SYNCED) |
| 218 | DataField::encode(reg.code_ > 0 ? reg.code_ : 0); |
| 219 } |
| 220 |
| 221 // Used to construct constant elements. |
| 222 FrameElement(Handle<Object> value, SyncFlag is_synced) { |
| 223 value_ = StaticTypeField::encode(StaticType::TypeOf(*value).static_type_) |
| 224 | TypeField::encode(CONSTANT) |
| 225 | CopiedField::encode(false) |
| 226 | SyncedField::encode(is_synced != NOT_SYNCED) |
| 227 | DataField::encode(ConstantList()->length()); |
| 228 ConstantList()->Add(value); |
| 229 } |
| 230 |
| 231 Type type() const { return TypeField::decode(value_); } |
| 232 void set_type(Type type) { |
| 233 value_ = value_ & ~TypeField::mask(); |
| 234 value_ = value_ | TypeField::encode(type); |
| 235 } |
| 236 |
| 237 void set_index(int new_index) { |
| 238 ASSERT(is_copy()); |
| 239 value_ = value_ & ~DataField::mask(); |
| 240 value_ = value_ | DataField::encode(new_index); |
| 241 } |
| 242 |
| 243 void set_reg(Register new_reg) { |
| 244 ASSERT(is_register()); |
| 245 value_ = value_ & ~DataField::mask(); |
| 246 value_ = value_ | DataField::encode(new_reg.code_); |
| 247 } |
| 248 |
| 249 // Encode static type, type, copied, synced and data in one 32 bit integer. |
| 250 uint32_t value_; |
| 251 |
| 252 class StaticTypeField: public BitField<StaticType::StaticTypeEnum, 0, 3> {}; |
| 253 class TypeField: public BitField<Type, 3, 3> {}; |
| 254 class CopiedField: public BitField<uint32_t, 6, 1> {}; |
| 255 class SyncedField: public BitField<uint32_t, 7, 1> {}; |
| 256 class DataField: public BitField<uint32_t, 8, 32 - 9> {}; |
| 257 |
| 258 friend class VirtualFrame; |
| 259 }; |
| 260 |
| 261 } } // namespace v8::internal |
| 262 |
| 263 #endif // V8_FRAME_ELEMENT_H_ |
OLD | NEW |