OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 #include "variables.h" | 31 #include "variables.h" |
32 #include "zone-inl.h" | 32 #include "zone-inl.h" |
33 | 33 |
34 namespace v8 { | 34 namespace v8 { |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 // Scope information represents information about a functions's | 37 // Scope information represents information about a functions's |
38 // scopes (currently only one, because we don't do any inlining) | 38 // scopes (currently only one, because we don't do any inlining) |
39 // and the allocation of the scope's variables. Scope information | 39 // and the allocation of the scope's variables. Scope information |
40 // is stored in a compressed form with Code objects and is used | 40 // is stored in a compressed form in FixedArray objects and is used |
41 // at runtime (stack dumps, deoptimization, etc.). | 41 // at runtime (stack dumps, deoptimization, etc.). |
42 // | 42 // |
43 // Historical note: In other VMs built by this team, ScopeInfo was | 43 // Historical note: In other VMs built by this team, ScopeInfo was |
44 // usually called DebugInfo since the information was used (among | 44 // usually called DebugInfo since the information was used (among |
45 // other things) for on-demand debugging (Self, Smalltalk). However, | 45 // other things) for on-demand debugging (Self, Smalltalk). However, |
46 // DebugInfo seems misleading, since this information is primarily used | 46 // DebugInfo seems misleading, since this information is primarily used |
47 // in debugging-unrelated contexts. | 47 // in debugging-unrelated contexts. |
48 | 48 |
49 // Forward defined as | 49 // Forward defined as |
50 // template <class Allocator = FreeStoreAllocationPolicy> class ScopeInfo; | 50 // template <class Allocator = FreeStoreAllocationPolicy> class ScopeInfo; |
51 template<class Allocator> | 51 template<class Allocator> |
52 class ScopeInfo BASE_EMBEDDED { | 52 class ScopeInfo BASE_EMBEDDED { |
53 public: | 53 public: |
54 // Create a ScopeInfo instance from a scope. | 54 // Create a ScopeInfo instance from a scope. |
55 explicit ScopeInfo(Scope* scope); | 55 explicit ScopeInfo(Scope* scope); |
56 | 56 |
57 // Create a ScopeInfo instance from a Code object. | 57 // Create a ScopeInfo instance from an Object holding the serialized data. |
58 explicit ScopeInfo(Code* code); | 58 explicit ScopeInfo(Object* data); |
59 | 59 |
60 // Write the ScopeInfo data into a Code object, and returns the | 60 // Creates a heap object holding the serialized scope info. |
61 // amount of space that was needed. If no Code object is provided | 61 Handle<Object> Serialize(); |
62 // (NULL handle), Serialize() only returns the amount of space needed. | |
63 // | |
64 // This operations requires that the Code object has the correct amount | |
65 // of space for the ScopeInfo data; otherwise the operation fails (fatal | |
66 // error). Any existing scope info in the Code object is simply overwritten. | |
67 int Serialize(Code* code); | |
68 | 62 |
69 // Garbage collection support for scope info embedded in Code objects. | 63 static Handle<Object> CreateHeapObject(Scope* scope); |
70 // This code is in ScopeInfo because only here we should have to know | |
71 // about the encoding. | |
72 static void IterateScopeInfo(Code* code, ObjectVisitor* v); | |
73 | 64 |
| 65 // Serializes empty scope info. |
| 66 static Object* EmptyHeapObject(); |
74 | 67 |
75 // -------------------------------------------------------------------------- | 68 // -------------------------------------------------------------------------- |
76 // Lookup | 69 // Lookup |
77 | 70 |
78 Handle<String> function_name() const { return function_name_; } | 71 Handle<String> function_name() const { return function_name_; } |
79 | 72 |
80 Handle<String> parameter_name(int i) const { return parameters_[i]; } | 73 Handle<String> parameter_name(int i) const { return parameters_[i]; } |
81 int number_of_parameters() const { return parameters_.length(); } | 74 int number_of_parameters() const { return parameters_.length(); } |
82 | 75 |
83 Handle<String> stack_slot_name(int i) const { return stack_slots_[i]; } | 76 Handle<String> stack_slot_name(int i) const { return stack_slots_[i]; } |
84 int number_of_stack_slots() const { return stack_slots_.length(); } | 77 int number_of_stack_slots() const { return stack_slots_.length(); } |
85 | 78 |
86 Handle<String> context_slot_name(int i) const { | 79 Handle<String> context_slot_name(int i) const { |
87 return context_slots_[i - Context::MIN_CONTEXT_SLOTS]; | 80 return context_slots_[i - Context::MIN_CONTEXT_SLOTS]; |
88 } | 81 } |
89 int number_of_context_slots() const { | 82 int number_of_context_slots() const { |
90 int l = context_slots_.length(); | 83 int l = context_slots_.length(); |
91 return l == 0 ? 0 : l + Context::MIN_CONTEXT_SLOTS; | 84 return l == 0 ? 0 : l + Context::MIN_CONTEXT_SLOTS; |
92 } | 85 } |
93 | 86 |
94 Handle<String> LocalName(int i) const; | 87 Handle<String> LocalName(int i) const; |
95 int NumberOfLocals() const; | 88 int NumberOfLocals() const; |
96 | 89 |
97 // -------------------------------------------------------------------------- | 90 // -------------------------------------------------------------------------- |
98 // The following functions provide quick access to scope info details | 91 // The following functions provide quick access to scope info details |
99 // for runtime routines w/o the need to explicitly create a ScopeInfo | 92 // for runtime routines w/o the need to explicitly create a ScopeInfo |
100 // object. | 93 // object. |
101 // | 94 // |
102 // ScopeInfo is the only class which should have to know about the | 95 // ScopeInfo is the only class which should have to know about the |
103 // encoding of it's information in a Code object, which is why these | 96 // encoding of it's information in a FixedArray object, which is why these |
104 // functions are in this class. | 97 // functions are in this class. |
105 | 98 |
106 // Does this scope call eval. | 99 // Does this scope call eval. |
107 static bool CallsEval(Code* code); | 100 static bool CallsEval(Object* data); |
108 | 101 |
109 // Return the number of stack slots for code. | 102 // Return the number of stack slots for code. |
110 static int NumberOfStackSlots(Code* code); | 103 static int NumberOfStackSlots(Object* data); |
111 | 104 |
112 // Return the number of context slots for code. | 105 // Return the number of context slots for code. |
113 static int NumberOfContextSlots(Code* code); | 106 static int NumberOfContextSlots(Object* data); |
114 | 107 |
115 // Return if this has context slots besides MIN_CONTEXT_SLOTS; | 108 // Return if this has context slots besides MIN_CONTEXT_SLOTS; |
116 static bool HasHeapAllocatedLocals(Code* code); | 109 static bool HasHeapAllocatedLocals(Object* data); |
117 | 110 |
118 // Lookup support for scope info embedded in Code objects. Returns | 111 // Lookup support for serialized scope info. Returns the |
119 // the stack slot index for a given slot name if the slot is | 112 // the stack slot index for a given slot name if the slot is |
120 // present; otherwise returns a value < 0. The name must be a symbol | 113 // present; otherwise returns a value < 0. The name must be a symbol |
121 // (canonicalized). | 114 // (canonicalized). |
122 static int StackSlotIndex(Code* code, String* name); | 115 static int StackSlotIndex(Object* data, String* name); |
123 | 116 |
124 // Lookup support for scope info embedded in Code objects. Returns the | 117 // Lookup support for serialized scope info. Returns the |
125 // context slot index for a given slot name if the slot is present; otherwise | 118 // context slot index for a given slot name if the slot is present; otherwise |
126 // returns a value < 0. The name must be a symbol (canonicalized). | 119 // returns a value < 0. The name must be a symbol (canonicalized). |
127 // If the slot is present and mode != NULL, sets *mode to the corresponding | 120 // If the slot is present and mode != NULL, sets *mode to the corresponding |
128 // mode for that variable. | 121 // mode for that variable. |
129 static int ContextSlotIndex(Code* code, String* name, Variable::Mode* mode); | 122 static int ContextSlotIndex(Object* data, String* name, Variable::Mode* mode); |
130 | 123 |
131 // Lookup support for scope info embedded in Code objects. Returns the | 124 // Lookup support for serialized scope info. Returns the |
132 // parameter index for a given parameter name if the parameter is present; | 125 // parameter index for a given parameter name if the parameter is present; |
133 // otherwise returns a value < 0. The name must be a symbol (canonicalized). | 126 // otherwise returns a value < 0. The name must be a symbol (canonicalized). |
134 static int ParameterIndex(Code* code, String* name); | 127 static int ParameterIndex(Object* data, String* name); |
135 | 128 |
136 // Lookup support for scope info embedded in Code objects. Returns the | 129 // Lookup support for serialized scope info. Returns the |
137 // function context slot index if the function name is present (named | 130 // function context slot index if the function name is present (named |
138 // function expressions, only), otherwise returns a value < 0. The name | 131 // function expressions, only), otherwise returns a value < 0. The name |
139 // must be a symbol (canonicalized). | 132 // must be a symbol (canonicalized). |
140 static int FunctionContextSlotIndex(Code* code, String* name); | 133 static int FunctionContextSlotIndex(Object* data, String* name); |
141 | 134 |
142 // -------------------------------------------------------------------------- | 135 // -------------------------------------------------------------------------- |
143 // Debugging support | 136 // Debugging support |
144 | 137 |
145 #ifdef DEBUG | 138 #ifdef DEBUG |
146 void Print(); | 139 void Print(); |
147 #endif | 140 #endif |
148 | 141 |
149 private: | 142 private: |
150 Handle<String> function_name_; | 143 Handle<String> function_name_; |
151 bool calls_eval_; | 144 bool calls_eval_; |
152 List<Handle<String>, Allocator > parameters_; | 145 List<Handle<String>, Allocator > parameters_; |
153 List<Handle<String>, Allocator > stack_slots_; | 146 List<Handle<String>, Allocator > stack_slots_; |
154 List<Handle<String>, Allocator > context_slots_; | 147 List<Handle<String>, Allocator > context_slots_; |
155 List<Variable::Mode, Allocator > context_modes_; | 148 List<Variable::Mode, Allocator > context_modes_; |
156 }; | 149 }; |
157 | 150 |
158 class ZoneScopeInfo: public ScopeInfo<ZoneListAllocationPolicy> { | |
159 public: | |
160 // Create a ZoneScopeInfo instance from a scope. | |
161 explicit ZoneScopeInfo(Scope* scope) | |
162 : ScopeInfo<ZoneListAllocationPolicy>(scope) {} | |
163 | 151 |
164 // Create a ZoneScopeInfo instance from a Code object. | 152 // Cache for mapping (data, property name) into context slot index. |
165 explicit ZoneScopeInfo(Code* code) | |
166 : ScopeInfo<ZoneListAllocationPolicy>(code) {} | |
167 }; | |
168 | |
169 | |
170 // Cache for mapping (code, property name) into context slot index. | |
171 // The cache contains both positive and negative results. | 153 // The cache contains both positive and negative results. |
172 // Slot index equals -1 means the property is absent. | 154 // Slot index equals -1 means the property is absent. |
173 // Cleared at startup and prior to mark sweep collection. | 155 // Cleared at startup and prior to mark sweep collection. |
174 class ContextSlotCache { | 156 class ContextSlotCache { |
175 public: | 157 public: |
176 // Lookup context slot index for (code, name). | 158 // Lookup context slot index for (data, name). |
177 // If absent, kNotFound is returned. | 159 // If absent, kNotFound is returned. |
178 static int Lookup(Code* code, | 160 static int Lookup(Object* data, |
179 String* name, | 161 String* name, |
180 Variable::Mode* mode); | 162 Variable::Mode* mode); |
181 | 163 |
182 // Update an element in the cache. | 164 // Update an element in the cache. |
183 static void Update(Code* code, | 165 static void Update(Object* data, |
184 String* name, | 166 String* name, |
185 Variable::Mode mode, | 167 Variable::Mode mode, |
186 int slot_index); | 168 int slot_index); |
187 | 169 |
188 // Clear the cache. | 170 // Clear the cache. |
189 static void Clear(); | 171 static void Clear(); |
190 | 172 |
191 static const int kNotFound = -2; | 173 static const int kNotFound = -2; |
192 private: | 174 private: |
193 inline static int Hash(Code* code, String* name); | 175 inline static int Hash(Object* data, String* name); |
194 | 176 |
195 #ifdef DEBUG | 177 #ifdef DEBUG |
196 static void ValidateEntry(Code* code, | 178 static void ValidateEntry(Object* data, |
197 String* name, | 179 String* name, |
198 Variable::Mode mode, | 180 Variable::Mode mode, |
199 int slot_index); | 181 int slot_index); |
200 #endif | 182 #endif |
201 | 183 |
202 static const int kLength = 256; | 184 static const int kLength = 256; |
203 struct Key { | 185 struct Key { |
204 Code* code; | 186 Object* data; |
205 String* name; | 187 String* name; |
206 }; | 188 }; |
207 | 189 |
208 struct Value { | 190 struct Value { |
209 Value(Variable::Mode mode, int index) { | 191 Value(Variable::Mode mode, int index) { |
210 ASSERT(ModeField::is_valid(mode)); | 192 ASSERT(ModeField::is_valid(mode)); |
211 ASSERT(IndexField::is_valid(index)); | 193 ASSERT(IndexField::is_valid(index)); |
212 value_ = ModeField::encode(mode) | IndexField::encode(index); | 194 value_ = ModeField::encode(mode) | IndexField::encode(index); |
213 ASSERT(mode == this->mode()); | 195 ASSERT(mode == this->mode()); |
214 ASSERT(index == this->index()); | 196 ASSERT(index == this->index()); |
(...skipping 16 matching lines...) Expand all Loading... |
231 }; | 213 }; |
232 | 214 |
233 static Key keys_[kLength]; | 215 static Key keys_[kLength]; |
234 static uint32_t values_[kLength]; | 216 static uint32_t values_[kLength]; |
235 }; | 217 }; |
236 | 218 |
237 | 219 |
238 } } // namespace v8::internal | 220 } } // namespace v8::internal |
239 | 221 |
240 #endif // V8_SCOPEINFO_H_ | 222 #endif // V8_SCOPEINFO_H_ |
OLD | NEW |