blob: deab09fb1193215b6d4dd06f910d2da3256a24ba [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>
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36// ----------------------------------------------------------------------------
37// General helper functions
38
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000039// Returns true iff x is a power of 2 (or zero). Cannot be used with the
40// maximally negative value of the type T (the -1 overflows).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041template <typename T>
42static inline bool IsPowerOf2(T x) {
43 return (x & (x - 1)) == 0;
44}
45
46
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000047// The C++ standard leaves the semantics of '>>' undefined for
48// negative signed operands. Most implementations do the right thing,
49// though.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050static inline int ArithmeticShiftRight(int x, int s) {
51 return x >> s;
52}
53
54
55// Compute the 0-relative offset of some absolute value x of type T.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000056// This allows conversion of Addresses and integral types into
57// 0-relative int offsets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058template <typename T>
kasperl@chromium.org71affb52009-05-26 05:44:31 +000059static inline intptr_t OffsetFrom(T x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060 return x - static_cast<T>(0);
61}
62
63
64// Compute the absolute value of type T for some 0-relative offset x.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000065// This allows conversion of 0-relative int offsets into Addresses and
66// integral types.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067template <typename T>
kasperl@chromium.org71affb52009-05-26 05:44:31 +000068static inline T AddressFrom(intptr_t x) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000069 return static_cast<T>(static_cast<T>(0) + x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070}
71
72
73// Return the largest multiple of m which is <= x.
74template <typename T>
75static inline T RoundDown(T x, int m) {
76 ASSERT(IsPowerOf2(m));
77 return AddressFrom<T>(OffsetFrom(x) & -m);
78}
79
80
81// Return the smallest multiple of m which is >= x.
82template <typename T>
83static inline T RoundUp(T x, int m) {
84 return RoundDown(x + m - 1, m);
85}
86
87
ager@chromium.orga74f0da2008-12-03 16:05:52 +000088template <typename T>
89static int Compare(const T& a, const T& b) {
90 if (a == b)
91 return 0;
92 else if (a < b)
93 return -1;
94 else
95 return 1;
96}
97
98
99template <typename T>
100static int PointerValueCompare(const T* a, const T* b) {
101 return Compare<T>(*a, *b);
102}
103
104
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000105// Returns the smallest power of two which is >= x. If you pass in a
106// number that is already a power of two, it is returned as is.
107uint32_t RoundUpToPowerOf2(uint32_t x);
108
109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110template <typename T>
111static inline bool IsAligned(T value, T alignment) {
112 ASSERT(IsPowerOf2(alignment));
113 return (value & (alignment - 1)) == 0;
114}
115
116
117// Returns true if (addr + offset) is aligned.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000118static inline bool IsAddressAligned(Address addr,
119 intptr_t alignment,
120 int offset) {
121 intptr_t offs = OffsetFrom(addr + offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 return IsAligned(offs, alignment);
123}
124
125
126// Returns the maximum of the two parameters.
127template <typename T>
128static T Max(T a, T b) {
129 return a < b ? b : a;
130}
131
132
133// Returns the minimum of the two parameters.
134template <typename T>
135static T Min(T a, T b) {
136 return a < b ? a : b;
137}
138
139
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000140inline int StrLength(const char* string) {
141 size_t length = strlen(string);
142 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
143 return static_cast<int>(length);
144}
145
146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147// ----------------------------------------------------------------------------
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000148// BitField is a help template for encoding and decode bitfield with
149// unsigned content.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150template<class T, int shift, int size>
151class BitField {
152 public:
153 // Tells whether the provided value fits into the bit field.
154 static bool is_valid(T value) {
155 return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0;
156 }
157
158 // Returns a uint32_t mask of bit field.
159 static uint32_t mask() {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000160 // To use all bits of a uint32 in a bitfield without compiler warnings we
161 // have to compute 2^32 without using a shift count of 32.
162 return ((1U << shift) << size) - (1U << shift);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 }
164
165 // Returns a uint32_t with the bit field value encoded.
166 static uint32_t encode(T value) {
167 ASSERT(is_valid(value));
168 return static_cast<uint32_t>(value) << shift;
169 }
170
171 // Extracts the bit field from the value.
172 static T decode(uint32_t value) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000173 return static_cast<T>((value & mask()) >> shift);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 }
175};
176
177
178// ----------------------------------------------------------------------------
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000179// Hash function.
180
181uint32_t ComputeIntegerHash(uint32_t key);
182
183
184// ----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185// I/O support.
186
187// Our version of printf(). Avoids compilation errors that we get
188// with standard printf when attempting to print pointers, etc.
189// (the errors are due to the extra compilation flags, which we
190// want elsewhere).
191void PrintF(const char* format, ...);
192
193// Our version of fflush.
194void Flush();
195
196
197// Read a line of characters after printing the prompt to stdout. The resulting
198// char* needs to be disposed off with DeleteArray by the caller.
199char* ReadLine(const char* prompt);
200
201
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000202// Read and return the raw bytes in a file. the size of the buffer is returned
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203// in size.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000204// The returned buffer must be freed by the caller.
205byte* ReadBytes(const char* filename, int* size, bool verbose = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
207
208// Write size chars from str to the file given by filename.
209// The file is overwritten. Returns the number of chars written.
210int WriteChars(const char* filename,
211 const char* str,
212 int size,
213 bool verbose = true);
214
215
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000216// Write size bytes to the file given by filename.
217// The file is overwritten. Returns the number of bytes written.
218int WriteBytes(const char* filename,
219 const byte* bytes,
220 int size,
221 bool verbose = true);
222
223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224// Write the C code
225// const char* <varname> = "<str>";
226// const int <varname>_len = <len>;
227// to the file given by filename. Only the first len chars are written.
228int WriteAsCFile(const char* filename, const char* varname,
229 const char* str, int size, bool verbose = true);
230
231
232// ----------------------------------------------------------------------------
233// Miscellaneous
234
235// A static resource holds a static instance that can be reserved in
236// a local scope using an instance of Access. Attempts to re-reserve
237// the instance will cause an error.
238template <typename T>
239class StaticResource {
240 public:
241 StaticResource() : is_reserved_(false) {}
242
243 private:
244 template <typename S> friend class Access;
245 T instance_;
246 bool is_reserved_;
247};
248
249
250// Locally scoped access to a static resource.
251template <typename T>
252class Access {
253 public:
254 explicit Access(StaticResource<T>* resource)
255 : resource_(resource)
256 , instance_(&resource->instance_) {
257 ASSERT(!resource->is_reserved_);
258 resource->is_reserved_ = true;
259 }
260
261 ~Access() {
262 resource_->is_reserved_ = false;
263 resource_ = NULL;
264 instance_ = NULL;
265 }
266
267 T* value() { return instance_; }
268 T* operator -> () { return instance_; }
269
270 private:
271 StaticResource<T>* resource_;
272 T* instance_;
273};
274
275
276template <typename T>
277class Vector {
278 public:
kasper.lund7276f142008-07-30 08:49:36 +0000279 Vector() : start_(NULL), length_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 Vector(T* data, int length) : start_(data), length_(length) {
281 ASSERT(length == 0 || (length > 0 && data != NULL));
282 }
283
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000284 static Vector<T> New(int length) {
285 return Vector<T>(NewArray<T>(length), length);
286 }
287
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000288 // Returns a vector using the same backing storage as this one,
289 // spanning from and including 'from', to but not including 'to'.
290 Vector<T> SubVector(int from, int to) {
291 ASSERT(from < length_);
292 ASSERT(to <= length_);
293 ASSERT(from < to);
294 return Vector<T>(start() + from, to - from);
295 }
296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 // Returns the length of the vector.
298 int length() const { return length_; }
299
300 // Returns whether or not the vector is empty.
301 bool is_empty() const { return length_ == 0; }
302
303 // Returns the pointer to the start of the data in the vector.
304 T* start() const { return start_; }
305
306 // Access individual vector elements - checks bounds in debug mode.
307 T& operator[](int index) const {
308 ASSERT(0 <= index && index < length_);
309 return start_[index];
310 }
311
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000312 T& first() { return start_[0]; }
313
314 T& last() { return start_[length_ - 1]; }
315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 // Returns a clone of this vector with a new backing store.
317 Vector<T> Clone() const {
318 T* result = NewArray<T>(length_);
319 for (int i = 0; i < length_; i++) result[i] = start_[i];
320 return Vector<T>(result, length_);
321 }
322
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000323 void Sort(int (*cmp)(const T*, const T*)) {
324 typedef int (*RawComparer)(const void*, const void*);
325 qsort(start(),
326 length(),
327 sizeof(T),
328 reinterpret_cast<RawComparer>(cmp));
329 }
330
331 void Sort() {
332 Sort(PointerValueCompare<T>);
333 }
334
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000335 void Truncate(int length) {
336 ASSERT(length <= length_);
337 length_ = length;
338 }
339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 // Releases the array underlying this vector. Once disposed the
341 // vector is empty.
342 void Dispose() {
kasper.lund7276f142008-07-30 08:49:36 +0000343 if (is_empty()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 DeleteArray(start_);
345 start_ = NULL;
346 length_ = 0;
347 }
348
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000349 inline Vector<T> operator+(int offset) {
350 ASSERT(offset < length_);
351 return Vector<T>(start_ + offset, length_ - offset);
352 }
353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 // Factory method for creating empty vectors.
355 static Vector<T> empty() { return Vector<T>(NULL, 0); }
356
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000357 protected:
358 void set_start(T* start) { start_ = start; }
359
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 private:
361 T* start_;
362 int length_;
363};
364
365
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000366// A temporary assignment sets a (non-local) variable to a value on
367// construction and resets it the value on destruction.
368template <typename T>
369class TempAssign {
370 public:
371 TempAssign(T* var, T value): var_(var), old_value_(*var) {
372 *var = value;
373 }
374
375 ~TempAssign() { *var_ = old_value_; }
376
377 private:
378 T* var_;
379 T old_value_;
380};
381
382
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000383template <typename T, int kSize>
384class EmbeddedVector : public Vector<T> {
385 public:
386 EmbeddedVector() : Vector<T>(buffer_, kSize) { }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000387
388 // When copying, make underlying Vector to reference our buffer.
389 EmbeddedVector(const EmbeddedVector& rhs)
390 : Vector<T>(rhs) {
391 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
392 set_start(buffer_);
393 }
394
395 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
396 if (this == &rhs) return *this;
397 Vector<T>::operator=(rhs);
398 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
399 set_start(buffer_);
400 return *this;
401 }
402
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000403 private:
404 T buffer_[kSize];
405};
406
407
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000408template <typename T>
409class ScopedVector : public Vector<T> {
410 public:
411 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
412 ~ScopedVector() {
413 DeleteArray(this->start());
414 }
415};
416
417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418inline Vector<const char> CStrVector(const char* data) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000419 return Vector<const char>(data, StrLength(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420}
421
422inline Vector<char> MutableCStrVector(char* data) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000423 return Vector<char>(data, StrLength(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424}
425
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000426inline Vector<char> MutableCStrVector(char* data, int max) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000427 int length = StrLength(data);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000428 return Vector<char>(data, (length < max) ? length : max);
429}
430
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431template <typename T>
432inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms,
433 int length) {
434 return Vector< Handle<Object> >(
435 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length);
436}
437
438
439// Simple support to read a file into a 0-terminated C-string.
440// The returned buffer must be freed by the caller.
ager@chromium.org32912102009-01-16 10:38:43 +0000441// On return, *exits tells whether the file existed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442Vector<const char> ReadFile(const char* filename,
443 bool* exists,
444 bool verbose = true);
445
446
447// Simple wrapper that allows an ExternalString to refer to a
448// Vector<const char>. Doesn't assume ownership of the data.
449class AsciiStringAdapter: public v8::String::ExternalAsciiStringResource {
450 public:
451 explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {}
452
453 virtual const char* data() const { return data_.start(); }
454
455 virtual size_t length() const { return data_.length(); }
456
457 private:
458 Vector<const char> data_;
459};
460
461
kasper.lund7276f142008-07-30 08:49:36 +0000462// Helper class for building result strings in a character buffer. The
463// purpose of the class is to use safe operations that checks the
464// buffer bounds on all operations in debug mode.
465class StringBuilder {
466 public:
467 // Create a string builder with a buffer of the given size. The
468 // buffer is allocated through NewArray<char> and must be
469 // deallocated by the caller of Finalize().
470 explicit StringBuilder(int size);
471
472 StringBuilder(char* buffer, int size)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000473 : buffer_(buffer, size), position_(0) { }
kasper.lund7276f142008-07-30 08:49:36 +0000474
475 ~StringBuilder() { if (!is_finalized()) Finalize(); }
476
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000477 int size() const { return buffer_.length(); }
kasper.lund7276f142008-07-30 08:49:36 +0000478
479 // Get the current position in the builder.
480 int position() const {
481 ASSERT(!is_finalized());
482 return position_;
483 }
484
485 // Reset the position.
486 void Reset() { position_ = 0; }
487
488 // Add a single character to the builder. It is not allowed to add
489 // 0-characters; use the Finalize() method to terminate the string
490 // instead.
491 void AddCharacter(char c) {
492 ASSERT(c != '\0');
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000493 ASSERT(!is_finalized() && position_ < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +0000494 buffer_[position_++] = c;
495 }
496
497 // Add an entire string to the builder. Uses strlen() internally to
498 // compute the length of the input string.
499 void AddString(const char* s);
500
501 // Add the first 'n' characters of the given string 's' to the
502 // builder. The input string must have enough characters.
503 void AddSubstring(const char* s, int n);
504
505 // Add formatted contents to the builder just like printf().
506 void AddFormatted(const char* format, ...);
507
508 // Add character padding to the builder. If count is non-positive,
509 // nothing is added to the builder.
510 void AddPadding(char c, int count);
511
512 // Finalize the string by 0-terminating it and returning the buffer.
513 char* Finalize();
514
515 private:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000516 Vector<char> buffer_;
kasper.lund7276f142008-07-30 08:49:36 +0000517 int position_;
518
519 bool is_finalized() const { return position_ < 0; }
mads.s.ager31e71382008-08-13 09:32:07 +0000520
521 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
kasper.lund7276f142008-07-30 08:49:36 +0000522};
523
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000524
525// Copy from ASCII/16bit chars to ASCII/16bit chars.
526template <typename sourcechar, typename sinkchar>
527static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) {
528 sinkchar* limit = dest + chars;
ager@chromium.org9085a012009-05-11 19:22:57 +0000529#ifdef V8_HOST_CAN_READ_UNALIGNED
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000530 if (sizeof(*dest) == sizeof(*src)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000531 // Number of characters in a uintptr_t.
532 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000533 while (dest <= limit - kStepSize) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000534 *reinterpret_cast<uintptr_t*>(dest) =
535 *reinterpret_cast<const uintptr_t*>(src);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000536 dest += kStepSize;
537 src += kStepSize;
538 }
539 }
540#endif
541 while (dest < limit) {
542 *dest++ = static_cast<sinkchar>(*src++);
543 }
544}
545
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000546
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000547// Compare ASCII/16bit chars to ASCII/16bit chars.
548template <typename lchar, typename rchar>
549static inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) {
550 const lchar* limit = lhs + chars;
551#ifdef V8_HOST_CAN_READ_UNALIGNED
552 if (sizeof(*lhs) == sizeof(*rhs)) {
553 // Number of characters in a uintptr_t.
554 static const int kStepSize = sizeof(uintptr_t) / sizeof(*lhs); // NOLINT
555 while (lhs <= limit - kStepSize) {
556 if (*reinterpret_cast<const uintptr_t*>(lhs) !=
557 *reinterpret_cast<const uintptr_t*>(rhs)) {
558 break;
559 }
560 lhs += kStepSize;
561 rhs += kStepSize;
562 }
563 }
564#endif
565 while (lhs < limit) {
566 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs);
567 if (r != 0) return r;
568 ++lhs;
569 ++rhs;
570 }
571 return 0;
572}
573
574
575template <typename T>
576static inline void MemsetPointer(T** dest, T* value, int counter) {
577#if defined(V8_HOST_ARCH_IA32)
578#define STOS "stosl"
579#elif defined(V8_HOST_ARCH_X64)
580#define STOS "stosq"
581#endif
582
583#if defined(__GNUC__) && defined(STOS)
584 asm("cld;"
585 "rep ; " STOS
586 : /* no output */
587 : "c" (counter), "a" (value), "D" (dest)
588 : /* no clobbered list as all inputs are considered clobbered */);
589#else
590 for (int i = 0; i < counter; i++) {
591 dest[i] = value;
592 }
593#endif
594
595#undef STOS
596}
597
598
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000599// Calculate 10^exponent.
600int TenToThe(int exponent);
601
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602} } // namespace v8::internal
603
604#endif // V8_UTILS_H_