ast-value-factory.h 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
#ifndef V8_AST_AST_VALUE_FACTORY_H_
#define V8_AST_AST_VALUE_FACTORY_H_
30

31 32
#include <forward_list>

lpy's avatar
lpy committed
33
#include "src/base/hashmap.h"
marja's avatar
marja committed
34
#include "src/conversions.h"
35
#include "src/globals.h"
36
#include "src/heap/factory.h"
37
#include "src/isolate.h"
38

39
// Ast(Raw|Cons)String and AstValueFactory are for storing strings and
40 41 42 43
// values independent of the V8 heap and internalizing them later. During
// parsing, they are created and stored outside the heap, in AstValueFactory.
// After parsing, the strings and values are internalized (moved into the V8
// heap).
44 45 46
namespace v8 {
namespace internal {

47
class AstRawString final : public ZoneObject {
48
 public:
49
  bool IsEmpty() const { return literal_bytes_.length() == 0; }
50
  int length() const {
51 52
    return is_one_byte() ? literal_bytes_.length()
                         : literal_bytes_.length() / 2;
53
  }
54
  bool AsArrayIndex(uint32_t* index) const;
55
  V8_EXPORT_PRIVATE bool IsOneByteEqualTo(const char* data) const;
56
  uint16_t FirstCharacter() const;
57

58
  void Internalize(Isolate* isolate);
59

60 61 62
  // Access the physical representation:
  bool is_one_byte() const { return is_one_byte_; }
  int byte_length() const { return literal_bytes_.length(); }
63 64 65
  const unsigned char* raw_data() const {
    return literal_bytes_.start();
  }
66

67
  // For storing AstRawStrings in a hash map.
68 69
  uint32_t hash_field() const { return hash_field_; }
  uint32_t Hash() const { return hash_field_ >> Name::kHashShift; }
70

71 72 73 74 75 76 77
  // This function can be called after internalizing.
  V8_INLINE Handle<String> string() const {
    DCHECK_NOT_NULL(string_);
    DCHECK(has_string_);
    return Handle<String>(string_);
  }

78
 private:
79
  friend class AstRawStringInternalizationKey;
80 81
  friend class AstStringConstants;
  friend class AstValueFactory;
82

83 84
  // Members accessed only by the AstValueFactory & related classes:
  static bool Compare(void* a, void* b);
85
  AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes,
86
               uint32_t hash_field)
87 88
      : next_(nullptr),
        literal_bytes_(literal_bytes),
89
        hash_field_(hash_field),
90 91 92 93 94 95 96 97
        is_one_byte_(is_one_byte) {}
  AstRawString* next() {
    DCHECK(!has_string_);
    return next_;
  }
  AstRawString** next_location() {
    DCHECK(!has_string_);
    return &next_;
98
  }
99

100 101 102 103 104 105 106
  void set_string(Handle<String> string) {
    DCHECK(!string.is_null());
    DCHECK(!has_string_);
    string_ = string.location();
#ifdef DEBUG
    has_string_ = true;
#endif
107
  }
108

109
  // {string_} is stored as Address* instead of a Handle<String> so it can be
110 111 112
  // stored in a union with {next_}.
  union {
    AstRawString* next_;
113
    Address* string_;
114
  };
115

116
  Vector<const byte> literal_bytes_;  // Memory owned by Zone.
117
  uint32_t hash_field_;
118 119 120 121 122 123 124
  bool is_one_byte_;
#ifdef DEBUG
  // (Debug-only:) Verify the object life-cylce: Some functions may only be
  // called after internalization (that is, after a v8::internal::String has
  // been set); some only before.
  bool has_string_ = false;
#endif
125 126
};

127
class AstConsString final : public ZoneObject {
128
 public:
129 130 131 132 133 134 135 136 137 138
  AstConsString* AddString(Zone* zone, const AstRawString* s) {
    if (s->IsEmpty()) return this;
    if (!IsEmpty()) {
      // We're putting the new string to the head of the list, meaning
      // the string segments will be in reverse order.
      Segment* tmp = new (zone->New(sizeof(Segment))) Segment;
      *tmp = segment_;
      segment_.next = tmp;
    }
    segment_.string = s;
139 140
    return this;
  }
141

142 143 144 145 146
  bool IsEmpty() const {
    DCHECK_IMPLIES(segment_.string == nullptr, segment_.next == nullptr);
    DCHECK_IMPLIES(segment_.string != nullptr, !segment_.string->IsEmpty());
    return segment_.string == nullptr;
  }
147

148
  void Internalize(Isolate* isolate);
149

150 151 152 153 154
  V8_INLINE Handle<String> string() const {
    DCHECK_NOT_NULL(string_);
    return Handle<String>(string_);
  }

155 156
  std::forward_list<const AstRawString*> ToRawStrings() const;

157
 private:
158 159
  friend class AstValueFactory;

160
  AstConsString() : next_(nullptr), segment_({nullptr, nullptr}) {}
161 162 163 164

