jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +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_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 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 60 | // Prepends the current process ID to the output. |
| 61 | void PRINTF_CHECKING PrintPID(const char* format, ...); |
| 62 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 63 | // Our version of fflush. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 64 | void Flush(FILE* out); |
| 65 | |
| 66 | inline void Flush() { |
| 67 | Flush(stdout); |
| 68 | } |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | // Read a line of characters after printing the prompt to stdout. The resulting |
| 72 | // char* needs to be disposed off with DeleteArray by the caller. |
| 73 | char* ReadLine(const char* prompt); |
| 74 | |
| 75 | |
| 76 | // Read and return the raw bytes in a file. the size of the buffer is returned |
| 77 | // in size. |
| 78 | // The returned buffer must be freed by the caller. |
| 79 | byte* ReadBytes(const char* filename, int* size, bool verbose = true); |
| 80 | |
| 81 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 82 | // Append size chars from str to the file given by filename. |
| 83 | // The file is overwritten. Returns the number of chars written. |
| 84 | int AppendChars(const char* filename, |
| 85 | const char* str, |
| 86 | int size, |
| 87 | bool verbose = true); |
| 88 | |
| 89 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 90 | // Write size chars from str to the file given by filename. |
| 91 | // The file is overwritten. Returns the number of chars written. |
| 92 | int WriteChars(const char* filename, |
| 93 | const char* str, |
| 94 | int size, |
| 95 | bool verbose = true); |
| 96 | |
| 97 | |
| 98 | // Write size bytes to the file given by filename. |
| 99 | // The file is overwritten. Returns the number of bytes written. |
| 100 | int WriteBytes(const char* filename, |
| 101 | const byte* bytes, |
| 102 | int size, |
| 103 | bool verbose = true); |
| 104 | |
| 105 | |
| 106 | // Write the C code |
| 107 | // const char* <varname> = "<str>"; |
| 108 | // const int <varname>_len = <len>; |
| 109 | // to the file given by filename. Only the first len chars are written. |
| 110 | int WriteAsCFile(const char* filename, const char* varname, |
| 111 | const char* str, int size, bool verbose = true); |
| 112 | |
| 113 | |
| 114 | // Data structures |
| 115 | |
| 116 | template <typename T> |
| 117 | inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, |
| 118 | int length) { |
| 119 | return Vector< Handle<Object> >( |
| 120 | reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); |
| 121 | } |
| 122 | |
| 123 | // Memory |
| 124 | |
| 125 | // Copies data from |src| to |dst|. The data spans MUST not overlap. |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 126 | template <typename T> |
| 127 | inline void CopyWords(T* dst, T* src, int num_words) { |
| 128 | STATIC_ASSERT(sizeof(T) == kPointerSize); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 129 | ASSERT(Min(dst, src) + num_words <= Max(dst, src)); |
| 130 | ASSERT(num_words > 0); |
| 131 | |
| 132 | // Use block copying memcpy if the segment we're copying is |
| 133 | // enough to justify the extra call/setup overhead. |
| 134 | static const int kBlockCopyLimit = 16; |
| 135 | |
| 136 | if (num_words >= kBlockCopyLimit) { |
| 137 | memcpy(dst, src, num_words * kPointerSize); |
| 138 | } else { |
| 139 | int remaining = num_words; |
| 140 | do { |
| 141 | remaining--; |
| 142 | *dst++ = *src++; |
| 143 | } while (remaining > 0); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 148 | template <typename T, typename U> |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 149 | inline void MemsetPointer(T** dest, U* value, int counter) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 150 | #ifdef DEBUG |
| 151 | T* a = NULL; |
| 152 | U* b = NULL; |
| 153 | a = b; // Fake assignment to check assignability. |
| 154 | USE(a); |
| 155 | #endif // DEBUG |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 156 | #if defined(V8_HOST_ARCH_IA32) |
| 157 | #define STOS "stosl" |
| 158 | #elif defined(V8_HOST_ARCH_X64) |
| 159 | #define STOS "stosq" |
| 160 | #endif |
| 161 | |
| 162 | #if defined(__GNUC__) && defined(STOS) |
| 163 | asm volatile( |
| 164 | "cld;" |
| 165 | "rep ; " STOS |
| 166 | : "+&c" (counter), "+&D" (dest) |
| 167 | : "a" (value) |
| 168 | : "memory", "cc"); |
| 169 | #else |
| 170 | for (int i = 0; i < counter; i++) { |
| 171 | dest[i] = value; |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #undef STOS |
| 176 | } |
| 177 | |
| 178 | |
| 179 | // Simple wrapper that allows an ExternalString to refer to a |
| 180 | // Vector<const char>. Doesn't assume ownership of the data. |
| 181 | class AsciiStringAdapter: public v8::String::ExternalAsciiStringResource { |
| 182 | public: |
| 183 | explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {} |
| 184 | |
| 185 | virtual const char* data() const { return data_.start(); } |
| 186 | |
| 187 | virtual size_t length() const { return data_.length(); } |
| 188 | |
| 189 | private: |
| 190 | Vector<const char> data_; |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | // Simple support to read a file into a 0-terminated C-string. |
| 195 | // The returned buffer must be freed by the caller. |
| 196 | // On return, *exits tells whether the file existed. |
| 197 | Vector<const char> ReadFile(const char* filename, |
| 198 | bool* exists, |
| 199 | bool verbose = true); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 200 | Vector<const char> ReadFile(FILE* file, |
| 201 | bool* exists, |
| 202 | bool verbose = true); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 203 | |
| 204 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 205 | template <typename sourcechar, typename sinkchar> |
| 206 | INLINE(static void CopyCharsUnsigned(sinkchar* dest, |
| 207 | const sourcechar* src, |
| 208 | int chars)); |
| 209 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 210 | // Copy from ASCII/16bit chars to ASCII/16bit chars. |
| 211 | template <typename sourcechar, typename sinkchar> |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 212 | INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, int chars)); |
| 213 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 214 | template<typename sourcechar, typename sinkchar> |
| 215 | void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { |
| 216 | ASSERT(sizeof(sourcechar) <= 2); |
| 217 | ASSERT(sizeof(sinkchar) <= 2); |
| 218 | if (sizeof(sinkchar) == 1) { |
| 219 | if (sizeof(sourcechar) == 1) { |
| 220 | CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest), |
| 221 | reinterpret_cast<const uint8_t*>(src), |
| 222 | chars); |
| 223 | } else { |
| 224 | CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest), |
| 225 | reinterpret_cast<const uint16_t*>(src), |
| 226 | chars); |
| 227 | } |
| 228 | } else { |
| 229 | if (sizeof(sourcechar) == 1) { |
| 230 | CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest), |
| 231 | reinterpret_cast<const uint8_t*>(src), |
| 232 | chars); |
| 233 | } else { |
| 234 | CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest), |
| 235 | reinterpret_cast<const uint16_t*>(src), |
| 236 | chars); |
| 237 | } |
| 238 | } |
| 239 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 240 | |
| 241 | template <typename sourcechar, typename sinkchar> |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 242 | void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, int chars) { |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 243 | sinkchar* limit = dest + chars; |
| 244 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 245 | if (sizeof(*dest) == sizeof(*src)) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 246 | if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) { |
| 247 | OS::MemCopy(dest, src, chars * sizeof(*dest)); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | // Number of characters in a uintptr_t. |
| 251 | static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 252 | ASSERT(dest + kStepSize > dest); // Check for overflow. |
| 253 | while (dest + kStepSize <= limit) { |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 254 | *reinterpret_cast<uintptr_t*>(dest) = |
| 255 | *reinterpret_cast<const uintptr_t*>(src); |
| 256 | dest += kStepSize; |
| 257 | src += kStepSize; |
| 258 | } |
| 259 | } |
| 260 | #endif |
| 261 | while (dest < limit) { |
| 262 | *dest++ = static_cast<sinkchar>(*src++); |
| 263 | } |
| 264 | } |
| 265 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 266 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 267 | class StringBuilder : public SimpleStringBuilder { |
| 268 | public: |
| 269 | explicit StringBuilder(int size) : SimpleStringBuilder(size) { } |
| 270 | StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { } |
| 271 | |
| 272 | // Add formatted contents to the builder just like printf(). |
| 273 | void AddFormatted(const char* format, ...); |
| 274 | |
| 275 | // Add formatted contents like printf based on a va_list. |
| 276 | void AddFormattedList(const char* format, va_list list); |
| 277 | private: |
| 278 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
| 279 | }; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 280 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 281 | } } // namespace v8::internal |
| 282 | |
| 283 | #endif // V8_V8UTILS_H_ |