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
)
413 } while (*++arg
== 'v');
417 char **handle_switch_I(char *arg
, char **next
)
423 /* Explaining '-I-' with a google search:
425 * "Specifying -I after -I- searches for #include directories.
426 * If -I- is specified before, it searches for #include "file"
427 * and not #include ."
429 * Which didn't explain it at all to me. Maybe somebody else can
430 * explain it properly. We ignore it for now.
434 case '\0': /* Plain "-I" */
437 die("missing argument for -I option");
440 add_pre_buffer("#add_include \"%s/\"\n", path
);
445 char **handle_switch_i(char *arg
, char **next
)
447 if (*next
&& !strcmp(arg
, "include")) {
448 char *name
= *++next
;
449 int fd
= open(name
, O_RDONLY
);
459 char **handle_switch_M(char *arg
, char **next
)
461 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
463 die("missing argument for -%s option", arg
);
469 char **handle_switch_m(char *arg
, char **next
)
471 if (!strcmp(arg
, "m64")) {
473 max_int_alignment
= 8;
474 bits_in_pointer
= 64;
475 pointer_alignment
= 8;
480 char **handle_switch_o(char *arg
, char **next
)
482 if (!strcmp (arg
, "o") && *next
)
483 return next
+ 1; // "-o foo"
485 return next
; // "-ofoo" or (bogus) terminal "-o"
488 const struct warning
{
492 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
493 { "undef", &Wundefined_preprocessor
},
494 { "bitwise", &Wbitwise
},
495 { "typesign", &Wtypesign
},
496 { "context", &Wcontext
},
500 char **handle_switch_W(char *arg
, char **next
)
506 // Prefixes "no" and "no-" mean to turn warning off.
507 if (p
[0] == 'n' && p
[1] == 'o') {
514 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
515 if (!strcmp(p
,warnings
[i
].name
)) {
516 *warnings
[i
].flag
= !no
;
525 char **handle_switch_U(char *arg
, char **next
)
527 const char *name
= arg
+ 1;
528 add_pre_buffer ("#undef %s\n", name
);
532 char **handle_switch_O(char *arg
, char **next
)
535 if (arg
[1] >= '0' && arg
[1] <= '9')
536 level
= arg
[1] - '0';
541 char **handle_nostdinc(char *arg
, char **next
)
543 add_pre_buffer("#nostdinc\n");
549 char **(*fn
)(char *, char**);
552 char **handle_switch(char *arg
, char **next
)
555 static struct switches cmd
[] = {
556 { "nostdinc", handle_nostdinc
},
562 case 'D': rc
= handle_switch_D(arg
, next
); break;
563 case 'E': rc
= handle_switch_E(arg
, next
); break;
564 case 'I': rc
= handle_switch_I(arg
, next
); break;
565 case 'i': rc
= handle_switch_i(arg
, next
); break;
566 case 'M': rc
= handle_switch_M(arg
, next
); break;
567 case 'm': rc
= handle_switch_m(arg
, next
); break;
568 case 'o': rc
= handle_switch_o(arg
, next
); break;
569 case 'U': rc
= handle_switch_U(arg
, next
); break;
570 case 'v': rc
= handle_switch_v(arg
, next
); break;
571 case 'W': rc
= handle_switch_W(arg
, next
); break;
572 case 'O': rc
= handle_switch_O(arg
, next
); break;
579 if (!strcmp(s
->name
, arg
))
580 return s
->fn(arg
, next
);
585 * Ignore unknown command line options:
586 * they're probably gcc switches
591 void declare_builtin_functions(void)
593 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
594 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
595 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
596 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
597 add_pre_buffer("extern void __builtin_trap(void);\n");
598 add_pre_buffer("extern int __builtin_ffs(int);\n");
599 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
602 void create_builtin_stream(void)
604 add_pre_buffer("#define __GNUC__ 2\n");
605 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
606 add_pre_buffer("#define __extension__\n");
607 add_pre_buffer("#define __pragma__\n");
609 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
610 // solaris/sparc that is really "unsigned int" and for linux/x86_64
611 // it is "long unsigned int". In either case we can probably
612 // get away with this. We need the #ifndef as cgcc will define
613 // the right __SIZE_TYPE__.
614 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
615 add_pre_buffer("#weak_define __STDC__ 1\n");
617 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
618 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
619 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
620 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
621 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
622 add_pre_buffer("#define __builtin_va_end(arg)\n");
625 static void do_predefined(char *filename
)
627 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
628 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
629 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
632 struct symbol_list
*sparse(int argc
, char **argv
)
635 char *filename
= NULL
, **args
;
638 // Initialize symbol stream first, so that we can add defines etc
646 if (arg
[0] == '-' && arg
[1]) {
647 args
= handle_switch(arg
+1, args
);
654 die("no input files given");
656 // Initialize type system
659 create_builtin_stream();
660 add_pre_buffer("#define __CHECKER__ 1\n");
661 if (!preprocess_only
)
662 declare_builtin_functions();
664 do_predefined(filename
);
666 if (strcmp (filename
, "-") == 0) {
669 fd
= open(filename
, O_RDONLY
);
671 die("No such file: %s", filename
);
674 // Tokenize the input stream
675 token
= tokenize(filename
, fd
, NULL
, includepath
);
678 // Prepend any "include" file to the stream.
680 token
= tokenize(include
, include_fd
, token
, includepath
);
682 // Prepend the initial built-in stream
683 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
685 // Pre-process the stream
686 token
= preprocess(token
);
688 if (preprocess_only
) {
689 while (!eof_token(token
)) {
691 struct token
*next
= token
->next
;
692 const char *separator
= "";
693 if (next
->pos
.whitespace
)
695 if (next
->pos
.newline
) {
696 separator
= "\n\t\t\t\t\t";
697 prec
= next
->pos
.pos
;
701 printf("%s%.*s", show_token(token
), prec
, separator
);
709 // Parse the resulting C code
710 return translation_unit(token
);