  AstConsString* next() const { return next_; }
  AstConsString** next_location() { return &next_; }

165
  // {string_} is stored as Address* instead of a Handle<String> so it can be
166 167 168 169
  // stored in a union with {next_}.
  void set_string(Handle<String> string) { string_ = string.location(); }
  union {
    AstConsString* next_;
170
    Address* string_;
171 172
  };

173 174 175 176 177
  struct Segment {
    const AstRawString* string;
    AstConsString::Segment* next;
  };
  Segment segment_;
178 179
};

180
enum class AstSymbol : uint8_t { kHomeObjectSymbol };
181

182
class AstBigInt {
183
 public:
184 185 186 187
  // |bigint| must be a NUL-terminated string of ASCII characters
  // representing a BigInt (suitable for passing to BigIntLiteral()
  // from conversions.h).
  explicit AstBigInt(const char* bigint) : bigint_(bigint) {}
188

189
  const char* c_str() const { return bigint_; }
190 191

 private:
192
  const char* bigint_;
193 194
};

195
// For generating constants.
196
#define AST_STRING_CONSTANTS(F)                 \
197
  F(anonymous, "anonymous")                     \
198 199
  F(anonymous_function, "(anonymous function)") \
  F(arguments, "arguments")                     \
200
  F(as, "as")                                   \
201 202 203 204
  F(async, "async")                             \
  F(await, "await")                             \
  F(bigint, "bigint")                           \
  F(boolean, "boolean")                         \
205
  F(computed, "<computed>")                     \
206 207 208 209
  F(constructor, "constructor")                 \
  F(default, "default")                         \
  F(done, "done")                               \
  F(dot, ".")                                   \
210
  F(dot_default, ".default")                    \
211 212 213 214 215 216 217 218 219
  F(dot_for, ".for")                            \
  F(dot_generator_object, ".generator_object")  \
  F(dot_iterator, ".iterator")                  \
  F(dot_promise, ".promise")                    \
  F(dot_result, ".result")                      \
  F(dot_switch_tag, ".switch_tag")              \
  F(dot_catch, ".catch")                        \
  F(empty, "")                                  \
  F(eval, "eval")                               \
220
  F(from, "from")                               \
221
  F(function, "function")                       \
222
  F(get, "get")                                 \
223 224 225
  F(get_space, "get ")                          \
  F(length, "length")                           \
  F(let, "let")                                 \
226
  F(meta, "meta")                               \
227 228 229 230 231 232
  F(name, "name")                               \
  F(native, "native")                           \
  F(new_target, ".new.target")                  \
  F(next, "next")                               \
  F(number, "number")                           \
  F(object, "object")                           \
233
  F(of, "of")                                   \
234
  F(private_constructor, "#constructor")        \
235 236 237
  F(proto, "__proto__")                         \
  F(prototype, "prototype")                     \
  F(return, "return")                           \
238
  F(set, "set")                                 \
239 240 241
  F(set_space, "set ")                          \
  F(string, "string")                           \
  F(symbol, "symbol")                           \
242
  F(target, "target")                           \
243 244 245 246
  F(this, "this")                               \
  F(this_function, ".this_function")            \
  F(throw, "throw")                             \
  F(undefined, "undefined")                     \
247
  F(value, "value")
248

249 250
class AstStringConstants final {
 public:
Yang Guo's avatar
Yang Guo committed
251
  AstStringConstants(Isolate* isolate, uint64_t hash_seed);
252 253

#define F(name, str) \
254
  const AstRawString* name##_string() const { return name##_string_; }
255
  AST_STRING_CONSTANTS(F)
256 257
#undef F

Yang Guo's avatar
Yang Guo committed
258
  uint64_t hash_seed() const { return hash_seed_; }
259 260 261
  const base::CustomMatcherHashMap* string_table() const {
    return &string_table_;
  }
262 263 264

 private:
  Zone zone_;
265
  base::CustomMatcherHashMap string_table_;
Yang Guo's avatar
Yang Guo committed
266
  uint64_t hash_seed_;
267 268

#define F(name, str) AstRawString* name##_string_;
269
  AST_STRING_CONSTANTS(F)
270 271 272 273 274
#undef F

