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
, preprocessing
;
33 struct token
*skip_to(struct token
*token
, int op
)
35 while (!match_op(token
, op
) && !eof_token(token
))
40 struct token
*expect(struct token
*token
, int op
, const char *where
)
42 if (!match_op(token
, op
)) {
43 static struct token bad_token
;
44 if (token
!= &bad_token
) {
45 bad_token
.next
= token
;
46 warning(token
->pos
, "Expected %s %s", show_special(op
), where
);
47 warning(token
->pos
, "got %s", show_token(token
));
50 return skip_to(token
, op
);
56 unsigned int hexval(unsigned int c
)
64 retval
= c
- 'a' + 10;
67 retval
= c
- 'A' + 10;
73 int ptr_list_size(struct ptr_list
*head
)
78 struct ptr_list
*list
= head
;
81 } while ((list
= list
->next
) != head
);
87 * Linearize the entries of a list up to a total of 'max',
88 * and return the nr of entries linearized.
90 * The array to linearize into (second argument) should really
91 * be "void *x[]", but we want to let people fill in any kind
92 * of pointer array, so let's just call it "void *".
94 int linearize_ptr_list(struct ptr_list
*head
, void **arr
, int max
)
97 if (head
&& max
> 0) {
98 struct ptr_list
*list
= head
;
104 memcpy(arr
, list
->list
, i
*sizeof(void *));
110 } while ((list
= list
->next
) != head
);
116 * When we've walked the list and deleted entries,
117 * we may need to re-pack it so that we don't have
118 * any empty blocks left (empty blocks upset the
121 void pack_ptr_list(struct ptr_list
**listp
)
123 struct ptr_list
*head
= *listp
;
126 struct ptr_list
*entry
= head
;
128 struct ptr_list
*next
;
132 struct ptr_list
*prev
;
149 } while (entry
!= head
);
153 void split_ptr_list_head(struct ptr_list
*head
)
155 int old
= head
->nr
, nr
= old
/ 2;
156 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
157 struct ptr_list
*next
= head
->next
;
161 newlist
->next
= next
;
162 next
->prev
= newlist
;
163 newlist
->prev
= head
;
164 head
->next
= newlist
;
166 memcpy(newlist
->list
, head
->list
+ old
, nr
* sizeof(void *));
167 memset(head
->list
+ old
, 0xf0, nr
* sizeof(void *));
170 void **__add_ptr_list(struct ptr_list
**listp
, void *ptr
)
172 struct ptr_list
*list
= *listp
;
173 struct ptr_list
*last
= NULL
; /* gcc complains needlessly */
177 if (!list
|| (nr
= (last
= list
->prev
)->nr
) >= LIST_NODE_NR
) {
178 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
180 die("out of memory for symbol/statement lists");
181 memset(newlist
, 0, sizeof(*newlist
));
183 newlist
->next
= newlist
;
184 newlist
->prev
= newlist
;
187 newlist
->prev
= last
;
188 newlist
->next
= list
;
189 list
->prev
= newlist
;
190 last
->next
= newlist
;
195 ret
= last
->list
+ nr
;
202 void delete_ptr_list_entry(struct ptr_list
**list
, void *entry
, int count
)
206 FOR_EACH_PTR(*list
, ptr
) {
208 DELETE_CURRENT_PTR(ptr
);
212 } END_FOR_EACH_PTR(ptr
);
218 void replace_ptr_list_entry(struct ptr_list
**list
, void *old_ptr
, void *new_ptr
, int count
)
222 FOR_EACH_PTR(*list
, ptr
) {
224 REPLACE_CURRENT_PTR(ptr
, new_ptr
);
228 }END_FOR_EACH_PTR(ptr
);
232 void * delete_ptr_list_last(struct ptr_list
**head
)
235 struct ptr_list
*last
, *first
= *head
;
241 ptr
= last
->list
[--last
->nr
];
243 first
->prev
= last
->prev
;
244 last
->prev
->next
= first
;
252 void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
)
255 FOR_EACH_PTR(a
, entry
) {
256 add_ptr_list(b
, entry
);
257 } END_FOR_EACH_PTR(entry
);
260 void __free_ptr_list(struct ptr_list
**listp
)
262 struct ptr_list
*tmp
, *list
= *listp
;
267 list
->prev
->next
= NULL
;
277 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
279 static char buffer
[512];
282 vsprintf(buffer
, fmt
, args
);
283 name
= stream_name(pos
.stream
);
285 fprintf(stderr
, "%s:%d:%d: %s%s\n",
286 name
, pos
.line
, pos
.pos
, type
, buffer
);
289 void info(struct position pos
, const char * fmt
, ...)
293 do_warn("", pos
, fmt
, args
);
297 void warning(struct position pos
, const char * fmt
, ...)
299 static int warnings
= 0;
302 if (warnings
> 100) {
306 fmt
= "too many warnings";
311 do_warn("warning: ", pos
, fmt
, args
);
316 void error(struct position pos
, const char * fmt
, ...)
318 static int errors
= 0;
325 fmt
= "too many errors";
330 do_warn("error: ", pos
, fmt
, args
);
335 void error_die(struct position pos
, const char * fmt
, ...)
339 do_warn("error: ", pos
, fmt
, args
);
344 void die(const char *fmt
, ...)
347 static char buffer
[512];
350 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
353 fprintf(stderr
, "%s\n", buffer
);
357 unsigned int pre_buffer_size
;
358 unsigned char pre_buffer
[8192];
360 int Wdefault_bitfield_sign
= 0;
364 int Wundefined_preprocessor
= 0;
369 void add_pre_buffer(const char *fmt
, ...)
375 size
= pre_buffer_size
;
376 size
+= vsnprintf(pre_buffer
+ size
,
377 sizeof(pre_buffer
) - size
,
379 pre_buffer_size
= size
;
383 char **handle_switch_D(char *arg
, char **next
)
385 const char *name
= arg
+ 1;
386 const char *value
= "1";
392 if (isspace((unsigned char)c
) || c
== '=') {
398 add_pre_buffer("#define %s %s\n", name
, value
);
402 char **handle_switch_E(char *arg
, char **next
)
408 char **handle_switch_v(char *arg
, char **next
)
412 } while (*++arg
== 'v');
416 char **handle_switch_I(char *arg
, char **next
)
422 /* Explaining '-I-' with a google search:
424 * "Specifying -I after -I- searches for #include directories.
425 * If -I- is specified before, it searches for #include "file"
426 * and not #include ."
428 * Which didn't explain it at all to me. Maybe somebody else can
429 * explain it properly. We ignore it for now.
433 case '\0': /* Plain "-I" */
436 die("missing argument for -I option");
439 add_pre_buffer("#add_include \"%s/\"\n", path
);
444 char **handle_switch_i(char *arg
, char **next
)
446 if (*next
&& !strcmp(arg
, "include")) {
447 char *name
= *++next
;
448 int fd
= open(name
, O_RDONLY
);
458 char **handle_switch_M(char *arg
, char **next
)
460 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
462 die("missing argument for -%s option", arg
);
468 char **handle_switch_m(char *arg
, char **next
)
470 if (!strcmp(arg
, "m64")) {
472 max_int_alignment
= 8;
473 bits_in_pointer
= 64;
474 pointer_alignment
= 8;
479 char **handle_switch_o(char *arg
, char **next
)
481 if (!strcmp (arg
, "o") && *next
)
482 return next
+ 1; // "-o foo"
484 return next
; // "-ofoo" or (bogus) terminal "-o"
487 const struct warning
{
491 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
492 { "undef", &Wundefined_preprocessor
},
493 { "bitwise", &Wbitwise
},
494 { "typesign", &Wtypesign
},
495 { "context", &Wcontext
},
499 char **handle_switch_W(char *arg
, char **next
)
505 // Prefixes "no" and "no-" mean to turn warning off.
506 if (p
[0] == 'n' && p
[1] == 'o') {
513 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
514 if (!strcmp(p
,warnings
[i
].name
)) {
515 *warnings
[i
].flag
= !no
;
524 char **handle_switch_U(char *arg
, char **next
)
526 const char *name
= arg
+ 1;
527 add_pre_buffer ("#undef %s\n", name
);
531 char **handle_switch_O(char *arg
, char **next
)
534 if (arg
[1] >= '0' && arg
[1] <= '9')
535 level
= arg
[1] - '0';
540 char **handle_nostdinc(char *arg
, char **next
)
542 add_pre_buffer("#nostdinc\n");
548 char **(*fn
)(char *, char**);
551 char **handle_switch(char *arg
, char **next
)
554 static struct switches cmd
[] = {
555 { "nostdinc", handle_nostdinc
},
561 case 'D': rc
= handle_switch_D(arg
, next
); break;
562 case 'E': rc
= handle_switch_E(arg
, next
); break;
563 case 'I': rc
= handle_switch_I(arg
, next
); break;
564 case 'i': rc
= handle_switch_i(arg
, next
); break;
565 case 'M': rc
= handle_switch_M(arg
, next
); break;
566 case 'm': rc
= handle_switch_m(arg
, next
); break;
567 case 'o': rc
= handle_switch_o(arg
, next
); break;
568 case 'U': rc
= handle_switch_U(arg
, next
); break;
569 case 'v': rc
= handle_switch_v(arg
, next
); break;
570 case 'W': rc
= handle_switch_W(arg
, next
); break;
571 case 'O': rc
= handle_switch_O(arg
, next
); break;
578 if (!strcmp(s
->name
, arg
))
579 return s
->fn(arg
, next
);
584 * Ignore unknown command line options:
585 * they're probably gcc switches
590 void declare_builtin_functions(void)
592 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
593 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
594 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
595 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
596 add_pre_buffer("extern void __builtin_trap(void);\n");
597 add_pre_buffer("extern int __builtin_ffs(int);\n");
598 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
601 void create_builtin_stream(void)
603 add_pre_buffer("#define __GNUC__ 2\n");
604 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
605 add_pre_buffer("#define __extension__\n");
606 add_pre_buffer("#define __pragma__\n");
608 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
609 // solaris/sparc that is really "unsigned int" and for linux/x86_64
610 // it is "long unsigned int". In either case we can probably
611 // get away with this. We need the #ifndef as cgcc will define
612 // the right __SIZE_TYPE__.
613 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
614 add_pre_buffer("#weak_define __STDC__ 1\n");
616 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
617 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
618 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
619 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
620 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
621 add_pre_buffer("#define __builtin_va_end(arg)\n");
624 static void do_predefined(char *filename
)
626 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
627 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
628 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
631 struct symbol_list
*sparse(int argc
, char **argv
)
634 char *filename
= NULL
, **args
;
637 // Initialize symbol stream first, so that we can add defines etc
645 if (arg
[0] == '-' && arg
[1]) {
646 args
= handle_switch(arg
+1, args
);
653 die("no input files given");
655 // Initialize type system
658 create_builtin_stream();
659 add_pre_buffer("#define __CHECKER__ 1\n");
660 if (!preprocess_only
)
661 declare_builtin_functions();
663 do_predefined(filename
);
665 if (strcmp (filename
, "-") == 0) {
668 fd
= open(filename
, O_RDONLY
);
670 die("No such file: %s", filename
);
673 // Tokenize the input stream
674 token
= tokenize(filename
, fd
, NULL
, includepath
);
677 // Prepend any "include" file to the stream.
679 token
= tokenize(include
, include_fd
, token
, includepath
);
681 // Prepend the initial built-in stream
682 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
684 // Pre-process the stream
685 token
= preprocess(token
);
687 if (preprocess_only
) {
688 while (!eof_token(token
)) {
690 struct token
*next
= token
->next
;
691 const char *separator
= "";
692 if (next
->pos
.whitespace
)
694 if (next
->pos
.newline
) {
695 separator
= "\n\t\t\t\t\t";
696 prec
= next
->pos
.pos
;
700 printf("%s%.*s", show_token(token
), prec
, separator
);
708 // Parse the resulting C code
709 return translation_unit(token
);