v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
checks.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_CHECKS_H_
29 #define V8_CHECKS_H_
30 
31 #include <string.h>
32 
33 #include "../include/v8stdint.h"
34 
35 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
36 
37 
38 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
39 // development, but they should not be relied on in the final product.
40 #ifdef DEBUG
41 #define FATAL(msg) \
42  V8_Fatal(__FILE__, __LINE__, "%s", (msg))
43 #define UNIMPLEMENTED() \
44  V8_Fatal(__FILE__, __LINE__, "unimplemented code")
45 #define UNREACHABLE() \
46  V8_Fatal(__FILE__, __LINE__, "unreachable code")
47 #else
48 #define FATAL(msg) \
49  V8_Fatal("", 0, "%s", (msg))
50 #define UNIMPLEMENTED() \
51  V8_Fatal("", 0, "unimplemented code")
52 #define UNREACHABLE() ((void) 0)
53 #endif
54 
55 // Simulator specific helpers.
56 #if defined(USE_SIMULATOR) && defined(V8_TARGET_ARCH_ARM64)
57  // TODO(all): If possible automatically prepend an indicator like
58  // UNIMPLEMENTED or LOCATION.
59  #define ASM_UNIMPLEMENTED(message) \
60  __ Debug(message, __LINE__, NO_PARAM)
61  #define ASM_UNIMPLEMENTED_BREAK(message) \
62  __ Debug(message, __LINE__, \
63  FLAG_ignore_asm_unimplemented_break ? NO_PARAM : BREAK)
64  #define ASM_LOCATION(message) \
65  __ Debug("LOCATION: " message, __LINE__, NO_PARAM)
66 #else
67  #define ASM_UNIMPLEMENTED(message)
68  #define ASM_UNIMPLEMENTED_BREAK(message)
69  #define ASM_LOCATION(message)
70 #endif
71 
72 
73 // The CHECK macro checks that the given condition is true; if not, it
74 // prints a message to stderr and aborts.
75 #define CHECK(condition) do { \
76  if (!(condition)) { \
77  V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \
78  } \
79  } while (0)
80 
81 
82 // Helper function used by the CHECK_EQ function when given int
83 // arguments. Should not be called directly.
84 inline void CheckEqualsHelper(const char* file, int line,
85  const char* expected_source, int expected,
86  const char* value_source, int value) {
87  if (expected != value) {
88  V8_Fatal(file, line,
89  "CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i",
90  expected_source, value_source, expected, value);
91  }
92 }
93 
94 
95 // Helper function used by the CHECK_EQ function when given int64_t
96 // arguments. Should not be called directly.
97 inline void CheckEqualsHelper(const char* file, int line,
98  const char* expected_source,
99  int64_t expected,
100  const char* value_source,
101  int64_t value) {
102  if (expected != value) {
103  // Print int64_t values in hex, as two int32s,
104  // to avoid platform-dependencies.
105  V8_Fatal(file, line,
106  "CHECK_EQ(%s, %s) failed\n#"
107  " Expected: 0x%08x%08x\n# Found: 0x%08x%08x",
108  expected_source, value_source,
109  static_cast<uint32_t>(expected >> 32),
110  static_cast<uint32_t>(expected),
111  static_cast<uint32_t>(value >> 32),
112  static_cast<uint32_t>(value));
113  }
114 }
115 
116 
117 // Helper function used by the CHECK_NE function when given int
118 // arguments. Should not be called directly.
119 inline void CheckNonEqualsHelper(const char* file,
120  int line,
121  const char* unexpected_source,
122  int unexpected,
123  const char* value_source,
124  int value) {
125  if (unexpected == value) {
126  V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
127  unexpected_source, value_source, value);
128  }
129 }
130 
131 
132 // Helper function used by the CHECK function when given string
133 // arguments. Should not be called directly.
134 inline void CheckEqualsHelper(const char* file,
135  int line,
136  const char* expected_source,
137  const char* expected,
138  const char* value_source,
139  const char* value) {
140  if ((expected == NULL && value != NULL) ||
141  (expected != NULL && value == NULL) ||
142  (expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
143  V8_Fatal(file, line,
144  "CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s",
145  expected_source, value_source, expected, value);
146  }
147 }
148 
149 
150 inline void CheckNonEqualsHelper(const char* file,
151  int line,
152  const char* expected_source,
153  const char* expected,
154  const char* value_source,
155  const char* value) {
156  if (expected == value ||
157  (expected != NULL && value != NULL && strcmp(expected, value) == 0)) {
158  V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s",
159  expected_source, value_source, value);
160  }
161 }
162 
163 
164 // Helper function used by the CHECK function when given pointer
165 // arguments. Should not be called directly.
166 inline void CheckEqualsHelper(const char* file,
167  int line,
168  const char* expected_source,
169  const void* expected,
170  const char* value_source,
171  const void* value) {
172  if (expected != value) {
173  V8_Fatal(file, line,
174  "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p",
175  expected_source, value_source,
176  expected, value);
177  }
178 }
179 
180 
181 inline void CheckNonEqualsHelper(const char* file,
182  int line,
183  const char* expected_source,
184  const void* expected,
185  const char* value_source,
186  const void* value) {
187  if (expected == value) {
188  V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
189  expected_source, value_source, value);
190  }
191 }
192 
193 
194 // Helper function used by the CHECK function when given floating
195 // point arguments. Should not be called directly.
196 inline void CheckEqualsHelper(const char* file,
197  int line,
198  const char* expected_source,
199  double expected,
200  const char* value_source,
201  double value) {
202  // Force values to 64 bit memory to truncate 80 bit precision on IA32.
203  volatile double* exp = new double[1];
204  *exp = expected;
205  volatile double* val = new double[1];
206  *val = value;
207  if (*exp != *val) {
208  V8_Fatal(file, line,
209  "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
210  expected_source, value_source, *exp, *val);
211  }
212  delete[] exp;
213  delete[] val;
214 }
215 
216 
217 inline void CheckNonEqualsHelper(const char* file,
218  int line,
219  const char* expected_source,
220  int64_t expected,
221  const char* value_source,
222  int64_t value) {
223  if (expected == value) {
224  V8_Fatal(file, line,
225  "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
226  expected_source, value_source, expected, value);
227  }
228 }
229 
230 
231 inline void CheckNonEqualsHelper(const char* file,
232  int line,
233  const char* expected_source,
234  double expected,
235  const char* value_source,
236  double value) {
237  // Force values to 64 bit memory to truncate 80 bit precision on IA32.
238  volatile double* exp = new double[1];
239  *exp = expected;
240  volatile double* val = new double[1];
241  *val = value;
242  if (*exp == *val) {
243  V8_Fatal(file, line,
244  "CHECK_NE(%s, %s) failed\n# Value: %f",
245  expected_source, value_source, *val);
246  }
247  delete[] exp;
248  delete[] val;
249 }
250 
251 
252 #define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \
253  #expected, expected, #value, value)
254 
255 
256 #define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \
257  #unexpected, unexpected, #value, value)
258 
259 
260 #define CHECK_GT(a, b) CHECK((a) > (b))
261 #define CHECK_GE(a, b) CHECK((a) >= (b))
262 #define CHECK_LT(a, b) CHECK((a) < (b))
263 #define CHECK_LE(a, b) CHECK((a) <= (b))
264 
265 
266 // Use C++11 static_assert if possible, which gives error
267 // messages that are easier to understand on first sight.
268 #if V8_HAS_CXX11_STATIC_ASSERT
269 #define STATIC_CHECK(test) static_assert(test, #test)
270 #else
271 // This is inspired by the static assertion facility in boost. This
272 // is pretty magical. If it causes you trouble on a platform you may
273 // find a fix in the boost code.
274 template <bool> class StaticAssertion;
275 template <> class StaticAssertion<true> { };
276 // This macro joins two tokens. If one of the tokens is a macro the
277 // helper call causes it to be resolved before joining.
278 #define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
279 #define SEMI_STATIC_JOIN_HELPER(a, b) a##b
280 // Causes an error during compilation of the condition is not
281 // statically known to be true. It is formulated as a typedef so that
282 // it can be used wherever a typedef can be used. Beware that this
283 // actually causes each use to introduce a new defined type with a
284 // name depending on the source line.
285 template <int> class StaticAssertionHelper { };
286 #define STATIC_CHECK(test) \
287  typedef \
288  StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \
289  SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) V8_UNUSED
290 #endif
291 
292 
293 #ifdef DEBUG
294 #ifndef OPTIMIZED_DEBUG
295 #define ENABLE_SLOW_ASSERTS 1
296 #endif
297 #endif
298 
299 namespace v8 {
300 namespace internal {
301 #ifdef ENABLE_SLOW_ASSERTS
302 #define SLOW_ASSERT(condition) \
303  CHECK(!v8::internal::FLAG_enable_slow_asserts || (condition))
304 extern bool FLAG_enable_slow_asserts;
305 #else
306 #define SLOW_ASSERT(condition) ((void) 0)
307 const bool FLAG_enable_slow_asserts = false;
308 #endif
309 
310 // Exposed for making debugging easier (to see where your function is being
311 // called, just add a call to DumpBacktrace).
312 void DumpBacktrace();
313 
314 } } // namespace v8::internal
315 
316 
317 // The ASSERT macro is equivalent to CHECK except that it only
318 // generates code in debug builds.
319 #ifdef DEBUG
320 #define ASSERT_RESULT(expr) CHECK(expr)
321 #define ASSERT(condition) CHECK(condition)
322 #define ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2)
323 #define ASSERT_NE(v1, v2) CHECK_NE(v1, v2)
324 #define ASSERT_GE(v1, v2) CHECK_GE(v1, v2)
325 #define ASSERT_LT(v1, v2) CHECK_LT(v1, v2)
326 #define ASSERT_LE(v1, v2) CHECK_LE(v1, v2)
327 #else
328 #define ASSERT_RESULT(expr) (expr)
329 #define ASSERT(condition) ((void) 0)
330 #define ASSERT_EQ(v1, v2) ((void) 0)
331 #define ASSERT_NE(v1, v2) ((void) 0)
332 #define ASSERT_GE(v1, v2) ((void) 0)
333 #define ASSERT_LT(v1, v2) ((void) 0)
334 #define ASSERT_LE(v1, v2) ((void) 0)
335 #endif
336 // Static asserts has no impact on runtime performance, so they can be
337 // safely enabled in release mode. Moreover, the ((void) 0) expression
338 // obeys different syntax rules than typedef's, e.g. it can't appear
339 // inside class declaration, this leads to inconsistency between debug
340 // and release compilation modes behavior.
341 #define STATIC_ASSERT(test) STATIC_CHECK(test)
342 
343 #define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p)
344 
345 // "Extra checks" are lightweight checks that are enabled in some release
346 // builds.
347 #ifdef ENABLE_EXTRA_CHECKS
348 #define EXTRA_CHECK(condition) CHECK(condition)
349 #else
350 #define EXTRA_CHECK(condition) ((void) 0)
351 #endif
352 
353 #endif // V8_CHECKS_H_
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
void V8_Fatal(const char *file, int line, const char *format,...)
Definition: checks.cc:100
const bool FLAG_enable_slow_asserts
Definition: checks.h:307
void CheckNonEqualsHelper(const char *file, int line, const char *unexpected_source, int unexpected, const char *value_source, int value)
Definition: checks.h:119
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths true
void DumpBacktrace()
Definition: checks.cc:47
void CheckEqualsHelper(const char *file, int line, const char *expected_source, int expected, const char *value_source, int value)
Definition: checks.h:84