OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_OBJECTS_SCOPE_INFO_H_ |
| 6 #define V8_OBJECTS_SCOPE_INFO_H_ |
| 7 |
| 8 #include "src/objects.h" |
| 9 #include "src/objects/object-macros.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 class Isolate; |
| 15 class Scope; |
| 16 class Zone; |
| 17 |
| 18 // ScopeInfo represents information about different scopes of a source |
| 19 // program and the allocation of the scope's variables. Scope information |
| 20 // is stored in a compressed form in ScopeInfo objects and is used |
| 21 // at runtime (stack dumps, deoptimization, etc.). |
| 22 |
| 23 // This object provides quick access to scope info details for runtime |
| 24 // routines. |
| 25 class ScopeInfo : public FixedArray { |
| 26 public: |
| 27 DECLARE_CAST(ScopeInfo) |
| 28 |
| 29 // Return the type of this scope. |
| 30 ScopeType scope_type(); |
| 31 |
| 32 // Does this scope call eval? |
| 33 bool CallsEval(); |
| 34 |
| 35 // Return the language mode of this scope. |
| 36 LanguageMode language_mode(); |
| 37 |
| 38 // True if this scope is a (var) declaration scope. |
| 39 bool is_declaration_scope(); |
| 40 |
| 41 // Does this scope make a sloppy eval call? |
| 42 bool CallsSloppyEval() { return CallsEval() && is_sloppy(language_mode()); } |
| 43 |
| 44 // Return the total number of locals allocated on the stack and in the |
| 45 // context. This includes the parameters that are allocated in the context. |
| 46 int LocalCount(); |
| 47 |
| 48 // Return the number of stack slots for code. This number consists of two |
| 49 // parts: |
| 50 // 1. One stack slot per stack allocated local. |
| 51 // 2. One stack slot for the function name if it is stack allocated. |
| 52 int StackSlotCount(); |
| 53 |
| 54 // Return the number of context slots for code if a context is allocated. This |
| 55 // number consists of three parts: |
| 56 // 1. Size of fixed header for every context: Context::MIN_CONTEXT_SLOTS |
| 57 // 2. One context slot per context allocated local. |
| 58 // 3. One context slot for the function name if it is context allocated. |
| 59 // Parameters allocated in the context count as context allocated locals. If |
| 60 // no contexts are allocated for this scope ContextLength returns 0. |
| 61 int ContextLength(); |
| 62 |
| 63 // Does this scope declare a "this" binding? |
| 64 bool HasReceiver(); |
| 65 |
| 66 // Does this scope declare a "this" binding, and the "this" binding is stack- |
| 67 // or context-allocated? |
| 68 bool HasAllocatedReceiver(); |
| 69 |
| 70 // Does this scope declare a "new.target" binding? |
| 71 bool HasNewTarget(); |
| 72 |
| 73 // Is this scope the scope of a named function expression? |
| 74 bool HasFunctionName(); |
| 75 |
| 76 // Return if this has context allocated locals. |
| 77 bool HasHeapAllocatedLocals(); |
| 78 |
| 79 // Return if contexts are allocated for this scope. |
| 80 bool HasContext(); |
| 81 |
| 82 // Return if this is a function scope with "use asm". |
| 83 inline bool IsAsmModule(); |
| 84 |
| 85 // Return if this is a nested function within an asm module scope. |
| 86 inline bool IsAsmFunction(); |
| 87 |
| 88 inline bool HasSimpleParameters(); |
| 89 |
| 90 // Return the function_name if present. |
| 91 String* FunctionName(); |
| 92 |
| 93 ModuleInfo* ModuleDescriptorInfo(); |
| 94 |
| 95 // Return the name of the given parameter. |
| 96 String* ParameterName(int var); |
| 97 |
| 98 // Return the name of the given local. |
| 99 String* LocalName(int var); |
| 100 |
| 101 // Return the name of the given stack local. |
| 102 String* StackLocalName(int var); |
| 103 |
| 104 // Return the name of the given stack local. |
| 105 int StackLocalIndex(int var); |
| 106 |
| 107 // Return the name of the given context local. |
| 108 String* ContextLocalName(int var); |
| 109 |
| 110 // Return the mode of the given context local. |
| 111 VariableMode ContextLocalMode(int var); |
| 112 |
| 113 // Return the initialization flag of the given context local. |
| 114 InitializationFlag ContextLocalInitFlag(int var); |
| 115 |
| 116 // Return the initialization flag of the given context local. |
| 117 MaybeAssignedFlag ContextLocalMaybeAssignedFlag(int var); |
| 118 |
| 119 // Return true if this local was introduced by the compiler, and should not be |
| 120 // exposed to the user in a debugger. |
| 121 static bool VariableIsSynthetic(String* name); |
| 122 |
| 123 // Lookup support for serialized scope info. Returns the |
| 124 // the stack slot index for a given slot name if the slot is |
| 125 // present; otherwise returns a value < 0. The name must be an internalized |
| 126 // string. |
| 127 int StackSlotIndex(String* name); |
| 128 |
| 129 // Lookup support for serialized scope info. Returns the local context slot |
| 130 // index for a given slot name if the slot is present; otherwise |
| 131 // returns a value < 0. The name must be an internalized string. |
| 132 // If the slot is present and mode != NULL, sets *mode to the corresponding |
| 133 // mode for that variable. |
| 134 static int ContextSlotIndex(Handle<ScopeInfo> scope_info, Handle<String> name, |
| 135 VariableMode* mode, InitializationFlag* init_flag, |
| 136 MaybeAssignedFlag* maybe_assigned_flag); |
| 137 |
| 138 // Lookup metadata of a MODULE-allocated variable. Return 0 if there is no |
| 139 // module variable with the given name (the index value of a MODULE variable |
| 140 // is never 0). |
| 141 int ModuleIndex(Handle<String> name, VariableMode* mode, |
| 142 InitializationFlag* init_flag, |
| 143 MaybeAssignedFlag* maybe_assigned_flag); |
| 144 |
| 145 // Lookup the name of a certain context slot by its index. |
| 146 String* ContextSlotName(int slot_index); |
| 147 |
| 148 // Lookup support for serialized scope info. Returns the |
| 149 // parameter index for a given parameter name if the parameter is present; |
| 150 // otherwise returns a value < 0. The name must be an internalized string. |
| 151 int ParameterIndex(String* name); |
| 152 |
| 153 // Lookup support for serialized scope info. Returns the function context |
| 154 // slot index if the function name is present and context-allocated (named |
| 155 // function expressions, only), otherwise returns a value < 0. The name |
| 156 // must be an internalized string. |
| 157 int FunctionContextSlotIndex(String* name); |
| 158 |
| 159 // Lookup support for serialized scope info. Returns the receiver context |
| 160 // slot index if scope has a "this" binding, and the binding is |
| 161 // context-allocated. Otherwise returns a value < 0. |
| 162 int ReceiverContextSlotIndex(); |
| 163 |
| 164 FunctionKind function_kind(); |
| 165 |
| 166 // Returns true if this ScopeInfo is linked to a outer ScopeInfo. |
| 167 bool HasOuterScopeInfo(); |
| 168 |
| 169 // Returns true if this ScopeInfo was created for a debug-evaluate scope. |
| 170 bool IsDebugEvaluateScope(); |
| 171 |
| 172 // Can be used to mark a ScopeInfo that looks like a with-scope as actually |
| 173 // being a debug-evaluate scope. |
| 174 void SetIsDebugEvaluateScope(); |
| 175 |
| 176 // Return the outer ScopeInfo if present. |
| 177 ScopeInfo* OuterScopeInfo(); |
| 178 |
| 179 #ifdef DEBUG |
| 180 bool Equals(ScopeInfo* other) const; |
| 181 #endif |
| 182 |
| 183 static Handle<ScopeInfo> Create(Isolate* isolate, Zone* zone, Scope* scope, |
| 184 MaybeHandle<ScopeInfo> outer_scope); |
| 185 static Handle<ScopeInfo> CreateForWithScope( |
| 186 Isolate* isolate, MaybeHandle<ScopeInfo> outer_scope); |
| 187 static Handle<ScopeInfo> CreateGlobalThisBinding(Isolate* isolate); |
| 188 |
| 189 // Serializes empty scope info. |
| 190 V8_EXPORT_PRIVATE static ScopeInfo* Empty(Isolate* isolate); |
| 191 |
| 192 #ifdef DEBUG |
| 193 void Print(); |
| 194 #endif |
| 195 |
| 196 // The layout of the static part of a ScopeInfo is as follows. Each entry is |
| 197 // numeric and occupies one array slot. |
| 198 // 1. A set of properties of the scope. |
| 199 // 2. The number of parameters. For non-function scopes this is 0. |
| 200 // 3. The number of non-parameter variables allocated on the stack. |
| 201 // 4. The number of non-parameter and parameter variables allocated in the |
| 202 // context. |
| 203 #define FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(V) \ |
| 204 V(Flags) \ |
| 205 V(ParameterCount) \ |
| 206 V(StackLocalCount) \ |
| 207 V(ContextLocalCount) |
| 208 |
| 209 #define FIELD_ACCESSORS(name) \ |
| 210 inline void Set##name(int value); \ |
| 211 inline int name(); |
| 212 FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(FIELD_ACCESSORS) |
| 213 #undef FIELD_ACCESSORS |
| 214 |
| 215 enum { |
| 216 #define DECL_INDEX(name) k##name, |
| 217 FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(DECL_INDEX) |
| 218 #undef DECL_INDEX |
| 219 kVariablePartIndex |
| 220 }; |
| 221 |
| 222 private: |
| 223 // The layout of the variable part of a ScopeInfo is as follows: |
| 224 // 1. ParameterNames: |
| 225 // This part stores the names of the parameters for function scopes. One |
| 226 // slot is used per parameter, so in total this part occupies |
| 227 // ParameterCount() slots in the array. For other scopes than function |
| 228 // scopes ParameterCount() is 0. |
| 229 // 2. StackLocalFirstSlot: |
| 230 // Index of a first stack slot for stack local. Stack locals belonging to |
| 231 // this scope are located on a stack at slots starting from this index. |
| 232 // 3. StackLocalNames: |
| 233 // Contains the names of local variables that are allocated on the stack, |
| 234 // in increasing order of the stack slot index. First local variable has a |
| 235 // stack slot index defined in StackLocalFirstSlot (point 2 above). |
| 236 // One slot is used per stack local, so in total this part occupies |
| 237 // StackLocalCount() slots in the array. |
| 238 // 4. ContextLocalNames: |
| 239 // Contains the names of local variables and parameters that are allocated |
| 240 // in the context. They are stored in increasing order of the context slot |
| 241 // index starting with Context::MIN_CONTEXT_SLOTS. One slot is used per |
| 242 // context local, so in total this part occupies ContextLocalCount() slots |
| 243 // in the array. |
| 244 // 5. ContextLocalInfos: |
| 245 // Contains the variable modes and initialization flags corresponding to |
| 246 // the context locals in ContextLocalNames. One slot is used per |
| 247 // context local, so in total this part occupies ContextLocalCount() |
| 248 // slots in the array. |
| 249 // 6. ReceiverInfo: |
| 250 // If the scope binds a "this" value, one slot is reserved to hold the |
| 251 // context or stack slot index for the variable. |
| 252 // 7. FunctionNameInfo: |
| 253 // If the scope belongs to a named function expression this part contains |
| 254 // information about the function variable. It always occupies two array |
| 255 // slots: a. The name of the function variable. |
| 256 // b. The context or stack slot index for the variable. |
| 257 // 8. OuterScopeInfoIndex: |
| 258 // The outer scope's ScopeInfo or the hole if there's none. |
| 259 // 9. ModuleInfo, ModuleVariableCount, and ModuleVariables: |
| 260 // For a module scope, this part contains the ModuleInfo, the number of |
| 261 // MODULE-allocated variables, and the metadata of those variables. For |
| 262 // non-module scopes it is empty. |
| 263 int ParameterNamesIndex(); |
| 264 int StackLocalFirstSlotIndex(); |
| 265 int StackLocalNamesIndex(); |
| 266 int ContextLocalNamesIndex(); |
| 267 int ContextLocalInfosIndex(); |
| 268 int ReceiverInfoIndex(); |
| 269 int FunctionNameInfoIndex(); |
| 270 int OuterScopeInfoIndex(); |
| 271 int ModuleInfoIndex(); |
| 272 int ModuleVariableCountIndex(); |
| 273 int ModuleVariablesIndex(); |
| 274 |
| 275 int Lookup(Handle<String> name, int start, int end, VariableMode* mode, |
| 276 VariableLocation* location, InitializationFlag* init_flag, |
| 277 MaybeAssignedFlag* maybe_assigned_flag); |
| 278 |
| 279 // Get metadata of i-th MODULE-allocated variable, where 0 <= i < |
| 280 // ModuleVariableCount. The metadata is returned via out-arguments, which may |
| 281 // be nullptr if the corresponding information is not requested |
| 282 void ModuleVariable(int i, String** name, int* index, |
| 283 VariableMode* mode = nullptr, |
| 284 InitializationFlag* init_flag = nullptr, |
| 285 MaybeAssignedFlag* maybe_assigned_flag = nullptr); |
| 286 |
| 287 // Used for the function name variable for named function expressions, and for |
| 288 // the receiver. |
| 289 enum VariableAllocationInfo { NONE, STACK, CONTEXT, UNUSED }; |
| 290 |
| 291 // Properties of scopes. |
| 292 class ScopeTypeField : public BitField<ScopeType, 0, 4> {}; |
| 293 class CallsEvalField : public BitField<bool, ScopeTypeField::kNext, 1> {}; |
| 294 STATIC_ASSERT(LANGUAGE_END == 2); |
| 295 class LanguageModeField |
| 296 : public BitField<LanguageMode, CallsEvalField::kNext, 1> {}; |
| 297 class DeclarationScopeField |
| 298 : public BitField<bool, LanguageModeField::kNext, 1> {}; |
| 299 class ReceiverVariableField |
| 300 : public BitField<VariableAllocationInfo, DeclarationScopeField::kNext, |
| 301 2> {}; |
| 302 class HasNewTargetField |
| 303 : public BitField<bool, ReceiverVariableField::kNext, 1> {}; |
| 304 class FunctionVariableField |
| 305 : public BitField<VariableAllocationInfo, HasNewTargetField::kNext, 2> {}; |
| 306 class AsmModuleField |
| 307 : public BitField<bool, FunctionVariableField::kNext, 1> {}; |
| 308 class AsmFunctionField : public BitField<bool, AsmModuleField::kNext, 1> {}; |
| 309 class HasSimpleParametersField |
| 310 : public BitField<bool, AsmFunctionField::kNext, 1> {}; |
| 311 class FunctionKindField |
| 312 : public BitField<FunctionKind, HasSimpleParametersField::kNext, 10> {}; |
| 313 class HasOuterScopeInfoField |
| 314 : public BitField<bool, FunctionKindField::kNext, 1> {}; |
| 315 class IsDebugEvaluateScopeField |
| 316 : public BitField<bool, HasOuterScopeInfoField::kNext, 1> {}; |
| 317 |
| 318 // Properties of variables. |
| 319 class VariableModeField : public BitField<VariableMode, 0, 3> {}; |
| 320 class InitFlagField : public BitField<InitializationFlag, 3, 1> {}; |
| 321 class MaybeAssignedFlagField : public BitField<MaybeAssignedFlag, 4, 1> {}; |
| 322 |
| 323 friend class ScopeIterator; |
| 324 }; |
| 325 |
| 326 } // namespace internal |
| 327 } // namespace v8 |
| 328 |
| 329 #include "src/objects/object-macros-undef.h" |
| 330 |
| 331 #endif // V8_OBJECTS_SCOPE_INFO_H_ |
OLD | NEW |