blob: ae5d6a1bf2f1859f0e8aff7d58d5dfc4937dc35b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
Steve Block6ded16b2010-05-10 14:33:55 +010031#include "type-info.h"
Andrei Popescu402d9372010-02-26 13:31:12 +000032#include "macro-assembler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010033#include "zone.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35namespace v8 {
36namespace internal {
37
38// -------------------------------------------------------------------------
39// Virtual frame elements
40//
41// The internal elements of the virtual frames. There are several kinds of
42// elements:
43// * Invalid: elements that are uninitialized or not actually part
44// of the virtual frame. They should not be read.
45// * Memory: an element that resides in the actual frame. Its address is
46// given by its position in the virtual frame.
47// * Register: an element that resides in a register.
48// * Constant: an element whose value is known at compile time.
49
50class FrameElement BASE_EMBEDDED {
51 public:
52 enum SyncFlag {
53 NOT_SYNCED,
54 SYNCED
55 };
56
Steve Block6ded16b2010-05-10 14:33:55 +010057 inline TypeInfo type_info() {
58 // Copied elements do not have type info. Instead
Andrei Popescu402d9372010-02-26 13:31:12 +000059 // we have to inspect their backing element in the frame.
60 ASSERT(!is_copy());
Steve Block6ded16b2010-05-10 14:33:55 +010061 return TypeInfo::FromInt(TypeInfoField::decode(value_));
Andrei Popescu402d9372010-02-26 13:31:12 +000062 }
63
Steve Block6ded16b2010-05-10 14:33:55 +010064 inline void set_type_info(TypeInfo info) {
65 // Copied elements do not have type info. Instead
Andrei Popescu402d9372010-02-26 13:31:12 +000066 // we have to inspect their backing element in the frame.
67 ASSERT(!is_copy());
Steve Block6ded16b2010-05-10 14:33:55 +010068 value_ = value_ & ~TypeInfoField::mask();
69 value_ = value_ | TypeInfoField::encode(info.ToInt());
Andrei Popescu402d9372010-02-26 13:31:12 +000070 }
71
Steve Blocka7e24c12009-10-30 11:49:00 +000072 // The default constructor creates an invalid frame element.
73 FrameElement() {
74 value_ = TypeField::encode(INVALID)
75 | CopiedField::encode(false)
76 | SyncedField::encode(false)
Steve Block6ded16b2010-05-10 14:33:55 +010077 | TypeInfoField::encode(TypeInfo::Uninitialized().ToInt())
Steve Blocka7e24c12009-10-30 11:49:00 +000078 | DataField::encode(0);
79 }
80
81 // Factory function to construct an invalid frame element.
82 static FrameElement InvalidElement() {
83 FrameElement result;
84 return result;
85 }
86
87 // Factory function to construct an in-memory frame element.
Steve Block6ded16b2010-05-10 14:33:55 +010088 static FrameElement MemoryElement(TypeInfo info) {
Andrei Popescu402d9372010-02-26 13:31:12 +000089 FrameElement result(MEMORY, no_reg, SYNCED, info);
Steve Blocka7e24c12009-10-30 11:49:00 +000090 return result;
91 }
92
93 // Factory function to construct an in-register frame element.
94 static FrameElement RegisterElement(Register reg,
Andrei Popescu402d9372010-02-26 13:31:12 +000095 SyncFlag is_synced,
Steve Block6ded16b2010-05-10 14:33:55 +010096 TypeInfo info) {
Andrei Popescu402d9372010-02-26 13:31:12 +000097 return FrameElement(REGISTER, reg, is_synced, info);
Steve Blocka7e24c12009-10-30 11:49:00 +000098 }
99
100 // Factory function to construct a frame element whose value is known at
101 // compile time.
102 static FrameElement ConstantElement(Handle<Object> value,
103 SyncFlag is_synced) {
Steve Block6ded16b2010-05-10 14:33:55 +0100104 TypeInfo info = TypeInfo::TypeFromValue(value);
105 FrameElement result(value, is_synced, info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 return result;
107 }
108
109 // Static indirection table for handles to constants. If a frame
110 // element represents a constant, the data contains an index into
111 // this table of handles to the actual constants.
112 typedef ZoneList<Handle<Object> > ZoneObjectList;
113
114 static ZoneObjectList* ConstantList();
115
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100116 static bool ConstantPoolOverflowed() {
117 return !DataField::is_valid(ConstantList()->length());
118 }
119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 // Clear the constants indirection table.
121 static void ClearConstantList() {
122 ConstantList()->Clear();
123 }
124
125 bool is_synced() const { return SyncedField::decode(value_); }
126
127 void set_sync() {
128 ASSERT(type() != MEMORY);
129 value_ = value_ | SyncedField::encode(true);
130 }
131
132 void clear_sync() {
133 ASSERT(type() != MEMORY);
134 value_ = value_ & ~SyncedField::mask();
135 }
136
137 bool is_valid() const { return type() != INVALID; }
138 bool is_memory() const { return type() == MEMORY; }
139 bool is_register() const { return type() == REGISTER; }
140 bool is_constant() const { return type() == CONSTANT; }
141 bool is_copy() const { return type() == COPY; }
142
143 bool is_copied() const { return CopiedField::decode(value_); }
144 void set_copied() { value_ = value_ | CopiedField::encode(true); }
145 void clear_copied() { value_ = value_ & ~CopiedField::mask(); }
146
Steve Block6ded16b2010-05-10 14:33:55 +0100147 // An untagged int32 FrameElement represents a signed int32
148 // on the stack. These are only allowed in a side-effect-free
149 // int32 calculation, and if a non-int32 input shows up or an overflow
150 // occurs, we bail out and drop all the int32 values.
151 void set_untagged_int32(bool value) {
152 value_ &= ~UntaggedInt32Field::mask();
153 value_ |= UntaggedInt32Field::encode(value);
154 }
155 bool is_untagged_int32() const { return UntaggedInt32Field::decode(value_); }
156
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 Register reg() const {
158 ASSERT(is_register());
159 uint32_t reg = DataField::decode(value_);
160 Register result;
161 result.code_ = reg;
162 return result;
163 }
164
165 Handle<Object> handle() const {
166 ASSERT(is_constant());
167 return ConstantList()->at(DataField::decode(value_));
168 }
169
170 int index() const {
171 ASSERT(is_copy());
172 return DataField::decode(value_);
173 }
174
175 bool Equals(FrameElement other) {
176 uint32_t masked_difference = (value_ ^ other.value_) & ~CopiedField::mask();
177 if (!masked_difference) {
178 // The elements are equal if they agree exactly except on copied field.
179 return true;
180 } else {
181 // If two constants have the same value, and agree otherwise, return true.
182 return !(masked_difference & ~DataField::mask()) &&
183 is_constant() &&
184 handle().is_identical_to(other.handle());
185 }
186 }
187
188 // Test if two FrameElements refer to the same memory or register location.
189 bool SameLocation(FrameElement* other) {
190 if (type() == other->type()) {
191 if (value_ == other->value_) return true;
192 if (is_constant() && handle().is_identical_to(other->handle())) {
193 return true;
194 }
195 }
196 return false;
197 }
198
199 // Given a pair of non-null frame element pointers, return one of them
200 // as an entry frame candidate or null if they are incompatible.
201 FrameElement* Combine(FrameElement* other) {
202 // If either is invalid, the result is.
203 if (!is_valid()) return this;
204 if (!other->is_valid()) return other;
205
206 if (!SameLocation(other)) return NULL;
207 // If either is unsynced, the result is.
208 FrameElement* result = is_synced() ? other : this;
209 return result;
210 }
211
212 private:
213 enum Type {
214 INVALID,
215 MEMORY,
216 REGISTER,
217 CONSTANT,
218 COPY
219 };
220
221 // Used to construct memory and register elements.
Andrei Popescu402d9372010-02-26 13:31:12 +0000222 FrameElement(Type type,
223 Register reg,
224 SyncFlag is_synced,
Steve Block6ded16b2010-05-10 14:33:55 +0100225 TypeInfo info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 value_ = TypeField::encode(type)
227 | CopiedField::encode(false)
228 | SyncedField::encode(is_synced != NOT_SYNCED)
Steve Block6ded16b2010-05-10 14:33:55 +0100229 | TypeInfoField::encode(info.ToInt())
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 | DataField::encode(reg.code_ > 0 ? reg.code_ : 0);
231 }
232
233 // Used to construct constant elements.
Steve Block6ded16b2010-05-10 14:33:55 +0100234 FrameElement(Handle<Object> value, SyncFlag is_synced, TypeInfo info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 value_ = TypeField::encode(CONSTANT)
236 | CopiedField::encode(false)
237 | SyncedField::encode(is_synced != NOT_SYNCED)
Steve Block6ded16b2010-05-10 14:33:55 +0100238 | TypeInfoField::encode(info.ToInt())
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 | DataField::encode(ConstantList()->length());
240 ConstantList()->Add(value);
241 }
242
243 Type type() const { return TypeField::decode(value_); }
244 void set_type(Type type) {
245 value_ = value_ & ~TypeField::mask();
246 value_ = value_ | TypeField::encode(type);
247 }
248
249 void set_index(int new_index) {
250 ASSERT(is_copy());
251 value_ = value_ & ~DataField::mask();
252 value_ = value_ | DataField::encode(new_index);
253 }
254
255 void set_reg(Register new_reg) {
256 ASSERT(is_register());
257 value_ = value_ & ~DataField::mask();
258 value_ = value_ | DataField::encode(new_reg.code_);
259 }
260
261 // Encode type, copied, synced and data in one 32 bit integer.
262 uint32_t value_;
263
Steve Block6ded16b2010-05-10 14:33:55 +0100264 // Declare BitFields with template parameters <type, start, size>.
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 class TypeField: public BitField<Type, 0, 3> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000266 class CopiedField: public BitField<bool, 3, 1> {};
267 class SyncedField: public BitField<bool, 4, 1> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100268 class UntaggedInt32Field: public BitField<bool, 5, 1> {};
Ben Murdochb0fe1622011-05-05 13:52:32 +0100269 class TypeInfoField: public BitField<int, 6, 7> {};
270 class DataField: public BitField<uint32_t, 13, 32 - 13> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
272 friend class VirtualFrame;
273};
274
275} } // namespace v8::internal
276
277#endif // V8_FRAME_ELEMENT_H_