Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #include <stdarg.h> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | |
| 8 | #include "src/v8.h" |
| 9 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 10 | #include "src/base/functional.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/base/logging.h" |
| 12 | #include "src/base/platform/platform.h" |
| 13 | #include "src/utils.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
| 18 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 19 | SimpleStringBuilder::SimpleStringBuilder(int size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | buffer_ = Vector<char>::New(size); |
| 21 | position_ = 0; |
| 22 | } |
| 23 | |
| 24 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 25 | void SimpleStringBuilder::AddString(const char* s) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 26 | AddSubstring(s, StrLength(s)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 30 | void SimpleStringBuilder::AddSubstring(const char* s, int n) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | DCHECK(!is_finalized() && position_ + n <= buffer_.length()); |
| 32 | DCHECK(static_cast<size_t>(n) <= strlen(s)); |
| 33 | MemCopy(&buffer_[position_], s, n * kCharSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | position_ += n; |
| 35 | } |
| 36 | |
| 37 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 38 | void SimpleStringBuilder::AddPadding(char c, int count) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | for (int i = 0; i < count; i++) { |
| 40 | AddCharacter(c); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 45 | void SimpleStringBuilder::AddDecimalInteger(int32_t value) { |
| 46 | uint32_t number = static_cast<uint32_t>(value); |
| 47 | if (value < 0) { |
| 48 | AddCharacter('-'); |
| 49 | number = static_cast<uint32_t>(-value); |
| 50 | } |
| 51 | int digits = 1; |
| 52 | for (uint32_t factor = 10; digits < 10; digits++, factor *= 10) { |
| 53 | if (factor > number) break; |
| 54 | } |
| 55 | position_ += digits; |
| 56 | for (int i = 1; i <= digits; i++) { |
| 57 | buffer_[position_ - i] = '0' + static_cast<char>(number % 10); |
| 58 | number /= 10; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | char* SimpleStringBuilder::Finalize() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | DCHECK(!is_finalized() && position_ <= buffer_.length()); |
| 65 | // If there is no space for null termination, overwrite last character. |
| 66 | if (position_ == buffer_.length()) { |
| 67 | position_--; |
| 68 | // Print ellipsis. |
| 69 | for (int i = 3; i > 0 && position_ > i; --i) buffer_[position_ - i] = '.'; |
| 70 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | buffer_[position_] = '\0'; |
| 72 | // Make sure nobody managed to add a 0-character to the |
| 73 | // buffer while building the string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | DCHECK(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 75 | position_ = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | DCHECK(is_finalized()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | return buffer_.start(); |
| 78 | } |
| 79 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 81 | size_t hash_value(BailoutId id) { |
| 82 | base::hash<int> h; |
| 83 | return h(id.id_); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | std::ostream& operator<<(std::ostream& os, BailoutId id) { |
| 88 | return os << id.id_; |
| 89 | } |
| 90 | |
| 91 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | void PrintF(const char* format, ...) { |
| 93 | va_list arguments; |
| 94 | va_start(arguments, format); |
| 95 | base::OS::VPrint(format, arguments); |
| 96 | va_end(arguments); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void PrintF(FILE* out, const char* format, ...) { |
| 101 | va_list arguments; |
| 102 | va_start(arguments, format); |
| 103 | base::OS::VFPrint(out, format, arguments); |
| 104 | va_end(arguments); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void PrintPID(const char* format, ...) { |
| 109 | base::OS::Print("[%d] ", base::OS::GetCurrentProcessId()); |
| 110 | va_list arguments; |
| 111 | va_start(arguments, format); |
| 112 | base::OS::VPrint(format, arguments); |
| 113 | va_end(arguments); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | int SNPrintF(Vector<char> str, const char* format, ...) { |
| 118 | va_list args; |
| 119 | va_start(args, format); |
| 120 | int result = VSNPrintF(str, format, args); |
| 121 | va_end(args); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | int VSNPrintF(Vector<char> str, const char* format, va_list args) { |
| 127 | return base::OS::VSNPrintF(str.start(), str.length(), format, args); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void StrNCpy(Vector<char> dest, const char* src, size_t n) { |
| 132 | base::OS::StrNCpy(dest.start(), dest.length(), src, n); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void Flush(FILE* out) { |
| 137 | fflush(out); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | char* ReadLine(const char* prompt) { |
| 142 | char* result = NULL; |
| 143 | char line_buf[256]; |
| 144 | int offset = 0; |
| 145 | bool keep_going = true; |
| 146 | fprintf(stdout, "%s", prompt); |
| 147 | fflush(stdout); |
| 148 | while (keep_going) { |
| 149 | if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) { |
| 150 | // fgets got an error. Just give up. |
| 151 | if (result != NULL) { |
| 152 | DeleteArray(result); |
| 153 | } |
| 154 | return NULL; |
| 155 | } |
| 156 | int len = StrLength(line_buf); |
| 157 | if (len > 1 && |
| 158 | line_buf[len - 2] == '\\' && |
| 159 | line_buf[len - 1] == '\n') { |
| 160 | // When we read a line that ends with a "\" we remove the escape and |
| 161 | // append the remainder. |
| 162 | line_buf[len - 2] = '\n'; |
| 163 | line_buf[len - 1] = 0; |
| 164 | len -= 1; |
| 165 | } else if ((len > 0) && (line_buf[len - 1] == '\n')) { |
| 166 | // Since we read a new line we are done reading the line. This |
| 167 | // will exit the loop after copying this buffer into the result. |
| 168 | keep_going = false; |
| 169 | } |
| 170 | if (result == NULL) { |
| 171 | // Allocate the initial result and make room for the terminating '\0' |
| 172 | result = NewArray<char>(len + 1); |
| 173 | } else { |
| 174 | // Allocate a new result with enough room for the new addition. |
| 175 | int new_len = offset + len + 1; |
| 176 | char* new_result = NewArray<char>(new_len); |
| 177 | // Copy the existing input into the new array and set the new |
| 178 | // array as the result. |
| 179 | MemCopy(new_result, result, offset * kCharSize); |
| 180 | DeleteArray(result); |
| 181 | result = new_result; |
| 182 | } |
| 183 | // Copy the newly read line into the result. |
| 184 | MemCopy(result + offset, line_buf, len * kCharSize); |
| 185 | offset += len; |
| 186 | } |
| 187 | DCHECK(result != NULL); |
| 188 | result[offset] = '\0'; |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | char* ReadCharsFromFile(FILE* file, |
| 194 | int* size, |
| 195 | int extra_space, |
| 196 | bool verbose, |
| 197 | const char* filename) { |
| 198 | if (file == NULL || fseek(file, 0, SEEK_END) != 0) { |
| 199 | if (verbose) { |
| 200 | base::OS::PrintError("Cannot read from file %s.\n", filename); |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | // Get the size of the file and rewind it. |
| 206 | *size = ftell(file); |
| 207 | rewind(file); |
| 208 | |
| 209 | char* result = NewArray<char>(*size + extra_space); |
| 210 | for (int i = 0; i < *size && feof(file) == 0;) { |
| 211 | int read = static_cast<int>(fread(&result[i], 1, *size - i, file)); |
| 212 | if (read != (*size - i) && ferror(file) != 0) { |
| 213 | fclose(file); |
| 214 | DeleteArray(result); |
| 215 | return NULL; |
| 216 | } |
| 217 | i += read; |
| 218 | } |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | char* ReadCharsFromFile(const char* filename, |
| 224 | int* size, |
| 225 | int extra_space, |
| 226 | bool verbose) { |
| 227 | FILE* file = base::OS::FOpen(filename, "rb"); |
| 228 | char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename); |
| 229 | if (file != NULL) fclose(file); |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | byte* ReadBytes(const char* filename, int* size, bool verbose) { |
| 235 | char* chars = ReadCharsFromFile(filename, size, 0, verbose); |
| 236 | return reinterpret_cast<byte*>(chars); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | static Vector<const char> SetVectorContents(char* chars, |
| 241 | int size, |
| 242 | bool* exists) { |
| 243 | if (!chars) { |
| 244 | *exists = false; |
| 245 | return Vector<const char>::empty(); |
| 246 | } |
| 247 | chars[size] = '\0'; |
| 248 | *exists = true; |
| 249 | return Vector<const char>(chars, size); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | Vector<const char> ReadFile(const char* filename, |
| 254 | bool* exists, |
| 255 | bool verbose) { |
| 256 | int size; |
| 257 | char* result = ReadCharsFromFile(filename, &size, 1, verbose); |
| 258 | return SetVectorContents(result, size, exists); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | Vector<const char> ReadFile(FILE* file, |
| 263 | bool* exists, |
| 264 | bool verbose) { |
| 265 | int size; |
| 266 | char* result = ReadCharsFromFile(file, &size, 1, verbose, ""); |
| 267 | return SetVectorContents(result, size, exists); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | int WriteCharsToFile(const char* str, int size, FILE* f) { |
| 272 | int total = 0; |
| 273 | while (total < size) { |
| 274 | int write = static_cast<int>(fwrite(str, 1, size - total, f)); |
| 275 | if (write == 0) { |
| 276 | return total; |
| 277 | } |
| 278 | total += write; |
| 279 | str += write; |
| 280 | } |
| 281 | return total; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | int AppendChars(const char* filename, |
| 286 | const char* str, |
| 287 | int size, |
| 288 | bool verbose) { |
| 289 | FILE* f = base::OS::FOpen(filename, "ab"); |
| 290 | if (f == NULL) { |
| 291 | if (verbose) { |
| 292 | base::OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | int written = WriteCharsToFile(str, size, f); |
| 297 | fclose(f); |
| 298 | return written; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | int WriteChars(const char* filename, |
| 303 | const char* str, |
| 304 | int size, |
| 305 | bool verbose) { |
| 306 | FILE* f = base::OS::FOpen(filename, "wb"); |
| 307 | if (f == NULL) { |
| 308 | if (verbose) { |
| 309 | base::OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | int written = WriteCharsToFile(str, size, f); |
| 314 | fclose(f); |
| 315 | return written; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | int WriteBytes(const char* filename, |
| 320 | const byte* bytes, |
| 321 | int size, |
| 322 | bool verbose) { |
| 323 | const char* str = reinterpret_cast<const char*>(bytes); |
| 324 | return WriteChars(filename, str, size, verbose); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | void StringBuilder::AddFormatted(const char* format, ...) { |
| 330 | va_list arguments; |
| 331 | va_start(arguments, format); |
| 332 | AddFormattedList(format, arguments); |
| 333 | va_end(arguments); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void StringBuilder::AddFormattedList(const char* format, va_list list) { |
| 338 | DCHECK(!is_finalized() && position_ <= buffer_.length()); |
| 339 | int n = VSNPrintF(buffer_ + position_, format, list); |
| 340 | if (n < 0 || n >= (buffer_.length() - position_)) { |
| 341 | position_ = buffer_.length(); |
| 342 | } else { |
| 343 | position_ += n; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87 |
| 349 | static void MemMoveWrapper(void* dest, const void* src, size_t size) { |
| 350 | memmove(dest, src, size); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | // Initialize to library version so we can call this at any time during startup. |
| 355 | static MemMoveFunction memmove_function = &MemMoveWrapper; |
| 356 | |
| 357 | // Defined in codegen-ia32.cc. |
| 358 | MemMoveFunction CreateMemMoveFunction(); |
| 359 | |
| 360 | // Copy memory area to disjoint memory area. |
| 361 | void MemMove(void* dest, const void* src, size_t size) { |
| 362 | if (size == 0) return; |
| 363 | // Note: here we rely on dependent reads being ordered. This is true |
| 364 | // on all architectures we currently support. |
| 365 | (*memmove_function)(dest, src, size); |
| 366 | } |
| 367 | |
| 368 | #elif V8_OS_POSIX && V8_HOST_ARCH_ARM |
| 369 | void MemCopyUint16Uint8Wrapper(uint16_t* dest, const uint8_t* src, |
| 370 | size_t chars) { |
| 371 | uint16_t* limit = dest + chars; |
| 372 | while (dest < limit) { |
| 373 | *dest++ = static_cast<uint16_t>(*src++); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
| 378 | MemCopyUint8Function memcopy_uint8_function = &MemCopyUint8Wrapper; |
| 379 | MemCopyUint16Uint8Function memcopy_uint16_uint8_function = |
| 380 | &MemCopyUint16Uint8Wrapper; |
| 381 | // Defined in codegen-arm.cc. |
| 382 | MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub); |
| 383 | MemCopyUint16Uint8Function CreateMemCopyUint16Uint8Function( |
| 384 | MemCopyUint16Uint8Function stub); |
| 385 | |
| 386 | #elif V8_OS_POSIX && V8_HOST_ARCH_MIPS |
| 387 | MemCopyUint8Function memcopy_uint8_function = &MemCopyUint8Wrapper; |
| 388 | // Defined in codegen-mips.cc. |
| 389 | MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub); |
| 390 | #endif |
| 391 | |
| 392 | |
| 393 | void init_memcopy_functions() { |
| 394 | #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87 |
| 395 | MemMoveFunction generated_memmove = CreateMemMoveFunction(); |
| 396 | if (generated_memmove != NULL) { |
| 397 | memmove_function = generated_memmove; |
| 398 | } |
| 399 | #elif V8_OS_POSIX && V8_HOST_ARCH_ARM |
| 400 | memcopy_uint8_function = CreateMemCopyUint8Function(&MemCopyUint8Wrapper); |
| 401 | memcopy_uint16_uint8_function = |
| 402 | CreateMemCopyUint16Uint8Function(&MemCopyUint16Uint8Wrapper); |
| 403 | #elif V8_OS_POSIX && V8_HOST_ARCH_MIPS |
| 404 | memcopy_uint8_function = CreateMemCopyUint8Function(&MemCopyUint8Wrapper); |
| 405 | #endif |
| 406 | } |
| 407 | |
| 408 | |
| 409 | bool DoubleToBoolean(double d) { |
| 410 | // NaN, +0, and -0 should return the false object |
| 411 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 412 | union IeeeDoubleLittleEndianArchType u; |
| 413 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 414 | union IeeeDoubleBigEndianArchType u; |
| 415 | #endif |
| 416 | u.d = d; |
| 417 | if (u.bits.exp == 2047) { |
| 418 | // Detect NaN for IEEE double precision floating point. |
| 419 | if ((u.bits.man_low | u.bits.man_high) != 0) return false; |
| 420 | } |
| 421 | if (u.bits.exp == 0) { |
| 422 | // Detect +0, and -0 for IEEE double precision floating point. |
| 423 | if ((u.bits.man_low | u.bits.man_high) == 0) return false; |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 429 | } } // namespace v8::internal |