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