00001 // Copyright 2006-2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_SCOPEINFO_H_ 00029 #define V8_SCOPEINFO_H_ 00030 00031 #include "variables.h" 00032 00033 namespace v8 { namespace internal { 00034 00035 // Scope information represents information about a functions's 00036 // scopes (currently only one, because we don't do any inlining) 00037 // and the allocation of the scope's variables. Scope information 00038 // is stored in a compressed form with Code objects and is used 00039 // at runtime (stack dumps, deoptimization, etc.). 00040 // 00041 // Historical note: In other VMs built by this team, ScopeInfo was 00042 // usually called DebugInfo since the information was used (among 00043 // other things) for on-demand debugging (Self, Smalltalk). However, 00044 // DebugInfo seems misleading, since this information is primarily used 00045 // in debugging-unrelated contexts. 00046 00047 // Forward defined as 00048 // template <class Allocator = FreeStoreAllocationPolicy> class ScopeInfo; 00049 template<class Allocator> 00050 class ScopeInfo BASE_EMBEDDED { 00051 public: 00052 // Create a ScopeInfo instance from a scope. 00053 explicit ScopeInfo(Scope* scope); 00054 00055 // Create a ScopeInfo instance from a Code object. 00056 explicit ScopeInfo(Code* code); 00057 00058 // Write the ScopeInfo data into a Code object, and returns the 00059 // amount of space that was needed. If no Code object is provided 00060 // (NULL handle), Serialize() only returns the amount of space needed. 00061 // 00062 // This operations requires that the Code object has the correct amount 00063 // of space for the ScopeInfo data; otherwise the operation fails (fatal 00064 // error). Any existing scope info in the Code object is simply overwritten. 00065 int Serialize(Code* code); 00066 00067 // Garbage collection support for scope info embedded in Code objects. 00068 // This code is in ScopeInfo because only here we should have to know 00069 // about the encoding. 00070 static void IterateScopeInfo(Code* code, ObjectVisitor* v); 00071 00072 00073 // -------------------------------------------------------------------------- 00074 // Lookup 00075 00076 Handle<String> function_name() const { return function_name_; } 00077 00078 bool supports_eval() const { return supports_eval_; } 00079 00080 Handle<String> parameter_name(int i) const { return parameters_[i]; } 00081 int number_of_parameters() const { return parameters_.length(); } 00082 00083 Handle<String> stack_slot_name(int i) const { return stack_slots_[i]; } 00084 int number_of_stack_slots() const { return stack_slots_.length(); } 00085 00086 Handle<String> context_slot_name(int i) const { 00087 return context_slots_[i - Context::MIN_CONTEXT_SLOTS]; 00088 } 00089 int number_of_context_slots() const { 00090 int l = context_slots_.length(); 00091 return l == 0 ? 0 : l + Context::MIN_CONTEXT_SLOTS; 00092 } 00093 00094 Handle<String> LocalName(int i) const; 00095 int NumberOfLocals() const; 00096 00097 00098 // -------------------------------------------------------------------------- 00099 // The following functions provide quick access to scope info details 00100 // for runtime routines w/o the need to explicitly create a ScopeInfo 00101 // object. 00102 // 00103 // ScopeInfo is the only class which should have to know about the 00104 // encoding of it's information in a Code object, which is why these 00105 // functions are in this class. 00106 00107 static bool SupportsEval(Code* code); 00108 00109 // Return the number of stack slots for code. 00110 static int NumberOfStackSlots(Code* code); 00111 00112 // Return the number of context slots for code. 00113 static int NumberOfContextSlots(Code* code); 00114 00115 // Lookup support for scope info embedded in Code objects. Returns 00116 // the stack slot index for a given slot name if the slot is 00117 // present; otherwise returns a value < 0. The name must be a symbol 00118 // (canonicalized). 00119 static int StackSlotIndex(Code* code, String* name); 00120 00121 // Lookup support for scope info embedded in Code objects. Returns the 00122 // context slot index for a given slot name if the slot is present; otherwise 00123 // returns a value < 0. The name must be a symbol (canonicalized). 00124 // If the slot is present and mode != NULL, sets *mode to the corresponding 00125 // mode for that variable. 00126 static int ContextSlotIndex(Code* code, String* name, Variable::Mode* mode); 00127 00128 // Lookup support for scope info embedded in Code objects. Returns the 00129 // parameter index for a given parameter name if the parameter is present; 00130 // otherwise returns a value < 0. The name must be a symbol (canonicalized). 00131 static int ParameterIndex(Code* code, String* name); 00132 00133 // Lookup support for scope info embedded in Code objects. Returns the 00134 // function context slot index if the function name is present (named 00135 // function expressions, only), otherwise returns a value < 0. The name 00136 // must be a symbol (canonicalized). 00137 static int FunctionContextSlotIndex(Code* code, String* name); 00138 00139 00140 // -------------------------------------------------------------------------- 00141 // Debugging support 00142 00143 #ifdef DEBUG 00144 void Print(); 00145 #endif 00146 00147 private: 00148 Handle<String> function_name_; 00149 bool supports_eval_; 00150 List<Handle<String>, Allocator > parameters_; 00151 List<Handle<String>, Allocator > stack_slots_; 00152 List<Handle<String>, Allocator > context_slots_; 00153 List<Variable::Mode, Allocator > context_modes_; 00154 }; 00155 00156 } } // namespace v8::internal 00157 00158 #endif // V8_SCOPEINFO_H_