blob: 3c8d873edd33d46a98e8475d29f2cdba0523603b [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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>
ager@chromium.orga74f0da2008-12-03 16:05:52 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37// ----------------------------------------------------------------------------
38// General helper functions
39
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000040// Returns true iff x is a power of 2 (or zero). Cannot be used with the
41// maximally negative value of the type T (the -1 overflows).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042template <typename T>
43static inline bool IsPowerOf2(T x) {
44 return (x & (x - 1)) == 0;
45}
46
47
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000048// The C++ standard leaves the semantics of '>>' undefined for
49// negative signed operands. Most implementations do the right thing,
50// though.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051static inline int ArithmeticShiftRight(int x, int s) {
52 return x >> s;
53}
54
55
56// Compute the 0-relative offset of some absolute value x of type T.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000057// This allows conversion of Addresses and integral types into
58// 0-relative int offsets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059template <typename T>
kasperl@chromium.org71affb52009-05-26 05:44:31 +000060static inline intptr_t OffsetFrom(T x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061 return x - static_cast<T>(0);
62}
63
64
65// Compute the absolute value of type T for some 0-relative offset x.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000066// This allows conversion of 0-relative int offsets into Addresses and
67// integral types.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068template <typename T>
kasperl@chromium.org71affb52009-05-26 05:44:31 +000069static inline T AddressFrom(intptr_t x) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000070 return static_cast<T>(static_cast<T>(0) + x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071}
72
73
74// Return the largest multiple of m which is <= x.
75template <typename T>
76static inline T RoundDown(T x, int m) {
77 ASSERT(IsPowerOf2(m));
78 return AddressFrom<T>(OffsetFrom(x) & -m);
79}
80
81
82// Return the smallest multiple of m which is >= x.
83template <typename T>
84static inline T RoundUp(T x, int m) {
85 return RoundDown(x + m - 1, m);
86}
87
88
ager@chromium.orga74f0da2008-12-03 16:05:52 +000089template <typename T>
90static int Compare(const T& a, const T& b) {
91 if (a == b)
92 return 0;
93 else if (a < b)
94 return -1;
95 else
96 return 1;
97}
98
99
100template <typename T>
101static int PointerValueCompare(const T* a, const T* b) {
102 return Compare<T>(*a, *b);
103}
104
105
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000106// Returns the smallest power of two which is >= x. If you pass in a
107// number that is already a power of two, it is returned as is.
108uint32_t RoundUpToPowerOf2(uint32_t x);
109
110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111template <typename T>
112static inline bool IsAligned(T value, T alignment) {
113 ASSERT(IsPowerOf2(alignment));
114 return (value & (alignment - 1)) == 0;
115}
116
117
118// Returns true if (addr + offset) is aligned.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000119static inline bool IsAddressAligned(Address addr,
120 intptr_t alignment,
121 int offset) {
122 intptr_t offs = OffsetFrom(addr + offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 return IsAligned(offs, alignment);
124}
125
126
127// Returns the maximum of the two parameters.
128template <typename T>
129static T Max(T a, T b) {
130 return a < b ? b : a;
131}
132
133
134// Returns the minimum of the two parameters.
135template <typename T>
136static T Min(T a, T b) {
137 return a < b ? a : b;
138}
139
140
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000141inline int StrLength(const char* string) {
142 size_t length = strlen(string);
143 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
144 return static_cast<int>(length);
145}
146
147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148// ----------------------------------------------------------------------------
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000149// BitField is a help template for encoding and decode bitfield with
150// unsigned content.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151template<class T, int shift, int size>
152class BitField {
153 public:
154 // Tells whether the provided value fits into the bit field.
155 static bool is_valid(T value) {
156 return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0;
157 }
158
159 // Returns a uint32_t mask of bit field.
160 static uint32_t mask() {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000161 // To use all bits of a uint32 in a bitfield without compiler warnings we
162 // have to compute 2^32 without using a shift count of 32.
163 return ((1U << shift) << size) - (1U << shift);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 }
165
166 // Returns a uint32_t with the bit field value encoded.
167 static uint32_t encode(T value) {
168 ASSERT(is_valid(value));
169 return static_cast<uint32_t>(value) << shift;
170 }
171
172 // Extracts the bit field from the value.
173 static T decode(uint32_t value) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000174 return static_cast<T>((value & mask()) >> shift);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 }
176};
177
178
179// ----------------------------------------------------------------------------
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180// Hash function.
181
182uint32_t ComputeIntegerHash(uint32_t key);
183
184
185// ----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186// I/O support.
187
188// Our version of printf(). Avoids compilation errors that we get
189// with standard printf when attempting to print pointers, etc.
190// (the errors are due to the extra compilation flags, which we
191// want elsewhere).
192void PrintF(const char* format, ...);
193
194// Our version of fflush.
195void Flush();
196
197
198// Read a line of characters after printing the prompt to stdout. The resulting
199// char* needs to be disposed off with DeleteArray by the caller.
200char* ReadLine(const char* prompt);
201
202
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000203// Read and return the raw bytes in a file. the size of the buffer is returned
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204// in size.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000205// The returned buffer must be freed by the caller.
206byte* ReadBytes(const char* filename, int* size, bool verbose = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
208
209// Write size chars from str to the file given by filename.
210// The file is overwritten. Returns the number of chars written.
211int WriteChars(const char* filename,
212 const char* str,
213 int size,
214 bool verbose = true);
215
216
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000217// Write size bytes to the file given by filename.
218// The file is overwritten. Returns the number of bytes written.
219int WriteBytes(const char* filename,
220 const byte* bytes,
221 int size,
222 bool verbose = true);
223
224
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225// Write the C code
226// const char* <varname> = "<str>";
227// const int <varname>_len = <len>;
228// to the file given by filename. Only the first len chars are written.
229int WriteAsCFile(const char* filename, const char* varname,
230 const char* str, int size, bool verbose = true);
231
232
233// ----------------------------------------------------------------------------
234// Miscellaneous
235
236// A static resource holds a static instance that can be reserved in
237// a local scope using an instance of Access. Attempts to re-reserve
238// the instance will cause an error.
239template <typename T>
240class StaticResource {
241 public:
242 StaticResource() : is_reserved_(false) {}
243
244 private:
245 template <typename S> friend class Access;
246 T instance_;
247 bool is_reserved_;
248};
249
250
251// Locally scoped access to a static resource.
252template <typename T>
253class Access {
254 public:
255 explicit Access(StaticResource<T>* resource)
256 : resource_(resource)
257 , instance_(&resource->instance_) {
258 ASSERT(!resource->is_reserved_);
259 resource->is_reserved_ = true;
260 }
261
262 ~Access() {
263 resource_->is_reserved_ = false;
264 resource_ = NULL;
265 instance_ = NULL;
266 }
267
268 T* value() { return instance_; }
269 T* operator -> () { return instance_; }
270
271 private:
272 StaticResource<T>* resource_;
273 T* instance_;
274};
275
276
277template <typename T>
278class Vector {
279 public:
kasper.lund7276f142008-07-30 08:49:36 +0000280 Vector() : start_(NULL), length_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 Vector(T* data, int length) : start_(data), length_(length) {
282 ASSERT(length == 0 || (length > 0 && data != NULL));
283 }
284
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000285 static Vector<T> New(int length) {
286 return Vector<T>(NewArray<T>(length), length);
287 }
288
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000289 // Returns a vector using the same backing storage as this one,
290 // spanning from and including 'from', to but not including 'to'.
291 Vector<T> SubVector(int from, int to) {
292 ASSERT(from < length_);
293 ASSERT(to <= length_);
294 ASSERT(from < to);
295 return Vector<T>(start() + from, to - from);
296 }
297
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 // Returns the length of the vector.
299 int length() const { return length_; }
300
301 // Returns whether or not the vector is empty.
302 bool is_empty() const { return length_ == 0; }
303
304 // Returns the pointer to the start of the data in the vector.
305 T* start() const { return start_; }
306
307 // Access individual vector elements - checks bounds in debug mode.
308 T& operator[](int index) const {
309 ASSERT(0 <= index && index < length_);
310 return start_[index];
311 }
312
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000313 T& first() { return start_[0]; }
314
315 T& last() { return start_[length_ - 1]; }
316
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 // Returns a clone of this vector with a new backing store.
318 Vector<T> Clone() const {
319 T* result = NewArray<T>(length_);
320 for (int i = 0; i < length_; i++) result[i] = start_[i];
321 return Vector<T>(result, length_);
322 }
323
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000324 void Sort(int (*cmp)(const T*, const T*)) {
325 typedef int (*RawComparer)(const void*, const void*);
326 qsort(start(),
327 length(),
328 sizeof(T),
329 reinterpret_cast<RawComparer>(cmp));
330 }
331
332 void Sort() {
333 Sort(PointerValueCompare<T>);
334 }
335
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000336 void Truncate(int length) {
337 ASSERT(length <= length_);
338 length_ = length;
339 }
340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // Releases the array underlying this vector. Once disposed the
342 // vector is empty.
343 void Dispose() {
kasper.lund7276f142008-07-30 08:49:36 +0000344 if (is_empty()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 DeleteArray(start_);
346 start_ = NULL;
347 length_ = 0;
348 }
349
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000350 inline Vector<T> operator+(int offset) {
351 ASSERT(offset < length_);
352 return Vector<T>(start_ + offset, length_ - offset);
353 }
354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 // Factory method for creating empty vectors.
356 static Vector<T> empty() { return Vector<T>(NULL, 0); }
357
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000358 protected:
359 void set_start(T* start) { start_ = start; }
360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 private:
362 T* start_;
363 int length_;
364};
365
366
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000367// A temporary assignment sets a (non-local) variable to a value on
368// construction and resets it the value on destruction.
369template <typename T>
370class TempAssign {
371 public:
372 TempAssign(T* var, T value): var_(var), old_value_(*var) {
373 *var = value;
374 }
375
376 ~TempAssign() { *var_ = old_value_; }
377
378 private:
379 T* var_;
380 T old_value_;
381};
382
383
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000384template <typename T, int kSize>
385class EmbeddedVector : public Vector<T> {
386 public:
387 EmbeddedVector() : Vector<T>(buffer_, kSize) { }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000388
389 // When copying, make underlying Vector to reference our buffer.
390 EmbeddedVector(const EmbeddedVector& rhs)
391 : Vector<T>(rhs) {
392 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
393 set_start(buffer_);
394 }
395
396 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
397 if (this == &rhs) return *this;
398 Vector<T>::operator=(rhs);
399 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000400 this->set_start(buffer_);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000401 return *this;
402 }
403
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000404 private:
405 T buffer_[kSize];
406};
407
408
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000409template <typename T>
410class ScopedVector : public Vector<T> {
411 public:
412 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
413 ~ScopedVector() {
414 DeleteArray(this->start());
415 }
416};
417
418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419inline Vector<const char> CStrVector(const char* data) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000420 return Vector<const char>(data, StrLength(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421}
422
423inline Vector<char> MutableCStrVector(char* data) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000424 return Vector<char>(data, StrLength(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425}
426
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000427inline Vector<char> MutableCStrVector(char* data, int max) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000428 int length = StrLength(data);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000429 return Vector<char>(data, (length < max) ? length : max);
430}
431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432template <typename T>
433inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms,
434 int length) {
435 return Vector< Handle<Object> >(
436 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length);
437}
438
439
440// Simple support to read a file into a 0-terminated C-string.
441// The returned buffer must be freed by the caller.
ager@chromium.org32912102009-01-16 10:38:43 +0000442// On return, *exits tells whether the file existed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443Vector<const char> ReadFile(const char* filename,
444 bool* exists,
445 bool verbose = true);
446
447
448// Simple wrapper that allows an ExternalString to refer to a
449// Vector<const char>. Doesn't assume ownership of the data.
450class AsciiStringAdapter: public v8::String::ExternalAsciiStringResource {
451 public:
452 explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {}
453
454 virtual const char* data() const { return data_.start(); }
455
456 virtual size_t length() const { return data_.length(); }
457
458 private:
459 Vector<const char> data_;
460};
461
462
kasper.lund7276f142008-07-30 08:49:36 +0000463// Helper class for building result strings in a character buffer. The
464// purpose of the class is to use safe operations that checks the
465// buffer bounds on all operations in debug mode.
466class StringBuilder {
467 public:
468 // Create a string builder with a buffer of the given size. The
469 // buffer is allocated through NewArray<char> and must be
470 // deallocated by the caller of Finalize().
471 explicit StringBuilder(int size);
472
473 StringBuilder(char* buffer, int size)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000474 : buffer_(buffer, size), position_(0) { }
kasper.lund7276f142008-07-30 08:49:36 +0000475
476 ~StringBuilder() { if (!is_finalized()) Finalize(); }
477
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000478 int size() const { return buffer_.length(); }
kasper.lund7276f142008-07-30 08:49:36 +0000479
480 // Get the current position in the builder.
481 int position() const {
482 ASSERT(!is_finalized());
483 return position_;
484 }
485
486 // Reset the position.
487 void Reset() { position_ = 0; }
488
489 // Add a single character to the builder. It is not allowed to add
490 // 0-characters; use the Finalize() method to terminate the string
491 // instead.
492 void AddCharacter(char c) {
493 ASSERT(c != '\0');
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000494 ASSERT(!is_finalized() && position_ < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +0000495 buffer_[position_++] = c;
496 }
497
498 // Add an entire string to the builder. Uses strlen() internally to
499 // compute the length of the input string.
500 void AddString(const char* s);
501
502 // Add the first 'n' characters of the given string 's' to the
503 // builder. The input string must have enough characters.
504 void AddSubstring(const char* s, int n);
505
506 // Add formatted contents to the builder just like printf().
507 void AddFormatted(const char* format, ...);
508
509 // Add character padding to the builder. If count is non-positive,
510 // nothing is added to the builder.
511 void AddPadding(char c, int count);
512
513 // Finalize the string by 0-terminating it and returning the buffer.
514 char* Finalize();
515
516 private:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000517 Vector<char> buffer_;
kasper.lund7276f142008-07-30 08:49:36 +0000518 int position_;
519
520 bool is_finalized() const { return position_ < 0; }
mads.s.ager31e71382008-08-13 09:32:07 +0000521
522 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
kasper.lund7276f142008-07-30 08:49:36 +0000523};
524
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000525
526// Copy from ASCII/16bit chars to ASCII/16bit chars.
527template <typename sourcechar, typename sinkchar>
528static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) {
529 sinkchar* limit = dest + chars;
ager@chromium.org9085a012009-05-11 19:22:57 +0000530#ifdef V8_HOST_CAN_READ_UNALIGNED
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000531 if (sizeof(*dest) == sizeof(*src)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000532 // Number of characters in a uintptr_t.
533 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000534 while (dest <= limit - kStepSize) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000535 *reinterpret_cast<uintptr_t*>(dest) =
536 *reinterpret_cast<const uintptr_t*>(src);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000537 dest += kStepSize;
538 src += kStepSize;
539 }
540 }
541#endif
542 while (dest < limit) {
543 *dest++ = static_cast<sinkchar>(*src++);
544 }
545}
546
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000547
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000548// Compare ASCII/16bit chars to ASCII/16bit chars.
549template <typename lchar, typename rchar>
550static inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) {
551 const lchar* limit = lhs + chars;
552#ifdef V8_HOST_CAN_READ_UNALIGNED
553 if (sizeof(*lhs) == sizeof(*rhs)) {
554 // Number of characters in a uintptr_t.
555 static const int kStepSize = sizeof(uintptr_t) / sizeof(*lhs); // NOLINT
556 while (lhs <= limit - kStepSize) {
557 if (*reinterpret_cast<const uintptr_t*>(lhs) !=
558 *reinterpret_cast<const uintptr_t*>(rhs)) {
559 break;
560 }
561 lhs += kStepSize;
562 rhs += kStepSize;
563 }
564 }
565#endif
566 while (lhs < limit) {
567 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs);
568 if (r != 0) return r;
569 ++lhs;
570 ++rhs;
571 }
572 return 0;
573}
574
575
576template <typename T>
577static inline void MemsetPointer(T** dest, T* value, int counter) {
578#if defined(V8_HOST_ARCH_IA32)
579#define STOS "stosl"
580#elif defined(V8_HOST_ARCH_X64)
581#define STOS "stosq"
582#endif
583
584#if defined(__GNUC__) && defined(STOS)
585 asm("cld;"
586 "rep ; " STOS
587 : /* no output */
588 : "c" (counter), "a" (value), "D" (dest)
589 : /* no clobbered list as all inputs are considered clobbered */);
590#else
591 for (int i = 0; i < counter; i++) {
592 dest[i] = value;
593 }
594#endif
595
596#undef STOS
597}
598
599
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000600// Calculate 10^exponent.
601int TenToThe(int exponent);
602
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000603
604// The type-based aliasing rule allows the compiler to assume that pointers of
605// different types (for some definition of different) never alias each other.
606// Thus the following code does not work:
607//
608// float f = foo();
609// int fbits = *(int*)(&f);
610//
611// The compiler 'knows' that the int pointer can't refer to f since the types
612// don't match, so the compiler may cache f in a register, leaving random data
613// in fbits. Using C++ style casts makes no difference, however a pointer to
614// char data is assumed to alias any other pointer. This is the 'memcpy
615// exception'.
616//
617// Bit_cast uses the memcpy exception to move the bits from a variable of one
618// type of a variable of another type. Of course the end result is likely to
619// be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
620// will completely optimize BitCast away.
621//
622// There is an additional use for BitCast.
623// Recent gccs will warn when they see casts that may result in breakage due to
624// the type-based aliasing rule. If you have checked that there is no breakage
625// you can use BitCast to cast one pointer type to another. This confuses gcc
626// enough that it can no longer see that you have cast one pointer type to
627// another thus avoiding the warning.
628template <class Dest, class Source>
629inline Dest BitCast(const Source& source) {
630 // Compile time assertion: sizeof(Dest) == sizeof(Source)
631 // A compile error here means your Dest and Source have different sizes.
632 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
633
634 Dest dest;
635 memcpy(&dest, &source, sizeof(dest));
636 return dest;
637}
638
639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640} } // namespace v8::internal
641
642#endif // V8_UTILS_H_