Poison deleted ptr-list entries.
[smatch.git] / lib.h
blob92745830ec062226b6bbc6f8d8728ec314435758
1 #ifndef LIB_H
2 #define LIB_H
3 /*
4 * Basic helper routine descriptions for 'sparse'.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
8 * 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
13 extern unsigned int hexval(unsigned int c);
15 struct position {
16 unsigned int type:6,
17 stream:10,
18 pos:14,
19 newline:1,
20 whitespace:1;
21 unsigned int line:31,
22 noexpand:1;
25 struct ident;
26 struct token;
27 struct symbol;
28 struct symbol_list;
29 struct statement;
30 struct statement_list;
31 struct expression;
32 struct expression_list;
33 struct basic_block;
34 struct basic_block_list;
35 struct entrypoint;
36 struct instruction;
37 struct instruction_list;
38 struct multijmp;
39 struct multijmp_list;
40 struct phi;
41 struct phi_list;
43 struct token *skip_to(struct token *, int);
44 struct token *expect(struct token *, int, const char *);
45 extern void info(struct position, const char *, ...);
46 extern void warn(struct position, const char *, ...);
47 extern void error(struct position, const char *, ...);
49 #define __DECLARE_ALLOCATOR(type, x) \
50 extern type *__alloc_##x(int); \
51 extern void show_##x##_alloc(void); \
52 extern void clear_##x##_alloc(void);
53 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
55 DECLARE_ALLOCATOR(ident);
56 DECLARE_ALLOCATOR(token);
57 DECLARE_ALLOCATOR(symbol);
58 DECLARE_ALLOCATOR(expression);
59 DECLARE_ALLOCATOR(statement);
60 DECLARE_ALLOCATOR(string);
61 DECLARE_ALLOCATOR(scope);
62 __DECLARE_ALLOCATOR(void, bytes);
63 DECLARE_ALLOCATOR(basic_block);
64 DECLARE_ALLOCATOR(entrypoint);
65 DECLARE_ALLOCATOR(instruction);
66 DECLARE_ALLOCATOR(multijmp);
67 DECLARE_ALLOCATOR(phi);
68 DECLARE_ALLOCATOR(pseudo);
71 #define LIST_NODE_NR (29)
73 struct ptr_list {
74 int nr;
75 struct ptr_list *prev;
76 struct ptr_list *next;
77 void *list[LIST_NODE_NR];
80 struct list_iterator {
81 struct ptr_list **head;
82 struct ptr_list *active;
83 int index;
84 unsigned int flags;
87 enum iterator_br_state {
88 BR_INIT,
89 BR_TRUE,
90 BR_FALSE,
91 BR_END,
94 struct terminator_iterator {
95 struct instruction *terminator;
96 union {
97 struct list_iterator multijmp;
98 int branch;
102 #define ITERATOR_BACKWARDS 1
103 #define ITERATOR_CURRENT 2
105 #define ITERATE_FIRST 1
106 #define ITERATE_LAST 2
108 #define ptr_list_empty(x) ((x) == NULL)
110 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
111 void init_iterator(struct ptr_list **head, struct list_iterator *iterator, int flags);
112 void * next_iterator(struct list_iterator *iterator);
113 void delete_iterator(struct list_iterator *iterator);
114 void init_terminator_iterator(struct instruction* terminator, struct terminator_iterator *iterator);
115 struct basic_block* next_terminator_bb(struct terminator_iterator *iterator);
116 void replace_terminator_bb(struct terminator_iterator *iterator, struct basic_block* bb);
117 void * delete_ptr_list_last(struct ptr_list **head);
118 int replace_ptr_list(struct ptr_list *head, void *old_ptr, void *new_ptr);
120 extern void add_ptr_list(struct ptr_list **, void *);
121 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
122 extern void free_ptr_list(struct ptr_list **);
123 extern int ptr_list_size(struct ptr_list *);
124 extern char **handle_switch(char *arg, char **next);
125 extern void add_pre_buffer(const char *fmt, ...);
126 void * next_iterator(struct list_iterator *iterator);
128 extern unsigned int pre_buffer_size;
129 extern unsigned char pre_buffer[8192];
130 extern int include_fd;
131 extern char *include;
132 extern int preprocess_only;
133 extern int Wdefault_bitfield_sign;
134 extern int Wbitwise;
136 extern void create_builtin_stream(void);
138 static inline int symbol_list_size(struct symbol_list* list)
140 return ptr_list_size((struct ptr_list *)(list));
143 static inline int statement_list_size(struct statement_list* list)
145 return ptr_list_size((struct ptr_list *)(list));
148 static inline int expression_list_size(struct expression_list* list)
150 return ptr_list_size((struct ptr_list *)(list));
153 static inline int instruction_list_size(struct instruction_list* list)
155 return ptr_list_size((struct ptr_list *)(list));
158 static inline int phi_list_size(struct phi_list* list)
160 return ptr_list_size((struct ptr_list *)(list));
163 static inline int bb_list_size(struct basic_block_list* list)
165 return ptr_list_size((struct ptr_list *)(list));
168 static inline struct basic_block* next_basic_block(struct list_iterator *iterator)
170 return next_iterator(iterator);
173 static inline struct multijmp* next_multijmp(struct list_iterator *iterator)
175 return next_iterator(iterator);
178 static inline void free_instruction_list(struct instruction_list **head)
180 free_ptr_list((struct ptr_list **)head);
183 static inline void init_multijmp_iterator(struct multijmp_list **head, struct list_iterator *iterator, int flags)
185 init_iterator((struct ptr_list **)head, iterator, flags);
188 static inline void init_bb_iterator(struct basic_block_list **head, struct list_iterator *iterator, int flags)
190 init_iterator((struct ptr_list **)head, iterator, flags);
193 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
195 return delete_ptr_list_last((struct ptr_list **)head);
198 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
200 return delete_ptr_list_last((struct ptr_list **)head);
203 static inline void *first_ptr_list(struct ptr_list *list)
205 if (!list)
206 return NULL;
207 return list->list[0];
210 static inline void *last_ptr_list(struct ptr_list *list)
213 if (!list)
214 return NULL;
215 list = list->prev;
216 return list->list[list->nr-1];
219 static inline void * current_iterator(struct list_iterator *iterator)
221 struct ptr_list *list = iterator->active;
222 return list ? list->list[iterator->index] : NULL;
225 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
227 return first_ptr_list((struct ptr_list *)head);
229 static inline struct instruction *last_instruction(struct instruction_list *head)
231 return last_ptr_list((struct ptr_list *)head);
234 static inline struct instruction *first_instruction(struct instruction_list *head)
236 return first_ptr_list((struct ptr_list *)head);
239 static inline struct phi *first_phi(struct phi_list *head)
241 return first_ptr_list((struct ptr_list *)head);
244 static inline int replace_basic_block_list(struct basic_block_list *head, struct basic_block *from, struct basic_block *to)
246 return replace_ptr_list((struct ptr_list *)head, (void*)from, (void*)to);
249 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
251 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
254 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
256 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
259 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
261 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
264 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
266 add_ptr_list((struct ptr_list **)list, sym);
269 static inline void add_statement(struct statement_list **list, struct statement *stmt)
271 add_ptr_list((struct ptr_list **)list, stmt);
274 static inline void add_expression(struct expression_list **list, struct expression *expr)
276 add_ptr_list((struct ptr_list **)list, expr);
279 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
281 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
284 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
286 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
289 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
291 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
294 #define DO_PREPARE(head, ptr, __head, __list, __nr) \
295 do { \
296 struct ptr_list *__head = (struct ptr_list *) (head); \
297 struct ptr_list *__list = __head; \
298 int __nr = 0; \
299 if (__head) ptr = (__typeof__(ptr)) __head->list[0]; \
300 else ptr = NULL
302 #define DO_NEXT(ptr, __head, __list, __nr) \
303 if (ptr) { \
304 if (++__nr < __list->nr) { \
305 ptr = (__typeof__(ptr)) __list->list[__nr]; \
306 } else { \
307 __list = __list->next; \
308 ptr = NULL; \
309 if (__list != __head) { \
310 __nr = 0; \
311 ptr = (__typeof__(ptr)) __list->list[0]; \
316 #define DO_RESET(ptr, __head, __list, __nr) \
317 do { \
318 __nr = 0; \
319 __list = __head; \
320 if (__head) ptr = (__typeof__(ptr)) __head->list[0]; \
321 } while (0)
323 #define DO_FINISH(ptr, __head, __list, __nr) \
324 (void)(__nr); /* Sanity-check nesting */ \
325 } while (0)
327 #define PREPARE_PTR_LIST(head, ptr) \
328 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
330 #define NEXT_PTR_LIST(ptr) \
331 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr)
333 #define RESET_PTR_LIST(ptr) \
334 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr)
336 #define FINISH_PTR_LIST(ptr) \
337 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
339 #define DO_FOR_EACH(head, ptr, __head, __list, __nr) do { \
340 struct ptr_list *__head = (struct ptr_list *) (head); \
341 struct ptr_list *__list = __head; \
342 if (__head) { \
343 do { int __nr; \
344 for (__nr = 0; __nr < __list->nr; __nr++) { \
345 do { \
346 ptr = (__typeof__(ptr)) (__list->list[__nr]); \
347 do {
349 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
350 } while (0); \
351 } while (0); \
353 } while ((__list = __list->next) != __head); \
355 } while (0)
357 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr) do { \
358 struct ptr_list *__head = (struct ptr_list *) (head); \
359 struct ptr_list *__list = __head; \
360 if (__head) { \
361 do { int __nr; \
362 __list = __list->prev; \
363 __nr = __list->nr; \
364 while (--__nr >= 0) { \
365 do { \
366 ptr = (__typeof__(ptr)) (__list->list[__nr]); \
367 do {
370 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
371 } while (0); \
372 } while (0); \
374 } while (__list != __head); \
376 } while (0)
378 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
379 ((__typeof__(&(ptr))) (__list->list + __nr))
381 #define FOR_EACH_PTR(head, ptr) \
382 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
384 #define END_FOR_EACH_PTR(ptr) \
385 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
387 #define FOR_EACH_PTR_REVERSE(head, ptr) \
388 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
390 #define END_FOR_EACH_PTR_REVERSE(ptr) \
391 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
393 #define THIS_ADDRESS(ptr) \
394 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
396 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
397 void **__this = __list->list + __nr; \
398 void **__last = __list->list + __list->nr - 1; \
399 while (__this < __last) { \
400 __this[0] = __this[1]; \
401 __this++; \
403 *__this = (void *)0xf0f0f0f0; \
404 __list->nr--; __nr--; \
405 } while (0)
407 #define DELETE_CURRENT_PTR(ptr) \
408 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
410 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
411 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
413 #endif