Add INSERT_CURRENT() macro to insert a new entry at the
[smatch.git] / lib.h
blob54939805e653c13dbe1adbce2163696de610ec24
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 void delete_ptr_list_entry(struct ptr_list **, void *, int);
91 void 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 *);
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(list,entry) \
109 (TYPEOF(*(list))) (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), (entry)))
110 #define free_ptr_list(list) \
111 do { VRFY_PTR_LIST(*(list)); __free_ptr_list((struct ptr_list **)(list)); } while (0)
113 extern unsigned int pre_buffer_size;
114 extern unsigned char pre_buffer[8192];
115 extern int include_fd;
116 extern char *include;
117 extern int preprocess_only;
118 extern int Wdefault_bitfield_sign;
119 extern int Wundefined_preprocessor;
120 extern int Wbitwise, Wtypesign, Wcontext;
122 extern void declare_builtin_functions(void);
123 extern void create_builtin_stream(void);
124 extern struct symbol_list *sparse(int argc, char **argv);
126 static inline int symbol_list_size(struct symbol_list* list)
128 return ptr_list_size((struct ptr_list *)(list));
131 static inline int statement_list_size(struct statement_list* list)
133 return ptr_list_size((struct ptr_list *)(list));
136 static inline int expression_list_size(struct expression_list* list)
138 return ptr_list_size((struct ptr_list *)(list));
141 static inline int instruction_list_size(struct instruction_list* list)
143 return ptr_list_size((struct ptr_list *)(list));
146 static inline int pseudo_list_size(struct pseudo_list* list)
148 return ptr_list_size((struct ptr_list *)(list));
151 static inline int bb_list_size(struct basic_block_list* list)
153 return ptr_list_size((struct ptr_list *)(list));
156 static inline void free_instruction_list(struct instruction_list **head)
158 free_ptr_list((struct ptr_list **)head);
161 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
163 return delete_ptr_list_last((struct ptr_list **)head);
166 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
168 return delete_ptr_list_last((struct ptr_list **)head);
171 static inline void *first_ptr_list(struct ptr_list *list)
173 if (!list)
174 return NULL;
175 return list->list[0];
178 static inline void *last_ptr_list(struct ptr_list *list)
181 if (!list)
182 return NULL;
183 list = list->prev;
184 return list->list[list->nr-1];
187 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
189 return first_ptr_list((struct ptr_list *)head);
191 static inline struct instruction *last_instruction(struct instruction_list *head)
193 return last_ptr_list((struct ptr_list *)head);
196 static inline struct instruction *first_instruction(struct instruction_list *head)
198 return first_ptr_list((struct ptr_list *)head);
201 static inline pseudo_t first_pseudo(struct pseudo_list *head)
203 return first_ptr_list((struct ptr_list *)head);
206 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
208 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
211 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
213 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
216 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
218 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
221 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
223 add_ptr_list(list, sym);
226 static inline void add_statement(struct statement_list **list, struct statement *stmt)
228 add_ptr_list(list, stmt);
231 static inline void add_expression(struct expression_list **list, struct expression *expr)
233 add_ptr_list(list, expr);
236 #define DO_PREPARE(head, ptr, __head, __list, __nr) \
237 do { \
238 struct ptr_list *__head = (struct ptr_list *) (head); \
239 struct ptr_list *__list = __head; \
240 int __nr = 0; \
241 CHECK_TYPE(head,ptr); \
242 if (__head) ptr = (__typeof__(ptr)) __head->list[0]; \
243 else ptr = NULL
245 #define DO_NEXT(ptr, __head, __list, __nr) \
246 if (ptr) { \
247 if (++__nr < __list->nr) { \
248 ptr = (__typeof__(ptr)) __list->list[__nr]; \
249 } else { \
250 __list = __list->next; \
251 ptr = NULL; \
252 if (__list != __head) { \
253 __nr = 0; \
254 ptr = (__typeof__(ptr)) __list->list[0]; \
259 #define DO_RESET(ptr, __head, __list, __nr) \
260 do { \
261 __nr = 0; \
262 __list = __head; \
263 if (__head) ptr = (__typeof__(ptr)) __head->list[0]; \
264 } while (0)
266 #define DO_FINISH(ptr, __head, __list, __nr) \
267 (void)(__nr); /* Sanity-check nesting */ \
268 } while (0)
270 #define PREPARE_PTR_LIST(head, ptr) \
271 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
273 #define NEXT_PTR_LIST(ptr) \
274 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr)
276 #define RESET_PTR_LIST(ptr) \
277 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr)
279 #define FINISH_PTR_LIST(ptr) \
280 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
282 #define DO_FOR_EACH(head, ptr, __head, __list, __nr) do { \
283 struct ptr_list *__head = (struct ptr_list *) (head); \
284 struct ptr_list *__list = __head; \
285 CHECK_TYPE(head,ptr); \
286 if (__head) { \
287 do { int __nr; \
288 for (__nr = 0; __nr < __list->nr; __nr++) { \
289 do { \
290 ptr = (__typeof__(ptr)) (__list->list[__nr]); \
291 do {
293 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
294 } while (0); \
295 } while (0); \
297 } while ((__list = __list->next) != __head); \
299 } while (0)
301 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr) do { \
302 struct ptr_list *__head = (struct ptr_list *) (head); \
303 struct ptr_list *__list = __head; \
304 CHECK_TYPE(head,ptr); \
305 if (__head) { \
306 do { int __nr; \
307 __list = __list->prev; \
308 __nr = __list->nr; \
309 while (--__nr >= 0) { \
310 do { \
311 ptr = (__typeof__(ptr)) (__list->list[__nr]); \
312 do {
315 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
316 } while (0); \
317 } while (0); \
319 } while (__list != __head); \
321 } while (0)
323 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, __newlist, __newnr) do { \
324 struct ptr_list *__newhead = __head; \
325 struct ptr_list *__newlist = __list; \
326 int __newnr = __nr; \
327 new = ptr; \
328 goto __inside##new; \
329 if (1) { \
330 do { \
331 __newlist = __newlist->prev; \
332 __newnr = __newlist->nr; \
333 __inside##new: \
334 while (--__newnr >= 0) { \
335 do { \
336 new = (__typeof__(ptr)) (__newlist->list[__newnr]);\
337 do {
339 #define RECURSE_PTR_REVERSE(ptr, new) \
340 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
341 new, __head##new, __list##new, __nr##new)
343 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
344 ((__typeof__(&(ptr))) (__list->list + __nr))
346 #define FOR_EACH_PTR(head, ptr) \
347 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
349 #define END_FOR_EACH_PTR(ptr) \
350 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
352 #define FOR_EACH_PTR_REVERSE(head, ptr) \
353 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
355 #define END_FOR_EACH_PTR_REVERSE(ptr) \
356 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
358 #define THIS_ADDRESS(ptr) \
359 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
361 extern void split_ptr_list_head(struct ptr_list *);
363 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
364 split_ptr_list_head(__list); \
365 if (__nr >= __list->nr) { \
366 __nr -= __list->nr; \
367 __list = __list->next; \
368 }; \
369 } while (0)
371 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
372 void **__this, **__last; \
373 if (__list->nr == LIST_NODE_NR) \
374 DO_SPLIT(ptr, __head, __list, __nr); \
375 __this = __list->list + __nr; \
376 __last = __list->list + __list->nr - 1; \
377 while (__last >= __this) { \
378 __last[1] = __last[0]; \
379 __last--; \
381 *__this = (new); \
382 __list->nr++; \
383 } while (0)
385 #define INSERT_CURRENT(new, ptr) \
386 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
388 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
389 void **__this = __list->list + __nr; \
390 void **__last = __list->list + __list->nr - 1; \
391 while (__this < __last) { \
392 __this[0] = __this[1]; \
393 __this++; \
395 *__this = (void *)0xf0f0f0f0; \
396 __list->nr--; __nr--; \
397 } while (0)
399 #define DELETE_CURRENT_PTR(ptr) \
400 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
402 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
403 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
405 extern void pack_ptr_list(struct ptr_list **);
407 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
409 #endif