1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef JRT_H
17425bb815Sopenharmony_ci#define JRT_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#include <stdio.h>
20425bb815Sopenharmony_ci#include <string.h>
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci#include "config.h"
23425bb815Sopenharmony_ci#include "jerryscript-port.h"
24425bb815Sopenharmony_ci#include "jrt-types.h"
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_ci/*
27425bb815Sopenharmony_ci * Constants
28425bb815Sopenharmony_ci */
29425bb815Sopenharmony_ci#define JERRY_BITSINBYTE 8
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_ci/*
32425bb815Sopenharmony_ci * Make sure unused parameters, variables, or expressions trigger no compiler warning.
33425bb815Sopenharmony_ci */
34425bb815Sopenharmony_ci#define JERRY_UNUSED(x) ((void) (x))
35425bb815Sopenharmony_ci
36425bb815Sopenharmony_ci#define JERRY_UNUSED_1(_1)                 JERRY_UNUSED (_1)
37425bb815Sopenharmony_ci#define JERRY_UNUSED_2(_1, _2)             JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2)
38425bb815Sopenharmony_ci#define JERRY_UNUSED_3(_1, _2, _3)         JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3)
39425bb815Sopenharmony_ci#define JERRY_UNUSED_4(_1, _2, _3, _4)     JERRY_UNUSED (_1), JERRY_UNUSED_3 (_2, _3, _4)
40425bb815Sopenharmony_ci#define JERRY_UNUSED_5(_1, _2, _3, _4, _5) JERRY_UNUSED (_1), JERRY_UNUSED_4 (_2, _3, _4, _5)
41425bb815Sopenharmony_ci
42425bb815Sopenharmony_ci#define JERRY_VA_ARGS_NUM_IMPL(_1, _2, _3, _4, _5, N, ...) N
43425bb815Sopenharmony_ci#define JERRY_VA_ARGS_NUM(...) JERRY_VA_ARGS_NUM_IMPL (__VA_ARGS__, 5, 4, 3, 2, 1, 0)
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_ci#define JERRY_UNUSED_ALL_IMPL_(nargs) JERRY_UNUSED_ ## nargs
46425bb815Sopenharmony_ci#define JERRY_UNUSED_ALL_IMPL(nargs) JERRY_UNUSED_ALL_IMPL_ (nargs)
47425bb815Sopenharmony_ci#define JERRY_UNUSED_ALL(...) JERRY_UNUSED_ALL_IMPL (JERRY_VA_ARGS_NUM (__VA_ARGS__)) (__VA_ARGS__)
48425bb815Sopenharmony_ci
49425bb815Sopenharmony_ci/*
50425bb815Sopenharmony_ci * Asserts
51425bb815Sopenharmony_ci *
52425bb815Sopenharmony_ci * Warning:
53425bb815Sopenharmony_ci *         Don't use JERRY_STATIC_ASSERT in headers, because
54425bb815Sopenharmony_ci *         __LINE__ may be the same for asserts in a header
55425bb815Sopenharmony_ci *         and in an implementation file.
56425bb815Sopenharmony_ci */
57425bb815Sopenharmony_ci#define JERRY_STATIC_ASSERT_GLUE_(a, b, c) a ## b ## _ ## c
58425bb815Sopenharmony_ci#define JERRY_STATIC_ASSERT_GLUE(a, b, c) JERRY_STATIC_ASSERT_GLUE_ (a, b, c)
59425bb815Sopenharmony_ci#define JERRY_STATIC_ASSERT(x, msg) \
60425bb815Sopenharmony_ci  enum { JERRY_STATIC_ASSERT_GLUE (static_assertion_failed_, __LINE__, msg) = 1 / (!!(x)) }
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ci#ifndef JERRY_NDEBUG
63425bb815Sopenharmony_civoid JERRY_ATTR_NORETURN
64425bb815Sopenharmony_cijerry_assert_fail (const char *assertion, const char *file, const char *function, const uint32_t line);
65425bb815Sopenharmony_civoid JERRY_ATTR_NORETURN
66425bb815Sopenharmony_cijerry_unreachable (const char *file, const char *function, const uint32_t line);
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ci#define JERRY_ASSERT(x) \
69425bb815Sopenharmony_ci  do \
70425bb815Sopenharmony_ci  { \
71425bb815Sopenharmony_ci    if (JERRY_UNLIKELY (!(x))) \
72425bb815Sopenharmony_ci    { \
73425bb815Sopenharmony_ci      jerry_assert_fail (#x, __FILE__, __func__, __LINE__); \
74425bb815Sopenharmony_ci    } \
75425bb815Sopenharmony_ci  } while (0)
76425bb815Sopenharmony_ci
77425bb815Sopenharmony_ci#define JERRY_UNREACHABLE() \
78425bb815Sopenharmony_ci  do \
79425bb815Sopenharmony_ci  { \
80425bb815Sopenharmony_ci    jerry_unreachable (__FILE__, __func__, __LINE__); \
81425bb815Sopenharmony_ci  } while (0)
82425bb815Sopenharmony_ci#else /* JERRY_NDEBUG */
83425bb815Sopenharmony_ci#define JERRY_ASSERT(x) \
84425bb815Sopenharmony_ci  do \
85425bb815Sopenharmony_ci  { \
86425bb815Sopenharmony_ci    if (false) \
87425bb815Sopenharmony_ci    { \
88425bb815Sopenharmony_ci      JERRY_UNUSED (x); \
89425bb815Sopenharmony_ci    } \
90425bb815Sopenharmony_ci  } while (0)
91425bb815Sopenharmony_ci
92425bb815Sopenharmony_ci#ifdef __GNUC__
93425bb815Sopenharmony_ci#define JERRY_UNREACHABLE() __builtin_unreachable ()
94425bb815Sopenharmony_ci#endif /* __GNUC__ */
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_ci#ifdef _MSC_VER
97425bb815Sopenharmony_ci#define JERRY_UNREACHABLE()  _assume (0)
98425bb815Sopenharmony_ci#endif /* _MSC_VER */
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ci#ifndef JERRY_UNREACHABLE
101425bb815Sopenharmony_ci#define JERRY_UNREACHABLE()
102425bb815Sopenharmony_ci#endif /* !JERRY_UNREACHABLE */
103425bb815Sopenharmony_ci
104425bb815Sopenharmony_ci#endif /* !JERRY_NDEBUG */
105425bb815Sopenharmony_ci
106425bb815Sopenharmony_ci/**
107425bb815Sopenharmony_ci * Exit on fatal error
108425bb815Sopenharmony_ci */
109425bb815Sopenharmony_civoid JERRY_ATTR_NORETURN jerry_fatal (jerry_fatal_code_t code);
110425bb815Sopenharmony_ci
111425bb815Sopenharmony_ci/*
112425bb815Sopenharmony_ci * Logging
113425bb815Sopenharmony_ci */
114425bb815Sopenharmony_ci#if ENABLED (JERRY_LOGGING)
115425bb815Sopenharmony_ci#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
116425bb815Sopenharmony_ci#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
117425bb815Sopenharmony_ci#define JERRY_DEBUG_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
118425bb815Sopenharmony_ci#define JERRY_TRACE_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
119425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_LOGGING) */
120425bb815Sopenharmony_ci#define JERRY_ERROR_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
121425bb815Sopenharmony_ci#define JERRY_WARNING_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
122425bb815Sopenharmony_ci#define JERRY_DEBUG_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
123425bb815Sopenharmony_ci#define JERRY_TRACE_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0)
124425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_LOGGING) */
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci/**
127425bb815Sopenharmony_ci * Size of struct member
128425bb815Sopenharmony_ci */
129425bb815Sopenharmony_ci#define JERRY_SIZE_OF_STRUCT_MEMBER(struct_name, member_name) sizeof (((struct_name *) NULL)->member_name)
130425bb815Sopenharmony_ci
131425bb815Sopenharmony_ci/**
132425bb815Sopenharmony_ci * Aligns @a value to @a alignment. @a must be the power of 2.
133425bb815Sopenharmony_ci *
134425bb815Sopenharmony_ci * Returns minimum positive value, that divides @a alignment and is more than or equal to @a value
135425bb815Sopenharmony_ci */
136425bb815Sopenharmony_ci#define JERRY_ALIGNUP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1))
137425bb815Sopenharmony_ci
138425bb815Sopenharmony_ci/*
139425bb815Sopenharmony_ci * min, max
140425bb815Sopenharmony_ci */
141425bb815Sopenharmony_ci#define JERRY_MIN(v1, v2) (((v1) < (v2)) ? (v1) : (v2))
142425bb815Sopenharmony_ci#define JERRY_MAX(v1, v2) (((v1) < (v2)) ? (v2) : (v1))
143425bb815Sopenharmony_ci
144425bb815Sopenharmony_ci/**
145425bb815Sopenharmony_ci * Calculate the index of the first non-zero bit of a 32 bit integer value
146425bb815Sopenharmony_ci */
147425bb815Sopenharmony_ci#define JERRY__LOG2_1(n) (((n) >= 2) ? 1 : 0)
148425bb815Sopenharmony_ci#define JERRY__LOG2_2(n) (((n) >= 1 << 2) ? (2 + JERRY__LOG2_1 ((n) >> 2)) : JERRY__LOG2_1 (n))
149425bb815Sopenharmony_ci#define JERRY__LOG2_4(n) (((n) >= 1 << 4) ? (4 + JERRY__LOG2_2 ((n) >> 4)) : JERRY__LOG2_2 (n))
150425bb815Sopenharmony_ci#define JERRY__LOG2_8(n) (((n) >= 1 << 8) ? (8 + JERRY__LOG2_4 ((n) >> 8)) : JERRY__LOG2_4 (n))
151425bb815Sopenharmony_ci#define JERRY_LOG2(n) (((n) >= 1 << 16) ? (16 + JERRY__LOG2_8 ((n) >> 16)) : JERRY__LOG2_8 (n))
152425bb815Sopenharmony_ci
153425bb815Sopenharmony_ci#endif /* !JRT_H */
154