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