v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
v8utils.h
Go to the documentation of this file.
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // 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"
32 #include "platform.h" // For va_list on Solaris.
33 
34 namespace v8 {
35 namespace 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
46 #define FPRINTF_CHECKING
47 #else // MacOsX.
48 #define PRINTF_CHECKING __attribute__ ((format (printf, 1, 2)))
49 #define FPRINTF_CHECKING __attribute__ ((format (printf, 2, 3)))
50 #endif
51 #else
52 #define PRINTF_CHECKING
53 #define FPRINTF_CHECKING
54 #endif
55 
56 // Our version of printf().
57 void PRINTF_CHECKING PrintF(const char* format, ...);
58 void FPRINTF_CHECKING PrintF(FILE* out, const char* format, ...);
59 
60 // Prepends the current process ID to the output.
61 void PRINTF_CHECKING PrintPID(const char* format, ...);
62 
63 // Our version of fflush.
64 void Flush(FILE* out);
65 
66 inline void Flush() {
67  Flush(stdout);
68 }
69 
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.
73 char* 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.
79 byte* ReadBytes(const char* filename, int* size, bool verbose = true);
80 
81 
82 // Append size chars from str to the file given by filename.
83 // The file is overwritten. Returns the number of chars written.
84 int AppendChars(const char* filename,
85  const char* str,
86  int size,
87  bool verbose = true);
88 
89 
90 // Write size chars from str to the file given by filename.
91 // The file is overwritten. Returns the number of chars written.
92 int 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.
100 int 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.
110 int WriteAsCFile(const char* filename, const char* varname,
111  const char* str, int size, bool verbose = true);
112 
113 
114 // Data structures
115 
116 template <typename T>
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.
126 template <typename T>
127 inline void CopyWords(T* dst, T* src, int num_words) {
128  STATIC_ASSERT(sizeof(T) == kPointerSize);
129  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 
148 template <typename T, typename U>
149 inline void MemsetPointer(T** dest, U* value, int counter) {
150 #ifdef DEBUG
151  T* a = NULL;
152  U* b = NULL;
153  a = b; // Fake assignment to check assignability.
154  USE(a);
155 #endif // DEBUG
156 #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.
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.
197 Vector<const char> ReadFile(const char* filename,
198  bool* exists,
199  bool verbose = true);
200 Vector<const char> ReadFile(FILE* file,
201  bool* exists,
202  bool verbose = true);
203 
204 
205 // Copy from ASCII/16bit chars to ASCII/16bit chars.
206 template <typename sourcechar, typename sinkchar>
207 INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, int chars));
208 
209 
210 template <typename sourcechar, typename sinkchar>
211 void CopyChars(sinkchar* dest, const sourcechar* src, int chars) {
212  sinkchar* limit = dest + chars;
213 #ifdef V8_HOST_CAN_READ_UNALIGNED
214  if (sizeof(*dest) == sizeof(*src)) {
215  if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) {
216  OS::MemCopy(dest, src, chars * sizeof(*dest));
217  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 
234 
235 // A resource for using mmapped files to back external strings that are read
236 // from files.
239  public:
240  explicit MemoryMappedExternalResource(const char* filename);
241  MemoryMappedExternalResource(const char* filename,
242  bool remove_file_on_cleanup);
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 
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 };
279 
280 } } // namespace v8::internal
281 
282 #endif // V8_V8UTILS_H_
void PrintF(const char *format,...)
Definition: v8utils.cc:40
T Max(T a, T b)
Definition: utils.h:222
virtual size_t length() const
Definition: v8utils.h:187
#define ASSERT(condition)
Definition: checks.h:270
byte * ReadBytes(const char *filename, int *size, bool verbose)
Definition: v8utils.cc:163
char * ReadLine(const char *prompt)
Definition: v8utils.cc:70
static const int kMinComplexMemCopy
Definition: platform.h:342
virtual const char * data() const
Definition: v8utils.h:245
uint8_t byte
Definition: globals.h:156
void CopyWords(T *dst, T *src, int num_words)
Definition: v8utils.h:127
AsciiStringAdapter(Vector< const char > data)
Definition: v8utils.h:183
T * start() const
Definition: utils.h:390
STATIC_ASSERT((FixedDoubleArray::kHeaderSize &kDoubleAlignmentMask)==0)
virtual size_t length() const
Definition: v8utils.h:246
static void MemCopy(void *dest, const void *src, size_t size)
Definition: platform.h:339
Vector< const char > ReadFile(const char *filename, bool *exists, bool verbose)
Definition: v8utils.cc:182
const int kPointerSize
Definition: globals.h:220
#define PRINTF_CHECKING
Definition: v8utils.h:52
void AddFormatted(const char *format,...)
Definition: v8utils.cc:258
StringBuilder(char *buffer, int size)
Definition: v8utils.h:269
int length() const
Definition: utils.h:384
#define T(name, string, precedence)
Definition: token.cc:48
void AddFormattedList(const char *format, va_list list)
Definition: v8utils.cc:266
int WriteChars(const char *filename, const char *str, int size, bool verbose)
Definition: v8utils.cc:231
#define FPRINTF_CHECKING
Definition: v8utils.h:53
INLINE(static HeapObject *EnsureDoubleAligned(Heap *heap, HeapObject *object, int size))
int WriteAsCFile(const char *filename, const char *varname, const char *str, int size, bool verbose=true)
int AppendChars(const char *filename, const char *str, int size, bool verbose)
Definition: v8utils.cc:214
void MemsetPointer(T **dest, U *value, int counter)
Definition: v8utils.h:149
void PrintPID(const char *format,...)
Definition: v8utils.cc:56
void USE(T)
Definition: globals.h:289
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
int WriteBytes(const char *filename, const byte *bytes, int size, bool verbose)
Definition: v8utils.cc:248
Vector< Handle< Object > > HandleVector(v8::internal::Handle< T > *elms, int length)
Definition: v8utils.h:117
void CopyChars(sinkchar *dest, const sourcechar *src, int chars)
Definition: v8utils.h:211
T Min(T a, T b)
Definition: utils.h:229
void Flush(FILE *out)
Definition: v8utils.cc:65
virtual const char * data() const
Definition: v8utils.h:185
MemoryMappedExternalResource(const char *filename)
Definition: v8utils.cc:277