blob: aada521e4cc8dae3b6e0ee26934f6d431ed82a38 [file] [log] [blame]
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001// Copyright 2011 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
60// Our version of fflush.
whesse@chromium.org023421e2010-12-21 12:19:12 +000061void Flush(FILE* out);
62
63inline void Flush() {
64 Flush(stdout);
65}
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000066
67
68// Read a line of characters after printing the prompt to stdout. The resulting
69// char* needs to be disposed off with DeleteArray by the caller.
70char* ReadLine(const char* prompt);
71
72
73// Read and return the raw bytes in a file. the size of the buffer is returned
74// in size.
75// The returned buffer must be freed by the caller.
76byte* ReadBytes(const char* filename, int* size, bool verbose = true);
77
78
kasperl@chromium.orga5551262010-12-07 12:49:48 +000079// Append size chars from str to the file given by filename.
80// The file is overwritten. Returns the number of chars written.
81int AppendChars(const char* filename,
82 const char* str,
83 int size,
84 bool verbose = true);
85
86
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000087// Write size chars from str to the file given by filename.
88// The file is overwritten. Returns the number of chars written.
89int WriteChars(const char* filename,
90 const char* str,
91 int size,
92 bool verbose = true);
93
94
95// Write size bytes to the file given by filename.
96// The file is overwritten. Returns the number of bytes written.
97int WriteBytes(const char* filename,
98 const byte* bytes,
99 int size,
100 bool verbose = true);
101
102
103// Write the C code
104// const char* <varname> = "<str>";
105// const int <varname>_len = <len>;
106// to the file given by filename. Only the first len chars are written.
107int WriteAsCFile(const char* filename, const char* varname,
108 const char* str, int size, bool verbose = true);
109
110
111// Data structures
112
113template <typename T>
114inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms,
115 int length) {
116 return Vector< Handle<Object> >(
117 reinterpret_cast<v8::internal::Handle<Object>*>(elms), length);
118}
119
120// Memory
121
122// Copies data from |src| to |dst|. The data spans MUST not overlap.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000123template <typename T>
124inline void CopyWords(T* dst, T* src, int num_words) {
125 STATIC_ASSERT(sizeof(T) == kPointerSize);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000126 ASSERT(Min(dst, src) + num_words <= Max(dst, src));
127 ASSERT(num_words > 0);
128
129 // Use block copying memcpy if the segment we're copying is
130 // enough to justify the extra call/setup overhead.
131 static const int kBlockCopyLimit = 16;
132
133 if (num_words >= kBlockCopyLimit) {
134 memcpy(dst, src, num_words * kPointerSize);
135 } else {
136 int remaining = num_words;
137 do {
138 remaining--;
139 *dst++ = *src++;
140 } while (remaining > 0);
141 }
142}
143
144
145template <typename T>
146static inline void MemsetPointer(T** dest, T* value, int counter) {
147#if defined(V8_HOST_ARCH_IA32)
148#define STOS "stosl"
149#elif defined(V8_HOST_ARCH_X64)
150#define STOS "stosq"
151#endif
152
153#if defined(__GNUC__) && defined(STOS)
154 asm volatile(
155 "cld;"
156 "rep ; " STOS
157 : "+&c" (counter), "+&D" (dest)
158 : "a" (value)
159 : "memory", "cc");
160#else
161 for (int i = 0; i < counter; i++) {
162 dest[i] = value;
163 }
164#endif
165
166#undef STOS
167}
168
169
170// Simple wrapper that allows an ExternalString to refer to a
171// Vector<const char>. Doesn't assume ownership of the data.
172class AsciiStringAdapter: public v8::String::ExternalAsciiStringResource {
173 public:
174 explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {}
175
176 virtual const char* data() const { return data_.start(); }
177
178 virtual size_t length() const { return data_.length(); }
179
180 private:
181 Vector<const char> data_;
182};
183
184
185// Simple support to read a file into a 0-terminated C-string.
186// The returned buffer must be freed by the caller.
187// On return, *exits tells whether the file existed.
188Vector<const char> ReadFile(const char* filename,
189 bool* exists,
190 bool verbose = true);
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000191Vector<const char> ReadFile(FILE* file,
192 bool* exists,
193 bool verbose = true);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000194
195
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000196
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000197// Copy from ASCII/16bit chars to ASCII/16bit chars.
198template <typename sourcechar, typename sinkchar>
199static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) {
200 sinkchar* limit = dest + chars;
201#ifdef V8_HOST_CAN_READ_UNALIGNED
202 if (sizeof(*dest) == sizeof(*src)) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000203 if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) {
204 OS::MemCopy(dest, src, chars * sizeof(*dest));
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000205 return;
206 }
207 // Number of characters in a uintptr_t.
208 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT
209 while (dest <= limit - kStepSize) {
210 *reinterpret_cast<uintptr_t*>(dest) =
211 *reinterpret_cast<const uintptr_t*>(src);
212 dest += kStepSize;
213 src += kStepSize;
214 }
215 }
216#endif
217 while (dest < limit) {
218 *dest++ = static_cast<sinkchar>(*src++);
219 }
220}
221
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000222
223// A resource for using mmapped files to back external strings that are read
224// from files.
225class MemoryMappedExternalResource: public
226 v8::String::ExternalAsciiStringResource {
227 public:
228 explicit MemoryMappedExternalResource(const char* filename);
229 MemoryMappedExternalResource(const char* filename,
230 bool remove_file_on_cleanup);
231 virtual ~MemoryMappedExternalResource();
232
233 virtual const char* data() const { return data_; }
234 virtual size_t length() const { return length_; }
235
236 bool exists() const { return file_ != NULL; }
237 bool is_empty() const { return length_ == 0; }
238
239 bool EnsureIsAscii(bool abort_if_failed) const;
240 bool EnsureIsAscii() const { return EnsureIsAscii(true); }
241 bool IsAscii() const { return EnsureIsAscii(false); }
242
243 private:
244 void Init(const char* filename);
245
246 char* filename_;
247 OS::MemoryMappedFile* file_;
248
249 const char* data_;
250 size_t length_;
251 bool remove_file_on_cleanup_;
252};
253
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000254class StringBuilder : public SimpleStringBuilder {
255 public:
256 explicit StringBuilder(int size) : SimpleStringBuilder(size) { }
257 StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { }
258
259 // Add formatted contents to the builder just like printf().
260 void AddFormatted(const char* format, ...);
261
262 // Add formatted contents like printf based on a va_list.
263 void AddFormattedList(const char* format, va_list list);
264 private:
265 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
266};
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000267
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000268} } // namespace v8::internal
269
270#endif // V8_V8UTILS_H_