1 /*===- InstrProfilingPort.h- Support library for PGO instrumentation ------===*\
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 /* This header must be included after all others so it can provide fallback
10    definitions for stuff missing in system headers. */
11 
12 #ifndef PROFILE_INSTRPROFILING_PORT_H_
13 #define PROFILE_INSTRPROFILING_PORT_H_
14 
15 #ifdef _MSC_VER
16 #define COMPILER_RT_ALIGNAS(x) __declspec(align(x))
17 #define COMPILER_RT_VISIBILITY
18 /* FIXME: selectany does not have the same semantics as weak. */
19 #define COMPILER_RT_WEAK __declspec(selectany)
20 /* Need to include <windows.h> */
21 #define COMPILER_RT_ALLOCA _alloca
22 /* Need to include <stdio.h> and <io.h> */
23 #define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
24 #define COMPILER_RT_ALWAYS_INLINE __forceinline
25 #define COMPILER_RT_CLEANUP(x)
26 #elif __GNUC__
27 #ifdef _WIN32
28 #define COMPILER_RT_FTRUNCATE(f, l) _chsize(fileno(f), l)
29 #define COMPILER_RT_VISIBILITY
30 #define COMPILER_RT_WEAK __attribute__((selectany))
31 #else
32 #define COMPILER_RT_FTRUNCATE(f, l) ftruncate(fileno(f), l)
33 #define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
34 #define COMPILER_RT_WEAK __attribute__((weak))
35 #endif
36 #define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
37 #define COMPILER_RT_ALLOCA __builtin_alloca
38 #define COMPILER_RT_ALWAYS_INLINE inline __attribute((always_inline))
39 #define COMPILER_RT_CLEANUP(x) __attribute__((cleanup(x)))
40 #endif
41 
42 #if defined(__APPLE__)
43 #define COMPILER_RT_SEG "__DATA,"
44 #else
45 #define COMPILER_RT_SEG ""
46 #endif
47 
48 #ifdef _MSC_VER
49 #define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
50 #else
51 #define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
52 #endif
53 
54 #define COMPILER_RT_MAX_HOSTLEN 128
55 #ifdef __ORBIS__
56 #define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
57 #else
58 #define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
59 #endif
60 
61 #if COMPILER_RT_HAS_ATOMICS == 1
62 #ifdef _WIN32
63 #include <windows.h>
64 #if defined(_MSC_VER) && _MSC_VER < 1900
65 #define snprintf _snprintf
66 #endif
67 #if defined(_WIN64)
68 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
69   (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV,      \
70                                 (LONGLONG)OldV) == (LONGLONG)OldV)
71 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
72   (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar,            \
73                                       (LONGLONG)sizeof(DomType) * PtrIncr)
74 #else /* !defined(_WIN64) */
75 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
76   (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \
77    (LONG)OldV)
78 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
79   (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar,                  \
80                                     (LONG)sizeof(DomType) * PtrIncr)
81 #endif
82 #else /* !defined(_WIN32) */
83 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
84   __sync_bool_compare_and_swap(Ptr, OldV, NewV)
85 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
86   (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)
87 #endif
88 #else /* COMPILER_RT_HAS_ATOMICS != 1 */
89 #include "InstrProfilingUtil.h"
90 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
91   lprofBoolCmpXchg((void **)Ptr, OldV, NewV)
92 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
93   (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)
94 #endif
95 
96 #if defined(_WIN32)
97 #define DIR_SEPARATOR '\\'
98 #define DIR_SEPARATOR_2 '/'
99 #else
100 #define DIR_SEPARATOR '/'
101 #endif
102 
103 #ifndef DIR_SEPARATOR_2
104 #define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
105 #else /* DIR_SEPARATOR_2 */
106 #define IS_DIR_SEPARATOR(ch)                                                   \
107   (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
108 #endif /* DIR_SEPARATOR_2 */
109 
110 #if defined(_WIN32)
111 #include <windows.h>
112 static inline size_t getpagesize() {
113   SYSTEM_INFO S;
114   GetNativeSystemInfo(&S);
115   return S.dwPageSize;
116 }
117 #else /* defined(_WIN32) */
118 #include <unistd.h>
119 #endif /* defined(_WIN32) */
120 
121 #define PROF_ERR(Format, ...)                                                  \
122   fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
123 
124 #define PROF_WARN(Format, ...)                                                 \
125   fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
126 
127 #define PROF_NOTE(Format, ...)                                                 \
128   fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
129 
130 #ifndef MAP_FILE
131 #define MAP_FILE 0
132 #endif
133 
134 #ifndef O_BINARY
135 #define O_BINARY 0
136 #endif
137 
138 #if defined(__FreeBSD__)
139 
140 #include <inttypes.h>
141 #include <sys/types.h>
142 
143 #else /* defined(__FreeBSD__) */
144 
145 #include <inttypes.h>
146 #include <stdint.h>
147 
148 #endif /* defined(__FreeBSD__) && defined(__i386__) */
149 
150 #endif /* PROFILE_INSTRPROFILING_PORT_H_ */
151