ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame^] | 32 | #include <string.h> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // General helper functions |
| 39 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 40 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | template <typename T> |
| 43 | static inline bool IsPowerOf2(T x) { |
| 44 | return (x & (x - 1)) == 0; |
| 45 | } |
| 46 | |
| 47 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 48 | // The C++ standard leaves the semantics of '>>' undefined for |
| 49 | // negative signed operands. Most implementations do the right thing, |
| 50 | // though. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | static 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.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 57 | // This allows conversion of Addresses and integral types into |
| 58 | // 0-relative int offsets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | template <typename T> |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 60 | static inline intptr_t OffsetFrom(T x) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | 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.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 66 | // This allows conversion of 0-relative int offsets into Addresses and |
| 67 | // integral types. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | template <typename T> |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 69 | static inline T AddressFrom(intptr_t x) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 70 | return static_cast<T>(static_cast<T>(0) + x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | // Return the largest multiple of m which is <= x. |
| 75 | template <typename T> |
| 76 | static 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. |
| 83 | template <typename T> |
| 84 | static inline T RoundUp(T x, int m) { |
| 85 | return RoundDown(x + m - 1, m); |
| 86 | } |
| 87 | |
| 88 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 89 | template <typename T> |
| 90 | static 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 | |
| 100 | template <typename T> |
| 101 | static int PointerValueCompare(const T* a, const T* b) { |
| 102 | return Compare<T>(*a, *b); |
| 103 | } |
| 104 | |
| 105 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 106 | // 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. |
| 108 | uint32_t RoundUpToPowerOf2(uint32_t x); |
| 109 | |
| 110 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | template <typename T> |
| 112 | static 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.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 119 | static inline bool IsAddressAligned(Address addr, |
| 120 | intptr_t alignment, |
| 121 | int offset) { |
| 122 | intptr_t offs = OffsetFrom(addr + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | return IsAligned(offs, alignment); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | // Returns the maximum of the two parameters. |
| 128 | template <typename T> |
| 129 | static T Max(T a, T b) { |
| 130 | return a < b ? b : a; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | // Returns the minimum of the two parameters. |
| 135 | template <typename T> |
| 136 | static T Min(T a, T b) { |
| 137 | return a < b ? a : b; |
| 138 | } |
| 139 | |
| 140 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 141 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | // ---------------------------------------------------------------------------- |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 149 | // BitField is a help template for encoding and decode bitfield with |
| 150 | // unsigned content. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | template<class T, int shift, int size> |
| 152 | class 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.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 161 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | } |
| 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.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 174 | return static_cast<T>((value & mask()) >> shift); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | } |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | // ---------------------------------------------------------------------------- |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 180 | // Hash function. |
| 181 | |
| 182 | uint32_t ComputeIntegerHash(uint32_t key); |
| 183 | |
| 184 | |
| 185 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | // 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). |
| 192 | void PrintF(const char* format, ...); |
| 193 | |
| 194 | // Our version of fflush. |
| 195 | void 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. |
| 200 | char* ReadLine(const char* prompt); |
| 201 | |
| 202 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 203 | // Read and return the raw bytes in a file. the size of the buffer is returned |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | // in size. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 205 | // The returned buffer must be freed by the caller. |
| 206 | byte* ReadBytes(const char* filename, int* size, bool verbose = true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | |
| 208 | |
| 209 | // Write size chars from str to the file given by filename. |
| 210 | // The file is overwritten. Returns the number of chars written. |
| 211 | int WriteChars(const char* filename, |
| 212 | const char* str, |
| 213 | int size, |
| 214 | bool verbose = true); |
| 215 | |
| 216 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 217 | // Write size bytes to the file given by filename. |
| 218 | // The file is overwritten. Returns the number of bytes written. |
| 219 | int WriteBytes(const char* filename, |
| 220 | const byte* bytes, |
| 221 | int size, |
| 222 | bool verbose = true); |
| 223 | |
| 224 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | // 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. |
| 229 | int 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. |
| 239 | template <typename T> |
| 240 | class 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. |
| 252 | template <typename T> |
| 253 | class 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 | |
| 277 | template <typename T> |
| 278 | class Vector { |
| 279 | public: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 280 | Vector() : start_(NULL), length_(0) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | Vector(T* data, int length) : start_(data), length_(length) { |
| 282 | ASSERT(length == 0 || (length > 0 && data != NULL)); |
| 283 | } |
| 284 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 285 | static Vector<T> New(int length) { |
| 286 | return Vector<T>(NewArray<T>(length), length); |
| 287 | } |
| 288 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 289 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | // 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.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 313 | T& first() { return start_[0]; } |
| 314 | |
| 315 | T& last() { return start_[length_ - 1]; } |
| 316 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 317 | // 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.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 324 | 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.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 336 | void Truncate(int length) { |
| 337 | ASSERT(length <= length_); |
| 338 | length_ = length; |
| 339 | } |
| 340 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | // Releases the array underlying this vector. Once disposed the |
| 342 | // vector is empty. |
| 343 | void Dispose() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 344 | if (is_empty()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | DeleteArray(start_); |
| 346 | start_ = NULL; |
| 347 | length_ = 0; |
| 348 | } |
| 349 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 350 | inline Vector<T> operator+(int offset) { |
| 351 | ASSERT(offset < length_); |
| 352 | return Vector<T>(start_ + offset, length_ - offset); |
| 353 | } |
| 354 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | // Factory method for creating empty vectors. |
| 356 | static Vector<T> empty() { return Vector<T>(NULL, 0); } |
| 357 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 358 | protected: |
| 359 | void set_start(T* start) { start_ = start; } |
| 360 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | private: |
| 362 | T* start_; |
| 363 | int length_; |
| 364 | }; |
| 365 | |
| 366 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 367 | // A temporary assignment sets a (non-local) variable to a value on |
| 368 | // construction and resets it the value on destruction. |
| 369 | template <typename T> |
| 370 | class 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 384 | template <typename T, int kSize> |
| 385 | class EmbeddedVector : public Vector<T> { |
| 386 | public: |
| 387 | EmbeddedVector() : Vector<T>(buffer_, kSize) { } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 388 | |
| 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.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame^] | 400 | this->set_start(buffer_); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 401 | return *this; |
| 402 | } |
| 403 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 404 | private: |
| 405 | T buffer_[kSize]; |
| 406 | }; |
| 407 | |
| 408 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 409 | template <typename T> |
| 410 | class 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 419 | inline Vector<const char> CStrVector(const char* data) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 420 | return Vector<const char>(data, StrLength(data)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | inline Vector<char> MutableCStrVector(char* data) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 424 | return Vector<char>(data, StrLength(data)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | } |
| 426 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 427 | inline Vector<char> MutableCStrVector(char* data, int max) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 428 | int length = StrLength(data); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 429 | return Vector<char>(data, (length < max) ? length : max); |
| 430 | } |
| 431 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | template <typename T> |
| 433 | inline 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.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 442 | // On return, *exits tells whether the file existed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | Vector<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. |
| 450 | class 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.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 463 | // 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. |
| 466 | class 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 474 | : buffer_(buffer, size), position_(0) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 475 | |
| 476 | ~StringBuilder() { if (!is_finalized()) Finalize(); } |
| 477 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 478 | int size() const { return buffer_.length(); } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 479 | |
| 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 494 | ASSERT(!is_finalized() && position_ < buffer_.length()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 495 | 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 517 | Vector<char> buffer_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 518 | int position_; |
| 519 | |
| 520 | bool is_finalized() const { return position_ < 0; } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 521 | |
| 522 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 523 | }; |
| 524 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 525 | |
| 526 | // Copy from ASCII/16bit chars to ASCII/16bit chars. |
| 527 | template <typename sourcechar, typename sinkchar> |
| 528 | static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { |
| 529 | sinkchar* limit = dest + chars; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 530 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 531 | if (sizeof(*dest) == sizeof(*src)) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 532 | // Number of characters in a uintptr_t. |
| 533 | static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 534 | while (dest <= limit - kStepSize) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 535 | *reinterpret_cast<uintptr_t*>(dest) = |
| 536 | *reinterpret_cast<const uintptr_t*>(src); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 537 | dest += kStepSize; |
| 538 | src += kStepSize; |
| 539 | } |
| 540 | } |
| 541 | #endif |
| 542 | while (dest < limit) { |
| 543 | *dest++ = static_cast<sinkchar>(*src++); |
| 544 | } |
| 545 | } |
| 546 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 547 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 548 | // Compare ASCII/16bit chars to ASCII/16bit chars. |
| 549 | template <typename lchar, typename rchar> |
| 550 | static 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 | |
| 576 | template <typename T> |
| 577 | static 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.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 600 | // Calculate 10^exponent. |
| 601 | int TenToThe(int exponent); |
| 602 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame^] | 603 | |
| 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. |
| 628 | template <class Dest, class Source> |
| 629 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | } } // namespace v8::internal |
| 641 | |
| 642 | #endif // V8_UTILS_H_ |