blob: 3b91b9d34ac99f9056e866ebf8c014de62cb616e [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
116 // Clear the constants indirection table.
117 static void ClearConstantList() {
118 ConstantList()->Clear();
119 }
120
121 bool is_synced() const { return SyncedField::decode(value_); }
122
123 void set_sync() {
124 ASSERT(type() != MEMORY);
125 value_ = value_ | SyncedField::encode(true);
126 }
127
128 void clear_sync() {
129 ASSERT(type() != MEMORY);
130 value_ = value_ & ~SyncedField::mask();
131 }
132
133 bool is_valid() const { return type() != INVALID; }
134 bool is_memory() const { return type() == MEMORY; }
135 bool is_register() const { return type() == REGISTER; }
136 bool is_constant() const { return type() == CONSTANT; }
137 bool is_copy() const { return type() == COPY; }
138
139 bool is_copied() const { return CopiedField::decode(value_); }
140 void set_copied() { value_ = value_ | CopiedField::encode(true); }
141 void clear_copied() { value_ = value_ & ~CopiedField::mask(); }
142
Steve Block6ded16b2010-05-10 14:33:55 +0100143 // An untagged int32 FrameElement represents a signed int32
144 // on the stack. These are only allowed in a side-effect-free
145 // int32 calculation, and if a non-int32 input shows up or an overflow
146 // occurs, we bail out and drop all the int32 values.
147 void set_untagged_int32(bool value) {
148 value_ &= ~UntaggedInt32Field::mask();
149 value_ |= UntaggedInt32Field::encode(value);
150 }
151 bool is_untagged_int32() const { return UntaggedInt32Field::decode(value_); }
152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 Register reg() const {
154 ASSERT(is_register());
155 uint32_t reg = DataField::decode(value_);
156 Register result;
157 result.code_ = reg;
158 return result;
159 }
160
161 Handle<Object> handle() const {
162 ASSERT(is_constant());
163 return ConstantList()->at(DataField::decode(value_));
164 }
165
166 int index() const {
167 ASSERT(is_copy());
168 return DataField::decode(value_);
169 }
170
171 bool Equals(FrameElement other) {
172 uint32_t masked_difference = (value_ ^ other.value_) & ~CopiedField::mask();
173 if (!masked_difference) {
174 // The elements are equal if they agree exactly except on copied field.
175 return true;
176 } else {
177 // If two constants have the same value, and agree otherwise, return true.
178 return !(masked_difference & ~DataField::mask()) &&
179 is_constant() &&
180 handle().is_identical_to(other.handle());
181 }
182 }
183
184 // Test if two FrameElements refer to the same memory or register location.
185 bool SameLocation(FrameElement* other) {
186 if (type() == other->type()) {
187 if (value_ == other->value_) return true;
188 if (is_constant() && handle().is_identical_to(other->handle())) {
189 return true;
190 }
191 }
192 return false;
193 }
194
195 // Given a pair of non-null frame element pointers, return one of them
196 // as an entry frame candidate or null if they are incompatible.
197 FrameElement* Combine(FrameElement* other) {
198 // If either is invalid, the result is.
199 if (!is_valid()) return this;
200 if (!other->is_valid()) return other;
201
202 if (!SameLocation(other)) return NULL;
203 // If either is unsynced, the result is.
204 FrameElement* result = is_synced() ? other : this;
205 return result;
206 }
207
208 private:
209 enum Type {
210 INVALID,
211 MEMORY,
212 REGISTER,
213 CONSTANT,
214 COPY
215 };
216
217 // Used to construct memory and register elements.
Andrei Popescu402d9372010-02-26 13:31:12 +0000218 FrameElement(Type type,
219 Register reg,
220 SyncFlag is_synced,
Steve Block6ded16b2010-05-10 14:33:55 +0100221 TypeInfo info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 value_ = TypeField::encode(type)
223 | CopiedField::encode(false)
224 | SyncedField::encode(is_synced != NOT_SYNCED)
Steve Block6ded16b2010-05-10 14:33:55 +0100225 | TypeInfoField::encode(info.ToInt())
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 | DataField::encode(reg.code_ > 0 ? reg.code_ : 0);
227 }
228
229 // Used to construct constant elements.
Steve Block6ded16b2010-05-10 14:33:55 +0100230 FrameElement(Handle<Object> value, SyncFlag is_synced, TypeInfo info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 value_ = TypeField::encode(CONSTANT)
232 | CopiedField::encode(false)
233 | SyncedField::encode(is_synced != NOT_SYNCED)
Steve Block6ded16b2010-05-10 14:33:55 +0100234 | TypeInfoField::encode(info.ToInt())
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 | DataField::encode(ConstantList()->length());
236 ConstantList()->Add(value);
237 }
238
239 Type type() const { return TypeField::decode(value_); }
240 void set_type(Type type) {
241 value_ = value_ & ~TypeField::mask();
242 value_ = value_ | TypeField::encode(type);
243 }
244
245 void set_index(int new_index) {
246 ASSERT(is_copy());
247 value_ = value_ & ~DataField::mask();
248 value_ = value_ | DataField::encode(new_index);
249 }
250
251 void set_reg(Register new_reg) {
252 ASSERT(is_register());
253 value_ = value_ & ~DataField::mask();
254 value_ = value_ | DataField::encode(new_reg.code_);
255 }
256
257 // Encode type, copied, synced and data in one 32 bit integer.
258 uint32_t value_;
259
Steve Block6ded16b2010-05-10 14:33:55 +0100260 // Declare BitFields with template parameters <type, start, size>.
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 class TypeField: public BitField<Type, 0, 3> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000262 class CopiedField: public BitField<bool, 3, 1> {};
263 class SyncedField: public BitField<bool, 4, 1> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100264 class UntaggedInt32Field: public BitField<bool, 5, 1> {};
Ben Murdochb0fe1622011-05-05 13:52:32 +0100265 class TypeInfoField: public BitField<int, 6, 7> {};
266 class DataField: public BitField<uint32_t, 13, 32 - 13> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000267
268 friend class VirtualFrame;
269};
270
271} } // namespace v8::internal
272
273#endif // V8_FRAME_ELEMENT_H_