1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_SCOPEINFO_H_
6 #define V8_SCOPEINFO_H_
8 #include "allocation.h"
15 // Cache for mapping (data, property name) into context slot index.
16 // The cache contains both positive and negative results.
17 // Slot index equals -1 means the property is absent.
18 // Cleared at startup and prior to mark sweep collection.
19 class ContextSlotCache {
21 // Lookup context slot index for (data, name).
22 // If absent, kNotFound is returned.
23 int Lookup(Object* data,
26 InitializationFlag* init_flag);
28 // Update an element in the cache.
29 void Update(Handle<Object> data,
32 InitializationFlag init_flag,
38 static const int kNotFound = -2;
42 for (int i = 0; i < kLength; ++i) {
45 values_[i] = kNotFound;
49 inline static int Hash(Object* data, String* name);
52 void ValidateEntry(Handle<Object> data,
55 InitializationFlag init_flag,
59 static const int kLength = 256;
66 Value(VariableMode mode,
67 InitializationFlag init_flag,
69 ASSERT(ModeField::is_valid(mode));
70 ASSERT(InitField::is_valid(init_flag));
71 ASSERT(IndexField::is_valid(index));
72 value_ = ModeField::encode(mode) |
73 IndexField::encode(index) |
74 InitField::encode(init_flag);
75 ASSERT(mode == this->mode());
76 ASSERT(init_flag == this->initialization_flag());
77 ASSERT(index == this->index());
80 explicit inline Value(uint32_t value) : value_(value) {}
82 uint32_t raw() { return value_; }
84 VariableMode mode() { return ModeField::decode(value_); }
86 InitializationFlag initialization_flag() {
87 return InitField::decode(value_);
90 int index() { return IndexField::decode(value_); }
92 // Bit fields in value_ (type, shift, size). Must be public so the
93 // constants can be embedded in generated code.
94 class ModeField: public BitField<VariableMode, 0, 4> {};
95 class InitField: public BitField<InitializationFlag, 4, 1> {};
96 class IndexField: public BitField<int, 5, 32-5> {};
103 uint32_t values_[kLength];
105 friend class Isolate;
106 DISALLOW_COPY_AND_ASSIGN(ContextSlotCache);
112 //---------------------------------------------------------------------------
113 // Auxiliary class used for the description of module instances.
114 // Used by Runtime_DeclareModules.
116 class ModuleInfo: public FixedArray {
118 static ModuleInfo* cast(Object* description) {
119 return static_cast<ModuleInfo*>(FixedArray::cast(description));
122 static Handle<ModuleInfo> Create(
123 Isolate* isolate, Interface* interface, Scope* scope);
125 // Index of module's context in host context.
126 int host_index() { return Smi::cast(get(HOST_OFFSET))->value(); }
128 // Name, mode, and index of the i-th export, respectively.
129 // For value exports, the index is the slot of the value in the module
130 // context, for exported modules it is the slot index of the
131 // referred module's context in the host context.
132 // TODO(rossberg): This format cannot yet handle exports of modules declared
133 // in earlier scripts.
134 String* name(int i) { return String::cast(get(name_offset(i))); }
135 VariableMode mode(int i) {
136 return static_cast<VariableMode>(Smi::cast(get(mode_offset(i)))->value());
138 int index(int i) { return Smi::cast(get(index_offset(i)))->value(); }
140 int length() { return (FixedArray::length() - HEADER_SIZE) / ITEM_SIZE; }
143 // The internal format is: Index, (Name, VariableMode, Index)*
149 HEADER_SIZE = NAME_OFFSET,
150 ITEM_SIZE = INDEX_OFFSET - NAME_OFFSET + 1
152 inline int name_offset(int i) { return NAME_OFFSET + i * ITEM_SIZE; }
153 inline int mode_offset(int i) { return MODE_OFFSET + i * ITEM_SIZE; }
154 inline int index_offset(int i) { return INDEX_OFFSET + i * ITEM_SIZE; }
156 static Handle<ModuleInfo> Allocate(Isolate* isolate, int length) {
157 return Handle<ModuleInfo>::cast(
158 isolate->factory()->NewFixedArray(HEADER_SIZE + ITEM_SIZE * length));
160 void set_host_index(int index) { set(HOST_OFFSET, Smi::FromInt(index)); }
161 void set_name(int i, String* name) { set(name_offset(i), name); }
162 void set_mode(int i, VariableMode mode) {
163 set(mode_offset(i), Smi::FromInt(mode));
165 void set_index(int i, int index) {
166 set(index_offset(i), Smi::FromInt(index));
171 } } // namespace v8::internal
173 #endif // V8_SCOPEINFO_H_