Duh. When a function returns VOID, we should _not_ add that
[smatch.git] / lib.h
blobe768963b476cf1204945f9dac878cc32a8e3448a
1 #ifndef LIB_H
2 #define LIB_H
4 #include <stdlib.h>
5 #include <stddef.h>
7 /*
8 * Basic helper routine descriptions for 'sparse'.
10 * Copyright (C) 2003 Transmeta Corp.
11 * 2003 Linus Torvalds
12 * 2004 Christopher Li
14 * Licensed under the Open Software License version 1.1
17 #include "compat.h"
19 extern int verbose, optimize, preprocessing;
20 extern int repeat_phase, merge_phi_sources;
22 #define container(ptr, type, member) \
23 (type *)((void *)(ptr) - offsetof(type, member))
25 extern unsigned int hexval(unsigned int c);
27 struct position {
28 unsigned int type:6,
29 stream:10,
30 newline:1,
31 whitespace:1,
32 pos:14;
33 unsigned int line:31,
34 noexpand:1;
37 struct ident;
38 struct token;
39 struct symbol;
40 struct statement;
41 struct expression;
42 struct basic_block;
43 struct entrypoint;
44 struct instruction;
45 struct multijmp;
46 struct pseudo;
48 /* Silly type-safety check ;) */
49 #define DECLARE_PTR_LIST(listname,type) struct listname { type *list[1]; }
50 #define CHECK_TYPE(head,ptr) (void)(&(ptr) == &(head)->list[0])
51 #define TYPEOF(head) __typeof__(&(head)->list[0])
52 #define VRFY_PTR_LIST(head) (void)(sizeof((head)->list[0]))
54 DECLARE_PTR_LIST(symbol_list, struct symbol);
55 DECLARE_PTR_LIST(statement_list, struct statement);
56 DECLARE_PTR_LIST(expression_list, struct expression);
57 DECLARE_PTR_LIST(basic_block_list, struct basic_block);
58 DECLARE_PTR_LIST(instruction_list, struct instruction);
59 DECLARE_PTR_LIST(multijmp_list, struct multijmp);
60 DECLARE_PTR_LIST(pseudo_list, struct pseudo);
62 typedef struct pseudo *pseudo_t;
64 struct token *skip_to(struct token *, int);
65 struct token *expect(struct token *, int, const char *);
66 #ifdef __GNUC__
67 #define FORMAT_ATTR(pos) __attribute__ ((__format__ (__printf__, pos, pos+1)))
68 #else
69 #define FORMAT_ATTR(pos)
70 #endif
71 extern void die(const char *, ...) FORMAT_ATTR(1);
72 extern void info(struct position, const char *, ...) FORMAT_ATTR(2);
73 extern void warning(struct position, const char *, ...) FORMAT_ATTR(2);
74 extern void error(struct position, const char *, ...) FORMAT_ATTR(2);
75 extern void error_die(struct position, const char *, ...) FORMAT_ATTR(2);
76 #undef FORMAT_ATTR
78 #define LIST_NODE_NR (29)
80 struct ptr_list {
81 int nr;
82 struct ptr_list *prev;
83 struct ptr_list *next;
84 void *list[LIST_NODE_NR];
87 #define ptr_list_empty(x) ((x) == NULL)
89 void * delete_ptr_list_last(struct ptr_list **head);
90 int delete_ptr_list_entry(struct ptr_list **, void *, int);
91 int replace_ptr_list_entry(struct ptr_list **, void *old, void *new, int);
92 extern void sort_list(struct ptr_list **, int (*)(const void *, const void *));
94 extern void **__add_ptr_list(struct ptr_list **, void *, unsigned long tag);
95 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
96 extern void __free_ptr_list(struct ptr_list **);
97 extern int ptr_list_size(struct ptr_list *);
98 extern char **handle_switch(char *arg, char **next);
99 extern void add_pre_buffer(const char *fmt, ...);
100 int linearize_ptr_list(struct ptr_list *, void **, int);
103 * Hey, who said that you can't do overloading in C?
105 * You just have to be creative, and use some gcc
106 * extensions..
108 #define add_ptr_list_tag(list,entry,tag) \
109 (TYPEOF(*(list))) (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), (entry), (tag)))
110 #define add_ptr_list(list,entry) \
111 add_ptr_list_tag(list,entry,0)
112 #define free_ptr_list(list) \
113 do { VRFY_PTR_LIST(*(list)); __free_ptr_list((struct ptr_list **)(list)); } while (0)
115 extern unsigned int pre_buffer_size;
116 extern unsigned char pre_buffer[8192];
117 extern int include_fd;
118 extern char *include;
119 extern int preprocess_only;
120 extern int Wptr_subtraction_blows;
121 extern int Wdefault_bitfield_sign;
122 extern int Wundefined_preprocessor;
123 extern int Wbitwise, Wtypesign, Wcontext;
125 extern void declare_builtin_functions(void);
126 extern void create_builtin_stream(void);
127 extern struct symbol_list *sparse(int argc, char **argv);
129 static inline int symbol_list_size(struct symbol_list* list)
131 return ptr_list_size((struct ptr_list *)(list));
134 static inline int statement_list_size(struct statement_list* list)
136 return ptr_list_size((struct ptr_list *)(list));
139 static inline int expression_list_size(struct expression_list* list)
141 return ptr_list_size((struct ptr_list *)(list));
144 static inline int instruction_list_size(struct instruction_list* list)
146 return ptr_list_size((struct ptr_list *)(list));
149 static inline int pseudo_list_size(struct pseudo_list* list)
151 return ptr_list_size((struct ptr_list *)(list));
154 static inline int bb_list_size(struct basic_block_list* list)
156 return ptr_list_size((struct ptr_list *)(list));
159 static inline void free_instruction_list(struct instruction_list **head)
161 free_ptr_list((struct ptr_list **)head);
164 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
166 return delete_ptr_list_last((struct ptr_list **)head);
169 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
171 return delete_ptr_list_last((struct ptr_list **)head);
174 #define PTR_ENTRY(h,i) (void *)(~3UL & (unsigned long)(h)->list[i])
176 static inline void *first_ptr_list(struct ptr_list *list)
178 if (!list)
179 return NULL;
180 return PTR_ENTRY(list, 0);
183 static inline void *last_ptr_list(struct ptr_list *list)
186 if (!list)
187 return NULL;
188 list = list->prev;
189 return PTR_ENTRY(list, list->nr-1);
192 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
194 return first_ptr_list((struct ptr_list *)head);
196 static inline struct instruction *last_instruction(struct instruction_list *head)
198 return last_ptr_list((struct ptr_list *)head);
201 static inline struct instruction *first_instruction(struct instruction_list *head)
203 return first_ptr_list((struct ptr_list *)head);
206 static inline pseudo_t first_pseudo(struct pseudo_list *head)
208 return first_ptr_list((struct ptr_list *)head);
211 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
213 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
216 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
218 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
221 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
223 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
226 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
228 add_ptr_list(list, sym);
231 static inline void add_statement(struct statement_list **list, struct statement *stmt)
233 add_ptr_list(list, stmt);
236 static inline void add_expression(struct expression_list **list, struct expression *expr)
238 add_ptr_list(list, expr);
241 #define DO_PREPARE(head, ptr, __head, __list, __nr) \
242 do { \
243 struct ptr_list *__head = (struct ptr_list *) (head); \
244 struct ptr_list *__list = __head; \
245 int __nr = 0; \
246 CHECK_TYPE(head,ptr); \
247 if (__head) ptr = PTR_ENTRY(__head, 0); \
248 else ptr = NULL
250 #define DO_NEXT(ptr, __head, __list, __nr) \
251 if (ptr) { \
252 if (++__nr < __list->nr) { \
253 ptr = PTR_ENTRY(__list,__nr); \
254 } else { \
255 __list = __list->next; \
256 ptr = NULL; \
257 if (__list != __head) { \
258 __nr = 0; \
259 ptr = PTR_ENTRY(__list,0); \
264 #define DO_RESET(ptr, __head, __list, __nr) \
265 do { \
266 __nr = 0; \
267 __list = __head; \
268 if (__head) ptr = PTR_ENTRY(__head, 0); \
269 } while (0)
271 #define DO_FINISH(ptr, __head, __list, __nr) \
272 (void)(__nr); /* Sanity-check nesting */ \
273 } while (0)
275 #define PREPARE_PTR_LIST(head, ptr) \
276 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
278 #define NEXT_PTR_LIST(ptr) \
279 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr)
281 #define RESET_PTR_LIST(ptr) \
282 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr)
284 #define FINISH_PTR_LIST(ptr) \
285 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
287 #define DO_FOR_EACH(head, ptr, __head, __list, __nr) do { \
288 struct ptr_list *__head = (struct ptr_list *) (head); \
289 struct ptr_list *__list = __head; \
290 CHECK_TYPE(head,ptr); \
291 if (__head) { \
292 do { int __nr; \
293 for (__nr = 0; __nr < __list->nr; __nr++) { \
294 do { \
295 ptr = PTR_ENTRY(__list,__nr); \
296 do {
298 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
299 } while (0); \
300 } while (0); \
302 } while ((__list = __list->next) != __head); \
304 } while (0)
306 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr) do { \
307 struct ptr_list *__head = (struct ptr_list *) (head); \
308 struct ptr_list *__list = __head; \
309 CHECK_TYPE(head,ptr); \
310 if (__head) { \
311 do { int __nr; \
312 __list = __list->prev; \
313 __nr = __list->nr; \
314 while (--__nr >= 0) { \
315 do { \
316 ptr = PTR_ENTRY(__list,__nr); \
317 do {
320 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
321 } while (0); \
322 } while (0); \
324 } while (__list != __head); \
326 } while (0)
328 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, __newlist, __newnr) do { \
329 struct ptr_list *__newhead = __head; \
330 struct ptr_list *__newlist = __list; \
331 int __newnr = __nr; \
332 new = ptr; \
333 goto __inside##new; \
334 if (1) { \
335 do { \
336 __newlist = __newlist->prev; \
337 __newnr = __newlist->nr; \
338 __inside##new: \
339 while (--__newnr >= 0) { \
340 do { \
341 new = PTR_ENTRY(__newlist,__newnr); \
342 do {
344 #define RECURSE_PTR_REVERSE(ptr, new) \
345 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
346 new, __head##new, __list##new, __nr##new)
348 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
349 ((__typeof__(&(ptr))) (__list->list + __nr))
351 #define FOR_EACH_PTR(head, ptr) \
352 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
354 #define END_FOR_EACH_PTR(ptr) \
355 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
357 #define FOR_EACH_PTR_REVERSE(head, ptr) \
358 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
360 #define END_FOR_EACH_PTR_REVERSE(ptr) \
361 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
363 #define THIS_ADDRESS(ptr) \
364 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
366 extern void split_ptr_list_head(struct ptr_list *);
368 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
369 split_ptr_list_head(__list); \
370 if (__nr >= __list->nr) { \
371 __nr -= __list->nr; \
372 __list = __list->next; \
373 }; \
374 } while (0)
376 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
377 void **__this, **__last; \
378 if (__list->nr == LIST_NODE_NR) \
379 DO_SPLIT(ptr, __head, __list, __nr); \
380 __this = __list->list + __nr; \
381 __last = __list->list + __list->nr - 1; \
382 while (__last >= __this) { \
383 __last[1] = __last[0]; \
384 __last--; \
386 *__this = (new); \
387 __list->nr++; \
388 } while (0)
390 #define INSERT_CURRENT(new, ptr) \
391 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
393 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
394 void **__this = __list->list + __nr; \
395 void **__last = __list->list + __list->nr - 1; \
396 while (__this < __last) { \
397 __this[0] = __this[1]; \
398 __this++; \
400 *__this = (void *)0xf0f0f0f0; \
401 __list->nr--; __nr--; \
402 } while (0)
404 #define DELETE_CURRENT_PTR(ptr) \
405 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
407 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
408 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
410 extern void pack_ptr_list(struct ptr_list **);
412 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
414 #define hashval(x) ((unsigned long)(x))
416 static inline void update_tag(void *p, unsigned long tag)
418 unsigned long *ptr = p;
419 *ptr = tag | (~3UL & *ptr);
422 static inline void *tag_ptr(void *ptr, unsigned long tag)
424 return (void *)(tag | (unsigned long)ptr);
427 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
428 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
430 #endif