1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef JRT_H
17#define JRT_H
18
19#include <stdio.h>
20#include <string.h>
21
22#include "config.h"
23#include "jerryscript-port.h"
24#include "jrt-types.h"
25
26/*
27 * Constants
28 */
29#define JERRY_BITSINBYTE 8
30
31/*
32 * Make sure unused parameters, variables, or expressions trigger no compiler warning.
33 */
34#define JERRY_UNUSED(x) ((void) (x))
35
36#define JERRY_UNUSED_1(_1) JERRY_UNUSED (_1)
37#define JERRY_UNUSED_2(_1, _2) JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2)
38#define JERRY_UNUSED_3(_1, _2, _3) JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3)
39#define JERRY_UNUSED_4(_1, _2, _3, _4) JERRY_UNUSED (_1), JERRY_UNUSED_3 (_2, _3, _4)
40#define JERRY_UNUSED_5(_1, _2, _3, _4, _5) JERRY_UNUSED (_1), JERRY_UNUSED_4 (_2, _3, _4, _5)
41
42#define JERRY_VA_ARGS_NUM_IMPL(_1, _2, _3, _4, _5, N, ...) N
43#define JERRY_VA_ARGS_NUM(...) JERRY_VA_ARGS_NUM_IMPL (__VA_ARGS__, 5, 4, 3, 2, 1, 0)
44
45#define JERRY_UNUSED_ALL_IMPL_(nargs) JERRY_UNUSED_ ## nargs
46#define JERRY_UNUSED_ALL_IMPL(nargs) JERRY_UNUSED_ALL_IMPL_ (nargs)
47#define JERRY_UNUSED_ALL(...) JERRY_UNUSED_ALL_IMPL (JERRY_VA_ARGS_NUM (__VA_ARGS__)) (__VA_ARGS__)
48
49/*
50 * Asserts
51 *
52 * Warning:
53 * Don't use JERRY_STATIC_ASSERT in headers, because
54 * __LINE__ may be the same for asserts in a header
55 * and in an implementation file.
56 */
57#define JERRY_STATIC_ASSERT_GLUE_(a, b, c) a ## b ## _ ## c
58#define JERRY_STATIC_ASSERT_GLUE(a, b, c) JERRY_STATIC_ASSERT_GLUE_ (a, b, c)
59#define JERRY_STATIC_ASSERT(x, msg) \
60 enum { JERRY_STATIC_ASSERT_GLUE (static_assertion_failed_, __LINE__, msg) = 1 / (!!(x)) }
61
62#ifndef JERRY_NDEBUG
63void JERRY_ATTR_NORETURN
64jerry_assert_fail (const char *assertion, const char *file, const char *function, const uint32_t line);
65void JERRY_ATTR_NORETURN
66jerry_unreachable (const char *file, const char *function, const uint32_t line);
67
68#define JERRY_ASSERT(x) \
69 do \
70 { \
71 if (JERRY_UNLIKELY (!(x))) \
72 { \
73 jerry_assert_fail (#x, __FILE__, __func__, __LINE__); \
74 } \
75 } while (0)
76
77#define JERRY_UNREACHABLE() \
78 do \
79 { \
80 jerry_unreachable (__FILE__, __func__, __LINE__); \
81 } while (0)
82#else /* JERRY_NDEBUG */
83#define JERRY_ASSERT(x) \
84 do \
85 { \
86 if (false) \
87 { \
88 JERRY_UNUSED (x); \
89 } \
90 } while (0)
91
92#ifdef __GNUC__
93#define JERRY_UNREACHABLE() __builtin_unreachable ()
94#endif /* __GNUC__ */
95
96#ifdef _MSC_VER
97#define JERRY_UNREACHABLE() _assume (0)
98#endif /* _MSC_VER */
99
100#ifndef JERRY_UNREACHABLE
101#define JERRY_UNREACHABLE()
102#endif /* !JERRY_UNREACHABLE */
103
104#endif /* !JERRY_NDEBUG */
105
106/**
107 * Exit on fatal error
108 */
109void JERRY_ATTR_NORETURN jerry_fatal (jerry_fatal_code_t code);
110
111/*
112 * Logging
113 */
114#if ENABLED (JERRY_LOGGING)
115#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
116#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
117#define JERRY_DEBUG_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
118#define JERRY_TRACE_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
119#else /* !ENABLED (JERRY_LOGGING) */
120#define JERRY_ERROR_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
121#define JERRY_WARNING_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
122#define JERRY_DEBUG_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
123#define JERRY_TRACE_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
124#endif /* ENABLED (JERRY_LOGGING) */
125
126/**
127 * Size of struct member
128 */
129#define JERRY_SIZE_OF_STRUCT_MEMBER(struct_name, member_name) sizeof (((struct_name *) NULL)->member_name)
130
131/**
132 * Aligns @a value to @a alignment. @a must be the power of 2.
133 *
134 * Returns minimum positive value, that divides @a alignment and is more than or equal to @a value
135 */
136#define JERRY_ALIGNUP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1))
137
138/*
139 * min, max
140 */
141#define JERRY_MIN(v1, v2) (((v1) < (v2)) ? (v1) : (v2))
142#define JERRY_MAX(v1, v2) (((v1) < (v2)) ? (v2) : (v1))
143
144/**
145 * Calculate the index of the first non-zero bit of a 32 bit integer value
146 */
147#define JERRY__LOG2_1(n) (((n) >= 2) ? 1 : 0)
148#define JERRY__LOG2_2(n) (((n) >= 1 << 2) ? (2 + JERRY__LOG2_1 ((n) >> 2)) : JERRY__LOG2_1 (n))
149#define JERRY__LOG2_4(n) (((n) >= 1 << 4) ? (4 + JERRY__LOG2_2 ((n) >> 4)) : JERRY__LOG2_2 (n))
150#define JERRY__LOG2_8(n) (((n) >= 1 << 8) ? (8 + JERRY__LOG2_4 ((n) >> 8)) : JERRY__LOG2_4 (n))
151#define JERRY_LOG2(n) (((n) >= 1 << 16) ? (16 + JERRY__LOG2_8 ((n) >> 16)) : JERRY__LOG2_8 (n))
152
153#endif /* !JRT_H */
154