Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Side by Side Diff: src/ast-value-factory.h

Issue 335293004: New try: Parser: Delay internalizing strings and values (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: variable renaming + passing const & Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ast.cc ('k') | src/ast-value-factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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_AST_VALUE_FACTORY_H_
29 #define V8_AST_VALUE_FACTORY_H_
30
31 #include "src/api.h"
32 #include "src/hashmap.h"
33 #include "src/utils.h"
34
35 // AstString, AstValue and AstValueFactory are for storing strings and values
36 // independent of the V8 heap and internalizing them later. During parsing,
37 // AstStrings and AstValues are created and stored outside the heap, in
38 // AstValueFactory. After parsing, the strings and values are internalized
39 // (moved into the V8 heap).
40 namespace v8 {
41 namespace internal {
42
43 class AstString : public ZoneObject {
44 public:
45 AstString(bool is_one_byte, Vector<const byte> literal_bytes, int hash)
46 : type_(NORMAL),
47 is_one_byte_(is_one_byte),
48 literal_bytes_(literal_bytes),
49 hash_(hash),
50 left_(NULL),
51 right_(NULL) {}
52
53 AstString()
54 : type_(NORMAL),
55 is_one_byte_(true),
56 hash_(0),
57 left_(NULL),
58 right_(NULL) {}
59
60 // Creates a cons string. The only allowed operations for cons strings are
61 // is_one_byte(), length(), Internalize() and string().
62 AstString(const AstString* left, const AstString* right)
63 : type_(CONS),
64 is_one_byte_(left->is_one_byte_ && right->is_one_byte_),
65 hash_(0),
66 left_(left),
67 right_(right) {}
68
69 bool AsArrayIndex(uint32_t* index) const;
70
71 // The string is not null-terminated, use length() to find out the length.
72 const unsigned char* raw_data() const {
73 ASSERT(type_ == NORMAL);
74 return literal_bytes_.start();
75 }
76 int length() const {
77 if (type_ == CONS) {
78 return left_->length() + right_->length();
79 }
80 if (is_one_byte_)
81 return literal_bytes_.length();
82 return literal_bytes_.length() / 2;
83 }
84 bool is_one_byte() const { return is_one_byte_; }
85 bool IsEmpty() const { return length() == 0; }
86 bool IsOneByteEqualTo(const char* data) const;
87 uint16_t FirstCharacter() const {
88 ASSERT(type_ == NORMAL);
89 if (is_one_byte_)
90 return literal_bytes_[0];
91 const uint16_t* c =
92 reinterpret_cast<const uint16_t*>(literal_bytes_.start());
93 return *c;
94 }
95
96 // Puts the string into the V8 heap.
97 void Internalize(Isolate* isolate);
98
99 // This function can be called after internalizing.
100 V8_INLINE Handle<String> string() const {
101 ASSERT(!string_.is_null());
102 return string_;
103 }
104
105 // For storing AstStrings in a hash map.
106 int hash() const {
107 ASSERT(type_ == NORMAL);
108 return hash_;
109 }
110 static bool Compare(void* a, void* b);
111
112 private:
113 friend class AstValueFactory;
114
115 enum Type {
116 NORMAL,
117 CONS
118 };
119
120 Type type_;
121 bool is_one_byte_;
122
123 // For normal strings. Points to memory owned by Zone.
124 Vector<const byte> literal_bytes_;
125 int hash_;
126
127 // For cons strings.
128 const AstString* left_;
129 const AstString* right_;
130
131 // This is null until the string is internalized.
132 Handle<String> string_;
133 };
134
135
136 // AstValue is either a string, a number, a string array, a boolean, or a
137 // special value (null, undefined, the hole).
138 class AstValue : public ZoneObject {
139 public:
140 bool IsString() const {
141 return type_ == STRING;
142 }
143
144 bool IsNumber() const {
145 return type_ == NUMBER || type_ == SMI;
146 }
147
148 const AstString* AsString() const {
149 if (type_ == STRING)
150 return string_;
151 UNREACHABLE();
152 return 0;
153 }
154
155 double AsNumber() const {
156 if (type_ == NUMBER)
157 return number_;
158 if (type_ == SMI)
159 return smi_;
160 UNREACHABLE();
161 return 0;
162 }
163
164 bool EqualsString(const AstString* string) const {
165 return type_ == STRING && string_ == string;
166 }
167
168 bool IsPropertyName() const;
169
170 bool BooleanValue() const;
171
172 void Internalize(Isolate* isolate);
173
174 // Can be called after Internalize has been called.
175 V8_INLINE Handle<Object> value() const {
176 if (type_ == STRING) {
177 return string_->string();
178 }
179 ASSERT(!value_.is_null());
180 return value_;
181 }
182
183 private:
184 friend class AstValueFactory;
185
186 enum Type {
187 STRING,
188 SYMBOL,
189 NUMBER,
190 SMI,
191 BOOLEAN,
192 STRING_ARRAY,
193 NULL_TYPE,
194 UNDEFINED,
195 THE_HOLE
196 };
197
198 explicit AstValue(const AstString* s) : type_(STRING) { string_ = s; }
199
200 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; }
201
202 explicit AstValue(double n) : type_(NUMBER) { number_ = n; }
203
204 AstValue(Type t, int i) : type_(t) {
205 ASSERT(type_ == SMI);
206 smi_ = i;
207 }
208
209 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; }
210
211 explicit AstValue(ZoneList<const AstString*>* s) : type_(STRING_ARRAY) {
212 strings_ = s;
213 }
214
215 explicit AstValue(Type t) : type_(t) {
216 ASSERT(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE);
217 }
218
219 Type type_;
220
221 // Uninternalized value.
222 union {
223 const AstString* string_;
224 double number_;
225 int smi_;
226 bool bool_;
227 ZoneList<const AstString*>* strings_;
228 const char* symbol_name_;
229 };
230
231 // Internalized value (empty before internalized).
232 Handle<Object> value_;
233 };
234
235
236 // For generating string constants.
237 #define STRING_CONSTANTS(F) \
238 F(anonymous_function, "(anonymous function)") \
239 F(arguments, "arguments") \
240 F(done, "done") \
241 F(dot, ".") \
242 F(dot_for, ".for") \
243 F(dot_generator, ".generator") \
244 F(dot_generator_object, ".generator_object") \
245 F(dot_iterable, ".iterable") \
246 F(dot_iterator, ".iterator") \
247 F(dot_module, ".module") \
248 F(dot_result, ".result") \
249 F(empty, "") \
250 F(eval, "eval") \
251 F(initialize_const_global, "initializeConstGlobal") \
252 F(initialize_var_global, "initializeVarGlobal") \
253 F(make_reference_error, "MakeReferenceError") \
254 F(make_syntax_error, "MakeSyntaxError") \
255 F(make_type_error, "MakeTypeError") \
256 F(module, "module") \
257 F(native, "native") \
258 F(next, "next") \
259 F(proto, "__proto__") \
260 F(prototype, "prototype") \
261 F(this, "this") \
262 F(use_strict, "use strict") \
263 F(value, "value")
264
265 class AstValueFactory {
266 public:
267 explicit AstValueFactory(Zone* zone)
268 : string_table_(AstString::Compare),
269 zone_(zone),
270 isolate_(NULL) {
271 #define F(name, str) { \
272 const char* data = str; \
273 name##_string_ = GetOneByteString( \
274 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \
275 static_cast<int>(strlen(data)))); \
276 }
277 STRING_CONSTANTS(F)
278 #undef F
279 }
280
281 const AstString* GetOneByteString(const Vector<const uint8_t>& literal);
rossberg 2014/06/23 09:44:27 Hm, isn't the idea of the Vector abstraction that
marja 2014/06/23 11:46:45 It's not a huge difference; this will copy the len
rossberg 2014/06/23 13:30:52 But you pay with an extra indirection for all uses
marja 2014/06/23 14:56:42 As you wish. (Changed back to Vector.)
282 const AstString* GetTwoByteString(const Vector<const uint16_t>& literal);
283 const AstString* GetString(Handle<String> literal);
284 const AstString* NewConsString(const AstString* left, const AstString* right);
285
286 void Internalize(Isolate* isolate);
287 bool IsInternalized() {
288 return isolate_ != NULL;
289 }
290
291 #define F(name, str) \
292 const AstString* name##_string() const { return name##_string_; }
293 STRING_CONSTANTS(F)
294 #undef F
295
296 const AstValue* NewString(const AstString* string);
297 // A JavaScript symbol (ECMA-262 edition 6).
298 const AstValue* NewSymbol(const char* name);
299 const AstValue* NewNumber(double number);
300 const AstValue* NewSmi(int number);
301 const AstValue* NewBoolean(bool b);
302 const AstValue* NewStringList(ZoneList<const AstString*>* strings);
303 const AstValue* NewNull();
304 const AstValue* NewUndefined();
305 const AstValue* NewTheHole();
306
307 private:
308 const AstString* GetString(int hash, bool is_one_byte,
309 const Vector<const byte>& literal_bytes);
310
311 // All strings are copied here, one after another (no NULLs inbetween).
312 HashMap string_table_;
313 // For keeping track of all AstValues and AstStrings we've created (so that
314 // they can be internalized later).
315 List<AstValue*> values_;
316 List<AstString*> strings_;
317 Zone* zone_;
318 Isolate* isolate_;
319
320 #define F(name, str) \
321 const AstString* name##_string_;
322 STRING_CONSTANTS(F)
323 #undef F
324 };
325
326 } } // namespace v8::internal
327
328 #undef STRING_CONSTANTS
329
330 #endif // V8_AST_VALUE_FACTORY_H_
OLDNEW
« no previous file with comments | « src/ast.cc ('k') | src/ast-value-factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698