Simplify constant unops
[smatch.git] / lib.c
blob410217a9bcd17f8244bb780244c9ce08c9fce0a4
1 /*
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
8 */
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
22 #include "lib.h"
23 #include "allocate.h"
24 #include "token.h"
25 #include "parse.h"
26 #include "symbol.h"
27 #include "expression.h"
28 #include "scope.h"
29 #include "linearize.h"
30 #include "target.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))
37 token = token->next;
38 return 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));
50 if (op == ';')
51 return skip_to(token, op);
52 return &bad_token;
54 return token->next;
57 unsigned int hexval(unsigned int c)
59 int retval = 256;
60 switch (c) {
61 case '0'...'9':
62 retval = c - '0';
63 break;
64 case 'a'...'f':
65 retval = c - 'a' + 10;
66 break;
67 case 'A'...'F':
68 retval = c - 'A' + 10;
69 break;
71 return retval;
74 int ptr_list_size(struct ptr_list *head)
76 int nr = 0;
78 if (head) {
79 struct ptr_list *list = head;
80 do {
81 nr += list->nr;
82 } while ((list = list->next) != head);
84 return nr;
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)
97 int nr = 0;
98 if (head && max > 0) {
99 struct ptr_list *list = head;
101 do {
102 int i = list->nr;
103 if (i > max)
104 i = max;
105 memcpy(arr, list->list, i*sizeof(void *));
106 arr += i;
107 nr += i;
108 max -= i;
109 if (!max)
110 break;
111 } while ((list = list->next) != head);
113 return nr;
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
120 * walking code
122 void pack_ptr_list(struct ptr_list **listp)
124 struct ptr_list *head = *listp;
126 if (head) {
127 struct ptr_list *entry = head;
128 do {
129 struct ptr_list *next;
130 restart:
131 next = entry->next;
132 if (!entry->nr) {
133 struct ptr_list *prev;
134 if (next == entry) {
135 *listp = NULL;
136 return;
138 prev = entry->prev;
139 prev->next = next;
140 next->prev = prev;
141 free(entry);
142 if (entry == head) {
143 *listp = next;
144 head = next;
145 entry = next;
146 goto restart;
149 entry = next;
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;
160 old -= nr;
161 head->nr = old;
162 newlist->next = next;
163 next->prev = newlist;
164 newlist->prev = head;
165 head->next = newlist;
166 newlist->nr = nr;
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 */
175 void **ret;
176 int nr;
178 if (!list || (nr = (last = list->prev)->nr) >= LIST_NODE_NR) {
179 struct ptr_list *newlist = malloc(sizeof(*newlist));
180 if (!newlist)
181 die("out of memory for symbol/statement lists");
182 memset(newlist, 0, sizeof(*newlist));
183 if (!list) {
184 newlist->next = newlist;
185 newlist->prev = newlist;
186 *listp = newlist;
187 } else {
188 newlist->prev = last;
189 newlist->next = list;
190 list->prev = newlist;
191 last->next = newlist;
193 last = newlist;
194 nr = 0;
196 ret = last->list + nr;
197 *ret = ptr;
198 nr++;
199 last->nr = nr;
200 return ret;
203 void delete_ptr_list_entry(struct ptr_list **list, void *entry, int count)
205 void *ptr;
207 FOR_EACH_PTR(*list, ptr) {
208 if (ptr == entry) {
209 DELETE_CURRENT_PTR(ptr);
210 if (!--count)
211 goto out;
213 } END_FOR_EACH_PTR(ptr);
214 assert(count <= 0);
215 out:
216 pack_ptr_list(list);
219 void replace_ptr_list_entry(struct ptr_list **list, void *old_ptr, void *new_ptr, int count)
221 void *ptr;
223 FOR_EACH_PTR(*list, ptr) {
224 if (ptr==old_ptr) {
225 REPLACE_CURRENT_PTR(ptr, new_ptr);
226 if (!--count)
227 return;
229 }END_FOR_EACH_PTR(ptr);
230 assert(count <= 0);
233 void * delete_ptr_list_last(struct ptr_list **head)
235 void *ptr = NULL;
236 struct ptr_list *last, *first = *head;
238 if (!first)
239 return NULL;
240 last = first->prev;
241 if (last->nr)
242 ptr = last->list[--last->nr];
243 if (last->nr <=0) {
244 first->prev = last->prev;
245 last->prev->next = first;
246 if (last == first)
247 *head = NULL;
248 free(last);
250 return ptr;
253 void concat_ptr_list(struct ptr_list *a, struct ptr_list **b)
255 void *entry;
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;
265 if (!list)
266 return;
268 list->prev->next = NULL;
269 while (list) {
270 tmp = list;
271 list = list->next;
272 free(tmp);
275 *listp = NULL;
278 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
280 static char buffer[512];
281 const char *name;
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, ...)
292 va_list args;
293 va_start(args, fmt);
294 do_warn("", pos, fmt, args);
295 va_end(args);
298 void warning(struct position pos, const char * fmt, ...)
300 static int warnings = 0;
301 va_list args;
303 if (warnings > 100) {
304 static int once = 0;
305 if (once)
306 return;
307 fmt = "too many warnings";
308 once = 1;
311 va_start(args, fmt);
312 do_warn("warning: ", pos, fmt, args);
313 va_end(args);
314 warnings++;
317 void error(struct position pos, const char * fmt, ...)
319 static int errors = 0;
320 va_list args;
322 if (errors > 100) {
323 static int once = 0;
324 if (once)
325 return;
326 fmt = "too many errors";
327 once = 1;
330 va_start(args, fmt);
331 do_warn("error: ", pos, fmt, args);
332 va_end(args);
333 errors++;
336 void error_die(struct position pos, const char * fmt, ...)
338 va_list args;
339 va_start(args, fmt);
340 do_warn("error: ", pos, fmt, args);
341 va_end(args);
342 exit(1);
345 void die(const char *fmt, ...)
347 va_list args;
348 static char buffer[512];
350 va_start(args, fmt);
351 vsnprintf(buffer, sizeof(buffer), fmt, args);
352 va_end(args);
354 fprintf(stderr, "%s\n", buffer);
355 exit(1);
358 unsigned int pre_buffer_size;
359 unsigned char pre_buffer[8192];
361 int Wdefault_bitfield_sign = 0;
362 int Wbitwise = 0;
363 int Wtypesign = 0;
364 int Wcontext = 0;
365 int Wundefined_preprocessor = 0;
366 int preprocess_only;
367 char *include;
368 int include_fd = -1;
370 void add_pre_buffer(const char *fmt, ...)
372 va_list args;
373 unsigned int size;
375 va_start(args, fmt);
376 size = pre_buffer_size;
377 size += vsnprintf(pre_buffer + size,
378 sizeof(pre_buffer) - size,
379 fmt, args);
380 pre_buffer_size = size;
381 va_end(args);
384 char **handle_switch_D(char *arg, char **next)
386 const char *name = arg + 1;
387 const char *value = "1";
388 for (;;) {
389 char c;
390 c = *++arg;
391 if (!c)
392 break;
393 if (isspace((unsigned char)c) || c == '=') {
394 *arg = '\0';
395 value = arg + 1;
396 break;
399 add_pre_buffer("#define %s %s\n", name, value);
400 return next;
403 char **handle_switch_E(char *arg, char **next)
405 preprocess_only = 1;
406 return next;
409 char **handle_switch_v(char *arg, char **next)
411 verbose++;
412 return next;
415 char **handle_switch_I(char *arg, char **next)
417 char *path = arg+1;
419 switch (arg[1]) {
420 case '-':
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.
430 return next;
432 case '\0': /* Plain "-I" */
433 path = *++next;
434 if (!path)
435 die("missing argument for -I option");
436 /* Fallthrough */
437 default:
438 add_pre_buffer("#add_include \"%s/\"\n", path);
440 return next;
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);
449 include_fd = fd;
450 include = name;
451 if (fd < 0)
452 perror(name);
454 return next;
457 char **handle_switch_M(char *arg, char **next)
459 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
460 if (!*next)
461 die("missing argument for -%s option", arg);
462 return next + 1;
464 return next;
467 char **handle_switch_m(char *arg, char **next)
469 if (!strcmp(arg, "m64")) {
470 bits_in_long = 64;
471 max_int_alignment = 8;
472 bits_in_pointer = 64;
473 pointer_alignment = 8;
475 return next;
478 char **handle_switch_o(char *arg, char **next)
480 if (!strcmp (arg, "o") && *next)
481 return next + 1; // "-o foo"
482 else
483 return next; // "-ofoo" or (bogus) terminal "-o"
486 const struct warning {
487 const char *name;
488 int *flag;
489 } warnings[] = {
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)
500 int no = 0;
501 char *p = arg + 1;
502 unsigned i;
504 // Prefixes "no" and "no-" mean to turn warning off.
505 if (p[0] == 'n' && p[1] == 'o') {
506 p += 2;
507 if (p[0] == '-')
508 p++;
509 no = 1;
512 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
513 if (!strcmp(p,warnings[i].name)) {
514 *warnings[i].flag = !no;
515 return next;
519 // Unknown.
520 return next;
523 char **handle_switch_U(char *arg, char **next)
525 const char *name = arg + 1;
526 add_pre_buffer ("#undef %s\n", name);
527 return next;
530 char **handle_switch_O(char *arg, char **next)
532 int level = 1;
533 if (arg[1] >= '0' && arg[1] <= '9')
534 level = arg[1] - '0';
535 optimize = level;
536 return next;
539 char **handle_nostdinc(char *arg, char **next)
541 add_pre_buffer("#nostdinc\n");
542 return next;
545 struct switches {
546 const char *name;
547 char **(*fn)(char *, char**);
550 char **handle_switch(char *arg, char **next)
552 char **rc = next;
553 static struct switches cmd[] = {
554 { "nostdinc", handle_nostdinc },
555 { NULL, NULL }
557 struct switches *s;
559 switch (*arg) {
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;
571 default:
572 break;
575 s = cmd;
576 while (s->name) {
577 if (!strcmp(s->name, arg))
578 return s->fn(arg, next);
579 s++;
583 * Ignore unknown command line options:
584 * they're probably gcc switches
586 return rc;
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)
632 int fd;
633 char *filename = NULL, **args;
634 struct token *token;
636 // Initialize symbol stream first, so that we can add defines etc
637 init_symbols();
639 args = argv;
640 for (;;) {
641 char *arg = *++args;
642 if (!arg)
643 break;
644 if (arg[0] == '-' && arg[1]) {
645 args = handle_switch(arg+1, args);
646 continue;
648 filename = arg;
651 if (!filename)
652 die("no input files given");
654 // Initialize type system
655 init_ctype();
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) {
665 fd = 0;
666 } else {
667 fd = open(filename, O_RDONLY);
668 if (fd < 0)
669 die("No such file: %s", filename);
672 // Tokenize the input stream
673 token = tokenize(filename, fd, NULL, includepath);
674 close(fd);
676 // Prepend any "include" file to the stream.
677 if (include_fd >= 0)
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)) {
688 int prec = 1;
689 struct token *next = token->next;
690 const char *separator = "";
691 if (next->pos.whitespace)
692 separator = " ";
693 if (next->pos.newline) {
694 separator = "\n\t\t\t\t\t";
695 prec = next->pos.pos;
696 if (prec > 4)
697 prec = 4;
699 printf("%s%.*s", show_token(token), prec, separator);
700 token = next;
702 putchar('\n');
704 return NULL;
707 // Parse the resulting C code
708 return translation_unit(token);