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. 4 5 #ifndef V8_SCOPEINFO_H_ 6 #define V8_SCOPEINFO_H_ 7 8 #include "src/allocation.h" 9 #include "src/variables.h" 10 #include "src/zone-inl.h" 11 12 namespace v8 { 13 namespace internal { 14 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 { 20 public: 21 // Lookup context slot index for (data, name). 22 // If absent, kNotFound is returned. 23 int Lookup(Object* data, 24 String* name, 25 VariableMode* mode, 26 InitializationFlag* init_flag); 27 28 // Update an element in the cache. 29 void Update(Handle<Object> data, 30 Handle<String> name, 31 VariableMode mode, 32 InitializationFlag init_flag, 33 int slot_index); 34 35 // Clear the cache. 36 void Clear(); 37 38 static const int kNotFound = -2; 39 40 private: ContextSlotCache()41 ContextSlotCache() { 42 for (int i = 0; i < kLength; ++i) { 43 keys_[i].data = NULL; 44 keys_[i].name = NULL; 45 values_[i] = kNotFound; 46 } 47 } 48 49 inline static int Hash(Object* data, String* name); 50 51 #ifdef DEBUG 52 void ValidateEntry(Handle<Object> data, 53 Handle<String> name, 54 VariableMode mode, 55 InitializationFlag init_flag, 56 int slot_index); 57 #endif 58 59 static const int kLength = 256; 60 struct Key { 61 Object* data; 62 String* name; 63 }; 64 65 struct Value { ValueValue66 Value(VariableMode mode, 67 InitializationFlag init_flag, 68 int index) { 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()); 78 } 79 ValueValue80 explicit inline Value(uint32_t value) : value_(value) {} 81 rawValue82 uint32_t raw() { return value_; } 83 modeValue84 VariableMode mode() { return ModeField::decode(value_); } 85 initialization_flagValue86 InitializationFlag initialization_flag() { 87 return InitField::decode(value_); 88 } 89 indexValue90 int index() { return IndexField::decode(value_); } 91 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> {}; 97 98 private: 99 uint32_t value_; 100 }; 101 102 Key keys_[kLength]; 103 uint32_t values_[kLength]; 104 105 friend class Isolate; 106 DISALLOW_COPY_AND_ASSIGN(ContextSlotCache); 107 }; 108 109 110 111 112 //--------------------------------------------------------------------------- 113 // Auxiliary class used for the description of module instances. 114 // Used by Runtime_DeclareModules. 115 116 class ModuleInfo: public FixedArray { 117 public: cast(Object * description)118 static ModuleInfo* cast(Object* description) { 119 return static_cast<ModuleInfo*>(FixedArray::cast(description)); 120 } 121 122 static Handle<ModuleInfo> Create( 123 Isolate* isolate, Interface* interface, Scope* scope); 124 125 // Index of module's context in host context. host_index()126 int host_index() { return Smi::cast(get(HOST_OFFSET))->value(); } 127 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. name(int i)134 String* name(int i) { return String::cast(get(name_offset(i))); } mode(int i)135 VariableMode mode(int i) { 136 return static_cast<VariableMode>(Smi::cast(get(mode_offset(i)))->value()); 137 } index(int i)138 int index(int i) { return Smi::cast(get(index_offset(i)))->value(); } 139 length()140 int length() { return (FixedArray::length() - HEADER_SIZE) / ITEM_SIZE; } 141 142 private: 143 // The internal format is: Index, (Name, VariableMode, Index)* 144 enum { 145 HOST_OFFSET, 146 NAME_OFFSET, 147 MODE_OFFSET, 148 INDEX_OFFSET, 149 HEADER_SIZE = NAME_OFFSET, 150 ITEM_SIZE = INDEX_OFFSET - NAME_OFFSET + 1 151 }; name_offset(int i)152 inline int name_offset(int i) { return NAME_OFFSET + i * ITEM_SIZE; } mode_offset(int i)153 inline int mode_offset(int i) { return MODE_OFFSET + i * ITEM_SIZE; } index_offset(int i)154 inline int index_offset(int i) { return INDEX_OFFSET + i * ITEM_SIZE; } 155 Allocate(Isolate * isolate,int length)156 static Handle<ModuleInfo> Allocate(Isolate* isolate, int length) { 157 return Handle<ModuleInfo>::cast( 158 isolate->factory()->NewFixedArray(HEADER_SIZE + ITEM_SIZE * length)); 159 } set_host_index(int index)160 void set_host_index(int index) { set(HOST_OFFSET, Smi::FromInt(index)); } set_name(int i,String * name)161 void set_name(int i, String* name) { set(name_offset(i), name); } set_mode(int i,VariableMode mode)162 void set_mode(int i, VariableMode mode) { 163 set(mode_offset(i), Smi::FromInt(mode)); 164 } set_index(int i,int index)165 void set_index(int i, int index) { 166 set(index_offset(i), Smi::FromInt(index)); 167 } 168 }; 169 170 171 } } // namespace v8::internal 172 173 #endif // V8_SCOPEINFO_H_ 174