28b1137de44ea5ff29c3ee1cf77d6b0b965bcf25
[fw/sdcc] / support / cpp2 / system.h
1 /* Get common system includes and various definitions and declarations based
2    on autoconf macros.
3    Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #ifndef GCC_SYSTEM_H
24 #define GCC_SYSTEM_H
25
26 #include "ansidecl.h"
27
28 /* We must include stdarg.h/varargs.h before stdio.h.  */
29 #ifdef ANSI_PROTOTYPES
30 #include <stdarg.h>
31 #else
32 #include <varargs.h>
33 #endif
34
35 #ifndef va_copy
36 # ifdef __va_copy
37 #   define va_copy(d,s)  __va_copy((d),(s))
38 # else
39 #   define va_copy(d,s)  ((d) = (s))
40 # endif
41 #endif
42
43 #ifdef HAVE_STDDEF_H
44 # include <stddef.h>
45 #endif
46
47 #include <stdio.h>
48
49 #define CHAR_BIT 8
50
51 /* Define a generic NULL if one hasn't already been defined.  */
52 #ifndef NULL
53 #define NULL 0
54 #endif
55
56 /* There are an extraordinary number of issues with <ctype.h>.
57    The last straw is that it varies with the locale.  Use libiberty's
58    replacement instead.  */
59 #if defined(__APPLE__) && defined(__MACH__)
60 #include <libiberty/safe-ctype.h>
61 #else
62 #include <safe-ctype.h>
63 #endif
64
65 #include <sys/types.h>
66
67 #include <errno.h>
68
69 #if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
70 extern int errno;
71 #endif
72
73 #ifdef STRING_WITH_STRINGS
74 # include <string.h>
75 # include <strings.h>
76 #else
77 # ifdef HAVE_STRING_H
78 #  include <string.h>
79 # else
80 #  ifdef HAVE_STRINGS_H
81 #   include <strings.h>
82 #  endif
83 # endif
84 #endif
85
86 #ifdef HAVE_STDLIB_H
87 # include <stdlib.h>
88 #endif
89
90 /* If we don't have an overriding definition, set SUCCESS_EXIT_CODE and
91    FATAL_EXIT_CODE to EXIT_SUCCESS and EXIT_FAILURE respectively,
92    or 0 and 1 if those macros are not defined.  */
93 #ifndef SUCCESS_EXIT_CODE
94 # ifdef EXIT_SUCCESS
95 #  define SUCCESS_EXIT_CODE EXIT_SUCCESS
96 # else
97 #  define SUCCESS_EXIT_CODE 0
98 # endif
99 #endif
100
101 #ifndef FATAL_EXIT_CODE
102 # ifdef EXIT_FAILURE
103 #  define FATAL_EXIT_CODE EXIT_FAILURE
104 # else
105 #  define FATAL_EXIT_CODE 1
106 # endif
107 #endif
108
109 #ifdef HAVE_UNISTD_H
110 # include <unistd.h>
111 #endif
112
113 #ifdef HAVE_SYS_PARAM_H
114 # include <sys/param.h>
115 /* We use this identifier later and it appears in some vendor param.h's.  */
116 # undef PREFETCH
117 #endif
118
119 #if HAVE_LIMITS_H
120 # include <limits.h>
121 #endif
122
123 /* Get definitions of HOST_WIDE_INT and HOST_WIDEST_INT.  */
124 #include "hwint.h"
125
126 /* A macro to determine whether a VALUE lies inclusively within a
127    certain range without evaluating the VALUE more than once.  This
128    macro won't warn if the VALUE is unsigned and the LOWER bound is
129    zero, as it would e.g. with "VALUE >= 0 && ...".  Note the LOWER
130    bound *is* evaluated twice, and LOWER must not be greater than
131    UPPER.  However the bounds themselves can be either positive or
132    negative.  */
133 #define IN_RANGE(VALUE, LOWER, UPPER) \
134   ((unsigned HOST_WIDE_INT) ((VALUE) - (LOWER)) <= ((UPPER) - (LOWER)))
135
136 /* Infrastructure for defining missing _MAX and _MIN macros.  Note that
137    macros defined with these cannot be used in #if.  */
138
139 /* The extra casts work around common compiler bugs.  */
140 #define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
141 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
142    It is necessary at least when t == time_t.  */
143 #define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
144                              ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
145 #define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
146
147 /* Use that infrastructure to provide a few constants.  */
148 #ifndef UCHAR_MAX
149 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
150 #endif
151
152 #ifdef TIME_WITH_SYS_TIME
153 # include <sys/time.h>
154 # include <time.h>
155 #else
156 # if HAVE_SYS_TIME_H
157 #  include <sys/time.h>
158 # else
159 #  ifdef HAVE_TIME_H
160 #   include <time.h>
161 #  endif
162 # endif
163 #endif
164
165 #ifdef HAVE_FCNTL_H
166 # include <fcntl.h>
167 #else
168 # ifdef HAVE_SYS_FILE_H
169 #  include <sys/file.h>
170 # endif
171 #endif
172
173 #ifndef SEEK_SET
174 # define SEEK_SET 0
175 # define SEEK_CUR 1
176 # define SEEK_END 2
177 #endif
178 #ifndef F_OK
179 # define F_OK 0
180 # define X_OK 1
181 # define W_OK 2
182 # define R_OK 4
183 #endif
184 #ifndef O_RDONLY
185 # define O_RDONLY 0
186 #endif
187 #ifndef O_WRONLY
188 # define O_WRONLY 1
189 #endif
190
191 /* Some systems define these in, e.g., param.h.  We undefine these names
192    here to avoid the warnings.  We prefer to use our definitions since we
193    know they are correct.  */
194
195 #undef MIN
196 #undef MAX
197 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
198 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
199
200 /* Returns the least number N such that N * Y >= X.  */
201 #define CEIL(x,y) (((x) + (y) - 1) / (y))
202
203 #ifdef HAVE_SYS_WAIT_H
204 #include <sys/wait.h>
205 #endif
206
207 #ifndef WIFSIGNALED
208 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
209 #endif
210 #ifndef WTERMSIG
211 #define WTERMSIG(S) ((S) & 0x7f)
212 #endif
213 #ifndef WIFEXITED
214 #define WIFEXITED(S) (((S) & 0xff) == 0)
215 #endif
216 #ifndef WEXITSTATUS
217 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
218 #endif
219 #ifndef WSTOPSIG
220 #define WSTOPSIG WEXITSTATUS
221 #endif
222 #ifndef WCOREDUMP
223 #define WCOREDUMP(S) ((S) & WCOREFLG)
224 #endif
225 #ifndef WCOREFLG
226 #define WCOREFLG 0200
227 #endif
228
229 /* The HAVE_DECL_* macros are three-state, undefined, 0 or 1.  If they
230    are defined to 0 then we must provide the relevant declaration
231    here.  These checks will be in the undefined state while configure
232    is running so be careful to test "defined (HAVE_DECL_*)".  */
233
234 #if defined (HAVE_DECL_ATOF) && !HAVE_DECL_ATOF
235 extern double atof PARAMS ((const char *));
236 #endif
237
238 #if defined (HAVE_DECL_ATOL) && !HAVE_DECL_ATOL
239 extern long atol PARAMS ((const char *));
240 #endif
241
242 #if defined (HAVE_DECL_FREE) && !HAVE_DECL_FREE
243 extern void free PARAMS ((PTR));
244 #endif
245
246 #if defined (HAVE_DECL_GETENV) && !HAVE_DECL_GETENV
247 extern char *getenv PARAMS ((const char *));
248 #endif
249
250 #if defined (HAVE_DECL_STRSTR) && !HAVE_DECL_STRSTR
251 extern char *strstr PARAMS ((const char *, const char *));
252 #endif
253
254 #ifdef HAVE_MALLOC_H
255 #include <malloc.h>
256 #endif
257
258 #if defined (HAVE_DECL_MALLOC) && !HAVE_DECL_MALLOC
259 extern PTR malloc PARAMS ((size_t));
260 #endif
261
262 #if defined (HAVE_DECL_CALLOC) && !HAVE_DECL_CALLOC
263 extern PTR calloc PARAMS ((size_t, size_t));
264 #endif
265
266 #if defined (HAVE_DECL_REALLOC) && !HAVE_DECL_REALLOC
267 extern PTR realloc PARAMS ((PTR, size_t));
268 #endif
269
270 #if !defined(__STDC__) && !defined(volatile)
271 #define volatile
272 #endif
273
274 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
275 extern void abort PARAMS ((void));
276 #endif
277
278 /* 1 if we have C99 designated initializers.  */
279 #if !defined(HAVE_DESIGNATED_INITIALIZERS)
280 #if defined(__APPLE__) && (__MACH__)
281 #define HAVE_DESIGNATED_INITIALIZERS 0
282 #else
283 #define HAVE_DESIGNATED_INITIALIZERS \
284   ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
285 #endif
286 #endif
287
288 #if HAVE_SYS_STAT_H
289 # include <sys/stat.h>
290 #endif
291
292 /* Test if something is a normal file.  */
293 #ifndef S_ISREG
294 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
295 #endif
296
297 /* Test if something is a directory.  */
298 #ifndef S_ISDIR
299 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
300 #endif
301
302 /* Test if something is a character special file.  */
303 #ifndef S_ISCHR
304 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
305 #endif
306
307 /* Test if something is a block special file.  */
308 #ifndef S_ISBLK
309 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
310 #endif
311
312 /* Test if something is a socket.  */
313 #ifndef S_ISSOCK
314 # ifdef S_IFSOCK
315 #   define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
316 # else
317 #   define S_ISSOCK(m) 0
318 # endif
319 #endif
320
321 /* Test if something is a FIFO.  */
322 #ifndef S_ISFIFO
323 # ifdef S_IFIFO
324 #  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
325 # else
326 #  define S_ISFIFO(m) 0
327 # endif
328 #endif
329
330 /* Approximate O_NONBLOCK.  */
331 #ifndef O_NONBLOCK
332 #define O_NONBLOCK O_NDELAY
333 #endif
334
335 /* Approximate O_NOCTTY.  */
336 #ifndef O_NOCTTY
337 #define O_NOCTTY 0
338 #endif
339
340 /* Define well known filenos if the system does not define them.  */
341 #ifndef STDIN_FILENO
342 # define STDIN_FILENO   0
343 #endif
344 #ifndef STDOUT_FILENO
345 # define STDOUT_FILENO  1
346 #endif
347 #ifndef STDERR_FILENO
348 # define STDERR_FILENO  2
349 #endif
350
351 /* Some systems have mkdir that takes a single argument.  */
352 #ifdef MKDIR_TAKES_ONE_ARG
353 # define mkdir(a,b) mkdir(a)
354 #endif
355
356 /* Provide a way to print an address via printf.  */
357 #ifndef HOST_PTR_PRINTF
358 # ifdef HAVE_PRINTF_PTR
359 #  define HOST_PTR_PRINTF "%p"
360 # else
361 #  define HOST_PTR_PRINTF \
362     (sizeof (int) == sizeof (char *) ? "%x" \
363      : sizeof (long) == sizeof (char *) ? "%lx" : "%llx")
364 # endif
365 #endif /* ! HOST_PTR_PRINTF */
366
367 /* By default, colon separates directories in a path.  */
368 #ifndef PATH_SEPARATOR
369 #define PATH_SEPARATOR ':'
370 #endif
371
372 #ifndef DIR_SEPARATOR
373 #define DIR_SEPARATOR '/'
374 #endif
375
376 /* Define IS_DIR_SEPARATOR.  */
377 #ifndef DIR_SEPARATOR_2
378 # define IS_DIR_SEPARATOR(CH) ((CH) == DIR_SEPARATOR)
379 #else /* DIR_SEPARATOR_2 */
380 # define IS_DIR_SEPARATOR(CH) \
381         (((CH) == DIR_SEPARATOR) || ((CH) == DIR_SEPARATOR_2))
382 #endif /* DIR_SEPARATOR_2 */
383
384 /* Say how to test for an absolute pathname.  On Unix systems, this is if
385    it starts with a leading slash or a '$', the latter meaning the value of
386    an environment variable is to be used.  On machien with DOS-based
387    file systems, it is also absolute if it starts with a drive identifier.  */
388 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
389 #define IS_ABSOLUTE_PATHNAME(STR) \
390   (IS_DIR_SEPARATOR ((STR)[0]) || (STR)[0] == '$' \
391    || ((STR)[0] != '\0' && (STR)[1] == ':' && IS_DIR_SEPARATOR ((STR)[2])))
392 #else
393 #define IS_ABSOLUTE_PATHNAME(STR) \
394   (IS_DIR_SEPARATOR ((STR)[0]) || (STR)[0] == '$')
395 #endif
396
397 /* Get libiberty declarations.  */
398 #include "libiberty.h"
399 #include "symcat.h"
400
401 /* Provide a default for the HOST_BIT_BUCKET.
402    This suffices for POSIX-like hosts.  */
403
404 #ifndef HOST_BIT_BUCKET
405 #define HOST_BIT_BUCKET "/dev/null"
406 #endif
407
408 /* Be conservative and only use enum bitfields with GCC.
409    FIXME: provide a complete autoconf test for buggy enum bitfields.  */
410
411 #if (GCC_VERSION > 2000)
412 #define ENUM_BITFIELD(TYPE) enum TYPE
413 #else
414 #define ENUM_BITFIELD(TYPE) unsigned int
415 #endif
416
417 #ifndef offsetof
418 #define offsetof(TYPE, MEMBER)  ((size_t) &((TYPE *) 0)->MEMBER)
419 #endif
420
421 /* Traditional C cannot initialize union members of structs.  Provide
422    a macro which expands appropriately to handle it.  This only works
423    if you intend to initialize the union member to zero since it relies
424    on default initialization to zero in the traditional C case.  */
425 #ifdef __STDC__
426 #define UNION_INIT_ZERO , {0}
427 #else
428 #define UNION_INIT_ZERO
429 #endif
430
431 /* Various error reporting routines want to use __FUNCTION__.  */
432 #if (GCC_VERSION < 2007)
433 #ifndef __FUNCTION__
434 #define __FUNCTION__ "?"
435 #endif /* ! __FUNCTION__ */
436 #endif
437
438 /* __builtin_expect(A, B) evaluates to A, but notifies the compiler that
439    the most likely value of A is B.  This feature was added at some point
440    between 2.95 and 3.0.  Let's use 3.0 as the lower bound for now.  */
441 #if (GCC_VERSION < 3000)
442 #define __builtin_expect(a, b) (a)
443 #endif
444
445 /* Provide some sort of boolean type.  We use stdbool.h if it's
446   available.  This must be after all inclusion of system headers,
447   as some of them will mess us up.  */
448 #undef bool
449 #undef true
450 #undef false
451 #undef TRUE
452 #undef FALSE
453
454 #ifdef HAVE_STDBOOL_H
455 # include <stdbool.h>
456 #else
457 # if !HAVE__BOOL
458 typedef char _Bool;
459 # endif
460 # define bool _Bool
461 # define true 1
462 # define false 0
463 #endif
464
465 #define TRUE true
466 #define FALSE false
467
468 /* As the last action in this file, we poison the identifiers that
469    shouldn't be used.  Note, luckily gcc-3.0's token-based integrated
470    preprocessor won't trip on poisoned identifiers that arrive from
471    the expansion of macros.  E.g. #define strrchr rindex, won't error
472    if rindex is poisoned after this directive is issued and later on
473    strrchr is called.
474
475    Note: We define bypass macros for the few cases where we really
476    want to use the libc memory allocation routines.  Otherwise we
477    insist you use the "x" versions from libiberty.  */
478
479 #define really_call_malloc malloc
480 #define really_call_calloc calloc
481 #define really_call_realloc realloc
482
483 #if (GCC_VERSION >= 3000)
484
485 /* Note autoconf checks for prototype declarations and includes
486    system.h while doing so.  Only poison these tokens if actually
487    compiling gcc, so that the autoconf declaration tests for malloc
488    etc don't spuriously fail.  */
489 #ifdef IN_GCC
490 #undef calloc
491 #undef strdup
492  #pragma GCC poison calloc strdup
493
494 #if defined(FLEX_SCANNER) || defined (YYBISON) || defined(YYBYACC)
495 /* Flex and bison use malloc and realloc.  Yuk.  */
496 #define malloc xmalloc
497 #define realloc xrealloc
498 #else
499 #undef malloc
500 #undef realloc
501  #pragma GCC poison malloc realloc
502 #endif
503
504 /* Old target macros that have moved to the target hooks structure.  */
505  #pragma GCC poison ASM_OPEN_PAREN ASM_CLOSE_PAREN                      \
506         FUNCTION_PROLOGUE FUNCTION_EPILOGUE                             \
507         FUNCTION_END_PROLOGUE FUNCTION_BEGIN_EPILOGUE                   \
508         DECL_MACHINE_ATTRIBUTES COMP_TYPE_ATTRIBUTES INSERT_ATTRIBUTES  \
509         VALID_MACHINE_DECL_ATTRIBUTE VALID_MACHINE_TYPE_ATTRIBUTE       \
510         SET_DEFAULT_TYPE_ATTRIBUTES SET_DEFAULT_DECL_ATTRIBUTES         \
511         MERGE_MACHINE_TYPE_ATTRIBUTES MERGE_MACHINE_DECL_ATTRIBUTES     \
512         MD_INIT_BUILTINS MD_EXPAND_BUILTIN ASM_OUTPUT_CONSTRUCTOR       \
513         ASM_OUTPUT_DESTRUCTOR SIGNED_CHAR_SPEC MAX_CHAR_TYPE_SIZE       \
514         WCHAR_UNSIGNED UNIQUE_SECTION SELECT_SECTION SELECT_RTX_SECTION \
515         ENCODE_SECTION_INFO STRIP_NAME_ENCODING ASM_GLOBALIZE_LABEL     \
516         ASM_OUTPUT_MI_THUNK
517
518 /* Other obsolete target macros, or macros that used to be in target
519    headers and were not used, and may be obsolete or may never have
520    been used.  */
521  #pragma GCC poison INT_ASM_OP ASM_OUTPUT_EH_REGION_BEG                    \
522         ASM_OUTPUT_EH_REGION_END ASM_OUTPUT_LABELREF_AS_INT                \
523         DOESNT_NEED_UNWINDER EH_TABLE_LOOKUP OBJC_SELECTORS_WITHOUT_LABELS \
524         OMIT_EH_TABLE EASY_DIV_EXPR IMPLICIT_FIX_EXPR                      \
525         LONGJMP_RESTORE_FROM_STACK MAX_INT_TYPE_SIZE ASM_IDENTIFY_GCC      \
526         STDC_VALUE TRAMPOLINE_ALIGN ASM_IDENTIFY_GCC_AFTER_SOURCE          \
527         SLOW_ZERO_EXTEND SUBREG_REGNO_OFFSET DWARF_LINE_MIN_INSTR_LENGTH   \
528         TRADITIONAL_RETURN_FLOAT NO_BUILTIN_SIZE_TYPE                      \
529         NO_BUILTIN_PTRDIFF_TYPE NO_BUILTIN_WCHAR_TYPE NO_BUILTIN_WINT_TYPE \
530         BLOCK_PROFILER BLOCK_PROFILER_CODE FUNCTION_BLOCK_PROFILER         \
531         FUNCTION_BLOCK_PROFILER_EXIT MACHINE_STATE_SAVE                    \
532         MACHINE_STATE_RESTORE SCCS_DIRECTIVE SECTION_ASM_OP                \
533         ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
534
535 /* Hooks that are no longer used.  */
536  #pragma GCC poison LANG_HOOKS_FUNCTION_MARK LANG_HOOKS_FUNCTION_FREE   \
537         LANG_HOOKS_MARK_TREE
538
539 #endif /* IN_GCC */
540
541 /* Note: not all uses of the `index' token (e.g. variable names and
542    structure members) have been eliminated.  */
543 #undef bcopy
544 #undef bzero
545 #undef bcmp
546 #undef rindex
547  #pragma GCC poison bcopy bzero bcmp rindex
548
549 #endif /* GCC >= 3.0 */
550
551 /* SDCC specific */
552 #include "sdcc.h"
553
554 #endif /* ! GCC_SYSTEM_H */