11 #include "src/base/functional.h" 12 #include "src/base/logging.h" 13 #include "src/base/platform/platform.h" 18 SimpleStringBuilder::SimpleStringBuilder(
int size) {
19 buffer_ = Vector<char>::New(size);
24 void SimpleStringBuilder::AddString(
const char* s) {
25 AddSubstring(s, StrLength(s));
29 void SimpleStringBuilder::AddSubstring(
const char* s,
int n) {
30 DCHECK(!is_finalized() && position_ + n <= buffer_.length());
31 DCHECK(static_cast<size_t>(n) <= strlen(s));
32 MemCopy(&buffer_[position_], s, n * kCharSize);
37 void SimpleStringBuilder::AddPadding(
char c,
int count) {
38 for (
int i = 0;
i < count;
i++) {
44 void SimpleStringBuilder::AddDecimalInteger(int32_t value) {
48 number =
static_cast<uint32_t>(-value);
51 for (
uint32_t factor = 10; digits < 10; digits++, factor *= 10) {
52 if (factor > number)
break;
55 for (
int i = 1;
i <= digits;
i++) {
56 buffer_[position_ -
i] =
'0' +
static_cast<char>(number % 10);
62 char* SimpleStringBuilder::Finalize() {
63 DCHECK(!is_finalized() && position_ <= buffer_.length());
65 if (position_ == buffer_.length()) {
68 for (
int i = 3;
i > 0 && position_ >
i; --
i) buffer_[position_ -
i] =
'.';
70 buffer_[position_] =
'\0';
73 DCHECK(strlen(buffer_.start()) == static_cast<size_t>(position_));
75 DCHECK(is_finalized());
76 return buffer_.start();
79 std::ostream& operator<<(std::ostream& os, FeedbackSlot slot) {
80 return os <<
"#" << slot.id_;
84 size_t hash_value(BailoutId
id) {
90 std::ostream& operator<<(std::ostream& os, BailoutId
id) {
95 void PrintF(
const char* format, ...) {
97 va_start(arguments, format);
98 base::OS::VPrint(format, arguments);
103 void PrintF(FILE* out,
const char* format, ...) {
105 va_start(arguments, format);
106 base::OS::VFPrint(out, format, arguments);
111 void PrintPID(
const char* format, ...) {
112 base::OS::Print(
"[%d] ", base::OS::GetCurrentProcessId());
114 va_start(arguments, format);
115 base::OS::VPrint(format, arguments);
120 void PrintIsolate(
void* isolate,
const char* format, ...) {
121 base::OS::Print(
"[%d:%p] ", base::OS::GetCurrentProcessId(), isolate);
123 va_start(arguments, format);
124 base::OS::VPrint(format, arguments);
129 int SNPrintF(Vector<char> str,
const char* format, ...) {
131 va_start(args, format);
132 int result = VSNPrintF(str, format, args);
138 int VSNPrintF(Vector<char> str,
const char* format, va_list args) {
139 return base::OS::VSNPrintF(str.start(), str.length(), format, args);
143 void StrNCpy(Vector<char> dest,
const char* src,
size_t n) {
144 base::OS::StrNCpy(dest.start(), dest.length(), src, n);
148 void Flush(FILE* out) {
153 char* ReadLine(
const char* prompt) {
154 char* result =
nullptr;
157 bool keep_going =
true;
158 fprintf(stdout,
"%s", prompt);
161 if (fgets(line_buf,
sizeof(line_buf), stdin) ==
nullptr) {
163 if (result !=
nullptr) {
168 int len = StrLength(line_buf);
170 line_buf[len - 2] ==
'\\' &&
171 line_buf[len - 1] ==
'\n') {
174 line_buf[len - 2] =
'\n';
175 line_buf[len - 1] = 0;
177 }
else if ((len > 0) && (line_buf[len - 1] ==
'\n')) {
182 if (result ==
nullptr) {
184 result = NewArray<char>(len + 1);
187 int new_len = offset + len + 1;
188 char* new_result = NewArray<char>(new_len);
191 MemCopy(new_result, result, offset * kCharSize);
196 MemCopy(result + offset, line_buf, len * kCharSize);
199 DCHECK_NOT_NULL(result);
200 result[offset] =
'\0';
206 std::vector<char> ReadCharsFromFile(FILE* file,
bool* exists,
bool verbose,
207 const char* filename) {
208 if (file ==
nullptr || fseek(file, 0, SEEK_END) != 0) {
210 base::OS::PrintError(
"Cannot read from file %s.\n", filename);
213 return std::vector<char>();
217 ptrdiff_t size = ftell(file);
220 std::vector<char> result(size);
221 for (ptrdiff_t
i = 0;
i < size && feof(file) == 0;) {
222 ptrdiff_t read = fread(result.data() +
i, 1, size -
i, file);
223 if (read != (size -
i) && ferror(file) != 0) {
226 return std::vector<char>();
234 std::vector<char> ReadCharsFromFile(
const char* filename,
bool* exists,
236 FILE* file = base::OS::FOpen(filename,
"rb");
237 std::vector<char> result = ReadCharsFromFile(file, exists, verbose, filename);
238 if (file !=
nullptr) fclose(file);
242 std::string VectorToString(
const std::vector<char>& chars) {
243 if (chars.size() == 0) {
244 return std::string();
246 return std::string(chars.begin(), chars.end());
251 std::string ReadFile(
const char* filename,
bool* exists,
bool verbose) {
252 std::vector<char> result = ReadCharsFromFile(filename, exists, verbose);
253 return VectorToString(result);
256 std::string ReadFile(FILE* file,
bool* exists,
bool verbose) {
257 std::vector<char> result = ReadCharsFromFile(file, exists, verbose,
"");
258 return VectorToString(result);
262 int WriteCharsToFile(
const char* str,
int size, FILE* f) {
264 while (total < size) {
265 int write =
static_cast<int>(fwrite(str, 1, size - total, f));
276 int AppendChars(
const char* filename,
280 FILE* f = base::OS::FOpen(filename,
"ab");
283 base::OS::PrintError(
"Cannot open file %s for writing.\n", filename);
287 int written = WriteCharsToFile(str, size, f);
293 int WriteChars(
const char* filename,
297 FILE* f = base::OS::FOpen(filename,
"wb");
300 base::OS::PrintError(
"Cannot open file %s for writing.\n", filename);
304 int written = WriteCharsToFile(str, size, f);
310 int WriteBytes(
const char* filename,
314 const char* str =
reinterpret_cast<const char*
>(bytes);
315 return WriteChars(filename, str, size, verbose);
320 void StringBuilder::AddFormatted(
const char* format, ...) {
322 va_start(arguments, format);
323 AddFormattedList(format, arguments);
328 void StringBuilder::AddFormattedList(
const char* format, va_list list) {
329 DCHECK(!is_finalized() && position_ <= buffer_.length());
330 int n = VSNPrintF(buffer_ + position_, format, list);
331 if (n < 0 || n >= (buffer_.length() - position_)) {
332 position_ = buffer_.length();
338 #if V8_TARGET_ARCH_IA32 339 static void MemMoveWrapper(
void* dest,
const void* src,
size_t size) {
340 memmove(dest, src, size);
345 static MemMoveFunction memmove_function = &MemMoveWrapper;
348 MemMoveFunction CreateMemMoveFunction();
351 void MemMove(
void* dest,
const void* src,
size_t size) {
352 if (size == 0)
return;
355 (*memmove_function)(dest, src, size);
358 #elif V8_OS_POSIX && V8_HOST_ARCH_ARM 359 void MemCopyUint16Uint8Wrapper(uint16_t* dest,
const uint8_t* src,
361 uint16_t* limit = dest + chars;
362 while (dest < limit) {
363 *dest++ =
static_cast<uint16_t
>(*src++);
367 V8_EXPORT_PRIVATE MemCopyUint8Function memcopy_uint8_function =
368 &MemCopyUint8Wrapper;
369 MemCopyUint16Uint8Function memcopy_uint16_uint8_function =
370 &MemCopyUint16Uint8Wrapper;
372 MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub);
373 MemCopyUint16Uint8Function CreateMemCopyUint16Uint8Function(
374 MemCopyUint16Uint8Function stub);
376 #elif V8_OS_POSIX && V8_HOST_ARCH_MIPS 377 V8_EXPORT_PRIVATE MemCopyUint8Function memcopy_uint8_function =
378 &MemCopyUint8Wrapper;
380 MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub);
384 static bool g_memcopy_functions_initialized =
false;
386 void init_memcopy_functions() {
387 if (g_memcopy_functions_initialized)
return;
388 g_memcopy_functions_initialized =
true;
389 #if V8_TARGET_ARCH_IA32 390 MemMoveFunction generated_memmove = CreateMemMoveFunction();
391 if (generated_memmove !=
nullptr) {
392 memmove_function = generated_memmove;
394 #elif V8_OS_POSIX && V8_HOST_ARCH_ARM 395 memcopy_uint8_function = CreateMemCopyUint8Function(&MemCopyUint8Wrapper);
396 memcopy_uint16_uint8_function =
397 CreateMemCopyUint16Uint8Function(&MemCopyUint16Uint8Wrapper);
398 #elif V8_OS_POSIX && V8_HOST_ARCH_MIPS 399 memcopy_uint8_function = CreateMemCopyUint8Function(&MemCopyUint8Wrapper);
404 bool DoubleToBoolean(
double d) {
405 IeeeDoubleArchType u;
407 if (u.bits.exp == 2047) {
409 if ((u.bits.man_low | u.bits.man_high) != 0)
return false;
411 if (u.bits.exp == 0) {
413 if ((u.bits.man_low | u.bits.man_high) == 0)
return false;
420 return reinterpret_cast<uintptr_t>(_AddressOfReturnAddress());
422 return reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
434 bool PassesFilter(Vector<const char> name, Vector<const char> filter) {
435 if (filter.size() == 0)
return name.size() == 0;
436 auto filter_it = filter.begin();
437 bool positive_filter =
true;
438 if (*filter_it ==
'-') {
440 positive_filter =
false;
442 if (filter_it == filter.end())
return name.size() != 0;
443 if (*filter_it ==
'*')
return positive_filter;
444 if (*filter_it ==
'~')
return !positive_filter;
446 bool prefix_match = filter[filter.size() - 1] ==
'*';
447 size_t min_match_length = filter.size();
448 if (!positive_filter) min_match_length--;
449 if (prefix_match) min_match_length--;
451 if (name.size() < min_match_length)
return !positive_filter;
455 auto res = std::mismatch(filter_it, filter.end(), name.begin());
456 if (res.first == filter.end()) {
457 if (res.second == name.end()) {
459 return positive_filter;
463 return !positive_filter;
465 if (*res.first ==
'*') {
468 return positive_filter;
471 return !positive_filter;