blob: 9072b4e2855435d7bf657b84e0aa0d4b3b9647c5 [file] [log] [blame]
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001// Copyright 2012 the V8 project authors. All rights reserved.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002// 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.orgc6c57182011-01-17 12:24:25 +000032#include "platform.h" // For va_list on Solaris.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000033
34namespace v8 {
35namespace 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.org023421e2010-12-21 12:19:12 +000046#define FPRINTF_CHECKING
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000047#else // MacOsX.
48#define PRINTF_CHECKING __attribute__ ((format (printf, 1, 2)))
whesse@chromium.org023421e2010-12-21 12:19:12 +000049#define FPRINTF_CHECKING __attribute__ ((format (printf, 2, 3)))
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000050#endif
51#else
52#define PRINTF_CHECKING
whesse@chromium.org023421e2010-12-21 12:19:12 +000053#define FPRINTF_CHECKING
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000054#endif
55
56// Our version of printf().
57void PRINTF_CHECKING PrintF(const char* format, ...);
whesse@chromium.org023421e2010-12-21 12:19:12 +000058void FPRINTF_CHECKING PrintF(FILE* out, const char* format, ...);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000059
rossberg@chromium.org657d53b2012-07-12 11:06:03 +000060// Prepends the current process ID to the output.
61void PRINTF_CHECKING PrintPID(const char* format, ...);
62
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000063// Our version of fflush.
whesse@chromium.org023421e2010-12-21 12:19:12 +000064void Flush(FILE* out);
65
66inline void Flush() {
67 Flush(stdout);
68}
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000069
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.
73char* 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.
79byte* ReadBytes(const char* filename, int* size, bool verbose = true);
80
81
kasperl@chromium.orga5551262010-12-07 12:49:48 +000082// Append size chars from str to the file given by filename.
83// The file is overwritten. Returns the number of chars written.
84int AppendChars(const char* filename,
85 const char* str,
86 int size,
87 bool verbose = true);
88
89
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000090// Write size chars from str to the file given by filename.
91// The file is overwritten. Returns the number of chars written.
92int 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.
100int 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.
110int WriteAsCFile(const char* filename, const char* varname,
111 const char* str, int size, bool verbose = true);
112
113
114// Data structures
115
116template <typename T>
117inline 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.org44bc7082011-04-11 12:33:05 +0000126template <typename T>
127inline void CopyWords(T* dst, T* src, int num_words) {
128 STATIC_ASSERT(sizeof(T) == kPointerSize);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000129 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.orga8bb4d92011-10-10 13:20:40 +0000148template <typename T, typename U>
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000149inline void MemsetPointer(T** dest, U* value, int counter) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000150#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.orgf0ac72d2010-11-08 12:47:26 +0000156#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.
181class 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.
197Vector<const char> ReadFile(const char* filename,
198 bool* exists,
199 bool verbose = true);
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000200Vector<const char> ReadFile(FILE* file,
201 bool* exists,
202 bool verbose = true);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000203
204
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000205// Copy from ASCII/16bit chars to ASCII/16bit chars.
206template <typename sourcechar, typename sinkchar>
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000207INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, int chars));
208
209
210template <typename sourcechar, typename sinkchar>
211void CopyChars(sinkchar* dest, const sourcechar* src, int chars) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000212 sinkchar* limit = dest + chars;
213#ifdef V8_HOST_CAN_READ_UNALIGNED
214 if (sizeof(*dest) == sizeof(*src)) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000215 if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) {
216 OS::MemCopy(dest, src, chars * sizeof(*dest));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000217 return;
218 }
219 // Number of characters in a uintptr_t.
220 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT
221 while (dest <= limit - kStepSize) {
222 *reinterpret_cast<uintptr_t*>(dest) =
223 *reinterpret_cast<const uintptr_t*>(src);
224 dest += kStepSize;
225 src += kStepSize;
226 }
227 }
228#endif
229 while (dest < limit) {
230 *dest++ = static_cast<sinkchar>(*src++);
231 }
232}
233
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000234
235// A resource for using mmapped files to back external strings that are read
236// from files.
237class MemoryMappedExternalResource: public
238 v8::String::ExternalAsciiStringResource {
239 public:
240 explicit MemoryMappedExternalResource(const char* filename);
241 MemoryMappedExternalResource(const char* filename,
242 bool remove_file_on_cleanup);
243 virtual ~MemoryMappedExternalResource();
244
245 virtual const char* data() const { return data_; }
246 virtual size_t length() const { return length_; }
247
248 bool exists() const { return file_ != NULL; }
249 bool is_empty() const { return length_ == 0; }
250
251 bool EnsureIsAscii(bool abort_if_failed) const;
252 bool EnsureIsAscii() const { return EnsureIsAscii(true); }
253 bool IsAscii() const { return EnsureIsAscii(false); }
254
255 private:
256 void Init(const char* filename);
257
258 char* filename_;
259 OS::MemoryMappedFile* file_;
260
261 const char* data_;
262 size_t length_;
263 bool remove_file_on_cleanup_;
264};
265
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000266class StringBuilder : public SimpleStringBuilder {
267 public:
268 explicit StringBuilder(int size) : SimpleStringBuilder(size) { }
269 StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { }
270
271 // Add formatted contents to the builder just like printf().
272 void AddFormatted(const char* format, ...);
273
274 // Add formatted contents like printf based on a va_list.
275 void AddFormattedList(const char* format, va_list list);
276 private:
277 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
278};
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000279
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000280} } // namespace v8::internal
281
282#endif // V8_V8UTILS_H_