whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 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_V8UTILS_H_ |
| 29 | #define V8_V8UTILS_H_ |
| 30 | |
| 31 | #include "utils.h" |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame^] | 32 | #include "platform.h" // For va_list on Solaris. |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // I/O support. |
| 39 | |
| 40 | #if __GNUC__ >= 4 |
| 41 | // On gcc we can ask the compiler to check the types of %d-style format |
| 42 | // specifiers and their associated arguments. TODO(erikcorry) fix this |
| 43 | // so it works on MacOSX. |
| 44 | #if defined(__MACH__) && defined(__APPLE__) |
| 45 | #define PRINTF_CHECKING |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 46 | #define FPRINTF_CHECKING |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 47 | #else // MacOsX. |
| 48 | #define PRINTF_CHECKING __attribute__ ((format (printf, 1, 2))) |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 49 | #define FPRINTF_CHECKING __attribute__ ((format (printf, 2, 3))) |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 50 | #endif |
| 51 | #else |
| 52 | #define PRINTF_CHECKING |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 53 | #define FPRINTF_CHECKING |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | // Our version of printf(). |
| 57 | void PRINTF_CHECKING PrintF(const char* format, ...); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 58 | void FPRINTF_CHECKING PrintF(FILE* out, const char* format, ...); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 59 | |
| 60 | // Our version of fflush. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 61 | void Flush(FILE* out); |
| 62 | |
| 63 | inline void Flush() { |
| 64 | Flush(stdout); |
| 65 | } |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | // Read a line of characters after printing the prompt to stdout. The resulting |
| 69 | // char* needs to be disposed off with DeleteArray by the caller. |
| 70 | char* ReadLine(const char* prompt); |
| 71 | |
| 72 | |
| 73 | // Read and return the raw bytes in a file. the size of the buffer is returned |
| 74 | // in size. |
| 75 | // The returned buffer must be freed by the caller. |
| 76 | byte* ReadBytes(const char* filename, int* size, bool verbose = true); |
| 77 | |
| 78 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 79 | // Append size chars from str to the file given by filename. |
| 80 | // The file is overwritten. Returns the number of chars written. |
| 81 | int AppendChars(const char* filename, |
| 82 | const char* str, |
| 83 | int size, |
| 84 | bool verbose = true); |
| 85 | |
| 86 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 87 | // Write size chars from str to the file given by filename. |
| 88 | // The file is overwritten. Returns the number of chars written. |
| 89 | int WriteChars(const char* filename, |
| 90 | const char* str, |
| 91 | int size, |
| 92 | bool verbose = true); |
| 93 | |
| 94 | |
| 95 | // Write size bytes to the file given by filename. |
| 96 | // The file is overwritten. Returns the number of bytes written. |
| 97 | int WriteBytes(const char* filename, |
| 98 | const byte* bytes, |
| 99 | int size, |
| 100 | bool verbose = true); |
| 101 | |
| 102 | |
| 103 | // Write the C code |
| 104 | // const char* <varname> = "<str>"; |
| 105 | // const int <varname>_len = <len>; |
| 106 | // to the file given by filename. Only the first len chars are written. |
| 107 | int WriteAsCFile(const char* filename, const char* varname, |
| 108 | const char* str, int size, bool verbose = true); |
| 109 | |
| 110 | |
| 111 | // Data structures |
| 112 | |
| 113 | template <typename T> |
| 114 | inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, |
| 115 | int length) { |
| 116 | return Vector< Handle<Object> >( |
| 117 | reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); |
| 118 | } |
| 119 | |
| 120 | // Memory |
| 121 | |
| 122 | // Copies data from |src| to |dst|. The data spans MUST not overlap. |
| 123 | inline void CopyWords(Object** dst, Object** src, int num_words) { |
| 124 | ASSERT(Min(dst, src) + num_words <= Max(dst, src)); |
| 125 | ASSERT(num_words > 0); |
| 126 | |
| 127 | // Use block copying memcpy if the segment we're copying is |
| 128 | // enough to justify the extra call/setup overhead. |
| 129 | static const int kBlockCopyLimit = 16; |
| 130 | |
| 131 | if (num_words >= kBlockCopyLimit) { |
| 132 | memcpy(dst, src, num_words * kPointerSize); |
| 133 | } else { |
| 134 | int remaining = num_words; |
| 135 | do { |
| 136 | remaining--; |
| 137 | *dst++ = *src++; |
| 138 | } while (remaining > 0); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | template <typename T> |
| 144 | static inline void MemsetPointer(T** dest, T* value, int counter) { |
| 145 | #if defined(V8_HOST_ARCH_IA32) |
| 146 | #define STOS "stosl" |
| 147 | #elif defined(V8_HOST_ARCH_X64) |
| 148 | #define STOS "stosq" |
| 149 | #endif |
| 150 | |
| 151 | #if defined(__GNUC__) && defined(STOS) |
| 152 | asm volatile( |
| 153 | "cld;" |
| 154 | "rep ; " STOS |
| 155 | : "+&c" (counter), "+&D" (dest) |
| 156 | : "a" (value) |
| 157 | : "memory", "cc"); |
| 158 | #else |
| 159 | for (int i = 0; i < counter; i++) { |
| 160 | dest[i] = value; |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | #undef STOS |
| 165 | } |
| 166 | |
| 167 | |
| 168 | // Simple wrapper that allows an ExternalString to refer to a |
| 169 | // Vector<const char>. Doesn't assume ownership of the data. |
| 170 | class AsciiStringAdapter: public v8::String::ExternalAsciiStringResource { |
| 171 | public: |
| 172 | explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {} |
| 173 | |
| 174 | virtual const char* data() const { return data_.start(); } |
| 175 | |
| 176 | virtual size_t length() const { return data_.length(); } |
| 177 | |
| 178 | private: |
| 179 | Vector<const char> data_; |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | // Simple support to read a file into a 0-terminated C-string. |
| 184 | // The returned buffer must be freed by the caller. |
| 185 | // On return, *exits tells whether the file existed. |
| 186 | Vector<const char> ReadFile(const char* filename, |
| 187 | bool* exists, |
| 188 | bool verbose = true); |
| 189 | |
| 190 | |
| 191 | // Helper class for building result strings in a character buffer. The |
| 192 | // purpose of the class is to use safe operations that checks the |
| 193 | // buffer bounds on all operations in debug mode. |
| 194 | class StringBuilder { |
| 195 | public: |
| 196 | // Create a string builder with a buffer of the given size. The |
| 197 | // buffer is allocated through NewArray<char> and must be |
| 198 | // deallocated by the caller of Finalize(). |
| 199 | explicit StringBuilder(int size); |
| 200 | |
| 201 | StringBuilder(char* buffer, int size) |
| 202 | : buffer_(buffer, size), position_(0) { } |
| 203 | |
| 204 | ~StringBuilder() { if (!is_finalized()) Finalize(); } |
| 205 | |
| 206 | int size() const { return buffer_.length(); } |
| 207 | |
| 208 | // Get the current position in the builder. |
| 209 | int position() const { |
| 210 | ASSERT(!is_finalized()); |
| 211 | return position_; |
| 212 | } |
| 213 | |
| 214 | // Reset the position. |
| 215 | void Reset() { position_ = 0; } |
| 216 | |
| 217 | // Add a single character to the builder. It is not allowed to add |
| 218 | // 0-characters; use the Finalize() method to terminate the string |
| 219 | // instead. |
| 220 | void AddCharacter(char c) { |
| 221 | ASSERT(c != '\0'); |
| 222 | ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 223 | buffer_[position_++] = c; |
| 224 | } |
| 225 | |
| 226 | // Add an entire string to the builder. Uses strlen() internally to |
| 227 | // compute the length of the input string. |
| 228 | void AddString(const char* s); |
| 229 | |
| 230 | // Add the first 'n' characters of the given string 's' to the |
| 231 | // builder. The input string must have enough characters. |
| 232 | void AddSubstring(const char* s, int n); |
| 233 | |
| 234 | // Add formatted contents to the builder just like printf(). |
| 235 | void AddFormatted(const char* format, ...); |
| 236 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 237 | // Add formatted contents like printf based on a va_list. |
| 238 | void AddFormattedList(const char* format, va_list list); |
| 239 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 240 | // Add character padding to the builder. If count is non-positive, |
| 241 | // nothing is added to the builder. |
| 242 | void AddPadding(char c, int count); |
| 243 | |
| 244 | // Finalize the string by 0-terminating it and returning the buffer. |
| 245 | char* Finalize(); |
| 246 | |
| 247 | private: |
| 248 | Vector<char> buffer_; |
| 249 | int position_; |
| 250 | |
| 251 | bool is_finalized() const { return position_ < 0; } |
| 252 | |
| 253 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
| 254 | }; |
| 255 | |
| 256 | |
| 257 | // Custom memcpy implementation for platforms where the standard version |
| 258 | // may not be good enough. |
| 259 | #if defined(V8_TARGET_ARCH_IA32) |
| 260 | |
| 261 | // The default memcpy on ia32 architectures is generally not as efficient |
| 262 | // as possible. (If any further ia32 platforms are introduced where the |
| 263 | // memcpy function is efficient, exclude them from this branch). |
| 264 | |
| 265 | typedef void (*MemCopyFunction)(void* dest, const void* src, size_t size); |
| 266 | |
| 267 | // Implemented in codegen-<arch>.cc. |
| 268 | MemCopyFunction CreateMemCopyFunction(); |
| 269 | |
| 270 | // Copy memory area to disjoint memory area. |
| 271 | static inline void MemCopy(void* dest, const void* src, size_t size) { |
| 272 | static MemCopyFunction memcopy = CreateMemCopyFunction(); |
| 273 | (*memcopy)(dest, src, size); |
| 274 | #ifdef DEBUG |
| 275 | CHECK_EQ(0, memcmp(dest, src, size)); |
| 276 | #endif |
| 277 | } |
| 278 | |
| 279 | // Limit below which the extra overhead of the MemCopy function is likely |
| 280 | // to outweigh the benefits of faster copying. |
| 281 | static const int kMinComplexMemCopy = 64; |
| 282 | |
| 283 | #else // V8_TARGET_ARCH_IA32 |
| 284 | |
| 285 | static inline void MemCopy(void* dest, const void* src, size_t size) { |
| 286 | memcpy(dest, src, size); |
| 287 | } |
| 288 | |
| 289 | static const int kMinComplexMemCopy = 256; |
| 290 | |
| 291 | #endif // V8_TARGET_ARCH_IA32 |
| 292 | |
| 293 | |
| 294 | // Copy from ASCII/16bit chars to ASCII/16bit chars. |
| 295 | template <typename sourcechar, typename sinkchar> |
| 296 | static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { |
| 297 | sinkchar* limit = dest + chars; |
| 298 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 299 | if (sizeof(*dest) == sizeof(*src)) { |
| 300 | if (chars >= static_cast<int>(kMinComplexMemCopy / sizeof(*dest))) { |
| 301 | MemCopy(dest, src, chars * sizeof(*dest)); |
| 302 | return; |
| 303 | } |
| 304 | // Number of characters in a uintptr_t. |
| 305 | static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT |
| 306 | while (dest <= limit - kStepSize) { |
| 307 | *reinterpret_cast<uintptr_t*>(dest) = |
| 308 | *reinterpret_cast<const uintptr_t*>(src); |
| 309 | dest += kStepSize; |
| 310 | src += kStepSize; |
| 311 | } |
| 312 | } |
| 313 | #endif |
| 314 | while (dest < limit) { |
| 315 | *dest++ = static_cast<sinkchar>(*src++); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | } } // namespace v8::internal |
| 320 | |
| 321 | #endif // V8_V8UTILS_H_ |