| 1 | /* @title: Compiler Functions */ |
| 2 | #pragma once |
| 3 | |
| 4 | #define __init /* Nothing for now, TODO: */ |
| 5 | |
| 6 | #define __noinline __attribute__((noinline)) |
| 7 | |
| 8 | #define __always_inline __attribute__((always_inline)) |
| 9 | |
| 10 | #define __noreturn __attribute__((noreturn)) |
| 11 | |
| 12 | #define __unused __attribute__((unused)) |
| 13 | |
| 14 | #define __warn_unused_result __attribute__((warn_unused_result)) |
| 15 | |
| 16 | #define __packed __attribute__((__packed__)) |
| 17 | |
| 18 | #define __aligned(x) __attribute__((aligned(x))) |
| 19 | |
| 20 | #define __cache_aligned __attribute__((aligned(64))) |
| 21 | |
| 22 | #define __used __attribute__((used)) |
| 23 | |
| 24 | #define __section(x) __attribute__((section(x))) |
| 25 | |
| 26 | #define __hidden __attribute__((visibility("hidden"))) |
| 27 | #define __export __attribute__((visibility("default"))) |
| 28 | |
| 29 | #define likely(x) __builtin_expect(!!(x), 1) |
| 30 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 31 | |
| 32 | #define __no_sanitize_address __attribute__((no_sanitize("address"))) |
| 33 | |
| 34 | #define __deprecated __attribute__((deprecated)) |
| 35 | #define __deprecated_msg(msg) __attribute__((deprecated(msg))) |
| 36 | |
| 37 | #define __pure __attribute__((pure)) |
| 38 | |
| 39 | #define __constfn __attribute__((const)) |
| 40 | |
| 41 | #define __noclone __attribute__((noclone)) |
| 42 | |
| 43 | #define __malloc_like __attribute__((malloc)) |
| 44 | |
| 45 | #define __nullable __attribute__((nullable)) |
| 46 | |
| 47 | #define __naked __attribute__((naked)) |
| 48 | |
| 49 | #define __returns_nonnull __attribute__((returns_nonnull)) |
| 50 | |
| 51 | #define __returns_twice __attribute__((returns_twice)) |
| 52 | |
| 53 | #define __may_alias __attribute__((__may_alias__)) |
| 54 | |
| 55 | #define __weak __attribute__((weak)) |
| 56 | |
| 57 | #define __weakref(sym) __attribute__((weakref(#sym))) |
| 58 | |
| 59 | #define __alias(sym) __attribute__((alias(#sym))) |
| 60 | |
| 61 | #define __no_sanitize_undefined __attribute__((no_sanitize("undefined"))) |
| 62 | |
| 63 | #define __no_sanitize_thread __attribute__((no_sanitize("thread"))) |
| 64 | |
| 65 | #define __no_sanitize_memory __attribute__((no_sanitize("memory"))) |
| 66 | |
| 67 | #define __no_sanitize_coverage __attribute__((no_sanitize("coverage"))) |
| 68 | |
| 69 | #define __printf_like(fmt_idx, arg_idx) \ |
| 70 | __attribute__((format(printf, fmt_idx, arg_idx))) |
| 71 | |
| 72 | #define __constructor(prio) __attribute__((constructor(prio))) |
| 73 | #define __destructor(prio) __attribute__((destructor(prio))) |
| 74 | |
| 75 | #define __fallthrough __attribute__((fallthrough)) |
| 76 | |
| 77 | #if defined(__GNUC__) |
| 78 | #define __restrict __restrict__ |
| 79 | #else |
| 80 | #define __restrict |
| 81 | #endif |
| 82 | |
| 83 | #define static_assert_1(cond) _Static_assert(cond, #cond) |
| 84 | #define static_assert_2(cond, msg) _Static_assert(cond, msg) |
| 85 | #define static_assert(...) \ |
| 86 | _DISPATCH(static_assert, PP_NARG(__VA_ARGS__))(__VA_ARGS__) |
| 87 | |
| 88 | #define static_assert_struct_size_eq(__struct, __want) \ |
| 89 | static_assert(sizeof(struct __struct) == (__want), \ |
| 90 | "sizeof(struct " #__struct ") != " #__want) |
| 91 | |
| 92 | #define smp_mb() atomic_thread_fence(memory_order_seq_cst) |
| 93 | #define smp_rmb() atomic_thread_fence(memory_order_acquire) |
| 94 | #define smp_wmb() atomic_thread_fence(memory_order_release) |
| 95 | |
| 96 | #define _DISPATCH_(name, n) name##_##n |
| 97 | #define _DISPATCH(name, n) _DISPATCH_(name, n) |
| 98 | |
| 99 | #define PP_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, \ |
| 100 | _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ |
| 101 | _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, \ |
| 102 | _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \ |
| 103 | _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, \ |
| 104 | _63, _64, N, ...) \ |
| 105 | N |
| 106 | |
| 107 | #define PP_RSEQ_N() \ |
| 108 | 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, \ |
| 109 | 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, \ |
| 110 | 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, \ |
| 111 | 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 |
| 112 | |
| 113 | #define PP_NARG_HELPER(...) PP_ARG_N(__VA_ARGS__) |
| 114 | #define PP_NARG(...) PP_NARG_HELPER(_, ##__VA_ARGS__, PP_RSEQ_N()) |
| 115 | |
| 116 | #define CONCAT_(a, b) a##b |
| 117 | #define CONCAT(a, b) CONCAT_(a, b) |
| 118 | |
| 119 | #define _UNUSED_1(a) ((void) (a)) |
| 120 | #define _UNUSED_2(a, b) ((void) (a), (void) (b)) |
| 121 | #define _UNUSED_3(a, b, c) ((void) (a), (void) (b), (void) (c)) |
| 122 | #define _UNUSED_4(a, b, c, d) ((void) (a), (void) (b), (void) (c), (void) (d)) |
| 123 | #define _UNUSED_5(a, b, c, d, e) \ |
| 124 | ((void) (a), (void) (b), (void) (c), (void) (d), (void) (e)) |
| 125 | #define _UNUSED_6(a, b, c, d, e, f) \ |
| 126 | ((void) (a), (void) (b), (void) (c), (void) (d), (void) (e), (void) (f)) |
| 127 | #define _UNUSED_7(a, b, c, d, e, f, g) \ |
| 128 | ((void) (a), (void) (b), (void) (c), (void) (d), (void) (e), (void) (f), \ |
| 129 | (void) (g)) |
| 130 | #define _UNUSED_8(a, b, c, d, e, f, g, h) \ |
| 131 | ((void) (a), (void) (b), (void) (c), (void) (d), (void) (e), (void) (f), \ |
| 132 | (void) (g), (void) (h)) |
| 133 | |
| 134 | #define unused(...) _DISPATCH(_UNUSED, PP_NARG(__VA_ARGS__))(__VA_ARGS__) |
| 135 | |
| 136 | #define __comptime_decay(x) __typeof__(0 ? (x) : (x)) |
| 137 | #define __comptime_is_str(x) \ |
| 138 | (__builtin_types_compatible_p(__comptime_decay(x), char *) || \ |
| 139 | __builtin_types_compatible_p(__comptime_decay(x), const char *)) |
| 140 | |
| 141 | #define __comptime_as_str(x) ((const char *) (uintptr_t) (x)) |
| 142 | #define __comptime_as_type(T, x) ((T) (uintptr_t) (x)) |
| 143 | |