|
24 | 24 | #include "jerry-port.h" |
25 | 25 | #include "jrt-types.h" |
26 | 26 |
|
27 | | -/** |
| 27 | +/* |
28 | 28 | * Attributes |
29 | 29 | */ |
30 | 30 | #define __noreturn __attribute__((noreturn)) |
|
40 | 40 | # define __attr_pure___ __attribute__((pure)) |
41 | 41 | #endif /* !__attr_pure___ */ |
42 | 42 |
|
43 | | -/** |
| 43 | +/* |
| 44 | + * Conditions' likeliness, unlikeliness. |
| 45 | + */ |
| 46 | +#define likely(x) __builtin_expect (!!(x), 1) |
| 47 | +#define unlikely(x) __builtin_expect (!!(x) , 0) |
| 48 | + |
| 49 | +/* |
44 | 50 | * Normally compilers store const(ant)s in ROM. Thus saving RAM. |
45 | 51 | * But if your compiler does not support it then the directive below can force it. |
46 | 52 | * |
|
51 | 57 | # define JERRY_CONST_DATA |
52 | 58 | #endif /* JERRY_CONST_DATA */ |
53 | 59 |
|
54 | | -#ifndef __GNUC__ |
55 | | -#define __extension__ |
56 | | -#endif /* !__GNUC__ */ |
57 | | - |
58 | | -/** |
| 60 | +/* |
59 | 61 | * Constants |
60 | 62 | */ |
61 | 63 | #define JERRY_BITSINBYTE 8 |
62 | 64 |
|
63 | | -/** |
| 65 | +/* |
| 66 | + * Make sure unused parameters, variables, or expressions trigger no compiler warning. |
| 67 | + */ |
| 68 | +#define JERRY_UNUSED(x) ((void) (x)) |
| 69 | + |
| 70 | +/* |
64 | 71 | * Asserts |
65 | 72 | * |
66 | 73 | * Warning: |
|
73 | 80 | #define JERRY_STATIC_ASSERT(x, msg) \ |
74 | 81 | enum { JERRY_STATIC_ASSERT_GLUE (static_assertion_failed_, __LINE__, msg) = 1 / (!!(x)) } |
75 | 82 |
|
76 | | -/** |
77 | | - * Variable that must not be referenced. |
78 | | - * |
79 | | - * May be used for static assertion checks. |
80 | | - */ |
81 | | -extern uint32_t jerry_unreferenced_expression; |
82 | | - |
83 | | -extern void __noreturn jerry_assert_fail (const char *, const char *, const char *, const uint32_t); |
84 | | -extern void __noreturn jerry_unreachable (const char *, const char *, const char *, const uint32_t); |
85 | | -extern void __noreturn jerry_unimplemented (const char *, const char *, const char *, const uint32_t); |
86 | | - |
87 | 83 | #ifndef JERRY_NDEBUG |
88 | | -#define JERRY_ASSERT(x) do { if (__builtin_expect (!(x), 0)) { \ |
89 | | - jerry_assert_fail (#x, __FILE__, __func__, __LINE__); } } while (0) |
90 | | -#else /* JERRY_NDEBUG */ |
91 | | -#define JERRY_ASSERT(x) do { if (false) { (void)(x); } } while (0) |
92 | | -#endif /* !JERRY_NDEBUG */ |
93 | | - |
94 | | -#define JERRY_UNUSED(x) ((void) (x)) |
| 84 | +extern void __noreturn jerry_assert_fail (const char *, const char *, const char *, const uint32_t); |
| 85 | +extern void __noreturn jerry_unreachable (const char *, const char *, const uint32_t); |
95 | 86 |
|
96 | | -#ifdef JERRY_ENABLE_LOG |
97 | | -#define JERRY_DLOG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__) |
98 | | -#define JERRY_DDLOG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__) |
99 | | -#else /* !JERRY_ENABLE_LOG */ |
100 | | -#define JERRY_DLOG(...) \ |
| 87 | +#define JERRY_ASSERT(x) \ |
101 | 88 | do \ |
102 | 89 | { \ |
103 | | - if (false) \ |
| 90 | + if (unlikely (!(x))) \ |
104 | 91 | { \ |
105 | | - jerry_ref_unused_variables (0, __VA_ARGS__); \ |
| 92 | + jerry_assert_fail (#x, __FILE__, __func__, __LINE__); \ |
106 | 93 | } \ |
107 | 94 | } while (0) |
108 | | -#define JERRY_DDLOG(...) JERRY_DLOG (__VA_ARGS__) |
109 | | -#endif /* JERRY_ENABLE_LOG */ |
110 | | - |
111 | | -#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__) |
112 | | -#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__) |
113 | 95 |
|
114 | | -/** |
115 | | - * Mark for unreachable points and unimplemented cases |
116 | | - */ |
117 | | -extern void jerry_ref_unused_variables (void *, ...); |
118 | | - |
119 | | -#ifndef JERRY_NDEBUG |
120 | 96 | #define JERRY_UNREACHABLE() \ |
121 | 97 | do \ |
122 | 98 | { \ |
123 | | - jerry_unreachable (NULL, __FILE__, __func__, __LINE__); \ |
| 99 | + jerry_unreachable (__FILE__, __func__, __LINE__); \ |
124 | 100 | } while (0) |
125 | | - |
126 | | -#define JERRY_UNIMPLEMENTED(comment) \ |
127 | | - do \ |
128 | | - { \ |
129 | | - jerry_unimplemented (comment, __FILE__, __func__, __LINE__); \ |
130 | | - } while (0) |
131 | | - |
132 | | -#define JERRY_UNIMPLEMENTED_REF_UNUSED_VARS(comment, ...) \ |
| 101 | +#else /* JERRY_NDEBUG */ |
| 102 | +#define JERRY_ASSERT(x) \ |
133 | 103 | do \ |
134 | 104 | { \ |
135 | 105 | if (false) \ |
136 | 106 | { \ |
137 | | - jerry_ref_unused_variables (0, __VA_ARGS__); \ |
| 107 | + JERRY_UNUSED (x); \ |
138 | 108 | } \ |
139 | | - jerry_unimplemented (comment, __FILE__, __func__, __LINE__); \ |
140 | | - } while (0) |
141 | | -#else /* JERRY_NDEBUG */ |
142 | | -#define JERRY_UNREACHABLE() \ |
143 | | - do \ |
144 | | - { \ |
145 | | - jerry_unreachable (NULL, NULL, NULL, 0); \ |
146 | 109 | } while (0) |
147 | 110 |
|
148 | | -#define JERRY_UNIMPLEMENTED(comment) \ |
149 | | - do \ |
150 | | - { \ |
151 | | - jerry_unimplemented (comment, NULL, NULL, 0); \ |
152 | | - } while (0) |
| 111 | +#define JERRY_UNREACHABLE() __builtin_unreachable () |
| 112 | +#endif /* !JERRY_NDEBUG */ |
| 113 | + |
| 114 | +/** |
| 115 | + * Exit on fatal error |
| 116 | + */ |
| 117 | +extern void __noreturn jerry_fatal (jerry_fatal_code_t); |
| 118 | + |
| 119 | +/* |
| 120 | + * Logging |
| 121 | + */ |
| 122 | +#ifdef JERRY_ENABLE_LOG |
| 123 | +#define JERRY_DLOG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__) |
| 124 | +#define JERRY_DDLOG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__) |
| 125 | +#else /* !JERRY_ENABLE_LOG */ |
| 126 | +/** |
| 127 | + * Mark for unreachable points and unimplemented cases |
| 128 | + */ |
| 129 | +extern void jerry_ref_unused_variables (void *, ...); |
153 | 130 |
|
154 | | -#define JERRY_UNIMPLEMENTED_REF_UNUSED_VARS(comment, ...) \ |
| 131 | +#define JERRY_DLOG(...) \ |
155 | 132 | do \ |
156 | 133 | { \ |
157 | 134 | if (false) \ |
158 | 135 | { \ |
159 | 136 | jerry_ref_unused_variables (0, __VA_ARGS__); \ |
160 | 137 | } \ |
161 | | - jerry_unimplemented (comment, NULL, NULL, 0); \ |
162 | 138 | } while (0) |
163 | | -#endif /* !JERRY_NDEBUG */ |
164 | | - |
165 | | -/** |
166 | | - * Conditions' likeliness, unlikeliness. |
167 | | - */ |
168 | | -#define likely(x) __builtin_expect (!!(x), 1) |
169 | | -#define unlikely(x) __builtin_expect (!!(x) , 0) |
| 139 | +#define JERRY_DDLOG(...) JERRY_DLOG (__VA_ARGS__) |
| 140 | +#endif /* JERRY_ENABLE_LOG */ |
170 | 141 |
|
171 | | -/** |
172 | | - * Exit |
173 | | - */ |
174 | | -extern void __noreturn jerry_fatal (jerry_fatal_code_t); |
| 142 | +#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__) |
| 143 | +#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__) |
175 | 144 |
|
176 | 145 | /** |
177 | | - * sizeof, offsetof, ... |
| 146 | + * Size of struct member |
178 | 147 | */ |
179 | 148 | #define JERRY_SIZE_OF_STRUCT_MEMBER(struct_name, member_name) sizeof (((struct_name *) NULL)->member_name) |
180 | 149 |
|
181 | | -/** |
182 | | - * Alignment |
183 | | - */ |
184 | | - |
185 | 150 | /** |
186 | 151 | * Aligns @a value to @a alignment. @a must be the power of 2. |
187 | 152 | * |
188 | 153 | * Returns minimum positive value, that divides @a alignment and is more than or equal to @a value |
189 | 154 | */ |
190 | 155 | #define JERRY_ALIGNUP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1)) |
191 | 156 |
|
192 | | -/** |
| 157 | +/* |
193 | 158 | * min, max |
194 | 159 | */ |
195 | 160 | #define JERRY_MIN(v1, v2) (((v1) < (v2)) ? (v1) : (v2)) |
196 | 161 | #define JERRY_MAX(v1, v2) (((v1) < (v2)) ? (v2) : (v1)) |
197 | 162 |
|
198 | | - |
199 | 163 | extern bool jrt_read_from_buffer_by_offset (const uint8_t *, size_t, size_t *, void *, size_t); |
200 | | - |
201 | 164 | extern bool jrt_write_to_buffer_by_offset (uint8_t *, size_t, size_t *, const void *, size_t); |
202 | 165 |
|
203 | 166 | #endif /* !JRT_H */ |
0 commit comments