1 /*
2  * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 /*
8  * This file should be at the top of the #include group, followed by
9  * standard system #include files, then by native client specific
10  * includes.
11  *
12  * TODO(gregoryd): explain why.  (Something to do with windows include
13  * files, to be reconstructed.)
14  */
15 
16 #ifndef NATIVE_CLIENT_SRC_INCLUDE_PORTABILITY_H_
17 #define NATIVE_CLIENT_SRC_INCLUDE_PORTABILITY_H_ 1
18 
19 #include <stdlib.h>
20 
21 #include "native_client/src/include/build_config.h"
22 #include "native_client/src/include/nacl_base.h"
23 #ifdef __native_client__
24 #include <bits/wordsize.h>
25 #else
26 #include "native_client/src/trusted/service_runtime/include/bits/wordsize.h"
27 #endif
28 
29 #include "native_client/src/include/nacl_compiler_annotations.h"
30 
31 #if NACL_WINDOWS
32 /* disable warnings for deprecated functions like getenv, etc. */
33 #pragma warning(disable : 4996)
34 # include <malloc.h>
35 /* TODO: eliminate port_win.h */
36 # include "native_client/src/include/win/port_win.h"
37 #else
38 # include <sys/types.h>
39 # include <stdint.h>
40 # include <unistd.h>
41 # include <sys/time.h>
42 #endif  /*NACL_WINDOWS*/
43 
44 /*
45  * Per C99 7.8.14, define __STDC_CONSTANT_MACROS before including <stdint.h>
46  * to get the INTn_C and UINTn_C macros for integer constants.  It's difficult
47  * to guarantee any specific ordering of header includes, so it's difficult to
48  * guarantee that the INTn_C macros can be defined by including <stdint.h> at
49  * any specific point.  Provide GG_INTn_C macros instead.
50  */
51 
52 #define GG_INT8_C(x)    (x)
53 #define GG_INT16_C(x)   (x)
54 #define GG_INT32_C(x)   (x)
55 #define GG_INT64_C(x)   GG_LONGLONG(x)
56 
57 #define GG_UINT8_C(x)   (x ## U)
58 #define GG_UINT16_C(x)  (x ## U)
59 #define GG_UINT32_C(x)  (x ## U)
60 #define GG_UINT64_C(x)  GG_ULONGLONG(x)
61 
62 #if NACL_WINDOWS
63 #define GG_LONGLONG(x) x##I64
64 #define GG_ULONGLONG(x) x##UI64
65 #else
66 #define GG_LONGLONG(x) x##LL
67 #define GG_ULONGLONG(x) x##ULL
68 #endif
69 
70 
71 /**
72  * Processor architecture detection. This code was derived from
73  * Chromium's build/build_config.h.
74  * For more info on what's defined, see:
75  * http://msdn.microsoft.com/en-us/library/b0084kay.aspx
76  * http://www.agner.org/optimize/calling_conventions.pdf
77  * r with gcc, run: "echo | gcc -E -dM -"
78  */
79 #if defined(_M_X64) || defined(__x86_64__)
80 #define NACL_HOST_WORDSIZE 64
81 #elif defined(_M_IX86) || defined(__i386__)
82 #define NACL_HOST_WORDSIZE 32
83 #elif defined(__ARMEL__)
84 #define NACL_HOST_WORDSIZE 32
85 #elif defined(__mips__)
86 #define NACL_HOST_WORDSIZE 32
87 #elif defined(__pnacl__)
88 #define NACL_HOST_WORDSIZE 32
89 #else
90 #error Unrecognized host architecture
91 #endif
92 
93 #ifndef SIZE_T_MAX
94 # define SIZE_T_MAX ((size_t) -1)
95 #endif
96 
97 /* use uint64_t as largest integral type, assume 8 bit bytes */
98 #ifndef OFF_T_MIN
99 # define OFF_T_MIN ((off_t) (((uint64_t) 1) << (8 * sizeof(off_t) - 1)))
100 #endif
101 #ifndef OFF_T_MAX
102 # define OFF_T_MAX ((off_t) ~(((uint64_t) 1) << (8 * sizeof(off_t) - 1)))
103 #endif
104 
105 
106 /*
107  * printf macros for size_t, in the style of inttypes.h.  this is
108  * needed since the windows compiler does not understand %zd
109  * etc. 64-bit windows uses 32-bit long and does not include long
110  * long
111  */
112 #if NACL_WINDOWS
113 # if defined(_WIN64)
114 #  define  NACL___PRIdS_PREFIX "I64"
115 #  define  NACL___PRIuS_PREFIX "I64"
116 # else
117 #  define  NACL___PRIdS_PREFIX
118 #  define  NACL___PRIuS_PREFIX
119 # endif
120 #elif NACL_OSX
121 # define  NACL___PRIdS_PREFIX "l" /* -pedantic C++ programs w/ xcode */
122 # define  NACL___PRIuS_PREFIX "l" /* -pedantic C++ programs w/ xcode */
123 #elif NACL_ANDROID
124 # define  NACL___PRIdS_PREFIX "z"
125 # define  NACL___PRIuS_PREFIX "z"
126 #elif defined(__native_client__)
127 # define NACL___PRIdS_PREFIX "z"
128 # define NACL___PRIuS_PREFIX "z"
129 #elif __WORDSIZE == 64
130 # define NACL___PRIdS_PREFIX "l"
131 # define NACL___PRIuS_PREFIX "l"
132 #else
133 # define NACL___PRIdS_PREFIX
134 # define NACL___PRIuS_PREFIX
135 #endif
136 
137 #if !defined(NACL_PRIdS)
138 #define NACL_PRIdS NACL___PRIdS_PREFIX "d"
139 #endif
140 #if !defined(NACL_PRIiS)
141 #define NACL_PRIiS NACL___PRIdS_PREFIX "i"
142 #endif
143 #if !defined(NACL_PRIoS)
144 #define NACL_PRIoS NACL___PRIuS_PREFIX "o"
145 #endif
146 #if !defined (NACL_PRIuS)
147 #define NACL_PRIuS NACL___PRIuS_PREFIX "u"
148 #endif
149 #if !defined(NACL_PRIxS)
150 #define NACL_PRIxS NACL___PRIuS_PREFIX "x"
151 #endif
152 #if !defined(NACL_PRIXS)
153 #define NACL_PRIXS NACL___PRIuS_PREFIX "X"
154 #endif
155 
156 /*
157  * printf macros for intptr_t and uintptr_t, int{8,16,32,64}
158  */
159 #if NACL_WINDOWS
160 # if defined(_WIN64)
161 #  define NACL___PRIPTR_PREFIX "I64"
162 # else
163 #  define NACL___PRIPTR_PREFIX "l"
164 # endif
165 # define NACL_PRIdPTR NACL___PRIPTR_PREFIX "d"
166 # define NACL_PRIiPTR NACL___PRIPTR_PREFIX "i"
167 # define NACL_PRIoPTR NACL___PRIPTR_PREFIX "o"
168 # define NACL_PRIuPTR NACL___PRIPTR_PREFIX "u"
169 # define NACL_PRIxPTR NACL___PRIPTR_PREFIX "x"
170 # define NACL_PRIXPTR NACL___PRIPTR_PREFIX "X"
171 
172 # define NACL_PRId8  "d"
173 # define NACL_PRIi8  "i"
174 # define NACL_PRIo8  "o"
175 # define NACL_PRIu8  "u"
176 # define NACL_PRIx8  "x"
177 # define NACL_PRIX8  "X"
178 
179 # define NACL_PRId16 "d"
180 # define NACL_PRIi16 "i"
181 # define NACL_PRIo16 "o"
182 # define NACL_PRIu16 "u"
183 # define NACL_PRIx16 "x"
184 # define NACL_PRIX16 "X"
185 
186 # define NACL___PRI32_PREFIX "I32"
187 
188 # define NACL_PRId32 NACL___PRI32_PREFIX "d"
189 # define NACL_PRIi32 NACL___PRI32_PREFIX "i"
190 # define NACL_PRIo32 NACL___PRI32_PREFIX "o"
191 # define NACL_PRIu32 NACL___PRI32_PREFIX "u"
192 # define NACL_PRIx32 NACL___PRI32_PREFIX "x"
193 # define NACL_PRIX32 NACL___PRI32_PREFIX "X"
194 
195 # define NACL___PRI64_PREFIX "I64"
196 
197 #if !defined(NACL_PRId64)
198 # define NACL_PRId64 NACL___PRI64_PREFIX "d"
199 #endif
200 #if !defined(NACL_PRIi64)
201 # define NACL_PRIi64 NACL___PRI64_PREFIX "i"
202 #endif
203 #if !defined(NACL_PRIo64)
204 # define NACL_PRIo64 NACL___PRI64_PREFIX "o"
205 #endif
206 #if !defined(NACL_PRIu64)
207 # define NACL_PRIu64 NACL___PRI64_PREFIX "u"
208 #endif
209 #if !defined(NACL_PRIx64)
210 # define NACL_PRIx64 NACL___PRI64_PREFIX "x"
211 #endif
212 #if !defined(NACL_PRIX64)
213 # define NACL_PRIX64 NACL___PRI64_PREFIX "X"
214 #endif
215 
216 #else  /* NACL_LINUX, NACL_OSX, __native_client__ */
217 
218 # ifndef __STDC_FORMAT_MACROS
219 #  define __STDC_FORMAT_MACROS  /* C++ */
220 # endif
221 
222 # include <inttypes.h>
223 
224 # if defined(__native_client__)
225 #  define NACL_PRIdPTR PRId32
226 #  define NACL_PRIiPTR PRIi32
227 #  define NACL_PRIoPTR PRIo32
228 #  define NACL_PRIuPTR PRIu32
229 #  define NACL_PRIxPTR PRIx32
230 #  define NACL_PRIXPTR PRIX32
231 # else
232 #  define NACL_PRIdPTR PRIdPTR
233 #  define NACL_PRIiPTR PRIiPTR
234 #  define NACL_PRIoPTR PRIoPTR
235 #  define NACL_PRIuPTR PRIuPTR
236 #  define NACL_PRIxPTR PRIxPTR
237 #  define NACL_PRIXPTR PRIXPTR
238 # endif
239 
240 # define NACL_PRId64 PRId64
241 # define NACL_PRIi64 PRIi64
242 # define NACL_PRIo64 PRIo64
243 # define NACL_PRIu64 PRIu64
244 # define NACL_PRIx64 PRIx64
245 # define NACL_PRIX64 PRIX64
246 
247 # define NACL_PRId32 PRId32
248 # define NACL_PRIi32 PRIi32
249 # define NACL_PRIo32 PRIo32
250 # define NACL_PRIu32 PRIu32
251 # define NACL_PRIx32 PRIx32
252 # define NACL_PRIX32 PRIX32
253 
254 # define NACL_PRId16 PRId16
255 # define NACL_PRIi16 PRIi16
256 # define NACL_PRIo16 PRIo16
257 # define NACL_PRIu16 PRIu16
258 # define NACL_PRIx16 PRIx16
259 # define NACL_PRIX16 PRIX16
260 
261 # define NACL_PRId8 PRId8
262 # define NACL_PRIi8 PRIi8
263 # define NACL_PRIo8 PRIo8
264 # define NACL_PRIu8 PRIu8
265 # define NACL_PRIx8 PRIx8
266 # define NACL_PRIX8 PRIX8
267 
268 # if NACL_OSX
269 /*
270  * OSX defines "hh" prefix for int8_t etc, but that's not standards
271  * compliant -- --std=c++98 -Wall -Werror rejects it.
272  */
273 #  undef NACL_PRId8
274 #  undef NACL_PRIi8
275 #  undef NACL_PRIo8
276 #  undef NACL_PRIu8
277 #  undef NACL_PRIx8
278 #  undef NACL_PRIX8
279 #  define NACL_PRId8  "d"
280 #  define NACL_PRIi8  "i"
281 #  define NACL_PRIo8  "o"
282 #  define NACL_PRIu8  "u"
283 #  define NACL_PRIx8  "x"
284 #  define NACL_PRIX8  "X"
285 # endif  /* NACL_OSX */
286 
287 #endif  /* NACL_LINUX, NACL_OSX, __native_client__ */
288 
289 /*
290  * macros for run-time error detectors (such as Valgrind/Memcheck).
291  */
292 #if defined(_DEBUG) && NACL_LINUX
293 #include "native_client/src/third_party/valgrind/memcheck.h"
294 #define NACL_MAKE_MEM_UNDEFINED(a, b) (void) VALGRIND_MAKE_MEM_UNDEFINED(a, b)
295 #else
296 #define NACL_MAKE_MEM_UNDEFINED(a, b)
297 #endif
298 
299 #endif  /* NATIVE_CLIENT_SRC_INCLUDE_PORTABILITY_H_ */
300