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>
27 #include "expression.h"
29 #include "linearize.h"
32 int verbose
, optimize
, preprocessing
;
34 struct token
*skip_to(struct token
*token
, int op
)
36 while (!match_op(token
, op
) && !eof_token(token
))
41 struct token
*expect(struct token
*token
, int op
, const char *where
)
43 if (!match_op(token
, op
)) {
44 static struct token bad_token
;
45 if (token
!= &bad_token
) {
46 bad_token
.next
= token
;
47 warning(token
->pos
, "Expected %s %s", show_special(op
), where
);
48 warning(token
->pos
, "got %s", show_token(token
));
51 return skip_to(token
, op
);
57 unsigned int hexval(unsigned int c
)
65 retval
= c
- 'a' + 10;
68 retval
= c
- 'A' + 10;
74 int ptr_list_size(struct ptr_list
*head
)
79 struct ptr_list
*list
= head
;
82 } while ((list
= list
->next
) != head
);
88 * Linearize the entries of a list up to a total of 'max',
89 * and return the nr of entries linearized.
91 * The array to linearize into (second argument) should really
92 * be "void *x[]", but we want to let people fill in any kind
93 * of pointer array, so let's just call it "void *".
95 int linearize_ptr_list(struct ptr_list
*head
, void **arr
, int max
)
98 if (head
&& max
> 0) {
99 struct ptr_list
*list
= head
;
105 memcpy(arr
, list
->list
, i
*sizeof(void *));
111 } while ((list
= list
->next
) != head
);
117 * When we've walked the list and deleted entries,
118 * we may need to re-pack it so that we don't have
119 * any empty blocks left (empty blocks upset the
122 void pack_ptr_list(struct ptr_list
**listp
)
124 struct ptr_list
*head
= *listp
;
127 struct ptr_list
*entry
= head
;
129 struct ptr_list
*next
;
133 struct ptr_list
*prev
;
150 } while (entry
!= head
);
154 void split_ptr_list_head(struct ptr_list
*head
)
156 int old
= head
->nr
, nr
= old
/ 2;
157 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
158 struct ptr_list
*next
= head
->next
;
162 newlist
->next
= next
;
163 next
->prev
= newlist
;
164 newlist
->prev
= head
;
165 head
->next
= newlist
;
167 memcpy(newlist
->list
, head
->list
+ old
, nr
* sizeof(void *));
168 memset(head
->list
+ old
, 0xf0, nr
* sizeof(void *));
171 void **__add_ptr_list(struct ptr_list
**listp
, void *ptr
)
173 struct ptr_list
*list
= *listp
;
174 struct ptr_list
*last
= NULL
; /* gcc complains needlessly */
178 if (!list
|| (nr
= (last
= list
->prev
)->nr
) >= LIST_NODE_NR
) {
179 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
181 die("out of memory for symbol/statement lists");
182 memset(newlist
, 0, sizeof(*newlist
));
184 newlist
->next
= newlist
;
185 newlist
->prev
= newlist
;
188 newlist
->prev
= last
;
189 newlist
->next
= list
;
190 list
->prev
= newlist
;
191 last
->next
= newlist
;
196 ret
= last
->list
+ nr
;
203 void delete_ptr_list_entry(struct ptr_list
**list
, void *entry
, int count
)
207 FOR_EACH_PTR(*list
, ptr
) {
209 DELETE_CURRENT_PTR(ptr
);
213 } END_FOR_EACH_PTR(ptr
);
219 void replace_ptr_list_entry(struct ptr_list
**list
, void *old_ptr
, void *new_ptr
, int count
)
223 FOR_EACH_PTR(*list
, ptr
) {
225 REPLACE_CURRENT_PTR(ptr
, new_ptr
);
229 }END_FOR_EACH_PTR(ptr
);
233 void * delete_ptr_list_last(struct ptr_list
**head
)
236 struct ptr_list
*last
, *first
= *head
;
242 ptr
= last
->list
[--last
->nr
];
244 first
->prev
= last
->prev
;
245 last
->prev
->next
= first
;
253 void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
)
256 FOR_EACH_PTR(a
, entry
) {
257 add_ptr_list(b
, entry
);
258 } END_FOR_EACH_PTR(entry
);
261 void __free_ptr_list(struct ptr_list
**listp
)
263 struct ptr_list
*tmp
, *list
= *listp
;
268 list
->prev
->next
= NULL
;
278 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
280 static char buffer
[512];
283 vsprintf(buffer
, fmt
, args
);
284 name
= input_streams
[pos
.stream
].name
;
286 fprintf(stderr
, "%s:%d:%d: %s%s\n",
287 name
, pos
.line
, pos
.pos
, type
, buffer
);
290 void info(struct position pos
, const char * fmt
, ...)
294 do_warn("", pos
, fmt
, args
);
298 void warning(struct position pos
, const char * fmt
, ...)
300 static int warnings
= 0;
303 if (warnings
> 100) {
307 fmt
= "too many warnings";
312 do_warn("warning: ", pos
, fmt
, args
);
317 void error(struct position pos
, const char * fmt
, ...)
319 static int errors
= 0;
326 fmt
= "too many errors";
331 do_warn("error: ", pos
, fmt
, args
);
336 void error_die(struct position pos
, const char * fmt
, ...)
340 do_warn("error: ", pos
, fmt
, args
);
345 void die(const char *fmt
, ...)
348 static char buffer
[512];
351 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
354 fprintf(stderr
, "%s\n", buffer
);
358 unsigned int pre_buffer_size
;
359 unsigned char pre_buffer
[8192];
361 int Wdefault_bitfield_sign
= 0;
365 int Wundefined_preprocessor
= 0;
370 void add_pre_buffer(const char *fmt
, ...)
376 size
= pre_buffer_size
;
377 size
+= vsnprintf(pre_buffer
+ size
,
378 sizeof(pre_buffer
) - size
,
380 pre_buffer_size
= size
;
384 char **handle_switch_D(char *arg
, char **next
)
386 const char *name
= arg
+ 1;
387 const char *value
= "1";
393 if (isspace((unsigned char)c
) || c
== '=') {
399 add_pre_buffer("#define %s %s\n", name
, value
);
403 char **handle_switch_E(char *arg
, char **next
)
409 char **handle_switch_v(char *arg
, char **next
)
415 char **handle_switch_I(char *arg
, char **next
)
421 /* Explaining '-I-' with a google search:
423 * "Specifying -I after -I- searches for #include directories.
424 * If -I- is specified before, it searches for #include "file"
425 * and not #include ."
427 * Which didn't explain it at all to me. Maybe somebody else can
428 * explain it properly. We ignore it for now.
432 case '\0': /* Plain "-I" */
435 die("missing argument for -I option");
438 add_pre_buffer("#add_include \"%s/\"\n", path
);
443 char **handle_switch_i(char *arg
, char **next
)
445 if (*next
&& !strcmp(arg
, "include")) {
446 char *name
= *++next
;
447 int fd
= open(name
, O_RDONLY
);
457 char **handle_switch_M(char *arg
, char **next
)
459 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
461 die("missing argument for -%s option", arg
);
467 char **handle_switch_m(char *arg
, char **next
)
469 if (!strcmp(arg
, "m64")) {
471 max_int_alignment
= 8;
472 bits_in_pointer
= 64;
473 pointer_alignment
= 8;
478 char **handle_switch_o(char *arg
, char **next
)
480 if (!strcmp (arg
, "o") && *next
)
481 return next
+ 1; // "-o foo"
483 return next
; // "-ofoo" or (bogus) terminal "-o"
486 const struct warning
{
490 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
491 { "undef", &Wundefined_preprocessor
},
492 { "bitwise", &Wbitwise
},
493 { "typesign", &Wtypesign
},
494 { "context", &Wcontext
},
498 char **handle_switch_W(char *arg
, char **next
)
504 // Prefixes "no" and "no-" mean to turn warning off.
505 if (p
[0] == 'n' && p
[1] == 'o') {
512 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
513 if (!strcmp(p
,warnings
[i
].name
)) {
514 *warnings
[i
].flag
= !no
;
523 char **handle_switch_U(char *arg
, char **next
)
525 const char *name
= arg
+ 1;
526 add_pre_buffer ("#undef %s\n", name
);
530 char **handle_switch_O(char *arg
, char **next
)
533 if (arg
[1] >= '0' && arg
[1] <= '9')
534 level
= arg
[1] - '0';
539 char **handle_nostdinc(char *arg
, char **next
)
541 add_pre_buffer("#nostdinc\n");
547 char **(*fn
)(char *, char**);
550 char **handle_switch(char *arg
, char **next
)
553 static struct switches cmd
[] = {
554 { "nostdinc", handle_nostdinc
},
560 case 'D': rc
= handle_switch_D(arg
, next
); break;
561 case 'E': rc
= handle_switch_E(arg
, next
); break;
562 case 'I': rc
= handle_switch_I(arg
, next
); break;
563 case 'i': rc
= handle_switch_i(arg
, next
); break;
564 case 'M': rc
= handle_switch_M(arg
, next
); break;
565 case 'm': rc
= handle_switch_m(arg
, next
); break;
566 case 'o': rc
= handle_switch_o(arg
, next
); break;
567 case 'U': rc
= handle_switch_U(arg
, next
); break;
568 case 'v': rc
= handle_switch_v(arg
, next
); break;
569 case 'W': rc
= handle_switch_W(arg
, next
); break;
570 case 'O': rc
= handle_switch_O(arg
, next
); break;
577 if (!strcmp(s
->name
, arg
))
578 return s
->fn(arg
, next
);
583 * Ignore unknown command line options:
584 * they're probably gcc switches
589 void declare_builtin_functions(void)
591 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
592 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
593 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
594 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
595 add_pre_buffer("extern void __builtin_trap(void);\n");
596 add_pre_buffer("extern int __builtin_ffs(int);\n");
597 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
600 void create_builtin_stream(void)
602 add_pre_buffer("#define __GNUC__ 2\n");
603 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
604 add_pre_buffer("#define __extension__\n");
605 add_pre_buffer("#define __pragma__\n");
607 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
608 // solaris/sparc that is really "unsigned int" and for linux/x86_64
609 // it is "long unsigned int". In either case we can probably
610 // get away with this. We need the #ifndef as cgcc will define
611 // the right __SIZE_TYPE__.
612 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
613 add_pre_buffer("#weak_define __STDC__ 1\n");
615 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
616 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
617 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
618 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
619 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
620 add_pre_buffer("#define __builtin_va_end(arg)\n");
623 static void do_predefined(char *filename
)
625 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
626 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
627 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
630 struct symbol_list
*sparse(int argc
, char **argv
)
633 char *filename
= NULL
, **args
;
636 // Initialize symbol stream first, so that we can add defines etc
644 if (arg
[0] == '-' && arg
[1]) {
645 args
= handle_switch(arg
+1, args
);
652 die("no input files given");
654 // Initialize type system
657 create_builtin_stream();
658 add_pre_buffer("#define __CHECKER__ 1\n");
659 if (!preprocess_only
)
660 declare_builtin_functions();
662 do_predefined(filename
);
664 if (strcmp (filename
, "-") == 0) {
667 fd
= open(filename
, O_RDONLY
);
669 die("No such file: %s", filename
);
672 // Tokenize the input stream
673 token
= tokenize(filename
, fd
, NULL
, includepath
);
676 // Prepend any "include" file to the stream.
678 token
= tokenize(include
, include_fd
, token
, includepath
);
680 // Prepend the initial built-in stream
681 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
683 // Pre-process the stream
684 token
= preprocess(token
);
686 if (preprocess_only
) {
687 while (!eof_token(token
)) {
689 struct token
*next
= token
->next
;
690 const char *separator
= "";
691 if (next
->pos
.whitespace
)
693 if (next
->pos
.newline
) {
694 separator
= "\n\t\t\t\t\t";
695 prec
= next
->pos
.pos
;
699 printf("%s%.*s", show_token(token
), prec
, separator
);
707 // Parse the resulting C code
708 return translation_unit(token
);