Skip to content

Commit 66a42d8

Browse files
committed
[NFC] Move some of the runtime's compiler abstraction into Compiler.h
1 parent e100449 commit 66a42d8

File tree

2 files changed

+55
-48
lines changed

2 files changed

+55
-48
lines changed

include/swift/Basic/Compiler.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,59 @@
3434
#define SWIFT_ASSUME(x)
3535
#endif
3636

37+
/// Attributes.
38+
3739
#if __has_attribute(constructor)
3840
#define SWIFT_CONSTRUCTOR __attribute__((constructor))
3941
#else
4042
#define SWIFT_CONSTRUCTOR
4143
#endif
4244

45+
/// \macro SWIFT_GNUC_PREREQ
46+
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
47+
/// available.
48+
#ifndef SWIFT_GNUC_PREREQ
49+
# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
50+
# define SWIFT_GNUC_PREREQ(maj, min, patch) \
51+
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
52+
((maj) << 20) + ((min) << 10) + (patch))
53+
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
54+
# define SWIFT_GNUC_PREREQ(maj, min, patch) \
55+
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
56+
# else
57+
# define SWIFT_GNUC_PREREQ(maj, min, patch) 0
58+
# endif
59+
#endif
60+
61+
62+
/// SWIFT_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
63+
/// mark a method "not for inlining".
64+
#if __has_attribute(noinline) || SWIFT_GNUC_PREREQ(3, 4, 0)
65+
#define SWIFT_ATTRIBUTE_NOINLINE __attribute__((noinline))
66+
#elif defined(_MSC_VER)
67+
#define SWIFT_ATTRIBUTE_NOINLINE __declspec(noinline)
68+
#else
69+
#define SWIFT_ATTRIBUTE_NOINLINE
70+
#endif
71+
72+
/// SWIFT_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
73+
/// so, mark a method "always inline" because it is performance sensitive. GCC
74+
/// 3.4 supported this but is buggy in various cases and produces unimplemented
75+
/// errors, just use it in GCC 4.0 and later.
76+
#if __has_attribute(always_inline) || SWIFT_GNUC_PREREQ(4, 0, 0)
77+
#define SWIFT_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
78+
#elif defined(_MSC_VER)
79+
#define SWIFT_ATTRIBUTE_ALWAYS_INLINE __forceinline
80+
#else
81+
#define SWIFT_ATTRIBUTE_ALWAYS_INLINE
82+
#endif
83+
84+
#ifdef __GNUC__
85+
#define SWIFT_ATTRIBUTE_NORETURN __attribute__((noreturn))
86+
#elif defined(_MSC_VER)
87+
#define SWIFT_ATTRIBUTE_NORETURN __declspec(noreturn)
88+
#else
89+
#define SWIFT_ATTRIBUTE_NORETURN
90+
#endif
91+
4392
#endif // SWIFT_BASIC_COMPILER_H

include/swift/Runtime/Config.h

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,29 @@
1717
#ifndef SWIFT_RUNTIME_CONFIG_H
1818
#define SWIFT_RUNTIME_CONFIG_H
1919

20+
#include "swift/Basic/Compiler.h"
2021
#include "swift/Runtime/CMakeConfig.h"
2122

22-
/// \macro SWIFT_RUNTIME_GNUC_PREREQ
23-
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
24-
/// available.
25-
#ifndef SWIFT_RUNTIME_GNUC_PREREQ
26-
# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
27-
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) \
28-
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
29-
((maj) << 20) + ((min) << 10) + (patch))
30-
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
31-
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) \
32-
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
33-
# else
34-
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) 0
35-
# endif
36-
#endif
37-
3823
/// SWIFT_RUNTIME_LIBRARY_VISIBILITY - If a class marked with this attribute is
3924
/// linked into a shared library, then the class should be private to the
4025
/// library and not accessible from outside it. Can also be used to mark
4126
/// variables and functions, making them private to any shared library they are
4227
/// linked into.
4328
/// On PE/COFF targets, library visibility is the default, so this isn't needed.
44-
#if (__has_attribute(visibility) || SWIFT_RUNTIME_GNUC_PREREQ(4, 0, 0)) && \
29+
#if (__has_attribute(visibility) || SWIFT_GNUC_PREREQ(4, 0, 0)) && \
4530
!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
4631
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
4732
#else
4833
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY
4934
#endif
5035

51-
/// Attributes.
52-
/// SWIFT_RUNTIME_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
53-
/// mark a method "not for inlining".
54-
#if __has_attribute(noinline) || SWIFT_RUNTIME_GNUC_PREREQ(3, 4, 0)
55-
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE __attribute__((noinline))
56-
#elif defined(_MSC_VER)
57-
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE __declspec(noinline)
58-
#else
59-
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
60-
#endif
61-
62-
/// SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
63-
/// so, mark a method "always inline" because it is performance sensitive. GCC
64-
/// 3.4 supported this but is buggy in various cases and produces unimplemented
65-
/// errors, just use it in GCC 4.0 and later.
66-
#if __has_attribute(always_inline) || SWIFT_RUNTIME_GNUC_PREREQ(4, 0, 0)
67-
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
68-
#elif defined(_MSC_VER)
69-
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE __forceinline
70-
#else
71-
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
72-
#endif
73-
74-
#ifdef __GNUC__
75-
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN __attribute__((noreturn))
76-
#elif defined(_MSC_VER)
77-
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN __declspec(noreturn)
78-
#else
79-
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN
80-
#endif
36+
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE SWIFT_ATTRIBUTE_NOINLINE
37+
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE SWIFT_ATTRIBUTE_ALWAYS_INLINE
38+
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_ATTRIBUTE_NORETURN
8139

8240
/// SWIFT_RUNTIME_BUILTIN_TRAP - On compilers which support it, expands to an expression
8341
/// which causes the program to exit abnormally.
84-
#if __has_builtin(__builtin_trap) || SWIFT_RUNTIME_GNUC_PREREQ(4, 3, 0)
42+
#if __has_builtin(__builtin_trap) || SWIFT_GNUC_PREREQ(4, 3, 0)
8543
# define SWIFT_RUNTIME_BUILTIN_TRAP __builtin_trap()
8644
#elif defined(_MSC_VER)
8745
// The __debugbreak intrinsic is supported by MSVC, does not require forward

0 commit comments

Comments
 (0)