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
, unsigned long tag
)
172 struct ptr_list
*list
= *listp
;
173 struct ptr_list
*last
= NULL
; /* gcc complains needlessly */
177 /* The low two bits are reserved for tags */
178 assert((3 & (unsigned long)ptr
) == 0);
179 assert((~3 & tag
) == 0);
180 ptr
= (void *)(tag
| (unsigned long)ptr
);
182 if (!list
|| (nr
= (last
= list
->prev
)->nr
) >= LIST_NODE_NR
) {
183 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
185 die("out of memory for symbol/statement lists");
186 memset(newlist
, 0, sizeof(*newlist
));
188 newlist
->next
= newlist
;
189 newlist
->prev
= newlist
;
192 newlist
->prev
= last
;
193 newlist
->next
= list
;
194 list
->prev
= newlist
;
195 last
->next
= newlist
;
200 ret
= last
->list
+ nr
;
207 int delete_ptr_list_entry(struct ptr_list
**list
, void *entry
, int count
)
211 FOR_EACH_PTR(*list
, ptr
) {
213 DELETE_CURRENT_PTR(ptr
);
217 } END_FOR_EACH_PTR(ptr
);
224 int replace_ptr_list_entry(struct ptr_list
**list
, void *old_ptr
, void *new_ptr
, int count
)
228 FOR_EACH_PTR(*list
, ptr
) {
230 REPLACE_CURRENT_PTR(ptr
, new_ptr
);
234 }END_FOR_EACH_PTR(ptr
);
240 void * delete_ptr_list_last(struct ptr_list
**head
)
243 struct ptr_list
*last
, *first
= *head
;
249 ptr
= last
->list
[--last
->nr
];
251 first
->prev
= last
->prev
;
252 last
->prev
->next
= first
;
260 void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
)
263 FOR_EACH_PTR(a
, entry
) {
264 add_ptr_list(b
, entry
);
265 } END_FOR_EACH_PTR(entry
);
268 void __free_ptr_list(struct ptr_list
**listp
)
270 struct ptr_list
*tmp
, *list
= *listp
;
275 list
->prev
->next
= NULL
;
285 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
287 static char buffer
[512];
290 vsprintf(buffer
, fmt
, args
);
291 name
= stream_name(pos
.stream
);
293 fprintf(stderr
, "%s:%d:%d: %s%s\n",
294 name
, pos
.line
, pos
.pos
, type
, buffer
);
297 void info(struct position pos
, const char * fmt
, ...)
301 do_warn("", pos
, fmt
, args
);
305 void warning(struct position pos
, const char * fmt
, ...)
307 static int warnings
= 0;
310 if (warnings
> 100) {
314 fmt
= "too many warnings";
319 do_warn("warning: ", pos
, fmt
, args
);
324 void error(struct position pos
, const char * fmt
, ...)
326 static int errors
= 0;
333 fmt
= "too many errors";
338 do_warn("error: ", pos
, fmt
, args
);
343 void error_die(struct position pos
, const char * fmt
, ...)
347 do_warn("error: ", pos
, fmt
, args
);
352 void die(const char *fmt
, ...)
355 static char buffer
[512];
358 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
361 fprintf(stderr
, "%s\n", buffer
);
365 unsigned int pre_buffer_size
;
366 unsigned char pre_buffer
[8192];
368 int Wdefault_bitfield_sign
= 0;
372 int Wundefined_preprocessor
= 0;
377 void add_pre_buffer(const char *fmt
, ...)
383 size
= pre_buffer_size
;
384 size
+= vsnprintf(pre_buffer
+ size
,
385 sizeof(pre_buffer
) - size
,
387 pre_buffer_size
= size
;
391 char **handle_switch_D(char *arg
, char **next
)
393 const char *name
= arg
+ 1;
394 const char *value
= "1";
400 if (isspace((unsigned char)c
) || c
== '=') {
406 add_pre_buffer("#define %s %s\n", name
, value
);
410 char **handle_switch_E(char *arg
, char **next
)
416 char **handle_switch_v(char *arg
, char **next
)
420 } while (*++arg
== 'v');
424 char **handle_switch_I(char *arg
, char **next
)
430 /* Explaining '-I-' with a google search:
432 * "Specifying -I after -I- searches for #include directories.
433 * If -I- is specified before, it searches for #include "file"
434 * and not #include ."
436 * Which didn't explain it at all to me. Maybe somebody else can
437 * explain it properly. We ignore it for now.
441 case '\0': /* Plain "-I" */
444 die("missing argument for -I option");
447 add_pre_buffer("#add_include \"%s/\"\n", path
);
452 char **handle_switch_i(char *arg
, char **next
)
454 if (*next
&& !strcmp(arg
, "include")) {
455 char *name
= *++next
;
456 int fd
= open(name
, O_RDONLY
);
466 char **handle_switch_M(char *arg
, char **next
)
468 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
470 die("missing argument for -%s option", arg
);
476 char **handle_switch_m(char *arg
, char **next
)
478 if (!strcmp(arg
, "m64")) {
480 max_int_alignment
= 8;
481 bits_in_pointer
= 64;
482 pointer_alignment
= 8;
487 char **handle_switch_o(char *arg
, char **next
)
489 if (!strcmp (arg
, "o") && *next
)
490 return next
+ 1; // "-o foo"
492 return next
; // "-ofoo" or (bogus) terminal "-o"
495 const struct warning
{
499 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
500 { "undef", &Wundefined_preprocessor
},
501 { "bitwise", &Wbitwise
},
502 { "typesign", &Wtypesign
},
503 { "context", &Wcontext
},
507 char **handle_switch_W(char *arg
, char **next
)
513 // Prefixes "no" and "no-" mean to turn warning off.
514 if (p
[0] == 'n' && p
[1] == 'o') {
521 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
522 if (!strcmp(p
,warnings
[i
].name
)) {
523 *warnings
[i
].flag
= !no
;
532 char **handle_switch_U(char *arg
, char **next
)
534 const char *name
= arg
+ 1;
535 add_pre_buffer ("#undef %s\n", name
);
539 char **handle_switch_O(char *arg
, char **next
)
542 if (arg
[1] >= '0' && arg
[1] <= '9')
543 level
= arg
[1] - '0';
548 char **handle_nostdinc(char *arg
, char **next
)
550 add_pre_buffer("#nostdinc\n");
556 char **(*fn
)(char *, char**);
559 char **handle_switch(char *arg
, char **next
)
562 static struct switches cmd
[] = {
563 { "nostdinc", handle_nostdinc
},
569 case 'D': rc
= handle_switch_D(arg
, next
); break;
570 case 'E': rc
= handle_switch_E(arg
, next
); break;
571 case 'I': rc
= handle_switch_I(arg
, next
); break;
572 case 'i': rc
= handle_switch_i(arg
, next
); break;
573 case 'M': rc
= handle_switch_M(arg
, next
); break;
574 case 'm': rc
= handle_switch_m(arg
, next
); break;
575 case 'o': rc
= handle_switch_o(arg
, next
); break;
576 case 'U': rc
= handle_switch_U(arg
, next
); break;
577 case 'v': rc
= handle_switch_v(arg
, next
); break;
578 case 'W': rc
= handle_switch_W(arg
, next
); break;
579 case 'O': rc
= handle_switch_O(arg
, next
); break;
586 if (!strcmp(s
->name
, arg
))
587 return s
->fn(arg
, next
);
592 * Ignore unknown command line options:
593 * they're probably gcc switches
598 void declare_builtin_functions(void)
600 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
601 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
602 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
603 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
604 add_pre_buffer("extern void __builtin_trap(void);\n");
605 add_pre_buffer("extern int __builtin_ffs(int);\n");
606 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
609 void create_builtin_stream(void)
611 add_pre_buffer("#define __GNUC__ 2\n");
612 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
613 add_pre_buffer("#define __extension__\n");
614 add_pre_buffer("#define __pragma__\n");
616 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
617 // solaris/sparc that is really "unsigned int" and for linux/x86_64
618 // it is "long unsigned int". In either case we can probably
619 // get away with this. We need the #ifndef as cgcc will define
620 // the right __SIZE_TYPE__.
621 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
622 add_pre_buffer("#weak_define __STDC__ 1\n");
624 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
625 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
626 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
627 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
628 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
629 add_pre_buffer("#define __builtin_va_end(arg)\n");
632 static void do_predefined(char *filename
)
634 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
635 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
636 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
639 struct symbol_list
*sparse(int argc
, char **argv
)
642 char *filename
= NULL
, **args
;
645 // Initialize symbol stream first, so that we can add defines etc
653 if (arg
[0] == '-' && arg
[1]) {
654 args
= handle_switch(arg
+1, args
);
661 die("no input files given");
663 // Initialize type system
666 create_builtin_stream();
667 add_pre_buffer("#define __CHECKER__ 1\n");
668 if (!preprocess_only
)
669 declare_builtin_functions();
671 do_predefined(filename
);
673 if (strcmp (filename
, "-") == 0) {
676 fd
= open(filename
, O_RDONLY
);
678 die("No such file: %s", filename
);
681 // Tokenize the input stream
682 token
= tokenize(filename
, fd
, NULL
, includepath
);
685 // Prepend any "include" file to the stream.
687 token
= tokenize(include
, include_fd
, token
, includepath
);
689 // Prepend the initial built-in stream
690 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
692 // Pre-process the stream
693 token
= preprocess(token
);
695 if (preprocess_only
) {
696 while (!eof_token(token
)) {
698 struct token
*next
= token
->next
;
699 const char *separator
= "";
700 if (next
->pos
.whitespace
)
702 if (next
->pos
.newline
) {
703 separator
= "\n\t\t\t\t\t";
704 prec
= next
->pos
.pos
;
708 printf("%s%.*s", show_token(token
), prec
, separator
);
716 // Parse the resulting C code
717 return translation_unit(token
);