2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 #include <sys/types.h>
42 #include "expression.h"
44 #include "linearize.h"
48 int verbose
, optimize
, optimize_size
, preprocessing
;
53 # define __GNUC_MINOR__ 95
54 # define __GNUC_PATCHLEVEL__ 0
57 int gcc_major
= __GNUC__
;
58 int gcc_minor
= __GNUC_MINOR__
;
59 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
61 static const char *gcc_base_dir
= GCC_BASE
;
62 static const char *multiarch_dir
= MULTIARCH_TRIPLET
;
64 struct token
*skip_to(struct token
*token
, int op
)
66 while (!match_op(token
, op
) && !eof_token(token
))
71 struct token
*expect(struct token
*token
, int op
, const char *where
)
73 if (!match_op(token
, op
)) {
74 static struct token bad_token
;
75 if (token
!= &bad_token
) {
76 bad_token
.next
= token
;
77 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
78 sparse_error(token
->pos
, "got %s", show_token(token
));
81 return skip_to(token
, op
);
87 unsigned int hexval(unsigned int c
)
95 retval
= c
- 'a' + 10;
98 retval
= c
- 'A' + 10;
104 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
106 static char buffer
[512];
109 vsprintf(buffer
, fmt
, args
);
110 name
= stream_name(pos
.stream
);
112 fprintf(stderr
, "%s:%d:%d: %s%s\n",
113 name
, pos
.line
, pos
.pos
, type
, buffer
);
116 static int max_warnings
= 100;
117 static int show_info
= 1;
119 void info(struct position pos
, const char * fmt
, ...)
126 do_warn("", pos
, fmt
, args
);
130 static void do_error(struct position pos
, const char * fmt
, va_list args
)
132 static int errors
= 0;
135 /* Shut up warnings after an error */
142 fmt
= "too many errors";
146 do_warn("error: ", pos
, fmt
, args
);
150 void warning(struct position pos
, const char * fmt
, ...)
156 do_error(pos
, fmt
, args
);
166 if (!--max_warnings
) {
168 fmt
= "too many warnings";
172 do_warn("warning: ", pos
, fmt
, args
);
176 void sparse_error(struct position pos
, const char * fmt
, ...)
180 do_error(pos
, fmt
, args
);
184 void expression_error(struct expression
*expr
, const char *fmt
, ...)
188 do_error(expr
->pos
, fmt
, args
);
190 expr
->ctype
= &bad_ctype
;
193 void error_die(struct position pos
, const char * fmt
, ...)
197 do_warn("error: ", pos
, fmt
, args
);
202 void die(const char *fmt
, ...)
205 static char buffer
[512];
208 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
211 fprintf(stderr
, "%s\n", buffer
);
215 static struct token
*pre_buffer_begin
= NULL
;
216 static struct token
*pre_buffer_end
= NULL
;
218 int Waddress_space
= 1;
221 int Wcast_truncate
= 1;
224 int Wdeclarationafterstatement
= -1;
225 int Wdefault_bitfield_sign
= 0;
226 int Wdesignated_init
= 1;
228 int Winit_cstring
= 0;
229 int Wenum_mismatch
= 1;
230 int Wsparse_error
= 0;
231 int Wnon_pointer_null
= 1;
232 int Wold_initializer
= 1;
233 int Wone_bit_signed_bitfield
= 1;
234 int Wparen_string
= 0;
235 int Wptr_subtraction_blows
= 0;
236 int Wreturn_void
= 0;
238 int Wsizeof_bool
= 0;
239 int Wtautological_compare
= 0;
240 int Wtransparent_union
= 0;
243 int Wuninitialized
= 1;
244 int Wunknown_attribute
= 1;
252 static enum { STANDARD_C89
,
258 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
265 #define ARCH_M64_DEFAULT ARCH_LP64
267 #define ARCH_M64_DEFAULT ARCH_LP32
270 int arch_m64
= ARCH_M64_DEFAULT
;
271 int arch_msize_long
= 0;
273 #define CMDLINE_INCLUDE 20
274 static int cmdline_include_nr
= 0;
275 static char *cmdline_include
[CMDLINE_INCLUDE
];
278 void add_pre_buffer(const char *fmt
, ...)
282 struct token
*begin
, *end
;
286 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
288 begin
= tokenize_buffer(buffer
, size
, &end
);
289 if (!pre_buffer_begin
)
290 pre_buffer_begin
= begin
;
292 pre_buffer_end
->next
= begin
;
293 pre_buffer_end
= end
;
296 static char **handle_switch_D(char *arg
, char **next
)
298 const char *name
= arg
+ 1;
299 const char *value
= "1";
301 if (!*name
|| isspace((unsigned char)*name
))
302 die("argument to `-D' is missing");
309 if (isspace((unsigned char)c
) || c
== '=') {
315 add_pre_buffer("#define %s %s\n", name
, value
);
319 static char **handle_switch_E(char *arg
, char **next
)
326 static char **handle_switch_I(char *arg
, char **next
)
332 add_pre_buffer("#split_include\n");
335 case '\0': /* Plain "-I" */
338 die("missing argument for -I option");
341 add_pre_buffer("#add_include \"%s/\"\n", path
);
346 static void add_cmdline_include(char *filename
)
348 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
349 die("too many include files for %s\n", filename
);
350 cmdline_include
[cmdline_include_nr
++] = filename
;
353 static char **handle_switch_i(char *arg
, char **next
)
355 if (*next
&& !strcmp(arg
, "include"))
356 add_cmdline_include(*++next
);
357 else if (*next
&& !strcmp(arg
, "imacros"))
358 add_cmdline_include(*++next
);
359 else if (*next
&& !strcmp(arg
, "isystem")) {
360 char *path
= *++next
;
362 die("missing argument for -isystem option");
363 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
364 } else if (*next
&& !strcmp(arg
, "idirafter")) {
365 char *path
= *++next
;
367 die("missing argument for -idirafter option");
368 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
373 static char **handle_switch_M(char *arg
, char **next
)
375 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
377 die("missing argument for -%s option", arg
);
383 static char **handle_multiarch_dir(char *arg
, char **next
)
385 multiarch_dir
= *++next
;
387 die("missing argument for -multiarch-dir option");
391 static char **handle_switch_m(char *arg
, char **next
)
393 if (!strcmp(arg
, "m64")) {
394 arch_m64
= ARCH_LP64
;
395 } else if (!strcmp(arg
, "m32")) {
396 arch_m64
= ARCH_LP32
;
397 } else if (!strcmp(arg
, "msize-llp64")) {
398 arch_m64
= ARCH_LLP64
;
399 } else if (!strcmp(arg
, "msize-long")) {
401 } else if (!strcmp(arg
, "multiarch-dir"))
402 return handle_multiarch_dir(arg
, next
);
406 static void handle_arch_m64_finalize(void)
414 max_int_alignment
= 8;
415 size_t_ctype
= &ulong_ctype
;
416 ssize_t_ctype
= &long_ctype
;
417 add_pre_buffer("#weak_define __LP64__ 1\n");
418 add_pre_buffer("#weak_define _LP64 1\n");
419 goto case_64bit_common
;
422 max_int_alignment
= 4;
423 size_t_ctype
= &ullong_ctype
;
424 ssize_t_ctype
= &llong_ctype
;
425 add_pre_buffer("#weak_define __LLP64__ 1\n");
426 goto case_64bit_common
;
428 bits_in_pointer
= 64;
429 pointer_alignment
= 8;
431 add_pre_buffer("#weak_define __x86_64__ 1\n");
437 static void handle_arch_msize_long_finalize(void)
439 if (arch_msize_long
) {
440 size_t_ctype
= &ulong_ctype
;
441 ssize_t_ctype
= &long_ctype
;
445 static void handle_arch_finalize(void)
447 handle_arch_m64_finalize();
448 handle_arch_msize_long_finalize();
452 static char **handle_switch_o(char *arg
, char **next
)
454 if (!strcmp (arg
, "o")) { // "-o foo"
456 die("argument to '-o' is missing");
463 static const struct warning
{
467 { "address-space", &Waddress_space
},
468 { "bitwise", &Wbitwise
},
469 { "cast-to-as", &Wcast_to_as
},
470 { "cast-truncate", &Wcast_truncate
},
471 { "context", &Wcontext
},
473 { "declaration-after-statement", &Wdeclarationafterstatement
},
474 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
475 { "designated-init", &Wdesignated_init
},
476 { "do-while", &Wdo_while
},
477 { "enum-mismatch", &Wenum_mismatch
},
478 { "sparse-error", &Wsparse_error
},
479 { "init-cstring", &Winit_cstring
},
480 { "non-pointer-null", &Wnon_pointer_null
},
481 { "old-initializer", &Wold_initializer
},
482 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
483 { "paren-string", &Wparen_string
},
484 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
485 { "return-void", &Wreturn_void
},
486 { "shadow", &Wshadow
},
487 { "sizeof-bool", &Wsizeof_bool
},
488 { "tautological-compare", &Wtautological_compare
},
489 { "transparent-union", &Wtransparent_union
},
490 { "typesign", &Wtypesign
},
491 { "undef", &Wundef
},
492 { "uninitialized", &Wuninitialized
},
493 { "unknown-attribute", &Wunknown_attribute
},
504 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
506 int flag
= WARNING_ON
;
510 if (!strcmp(p
, "sparse-all")) {
511 for (i
= 0; i
< n
; i
++) {
512 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
&& warnings
[i
].flag
!= &Wsparse_error
)
513 *warnings
[i
].flag
= WARNING_ON
;
517 // Prefixes "no" and "no-" mean to turn warning off.
518 if (p
[0] == 'n' && p
[1] == 'o') {
522 flag
= WARNING_FORCE_OFF
;
525 for (i
= 0; i
< n
; i
++) {
526 if (!strcmp(p
,warnings
[i
].name
)) {
527 *warnings
[i
].flag
= flag
;
536 static char **handle_switch_W(char *arg
, char **next
)
538 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, ARRAY_SIZE(warnings
));
546 static struct warning debugs
[] = {
547 { "entry", &dbg_entry
},
548 { "dead", &dbg_dead
},
552 static char **handle_switch_v(char *arg
, char **next
)
554 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, ARRAY_SIZE(debugs
));
561 } while (*++arg
== 'v');
566 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
570 for (i
= 0; i
< n
; i
++) {
571 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
572 *warnings
[i
].flag
= WARNING_OFF
;
576 static void handle_switch_W_finalize(void)
578 handle_onoff_switch_finalize(warnings
, ARRAY_SIZE(warnings
));
580 /* default Wdeclarationafterstatement based on the C dialect */
581 if (-1 == Wdeclarationafterstatement
)
587 Wdeclarationafterstatement
= 1;
595 Wdeclarationafterstatement
= 0;
605 static void handle_switch_v_finalize(void)
607 handle_onoff_switch_finalize(debugs
, ARRAY_SIZE(debugs
));
610 static char **handle_switch_U(char *arg
, char **next
)
612 const char *name
= arg
+ 1;
613 add_pre_buffer ("#undef %s\n", name
);
617 static char **handle_switch_O(char *arg
, char **next
)
620 if (arg
[1] >= '0' && arg
[1] <= '9')
621 level
= arg
[1] - '0';
623 optimize_size
= arg
[1] == 's';
627 static char **handle_switch_ftabstop(char *arg
, char **next
)
633 die("error: missing argument to \"-ftabstop=\"");
635 /* we silently ignore silly values */
636 val
= strtoul(arg
, &end
, 10);
637 if (*end
== '\0' && 1 <= val
&& val
<= 100)
643 static char **handle_switch_f(char *arg
, char **next
)
647 if (!strncmp(arg
, "tabstop=", 8))
648 return handle_switch_ftabstop(arg
+8, next
);
650 /* handle switches w/ arguments above, boolean and only boolean below */
652 if (!strncmp(arg
, "no-", 3)) {
655 /* handle switch here.. */
659 static char **handle_switch_G(char *arg
, char **next
)
661 if (!strcmp (arg
, "G") && *next
)
662 return next
+ 1; // "-G 0"
664 return next
; // "-G0" or (bogus) terminal "-G"
667 static char **handle_switch_a(char *arg
, char **next
)
669 if (!strcmp (arg
, "ansi"))
670 standard
= STANDARD_C89
;
675 static char **handle_switch_s(char *arg
, char **next
)
677 if (!strncmp (arg
, "std=", 4))
681 if (!strcmp (arg
, "c89") ||
682 !strcmp (arg
, "iso9899:1990"))
683 standard
= STANDARD_C89
;
685 else if (!strcmp (arg
, "iso9899:199409"))
686 standard
= STANDARD_C94
;
688 else if (!strcmp (arg
, "c99") ||
689 !strcmp (arg
, "c9x") ||
690 !strcmp (arg
, "iso9899:1999") ||
691 !strcmp (arg
, "iso9899:199x"))
692 standard
= STANDARD_C99
;
694 else if (!strcmp (arg
, "gnu89"))
695 standard
= STANDARD_GNU89
;
697 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
698 standard
= STANDARD_GNU99
;
700 else if (!strcmp(arg
, "c11") ||
701 !strcmp(arg
, "c1x") ||
702 !strcmp(arg
, "iso9899:2011"))
703 standard
= STANDARD_C11
;
705 else if (!strcmp(arg
, "gnu11"))
706 standard
= STANDARD_GNU11
;
709 die ("Unsupported C dialect");
715 static char **handle_nostdinc(char *arg
, char **next
)
717 add_pre_buffer("#nostdinc\n");
721 static char **handle_switch_n(char *arg
, char **next
)
723 if (!strcmp (arg
, "nostdinc"))
724 return handle_nostdinc(arg
, next
);
729 static char **handle_base_dir(char *arg
, char **next
)
731 gcc_base_dir
= *++next
;
733 die("missing argument for -gcc-base-dir option");
737 static char **handle_switch_g(char *arg
, char **next
)
739 if (!strcmp (arg
, "gcc-base-dir"))
740 return handle_base_dir(arg
, next
);
746 static char **handle_version(char *arg
, char **next
)
748 printf("%s\n", SPARSE_VERSION
);
752 static char **handle_param(char *arg
, char **next
)
756 /* For now just skip any '--param=*' or '--param *' */
759 } else if (isspace((unsigned char)*arg
) || *arg
== '=') {
764 die("missing argument for --param option");
771 char **(*fn
)(char *, char **);
772 unsigned int prefix
:1;
775 static char **handle_long_options(char *arg
, char **next
)
777 static struct switches cmd
[] = {
778 { "param", handle_param
, 1 },
779 { "version", handle_version
},
782 struct switches
*s
= cmd
;
785 int optlen
= strlen(s
->name
);
786 if (!strncmp(s
->name
, arg
, optlen
+ !s
->prefix
))
787 return s
->fn(arg
+ optlen
, next
);
793 static char **handle_switch(char *arg
, char **next
)
796 case 'a': return handle_switch_a(arg
, next
);
797 case 'D': return handle_switch_D(arg
, next
);
798 case 'E': return handle_switch_E(arg
, next
);
799 case 'f': return handle_switch_f(arg
, next
);
800 case 'g': return handle_switch_g(arg
, next
);
801 case 'G': return handle_switch_G(arg
, next
);
802 case 'I': return handle_switch_I(arg
, next
);
803 case 'i': return handle_switch_i(arg
, next
);
804 case 'M': return handle_switch_M(arg
, next
);
805 case 'm': return handle_switch_m(arg
, next
);
806 case 'n': return handle_switch_n(arg
, next
);
807 case 'o': return handle_switch_o(arg
, next
);
808 case 'O': return handle_switch_O(arg
, next
);
809 case 's': return handle_switch_s(arg
, next
);
810 case 'U': return handle_switch_U(arg
, next
);
811 case 'v': return handle_switch_v(arg
, next
);
812 case 'W': return handle_switch_W(arg
, next
);
813 case '-': return handle_long_options(arg
+ 1, next
);
819 * Ignore unknown command line options:
820 * they're probably gcc switches
825 static void predefined_macros(void)
827 unsigned long long val
;
829 add_pre_buffer("#define __CHECKER__ 1\n");
831 val
= (1ULL << (bits_in_long
-1)) - 1;
832 add_pre_buffer("#weak_define __LONG_MAX__ %#llxLL\n", val
);
833 add_pre_buffer("#weak_define __SIZEOF_POINTER__ %d\n", bits_in_pointer
/8);
836 void declare_builtin_functions(void)
838 /* Gaah. gcc knows tons of builtin <string.h> functions */
839 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
840 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
841 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
842 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
843 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
844 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
845 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
846 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
847 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
848 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
849 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
850 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
851 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
852 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
853 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
855 /* And bitwise operations.. */
856 add_pre_buffer("extern int __builtin_clz(int);\n");
857 add_pre_buffer("extern int __builtin_clzl(long);\n");
858 add_pre_buffer("extern int __builtin_clzll(long long);\n");
859 add_pre_buffer("extern int __builtin_ctz(int);\n");
860 add_pre_buffer("extern int __builtin_ctzl(long);\n");
861 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
862 add_pre_buffer("extern int __builtin_ffs(int);\n");
863 add_pre_buffer("extern int __builtin_ffsl(long);\n");
864 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
865 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
866 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
867 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
869 /* And byte swaps.. */
870 add_pre_buffer("extern unsigned short ____builtin_bswap16(unsigned short);\n");
871 add_pre_buffer("extern unsigned int ____builtin_bswap32(unsigned int);\n");
872 add_pre_buffer("extern unsigned long long ____builtin_bswap64(unsigned long long);\n");
873 add_pre_buffer("#define __sparse_constant_swab16(x) ((unsigned short)("
874 " (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) |"
875 " (((unsigned short)(x) & (unsigned short)0xff00U) >> 8)))\n");
876 add_pre_buffer("#define __sparse_constant_swab32(x) ((unsigned int)("
877 " (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) |"
878 " (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) |"
879 " (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) |"
880 " (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))\n");
881 add_pre_buffer("#define __sparse_constant_swab64(x) ((unsigned long long)("
882 " (((unsigned long long)(x) & (unsigned long long)0x00000000000000ffULL) << 56) |"
883 " (((unsigned long long)(x) & (unsigned long long)0x000000000000ff00ULL) << 40) |"
884 " (((unsigned long long)(x) & (unsigned long long)0x0000000000ff0000ULL) << 24) |"
885 " (((unsigned long long)(x) & (unsigned long long)0x00000000ff000000ULL) << 8) |"
886 " (((unsigned long long)(x) & (unsigned long long)0x000000ff00000000ULL) >> 8) |"
887 " (((unsigned long long)(x) & (unsigned long long)0x0000ff0000000000ULL) >> 24) |"
888 " (((unsigned long long)(x) & (unsigned long long)0x00ff000000000000ULL) >> 40) |"
889 " (((unsigned long long)(x) & (unsigned long long)0xff00000000000000ULL) >> 56)))\n");
890 add_pre_buffer("#define __builtin_bswap16(x)"
891 " (__builtin_constant_p((unsigned short)(x)) ?"
892 " __sparse_constant_swab16(x) :"
893 " ____builtin_bswap16(x))\n");
894 add_pre_buffer("#define __builtin_bswap32(x)"
895 " (__builtin_constant_p((unsigned int)(x)) ?"
896 " __sparse_constant_swab32(x) :"
897 " ____builtin_bswap32(x))\n");
898 add_pre_buffer("#define __builtin_bswap64(x)"
899 " (__builtin_constant_p((unsigned long long)(x)) ?"
900 " __sparse_constant_swab64(x) :"
901 " ____builtin_bswap64(x))\n");
903 /* And atomic memory access functions.. */
904 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
905 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
906 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
907 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
908 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
909 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
910 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
911 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
912 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
913 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
914 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
915 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
916 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
917 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
918 add_pre_buffer("extern void __sync_synchronize();\n");
919 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
920 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
922 /* And some random ones.. */
923 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
924 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
925 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
926 add_pre_buffer("extern void __builtin_trap(void);\n");
927 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
928 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
929 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
930 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
931 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
932 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
933 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
934 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
935 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
936 add_pre_buffer("extern long __builtin_labs(long);\n");
937 add_pre_buffer("extern double __builtin_fabs(double);\n");
938 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
940 /* Add Blackfin-specific stuff */
943 "extern void __builtin_bfin_csync(void);\n"
944 "extern void __builtin_bfin_ssync(void);\n"
945 "extern int __builtin_bfin_norm_fr1x32(int);\n"
949 /* And some floating point stuff.. */
950 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
951 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
952 add_pre_buffer("extern int __builtin_isless(float, float);\n");
953 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
954 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
955 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
957 /* And some INFINITY / NAN stuff.. */
958 add_pre_buffer("extern double __builtin_huge_val(void);\n");
959 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
960 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
961 add_pre_buffer("extern double __builtin_inf(void);\n");
962 add_pre_buffer("extern float __builtin_inff(void);\n");
963 add_pre_buffer("extern long double __builtin_infl(void);\n");
964 add_pre_buffer("extern double __builtin_nan(const char *);\n");
965 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
966 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
968 /* And some __FORTIFY_SOURCE ones.. */
969 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
970 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
971 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
972 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
973 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
974 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
975 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
976 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
977 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
978 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
979 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
980 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
981 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
982 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
983 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
986 void create_builtin_stream(void)
988 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
989 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
990 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
992 /* add the multiarch include directories, if any */
993 if (multiarch_dir
&& *multiarch_dir
) {
994 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir
);
995 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir
);
998 /* We add compiler headers path here because we have to parse
999 * the arguments to get it, falling back to default. */
1000 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
1001 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
1003 add_pre_buffer("#define __extension__\n");
1004 add_pre_buffer("#define __pragma__\n");
1006 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
1007 // solaris/sparc that is really "unsigned int" and for linux/x86_64
1008 // it is "long unsigned int". In either case we can probably
1009 // get away with this. We need the #weak_define as cgcc will define
1010 // the right __SIZE_TYPE__.
1011 if (size_t_ctype
== &ulong_ctype
)
1012 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
1014 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
1015 add_pre_buffer("#weak_define __STDC__ 1\n");
1020 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1024 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1025 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1029 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1030 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1033 case STANDARD_GNU89
:
1036 case STANDARD_GNU99
:
1037 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1041 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1042 case STANDARD_GNU11
:
1043 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1044 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1045 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1046 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1053 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1054 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1055 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1056 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1057 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1058 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1059 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1060 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1061 add_pre_buffer("#define __builtin_va_end(arg)\n");
1062 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1063 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1065 /* FIXME! We need to do these as special magic macros at expansion time! */
1066 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1069 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1071 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1073 /* GCC defines these for limits.h */
1074 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__
) "\n");
1075 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__
) "\n");
1076 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__
) "\n");
1077 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__
) "\n");
1078 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__
) "\n");
1079 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__
) "\n");
1080 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__
) "\n");
1081 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__
) "\n");
1084 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
1086 // Preprocess the stream
1087 token
= preprocess(token
);
1089 if (preprocess_only
) {
1090 while (!eof_token(token
)) {
1092 struct token
*next
= token
->next
;
1093 const char *separator
= "";
1094 if (next
->pos
.whitespace
)
1096 if (next
->pos
.newline
) {
1097 separator
= "\n\t\t\t\t\t";
1098 prec
= next
->pos
.pos
;
1102 printf("%s%.*s", show_token(token
), prec
, separator
);
1110 // Parse the resulting C code
1111 while (!eof_token(token
))
1112 token
= external_declaration(token
, &translation_unit_used_list
);
1113 return translation_unit_used_list
;
1116 static struct symbol_list
*sparse_file(const char *filename
)
1119 struct token
*token
;
1121 if (strcmp (filename
, "-") == 0) {
1124 fd
= open(filename
, O_RDONLY
);
1126 die("No such file: %s", filename
);
1129 // Tokenize the input stream
1130 token
= tokenize(filename
, fd
, NULL
, includepath
);
1133 return sparse_tokenstream(token
);
1137 * This handles the "-include" directive etc: we're in global
1138 * scope, and all types/macros etc will affect all the following
1141 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1142 * affect all subsequent files too, i.e. we can have non-local
1143 * behaviour between files!
1145 static struct symbol_list
*sparse_initial(void)
1149 // Prepend any "include" file to the stream.
1150 // We're in global scope, it will affect all files!
1151 for (i
= 0; i
< cmdline_include_nr
; i
++)
1152 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
1154 return sparse_tokenstream(pre_buffer_begin
);
1157 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
1160 struct symbol_list
*list
;
1162 // Initialize symbol stream first, so that we can add defines etc
1167 char *arg
= *++args
;
1171 if (arg
[0] == '-' && arg
[1]) {
1172 args
= handle_switch(arg
+1, args
);
1175 add_ptr_list_notag(filelist
, arg
);
1177 handle_switch_W_finalize();
1178 handle_switch_v_finalize();
1180 handle_arch_finalize();
1183 if (!ptr_list_empty(filelist
)) {
1184 // Initialize type system
1187 create_builtin_stream();
1188 predefined_macros();
1189 if (!preprocess_only
)
1190 declare_builtin_functions();
1192 list
= sparse_initial();
1195 * Protect the initial token allocations, since
1196 * they need to survive all the others
1198 protect_token_alloc();
1203 struct symbol_list
* sparse_keep_tokens(char *filename
)
1205 struct symbol_list
*res
;
1207 /* Clear previous symbol list */
1208 translation_unit_used_list
= NULL
;
1211 res
= sparse_file(filename
);
1218 struct symbol_list
* __sparse(char *filename
)
1220 struct symbol_list
*res
;
1222 res
= sparse_keep_tokens(filename
);
1224 /* Drop the tokens for this file after parsing */
1225 clear_token_alloc();
1231 struct symbol_list
* sparse(char *filename
)
1233 struct symbol_list
*res
= __sparse(filename
);
1235 /* Evaluate the complete symbol list */
1236 evaluate_symbol_list(res
);