From ede4604b7ec9fc79cabd786f2bbc65d718b5929c Mon Sep 17 00:00:00 2001 From: Timothy Harvey Date: Fri, 9 Dec 2016 18:16:17 -0600 Subject: [PATCH] Added #if control around GCC builtin functions so that the code can be compiled using non-GCC compilers. This included adding an initialization to a variable that looks(!) like it can reach a use before being initialized (b/c we turned JERRY_UNREACHABLE into a nop) -- an example: int foo; switch (value_can_only_be_one_or_two) case 1: case 2: foo = 5; default: JERRY_UNREACHABLE(); x = foo +1; ...the compiler assumes that the path can go through the default case, which leaves the value of foo undefined. JerryScript-DCO-1.0-Signed-off-by: Timothy Harvey t-harvey@ti.com --- .../ecma/builtin-objects/ecma-builtin-global.c | 2 +- jerry-core/jrt/jrt.h | 13 +++++++++++-- jerry-libc/include/assert.h | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-global.c b/jerry-core/ecma/builtin-objects/ecma-builtin-global.c index a7ce7f1fed..58ae632a99 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-global.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-global.c @@ -343,7 +343,7 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg, /**< this argument while (string_curr_p > start_p) { ecma_char_t current_char = *(--string_curr_p); - ecma_number_t current_number; + ecma_number_t current_number = ECMA_NUMBER_MINUS_ONE; if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z)) { diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index 6f09272bf5..712a2661ac 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -43,8 +43,13 @@ /* * Conditions' likeliness, unlikeliness. */ -#define likely(x) __builtin_expect (!!(x), 1) -#define unlikely(x) __builtin_expect (!!(x) , 0) +#ifdef __GNUC__ +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#else /* !__GNUC__ */ +#define likely(x) (x) +#define unlikely(x) (x) +#endif /* __GNUC__ */ /* * Normally compilers store const(ant)s in ROM. Thus saving RAM. @@ -108,7 +113,11 @@ void __noreturn jerry_unreachable (const char *file, const char *function, const } \ } while (0) +#ifdef __GNUC__ #define JERRY_UNREACHABLE() __builtin_unreachable () +#else /* !__GNUC__ */ +#define JERRY_UNREACHABLE() +#endif /* __GNUC__ */ #endif /* !JERRY_NDEBUG */ /** diff --git a/jerry-libc/include/assert.h b/jerry-libc/include/assert.h index d4942a0d99..8e43849671 100644 --- a/jerry-libc/include/assert.h +++ b/jerry-libc/include/assert.h @@ -28,7 +28,7 @@ extern "C" #define assert(x) \ do \ { \ - if (__builtin_expect (!(x), 0)) \ + if (!(x)) \ { \ fprintf (stderr, "%s:%d: %s: Assertion `%s' failed.", __FILE__, __LINE__, __func__, #x); \ abort (); \