blob: c391b9c4327f4bca495e723511289e7abfd2c887 [file] [log] [blame]
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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_UTILS_H_
29#define V8_UTILS_H_
30
ager@chromium.orga74f0da2008-12-03 16:05:52 +000031#include <stdlib.h>
vegorov@chromium.orgf8372902010-03-15 10:26:20 +000032#include <string.h>
ricow@chromium.org9fa09672011-07-25 11:05:35 +000033#include <climits>
ager@chromium.orga74f0da2008-12-03 16:05:52 +000034
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000035#include "globals.h"
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000036#include "checks.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000037#include "allocation.h"
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42// ----------------------------------------------------------------------------
43// General helper functions
44
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000045#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
46
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000047// Returns true iff x is a power of 2 (or zero). Cannot be used with the
48// maximally negative value of the type T (the -1 overflows).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000050inline bool IsPowerOf2(T x) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000051 return IS_POWER_OF_TWO(x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052}
53
54
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000055// X must be a power of 2. Returns the number of trailing zeros.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000056inline int WhichPowerOf2(uint32_t x) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000057 ASSERT(IsPowerOf2(x));
58 ASSERT(x != 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000059 int bits = 0;
60#ifdef DEBUG
61 int original_x = x;
62#endif
63 if (x >= 0x10000) {
64 bits += 16;
65 x >>= 16;
66 }
67 if (x >= 0x100) {
68 bits += 8;
69 x >>= 8;
70 }
71 if (x >= 0x10) {
72 bits += 4;
73 x >>= 4;
74 }
75 switch (x) {
76 default: UNREACHABLE();
77 case 8: bits++; // Fall through.
78 case 4: bits++; // Fall through.
79 case 2: bits++; // Fall through.
80 case 1: break;
81 }
82 ASSERT_EQ(1 << bits, original_x);
83 return bits;
84 return 0;
85}
86
87
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +000088// Magic numbers for integer division.
89// These are kind of 2's complement reciprocal of the divisors.
90// Details and proofs can be found in:
91// - Hacker's Delight, Henry S. Warren, Jr.
92// - The PowerPC Compiler Writer’s Guide
93// and probably many others.
94// See details in the implementation of the algorithm in
95// lithium-codegen-arm.cc : LCodeGen::TryEmitSignedIntegerDivisionByConstant().
96struct DivMagicNumbers {
97 unsigned M;
98 unsigned s;
99};
100
101const DivMagicNumbers InvalidDivMagicNumber= {0, 0};
102const DivMagicNumbers DivMagicNumberFor3 = {0x55555556, 0};
103const DivMagicNumbers DivMagicNumberFor5 = {0x66666667, 1};
104const DivMagicNumbers DivMagicNumberFor7 = {0x92492493, 2};
105const DivMagicNumbers DivMagicNumberFor9 = {0x38e38e39, 1};
106const DivMagicNumbers DivMagicNumberFor11 = {0x2e8ba2e9, 1};
107const DivMagicNumbers DivMagicNumberFor25 = {0x51eb851f, 3};
108const DivMagicNumbers DivMagicNumberFor125 = {0x10624dd3, 3};
109const DivMagicNumbers DivMagicNumberFor625 = {0x68db8bad, 8};
110
111const DivMagicNumbers DivMagicNumberFor(int32_t divisor);
112
113
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000114// The C++ standard leaves the semantics of '>>' undefined for
115// negative signed operands. Most implementations do the right thing,
116// though.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000117inline int ArithmeticShiftRight(int x, int s) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 return x >> s;
119}
120
121
122// Compute the 0-relative offset of some absolute value x of type T.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000123// This allows conversion of Addresses and integral types into
124// 0-relative int offsets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000126inline intptr_t OffsetFrom(T x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127 return x - static_cast<T>(0);
128}
129
130
131// Compute the absolute value of type T for some 0-relative offset x.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000132// This allows conversion of 0-relative int offsets into Addresses and
133// integral types.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000135inline T AddressFrom(intptr_t x) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000136 return static_cast<T>(static_cast<T>(0) + x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140// Return the largest multiple of m which is <= x.
141template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000142inline T RoundDown(T x, intptr_t m) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 ASSERT(IsPowerOf2(m));
144 return AddressFrom<T>(OffsetFrom(x) & -m);
145}
146
147
148// Return the smallest multiple of m which is >= x.
149template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000150inline T RoundUp(T x, intptr_t m) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000151 return RoundDown<T>(static_cast<T>(x + m - 1), m);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152}
153
154
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000155template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000156int Compare(const T& a, const T& b) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000157 if (a == b)
158 return 0;
159 else if (a < b)
160 return -1;
161 else
162 return 1;
163}
164
165
166template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000167int PointerValueCompare(const T* a, const T* b) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000168 return Compare<T>(*a, *b);
169}
170
171
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000172// Compare function to compare the object pointer value of two
173// handlified objects. The handles are passed as pointers to the
174// handles.
175template<typename T> class Handle; // Forward declaration.
176template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000177int HandleObjectPointerCompare(const Handle<T>* a, const Handle<T>* b) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000178 return Compare<T*>(*(*a), *(*b));
179}
180
181
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000182// Returns the smallest power of two which is >= x. If you pass in a
183// number that is already a power of two, it is returned as is.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000184// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
185// figure 3-3, page 48, where the function is called clp2.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000186inline uint32_t RoundUpToPowerOf2(uint32_t x) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000187 ASSERT(x <= 0x80000000u);
188 x = x - 1;
189 x = x | (x >> 1);
190 x = x | (x >> 2);
191 x = x | (x >> 4);
192 x = x | (x >> 8);
193 x = x | (x >> 16);
194 return x + 1;
195}
196
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000197
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000198inline uint32_t RoundDownToPowerOf2(uint32_t x) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000199 uint32_t rounded_up = RoundUpToPowerOf2(x);
200 if (rounded_up > x) return rounded_up >> 1;
201 return rounded_up;
202}
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000203
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000204
205template <typename T, typename U>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000206inline bool IsAligned(T value, U alignment) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 return (value & (alignment - 1)) == 0;
208}
209
210
211// Returns true if (addr + offset) is aligned.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000212inline bool IsAddressAligned(Address addr,
213 intptr_t alignment,
214 int offset = 0) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000215 intptr_t offs = OffsetFrom(addr + offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 return IsAligned(offs, alignment);
217}
218
219
220// Returns the maximum of the two parameters.
221template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000222T Max(T a, T b) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 return a < b ? b : a;
224}
225
226
227// Returns the minimum of the two parameters.
228template <typename T>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000229T Min(T a, T b) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 return a < b ? a : b;
231}
232
233
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000234inline int StrLength(const char* string) {
235 size_t length = strlen(string);
236 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
237 return static_cast<int>(length);
238}
239
240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241// ----------------------------------------------------------------------------
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000242// BitField is a help template for encoding and decode bitfield with
243// unsigned content.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244template<class T, int shift, int size>
245class BitField {
246 public:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000247 // A uint32_t mask of bit field. To use all bits of a uint32 in a
248 // bitfield without compiler warnings we have to compute 2^32 without
249 // using a shift count of 32.
250 static const uint32_t kMask = ((1U << shift) << size) - (1U << shift);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000251 static const uint32_t kShift = shift;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000252 static const uint32_t kSize = size;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000253
254 // Value for the field with all bits set.
255 static const T kMax = static_cast<T>((1U << size) - 1);
256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 // Tells whether the provided value fits into the bit field.
258 static bool is_valid(T value) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000259 return (static_cast<uint32_t>(value) & ~static_cast<uint32_t>(kMax)) == 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 }
261
262 // Returns a uint32_t with the bit field value encoded.
263 static uint32_t encode(T value) {
264 ASSERT(is_valid(value));
265 return static_cast<uint32_t>(value) << shift;
266 }
267
danno@chromium.org40cb8782011-05-25 07:58:50 +0000268 // Returns a uint32_t with the bit field value updated.
269 static uint32_t update(uint32_t previous, T value) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000270 return (previous & ~kMask) | encode(value);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000271 }
272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 // Extracts the bit field from the value.
274 static T decode(uint32_t value) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000275 return static_cast<T>((value & kMask) >> shift);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000276 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277};
278
279
280// ----------------------------------------------------------------------------
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000281// Hash function.
282
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000283static const uint32_t kZeroHashSeed = 0;
284
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000285// Thomas Wang, Integer Hash Functions.
286// http://www.concentric.net/~Ttwang/tech/inthash.htm
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000287inline uint32_t ComputeIntegerHash(uint32_t key, uint32_t seed) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000288 uint32_t hash = key;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000289 hash = hash ^ seed;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000290 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1;
291 hash = hash ^ (hash >> 12);
292 hash = hash + (hash << 2);
293 hash = hash ^ (hash >> 4);
294 hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11);
295 hash = hash ^ (hash >> 16);
296 return hash;
297}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298
299
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000300inline uint32_t ComputeLongHash(uint64_t key) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000301 uint64_t hash = key;
302 hash = ~hash + (hash << 18); // hash = (hash << 18) - hash - 1;
303 hash = hash ^ (hash >> 31);
304 hash = hash * 21; // hash = (hash + (hash << 2)) + (hash << 4);
305 hash = hash ^ (hash >> 11);
306 hash = hash + (hash << 6);
307 hash = hash ^ (hash >> 22);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000308 return static_cast<uint32_t>(hash);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000309}
310
311
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000312inline uint32_t ComputePointerHash(void* ptr) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000313 return ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000314 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr)),
315 v8::internal::kZeroHashSeed);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000316}
317
318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319// ----------------------------------------------------------------------------
320// Miscellaneous
321
322// A static resource holds a static instance that can be reserved in
323// a local scope using an instance of Access. Attempts to re-reserve
324// the instance will cause an error.
325template <typename T>
326class StaticResource {
327 public:
328 StaticResource() : is_reserved_(false) {}
329
330 private:
331 template <typename S> friend class Access;
332 T instance_;
333 bool is_reserved_;
334};
335
336
337// Locally scoped access to a static resource.
338template <typename T>
339class Access {
340 public:
341 explicit Access(StaticResource<T>* resource)
342 : resource_(resource)
343 , instance_(&resource->instance_) {
344 ASSERT(!resource->is_reserved_);
345 resource->is_reserved_ = true;
346 }
347
348 ~Access() {
349 resource_->is_reserved_ = false;
350 resource_ = NULL;
351 instance_ = NULL;
352 }
353
354 T* value() { return instance_; }
355 T* operator -> () { return instance_; }
356
357 private:
358 StaticResource<T>* resource_;
359 T* instance_;
360};
361
362
363template <typename T>
364class Vector {
365 public:
kasper.lund7276f142008-07-30 08:49:36 +0000366 Vector() : start_(NULL), length_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 Vector(T* data, int length) : start_(data), length_(length) {
368 ASSERT(length == 0 || (length > 0 && data != NULL));
369 }
370
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000371 static Vector<T> New(int length) {
372 return Vector<T>(NewArray<T>(length), length);
373 }
374
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000375 // Returns a vector using the same backing storage as this one,
376 // spanning from and including 'from', to but not including 'to'.
377 Vector<T> SubVector(int from, int to) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000378 ASSERT(to <= length_);
379 ASSERT(from < to);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000380 ASSERT(0 <= from);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000381 return Vector<T>(start() + from, to - from);
382 }
383
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 // Returns the length of the vector.
385 int length() const { return length_; }
386
387 // Returns whether or not the vector is empty.
388 bool is_empty() const { return length_ == 0; }
389
390 // Returns the pointer to the start of the data in the vector.
391 T* start() const { return start_; }
392
393 // Access individual vector elements - checks bounds in debug mode.
394 T& operator[](int index) const {
395 ASSERT(0 <= index && index < length_);
396 return start_[index];
397 }
398
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000399 const T& at(int index) const { return operator[](index); }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000400
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000401 T& first() { return start_[0]; }
402
403 T& last() { return start_[length_ - 1]; }
404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405 // Returns a clone of this vector with a new backing store.
406 Vector<T> Clone() const {
407 T* result = NewArray<T>(length_);
408 for (int i = 0; i < length_; i++) result[i] = start_[i];
409 return Vector<T>(result, length_);
410 }
411
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000412 void Sort(int (*cmp)(const T*, const T*)) {
413 typedef int (*RawComparer)(const void*, const void*);
414 qsort(start(),
415 length(),
416 sizeof(T),
417 reinterpret_cast<RawComparer>(cmp));
418 }
419
420 void Sort() {
421 Sort(PointerValueCompare<T>);
422 }
423
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000424 void Truncate(int length) {
425 ASSERT(length <= length_);
426 length_ = length;
427 }
428
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 // Releases the array underlying this vector. Once disposed the
430 // vector is empty.
431 void Dispose() {
432 DeleteArray(start_);
433 start_ = NULL;
434 length_ = 0;
435 }
436
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000437 inline Vector<T> operator+(int offset) {
438 ASSERT(offset < length_);
439 return Vector<T>(start_ + offset, length_ - offset);
440 }
441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 // Factory method for creating empty vectors.
443 static Vector<T> empty() { return Vector<T>(NULL, 0); }
444
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000445 template<typename S>
446 static Vector<T> cast(Vector<S> input) {
447 return Vector<T>(reinterpret_cast<T*>(input.start()),
448 input.length() * sizeof(S) / sizeof(T));
449 }
450
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000451 protected:
452 void set_start(T* start) { start_ = start; }
453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 private:
455 T* start_;
456 int length_;
457};
458
459
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000460// A pointer that can only be set once and doesn't allow NULL values.
461template<typename T>
462class SetOncePointer {
463 public:
464 SetOncePointer() : pointer_(NULL) { }
465
466 bool is_set() const { return pointer_ != NULL; }
467
468 T* get() const {
469 ASSERT(pointer_ != NULL);
470 return pointer_;
471 }
472
473 void set(T* value) {
474 ASSERT(pointer_ == NULL && value != NULL);
475 pointer_ = value;
476 }
477
478 private:
479 T* pointer_;
480};
481
482
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000483template <typename T, int kSize>
484class EmbeddedVector : public Vector<T> {
485 public:
486 EmbeddedVector() : Vector<T>(buffer_, kSize) { }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000487
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000488 explicit EmbeddedVector(T initial_value) : Vector<T>(buffer_, kSize) {
489 for (int i = 0; i < kSize; ++i) {
490 buffer_[i] = initial_value;
491 }
492 }
493
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000494 // When copying, make underlying Vector to reference our buffer.
495 EmbeddedVector(const EmbeddedVector& rhs)
496 : Vector<T>(rhs) {
497 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
498 set_start(buffer_);
499 }
500
501 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
502 if (this == &rhs) return *this;
503 Vector<T>::operator=(rhs);
504 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000505 this->set_start(buffer_);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000506 return *this;
507 }
508
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000509 private:
510 T buffer_[kSize];
511};
512
513
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000514template <typename T>
515class ScopedVector : public Vector<T> {
516 public:
517 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
518 ~ScopedVector() {
519 DeleteArray(this->start());
520 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000521
522 private:
523 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000524};
525
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000526#define STATIC_ASCII_VECTOR(x) \
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000527 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
528 ARRAY_SIZE(x)-1)
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000529
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530inline Vector<const char> CStrVector(const char* data) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000531 return Vector<const char>(data, StrLength(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532}
533
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000534inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
535 return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
536}
537
538inline Vector<const uint8_t> OneByteVector(const char* data) {
539 return OneByteVector(data, StrLength(data));
540}
541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542inline Vector<char> MutableCStrVector(char* data) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000543 return Vector<char>(data, StrLength(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544}
545
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000546inline Vector<char> MutableCStrVector(char* data, int max) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000547 int length = StrLength(data);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000548 return Vector<char>(data, (length < max) ? length : max);
549}
550
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551
ricow@chromium.org65fae842010-08-25 15:26:24 +0000552/*
553 * A class that collects values into a backing store.
554 * Specialized versions of the class can allow access to the backing store
555 * in different ways.
556 * There is no guarantee that the backing store is contiguous (and, as a
557 * consequence, no guarantees that consecutively added elements are adjacent
558 * in memory). The collector may move elements unless it has guaranteed not
559 * to.
560 */
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000561template <typename T, int growth_factor = 2, int max_growth = 1 * MB>
ricow@chromium.org65fae842010-08-25 15:26:24 +0000562class Collector {
563 public:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000564 explicit Collector(int initial_capacity = kMinCapacity)
565 : index_(0), size_(0) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000566 current_chunk_ = Vector<T>::New(initial_capacity);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000567 }
568
569 virtual ~Collector() {
570 // Free backing store (in reverse allocation order).
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000571 current_chunk_.Dispose();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000572 for (int i = chunks_.length() - 1; i >= 0; i--) {
573 chunks_.at(i).Dispose();
574 }
575 }
576
577 // Add a single element.
578 inline void Add(T value) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000579 if (index_ >= current_chunk_.length()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000580 Grow(1);
581 }
582 current_chunk_[index_] = value;
583 index_++;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000584 size_++;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000585 }
586
587 // Add a block of contiguous elements and return a Vector backed by the
588 // memory area.
589 // A basic Collector will keep this vector valid as long as the Collector
590 // is alive.
591 inline Vector<T> AddBlock(int size, T initial_value) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000592 ASSERT(size > 0);
593 if (size > current_chunk_.length() - index_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000594 Grow(size);
595 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000596 T* position = current_chunk_.start() + index_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000597 index_ += size;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000598 size_ += size;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000599 for (int i = 0; i < size; i++) {
600 position[i] = initial_value;
601 }
602 return Vector<T>(position, size);
603 }
604
605
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000606 // Add a contiguous block of elements and return a vector backed
607 // by the added block.
608 // A basic Collector will keep this vector valid as long as the Collector
609 // is alive.
610 inline Vector<T> AddBlock(Vector<const T> source) {
611 if (source.length() > current_chunk_.length() - index_) {
612 Grow(source.length());
613 }
614 T* position = current_chunk_.start() + index_;
615 index_ += source.length();
616 size_ += source.length();
617 for (int i = 0; i < source.length(); i++) {
618 position[i] = source[i];
619 }
620 return Vector<T>(position, source.length());
621 }
622
623
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000624 // Write the contents of the collector into the provided vector.
625 void WriteTo(Vector<T> destination) {
626 ASSERT(size_ <= destination.length());
627 int position = 0;
628 for (int i = 0; i < chunks_.length(); i++) {
629 Vector<T> chunk = chunks_.at(i);
630 for (int j = 0; j < chunk.length(); j++) {
631 destination[position] = chunk[j];
632 position++;
633 }
634 }
635 for (int i = 0; i < index_; i++) {
636 destination[position] = current_chunk_[i];
637 position++;
638 }
639 }
640
ricow@chromium.org65fae842010-08-25 15:26:24 +0000641 // Allocate a single contiguous vector, copy all the collected
642 // elements to the vector, and return it.
643 // The caller is responsible for freeing the memory of the returned
644 // vector (e.g., using Vector::Dispose).
645 Vector<T> ToVector() {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000646 Vector<T> new_store = Vector<T>::New(size_);
647 WriteTo(new_store);
648 return new_store;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000649 }
650
651 // Resets the collector to be empty.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000652 virtual void Reset();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000653
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000654 // Total number of elements added to collector so far.
655 inline int size() { return size_; }
656
ricow@chromium.org65fae842010-08-25 15:26:24 +0000657 protected:
658 static const int kMinCapacity = 16;
659 List<Vector<T> > chunks_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000660 Vector<T> current_chunk_; // Block of memory currently being written into.
661 int index_; // Current index in current chunk.
662 int size_; // Total number of elements in collector.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000663
664 // Creates a new current chunk, and stores the old chunk in the chunks_ list.
665 void Grow(int min_capacity) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000666 ASSERT(growth_factor > 1);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000667 int new_capacity;
668 int current_length = current_chunk_.length();
669 if (current_length < kMinCapacity) {
670 // The collector started out as empty.
671 new_capacity = min_capacity * growth_factor;
672 if (new_capacity < kMinCapacity) new_capacity = kMinCapacity;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000673 } else {
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000674 int growth = current_length * (growth_factor - 1);
675 if (growth > max_growth) {
676 growth = max_growth;
677 }
678 new_capacity = current_length + growth;
679 if (new_capacity < min_capacity) {
680 new_capacity = min_capacity + growth;
681 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000682 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000683 NewChunk(new_capacity);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000684 ASSERT(index_ + min_capacity <= current_chunk_.length());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000685 }
686
687 // Before replacing the current chunk, give a subclass the option to move
688 // some of the current data into the new chunk. The function may update
689 // the current index_ value to represent data no longer in the current chunk.
690 // Returns the initial index of the new chunk (after copied data).
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000691 virtual void NewChunk(int new_capacity) {
692 Vector<T> new_chunk = Vector<T>::New(new_capacity);
693 if (index_ > 0) {
694 chunks_.Add(current_chunk_.SubVector(0, index_));
695 } else {
696 current_chunk_.Dispose();
697 }
698 current_chunk_ = new_chunk;
699 index_ = 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000700 }
701};
702
703
704/*
705 * A collector that allows sequences of values to be guaranteed to
706 * stay consecutive.
707 * If the backing store grows while a sequence is active, the current
708 * sequence might be moved, but after the sequence is ended, it will
709 * not move again.
710 * NOTICE: Blocks allocated using Collector::AddBlock(int) can move
711 * as well, if inside an active sequence where another element is added.
712 */
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000713template <typename T, int growth_factor = 2, int max_growth = 1 * MB>
714class SequenceCollector : public Collector<T, growth_factor, max_growth> {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000715 public:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000716 explicit SequenceCollector(int initial_capacity)
717 : Collector<T, growth_factor, max_growth>(initial_capacity),
ricow@chromium.org65fae842010-08-25 15:26:24 +0000718 sequence_start_(kNoSequence) { }
719
720 virtual ~SequenceCollector() {}
721
722 void StartSequence() {
723 ASSERT(sequence_start_ == kNoSequence);
724 sequence_start_ = this->index_;
725 }
726
727 Vector<T> EndSequence() {
728 ASSERT(sequence_start_ != kNoSequence);
729 int sequence_start = sequence_start_;
730 sequence_start_ = kNoSequence;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000731 if (sequence_start == this->index_) return Vector<T>();
732 return this->current_chunk_.SubVector(sequence_start, this->index_);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000733 }
734
735 // Drops the currently added sequence, and all collected elements in it.
736 void DropSequence() {
737 ASSERT(sequence_start_ != kNoSequence);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000738 int sequence_length = this->index_ - sequence_start_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000739 this->index_ = sequence_start_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000740 this->size_ -= sequence_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000741 sequence_start_ = kNoSequence;
742 }
743
744 virtual void Reset() {
745 sequence_start_ = kNoSequence;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000746 this->Collector<T, growth_factor, max_growth>::Reset();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000747 }
748
749 private:
750 static const int kNoSequence = -1;
751 int sequence_start_;
752
753 // Move the currently active sequence to the new chunk.
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000754 virtual void NewChunk(int new_capacity) {
755 if (sequence_start_ == kNoSequence) {
756 // Fall back on default behavior if no sequence has been started.
757 this->Collector<T, growth_factor, max_growth>::NewChunk(new_capacity);
758 return;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000759 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000760 int sequence_length = this->index_ - sequence_start_;
761 Vector<T> new_chunk = Vector<T>::New(sequence_length + new_capacity);
762 ASSERT(sequence_length < new_chunk.length());
763 for (int i = 0; i < sequence_length; i++) {
764 new_chunk[i] = this->current_chunk_[sequence_start_ + i];
765 }
766 if (sequence_start_ > 0) {
767 this->chunks_.Add(this->current_chunk_.SubVector(0, sequence_start_));
768 } else {
769 this->current_chunk_.Dispose();
770 }
771 this->current_chunk_ = new_chunk;
772 this->index_ = sequence_length;
773 sequence_start_ = 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000774 }
775};
776
777
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000778// Compare ASCII/16bit chars to ASCII/16bit chars.
779template <typename lchar, typename rchar>
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000780inline int CompareCharsUnsigned(const lchar* lhs,
781 const rchar* rhs,
782 int chars) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000783 const lchar* limit = lhs + chars;
784#ifdef V8_HOST_CAN_READ_UNALIGNED
785 if (sizeof(*lhs) == sizeof(*rhs)) {
786 // Number of characters in a uintptr_t.
787 static const int kStepSize = sizeof(uintptr_t) / sizeof(*lhs); // NOLINT
788 while (lhs <= limit - kStepSize) {
789 if (*reinterpret_cast<const uintptr_t*>(lhs) !=
790 *reinterpret_cast<const uintptr_t*>(rhs)) {
791 break;
792 }
793 lhs += kStepSize;
794 rhs += kStepSize;
795 }
796 }
797#endif
798 while (lhs < limit) {
799 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs);
800 if (r != 0) return r;
801 ++lhs;
802 ++rhs;
803 }
804 return 0;
805}
806
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000807template<typename lchar, typename rchar>
808inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) {
809 ASSERT(sizeof(lchar) <= 2);
810 ASSERT(sizeof(rchar) <= 2);
811 if (sizeof(lchar) == 1) {
812 if (sizeof(rchar) == 1) {
813 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs),
814 reinterpret_cast<const uint8_t*>(rhs),
815 chars);
816 } else {
817 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs),
818 reinterpret_cast<const uint16_t*>(rhs),
819 chars);
820 }
821 } else {
822 if (sizeof(rchar) == 1) {
823 return CompareCharsUnsigned(reinterpret_cast<const uint16_t*>(lhs),
824 reinterpret_cast<const uint8_t*>(rhs),
825 chars);
826 } else {
827 return CompareCharsUnsigned(reinterpret_cast<const uint16_t*>(lhs),
828 reinterpret_cast<const uint16_t*>(rhs),
829 chars);
830 }
831 }
832}
833
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000834
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000835// Calculate 10^exponent.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000836inline int TenToThe(int exponent) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000837 ASSERT(exponent <= 9);
838 ASSERT(exponent >= 1);
839 int answer = 10;
840 for (int i = 1; i < exponent; i++) answer *= 10;
841 return answer;
842}
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000843
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000844
845// The type-based aliasing rule allows the compiler to assume that pointers of
846// different types (for some definition of different) never alias each other.
847// Thus the following code does not work:
848//
849// float f = foo();
850// int fbits = *(int*)(&f);
851//
852// The compiler 'knows' that the int pointer can't refer to f since the types
853// don't match, so the compiler may cache f in a register, leaving random data
854// in fbits. Using C++ style casts makes no difference, however a pointer to
855// char data is assumed to alias any other pointer. This is the 'memcpy
856// exception'.
857//
858// Bit_cast uses the memcpy exception to move the bits from a variable of one
859// type of a variable of another type. Of course the end result is likely to
860// be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
861// will completely optimize BitCast away.
862//
863// There is an additional use for BitCast.
864// Recent gccs will warn when they see casts that may result in breakage due to
865// the type-based aliasing rule. If you have checked that there is no breakage
866// you can use BitCast to cast one pointer type to another. This confuses gcc
867// enough that it can no longer see that you have cast one pointer type to
868// another thus avoiding the warning.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000869
870// We need different implementations of BitCast for pointer and non-pointer
871// values. We use partial specialization of auxiliary struct to work around
872// issues with template functions overloading.
873template <class Dest, class Source>
874struct BitCastHelper {
875 STATIC_ASSERT(sizeof(Dest) == sizeof(Source));
876
877 INLINE(static Dest cast(const Source& source)) {
878 Dest dest;
879 memcpy(&dest, &source, sizeof(dest));
880 return dest;
881 }
882};
883
884template <class Dest, class Source>
885struct BitCastHelper<Dest, Source*> {
886 INLINE(static Dest cast(Source* source)) {
887 return BitCastHelper<Dest, uintptr_t>::
888 cast(reinterpret_cast<uintptr_t>(source));
889 }
890};
891
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000892template <class Dest, class Source>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000893INLINE(Dest BitCast(const Source& source));
894
895template <class Dest, class Source>
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000896inline Dest BitCast(const Source& source) {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000897 return BitCastHelper<Dest, Source>::cast(source);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000898}
lrn@chromium.org25156de2010-04-06 13:10:27 +0000899
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000900
901template<typename ElementType, int NumElements>
902class EmbeddedContainer {
903 public:
904 EmbeddedContainer() : elems_() { }
905
jkummerow@chromium.orgc1956672012-10-11 15:57:38 +0000906 int length() const { return NumElements; }
907 const ElementType& operator[](int i) const {
908 ASSERT(i < length());
909 return elems_[i];
910 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000911 ElementType& operator[](int i) {
912 ASSERT(i < length());
913 return elems_[i];
914 }
915
916 private:
917 ElementType elems_[NumElements];
918};
919
920
921template<typename ElementType>
922class EmbeddedContainer<ElementType, 0> {
923 public:
jkummerow@chromium.orgc1956672012-10-11 15:57:38 +0000924 int length() const { return 0; }
925 const ElementType& operator[](int i) const {
926 UNREACHABLE();
927 static ElementType t = 0;
928 return t;
929 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000930 ElementType& operator[](int i) {
931 UNREACHABLE();
932 static ElementType t = 0;
933 return t;
934 }
935};
936
937
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000938// Helper class for building result strings in a character buffer. The
939// purpose of the class is to use safe operations that checks the
940// buffer bounds on all operations in debug mode.
941// This simple base class does not allow formatted output.
942class SimpleStringBuilder {
943 public:
944 // Create a string builder with a buffer of the given size. The
945 // buffer is allocated through NewArray<char> and must be
946 // deallocated by the caller of Finalize().
947 explicit SimpleStringBuilder(int size);
948
949 SimpleStringBuilder(char* buffer, int size)
950 : buffer_(buffer, size), position_(0) { }
951
952 ~SimpleStringBuilder() { if (!is_finalized()) Finalize(); }
953
954 int size() const { return buffer_.length(); }
955
956 // Get the current position in the builder.
957 int position() const {
958 ASSERT(!is_finalized());
959 return position_;
960 }
961
962 // Reset the position.
963 void Reset() { position_ = 0; }
964
965 // Add a single character to the builder. It is not allowed to add
966 // 0-characters; use the Finalize() method to terminate the string
967 // instead.
968 void AddCharacter(char c) {
969 ASSERT(c != '\0');
970 ASSERT(!is_finalized() && position_ < buffer_.length());
971 buffer_[position_++] = c;
972 }
973
974 // Add an entire string to the builder. Uses strlen() internally to
975 // compute the length of the input string.
976 void AddString(const char* s);
977
978 // Add the first 'n' characters of the given string 's' to the
979 // builder. The input string must have enough characters.
980 void AddSubstring(const char* s, int n);
981
982 // Add character padding to the builder. If count is non-positive,
983 // nothing is added to the builder.
984 void AddPadding(char c, int count);
985
986 // Add the decimal representation of the value.
987 void AddDecimalInteger(int value);
988
989 // Finalize the string by 0-terminating it and returning the buffer.
990 char* Finalize();
991
992 protected:
993 Vector<char> buffer_;
994 int position_;
995
996 bool is_finalized() const { return position_ < 0; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000997
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000998 private:
999 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder);
1000};
1001
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001002
1003// A poor man's version of STL's bitset: A bit set of enums E (without explicit
1004// values), fitting into an integral type T.
1005template <class E, class T = int>
1006class EnumSet {
1007 public:
1008 explicit EnumSet(T bits = 0) : bits_(bits) {}
1009 bool IsEmpty() const { return bits_ == 0; }
1010 bool Contains(E element) const { return (bits_ & Mask(element)) != 0; }
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001011 bool ContainsAnyOf(const EnumSet& set) const {
1012 return (bits_ & set.bits_) != 0;
1013 }
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001014 void Add(E element) { bits_ |= Mask(element); }
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001015 void Add(const EnumSet& set) { bits_ |= set.bits_; }
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001016 void Remove(E element) { bits_ &= ~Mask(element); }
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001017 void Remove(const EnumSet& set) { bits_ &= ~set.bits_; }
1018 void RemoveAll() { bits_ = 0; }
1019 void Intersect(const EnumSet& set) { bits_ &= set.bits_; }
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001020 T ToIntegral() const { return bits_; }
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001021 bool operator==(const EnumSet& set) { return bits_ == set.bits_; }
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001022
1023 private:
1024 T Mask(E element) const {
1025 // The strange typing in ASSERT is necessary to avoid stupid warnings, see:
1026 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43680
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001027 ASSERT(static_cast<int>(element) < static_cast<int>(sizeof(T) * CHAR_BIT));
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001028 return 1 << element;
1029 }
1030
1031 T bits_;
1032};
1033
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001034
1035class TypeFeedbackId {
1036 public:
1037 explicit TypeFeedbackId(int id) : id_(id) { }
1038 int ToInt() const { return id_; }
1039
1040 static TypeFeedbackId None() { return TypeFeedbackId(kNoneId); }
1041 bool IsNone() const { return id_ == kNoneId; }
1042
1043 private:
1044 static const int kNoneId = -1;
1045
1046 int id_;
1047};
1048
1049
1050class BailoutId {
1051 public:
1052 explicit BailoutId(int id) : id_(id) { }
1053 int ToInt() const { return id_; }
1054
1055 static BailoutId None() { return BailoutId(kNoneId); }
1056 static BailoutId FunctionEntry() { return BailoutId(kFunctionEntryId); }
1057 static BailoutId Declarations() { return BailoutId(kDeclarationsId); }
1058 static BailoutId FirstUsable() { return BailoutId(kFirstUsableId); }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001059 static BailoutId StubEntry() { return BailoutId(kStubEntryId); }
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001060
1061 bool IsNone() const { return id_ == kNoneId; }
1062 bool operator==(const BailoutId& other) const { return id_ == other.id_; }
1063
1064 private:
1065 static const int kNoneId = -1;
1066
1067 // Using 0 could disguise errors.
1068 static const int kFunctionEntryId = 2;
1069
1070 // This AST id identifies the point after the declarations have been visited.
1071 // We need it to capture the environment effects of declarations that emit
1072 // code (function declarations).
1073 static const int kDeclarationsId = 3;
1074
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001075 // Every FunctionState starts with this id.
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001076 static const int kFirstUsableId = 4;
1077
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001078 // Every compiled stub starts with this id.
1079 static const int kStubEntryId = 5;
1080
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001081 int id_;
1082};
1083
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001084} } // namespace v8::internal
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086#endif // V8_UTILS_H_