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
;
149 } while (entry
!= head
);
153 void split_ptr_list_head(struct ptr_list
*head
)
155 int nr
= head
->nr
/ 2;
156 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
159 newlist
->next
= head
->next
;
160 newlist
->prev
= head
;
161 head
->next
= newlist
;
163 memcpy(newlist
->list
, head
->list
+ head
->nr
, nr
* sizeof(void *));
164 memset(head
->list
+ head
->nr
, 0xf0, nr
* sizeof(void *));
167 void **__add_ptr_list(struct ptr_list
**listp
, void *ptr
)
169 struct ptr_list
*list
= *listp
;
170 struct ptr_list
*last
= NULL
; /* gcc complains needlessly */
174 if (!list
|| (nr
= (last
= list
->prev
)->nr
) >= LIST_NODE_NR
) {
175 struct ptr_list
*newlist
= malloc(sizeof(*newlist
));
177 die("out of memory for symbol/statement lists");
178 memset(newlist
, 0, sizeof(*newlist
));
180 newlist
->next
= newlist
;
181 newlist
->prev
= newlist
;
184 newlist
->prev
= last
;
185 newlist
->next
= list
;
186 list
->prev
= newlist
;
187 last
->next
= newlist
;
192 ret
= last
->list
+ nr
;
199 void delete_ptr_list_entry(struct ptr_list
**list
, void *entry
, int count
)
203 FOR_EACH_PTR(*list
, ptr
) {
205 DELETE_CURRENT_PTR(ptr
);
209 } END_FOR_EACH_PTR(ptr
);
215 void replace_ptr_list_entry(struct ptr_list
**list
, void *old_ptr
, void *new_ptr
, int count
)
219 FOR_EACH_PTR(*list
, ptr
) {
221 REPLACE_CURRENT_PTR(ptr
, new_ptr
);
225 }END_FOR_EACH_PTR(ptr
);
229 void * delete_ptr_list_last(struct ptr_list
**head
)
232 struct ptr_list
*last
, *first
= *head
;
238 ptr
= last
->list
[--last
->nr
];
240 first
->prev
= last
->prev
;
241 last
->prev
->next
= first
;
249 void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
)
252 FOR_EACH_PTR(a
, entry
) {
253 add_ptr_list(b
, entry
);
254 } END_FOR_EACH_PTR(entry
);
257 void __free_ptr_list(struct ptr_list
**listp
)
259 struct ptr_list
*tmp
, *list
= *listp
;
264 list
->prev
->next
= NULL
;
274 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
276 static char buffer
[512];
279 vsprintf(buffer
, fmt
, args
);
280 name
= input_streams
[pos
.stream
].name
;
282 fprintf(stderr
, "%s:%d:%d: %s%s\n",
283 name
, pos
.line
, pos
.pos
, type
, buffer
);
286 void info(struct position pos
, const char * fmt
, ...)
290 do_warn("", pos
, fmt
, args
);
294 void warning(struct position pos
, const char * fmt
, ...)
296 static int warnings
= 0;
299 if (warnings
> 100) {
303 fmt
= "too many warnings";
308 do_warn("warning: ", pos
, fmt
, args
);
313 void error(struct position pos
, const char * fmt
, ...)
315 static int errors
= 0;
322 fmt
= "too many errors";
327 do_warn("error: ", pos
, fmt
, args
);
332 void error_die(struct position pos
, const char * fmt
, ...)
336 do_warn("error: ", pos
, fmt
, args
);
341 void die(const char *fmt
, ...)
344 static char buffer
[512];
347 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
350 fprintf(stderr
, "%s\n", buffer
);
354 unsigned int pre_buffer_size
;
355 unsigned char pre_buffer
[8192];
357 int Wdefault_bitfield_sign
= 0;
361 int Wundefined_preprocessor
= 0;
366 void add_pre_buffer(const char *fmt
, ...)
372 size
= pre_buffer_size
;
373 size
+= vsnprintf(pre_buffer
+ size
,
374 sizeof(pre_buffer
) - size
,
376 pre_buffer_size
= size
;
380 char **handle_switch_D(char *arg
, char **next
)
382 const char *name
= arg
+ 1;
383 const char *value
= "1";
389 if (isspace((unsigned char)c
) || c
== '=') {
395 add_pre_buffer("#define %s %s\n", name
, value
);
399 char **handle_switch_E(char *arg
, char **next
)
405 char **handle_switch_v(char *arg
, char **next
)
411 char **handle_switch_I(char *arg
, char **next
)
417 /* Explaining '-I-' with a google search:
419 * "Specifying -I after -I- searches for #include directories.
420 * If -I- is specified before, it searches for #include "file"
421 * and not #include ."
423 * Which didn't explain it at all to me. Maybe somebody else can
424 * explain it properly. We ignore it for now.
428 case '\0': /* Plain "-I" */
431 die("missing argument for -I option");
434 add_pre_buffer("#add_include \"%s/\"\n", path
);
439 char **handle_switch_i(char *arg
, char **next
)
441 if (*next
&& !strcmp(arg
, "include")) {
442 char *name
= *++next
;
443 int fd
= open(name
, O_RDONLY
);
453 char **handle_switch_M(char *arg
, char **next
)
455 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
457 die("missing argument for -%s option", arg
);
463 char **handle_switch_m(char *arg
, char **next
)
465 if (!strcmp(arg
, "m64")) {
467 max_int_alignment
= 8;
468 bits_in_pointer
= 64;
469 pointer_alignment
= 8;
474 char **handle_switch_o(char *arg
, char **next
)
476 if (!strcmp (arg
, "o") && *next
)
477 return next
+ 1; // "-o foo"
479 return next
; // "-ofoo" or (bogus) terminal "-o"
482 const struct warning
{
486 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
487 { "undef", &Wundefined_preprocessor
},
488 { "bitwise", &Wbitwise
},
489 { "typesign", &Wtypesign
},
490 { "context", &Wcontext
},
494 char **handle_switch_W(char *arg
, char **next
)
500 // Prefixes "no" and "no-" mean to turn warning off.
501 if (p
[0] == 'n' && p
[1] == 'o') {
508 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
509 if (!strcmp(p
,warnings
[i
].name
)) {
510 *warnings
[i
].flag
= !no
;
519 char **handle_switch_U(char *arg
, char **next
)
521 const char *name
= arg
+ 1;
522 add_pre_buffer ("#undef %s\n", name
);
526 char **handle_switch_O(char *arg
, char **next
)
529 if (arg
[1] >= '0' && arg
[1] <= '9')
530 level
= arg
[1] - '0';
535 char **handle_nostdinc(char *arg
, char **next
)
537 add_pre_buffer("#nostdinc\n");
543 char **(*fn
)(char *, char**);
546 char **handle_switch(char *arg
, char **next
)
549 static struct switches cmd
[] = {
550 { "nostdinc", handle_nostdinc
},
556 case 'D': rc
= handle_switch_D(arg
, next
); break;
557 case 'E': rc
= handle_switch_E(arg
, next
); break;
558 case 'I': rc
= handle_switch_I(arg
, next
); break;
559 case 'i': rc
= handle_switch_i(arg
, next
); break;
560 case 'M': rc
= handle_switch_M(arg
, next
); break;
561 case 'm': rc
= handle_switch_m(arg
, next
); break;
562 case 'o': rc
= handle_switch_o(arg
, next
); break;
563 case 'U': rc
= handle_switch_U(arg
, next
); break;
564 case 'v': rc
= handle_switch_v(arg
, next
); break;
565 case 'W': rc
= handle_switch_W(arg
, next
); break;
566 case 'O': rc
= handle_switch_O(arg
, next
); break;
573 if (!strcmp(s
->name
, arg
))
574 return s
->fn(arg
, next
);
579 * Ignore unknown command line options:
580 * they're probably gcc switches
585 void declare_builtin_functions(void)
587 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
588 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
589 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
590 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
591 add_pre_buffer("extern void __builtin_trap(void);\n");
592 add_pre_buffer("extern int __builtin_ffs(int);\n");
593 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
596 void create_builtin_stream(void)
598 add_pre_buffer("#define __GNUC__ 2\n");
599 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
600 add_pre_buffer("#define __extension__\n");
601 add_pre_buffer("#define __pragma__\n");
603 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
604 // solaris/sparc that is really "unsigned int" and for linux/x86_64
605 // it is "long unsigned int". In either case we can probably
606 // get away with this. We need the #ifndef as cgcc will define
607 // the right __SIZE_TYPE__.
608 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
609 add_pre_buffer("#weak_define __STDC__ 1\n");
611 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
612 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
613 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
614 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
615 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
616 add_pre_buffer("#define __builtin_va_end(arg)\n");
619 static void do_predefined(char *filename
)
621 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
622 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
623 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
626 struct symbol_list
*sparse(int argc
, char **argv
)
629 char *filename
= NULL
, **args
;
632 // Initialize symbol stream first, so that we can add defines etc
640 if (arg
[0] == '-' && arg
[1]) {
641 args
= handle_switch(arg
+1, args
);
648 die("no input files given");
650 // Initialize type system
653 create_builtin_stream();
654 add_pre_buffer("#define __CHECKER__ 1\n");
655 if (!preprocess_only
)
656 declare_builtin_functions();
658 do_predefined(filename
);
660 if (strcmp (filename
, "-") == 0) {
663 fd
= open(filename
, O_RDONLY
);
665 die("No such file: %s", filename
);
668 // Tokenize the input stream
669 token
= tokenize(filename
, fd
, NULL
, includepath
);
672 // Prepend any "include" file to the stream.
674 token
= tokenize(include
, include_fd
, token
, includepath
);
676 // Prepend the initial built-in stream
677 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
679 // Pre-process the stream
680 token
= preprocess(token
);
682 if (preprocess_only
) {
683 while (!eof_token(token
)) {
685 struct token
*next
= token
->next
;
686 const char *separator
= "";
687 if (next
->pos
.whitespace
)
689 if (next
->pos
.newline
) {
690 separator
= "\n\t\t\t\t\t";
691 prec
= next
->pos
.pos
;
695 printf("%s%.*s", show_token(token
), prec
, separator
);
703 // Parse the resulting C code
704 return translation_unit(token
);