The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / variables.h @ 40c0f755

History | View | Annotate | Download (6.83 KB)

1 40c0f755 Ryan
// 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_VARIABLES_H_
29
#define V8_VARIABLES_H_
30
31
#include "zone.h"
32
33
namespace v8 { namespace internal {
34
35
class UseCount BASE_EMBEDDED {
36
 public:
37
  UseCount();
38
39
  // Inform the node of a "use". The weight can be used to indicate
40
  // heavier use, for instance if the variable is accessed inside a loop.
41
  void RecordRead(int weight);
42
  void RecordWrite(int weight);
43
  void RecordAccess(int weight);  // records a read & write
44
  void RecordUses(UseCount* uses);
45
46
  int nreads() const  { return nreads_; }
47
  int nwrites() const  { return nwrites_; }
48
  int nuses() const  { return nreads_ + nwrites_; }
49
50
  bool is_read() const  { return nreads() > 0; }
51
  bool is_written() const  { return nwrites() > 0; }
52
  bool is_used() const  { return nuses() > 0; }
53
54
#ifdef DEBUG
55
  void Print();
56
#endif
57
58
 private:
59
  int nreads_;
60
  int nwrites_;
61
};
62
63
64
// Variables and AST expression nodes can track their "type" to enable
65
// optimizations and removal of redundant checks when generating code.
66
67
class SmiAnalysis {
68
 public:
69
  enum Kind {
70
    UNKNOWN,
71
    LIKELY_SMI
72
  };
73
74
  SmiAnalysis() : kind_(UNKNOWN) {}
75
76
  bool Is(Kind kind) const { return kind_ == kind; }
77
78
  bool IsKnown() const { return !Is(UNKNOWN); }
79
  bool IsUnknown() const { return Is(UNKNOWN); }
80
  bool IsLikelySmi() const { return Is(LIKELY_SMI); }
81
82
  void CopyFrom(SmiAnalysis* other) {
83
    kind_ = other->kind_;
84
  }
85
86
  static const char* Type2String(SmiAnalysis* type);
87
88
  // LIKELY_SMI accessors
89
  void SetAsLikelySmi() {
90
    kind_ = LIKELY_SMI;
91
  }
92
93
  void SetAsLikelySmiIfUnknown() {
94
    if (IsUnknown()) {
95
      SetAsLikelySmi();
96
    }
97
  }
98
99
 private:
100
  Kind kind_;
101
102
  DISALLOW_COPY_AND_ASSIGN(SmiAnalysis);
103
};
104
105
106
// The AST refers to variables via VariableProxies - placeholders for the actual
107
// variables. Variables themselves are never directly referred to from the AST,
108
// they are maintained by scopes, and referred to from VariableProxies and Slots
109
// after binding and variable allocation.
110
111
class Variable: public ZoneObject {
112
 public:
113
  enum Mode {
114
    // User declared variables:
115
    VAR,       // declared via 'var', and 'function' declarations
116
117
    CONST,     // declared via 'const' declarations
118
119
    // Variables introduced by the compiler:
120
    DYNAMIC,         // always require dynamic lookup (we don't know
121
                     // the declaration)
122
123
    DYNAMIC_GLOBAL,  // requires dynamic lookup, but we know that the
124
                     // variable is global unless it has been shadowed
125
                     // by an eval-introduced variable
126
127
    DYNAMIC_LOCAL,   // requires dynamic lookup, but we know that the
128
                     // variable is local and where it is unless it
129
                     // has been shadowed by an eval-introduced
130
                     // variable
131
132
    INTERNAL,        // like VAR, but not user-visible (may or may not
133
                     // be in a context)
134
135
    TEMPORARY        // temporary variables (not user-visible), never
136
                     // in a context
137
  };
138
139
  // Printing support
140
  static const char* Mode2String(Mode mode);
141
142
  // Type testing & conversion
143
  Property* AsProperty();
144
  Variable* AsVariable();
145
  bool IsValidLeftHandSide() { return is_valid_LHS_; }
146
147
  // The source code for an eval() call may refer to a variable that is
148
  // in an outer scope about which we don't know anything (it may not
149
  // be the global scope). scope() is NULL in that case. Currently the
150
  // scope is only used to follow the context chain length.
151
  Scope* scope() const  { return scope_; }
152
  // If this assertion fails it means that some code has tried to
153
  // treat the special this variable as an ordinary variable with
154
  // the name "this".
155
  Handle<String> name() const  { return name_; }
156
  Mode mode() const  { return mode_; }
157
  bool is_accessed_from_inner_scope() const  {
158
    return is_accessed_from_inner_scope_;
159
  }
160
  UseCount* var_uses()  { return &var_uses_; }
161
  UseCount* obj_uses()  { return &obj_uses_; }
162
163
  bool IsVariable(Handle<String> n) {
164
    return !is_this() && name().is_identical_to(n);
165
  }
166
167
  bool is_dynamic() const {
168
    return (mode_ == DYNAMIC ||
169
            mode_ == DYNAMIC_GLOBAL ||
170
            mode_ == DYNAMIC_LOCAL);
171
  }
172
173
  bool is_global() const;
174
  bool is_this() const { return is_this_; }
175
176
  Variable* local_if_not_shadowed() const {
177
    ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
178
    return local_if_not_shadowed_;
179
  }
180
181
  void set_local_if_not_shadowed(Variable* local) {
182
    local_if_not_shadowed_ = local;
183
  }
184
185
  Expression* rewrite() const  { return rewrite_; }
186
  Slot* slot() const;
187
188
  SmiAnalysis* type() { return &type_; }
189
190
 private:
191
  Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS,
192
      bool is_this);
193
194
  Scope* scope_;
195
  Handle<String> name_;
196
  Mode mode_;
197
  bool is_valid_LHS_;
198
  bool is_this_;
199
200
  Variable* local_if_not_shadowed_;
201
202
  // Usage info.
203
  bool is_accessed_from_inner_scope_;  // set by variable resolver
204
  UseCount var_uses_;  // uses of the variable value
205
  UseCount obj_uses_;  // uses of the object the variable points to
206
207
  // Static type information
208
  SmiAnalysis type_;
209
210
  // Code generation.
211
  // rewrite_ is usually a Slot or a Property, but maybe any expression.
212
  Expression* rewrite_;
213
214
  friend class VariableProxy;
215
  friend class Scope;
216
  friend class LocalsMap;
217
  friend class AstBuildingParser;
218
};
219
220
221
} }  // namespace v8::internal
222
223
#endif  // V8_VARIABLES_H_