blob: 44865edede8dbc753e4e26e9b156e667bfefbc9a [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_UTILS_H_
6#define V8_UTILS_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include <limits.h>
Steve Blocka7e24c12009-10-30 11:49:00 +00009#include <stdlib.h>
Steve Block6ded16b2010-05-10 14:33:55 +010010#include <string.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include <cmath>
Steve Blocka7e24c12009-10-30 11:49:00 +000012
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "include/v8.h"
14#include "src/allocation.h"
15#include "src/base/bits.h"
16#include "src/base/logging.h"
17#include "src/base/macros.h"
18#include "src/base/platform/platform.h"
19#include "src/globals.h"
20#include "src/list.h"
21#include "src/vector.h"
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080022
Steve Blocka7e24c12009-10-30 11:49:00 +000023namespace v8 {
24namespace internal {
25
26// ----------------------------------------------------------------------------
27// General helper functions
28
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029// Returns the value (0 .. 15) of a hexadecimal character c.
30// If c is not a legal hexadecimal character, returns a value < 0.
31inline int HexValue(uc32 c) {
32 c -= '0';
33 if (static_cast<unsigned>(c) <= 9) return c;
34 c = (c | 0x20) - ('a' - '0'); // detect 0x11..0x16 and 0x31..0x36.
35 if (static_cast<unsigned>(c) <= 5) return c + 10;
36 return -1;
37}
38
39
40inline int BoolToInt(bool b) { return b ? 1 : 0; }
41
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010042
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043// Same as strcmp, but can handle NULL arguments.
44inline bool CStringEquals(const char* s1, const char* s2) {
45 return (s1 == s2) || (s1 != NULL && s2 != NULL && strcmp(s1, s2) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +000046}
47
48
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010049// X must be a power of 2. Returns the number of trailing zeros.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010050inline int WhichPowerOf2(uint32_t x) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 DCHECK(base::bits::IsPowerOfTwo32(x));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010052 int bits = 0;
53#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 uint32_t original_x = x;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010055#endif
56 if (x >= 0x10000) {
57 bits += 16;
58 x >>= 16;
59 }
60 if (x >= 0x100) {
61 bits += 8;
62 x >>= 8;
63 }
64 if (x >= 0x10) {
65 bits += 4;
66 x >>= 4;
67 }
68 switch (x) {
69 default: UNREACHABLE();
70 case 8: bits++; // Fall through.
71 case 4: bits++; // Fall through.
72 case 2: bits++; // Fall through.
73 case 1: break;
74 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 DCHECK_EQ(static_cast<uint32_t>(1) << bits, original_x);
76 return bits;
77}
78
79
80// X must be a power of 2. Returns the number of trailing zeros.
81inline int WhichPowerOf2_64(uint64_t x) {
82 DCHECK(base::bits::IsPowerOfTwo64(x));
83 int bits = 0;
84#ifdef DEBUG
85 uint64_t original_x = x;
86#endif
87 if (x >= 0x100000000L) {
88 bits += 32;
89 x >>= 32;
90 }
91 if (x >= 0x10000) {
92 bits += 16;
93 x >>= 16;
94 }
95 if (x >= 0x100) {
96 bits += 8;
97 x >>= 8;
98 }
99 if (x >= 0x10) {
100 bits += 4;
101 x >>= 4;
102 }
103 switch (x) {
104 default: UNREACHABLE();
105 case 8: bits++; // Fall through.
106 case 4: bits++; // Fall through.
107 case 2: bits++; // Fall through.
108 case 1: break;
109 }
110 DCHECK_EQ(static_cast<uint64_t>(1) << bits, original_x);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100111 return bits;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100112}
113
114
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115inline int MostSignificantBit(uint32_t x) {
116 static const int msb4[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};
117 int nibble = 0;
118 if (x & 0xffff0000) {
119 nibble += 16;
120 x >>= 16;
121 }
122 if (x & 0xff00) {
123 nibble += 8;
124 x >>= 8;
125 }
126 if (x & 0xf0) {
127 nibble += 4;
128 x >>= 4;
129 }
130 return nibble + msb4[x];
131}
132
133
Steve Blocka7e24c12009-10-30 11:49:00 +0000134// The C++ standard leaves the semantics of '>>' undefined for
135// negative signed operands. Most implementations do the right thing,
136// though.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100137inline int ArithmeticShiftRight(int x, int s) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 return x >> s;
139}
140
141
Steve Blocka7e24c12009-10-30 11:49:00 +0000142template <typename T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143int Compare(const T& a, const T& b) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 if (a == b)
145 return 0;
146 else if (a < b)
147 return -1;
148 else
149 return 1;
150}
151
152
153template <typename T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100154int PointerValueCompare(const T* a, const T* b) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 return Compare<T>(*a, *b);
156}
157
158
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100159// Compare function to compare the object pointer value of two
160// handlified objects. The handles are passed as pointers to the
161// handles.
162template<typename T> class Handle; // Forward declaration.
163template <typename T>
164int HandleObjectPointerCompare(const Handle<T>* a, const Handle<T>* b) {
165 return Compare<T*>(*(*a), *(*b));
166}
167
168
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100169template <typename T, typename U>
170inline bool IsAligned(T value, U alignment) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 return (value & (alignment - 1)) == 0;
172}
173
174
175// Returns true if (addr + offset) is aligned.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100176inline bool IsAddressAligned(Address addr,
177 intptr_t alignment,
178 int offset = 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 intptr_t offs = OffsetFrom(addr + offset);
180 return IsAligned(offs, alignment);
181}
182
183
184// Returns the maximum of the two parameters.
185template <typename T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100186T Max(T a, T b) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 return a < b ? b : a;
188}
189
190
191// Returns the minimum of the two parameters.
192template <typename T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100193T Min(T a, T b) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 return a < b ? a : b;
195}
196
197
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198// Returns the absolute value of its argument.
199template <typename T>
200T Abs(T a) {
201 return a < 0 ? -a : a;
Steve Blockd0582a62009-12-15 09:54:21 +0000202}
203
204
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205// Floor(-0.0) == 0.0
206inline double Floor(double x) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400207#if V8_CC_MSVC
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 if (x == 0) return x; // Fix for issue 3477.
209#endif
210 return std::floor(x);
211}
212
Ben Murdochda12d292016-06-02 14:46:10 +0100213inline double Pow(double x, double y) {
214#if (defined(__MINGW64_VERSION_MAJOR) && \
215 (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1)) || \
216 defined(V8_OS_AIX)
217 // MinGW64 and AIX have a custom implementation for pow. This handles certain
218 // special cases that are different.
219 if ((x == 0.0 || std::isinf(x)) && y != 0.0 && std::isfinite(y)) {
220 double f;
221 double result = ((x == 0.0) ^ (y > 0)) ? V8_INFINITY : 0;
222 /* retain sign if odd integer exponent */
223 return ((std::modf(y, &f) == 0.0) && (static_cast<int64_t>(y) & 1))
224 ? copysign(result, x)
225 : result;
226 }
227
228 if (x == 2.0) {
229 int y_int = static_cast<int>(y);
230 if (y == y_int) {
231 return std::ldexp(1.0, y_int);
232 }
233 }
234#endif
235 return std::pow(x, y);
236}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237
238// TODO(svenpanne) Clean up the whole power-of-2 mess.
239inline int32_t WhichPowerOf2Abs(int32_t x) {
240 return (x == kMinInt) ? 31 : WhichPowerOf2(Abs(x));
241}
242
243
244// Obtains the unsigned type corresponding to T
245// available in C++11 as std::make_unsigned
246template<typename T>
247struct make_unsigned {
248 typedef T type;
249};
250
251
252// Template specializations necessary to have make_unsigned work
253template<> struct make_unsigned<int32_t> {
254 typedef uint32_t type;
255};
256
257
258template<> struct make_unsigned<int64_t> {
259 typedef uint64_t type;
260};
261
262
Steve Blocka7e24c12009-10-30 11:49:00 +0000263// ----------------------------------------------------------------------------
264// BitField is a help template for encoding and decode bitfield with
265// unsigned content.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266
267template<class T, int shift, int size, class U>
268class BitFieldBase {
Steve Blocka7e24c12009-10-30 11:49:00 +0000269 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000270 // A type U mask of bit field. To use all bits of a type U of x bits
271 // in a bitfield without compiler warnings we have to compute 2^x
272 // without using a shift count of x in the computation.
273 static const U kOne = static_cast<U>(1U);
274 static const U kMask = ((kOne << shift) << size) - (kOne << shift);
275 static const U kShift = shift;
276 static const U kSize = size;
277 static const U kNext = kShift + kSize;
Ben Murdoch589d6972011-11-30 16:04:58 +0000278
279 // Value for the field with all bits set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 static const T kMax = static_cast<T>((kOne << size) - 1);
Ben Murdoch589d6972011-11-30 16:04:58 +0000281
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 // Tells whether the provided value fits into the bit field.
283 static bool is_valid(T value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 return (static_cast<U>(value) & ~static_cast<U>(kMax)) == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 }
286
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 // Returns a type U with the bit field value encoded.
288 static U encode(T value) {
289 DCHECK(is_valid(value));
290 return static_cast<U>(value) << shift;
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 }
292
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 // Returns a type U with the bit field value updated.
294 static U update(U previous, T value) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000295 return (previous & ~kMask) | encode(value);
Ben Murdoch257744e2011-11-30 15:57:28 +0000296 }
297
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 // Extracts the bit field from the value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 static T decode(U value) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000300 return static_cast<T>((value & kMask) >> shift);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100301 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000302};
303
304
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400305template <class T, int shift, int size>
306class BitField8 : public BitFieldBase<T, shift, size, uint8_t> {};
307
308
309template <class T, int shift, int size>
310class BitField16 : public BitFieldBase<T, shift, size, uint16_t> {};
311
312
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313template<class T, int shift, int size>
314class BitField : public BitFieldBase<T, shift, size, uint32_t> { };
315
316
317template<class T, int shift, int size>
318class BitField64 : public BitFieldBase<T, shift, size, uint64_t> { };
319
320
Steve Blocka7e24c12009-10-30 11:49:00 +0000321// ----------------------------------------------------------------------------
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400322// BitSetComputer is a help template for encoding and decoding information for
323// a variable number of items in an array.
324//
325// To encode boolean data in a smi array you would use:
326// typedef BitSetComputer<bool, 1, kSmiValueSize, uint32_t> BoolComputer;
327//
328template <class T, int kBitsPerItem, int kBitsPerWord, class U>
329class BitSetComputer {
330 public:
331 static const int kItemsPerWord = kBitsPerWord / kBitsPerItem;
332 static const int kMask = (1 << kBitsPerItem) - 1;
333
334 // The number of array elements required to embed T information for each item.
335 static int word_count(int items) {
336 if (items == 0) return 0;
337 return (items - 1) / kItemsPerWord + 1;
338 }
339
340 // The array index to look at for item.
341 static int index(int base_index, int item) {
342 return base_index + item / kItemsPerWord;
343 }
344
345 // Extract T data for a given item from data.
346 static T decode(U data, int item) {
347 return static_cast<T>((data >> shift(item)) & kMask);
348 }
349
350 // Return the encoding for a store of value for item in previous.
351 static U encode(U previous, int item, T value) {
352 int shift_value = shift(item);
353 int set_bits = (static_cast<int>(value) << shift_value);
354 return (previous & ~(kMask << shift_value)) | set_bits;
355 }
356
357 static int shift(int item) { return (item % kItemsPerWord) * kBitsPerItem; }
358};
359
360
361// ----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000362// Hash function.
363
Ben Murdochc7cc0282012-03-05 14:35:55 +0000364static const uint32_t kZeroHashSeed = 0;
365
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800366// Thomas Wang, Integer Hash Functions.
367// http://www.concentric.net/~Ttwang/tech/inthash.htm
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100368inline uint32_t ComputeIntegerHash(uint32_t key, uint32_t seed) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800369 uint32_t hash = key;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000370 hash = hash ^ seed;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800371 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1;
372 hash = hash ^ (hash >> 12);
373 hash = hash + (hash << 2);
374 hash = hash ^ (hash >> 4);
375 hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11);
376 hash = hash ^ (hash >> 16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000377 return hash & 0x3fffffff;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800378}
Steve Blocka7e24c12009-10-30 11:49:00 +0000379
380
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100381inline uint32_t ComputeLongHash(uint64_t key) {
382 uint64_t hash = key;
383 hash = ~hash + (hash << 18); // hash = (hash << 18) - hash - 1;
384 hash = hash ^ (hash >> 31);
385 hash = hash * 21; // hash = (hash + (hash << 2)) + (hash << 4);
386 hash = hash ^ (hash >> 11);
387 hash = hash + (hash << 6);
388 hash = hash ^ (hash >> 22);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 return static_cast<uint32_t>(hash);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100390}
391
392
393inline uint32_t ComputePointerHash(void* ptr) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000394 return ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000395 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr)),
396 v8::internal::kZeroHashSeed);
Ben Murdoch257744e2011-11-30 15:57:28 +0000397}
398
399
Steve Blocka7e24c12009-10-30 11:49:00 +0000400// ----------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401// Generated memcpy/memmove
402
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403// Initializes the codegen support that depends on CPU features.
404void init_memcopy_functions(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000405
406#if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_X87)
407// Limit below which the extra overhead of the MemCopy function is likely
408// to outweigh the benefits of faster copying.
409const int kMinComplexMemCopy = 64;
410
411// Copy memory area. No restrictions.
412void MemMove(void* dest, const void* src, size_t size);
413typedef void (*MemMoveFunction)(void* dest, const void* src, size_t size);
414
415// Keep the distinction of "move" vs. "copy" for the benefit of other
416// architectures.
417V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
418 MemMove(dest, src, size);
419}
420#elif defined(V8_HOST_ARCH_ARM)
421typedef void (*MemCopyUint8Function)(uint8_t* dest, const uint8_t* src,
422 size_t size);
423extern MemCopyUint8Function memcopy_uint8_function;
424V8_INLINE void MemCopyUint8Wrapper(uint8_t* dest, const uint8_t* src,
425 size_t chars) {
426 memcpy(dest, src, chars);
427}
428// For values < 16, the assembler function is slower than the inlined C code.
429const int kMinComplexMemCopy = 16;
430V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
431 (*memcopy_uint8_function)(reinterpret_cast<uint8_t*>(dest),
432 reinterpret_cast<const uint8_t*>(src), size);
433}
434V8_INLINE void MemMove(void* dest, const void* src, size_t size) {
435 memmove(dest, src, size);
436}
437
438typedef void (*MemCopyUint16Uint8Function)(uint16_t* dest, const uint8_t* src,
439 size_t size);
440extern MemCopyUint16Uint8Function memcopy_uint16_uint8_function;
441void MemCopyUint16Uint8Wrapper(uint16_t* dest, const uint8_t* src,
442 size_t chars);
443// For values < 12, the assembler function is slower than the inlined C code.
444const int kMinComplexConvertMemCopy = 12;
445V8_INLINE void MemCopyUint16Uint8(uint16_t* dest, const uint8_t* src,
446 size_t size) {
447 (*memcopy_uint16_uint8_function)(dest, src, size);
448}
449#elif defined(V8_HOST_ARCH_MIPS)
450typedef void (*MemCopyUint8Function)(uint8_t* dest, const uint8_t* src,
451 size_t size);
452extern MemCopyUint8Function memcopy_uint8_function;
453V8_INLINE void MemCopyUint8Wrapper(uint8_t* dest, const uint8_t* src,
454 size_t chars) {
455 memcpy(dest, src, chars);
456}
457// For values < 16, the assembler function is slower than the inlined C code.
458const int kMinComplexMemCopy = 16;
459V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
460 (*memcopy_uint8_function)(reinterpret_cast<uint8_t*>(dest),
461 reinterpret_cast<const uint8_t*>(src), size);
462}
463V8_INLINE void MemMove(void* dest, const void* src, size_t size) {
464 memmove(dest, src, size);
465}
466#else
467// Copy memory area to disjoint memory area.
468V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
469 memcpy(dest, src, size);
470}
471V8_INLINE void MemMove(void* dest, const void* src, size_t size) {
472 memmove(dest, src, size);
473}
474const int kMinComplexMemCopy = 16 * kPointerSize;
475#endif // V8_TARGET_ARCH_IA32
476
477
478// ----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000479// Miscellaneous
480
481// A static resource holds a static instance that can be reserved in
482// a local scope using an instance of Access. Attempts to re-reserve
483// the instance will cause an error.
484template <typename T>
485class StaticResource {
486 public:
487 StaticResource() : is_reserved_(false) {}
488
489 private:
490 template <typename S> friend class Access;
491 T instance_;
492 bool is_reserved_;
493};
494
495
496// Locally scoped access to a static resource.
497template <typename T>
498class Access {
499 public:
500 explicit Access(StaticResource<T>* resource)
501 : resource_(resource)
502 , instance_(&resource->instance_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 DCHECK(!resource->is_reserved_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 resource->is_reserved_ = true;
505 }
506
507 ~Access() {
508 resource_->is_reserved_ = false;
509 resource_ = NULL;
510 instance_ = NULL;
511 }
512
513 T* value() { return instance_; }
514 T* operator -> () { return instance_; }
515
516 private:
517 StaticResource<T>* resource_;
518 T* instance_;
519};
520
521
Ben Murdochb0fe1622011-05-05 13:52:32 +0100522// A pointer that can only be set once and doesn't allow NULL values.
523template<typename T>
524class SetOncePointer {
525 public:
526 SetOncePointer() : pointer_(NULL) { }
527
528 bool is_set() const { return pointer_ != NULL; }
529
530 T* get() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 DCHECK(pointer_ != NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100532 return pointer_;
533 }
534
535 void set(T* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536 DCHECK(pointer_ == NULL && value != NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100537 pointer_ = value;
538 }
539
540 private:
541 T* pointer_;
542};
543
544
Steve Blocka7e24c12009-10-30 11:49:00 +0000545template <typename T, int kSize>
546class EmbeddedVector : public Vector<T> {
547 public:
548 EmbeddedVector() : Vector<T>(buffer_, kSize) { }
549
Ben Murdochb0fe1622011-05-05 13:52:32 +0100550 explicit EmbeddedVector(T initial_value) : Vector<T>(buffer_, kSize) {
551 for (int i = 0; i < kSize; ++i) {
552 buffer_[i] = initial_value;
553 }
554 }
555
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 // When copying, make underlying Vector to reference our buffer.
557 EmbeddedVector(const EmbeddedVector& rhs)
558 : Vector<T>(rhs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559 MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize);
560 this->set_start(buffer_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 }
562
563 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
564 if (this == &rhs) return *this;
565 Vector<T>::operator=(rhs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100567 this->set_start(buffer_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 return *this;
569 }
570
571 private:
572 T buffer_[kSize];
573};
574
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575// Compare 8bit/16bit chars to 8bit/16bit chars.
Steve Block6ded16b2010-05-10 14:33:55 +0100576template <typename lchar, typename rchar>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000577inline int CompareCharsUnsigned(const lchar* lhs, const rchar* rhs,
578 size_t chars) {
Steve Block6ded16b2010-05-10 14:33:55 +0100579 const lchar* limit = lhs + chars;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 if (sizeof(*lhs) == sizeof(char) && sizeof(*rhs) == sizeof(char)) {
581 // memcmp compares byte-by-byte, yielding wrong results for two-byte
582 // strings on little-endian systems.
583 return memcmp(lhs, rhs, chars);
Steve Block6ded16b2010-05-10 14:33:55 +0100584 }
Steve Block6ded16b2010-05-10 14:33:55 +0100585 while (lhs < limit) {
586 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs);
587 if (r != 0) return r;
588 ++lhs;
589 ++rhs;
590 }
591 return 0;
592}
593
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000594template <typename lchar, typename rchar>
595inline int CompareChars(const lchar* lhs, const rchar* rhs, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596 DCHECK(sizeof(lchar) <= 2);
597 DCHECK(sizeof(rchar) <= 2);
598 if (sizeof(lchar) == 1) {
599 if (sizeof(rchar) == 1) {
600 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs),
601 reinterpret_cast<const uint8_t*>(rhs),
602 chars);
603 } else {
604 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs),
605 reinterpret_cast<const uint16_t*>(rhs),
606 chars);
607 }
608 } else {
609 if (sizeof(rchar) == 1) {
610 return CompareCharsUnsigned(reinterpret_cast<const uint16_t*>(lhs),
611 reinterpret_cast<const uint8_t*>(rhs),
612 chars);
613 } else {
614 return CompareCharsUnsigned(reinterpret_cast<const uint16_t*>(lhs),
615 reinterpret_cast<const uint16_t*>(rhs),
616 chars);
617 }
618 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800619}
Steve Blockd0582a62009-12-15 09:54:21 +0000620
Steve Block6ded16b2010-05-10 14:33:55 +0100621
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622// Calculate 10^exponent.
623inline int TenToThe(int exponent) {
624 DCHECK(exponent <= 9);
625 DCHECK(exponent >= 1);
626 int answer = 10;
627 for (int i = 1; i < exponent; i++) answer *= 10;
628 return answer;
Iain Merrick75681382010-08-19 15:07:18 +0100629}
Steve Blocka7e24c12009-10-30 11:49:00 +0000630
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000631
632template<typename ElementType, int NumElements>
633class EmbeddedContainer {
634 public:
635 EmbeddedContainer() : elems_() { }
636
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 int length() const { return NumElements; }
638 const ElementType& operator[](int i) const {
639 DCHECK(i < length());
640 return elems_[i];
641 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000642 ElementType& operator[](int i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000643 DCHECK(i < length());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000644 return elems_[i];
645 }
646
647 private:
648 ElementType elems_[NumElements];
649};
650
651
652template<typename ElementType>
653class EmbeddedContainer<ElementType, 0> {
654 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000655 int length() const { return 0; }
656 const ElementType& operator[](int i) const {
657 UNREACHABLE();
658 static ElementType t = 0;
659 return t;
660 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000661 ElementType& operator[](int i) {
662 UNREACHABLE();
663 static ElementType t = 0;
664 return t;
665 }
666};
667
668
669// Helper class for building result strings in a character buffer. The
670// purpose of the class is to use safe operations that checks the
671// buffer bounds on all operations in debug mode.
672// This simple base class does not allow formatted output.
673class SimpleStringBuilder {
674 public:
675 // Create a string builder with a buffer of the given size. The
676 // buffer is allocated through NewArray<char> and must be
677 // deallocated by the caller of Finalize().
678 explicit SimpleStringBuilder(int size);
679
680 SimpleStringBuilder(char* buffer, int size)
681 : buffer_(buffer, size), position_(0) { }
682
683 ~SimpleStringBuilder() { if (!is_finalized()) Finalize(); }
684
685 int size() const { return buffer_.length(); }
686
687 // Get the current position in the builder.
688 int position() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 DCHECK(!is_finalized());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000690 return position_;
691 }
692
693 // Reset the position.
694 void Reset() { position_ = 0; }
695
696 // Add a single character to the builder. It is not allowed to add
697 // 0-characters; use the Finalize() method to terminate the string
698 // instead.
699 void AddCharacter(char c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 DCHECK(c != '\0');
701 DCHECK(!is_finalized() && position_ < buffer_.length());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000702 buffer_[position_++] = c;
703 }
704
705 // Add an entire string to the builder. Uses strlen() internally to
706 // compute the length of the input string.
707 void AddString(const char* s);
708
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400709 // Add the first 'n' characters of the given 0-terminated string 's' to the
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000710 // builder. The input string must have enough characters.
711 void AddSubstring(const char* s, int n);
712
713 // Add character padding to the builder. If count is non-positive,
714 // nothing is added to the builder.
715 void AddPadding(char c, int count);
716
717 // Add the decimal representation of the value.
718 void AddDecimalInteger(int value);
719
720 // Finalize the string by 0-terminating it and returning the buffer.
721 char* Finalize();
722
723 protected:
724 Vector<char> buffer_;
725 int position_;
726
727 bool is_finalized() const { return position_ < 0; }
Ben Murdoch589d6972011-11-30 16:04:58 +0000728
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000729 private:
730 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder);
731};
732
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000733
734// A poor man's version of STL's bitset: A bit set of enums E (without explicit
735// values), fitting into an integral type T.
736template <class E, class T = int>
737class EnumSet {
738 public:
739 explicit EnumSet(T bits = 0) : bits_(bits) {}
740 bool IsEmpty() const { return bits_ == 0; }
741 bool Contains(E element) const { return (bits_ & Mask(element)) != 0; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100742 bool ContainsAnyOf(const EnumSet& set) const {
743 return (bits_ & set.bits_) != 0;
744 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000745 void Add(E element) { bits_ |= Mask(element); }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100746 void Add(const EnumSet& set) { bits_ |= set.bits_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000747 void Remove(E element) { bits_ &= ~Mask(element); }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100748 void Remove(const EnumSet& set) { bits_ &= ~set.bits_; }
749 void RemoveAll() { bits_ = 0; }
750 void Intersect(const EnumSet& set) { bits_ &= set.bits_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000751 T ToIntegral() const { return bits_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100752 bool operator==(const EnumSet& set) { return bits_ == set.bits_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753 bool operator!=(const EnumSet& set) { return bits_ != set.bits_; }
754 EnumSet<E, T> operator|(const EnumSet& set) const {
755 return EnumSet<E, T>(bits_ | set.bits_);
756 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000757
758 private:
759 T Mask(E element) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000760 // The strange typing in DCHECK is necessary to avoid stupid warnings, see:
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000761 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43680
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000762 DCHECK(static_cast<int>(element) < static_cast<int>(sizeof(T) * CHAR_BIT));
763 return static_cast<T>(1) << element;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000764 }
765
766 T bits_;
767};
768
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769// Bit field extraction.
770inline uint32_t unsigned_bitextract_32(int msb, int lsb, uint32_t x) {
771 return (x >> lsb) & ((1 << (1 + msb - lsb)) - 1);
772}
773
774inline uint64_t unsigned_bitextract_64(int msb, int lsb, uint64_t x) {
775 return (x >> lsb) & ((static_cast<uint64_t>(1) << (1 + msb - lsb)) - 1);
776}
777
778inline int32_t signed_bitextract_32(int msb, int lsb, int32_t x) {
779 return (x << (31 - msb)) >> (lsb + 31 - msb);
780}
781
782inline int signed_bitextract_64(int msb, int lsb, int x) {
783 // TODO(jbramley): This is broken for big bitfields.
784 return (x << (63 - msb)) >> (lsb + 63 - msb);
785}
786
787// Check number width.
788inline bool is_intn(int64_t x, unsigned n) {
789 DCHECK((0 < n) && (n < 64));
790 int64_t limit = static_cast<int64_t>(1) << (n - 1);
791 return (-limit <= x) && (x < limit);
792}
793
794inline bool is_uintn(int64_t x, unsigned n) {
795 DCHECK((0 < n) && (n < (sizeof(x) * kBitsPerByte)));
796 return !(x >> n);
797}
798
799template <class T>
800inline T truncate_to_intn(T x, unsigned n) {
801 DCHECK((0 < n) && (n < (sizeof(x) * kBitsPerByte)));
802 return (x & ((static_cast<T>(1) << n) - 1));
803}
804
805#define INT_1_TO_63_LIST(V) \
806V(1) V(2) V(3) V(4) V(5) V(6) V(7) V(8) \
807V(9) V(10) V(11) V(12) V(13) V(14) V(15) V(16) \
808V(17) V(18) V(19) V(20) V(21) V(22) V(23) V(24) \
809V(25) V(26) V(27) V(28) V(29) V(30) V(31) V(32) \
810V(33) V(34) V(35) V(36) V(37) V(38) V(39) V(40) \
811V(41) V(42) V(43) V(44) V(45) V(46) V(47) V(48) \
812V(49) V(50) V(51) V(52) V(53) V(54) V(55) V(56) \
813V(57) V(58) V(59) V(60) V(61) V(62) V(63)
814
815#define DECLARE_IS_INT_N(N) \
816inline bool is_int##N(int64_t x) { return is_intn(x, N); }
817#define DECLARE_IS_UINT_N(N) \
818template <class T> \
819inline bool is_uint##N(T x) { return is_uintn(x, N); }
820#define DECLARE_TRUNCATE_TO_INT_N(N) \
821template <class T> \
822inline T truncate_to_int##N(T x) { return truncate_to_intn(x, N); }
823INT_1_TO_63_LIST(DECLARE_IS_INT_N)
824INT_1_TO_63_LIST(DECLARE_IS_UINT_N)
825INT_1_TO_63_LIST(DECLARE_TRUNCATE_TO_INT_N)
826#undef DECLARE_IS_INT_N
827#undef DECLARE_IS_UINT_N
828#undef DECLARE_TRUNCATE_TO_INT_N
829
830class TypeFeedbackId {
831 public:
832 explicit TypeFeedbackId(int id) : id_(id) { }
833 int ToInt() const { return id_; }
834
835 static TypeFeedbackId None() { return TypeFeedbackId(kNoneId); }
836 bool IsNone() const { return id_ == kNoneId; }
837
838 private:
839 static const int kNoneId = -1;
840
841 int id_;
842};
843
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000844inline bool operator<(TypeFeedbackId lhs, TypeFeedbackId rhs) {
845 return lhs.ToInt() < rhs.ToInt();
846}
847inline bool operator>(TypeFeedbackId lhs, TypeFeedbackId rhs) {
848 return lhs.ToInt() > rhs.ToInt();
849}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000851
852class FeedbackVectorSlot {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400853 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000854 FeedbackVectorSlot() : id_(kInvalidSlot) {}
855 explicit FeedbackVectorSlot(int id) : id_(id) {}
856
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400857 int ToInt() const { return id_; }
858
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000859 static FeedbackVectorSlot Invalid() { return FeedbackVectorSlot(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400860 bool IsInvalid() const { return id_ == kInvalidSlot; }
861
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000862 bool operator==(FeedbackVectorSlot that) const {
863 return this->id_ == that.id_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400864 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000865 bool operator!=(FeedbackVectorSlot that) const { return !(*this == that); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400866
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000867 friend size_t hash_value(FeedbackVectorSlot slot) { return slot.ToInt(); }
868 friend std::ostream& operator<<(std::ostream& os, FeedbackVectorSlot);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400869
870 private:
871 static const int kInvalidSlot = -1;
872
873 int id_;
874};
875
876
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000877class BailoutId {
878 public:
879 explicit BailoutId(int id) : id_(id) { }
880 int ToInt() const { return id_; }
881
882 static BailoutId None() { return BailoutId(kNoneId); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000883 static BailoutId ScriptContext() { return BailoutId(kScriptContextId); }
884 static BailoutId FunctionContext() { return BailoutId(kFunctionContextId); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 static BailoutId FunctionEntry() { return BailoutId(kFunctionEntryId); }
886 static BailoutId Declarations() { return BailoutId(kDeclarationsId); }
887 static BailoutId FirstUsable() { return BailoutId(kFirstUsableId); }
888 static BailoutId StubEntry() { return BailoutId(kStubEntryId); }
889
890 bool IsNone() const { return id_ == kNoneId; }
891 bool operator==(const BailoutId& other) const { return id_ == other.id_; }
892 bool operator!=(const BailoutId& other) const { return id_ != other.id_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400893 friend size_t hash_value(BailoutId);
894 friend std::ostream& operator<<(std::ostream&, BailoutId);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895
896 private:
897 static const int kNoneId = -1;
898
899 // Using 0 could disguise errors.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000900 static const int kScriptContextId = 1;
901 static const int kFunctionContextId = 2;
902 static const int kFunctionEntryId = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903
904 // This AST id identifies the point after the declarations have been visited.
905 // We need it to capture the environment effects of declarations that emit
906 // code (function declarations).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000907 static const int kDeclarationsId = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000908
909 // Every FunctionState starts with this id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000910 static const int kFirstUsableId = 5;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000911
912 // Every compiled stub starts with this id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000913 static const int kStubEntryId = 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914
915 int id_;
916};
917
Ben Murdoch097c5b22016-05-18 11:27:45 +0100918class TokenDispenserForFinally {
919 public:
920 int GetBreakContinueToken() { return next_token_++; }
921 static const int kFallThroughToken = 0;
922 static const int kThrowToken = 1;
923 static const int kReturnToken = 2;
924
925 static const int kFirstBreakContinueToken = 3;
926 static const int kInvalidToken = -1;
927
928 private:
929 int next_token_ = kFirstBreakContinueToken;
930};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932// ----------------------------------------------------------------------------
933// I/O support.
934
935#if __GNUC__ >= 4
936// On gcc we can ask the compiler to check the types of %d-style format
937// specifiers and their associated arguments. TODO(erikcorry) fix this
938// so it works on MacOSX.
939#if defined(__MACH__) && defined(__APPLE__)
940#define PRINTF_CHECKING
941#define FPRINTF_CHECKING
942#define PRINTF_METHOD_CHECKING
943#define FPRINTF_METHOD_CHECKING
944#else // MacOsX.
945#define PRINTF_CHECKING __attribute__ ((format (printf, 1, 2)))
946#define FPRINTF_CHECKING __attribute__ ((format (printf, 2, 3)))
947#define PRINTF_METHOD_CHECKING __attribute__ ((format (printf, 2, 3)))
948#define FPRINTF_METHOD_CHECKING __attribute__ ((format (printf, 3, 4)))
949#endif
950#else
951#define PRINTF_CHECKING
952#define FPRINTF_CHECKING
953#define PRINTF_METHOD_CHECKING
954#define FPRINTF_METHOD_CHECKING
955#endif
956
957// Our version of printf().
958void PRINTF_CHECKING PrintF(const char* format, ...);
959void FPRINTF_CHECKING PrintF(FILE* out, const char* format, ...);
960
961// Prepends the current process ID to the output.
962void PRINTF_CHECKING PrintPID(const char* format, ...);
963
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000964// Prepends the current process ID and given isolate pointer to the output.
965void PrintIsolate(void* isolate, const char* format, ...);
966
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000967// Safe formatting print. Ensures that str is always null-terminated.
968// Returns the number of chars written, or -1 if output was truncated.
969int FPRINTF_CHECKING SNPrintF(Vector<char> str, const char* format, ...);
970int VSNPrintF(Vector<char> str, const char* format, va_list args);
971
972void StrNCpy(Vector<char> dest, const char* src, size_t n);
973
974// Our version of fflush.
975void Flush(FILE* out);
976
977inline void Flush() {
978 Flush(stdout);
979}
980
981
982// Read a line of characters after printing the prompt to stdout. The resulting
983// char* needs to be disposed off with DeleteArray by the caller.
984char* ReadLine(const char* prompt);
985
986
987// Read and return the raw bytes in a file. the size of the buffer is returned
988// in size.
989// The returned buffer must be freed by the caller.
990byte* ReadBytes(const char* filename, int* size, bool verbose = true);
991
992
993// Append size chars from str to the file given by filename.
994// The file is overwritten. Returns the number of chars written.
995int AppendChars(const char* filename,
996 const char* str,
997 int size,
998 bool verbose = true);
999
1000
1001// Write size chars from str to the file given by filename.
1002// The file is overwritten. Returns the number of chars written.
1003int WriteChars(const char* filename,
1004 const char* str,
1005 int size,
1006 bool verbose = true);
1007
1008
1009// Write size bytes to the file given by filename.
1010// The file is overwritten. Returns the number of bytes written.
1011int WriteBytes(const char* filename,
1012 const byte* bytes,
1013 int size,
1014 bool verbose = true);
1015
1016
1017// Write the C code
1018// const char* <varname> = "<str>";
1019// const int <varname>_len = <len>;
1020// to the file given by filename. Only the first len chars are written.
1021int WriteAsCFile(const char* filename, const char* varname,
1022 const char* str, int size, bool verbose = true);
1023
1024
1025// ----------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001026// Memory
1027
1028// Copies words from |src| to |dst|. The data spans must not overlap.
1029template <typename T>
1030inline void CopyWords(T* dst, const T* src, size_t num_words) {
1031 STATIC_ASSERT(sizeof(T) == kPointerSize);
1032 // TODO(mvstanton): disabled because mac builds are bogus failing on this
1033 // assert. They are doing a signed comparison. Investigate in
1034 // the morning.
1035 // DCHECK(Min(dst, const_cast<T*>(src)) + num_words <=
1036 // Max(dst, const_cast<T*>(src)));
1037 DCHECK(num_words > 0);
1038
1039 // Use block copying MemCopy if the segment we're copying is
1040 // enough to justify the extra call/setup overhead.
1041 static const size_t kBlockCopyLimit = 16;
1042
1043 if (num_words < kBlockCopyLimit) {
1044 do {
1045 num_words--;
1046 *dst++ = *src++;
1047 } while (num_words > 0);
1048 } else {
1049 MemCopy(dst, src, num_words * kPointerSize);
1050 }
1051}
1052
1053
1054// Copies words from |src| to |dst|. No restrictions.
1055template <typename T>
1056inline void MoveWords(T* dst, const T* src, size_t num_words) {
1057 STATIC_ASSERT(sizeof(T) == kPointerSize);
1058 DCHECK(num_words > 0);
1059
1060 // Use block copying MemCopy if the segment we're copying is
1061 // enough to justify the extra call/setup overhead.
1062 static const size_t kBlockCopyLimit = 16;
1063
1064 if (num_words < kBlockCopyLimit &&
1065 ((dst < src) || (dst >= (src + num_words * kPointerSize)))) {
1066 T* end = dst + num_words;
1067 do {
1068 num_words--;
1069 *dst++ = *src++;
1070 } while (num_words > 0);
1071 } else {
1072 MemMove(dst, src, num_words * kPointerSize);
1073 }
1074}
1075
1076
1077// Copies data from |src| to |dst|. The data spans must not overlap.
1078template <typename T>
1079inline void CopyBytes(T* dst, const T* src, size_t num_bytes) {
1080 STATIC_ASSERT(sizeof(T) == 1);
1081 DCHECK(Min(dst, const_cast<T*>(src)) + num_bytes <=
1082 Max(dst, const_cast<T*>(src)));
1083 if (num_bytes == 0) return;
1084
1085 // Use block copying MemCopy if the segment we're copying is
1086 // enough to justify the extra call/setup overhead.
1087 static const int kBlockCopyLimit = kMinComplexMemCopy;
1088
1089 if (num_bytes < static_cast<size_t>(kBlockCopyLimit)) {
1090 do {
1091 num_bytes--;
1092 *dst++ = *src++;
1093 } while (num_bytes > 0);
1094 } else {
1095 MemCopy(dst, src, num_bytes);
1096 }
1097}
1098
1099
1100template <typename T, typename U>
1101inline void MemsetPointer(T** dest, U* value, int counter) {
1102#ifdef DEBUG
1103 T* a = NULL;
1104 U* b = NULL;
1105 a = b; // Fake assignment to check assignability.
1106 USE(a);
1107#endif // DEBUG
1108#if V8_HOST_ARCH_IA32
1109#define STOS "stosl"
1110#elif V8_HOST_ARCH_X64
1111#if V8_HOST_ARCH_32_BIT
1112#define STOS "addr32 stosl"
1113#else
1114#define STOS "stosq"
1115#endif
1116#endif
1117#if defined(__native_client__)
1118 // This STOS sequence does not validate for x86_64 Native Client.
1119 // Here we #undef STOS to force use of the slower C version.
1120 // TODO(bradchen): Profile V8 and implement a faster REP STOS
1121 // here if the profile indicates it matters.
1122#undef STOS
1123#endif
1124
1125#if defined(MEMORY_SANITIZER)
1126 // MemorySanitizer does not understand inline assembly.
1127#undef STOS
1128#endif
1129
1130#if defined(__GNUC__) && defined(STOS)
1131 asm volatile(
1132 "cld;"
1133 "rep ; " STOS
1134 : "+&c" (counter), "+&D" (dest)
1135 : "a" (value)
1136 : "memory", "cc");
1137#else
1138 for (int i = 0; i < counter; i++) {
1139 dest[i] = value;
1140 }
1141#endif
1142
1143#undef STOS
1144}
1145
1146
1147// Simple support to read a file into a 0-terminated C-string.
1148// The returned buffer must be freed by the caller.
1149// On return, *exits tells whether the file existed.
1150Vector<const char> ReadFile(const char* filename,
1151 bool* exists,
1152 bool verbose = true);
1153Vector<const char> ReadFile(FILE* file,
1154 bool* exists,
1155 bool verbose = true);
1156
1157
1158template <typename sourcechar, typename sinkchar>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001159INLINE(static void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src,
1160 size_t chars));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001161#if defined(V8_HOST_ARCH_ARM)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001162INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars));
1163INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src,
1164 size_t chars));
1165INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src,
1166 size_t chars));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001167#elif defined(V8_HOST_ARCH_MIPS)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001168INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars));
1169INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src,
1170 size_t chars));
Ben Murdochda12d292016-06-02 14:46:10 +01001171#elif defined(V8_HOST_ARCH_PPC) || defined(V8_HOST_ARCH_S390)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001172INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars));
1173INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src,
1174 size_t chars));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001175#endif
1176
1177// Copy from 8bit/16bit chars to 8bit/16bit chars.
1178template <typename sourcechar, typename sinkchar>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001179INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, size_t chars));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001181template <typename sourcechar, typename sinkchar>
1182void CopyChars(sinkchar* dest, const sourcechar* src, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001183 DCHECK(sizeof(sourcechar) <= 2);
1184 DCHECK(sizeof(sinkchar) <= 2);
1185 if (sizeof(sinkchar) == 1) {
1186 if (sizeof(sourcechar) == 1) {
1187 CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest),
1188 reinterpret_cast<const uint8_t*>(src),
1189 chars);
1190 } else {
1191 CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest),
1192 reinterpret_cast<const uint16_t*>(src),
1193 chars);
1194 }
1195 } else {
1196 if (sizeof(sourcechar) == 1) {
1197 CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest),
1198 reinterpret_cast<const uint8_t*>(src),
1199 chars);
1200 } else {
1201 CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest),
1202 reinterpret_cast<const uint16_t*>(src),
1203 chars);
1204 }
1205 }
1206}
1207
1208template <typename sourcechar, typename sinkchar>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001209void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 sinkchar* limit = dest + chars;
1211 if ((sizeof(*dest) == sizeof(*src)) &&
1212 (chars >= static_cast<int>(kMinComplexMemCopy / sizeof(*dest)))) {
1213 MemCopy(dest, src, chars * sizeof(*dest));
1214 } else {
1215 while (dest < limit) *dest++ = static_cast<sinkchar>(*src++);
1216 }
1217}
1218
1219
1220#if defined(V8_HOST_ARCH_ARM)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001221void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 switch (static_cast<unsigned>(chars)) {
1223 case 0:
1224 break;
1225 case 1:
1226 *dest = *src;
1227 break;
1228 case 2:
1229 memcpy(dest, src, 2);
1230 break;
1231 case 3:
1232 memcpy(dest, src, 3);
1233 break;
1234 case 4:
1235 memcpy(dest, src, 4);
1236 break;
1237 case 5:
1238 memcpy(dest, src, 5);
1239 break;
1240 case 6:
1241 memcpy(dest, src, 6);
1242 break;
1243 case 7:
1244 memcpy(dest, src, 7);
1245 break;
1246 case 8:
1247 memcpy(dest, src, 8);
1248 break;
1249 case 9:
1250 memcpy(dest, src, 9);
1251 break;
1252 case 10:
1253 memcpy(dest, src, 10);
1254 break;
1255 case 11:
1256 memcpy(dest, src, 11);
1257 break;
1258 case 12:
1259 memcpy(dest, src, 12);
1260 break;
1261 case 13:
1262 memcpy(dest, src, 13);
1263 break;
1264 case 14:
1265 memcpy(dest, src, 14);
1266 break;
1267 case 15:
1268 memcpy(dest, src, 15);
1269 break;
1270 default:
1271 MemCopy(dest, src, chars);
1272 break;
1273 }
1274}
1275
1276
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001277void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src, size_t chars) {
1278 if (chars >= static_cast<size_t>(kMinComplexConvertMemCopy)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 MemCopyUint16Uint8(dest, src, chars);
1280 } else {
1281 MemCopyUint16Uint8Wrapper(dest, src, chars);
1282 }
1283}
1284
1285
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001286void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001287 switch (static_cast<unsigned>(chars)) {
1288 case 0:
1289 break;
1290 case 1:
1291 *dest = *src;
1292 break;
1293 case 2:
1294 memcpy(dest, src, 4);
1295 break;
1296 case 3:
1297 memcpy(dest, src, 6);
1298 break;
1299 case 4:
1300 memcpy(dest, src, 8);
1301 break;
1302 case 5:
1303 memcpy(dest, src, 10);
1304 break;
1305 case 6:
1306 memcpy(dest, src, 12);
1307 break;
1308 case 7:
1309 memcpy(dest, src, 14);
1310 break;
1311 default:
1312 MemCopy(dest, src, chars * sizeof(*dest));
1313 break;
1314 }
1315}
1316
1317
1318#elif defined(V8_HOST_ARCH_MIPS)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001319void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 if (chars < kMinComplexMemCopy) {
1321 memcpy(dest, src, chars);
1322 } else {
1323 MemCopy(dest, src, chars);
1324 }
1325}
1326
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001327void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, size_t chars) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001328 if (chars < kMinComplexMemCopy) {
1329 memcpy(dest, src, chars * sizeof(*dest));
1330 } else {
1331 MemCopy(dest, src, chars * sizeof(*dest));
1332 }
1333}
Ben Murdochda12d292016-06-02 14:46:10 +01001334#elif defined(V8_HOST_ARCH_PPC) || defined(V8_HOST_ARCH_S390)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001335#define CASE(n) \
1336 case n: \
1337 memcpy(dest, src, n); \
1338 break
1339void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars) {
1340 switch (static_cast<unsigned>(chars)) {
1341 case 0:
1342 break;
1343 case 1:
1344 *dest = *src;
1345 break;
1346 CASE(2);
1347 CASE(3);
1348 CASE(4);
1349 CASE(5);
1350 CASE(6);
1351 CASE(7);
1352 CASE(8);
1353 CASE(9);
1354 CASE(10);
1355 CASE(11);
1356 CASE(12);
1357 CASE(13);
1358 CASE(14);
1359 CASE(15);
1360 CASE(16);
1361 CASE(17);
1362 CASE(18);
1363 CASE(19);
1364 CASE(20);
1365 CASE(21);
1366 CASE(22);
1367 CASE(23);
1368 CASE(24);
1369 CASE(25);
1370 CASE(26);
1371 CASE(27);
1372 CASE(28);
1373 CASE(29);
1374 CASE(30);
1375 CASE(31);
1376 CASE(32);
1377 CASE(33);
1378 CASE(34);
1379 CASE(35);
1380 CASE(36);
1381 CASE(37);
1382 CASE(38);
1383 CASE(39);
1384 CASE(40);
1385 CASE(41);
1386 CASE(42);
1387 CASE(43);
1388 CASE(44);
1389 CASE(45);
1390 CASE(46);
1391 CASE(47);
1392 CASE(48);
1393 CASE(49);
1394 CASE(50);
1395 CASE(51);
1396 CASE(52);
1397 CASE(53);
1398 CASE(54);
1399 CASE(55);
1400 CASE(56);
1401 CASE(57);
1402 CASE(58);
1403 CASE(59);
1404 CASE(60);
1405 CASE(61);
1406 CASE(62);
1407 CASE(63);
1408 CASE(64);
1409 default:
1410 memcpy(dest, src, chars);
1411 break;
1412 }
1413}
1414#undef CASE
1415
1416#define CASE(n) \
1417 case n: \
1418 memcpy(dest, src, n * 2); \
1419 break
1420void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, size_t chars) {
1421 switch (static_cast<unsigned>(chars)) {
1422 case 0:
1423 break;
1424 case 1:
1425 *dest = *src;
1426 break;
1427 CASE(2);
1428 CASE(3);
1429 CASE(4);
1430 CASE(5);
1431 CASE(6);
1432 CASE(7);
1433 CASE(8);
1434 CASE(9);
1435 CASE(10);
1436 CASE(11);
1437 CASE(12);
1438 CASE(13);
1439 CASE(14);
1440 CASE(15);
1441 CASE(16);
1442 CASE(17);
1443 CASE(18);
1444 CASE(19);
1445 CASE(20);
1446 CASE(21);
1447 CASE(22);
1448 CASE(23);
1449 CASE(24);
1450 CASE(25);
1451 CASE(26);
1452 CASE(27);
1453 CASE(28);
1454 CASE(29);
1455 CASE(30);
1456 CASE(31);
1457 CASE(32);
1458 default:
1459 memcpy(dest, src, chars * 2);
1460 break;
1461 }
1462}
1463#undef CASE
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001464#endif
1465
1466
1467class StringBuilder : public SimpleStringBuilder {
1468 public:
1469 explicit StringBuilder(int size) : SimpleStringBuilder(size) { }
1470 StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { }
1471
1472 // Add formatted contents to the builder just like printf().
1473 void AddFormatted(const char* format, ...);
1474
1475 // Add formatted contents like printf based on a va_list.
1476 void AddFormattedList(const char* format, va_list list);
1477 private:
1478 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
1479};
1480
1481
1482bool DoubleToBoolean(double d);
1483
1484template <typename Stream>
1485bool StringToArrayIndex(Stream* stream, uint32_t* index) {
1486 uint16_t ch = stream->GetNext();
1487
1488 // If the string begins with a '0' character, it must only consist
1489 // of it to be a legal array index.
1490 if (ch == '0') {
1491 *index = 0;
1492 return !stream->HasMore();
1493 }
1494
1495 // Convert string to uint32 array index; character by character.
1496 int d = ch - '0';
1497 if (d < 0 || d > 9) return false;
1498 uint32_t result = d;
1499 while (stream->HasMore()) {
1500 d = stream->GetNext() - '0';
1501 if (d < 0 || d > 9) return false;
1502 // Check that the new result is below the 32 bit limit.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001503 if (result > 429496729U - ((d + 3) >> 3)) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001504 result = (result * 10) + d;
1505 }
1506
1507 *index = result;
1508 return true;
1509}
1510
1511
1512// Returns current value of top of the stack. Works correctly with ASAN.
1513DISABLE_ASAN
1514inline uintptr_t GetCurrentStackPosition() {
1515 // Takes the address of the limit variable in order to find out where
1516 // the top of stack is right now.
1517 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit);
1518 return limit;
1519}
1520
Ben Murdoch097c5b22016-05-18 11:27:45 +01001521template <typename V>
1522static inline V ReadUnalignedValue(const void* p) {
1523#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64)
1524 return *reinterpret_cast<const V*>(p);
1525#else
1526 V r;
1527 memmove(&r, p, sizeof(V));
1528 return r;
1529#endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
1530}
1531
1532template <typename V>
1533static inline void WriteUnalignedValue(void* p, V value) {
1534#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64)
1535 *(reinterpret_cast<V*>(p)) = value;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001536#else // V8_TARGET_ARCH_MIPS
Ben Murdoch097c5b22016-05-18 11:27:45 +01001537 memmove(p, &value, sizeof(V));
1538#endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
1539}
1540
1541static inline double ReadDoubleValue(const void* p) {
1542 return ReadUnalignedValue<double>(p);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543}
1544
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001545static inline void WriteDoubleValue(void* p, double value) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001546 WriteUnalignedValue(p, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001547}
1548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001549static inline uint16_t ReadUnalignedUInt16(const void* p) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001550 return ReadUnalignedValue<uint16_t>(p);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001551}
1552
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001553static inline void WriteUnalignedUInt16(void* p, uint16_t value) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001554 WriteUnalignedValue(p, value);
1555}
1556
Ben Murdochda12d292016-06-02 14:46:10 +01001557static inline uint32_t ReadUnalignedUInt32(const void* p) {
1558 return ReadUnalignedValue<uint32_t>(p);
1559}
1560
Ben Murdoch097c5b22016-05-18 11:27:45 +01001561static inline void WriteUnalignedUInt32(void* p, uint32_t value) {
1562 WriteUnalignedValue(p, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001563}
1564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565} // namespace internal
1566} // namespace v8
Steve Block6ded16b2010-05-10 14:33:55 +01001567
Steve Blocka7e24c12009-10-30 11:49:00 +00001568#endif // V8_UTILS_H_