blob: 9fb26d033981725d7cc19a9fb0bdfe6edc410ff7 [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
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100115 // Return if this has context slots besides MIN_CONTEXT_SLOTS;
116 static bool HasHeapAllocatedLocals(Code* code);
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 // Lookup support for scope info embedded in Code objects. Returns
119 // 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
121 // (canonicalized).
122 static int StackSlotIndex(Code* code, String* name);
123
124 // Lookup support for scope info embedded in Code objects. Returns the
125 // 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).
127 // If the slot is present and mode != NULL, sets *mode to the corresponding
128 // mode for that variable.
129 static int ContextSlotIndex(Code* code, String* name, Variable::Mode* mode);
130
131 // Lookup support for scope info embedded in Code objects. Returns the
132 // 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).
134 static int ParameterIndex(Code* code, String* name);
135
136 // Lookup support for scope info embedded in Code objects. Returns the
137 // function context slot index if the function name is present (named
138 // function expressions, only), otherwise returns a value < 0. The name
139 // must be a symbol (canonicalized).
140 static int FunctionContextSlotIndex(Code* code, String* name);
141
142 // --------------------------------------------------------------------------
143 // Debugging support
144
145#ifdef DEBUG
146 void Print();
147#endif
148
149 private:
150 Handle<String> function_name_;
151 bool calls_eval_;
152 List<Handle<String>, Allocator > parameters_;
153 List<Handle<String>, Allocator > stack_slots_;
154 List<Handle<String>, Allocator > context_slots_;
155 List<Variable::Mode, Allocator > context_modes_;
156};
157
158class ZoneScopeInfo: public ScopeInfo<ZoneListAllocationPolicy> {
159 public:
160 // Create a ZoneScopeInfo instance from a scope.
161 explicit ZoneScopeInfo(Scope* scope)
162 : ScopeInfo<ZoneListAllocationPolicy>(scope) {}
163
164 // Create a ZoneScopeInfo instance from a Code object.
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.
172// Slot index equals -1 means the property is absent.
173// Cleared at startup and prior to mark sweep collection.
174class ContextSlotCache {
175 public:
176 // Lookup context slot index for (code, name).
177 // If absent, kNotFound is returned.
178 static int Lookup(Code* code,
179 String* name,
180 Variable::Mode* mode);
181
182 // Update an element in the cache.
183 static void Update(Code* code,
184 String* name,
185 Variable::Mode mode,
186 int slot_index);
187
188 // Clear the cache.
189 static void Clear();
190
191 static const int kNotFound = -2;
192 private:
193 inline static int Hash(Code* code, String* name);
194
195#ifdef DEBUG
196 static void ValidateEntry(Code* code,
197 String* name,
198 Variable::Mode mode,
199 int slot_index);
200#endif
201
202 static const int kLength = 256;
203 struct Key {
204 Code* code;
205 String* name;
206 };
207
208 struct Value {
209 Value(Variable::Mode mode, int index) {
210 ASSERT(ModeField::is_valid(mode));
211 ASSERT(IndexField::is_valid(index));
212 value_ = ModeField::encode(mode) | IndexField::encode(index);
213 ASSERT(mode == this->mode());
214 ASSERT(index == this->index());
215 }
216
217 inline Value(uint32_t value) : value_(value) {}
218
219 uint32_t raw() { return value_; }
220
221 Variable::Mode mode() { return ModeField::decode(value_); }
222
223 int index() { return IndexField::decode(value_); }
224
225 // Bit fields in value_ (type, shift, size). Must be public so the
226 // constants can be embedded in generated code.
227 class ModeField: public BitField<Variable::Mode, 0, 3> {};
228 class IndexField: public BitField<int, 3, 32-3> {};
229 private:
230 uint32_t value_;
231 };
232
233 static Key keys_[kLength];
234 static uint32_t values_[kLength];
235};
236
237
238} } // namespace v8::internal
239
240#endif // V8_SCOPEINFO_H_