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_VARIABLES_H_ 00029 #define V8_VARIABLES_H_ 00030 00031 #include "zone.h" 00032 00033 namespace v8 { namespace internal { 00034 00035 class UseCount BASE_EMBEDDED { 00036 public: 00037 UseCount(); 00038 00039 // Inform the node of a "use". The weight can be used to indicate 00040 // heavier use, for instance if the variable is accessed inside a loop. 00041 void RecordRead(int weight); 00042 void RecordWrite(int weight); 00043 void RecordAccess(int weight); // records a read & write 00044 void RecordUses(UseCount* uses); 00045 00046 int nreads() const { return nreads_; } 00047 int nwrites() const { return nwrites_; } 00048 int nuses() const { return nreads_ + nwrites_; } 00049 00050 bool is_read() const { return nreads() > 0; } 00051 bool is_written() const { return nwrites() > 0; } 00052 bool is_used() const { return nuses() > 0; } 00053 00054 #ifdef DEBUG 00055 void Print(); 00056 #endif 00057 00058 private: 00059 int nreads_; 00060 int nwrites_; 00061 }; 00062 00063 00064 // Variables and AST expression nodes can track their "type" to enable 00065 // optimizations and removal of redundant checks when generating code. 00066 00067 class StaticType BASE_EMBEDDED { 00068 public: 00069 enum Kind { 00070 UNKNOWN, 00071 LIKELY_SMI 00072 }; 00073 00074 StaticType() : kind_(UNKNOWN) {} 00075 00076 bool Is(Kind kind) const { return kind_ == kind; } 00077 00078 bool IsKnown() const { return !Is(UNKNOWN); } 00079 bool IsUnknown() const { return Is(UNKNOWN); } 00080 bool IsLikelySmi() const { return Is(LIKELY_SMI); } 00081 00082 void CopyFrom(StaticType* other) { 00083 kind_ = other->kind_; 00084 } 00085 00086 static const char* Type2String(StaticType* type); 00087 00088 // LIKELY_SMI accessors 00089 void SetAsLikelySmi() { 00090 kind_ = LIKELY_SMI; 00091 } 00092 00093 void SetAsLikelySmiIfUnknown() { 00094 if (IsUnknown()) { 00095 SetAsLikelySmi(); 00096 } 00097 } 00098 00099 private: 00100 Kind kind_; 00101 00102 DISALLOW_COPY_AND_ASSIGN(StaticType); 00103 }; 00104 00105 00106 // The AST refers to variables via VariableProxies - placeholders for the actual 00107 // variables. Variables themselves are never directly referred to from the AST, 00108 // they are maintained by scopes, and referred to from VariableProxies and Slots 00109 // after binding and variable allocation. 00110 00111 class Variable: public ZoneObject { 00112 public: 00113 enum Mode { 00114 // User declared variables: 00115 VAR, // declared via 'var', and 'function' declarations 00116 CONST, // declared via 'const' declarations 00117 00118 // Variables introduced by the compiler: 00119 DYNAMIC, // always require dynamic lookup (we don't know the declaration) 00120 INTERNAL, // like VAR, but not user-visible (may or may not be in a 00121 // context) 00122 TEMPORARY // temporary variables (not user-visible), never in a context 00123 }; 00124 00125 // Printing support 00126 static const char* Mode2String(Mode mode); 00127 00128 // Type testing & conversion 00129 Property* AsProperty(); 00130 Variable* AsVariable(); 00131 bool IsValidLeftHandSide() { return is_valid_LHS_; } 00132 00133 // The source code for an eval() call may refer to a variable that is 00134 // in an outer scope about which we don't know anything (it may not 00135 // be the global scope). scope() is NULL in that case. Currently the 00136 // scope is only used to follow the context chain length. 00137 Scope* scope() const { return scope_; } 00138 // If this assertion fails it means that some code has tried to 00139 // treat the special this variable as an ordinary variable with 00140 // the name "this". 00141 Handle<String> name() const { return name_; } 00142 Mode mode() const { return mode_; } 00143 bool is_accessed_from_inner_scope() const { 00144 return is_accessed_from_inner_scope_; 00145 } 00146 UseCount* var_uses() { return &var_uses_; } 00147 UseCount* obj_uses() { return &obj_uses_; } 00148 00149 bool IsVariable(Handle<String> n) { 00150 return !is_this() && name().is_identical_to(n); 00151 } 00152 00153 bool is_global() const; 00154 bool is_this() const { return is_this_; } 00155 00156 Expression* rewrite() const { return rewrite_; } 00157 Slot* slot() const; 00158 00159 StaticType* type() { return &type_; } 00160 00161 private: 00162 Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS, 00163 bool is_this); 00164 00165 Scope* scope_; 00166 Handle<String> name_; 00167 Mode mode_; 00168 bool is_valid_LHS_; 00169 bool is_this_; 00170 00171 // Usage info. 00172 bool is_accessed_from_inner_scope_; // set by variable resolver 00173 UseCount var_uses_; // uses of the variable value 00174 UseCount obj_uses_; // uses of the object the variable points to 00175 00176 // Static type information 00177 StaticType type_; 00178 00179 // Code generation. 00180 // rewrite_ is usually a Slot or a Property, but maybe any expression. 00181 Expression* rewrite_; 00182 00183 friend class VariableProxy; 00184 friend class Scope; 00185 friend class LocalsMap; 00186 friend class AstBuildingParser; 00187 }; 00188 00189 00190 } } // namespace v8::internal 00191 00192 #endif // V8_VARIABLES_H_