v8  3.11.10(node0.8.26)
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 // Our version of fflush.
61 void Flush(FILE* out);
62 
63 inline void Flush() {
64  Flush(stdout);
65 }
66 
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.
70 char* 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.
76 byte* ReadBytes(const char* filename, int* size, bool verbose = true);
77 
78 
79 // Append size chars from str to the file given by filename.
80 // The file is overwritten. Returns the number of chars written.
81 int AppendChars(const char* filename,
82  const char* str,
83  int size,
84  bool verbose = true);
85 
86 
87 // Write size chars from str to the file given by filename.
88 // The file is overwritten. Returns the number of chars written.
89 int 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.
97 int 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.
107 int WriteAsCFile(const char* filename, const char* varname,
108  const char* str, int size, bool verbose = true);
109 
110 
111 // Data structures
112 
113 template <typename T>
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.
123 template <typename T>
124 inline void CopyWords(T* dst, T* src, int num_words) {
125  STATIC_ASSERT(sizeof(T) == kPointerSize);
126  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 
145 template <typename T, typename U>
146 inline void MemsetPointer(T** dest, U* value, int counter) {
147 #ifdef DEBUG
148  T* a = NULL;
149  U* b = NULL;
150  a = b; // Fake assignment to check assignability.
151  USE(a);
152 #endif // DEBUG
153 #if defined(V8_HOST_ARCH_IA32)
154 #define STOS "stosl"
155 #elif defined(V8_HOST_ARCH_X64)
156 #define STOS "stosq"
157 #endif
158 
159 #if defined(__GNUC__) && defined(STOS)
160  asm volatile(
161  "cld;"
162  "rep ; " STOS
163  : "+&c" (counter), "+&D" (dest)
164  : "a" (value)
165  : "memory", "cc");
166 #else
167  for (int i = 0; i < counter; i++) {
168  dest[i] = value;
169  }
170 #endif
171 
172 #undef STOS
173 }
174 
175 
176 // Simple wrapper that allows an ExternalString to refer to a
177 // Vector<const char>. Doesn't assume ownership of the data.
179  public:
180  explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {}
181 
182  virtual const char* data() const { return data_.start(); }
183 
184  virtual size_t length() const { return data_.length(); }
185 
186  private:
187  Vector<const char> data_;
188 };
189 
190 
191 // Simple support to read a file into a 0-terminated C-string.
192 // The returned buffer must be freed by the caller.
193 // On return, *exits tells whether the file existed.
194 Vector<const char> ReadFile(const char* filename,
195  bool* exists,
196  bool verbose = true);
197 Vector<const char> ReadFile(FILE* file,
198  bool* exists,
199  bool verbose = true);
200 
201 
202 // Copy from ASCII/16bit chars to ASCII/16bit chars.
203 template <typename sourcechar, typename sinkchar>
204 INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, int chars));
205 
206 
207 template <typename sourcechar, typename sinkchar>
208 void CopyChars(sinkchar* dest, const sourcechar* src, int chars) {
209  sinkchar* limit = dest + chars;
210 #ifdef V8_HOST_CAN_READ_UNALIGNED
211  if (sizeof(*dest) == sizeof(*src)) {
212  if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) {
213  OS::MemCopy(dest, src, chars * sizeof(*dest));
214  return;
215  }
216  // Number of characters in a uintptr_t.
217  static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT
218  while (dest <= limit - kStepSize) {
219  *reinterpret_cast<uintptr_t*>(dest) =
220  *reinterpret_cast<const uintptr_t*>(src);
221  dest += kStepSize;
222  src += kStepSize;
223  }
224  }
225 #endif
226  while (dest < limit) {
227  *dest++ = static_cast<sinkchar>(*src++);
228  }
229 }
230 
231 
232 // A resource for using mmapped files to back external strings that are read
233 // from files.
236  public:
237  explicit MemoryMappedExternalResource(const char* filename);
238  MemoryMappedExternalResource(const char* filename,
239  bool remove_file_on_cleanup);
241 
242  virtual const char* data() const { return data_; }
243  virtual size_t length() const { return length_; }
244 
245  bool exists() const { return file_ != NULL; }
246  bool is_empty() const { return length_ == 0; }
247 
248  bool EnsureIsAscii(bool abort_if_failed) const;
249  bool EnsureIsAscii() const { return EnsureIsAscii(true); }
250  bool IsAscii() const { return EnsureIsAscii(false); }
251 
252  private:
253  void Init(const char* filename);
254 
255  char* filename_;
256  OS::MemoryMappedFile* file_;
257 
258  const char* data_;
259  size_t length_;
260  bool remove_file_on_cleanup_;
261 };
262 
264  public:
265  explicit StringBuilder(int size) : SimpleStringBuilder(size) { }
266  StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { }
267 
268  // Add formatted contents to the builder just like printf().
269  void AddFormatted(const char* format, ...);
270 
271  // Add formatted contents like printf based on a va_list.
272  void AddFormattedList(const char* format, va_list list);
273  private:
274  DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
275 };
276 
277 } } // namespace v8::internal
278 
279 #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:184
#define ASSERT(condition)
Definition: checks.h:270
byte * ReadBytes(const char *filename, int *size, bool verbose)
Definition: v8utils.cc:154
char * ReadLine(const char *prompt)
Definition: v8utils.cc:61
static const int kMinComplexMemCopy
Definition: platform.h:317
virtual const char * data() const
Definition: v8utils.h:242
uint8_t byte
Definition: globals.h:171
void CopyWords(T *dst, T *src, int num_words)
Definition: v8utils.h:124
AsciiStringAdapter(Vector< const char > data)
Definition: v8utils.h:180
T * start() const
Definition: utils.h:389
STATIC_ASSERT((FixedDoubleArray::kHeaderSize &kDoubleAlignmentMask)==0)
virtual size_t length() const
Definition: v8utils.h:243
static void MemCopy(void *dest, const void *src, size_t size)
Definition: platform.h:314
Vector< const char > ReadFile(const char *filename, bool *exists, bool verbose)
Definition: v8utils.cc:173
const int kPointerSize
Definition: globals.h:234
#define PRINTF_CHECKING
Definition: v8utils.h:52
void AddFormatted(const char *format,...)
Definition: v8utils.cc:249
StringBuilder(char *buffer, int size)
Definition: v8utils.h:266
int length() const
Definition: utils.h:383
#define T(name, string, precedence)
Definition: token.cc:48
void AddFormattedList(const char *format, va_list list)
Definition: v8utils.cc:257
int WriteChars(const char *filename, const char *str, int size, bool verbose)
Definition: v8utils.cc:222
#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:205
void MemsetPointer(T **dest, U *value, int counter)
Definition: v8utils.h:146
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 trace on stack replacement optimize closures functions with arguments object optimize functions containing for in loops profiler considers IC stability primitive functions trigger their own optimization re try self optimization if it failed insert an interrupt check at function exit execution budget before interrupt is triggered call count before self optimization self_optimization count_based_interrupts weighted_back_edges trace_opt 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 enable use of ARMv7 instructions if enable use of MIPS FPU instructions if NULL
Definition: flags.cc:274
void USE(T)
Definition: globals.h:303
int WriteBytes(const char *filename, const byte *bytes, int size, bool verbose)
Definition: v8utils.cc:239
Vector< Handle< Object > > HandleVector(v8::internal::Handle< T > *elms, int length)
Definition: v8utils.h:114
void CopyChars(sinkchar *dest, const sourcechar *src, int chars)
Definition: v8utils.h:208
T Min(T a, T b)
Definition: utils.h:229
void Flush(FILE *out)
Definition: v8utils.cc:56
virtual const char * data() const
Definition: v8utils.h:182
MemoryMappedExternalResource(const char *filename)
Definition: v8utils.cc:268