blob: 927ac66fc4bee432d801537c7aaf8f83494e6a70 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 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_SCOPEINFO_H_
29#define V8_SCOPEINFO_H_
30
31#include "variables.h"
Steve Block6ded16b2010-05-10 14:33:55 +010032#include "zone-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34namespace v8 {
35namespace internal {
36
37// Scope information represents information about a functions's
38// scopes (currently only one, because we don't do any inlining)
39// and the allocation of the scope's variables. Scope information
40// is stored in a compressed form with Code objects and is used
41// at runtime (stack dumps, deoptimization, etc.).
42//
43// Historical note: In other VMs built by this team, ScopeInfo was
44// usually called DebugInfo since the information was used (among
45// other things) for on-demand debugging (Self, Smalltalk). However,
46// DebugInfo seems misleading, since this information is primarily used
47// in debugging-unrelated contexts.
48
49// Forward defined as
50// template <class Allocator = FreeStoreAllocationPolicy> class ScopeInfo;
51template<class Allocator>
52class ScopeInfo BASE_EMBEDDED {
53 public:
54 // Create a ScopeInfo instance from a scope.
55 explicit ScopeInfo(Scope* scope);
56
57 // Create a ScopeInfo instance from a Code object.
58 explicit ScopeInfo(Code* code);
59
60 // Write the ScopeInfo data into a Code object, and returns the
61 // amount of space that was needed. If no Code object is provided
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
69 // Garbage collection support for scope info embedded in Code objects.
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
74
75 // --------------------------------------------------------------------------
76 // Lookup
77
78 Handle<String> function_name() const { return function_name_; }
79
80 Handle<String> parameter_name(int i) const { return parameters_[i]; }
81 int number_of_parameters() const { return parameters_.length(); }
82
83 Handle<String> stack_slot_name(int i) const { return stack_slots_[i]; }
84 int number_of_stack_slots() const { return stack_slots_.length(); }
85
86 Handle<String> context_slot_name(int i) const {
87 return context_slots_[i - Context::MIN_CONTEXT_SLOTS];
88 }
89 int number_of_context_slots() const {
90 int l = context_slots_.length();
91 return l == 0 ? 0 : l + Context::MIN_CONTEXT_SLOTS;
92 }
93
94 Handle<String> LocalName(int i) const;
95 int NumberOfLocals() const;
96
97 // --------------------------------------------------------------------------
98 // The following functions provide quick access to scope info details
99 // for runtime routines w/o the need to explicitly create a ScopeInfo
100 // object.
101 //
102 // 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
104 // functions are in this class.
105
106 // Does this scope call eval.
107 static bool CallsEval(Code* code);
108
109 // Return the number of stack slots for code.
110 static int NumberOfStackSlots(Code* code);
111
112 // Return the number of context slots for code.
113 static int NumberOfContextSlots(Code* code);
114
115 // Lookup support for scope info embedded in Code objects. Returns
116 // the stack slot index for a given slot name if the slot is
117 // present; otherwise returns a value < 0. The name must be a symbol
118 // (canonicalized).
119 static int StackSlotIndex(Code* code, String* name);
120
121 // Lookup support for scope info embedded in Code objects. Returns the
122 // context slot index for a given slot name if the slot is present; otherwise
123 // returns a value < 0. The name must be a symbol (canonicalized).
124 // If the slot is present and mode != NULL, sets *mode to the corresponding
125 // mode for that variable.
126 static int ContextSlotIndex(Code* code, String* name, Variable::Mode* mode);
127
128 // Lookup support for scope info embedded in Code objects. Returns the
129 // parameter index for a given parameter name if the parameter is present;
130 // otherwise returns a value < 0. The name must be a symbol (canonicalized).
131 static int ParameterIndex(Code* code, String* name);
132
133 // Lookup support for scope info embedded in Code objects. Returns the
134 // function context slot index if the function name is present (named
135 // function expressions, only), otherwise returns a value < 0. The name
136 // must be a symbol (canonicalized).
137 static int FunctionContextSlotIndex(Code* code, String* name);
138
139 // --------------------------------------------------------------------------
140 // Debugging support
141
142#ifdef DEBUG
143 void Print();
144#endif
145
146 private:
147 Handle<String> function_name_;
148 bool calls_eval_;
149 List<Handle<String>, Allocator > parameters_;
150 List<Handle<String>, Allocator > stack_slots_;
151 List<Handle<String>, Allocator > context_slots_;
152 List<Variable::Mode, Allocator > context_modes_;
153};
154
155class ZoneScopeInfo: public ScopeInfo<ZoneListAllocationPolicy> {
156 public:
157 // Create a ZoneScopeInfo instance from a scope.
158 explicit ZoneScopeInfo(Scope* scope)
159 : ScopeInfo<ZoneListAllocationPolicy>(scope) {}
160
161 // Create a ZoneScopeInfo instance from a Code object.
162 explicit ZoneScopeInfo(Code* code)
163 : ScopeInfo<ZoneListAllocationPolicy>(code) {}
164};
165
166
167// Cache for mapping (code, property name) into context slot index.
168// The cache contains both positive and negative results.
169// Slot index equals -1 means the property is absent.
170// Cleared at startup and prior to mark sweep collection.
171class ContextSlotCache {
172 public:
173 // Lookup context slot index for (code, name).
174 // If absent, kNotFound is returned.
175 static int Lookup(Code* code,
176 String* name,
177 Variable::Mode* mode);
178
179 // Update an element in the cache.
180 static void Update(Code* code,
181 String* name,
182 Variable::Mode mode,
183 int slot_index);
184
185 // Clear the cache.
186 static void Clear();
187
188 static const int kNotFound = -2;
189 private:
190 inline static int Hash(Code* code, String* name);
191
192#ifdef DEBUG
193 static void ValidateEntry(Code* code,
194 String* name,
195 Variable::Mode mode,
196 int slot_index);
197#endif
198
199 static const int kLength = 256;
200 struct Key {
201 Code* code;
202 String* name;
203 };
204
205 struct Value {
206 Value(Variable::Mode mode, int index) {
207 ASSERT(ModeField::is_valid(mode));
208 ASSERT(IndexField::is_valid(index));
209 value_ = ModeField::encode(mode) | IndexField::encode(index);
210 ASSERT(mode == this->mode());
211 ASSERT(index == this->index());
212 }
213
214 inline Value(uint32_t value) : value_(value) {}
215
216 uint32_t raw() { return value_; }
217
218 Variable::Mode mode() { return ModeField::decode(value_); }
219
220 int index() { return IndexField::decode(value_); }
221
222 // Bit fields in value_ (type, shift, size). Must be public so the
223 // constants can be embedded in generated code.
224 class ModeField: public BitField<Variable::Mode, 0, 3> {};
225 class IndexField: public BitField<int, 3, 32-3> {};
226 private:
227 uint32_t value_;
228 };
229
230 static Key keys_[kLength];
231 static uint32_t values_[kLength];
232};
233
234
235} } // namespace v8::internal
236
237#endif // V8_SCOPEINFO_H_