1 /** @file
2     Common declarations and definitions for Standard C Library headers.
3 
4     This header consolidates definitions and declarations for compiler specific
5     features in one place in order to assist in making the remainder of the
6     library as compiler independent as possible.
7 
8     Certain macro and type definitions are required to be provided by several
9     different headers.  In order to avoid having multiple definitions, and the
10     attendant risk of having the definitions get out of sync, they are defined in
11     this header.
12 
13     Note that MdePkg/Include/Base.h is automatically included and will bring
14     processor architecture specific definitions along with it.
15 
16     Throughout the library, the following macros are used instead of keywords so
17     that the library can be easily tuned for different compilers.
18     __inline    Defined to the appropriate keyword or not defined.
19     __func__    Defined to __FUNC__, __FUNCTION__, or NULL as appropriate.
20     __restrict  Defined to nothing for VC++ or to restrict for GCC and C99 compliant compilers.
21 
22     This file and its contents are inspired by the <sys/cdefs.h> files in Berkeley
23     Unix.  They have been re-implemented to be specific to the EFI environment.
24 
25     Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
26     This program and the accompanying materials are licensed and made available under
27     the terms and conditions of the BSD License that accompanies this distribution.
28     The full text of the license may be found at
29     http://opensource.org/licenses/bsd-license.
30 
31     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
32     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
33 
34     Portions Copyright (c) 1991, 1993
35     The Regents of the University of California.  All rights reserved.
36 
37     Portions of this code are derived from software contributed to Berkeley by
38     Berkeley Software Design, Inc.
39     Redistribution and use in source and binary forms, with or without
40     modification, are permitted provided that the following conditions
41     are met:
42       1. Redistributions of source code must retain the above copyright
43         notice, this list of conditions and the following disclaimer.
44       2. Redistributions in binary form must reproduce the above copyright
45         notice, this list of conditions and the following disclaimer in the
46         documentation and/or other materials provided with the distribution.
47       3. Neither the name of the University nor the names of its contributors
48         may be used to endorse or promote products derived from this software
49         without specific prior written permission.
50 
51     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61     SUCH DAMAGE.
62 **/
63 #ifndef _EFI_CDEFS_H
64 #define _EFI_CDEFS_H
65 
66 /*
67 * Macro to test if we're using a GNU C compiler of a specific vintage
68 * or later, for e.g. features that appeared in a particular version
69 * of GNU C.  Usage:
70 *
71 *  #if __GNUC_PREREQ__(major, minor)
72 *  ...cool feature...
73 *  #else
74 *  ...delete feature...
75 *  #endif
76 */
77 #ifdef __GNUC__
78 #define __GNUC_PREREQ__(x, y)           \
79 ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||      \
80  (__GNUC__ > (x)))
81 
82 #define DONT_USE_STRONG_WEAK_ALIAS  1
83 
84 #else
85 #define __GNUC_PREREQ__(x, y) 0
86 #endif
87 
88 #include  <sys/featuretest.h>
89 //#include <machine/_EfiCdefs.h>
90 #ifdef __PE32__
91 #include <sys/_EfiCdefs_PE32.h>
92 #else
93 #include <sys/cdefs_aout.h>
94 #endif
95 
96 /* NULL is defined by the automatic inclusion of Base.h by the build tools. */
97 
98 #ifdef __GNUC__
99   #define _EFI_SIZE_T_      __SIZE_TYPE__     /* sizeof() */
100   #define _EFI_WCHAR_T      __WCHAR_TYPE__
101   #define _EFI_WINT_T       __WINT_TYPE__
102   //#define _EFI_WINT_MIN     (0)
103   //#define _EFI_WINT_MAX     (0xFFFF)
104   #define _EFI_PTRDIFF_T_   __PTRDIFF_TYPE__  /* ptr1 - ptr2 --- Must be same size as size_t */
105 
106 #else
107 #define _EFI_SIZE_T_      UINTN       /* sizeof() */
108 #define _EFI_WCHAR_T      UINT16
109 #define _EFI_WINT_T       INT32
110   //#define _EFI_WINT_MIN     (-2147483647)       /* wint_t   */
111   //#define _EFI_WINT_MAX     ( 2147483647)       /* wint_t   */
112   #define _EFI_PTRDIFF_T_   INTN       /* ptr1 - ptr2 --- Must be same size as size_t */
113 #endif  /* __GNUC__ */
114 
115 #define _EFI_CLOCK_T      UINT64
116 #define _EFI_TIME_T       INT32
117 
118 #if defined(__cplusplus)
119 #define __BEGIN_DECLS   extern "C" {
120 #define __END_DECLS   }
121 #define __static_cast(x,y)  static_cast<x>(y)
122 #else
123 #define __BEGIN_DECLS
124 #define __END_DECLS
125 #define __static_cast(x,y)  (x)y
126 #endif
127 
128   /*
129   * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
130   * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
131   * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
132   * in between its arguments.  __CONCAT can also concatenate double-quoted
133   * strings produced by the __STRING macro, but this only works with ANSI C.
134   */
135 
136 #define ___STRING(x)  __STRING(x)
137 #define ___CONCAT(x,y)  __CONCAT(x,y)
138 #define __CONCAT(x,y) x ## y
139 #define __STRING(x) #x
140 
141 #define __const     CONST
142 #define __signed    signed
143 #define __volatile  volatile
144 
145 #if __STDC__ || defined(__cplusplus)
146   #if defined(__cplusplus)
147     #define __inline  inline    /* convert to C++ keyword */
148   #else
149     #if defined(_MSC_VER) || (!defined(__GNUC__) && !defined(__lint__))
150       #define __inline      /* delete C99 keyword */
151     #endif /* !__GNUC__  && !__lint__ */
152   #endif /* !__cplusplus */
153 #endif  /* !(__STDC__ || __cplusplus) */
154 
155 /* Used in NetBSD for internal auditing of the source tree. */
156 #define __aconst
157 
158   /*
159   * The following macro is used to remove const cast-away warnings
160   * from gcc -Wcast-qual; it should be used with caution because it
161   * can hide valid errors; in particular most valid uses are in
162   * situations where the API requires it, not to cast away string
163   * constants. We don't use *intptr_t on purpose here and we are
164   * explicit about unsigned long so that we don't have additional
165   * dependencies.
166   */
167 #define __UNCONST(a)  ((void *)(a))
168 //#define __UNCONST(a)  ((void *)(PHYSICAL_ADDRESS)(const void *)(a))
169 
170   /*
171   * The following macro is used to remove the volatile cast-away warnings
172   * from gcc -Wcast-qual; as above it should be used with caution
173   * because it can hide valid errors or warnings.  Valid uses include
174   * making it possible to pass a volatile pointer to memset().
175   * For the same reasons as above, we use unsigned long and not intptr_t.
176   */
177 #define __UNVOLATILE(a) ((void *)(PHYSICAL_ADDRESS)(volatile void *)(a))
178 
179   /*
180   * GCC2 provides __extension__ to suppress warnings for various GNU C
181   * language extensions under "-ansi -pedantic".
182   */
183 #if !__GNUC_PREREQ__(2, 0)
184 #define __extension__   /* delete __extension__ if non-gcc or gcc1 */
185 #endif
186 
187   /*
188   * GCC1 and some versions of GCC2 declare dead (non-returning) and
189   * pure (no side effects) functions using "volatile" and "const";
190   * unfortunately, these then cause warnings under "-ansi -pedantic".
191   * GCC2 uses a new, peculiar __attribute__((attrs)) style.  All of
192   * these work for GNU C++ (modulo a slight glitch in the C++ grammar
193   * in the distribution version of 2.5.5).
194   */
195 #if !__GNUC_PREREQ__(2, 5)
196 #define __attribute__(x)  /* delete __attribute__ if non-gcc or gcc1 */
197 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
198 #define __dead    __volatile
199 #define __pure    __const
200 #endif
201 #endif
202 
203   /* Delete pseudo-keywords wherever they are not available or needed. */
204 #ifndef __dead
205 #define __dead
206 #define __pure
207 #endif
208 
209 #if __GNUC_PREREQ__(2, 7)
210 #define __unused  __attribute__((__unused__))
211 #define __noreturn  __attribute__((__noreturn__))
212 #else
213 #define __unused  /* delete */
214 #define __noreturn  /* delete */
215 #endif
216 
217 #if __GNUC_PREREQ__(3, 1)
218 #define __used    __attribute__((__used__))
219 #else
220 #define __used    __unused
221 #endif
222 
223 #if __GNUC_PREREQ__(2, 7)
224 #define __packed  __attribute__((__packed__))
225 #define __aligned(x)  __attribute__((__aligned__(x)))
226 #define __section(x)  __attribute__((__section__(x)))
227 #elif defined(__lint__)
228 #define __packed  /* delete */
229 #define __aligned(x)  /* delete */
230 #define __section(x)  /* delete */
231 #else
232 #define __packed  error: no __packed for this compiler
233 #define __aligned(x)  error: no __aligned for this compiler
234 #define __section(x)  error: no __section for this compiler
235 #endif
236 
237 /*
238 * C99 defines the restrict type qualifier keyword, which was made available
239 * in GCC 2.92.
240 */
241 #if __STDC_VERSION__ >= 199901L
242   #define __restrict  restrict
243 #else
244   #if defined(_MSC_VER) || !__GNUC_PREREQ__(2, 92)
245     #define __restrict  /* delete __restrict when not supported */
246   #endif
247 #endif
248 
249 /*
250 * C99 defines __func__ predefined identifier, which was made available
251 * in GCC 2.95.
252 */
253 #if !(__STDC_VERSION__ >= 199901L)
254   #if defined(_MSC_VER)
255     #define __func__    __FUNCTION__  /* Use the MS-specific predefined macro */
256   #elif __GNUC_PREREQ__(2, 6)
257     #define __func__  __PRETTY_FUNCTION__
258   #elif __GNUC_PREREQ__(2, 4)
259     #define __func__  __FUNCTION__
260   #else
261     #define __func__  ""
262   #endif
263 #endif /* !(__STDC_VERSION__ >= 199901L) */
264 
265 #define __RENAME(x)
266 
267   /*
268   * A barrier to stop the optimizer from moving code or assume live
269   * register values. This is gcc specific, the version is more or less
270   * arbitrary, might work with older compilers.
271   */
272 #if __GNUC_PREREQ__(2, 95)
273 #define __insn_barrier()  __asm __volatile("":::"memory")
274 #else
275 #define __insn_barrier()  /* */
276 #endif
277 
278   /*
279   * GNU C version 2.96 adds explicit branch prediction so that
280   * the CPU back-end can hint the processor and also so that
281   * code blocks can be reordered such that the predicted path
282   * sees a more linear flow, thus improving cache behavior, etc.
283   *
284   * The following two macros provide us with a way to use this
285   * compiler feature.  Use __predict_true() if you expect the expression
286   * to evaluate to true, and __predict_false() if you expect the
287   * expression to evaluate to false.
288   *
289   * A few notes about usage:
290   *
291   *  * Generally, __predict_false() error condition checks (unless
292   *    you have some _strong_ reason to do otherwise, in which case
293   *    document it), and/or __predict_true() `no-error' condition
294   *    checks, assuming you want to optimize for the no-error case.
295   *
296   *  * Other than that, if you don't know the likelihood of a test
297   *    succeeding from empirical or other `hard' evidence, don't
298   *    make predictions.
299   *
300   *  * These are meant to be used in places that are run `a lot'.
301   *    It is wasteful to make predictions in code that is run
302   *    seldomly (e.g. at subsystem initialization time) as the
303   *    basic block reordering that this affects can often generate
304   *    larger code.
305   */
306 #if __GNUC_PREREQ__(2, 96)
307 #define __predict_true(exp) __builtin_expect((exp) != 0, 1)
308 #define __predict_false(exp)  __builtin_expect((exp) != 0, 0)
309 #else
310 #define __predict_true(exp) (exp)
311 #define __predict_false(exp)  (exp)
312 #endif
313 
314 /* find least significant bit that is set */
315 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
316 
317 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
318 #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
319 #define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
320 
321 #if defined(_MSC_VER)           /* Handle Microsoft VC++ compiler specifics. */
322 
323     /*  VC++, by default, defines wchar_t as an intrinsic type, equivalent to
324     unsigned short.  This conflicts which Standard C Library
325     implementations which try to define wchar_t.
326     Make sure that this behavior has been turned off by using
327     /Zc:wchar_t- on the command line.
328     */
329   #ifdef _NATIVE_WCHAR_T_DEFINED
330   #error You must specify /Zc:wchar_t- to the compiler to turn off intrinsic wchar_t.
331   #endif
332 
333   /* Get rid of pre-defined macros that are misleading in this environment. */
334   #undef  _WIN32
335   #undef  _WIN64
336 
337   // Keep compiler quiet about casting from smaller to larger types
338   #pragma warning ( disable : 4306 )
339 
340   #define __STDC__            1
341   #define __STDC_VERSION__    199409L
342   #define __STDC_HOSTED__     1
343 
344 #endif  /* defined(_MSC_VER) */
345 extern int _fltused;    // VC++ requires this if you use floating point.  KEEP for all compilers.
346 
347 #define _Bool BOOLEAN
348 #define _DIAGASSERT(e)
349 
350 // Types used to replace long so that it will have constant length regardless of compiler.
351 typedef  INT32   LONG32;
352 typedef UINT32  ULONG32;
353 typedef  INT64   LONG64;
354 typedef UINT64  ULONG64;
355 
356 typedef   INTN   EFI_LONG_T;
357 typedef  UINTN   EFI_ULONG_T;
358 
359 /* These types reflect the compiler's size for long */
360 #if defined(__GNUC__)
361   #if __GNUC_PREREQ__(4,4)
362     /* GCC 4.4 or later */
363     typedef   INTN    LONGN;
364     typedef  UINTN    ULONGN;
365   #else
366     /* minGW gcc variant */
367     typedef   INT32   LONGN;
368     typedef  UINT32   ULONGN;
369   #endif  /* __GNUC_PREREQ__(4,4) */
370 #else   /* NOT GCC */
371   /* Microsoft or Intel compilers */
372   typedef   INT32   LONGN;
373   typedef  UINT32   ULONGN;
374 #endif  /* defined(__GNUC__) */
375 
376 #endif  /* _EFI_CDEFS_H */
377