Clean up type expression syntax.
[smatch.git] / lib.c
blob1254575b115c2d991c4d1b92db98c5b000dc8ead
1 /*
2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 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>
17 #include <sys/mman.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
21 #include "lib.h"
22 #include "token.h"
23 #include "parse.h"
24 #include "symbol.h"
25 #include "expression.h"
26 #include "scope.h"
27 #include "linearize.h"
29 struct token *skip_to(struct token *token, int op)
31 while (!match_op(token, op) && !eof_token(token))
32 token = token->next;
33 return token;
36 struct token *expect(struct token *token, int op, const char *where)
38 if (!match_op(token, op)) {
39 static struct token bad_token;
40 if (token != &bad_token) {
41 bad_token.next = token;
42 warn(token->pos, "Expected %s %s", show_special(op), where);
43 warn(token->pos, "got %s", show_token(token));
45 if (op == ';')
46 return skip_to(token, op);
47 return &bad_token;
49 return token->next;
52 unsigned int hexval(unsigned int c)
54 int retval = 256;
55 switch (c) {
56 case '0'...'9':
57 retval = c - '0';
58 break;
59 case 'a'...'f':
60 retval = c - 'a' + 10;
61 break;
62 case 'A'...'F':
63 retval = c - 'A' + 10;
64 break;
66 return retval;
70 * Simple allocator for data that doesn't get partially free'd.
71 * The tokenizer and parser allocate a _lot_ of small data structures
72 * (often just two-three bytes for things like small integers),
73 * and since they all depend on each other you can't free them
74 * individually _anyway_. So do something that is very space-
75 * efficient: allocate larger "blobs", and give out individual
76 * small bits and pieces of it with no maintenance overhead.
78 struct allocation_blob {
79 struct allocation_blob *next;
80 unsigned int left, offset;
81 unsigned char data[];
84 #define CHUNK 32768
85 #define blob_alloc(size) mmap(NULL, ((size)+4095) & ~4095, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
86 #define blob_free(addr,size) munmap((addr), ((size)+4095) & ~4095)
88 struct allocator_struct {
89 const char *name;
90 struct allocation_blob *blobs;
91 unsigned int alignment;
92 unsigned int chunking;
93 /* statistics */
94 unsigned int allocations, total_bytes, useful_bytes;
97 void drop_all_allocations(struct allocator_struct *desc)
99 struct allocation_blob *blob = desc->blobs;
101 desc->blobs = NULL;
102 desc->allocations = 0;
103 desc->total_bytes = 0;
104 desc->useful_bytes = 0;
105 while (blob) {
106 struct allocation_blob *next = blob->next;
107 blob_free(blob, desc->chunking);
108 blob = next;
112 void *allocate(struct allocator_struct *desc, unsigned int size)
114 unsigned long alignment = desc->alignment;
115 struct allocation_blob *blob = desc->blobs;
116 void *retval;
118 desc->allocations++;
119 desc->useful_bytes += size;
120 size = (size + alignment - 1) & ~(alignment-1);
121 if (!blob || blob->left < size) {
122 unsigned int offset, chunking = desc->chunking;
123 struct allocation_blob *newblob = blob_alloc(chunking);
124 if (!newblob)
125 die("out of memory");
126 desc->total_bytes += chunking;
127 newblob->next = blob;
128 blob = newblob;
129 desc->blobs = newblob;
130 offset = offsetof(struct allocation_blob, data);
131 if (alignment > offset)
132 offset = alignment;
133 blob->left = chunking - offset;
134 blob->offset = offset - offsetof(struct allocation_blob, data);
136 retval = blob->data + blob->offset;
137 blob->offset += size;
138 blob->left -= size;
139 return retval;
142 static void show_allocations(struct allocator_struct *x)
144 fprintf(stderr, "%s: %d allocations, %d bytes (%d total bytes, "
145 "%6.2f%% usage, %6.2f average size)\n",
146 x->name, x->allocations, x->useful_bytes, x->total_bytes,
147 100 * (double) x->useful_bytes / x->total_bytes,
148 (double) x->useful_bytes / x->allocations);
151 struct allocator_struct ident_allocator = { "identifiers", NULL, __alignof__(struct ident), CHUNK };
152 struct allocator_struct token_allocator = { "tokens", NULL, __alignof__(struct token), CHUNK };
153 struct allocator_struct symbol_allocator = { "symbols", NULL, __alignof__(struct symbol), CHUNK };
154 struct allocator_struct expression_allocator = { "expressions", NULL, __alignof__(struct expression), CHUNK };
155 struct allocator_struct statement_allocator = { "statements", NULL, __alignof__(struct statement), CHUNK };
156 struct allocator_struct string_allocator = { "strings", NULL, __alignof__(struct statement), CHUNK };
157 struct allocator_struct scope_allocator = { "scopes", NULL, __alignof__(struct scope), CHUNK };
158 struct allocator_struct bytes_allocator = { "bytes", NULL, 1, CHUNK };
159 struct allocator_struct basic_block_allocator = { "basic_block", NULL, __alignof__(struct basic_block), CHUNK };
160 struct allocator_struct entrypoint_allocator = { "entrypoint", NULL, __alignof__(struct entrypoint), CHUNK };
161 struct allocator_struct instruction_allocator = { "instruction", NULL, __alignof__(struct instruction), CHUNK };
163 #define __ALLOCATOR(type, size, x) \
164 type *__alloc_##x(int extra) \
166 return allocate(&x##_allocator, size+extra); \
168 void show_##x##_alloc(void) \
170 show_allocations(&x##_allocator); \
172 void clear_##x##_alloc(void) \
174 drop_all_allocations(&x##_allocator); \
176 #define ALLOCATOR(x) __ALLOCATOR(struct x, sizeof(struct x), x)
178 ALLOCATOR(ident); ALLOCATOR(token); ALLOCATOR(symbol);
179 ALLOCATOR(expression); ALLOCATOR(statement); ALLOCATOR(string);
180 ALLOCATOR(scope); __ALLOCATOR(void, 0, bytes);
181 ALLOCATOR(basic_block); ALLOCATOR(entrypoint);
182 ALLOCATOR(instruction);
184 int ptr_list_size(struct ptr_list *head)
186 int nr = 0;
188 if (head) {
189 struct ptr_list *list = head;
190 do {
191 nr += list->nr;
192 } while ((list = list->next) != head);
194 return nr;
197 void iterate(struct ptr_list *head, void (*callback)(void *, void *, int), void *data)
199 struct ptr_list *list = head;
200 int flag = ITERATE_FIRST;
202 if (!head)
203 return;
204 do {
205 int i;
206 for (i = 0; i < list->nr; i++) {
207 if (i == list->nr-1 && list->next == head)
208 flag |= ITERATE_LAST;
209 callback(list->list[i], data, flag);
210 flag = 0;
212 list = list->next;
213 } while (list != head);
216 void add_ptr_list(struct ptr_list **listp, void *ptr)
218 struct ptr_list *list = *listp;
219 struct ptr_list *last;
220 int nr;
222 if (!list || (nr = (last = list->prev)->nr) >= LIST_NODE_NR) {
223 struct ptr_list *newlist = malloc(sizeof(*newlist));
224 if (!newlist)
225 die("out of memory for symbol/statement lists");
226 memset(newlist, 0, sizeof(*newlist));
227 if (!list) {
228 newlist->next = newlist;
229 newlist->prev = newlist;
230 *listp = newlist;
231 } else {
232 newlist->prev = last;
233 newlist->next = list;
234 list->prev = newlist;
235 last->next = newlist;
237 last = newlist;
238 nr = 0;
240 last->list[nr] = ptr;
241 nr++;
242 last->nr = nr;
245 void concat_ptr_list(struct ptr_list *a, struct ptr_list **b)
247 void *entry;
248 FOR_EACH_PTR(a, entry) {
249 add_ptr_list(b, entry);
250 } END_FOR_EACH_PTR;
253 void free_ptr_list(struct ptr_list **listp)
255 struct ptr_list *tmp, *list = *listp;
257 if (!list)
258 return;
260 list->prev->next = NULL;
261 while (list) {
262 tmp = list;
263 list = list->next;
264 free(tmp);
267 *listp = NULL;
270 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
272 static char buffer[512];
273 const char *name;
275 vsprintf(buffer, fmt, args);
276 name = input_streams[pos.stream].name;
278 fprintf(stderr, "%s: %s:%d:%d: %s\n",
279 type, name, pos.line, pos.pos, buffer);
282 void warn(struct position pos, const char * fmt, ...)
284 static int warnings = 0;
285 va_list args;
287 if (warnings > 100) {
288 static int once = 0;
289 if (once)
290 return;
291 fmt = "too many warnings";
292 once = 1;
295 va_start(args, fmt);
296 do_warn("warning", pos, fmt, args);
297 va_end(args);
298 warnings++;
301 void error(struct position pos, const char * fmt, ...)
303 va_list args;
304 va_start(args, fmt);
305 do_warn("error", pos, fmt, args);
306 va_end(args);
307 exit(1);
310 void die(const char *fmt, ...)
312 va_list args;
313 static char buffer[512];
315 va_start(args, fmt);
316 vsnprintf(buffer, sizeof(buffer), fmt, args);
317 va_end(args);
319 fprintf(stderr, "%s\n", buffer);
320 exit(1);
323 unsigned int pre_buffer_size;
324 unsigned char pre_buffer[8192];
326 int preprocess_only;
327 char *include;
328 int include_fd = -1;
330 void add_pre_buffer(const char *fmt, ...)
332 va_list args;
333 unsigned int size;
335 va_start(args, fmt);
336 size = pre_buffer_size;
337 size += vsnprintf(pre_buffer + size,
338 sizeof(pre_buffer) - size,
339 fmt, args);
340 pre_buffer_size = size;
341 va_end(args);
344 char **handle_switch_D(char *arg, char **next)
346 const char *name = arg + 1;
347 const char *value = "";
348 for (;;) {
349 char c;
350 c = *++arg;
351 if (!c)
352 break;
353 if (isspace(c) || c == '=') {
354 *arg = '\0';
355 value = arg + 1;
356 break;
359 add_pre_buffer("#define %s %s\n", name, value);
360 return next;
363 char **handle_switch_E(char *arg, char **next)
365 preprocess_only = 1;
366 return next;
369 char **handle_switch_v(char *arg, char **next)
371 verbose = 1;
372 return next;
374 char **handle_switch_I(char *arg, char **next)
376 add_pre_buffer("#add_include \"%s/\"\n", arg + 1);
377 return next;
380 char **handle_switch_i(char *arg, char **next)
382 if (*next && !strcmp(arg, "include")) {
383 char *name = *++next;
384 int fd = open(name, O_RDONLY);
386 include_fd = fd;
387 include = name;
388 if (fd < 0)
389 perror(name);
391 return next;
394 char **handle_switch(char *arg, char **next)
396 char **rc = next;
398 switch (*arg) {
399 case 'D': rc = handle_switch_D(arg, next); break;
400 case 'E': rc = handle_switch_E(arg, next); break;
401 case 'v': rc = handle_switch_v(arg, next); break;
402 case 'I': rc = handle_switch_I(arg, next); break;
403 case 'i': rc = handle_switch_i(arg, next); break;
404 default:
406 * Ignore unknown command line options:
407 * they're probably gcc switches
409 break;
411 return rc;
414 void create_builtin_stream(void)
416 add_pre_buffer("#define __i386__ 1\n");
417 add_pre_buffer("#define __linux__ 1\n");
418 add_pre_buffer("#define __STDC__ 1\n");
419 add_pre_buffer("#define linux linux\n");
420 add_pre_buffer("#define cond_syscall(x)\n");
421 add_pre_buffer("#define __GNUC__ 2\n");
422 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
423 add_pre_buffer("#define __func__ \"function\"\n");
424 add_pre_buffer("#define __extension__\n");
425 add_pre_buffer("#define __pragma__\n");
426 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
427 add_pre_buffer("#define __builtin_va_arg(arg,type) ((type)0)\n");
428 add_pre_buffer("#define __builtin_va_end(arg)\n");