  DISALLOW_COPY_AND_ASSIGN(AstStringConstants);
};

275 276
class AstValueFactory {
 public:
277
  AstValueFactory(Zone* zone, const AstStringConstants* string_constants,
Yang Guo's avatar
Yang Guo committed
278
                  uint64_t hash_seed)
279
      : string_table_(string_constants->string_table()),
280 281
        strings_(nullptr),
        strings_end_(&strings_),
282 283
        cons_strings_(nullptr),
        cons_strings_end_(&cons_strings_),
284
        string_constants_(string_constants),
285
        empty_cons_string_(nullptr),
286 287
        zone_(zone),
        hash_seed_(hash_seed) {
288
    DCHECK_EQ(hash_seed, string_constants->hash_seed());
289 290 291
    std::fill(one_character_strings_,
              one_character_strings_ + arraysize(one_character_strings_),
              nullptr);
292
    empty_cons_string_ = NewConsString();
293 294
  }

295 296
  Zone* zone() const { return zone_; }

297 298
  const AstRawString* GetOneByteString(Vector<const uint8_t> literal) {
    return GetOneByteStringInternal(literal);
299
  }
300 301 302
  const AstRawString* GetOneByteString(const char* string) {
    return GetOneByteString(Vector<const uint8_t>(
        reinterpret_cast<const uint8_t*>(string), StrLength(string)));
303
  }
304 305
  const AstRawString* GetTwoByteString(Vector<const uint16_t> literal) {
    return GetTwoByteStringInternal(literal);
306
  }
307
  const AstRawString* GetString(Handle<String> literal);
308 309 310 311 312

  // Clones an AstRawString from another ast value factory, adding it to this
  // factory and returning the clone.
  const AstRawString* CloneFromOtherFactory(const AstRawString* raw_string);

313
  V8_EXPORT_PRIVATE AstConsString* NewConsString();
314 315 316
  V8_EXPORT_PRIVATE AstConsString* NewConsString(const AstRawString* str);
  V8_EXPORT_PRIVATE AstConsString* NewConsString(const AstRawString* str1,
                                                 const AstRawString* str2);
317

318
  V8_EXPORT_PRIVATE void Internalize(Isolate* isolate);
319

320
#define F(name, str)                           \
321
  const AstRawString* name##_string() const {  \
322
    return string_constants_->name##_string(); \
323
  }
324
  AST_STRING_CONSTANTS(F)
325
#undef F
326
  const AstConsString* empty_cons_string() const { return empty_cons_string_; }
327 328

 private:
329
  AstRawString* AddString(AstRawString* string) {
330 331
    *strings_end_ = string;
    strings_end_ = string->next_location();
332 333
    return string;
  }
334 335 336 337 338
  AstConsString* AddConsString(AstConsString* string) {
    *cons_strings_end_ = string;
    cons_strings_end_ = string->next_location();
    return string;
  }
339 340 341
  void ResetStrings() {
    strings_ = nullptr;
    strings_end_ = &strings_;
342 343
    cons_strings_ = nullptr;
    cons_strings_end_ = &cons_strings_;
344
  }
345
  V8_EXPORT_PRIVATE AstRawString* GetOneByteStringInternal(
346 347
      Vector<const uint8_t> literal);
  AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal);
348
  AstRawString* GetString(uint32_t hash, bool is_one_byte,
349
                          Vector<const byte> literal_bytes);
350

351
  // All strings are copied here, one after another (no zeroes inbetween).
352
  base::CustomMatcherHashMap string_table_;
heimbuef's avatar
heimbuef committed
353

354
  // We need to keep track of strings_ in order since cons strings require their
355
  // members to be internalized first.
356 357 358 359
  AstRawString* strings_;
  AstRawString** strings_end_;
  AstConsString* cons_strings_;
  AstConsString** cons_strings_end_;
360

361
  // Holds constant string values which are shared across the isolate.
362
  const AstStringConstants* string_constants_;
363
  const AstConsString* empty_cons_string_;
364

365
  // Caches one character lowercase strings (for minified code).
366 367
  static const int kMaxOneCharStringValue = 128;
  AstRawString* one_character_strings_[kMaxOneCharStringValue];
368

369 370
  Zone* zone_;

Yang Guo's avatar
Yang Guo committed
371
  uint64_t hash_seed_;
372
};
373 374
}  // namespace internal
}  // namespace v8
375

376
#endif  // V8_AST_AST_VALUE_FACTORY_H_