2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
19 #include <sys/types.h>
26 #include "expression.h"
28 #include "linearize.h"
31 int verbose
, optimize
, optimize_size
, preprocessing
;
36 # define __GNUC_MINOR__ 95
37 # define __GNUC_PATCHLEVEL__ 0
40 int gcc_major
= __GNUC__
;
41 int gcc_minor
= __GNUC_MINOR__
;
42 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
44 struct token
*skip_to(struct token
*token
, int op
)
46 while (!match_op(token
, op
) && !eof_token(token
))
51 struct token
*expect(struct token
*token
, int op
, const char *where
)
53 if (!match_op(token
, op
)) {
54 static struct token bad_token
;
55 if (token
!= &bad_token
) {
56 bad_token
.next
= token
;
57 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
58 sparse_error(token
->pos
, "got %s", show_token(token
));
61 return skip_to(token
, op
);
67 unsigned int hexval(unsigned int c
)
75 retval
= c
- 'a' + 10;
78 retval
= c
- 'A' + 10;
84 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
86 static char buffer
[512];
89 vsprintf(buffer
, fmt
, args
);
90 name
= stream_name(pos
.stream
);
92 fprintf(stderr
, "%s:%d:%d: %s%s\n",
93 name
, pos
.line
, pos
.pos
, type
, buffer
);
96 static int max_warnings
= 100;
97 static int show_info
= 1;
99 void info(struct position pos
, const char * fmt
, ...)
106 do_warn("", pos
, fmt
, args
);
110 void warning(struct position pos
, const char * fmt
, ...)
119 if (!--max_warnings
) {
121 fmt
= "too many warnings";
125 do_warn("warning: ", pos
, fmt
, args
);
129 void sparse_error(struct position pos
, const char * fmt
, ...)
131 static int errors
= 0;
135 /* Shut up warnings after an error */
142 fmt
= "too many errors";
147 do_warn("error: ", pos
, fmt
, args
);
152 void error_die(struct position pos
, const char * fmt
, ...)
156 do_warn("error: ", pos
, fmt
, args
);
161 void die(const char *fmt
, ...)
164 static char buffer
[512];
167 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
170 fprintf(stderr
, "%s\n", buffer
);
174 static unsigned int pre_buffer_size
;
175 static char pre_buffer
[8192];
177 int Wdefault_bitfield_sign
= 0;
178 int Wone_bit_signed_bitfield
= 1;
179 int Wcast_truncate
= 1;
183 int Wundefined_preprocessor
= 0;
184 int Wptr_subtraction_blows
= 0;
185 int Wcast_to_address_space
= 0;
187 int Wtransparent_union
= 1;
189 int Waddress_space
= 1;
190 int Wenum_mismatch
= 1;
192 int Wuninitialized
= 1;
198 void add_pre_buffer(const char *fmt
, ...)
204 size
= pre_buffer_size
;
205 size
+= vsnprintf(pre_buffer
+ size
,
206 sizeof(pre_buffer
) - size
,
208 pre_buffer_size
= size
;
212 static char **handle_switch_D(char *arg
, char **next
)
214 const char *name
= arg
+ 1;
215 const char *value
= "1";
221 if (isspace((unsigned char)c
) || c
== '=') {
227 add_pre_buffer("#define %s %s\n", name
, value
);
231 static char **handle_switch_E(char *arg
, char **next
)
237 static char **handle_switch_v(char *arg
, char **next
)
241 } while (*++arg
== 'v');
245 static char **handle_switch_I(char *arg
, char **next
)
251 add_pre_buffer("#split_include\n");
254 case '\0': /* Plain "-I" */
257 die("missing argument for -I option");
260 add_pre_buffer("#add_include \"%s/\"\n", path
);
265 static char **handle_switch_i(char *arg
, char **next
)
267 if (*next
&& !strcmp(arg
, "include")) {
268 char *name
= *++next
;
269 int fd
= open(name
, O_RDONLY
);
276 if (*next
&& !strcmp(arg
, "imacros")) {
277 char *name
= *++next
;
278 int fd
= open(name
, O_RDONLY
);
285 else if (*next
&& !strcmp(arg
, "isystem")) {
286 char *path
= *++next
;
288 die("missing argument for -isystem option");
289 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
294 static char **handle_switch_M(char *arg
, char **next
)
296 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
298 die("missing argument for -%s option", arg
);
304 static char **handle_switch_m(char *arg
, char **next
)
306 if (!strcmp(arg
, "m64")) {
308 max_int_alignment
= 8;
309 bits_in_pointer
= 64;
310 pointer_alignment
= 8;
315 static char **handle_switch_o(char *arg
, char **next
)
317 if (!strcmp (arg
, "o") && *next
)
318 return next
+ 1; // "-o foo"
320 return next
; // "-ofoo" or (bogus) terminal "-o"
323 static const struct warning
{
327 { "cast-to-as", &Wcast_to_address_space
},
329 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
330 { "cast-truncate", &Wcast_truncate
},
331 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
332 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
333 { "undef", &Wundefined_preprocessor
},
334 { "bitwise", &Wbitwise
},
335 { "typesign", &Wtypesign
},
336 { "context", &Wcontext
},
337 { "transparent-union", &Wtransparent_union
},
338 { "shadow", &Wshadow
},
339 { "address-space", &Waddress_space
},
340 { "enum-mismatch", &Wenum_mismatch
},
341 { "do-while", &Wdo_while
},
342 { "uninitialized", &Wuninitialized
},
346 static char **handle_switch_W(char *arg
, char **next
)
352 // Prefixes "no" and "no-" mean to turn warning off.
353 if (p
[0] == 'n' && p
[1] == 'o') {
360 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
361 if (!strcmp(p
,warnings
[i
].name
)) {
362 *warnings
[i
].flag
= !no
;
371 static char **handle_switch_U(char *arg
, char **next
)
373 const char *name
= arg
+ 1;
374 add_pre_buffer ("#undef %s\n", name
);
378 static char **handle_switch_O(char *arg
, char **next
)
381 if (arg
[1] >= '0' && arg
[1] <= '9')
382 level
= arg
[1] - '0';
384 optimize_size
= arg
[1] == 's';
388 static char **handle_switch_f(char *arg
, char **next
)
393 if (!strncmp(arg
, "no-", 3)) {
397 /* handle switch here.. */
401 static char **handle_switch_G(char *arg
, char **next
)
403 if (!strcmp (arg
, "G") && *next
)
404 return next
+ 1; // "-G 0"
406 return next
; // "-G0" or (bogus) terminal "-G"
409 static char **handle_nostdinc(char *arg
, char **next
)
411 add_pre_buffer("#nostdinc\n");
415 static char **handle_dirafter(char *arg
, char **next
)
417 char *path
= *++next
;
419 die("missing argument for -dirafter option");
420 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
426 char **(*fn
)(char *, char**);
429 char **handle_switch(char *arg
, char **next
)
431 static struct switches cmd
[] = {
432 { "nostdinc", handle_nostdinc
},
433 { "dirafter", handle_dirafter
},
439 case 'D': return handle_switch_D(arg
, next
);
440 case 'E': return handle_switch_E(arg
, next
);
441 case 'I': return handle_switch_I(arg
, next
);
442 case 'i': return handle_switch_i(arg
, next
);
443 case 'M': return handle_switch_M(arg
, next
);
444 case 'm': return handle_switch_m(arg
, next
);
445 case 'o': return handle_switch_o(arg
, next
);
446 case 'U': return handle_switch_U(arg
, next
);
447 case 'v': return handle_switch_v(arg
, next
);
448 case 'W': return handle_switch_W(arg
, next
);
449 case 'O': return handle_switch_O(arg
, next
);
450 case 'f': return handle_switch_f(arg
, next
);
451 case 'G': return handle_switch_G(arg
, next
);
458 if (!strcmp(s
->name
, arg
))
459 return s
->fn(arg
, next
);
464 * Ignore unknown command line options:
465 * they're probably gcc switches
470 void declare_builtin_functions(void)
472 /* Gaah. gcc knows tons of builtin <string.h> functions */
473 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
474 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
475 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
476 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
477 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
478 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
479 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
480 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
481 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
482 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
484 /* And some random ones.. */
485 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
486 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
487 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
488 add_pre_buffer("extern void __builtin_trap(void);\n");
489 add_pre_buffer("extern int __builtin_ffs(int);\n");
490 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
491 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
492 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
493 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
494 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
495 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
496 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
497 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
498 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
499 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
500 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
503 void create_builtin_stream(void)
505 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
506 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
507 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
508 add_pre_buffer("#define __extension__\n");
509 add_pre_buffer("#define __pragma__\n");
511 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
512 // solaris/sparc that is really "unsigned int" and for linux/x86_64
513 // it is "long unsigned int". In either case we can probably
514 // get away with this. We need the #ifndef as cgcc will define
515 // the right __SIZE_TYPE__.
516 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
517 add_pre_buffer("#weak_define __STDC__ 1\n");
519 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
520 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
521 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
522 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
523 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
524 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
525 add_pre_buffer("#define __builtin_va_end(arg)\n");
526 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
528 /* FIXME! We need to do these as special magic macros at expansion time! */
529 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
530 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
531 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
534 add_pre_buffer("#define __OPTIMIZE__ 1\n");
536 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
539 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
541 // Pre-process the stream
542 token
= preprocess(token
);
544 if (preprocess_only
) {
545 while (!eof_token(token
)) {
547 struct token
*next
= token
->next
;
548 const char *separator
= "";
549 if (next
->pos
.whitespace
)
551 if (next
->pos
.newline
) {
552 separator
= "\n\t\t\t\t\t";
553 prec
= next
->pos
.pos
;
557 printf("%s%.*s", show_token(token
), prec
, separator
);
565 // Parse the resulting C code
566 while (!eof_token(token
))
567 token
= external_declaration(token
, &translation_unit_used_list
);
568 return translation_unit_used_list
;
571 static struct symbol_list
*sparse_file(const char *filename
)
576 if (strcmp (filename
, "-") == 0) {
579 fd
= open(filename
, O_RDONLY
);
581 die("No such file: %s", filename
);
584 // Tokenize the input stream
585 token
= tokenize(filename
, fd
, NULL
, includepath
);
588 return sparse_tokenstream(token
);
592 * This handles the "-include" directive etc: we're in global
593 * scope, and all types/macros etc will affect all the following
596 * NOTE NOTE NOTE! "#undef" of anything in this stage will
597 * affect all subsequent files too, ie we can have non-local
598 * behaviour between files!
600 static struct symbol_list
*sparse_initial(void)
604 // Prepend any "include" file to the stream.
605 // We're in global scope, it will affect all files!
608 token
= tokenize(include
, include_fd
, NULL
, includepath
);
610 // Prepend the initial built-in stream
611 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
612 return sparse_tokenstream(token
);
615 struct symbol_list
*sparse_initialize(int argc
, char **argv
)
619 struct symbol_list
*list
;
621 // Initialize symbol stream first, so that we can add defines etc
630 if (arg
[0] == '-' && arg
[1]) {
631 args
= handle_switch(arg
+1, args
);
636 * Hacky hacky hacky: we re-use the argument space
637 * to save the filenames.
645 // Initialize type system
648 create_builtin_stream();
649 add_pre_buffer("#define __CHECKER__ 1\n");
650 if (!preprocess_only
)
651 declare_builtin_functions();
653 list
= sparse_initial();
656 * Protect the initial token allocations, since
657 * they need to survive all the others
659 protect_token_alloc();
664 struct symbol_list
* __sparse(char **argv
)
666 struct symbol_list
*res
;
667 char *filename
, *next
;
669 /* Clear previous symbol list */
670 translation_unit_used_list
= NULL
;
681 res
= sparse_file(filename
);
684 /* Drop the tokens for this file after parsing */
691 struct symbol_list
* sparse(char **argv
)
693 struct symbol_list
*res
= __sparse(argv
);
695 /* Evaluate the complete symbol list */
696 evaluate_symbol_list(res
);