1//===-- Common internal contructs -------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_COMMON_H
10#define LLVM_LIBC_SRC___SUPPORT_COMMON_H
11
12#ifndef LIBC_NAMESPACE
13#error "LIBC_NAMESPACE macro is not defined."
14#endif
15
16#include "src/__support/macros/attributes.h"
17#include "src/__support/macros/config.h"
18#include "src/__support/macros/properties/architectures.h"
19#include "src/__support/macros/properties/compiler.h"
20
21#ifndef LLVM_LIBC_FUNCTION_ATTR
22#define LLVM_LIBC_FUNCTION_ATTR
23#endif
24
25#ifndef LLVM_LIBC_VARIABLE_ATTR
26#define LLVM_LIBC_VARIABLE_ATTR
27#endif
28
29// clang-format off
30// Allow each function `func` to have extra attributes specified by defining:
31// `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with
32// "LLVM_LIBC_EMPTY, "
33//
34// For examples:
35// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, [[gnu::weak]]
36// #define LLVM_LIBC_FUNCTION_ATTR_memchr LLVM_LIBC_EMPTY, [[gnu::weak]] [[gnu::visibility("default")]]
37// clang-format on
38#define LLVM_LIBC_EMPTY
39
40#define GET_SECOND(first, second, ...) second
41#define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY)
42
43#define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
44
45// At the moment, [[gnu::alias()]] is not supported on MacOS, and it is needed
46// to cleanly export and alias the C++ symbol `LIBC_NAMESPACE::func` with the C
47// symbol `func`. So for public packaging on MacOS, we will only export the C
48// symbol. Moreover, a C symbol `func` in macOS is mangled as `_func`.
49#if defined(LIBC_COPT_PUBLIC_PACKAGING) && !defined(LIBC_COMPILER_IS_MSVC)
50#ifndef __APPLE__
51#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
52 LLVM_LIBC_ATTR(name) \
53 LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) \
54 __##name##_impl__ asm(#name); \
55 decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]]; \
56 type __##name##_impl__ arglist
57#else // __APPLE__
58#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
59 LLVM_LIBC_ATTR(name) \
60 LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) name asm("_" #name); \
61 type name arglist
62#endif // __APPLE__
63#else // LIBC_COPT_PUBLIC_PACKAGING
64#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
65#endif // LIBC_COPT_PUBLIC_PACKAGING
66
67// This extra layer of macro allows `name` to be a macro to rename a function.
68#define LLVM_LIBC_FUNCTION(type, name, arglist) \
69 LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
70
71// At the moment, [[gnu::alias()]] is not supported on MacOS, and it is needed
72// to cleanly export and alias the C++ symbol `LIBC_NAMESPACE::func` with the C
73// symbol `func`. So for public packaging on MacOS, we will only export the C
74// symbol. Moreover, a C symbol `func` in macOS is mangled as `_func`.
75#if defined(LIBC_COPT_PUBLIC_PACKAGING) && !defined(LIBC_COMPILER_IS_MSVC)
76#ifndef __APPLE__
77#define LLVM_LIBC_VARIABLE_IMPL(type, name) \
78 LLVM_LIBC_ATTR(name) \
79 extern LLVM_LIBC_VARIABLE_ATTR decltype(LIBC_NAMESPACE::name) \
80 __##name##_impl__ asm(#name); \
81 extern decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]]; \
82 type __##name##_impl__
83#else // __APPLE__
84#define LLVM_LIBC_VARIABLE_IMPL(type, name) \
85 LLVM_LIBC_ATTR(name) \
86 extern LLVM_LIBC_VARIABLE_ATTR decltype(LIBC_NAMESPACE::name) name asm( \
87 "_" #name); \
88 type name
89#endif // __APPLE__
90#else // LIBC_COPT_PUBLIC_PACKAGING
91#define LLVM_LIBC_VARIABLE_IMPL(type, name) type name
92#endif // LIBC_COPT_PUBLIC_PACKAGING
93
94#define LLVM_LIBC_VARIABLE(type, name) LLVM_LIBC_VARIABLE_IMPL(type, name)
95
96namespace LIBC_NAMESPACE_DECL {
97namespace internal {
98LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
99 for (; *lhs || *rhs; ++lhs, ++rhs)
100 if (*lhs != *rhs)
101 return false;
102 return true;
103}
104} // namespace internal
105} // namespace LIBC_NAMESPACE_DECL
106
107#define __LIBC_MACRO_TO_STRING(str) #str
108#define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)
109
110// LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
111// Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
112//
113// This works by comparing the stringified version of the macro with and without
114// evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO
115// is defined, one stringification yields "FOO" while the other yields its
116// stringified value "1".
117#define LLVM_LIBC_IS_DEFINED(macro) \
118 !LIBC_NAMESPACE::internal::same_string( \
119 LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)
120#define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s
121
122#endif // LLVM_LIBC_SRC___SUPPORT_COMMON_H
123