Remove typedef decl errors
[hiphop-php.git] / hphp / util / portability.h
blob6eedff03f8aa2cc01e290024fcda9428a0312937
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_PORTABILITY_H_
17 #define incl_HPHP_PORTABILITY_H_
19 #include <folly/Likely.h> // defining LIKELY/UNLIKELY is part of this header
20 #include <folly/Portability.h>
21 #include <folly/CPortability.h> // FOLLY_DISABLE_ADDRESS_SANITIZER, FOLLY_EXPORT
23 //////////////////////////////////////////////////////////////////////
26 * Various macros to make certain things conditional on either
27 * compiler or architecture.
29 * Currently we don't *really* compile on anything other than gcc or
30 * sometimes clang, and there are some parts of the code using
31 * __attribute__ stuff directly, but some things go through these
32 * macros to make it maybe easier to change later.
35 //////////////////////////////////////////////////////////////////////
37 // TODO: does clang define __GNUC__ ?
38 #ifndef __GNUC__
39 # define __attribute__(x)
40 #endif
42 //////////////////////////////////////////////////////////////////////
44 #ifdef ATTRIBUTE_UNUSED
45 # undef ATTRIBUTE_UNUSED
46 #endif
47 #ifdef ATTRIBUTE_PRINTF
48 # undef ATTRIBUTE_PRINTF
49 #endif
50 #ifdef ATTRIBUTE_PRINTF_STRING
51 # undef ATTRIBUTE_PRINTF_STRING
52 #endif
54 #define ATTRIBUTE_PRINTF_STRING FOLLY_PRINTF_FORMAT
56 #ifdef _MSC_VER
57 #define ATTRIBUTE_PRINTF(a1, a2)
58 #ifndef __thread
59 # define __thread __declspec(thread)
60 #endif
61 #define ATTRIBUTE_UNUSED
62 #define ATTRIBUTE_USED
64 #define ALWAYS_INLINE __forceinline
65 #define EXTERNALLY_VISIBLE
66 #define FLATTEN
67 #define NEVER_INLINE __declspec(noinline)
68 #define UNUSED
70 #else
71 #define ATTRIBUTE_PRINTF(a1, a2) \
72 __attribute__((__format__ (__printf__, a1, a2)))
73 #define ATTRIBUTE_UNUSED __attribute__((__unused__))
74 #define ATTRIBUTE_USED __attribute__((__used__))
75 #ifndef NDEBUG
76 # define FLATTEN /*nop*/
77 # define ALWAYS_INLINE inline
78 # define INLINE_FLATTEN inline
79 #else
80 # define FLATTEN __attribute__((__flatten__))
81 # define ALWAYS_INLINE inline __attribute__((__always_inline__))
82 # define INLINE_FLATTEN inline __attribute__((__always_inline__,__flatten__))
83 #endif
84 #define NEVER_INLINE __attribute__((__noinline__))
85 #define UNUSED __attribute__((__unused__))
87 #endif
89 #ifdef __clang__
90 #define NO_OPT [[clang::optnone]]
91 #define EXTERNALLY_VISIBLE ATTRIBUTE_USED FOLLY_EXPORT
92 #else
93 #define NO_OPT __attribute__((__optimize__("O0")))
94 #define EXTERNALLY_VISIBLE __attribute__((__externally_visible__))
95 #endif
97 #if defined(__GNUC__)
98 # define HHVM_ATTRIBUTE_WEAK __attribute__((__weak__))
99 #elif defined(__clang__)
100 # define HHVM_ATTRIBUTE_WEAK __attribute__((__weak_import__))
101 #else
102 # define HHVM_ATTRIBUTE_WEAK
103 #endif
105 #ifndef NDEBUG
106 # define DEBUG_ONLY /* nop */
107 #else
108 # define DEBUG_ONLY UNUSED
109 #endif
113 * AARCH64 needs to create a walkable stack frame for
114 * getFrameRegs() when a FixupEntry isIndirect()
116 #ifdef __aarch64__
117 #define AARCH64_WALKABLE_FRAME() asm("" ::: "memory");
118 #else
119 #define AARCH64_WALKABLE_FRAME()
120 #endif
124 * We need to keep some unreferenced functions from being removed by
125 * the linker. There is no compile time mechanism for doing this, but
126 * by putting them in the same section as some other, referenced function
127 * in the same file, we can keep them around.
129 * So this macro should be used to mark at least one function that is
130 * referenced, and other functions that are not referenced in the same
131 * file.
133 * Note: this may not work properly with LTO. We'll revisit when/if we
134 * move to it.
136 #ifndef __APPLE__
137 # define KEEP_SECTION \
138 __attribute__((__section__(".text.keep")))
139 #else
140 # define KEEP_SECTION \
141 __attribute__((__section__(".text,.text.keep")))
142 #endif
144 #if defined(__APPLE__)
145 // OS X has a macro "isset" defined in this header. Force the include so we can
146 // make sure the macro gets undef'd. (I think this also applies to BSD, but we
147 // can cross that road when we come to it.)
148 # include <sys/param.h>
149 # ifdef isset
150 # undef isset
151 # endif
152 #endif
154 //////////////////////////////////////////////////////////////////////
155 // DECLARE_FRAME_POINTER
157 #if defined(__x86_64__)
159 # define DECLARE_FRAME_POINTER(fp) \
160 auto const fp = (ActRec*) __builtin_frame_address(0)
161 # define FRAME_POINTER_IS_ACCURATE
163 #elif defined(_M_X64)
165 // TODO: FIXME! Without this implemented properly, the JIT
166 // will fail "pretty spectacularly".
167 # define DECLARE_FRAME_POINTER(fp) \
168 always_assert(false); \
169 register ActRec* fp = nullptr;
171 #elif defined(__AARCH64EL__)
173 # if defined(__clang__)
174 # define DECLARE_FRAME_POINTER(fp) register ActRec* fp = (ActRec*) \
175 __builtin_frame_address(0)
176 #else
177 # define DECLARE_FRAME_POINTER(fp) register ActRec* fp asm("x29")
178 #endif
180 #elif defined(__powerpc64__)
182 # if defined(__clang__)
183 # error Clang implementation not done for PPC64
184 # endif
185 # define DECLARE_FRAME_POINTER(fp) \
186 auto const fp = (ActRec*) __builtin_frame_address(0)
187 # define FRAME_POINTER_IS_ACCURATE
189 #else
191 # error What are the stack and frame pointers called on your architecture?
193 #endif
195 //////////////////////////////////////////////////////////////////////
196 // CALLEE_SAVED_BARRIER
198 #ifdef _MSC_VER
199 // Unfortunately, we have no way to tell MSVC to do this, so we'll
200 // probably have to use a pair of assembly stubs to manage this.
201 #define CALLEE_SAVED_BARRIER() always_assert(false);
202 #elif defined (__powerpc64__)
203 // After gcc 5.4.1 we can't clobber r30 on PPC64 anymore because it's used as
204 // PIC register.
205 #if __GNUC__ > 5 || (__GNUC__ == 5 && (__GNUC_MINOR__ >= 4) && \
206 (__GNUC_PATCHLEVEL__ >= 1))
207 #define CALLEE_SAVED_BARRIER()\
208 asm volatile("" : : : "r2", "r14", "r15", "r16", "r17", "r18", "r19",\
209 "r20", "r21", "r22", "r23", "r24", "r25", "r26", "r27", \
210 "r28", "r29", "cr2", "cr3", "cr4", "v20", "v21", "v22", \
211 "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", \
212 "v31");
213 #else
214 // On gcc versions < 5.4.1 we need to include r30 on barrier as it's not
215 // saved by gcc.
216 #define CALLEE_SAVED_BARRIER()\
217 asm volatile("" : : : "r2", "r14", "r15", "r16", "r17", "r18", "r19",\
218 "r20", "r21", "r22", "r23", "r24", "r25", "r26", "r27", \
219 "r28", "r29", "r30", "cr2", "cr3", "cr4", "v20", "v21", \
220 "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", \
221 "v30", "v31");
222 #endif
223 #elif defined (__AARCH64EL__)
224 #define CALLEE_SAVED_BARRIER()\
225 asm volatile("" : : : "x19", "x20", "x21", "x22", "x23", "x24", "x25",\
226 "x26", "x27", "x28", \
227 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15")
228 #else
229 #define CALLEE_SAVED_BARRIER()\
230 asm volatile("" : : : "rbx", "r12", "r13", "r14", "r15");
231 #endif
233 //////////////////////////////////////////////////////////////////////
235 // We reserve the exit status 127 to signal a failure in the
236 // interpreter. 127 is a valid exit code on all reasonable
237 // architectures: POSIX requires at least 8 unsigned bits and
238 // Windows 32 signed bits.
239 #define HPHP_EXIT_FAILURE 127
241 //////////////////////////////////////////////////////////////////////
243 #if FACEBOOK
244 #define USE_FOLLY_SYMBOLIZER 1
245 // Linking in libbfd is a gigantic PITA, but if folly symbolizer doesn't
246 // work on your platform, you'll need to figure it out.
247 #define HAVE_LIBBFD 1
248 #endif
250 #ifndef PACKAGE
251 // The value doesn't matter, but it must be defined before you include
252 // bfd.h
253 #define PACKAGE "hhvm"
254 #endif
256 //////////////////////////////////////////////////////////////////////
258 #ifdef _MSC_VER
259 # include "hphp/util/portability/fnmatch.h"
260 # include "hphp/util/portability/glob.h"
261 # include "hphp/util/portability/rand_r.h"
262 # include "hphp/util/portability/strfmon.h"
263 #endif
265 #if defined(_MSC_VER) && _MSC_FULL_VER <= 190023506 // 2015 Update 1 or below
266 // MSVC2015 has an issue with getting function pointers to templated functions
267 // if the expected result type isn't auto. Unfortunately, when I made the
268 // initial bug report, I oversimplified the use-case, and, while the case I
269 // reported was indeed fixed in Update 1 RC, none of our actual uses of it were
270 // fixed.
271 // This is being tracked at MS as #163251.
272 # define MSVC_REQUIRE_AUTO_TEMPLATED_OVERLOAD 1
273 // 2015 RTM doesn't like it when you try to add via a double duration.
274 // Bug Report: https://connect.microsoft.com/VisualStudio/feedback/details/1839243
275 # define MSVC_NO_STD_CHRONO_DURATION_DOUBLE_ADD 1
276 #endif
278 #ifdef __APPLE__
279 #define ASM_LOCAL_LABEL(x) "L" x
280 #else
281 #define ASM_LOCAL_LABEL(x) ".L" x
282 #endif
284 #endif