blob: 285280cdde99491a43d5b0851c23027377b7093e [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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"
32
33namespace v8 { namespace internal {
34
35// Scope information represents information about a functions's
36// scopes (currently only one, because we don't do any inlining)
37// and the allocation of the scope's variables. Scope information
38// is stored in a compressed form with Code objects and is used
39// at runtime (stack dumps, deoptimization, etc.).
40//
41// Historical note: In other VMs built by this team, ScopeInfo was
42// usually called DebugInfo since the information was used (among
43// other things) for on-demand debugging (Self, Smalltalk). However,
44// DebugInfo seems misleading, since this information is primarily used
45// in debugging-unrelated contexts.
46
47// Forward defined as
48// template <class Allocator = FreeStoreAllocationPolicy> class ScopeInfo;
49template<class Allocator>
50class ScopeInfo BASE_EMBEDDED {
51 public:
52 // Create a ScopeInfo instance from a scope.
53 explicit ScopeInfo(Scope* scope);
54
55 // Create a ScopeInfo instance from a Code object.
56 explicit ScopeInfo(Code* code);
57
58 // Write the ScopeInfo data into a Code object, and returns the
59 // amount of space that was needed. If no Code object is provided
60 // (NULL handle), Serialize() only returns the amount of space needed.
61 //
62 // This operations requires that the Code object has the correct amount
63 // of space for the ScopeInfo data; otherwise the operation fails (fatal
64 // error). Any existing scope info in the Code object is simply overwritten.
65 int Serialize(Code* code);
66
67 // Garbage collection support for scope info embedded in Code objects.
68 // This code is in ScopeInfo because only here we should have to know
69 // about the encoding.
70 static void IterateScopeInfo(Code* code, ObjectVisitor* v);
71
72
73 // --------------------------------------------------------------------------
74 // Lookup
75
76 Handle<String> function_name() const { return function_name_; }
77
78 bool supports_eval() const { return supports_eval_; }
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 // --------------------------------------------------------------------------
99 // The following functions provide quick access to scope info details
100 // for runtime routines w/o the need to explicitly create a ScopeInfo
101 // object.
102 //
103 // ScopeInfo is the only class which should have to know about the
104 // encoding of it's information in a Code object, which is why these
105 // functions are in this class.
106
107 static bool SupportsEval(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 // --------------------------------------------------------------------------
141 // Debugging support
142
143#ifdef DEBUG
144 void Print();
145#endif
146
147 private:
148 Handle<String> function_name_;
149 bool supports_eval_;
150 List<Handle<String>, Allocator > parameters_;
151 List<Handle<String>, Allocator > stack_slots_;
152 List<Handle<String>, Allocator > context_slots_;
153 List<Variable::Mode, Allocator > context_modes_;
154};
155
156} } // namespace v8::internal
157
158#endif // V8_SCOPEINFO_H_