blob: 40c8b404fdfd57ebb7ce3a7db98a814705aa5994 [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#include <stdarg.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include <sys/stat.h>
7
8#include "src/v8.h"
9
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#include "src/base/functional.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/base/logging.h"
12#include "src/base/platform/platform.h"
13#include "src/utils.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000014
15namespace v8 {
16namespace internal {
17
18
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000019SimpleStringBuilder::SimpleStringBuilder(int size) {
Steve Blocka7e24c12009-10-30 11:49:00 +000020 buffer_ = Vector<char>::New(size);
21 position_ = 0;
22}
23
24
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000025void SimpleStringBuilder::AddString(const char* s) {
Steve Blockd0582a62009-12-15 09:54:21 +000026 AddSubstring(s, StrLength(s));
Steve Blocka7e24c12009-10-30 11:49:00 +000027}
28
29
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000030void SimpleStringBuilder::AddSubstring(const char* s, int n) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 DCHECK(!is_finalized() && position_ + n <= buffer_.length());
32 DCHECK(static_cast<size_t>(n) <= strlen(s));
33 MemCopy(&buffer_[position_], s, n * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +000034 position_ += n;
35}
36
37
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000038void SimpleStringBuilder::AddPadding(char c, int count) {
Steve Blocka7e24c12009-10-30 11:49:00 +000039 for (int i = 0; i < count; i++) {
40 AddCharacter(c);
41 }
42}
43
44
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000045void 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
63char* SimpleStringBuilder::Finalize() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 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 Blocka7e24c12009-10-30 11:49:00 +000071 buffer_[position_] = '\0';
72 // Make sure nobody managed to add a 0-character to the
73 // buffer while building the string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 DCHECK(strlen(buffer_.start()) == static_cast<size_t>(position_));
Steve Blocka7e24c12009-10-30 11:49:00 +000075 position_ = -1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 DCHECK(is_finalized());
Steve Blocka7e24c12009-10-30 11:49:00 +000077 return buffer_.start();
78}
79
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080
Emily Bernierd0a1eb72015-03-24 16:35:39 -040081size_t hash_value(BailoutId id) {
82 base::hash<int> h;
83 return h(id.id_);
84}
85
86
87std::ostream& operator<<(std::ostream& os, BailoutId id) {
88 return os << id.id_;
89}
90
91
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092void 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
100void 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
108void 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
117int 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
126int VSNPrintF(Vector<char> str, const char* format, va_list args) {
127 return base::OS::VSNPrintF(str.start(), str.length(), format, args);
128}
129
130
131void StrNCpy(Vector<char> dest, const char* src, size_t n) {
132 base::OS::StrNCpy(dest.start(), dest.length(), src, n);
133}
134
135
136void Flush(FILE* out) {
137 fflush(out);
138}
139
140
141char* 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
193char* 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
223char* 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
234byte* 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
240static 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
253Vector<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
262Vector<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
271int 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
285int 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
302int 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
319int 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
329void 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
337void 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
349static 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.
355static MemMoveFunction memmove_function = &MemMoveWrapper;
356
357// Defined in codegen-ia32.cc.
358MemMoveFunction CreateMemMoveFunction();
359
360// Copy memory area to disjoint memory area.
361void 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
369void 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
378MemCopyUint8Function memcopy_uint8_function = &MemCopyUint8Wrapper;
379MemCopyUint16Uint8Function memcopy_uint16_uint8_function =
380 &MemCopyUint16Uint8Wrapper;
381// Defined in codegen-arm.cc.
382MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub);
383MemCopyUint16Uint8Function CreateMemCopyUint16Uint8Function(
384 MemCopyUint16Uint8Function stub);
385
386#elif V8_OS_POSIX && V8_HOST_ARCH_MIPS
387MemCopyUint8Function memcopy_uint8_function = &MemCopyUint8Wrapper;
388// Defined in codegen-mips.cc.
389MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub);
390#endif
391
392
393void 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
409bool 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 Blocka7e24c12009-10-30 11:49:00 +0000429} } // namespace v8::internal