Make sparse sources themselves be sparse-clean.
[smatch.git] / lib.h
blob539dcfd0d0fbb8239ef18ca13610c6a513f754d2
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;
24 struct ident;
25 struct token;
26 struct symbol;
27 struct symbol_list;
28 struct statement;
29 struct statement_list;
30 struct expression;
31 struct expression_list;
32 struct basic_block;
33 struct basic_block_list;
34 struct entrypoint;
35 struct instruction;
36 struct instruction_list;
37 struct multijmp;
38 struct multijmp_list;
39 struct phi;
40 struct phi_list;
42 struct token *skip_to(struct token *, int);
43 struct token *expect(struct token *, int, const char *);
44 extern void warn(struct position, const char *, ...);
45 extern void error(struct position, const char *, ...);
47 #define __DECLARE_ALLOCATOR(type, x) \
48 extern type *__alloc_##x(int); \
49 extern void show_##x##_alloc(void); \
50 extern void clear_##x##_alloc(void);
51 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
53 DECLARE_ALLOCATOR(ident);
54 DECLARE_ALLOCATOR(token);
55 DECLARE_ALLOCATOR(symbol);
56 DECLARE_ALLOCATOR(expression);
57 DECLARE_ALLOCATOR(statement);
58 DECLARE_ALLOCATOR(string);
59 DECLARE_ALLOCATOR(scope);
60 __DECLARE_ALLOCATOR(void, bytes);
61 DECLARE_ALLOCATOR(basic_block);
62 DECLARE_ALLOCATOR(entrypoint);
63 DECLARE_ALLOCATOR(instruction);
64 DECLARE_ALLOCATOR(multijmp);
65 DECLARE_ALLOCATOR(phi);
66 DECLARE_ALLOCATOR(pseudo);
69 #define LIST_NODE_NR (29)
71 struct ptr_list {
72 int nr;
73 struct ptr_list *prev;
74 struct ptr_list *next;
75 void *list[LIST_NODE_NR];
78 struct list_iterator {
79 struct ptr_list **head;
80 struct ptr_list *active;
81 int index;
82 unsigned int flags;
85 enum iterator_br_state {
86 BR_INIT,
87 BR_TRUE,
88 BR_FALSE,
89 BR_END,
92 struct terminator_iterator {
93 struct instruction *terminator;
94 union {
95 struct list_iterator multijmp;
96 int branch;
100 #define ITERATOR_BACKWARDS 1
101 #define ITERATOR_CURRENT 2
103 #define ITERATE_FIRST 1
104 #define ITERATE_LAST 2
106 #define ptr_list_empty(x) ((x) == NULL)
108 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
109 void init_iterator(struct ptr_list **head, struct list_iterator *iterator, int flags);
110 void * next_iterator(struct list_iterator *iterator);
111 void delete_iterator(struct list_iterator *iterator);
112 void init_terminator_iterator(struct instruction* terminator, struct terminator_iterator *iterator);
113 struct basic_block* next_terminator_bb(struct terminator_iterator *iterator);
114 void replace_terminator_bb(struct terminator_iterator *iterator, struct basic_block* bb);
115 void * delete_ptr_list_last(struct ptr_list **head);
116 int replace_ptr_list(struct ptr_list **head, void *old_ptr, void *new_ptr);
118 extern void add_ptr_list(struct ptr_list **, void *);
119 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
120 extern void free_ptr_list(struct ptr_list **);
121 extern int ptr_list_size(struct ptr_list *);
122 extern char **handle_switch(char *arg, char **next);
123 extern void add_pre_buffer(const char *fmt, ...);
124 void * next_iterator(struct list_iterator *iterator);
126 extern unsigned int pre_buffer_size;
127 extern unsigned char pre_buffer[8192];
128 extern int include_fd;
129 extern char *include;
130 extern int preprocess_only;
132 extern void create_builtin_stream(void);
134 static inline int symbol_list_size(struct symbol_list* list)
136 return ptr_list_size((struct ptr_list *)(list));
139 static inline int statement_list_size(struct statement_list* list)
141 return ptr_list_size((struct ptr_list *)(list));
144 static inline int expression_list_size(struct expression_list* list)
146 return ptr_list_size((struct ptr_list *)(list));
149 static inline int instruction_list_size(struct instruction_list* list)
151 return ptr_list_size((struct ptr_list *)(list));
154 static inline int phi_list_size(struct phi_list* list)
156 return ptr_list_size((struct ptr_list *)(list));
159 static inline int bb_list_size(struct basic_block_list* list)
161 return ptr_list_size((struct ptr_list *)(list));
164 static inline struct basic_block* next_basic_block(struct list_iterator *iterator)
166 return next_iterator(iterator);
169 static inline struct multijmp* next_multijmp(struct list_iterator *iterator)
171 return next_iterator(iterator);
174 static inline void free_instruction_list(struct instruction_list **head)
176 free_ptr_list((struct ptr_list **)head);
179 static inline void init_multijmp_iterator(struct multijmp_list **head, struct list_iterator *iterator, int flags)
181 init_iterator((struct ptr_list **)head, iterator, flags);
184 static inline void init_bb_iterator(struct basic_block_list **head, struct list_iterator *iterator, int flags)
186 init_iterator((struct ptr_list **)head, iterator, flags);
189 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
191 return delete_ptr_list_last((struct ptr_list **)head);
194 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
196 return delete_ptr_list_last((struct ptr_list **)head);
199 static inline void *first_ptr_list(struct ptr_list *list)
201 if (!list)
202 return NULL;
203 return list->list[0];
206 static inline void *last_ptr_list(struct ptr_list *list)
209 if (!list)
210 return NULL;
211 list = list->prev;
212 return list->list[list->nr-1];
215 static inline void * current_iterator(struct list_iterator *iterator)
217 struct ptr_list *list = iterator->active;
218 return list ? list->list[iterator->index] : NULL;
221 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
223 return last_ptr_list((struct ptr_list *)head);
225 static inline struct instruction *last_instruction(struct instruction_list *head)
227 return last_ptr_list((struct ptr_list *)head);
230 static inline struct instruction *first_instruction(struct instruction_list *head)
232 return first_ptr_list((struct ptr_list *)head);
235 static inline struct phi *first_phi(struct phi_list *head)
237 return first_ptr_list((struct ptr_list *)head);
240 static inline int replace_basic_block_list(struct basic_block_list **head, struct basic_block *from, struct basic_block *to)
242 return replace_ptr_list((struct ptr_list **)head, (void*)from, (void*)to);
245 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
247 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
250 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
252 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
255 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
257 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
260 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
262 add_ptr_list((struct ptr_list **)list, sym);
265 static inline void add_statement(struct statement_list **list, struct statement *stmt)
267 add_ptr_list((struct ptr_list **)list, stmt);
270 static inline void add_expression(struct expression_list **list, struct expression *expr)
272 add_ptr_list((struct ptr_list **)list, expr);
275 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
277 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
280 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
282 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
285 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
287 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
290 #define PREPARE_PTR_LIST(head, ptr) \
291 do { \
292 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
293 struct ptr_list *__list##ptr = __head##ptr; \
294 int __nr##ptr = 0; \
295 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
296 else ptr = NULL
298 #define NEXT_PTR_LIST(ptr) \
299 if (ptr) { \
300 if (++__nr##ptr < __list##ptr->nr) { \
301 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
302 } else { \
303 __list##ptr = __list##ptr->next; \
304 ptr = NULL; \
305 if (__list##ptr != __head##ptr) { \
306 __nr##ptr = 0; \
307 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
312 #define RESET_PTR_LIST(ptr) do { \
313 __nr##ptr = 0; \
314 __list##ptr = __head##ptr; \
315 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
316 } while (0)
319 #define FINISH_PTR_LIST(ptr) \
320 (void)(__nr##ptr); /* Sanity-check nesting */ \
321 } while (0)
323 #define FOR_EACH_PTR(head, ptr) do { \
324 struct ptr_list *__head = (struct ptr_list *) (head); \
325 struct ptr_list *__list = __head; \
326 int __flag = ITERATE_FIRST; \
327 if (__head) { \
328 do { int __i; \
329 for (__i = 0; __i < __list->nr; __i++) { \
330 if (__i == __list->nr-1 && __list->next == __head) \
331 __flag |= ITERATE_LAST; \
332 do { \
333 ptr = (__typeof__(ptr)) (__list->list[__i]); \
334 do {
336 #define END_FOR_EACH_PTR } while (0); \
337 } while (0); \
338 __flag = 0; \
340 } while ((__list = __list->next) != __head); \
342 } while (0)
344 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
345 struct ptr_list *__head = (struct ptr_list *) (head); \
346 struct ptr_list *__list = __head; \
347 int __flag = ITERATE_FIRST; \
348 if (__head) { \
349 do { int __i; \
350 __list = __list->prev; \
351 __i = __list->nr; \
352 while (--__i >= 0) { \
353 if (__i == 0 && __list == __head) \
354 __flag |= ITERATE_LAST; \
355 do { \
356 ptr = (__typeof__(ptr)) (__list->list[__i]); \
357 do {
359 #define END_FOR_EACH_PTR_REVERSE } while (0); \
360 } while (0); \
361 __flag = 0; \
363 } while (__list != __head); \
365 } while (0)
367 #define THIS_ADDRESS(x) \
368 ((__typeof__(&(x))) (__list->list + __i))
370 #endif