Fix pointer addition
[smatch.git] / lib.h
blob7ae96b09d1e187f2e967bc8c0932aa9e91591667
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;
40 struct token *skip_to(struct token *, int);
41 struct token *expect(struct token *, int, const char *);
42 extern void warn(struct position, const char *, ...);
43 extern void error(struct position, const char *, ...);
45 #define __DECLARE_ALLOCATOR(type, x) \
46 extern type *__alloc_##x(int); \
47 extern void show_##x##_alloc(void); \
48 extern void clear_##x##_alloc(void);
49 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
51 DECLARE_ALLOCATOR(ident);
52 DECLARE_ALLOCATOR(token);
53 DECLARE_ALLOCATOR(symbol);
54 DECLARE_ALLOCATOR(expression);
55 DECLARE_ALLOCATOR(statement);
56 DECLARE_ALLOCATOR(string);
57 DECLARE_ALLOCATOR(scope);
58 __DECLARE_ALLOCATOR(void, bytes);
59 DECLARE_ALLOCATOR(basic_block);
60 DECLARE_ALLOCATOR(entrypoint);
61 DECLARE_ALLOCATOR(instruction);
62 DECLARE_ALLOCATOR(multijmp);
63 DECLARE_ALLOCATOR(phi);
64 DECLARE_ALLOCATOR(pseudo);
67 #define LIST_NODE_NR (29)
69 struct ptr_list {
70 int nr;
71 struct ptr_list *prev;
72 struct ptr_list *next;
73 void *list[LIST_NODE_NR];
76 struct list_iterator {
77 struct ptr_list **head;
78 struct ptr_list *active;
79 int index;
80 unsigned int flags;
83 enum iterator_br_state {
84 BR_INIT,
85 BR_TRUE,
86 BR_FALSE,
87 BR_END,
90 struct terminator_iterator {
91 struct instruction *terminator;
92 union {
93 struct list_iterator multijmp;
94 int branch;
98 #define ITERATOR_BACKWARDS 1
99 #define ITERATOR_CURRENT 2
101 #define ITERATE_FIRST 1
102 #define ITERATE_LAST 2
104 #define ptr_list_empty(x) ((x) == NULL)
106 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
107 void init_iterator(struct ptr_list **head, struct list_iterator *iterator, int flags);
108 void * next_iterator(struct list_iterator *iterator);
109 void delete_iterator(struct list_iterator *iterator);
110 void init_terminator_iterator(struct instruction* terminator, struct terminator_iterator *iterator);
111 struct basic_block* next_terminator_bb(struct terminator_iterator *iterator);
112 void replace_terminator_bb(struct terminator_iterator *iterator, struct basic_block* bb);
113 void * delete_ptr_list_last(struct ptr_list **head);
114 int replace_ptr_list(struct ptr_list **head, void *old_ptr, void *new_ptr);
116 extern void add_ptr_list(struct ptr_list **, void *);
117 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
118 extern void free_ptr_list(struct ptr_list **);
119 extern int ptr_list_size(struct ptr_list *);
120 extern char **handle_switch(char *arg, char **next);
121 extern void add_pre_buffer(const char *fmt, ...);
122 void * next_iterator(struct list_iterator *iterator);
124 extern unsigned int pre_buffer_size;
125 extern unsigned char pre_buffer[8192];
126 extern int include_fd;
127 extern char *include;
128 extern int preprocess_only;
130 extern void create_builtin_stream(void);
132 static inline int symbol_list_size(struct symbol_list* list)
134 return ptr_list_size((struct ptr_list *)(list));
137 static inline int statement_list_size(struct statement_list* list)
139 return ptr_list_size((struct ptr_list *)(list));
142 static inline int expression_list_size(struct expression_list* list)
144 return ptr_list_size((struct ptr_list *)(list));
147 static inline int instruction_list_size(struct instruction_list* list)
149 return ptr_list_size((struct ptr_list *)(list));
152 static inline int bb_list_size(struct basic_block_list* list)
154 return ptr_list_size((struct ptr_list *)(list));
157 static inline struct basic_block* next_basic_block(struct list_iterator *iterator)
159 return next_iterator(iterator);
162 static inline struct multijmp* next_multijmp(struct list_iterator *iterator)
164 return next_multijmp(iterator);
167 static inline void free_instruction_list(struct instruction_list **head)
169 free_ptr_list((struct ptr_list **)head);
172 static inline void init_multijmp_iterator(struct multijmp_list **head, struct list_iterator *iterator, int flags)
174 init_iterator((struct ptr_list **)head, iterator, flags);
177 static inline void init_bb_iterator(struct basic_block_list **head, struct list_iterator *iterator, int flags)
179 init_iterator((struct ptr_list **)head, iterator, flags);
182 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
184 return delete_ptr_list_last((struct ptr_list **)head);
187 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
189 return delete_ptr_list_last((struct ptr_list **)head);
192 static inline void *first_ptr_list(struct ptr_list *list)
194 if (!list)
195 return NULL;
196 return list->list[0];
199 static inline void *last_ptr_list(struct ptr_list *list)
202 if (!list)
203 return NULL;
204 list = list->prev;
205 return list->list[list->nr-1];
208 static inline void * current_iterator(struct list_iterator *iterator)
210 struct ptr_list *list = iterator->active;
211 return list ? list->list[iterator->index] : NULL;
214 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
216 return last_ptr_list((struct ptr_list *)head);
218 static inline struct instruction *last_instruction(struct instruction_list *head)
220 return last_ptr_list((struct ptr_list *)head);
223 static inline struct instruction *first_instruction(struct instruction_list *head)
225 return first_ptr_list((struct ptr_list *)head);
228 static inline int replace_basic_block_list(struct basic_block_list **head, struct basic_block *from, struct basic_block *to)
230 return replace_ptr_list((struct ptr_list **)head, (void*)from, (void*)to);
233 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
235 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
238 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
240 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
243 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
245 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
248 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
250 add_ptr_list((struct ptr_list **)list, sym);
253 static inline void add_statement(struct statement_list **list, struct statement *stmt)
255 add_ptr_list((struct ptr_list **)list, stmt);
258 static inline void add_expression(struct expression_list **list, struct expression *expr)
260 add_ptr_list((struct ptr_list **)list, expr);
263 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
265 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
268 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
270 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
273 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
275 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
278 #define PREPARE_PTR_LIST(head, ptr) \
279 do { \
280 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
281 struct ptr_list *__list##ptr = __head##ptr; \
282 int __nr##ptr = 0; \
283 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
284 else ptr = NULL
286 #define NEXT_PTR_LIST(ptr) \
287 if (ptr) { \
288 if (++__nr##ptr < __list##ptr->nr) { \
289 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
290 } else { \
291 __list##ptr = __list##ptr->next; \
292 ptr = NULL; \
293 if (__list##ptr != __head##ptr) { \
294 __nr##ptr = 0; \
295 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
300 #define RESET_PTR_LIST(ptr) do { \
301 __nr##ptr = 0; \
302 __list##ptr = __head##ptr; \
303 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
304 } while (0)
307 #define FINISH_PTR_LIST(ptr) \
308 (void)(__nr##ptr); /* Sanity-check nesting */ \
309 } while (0)
311 #define FOR_EACH_PTR(head, ptr) do { \
312 struct ptr_list *__head = (struct ptr_list *) (head); \
313 struct ptr_list *__list = __head; \
314 int __flag = ITERATE_FIRST; \
315 if (__head) { \
316 do { int __i; \
317 for (__i = 0; __i < __list->nr; __i++) { \
318 if (__i == __list->nr-1 && __list->next == __head) \
319 __flag |= ITERATE_LAST; \
320 do { \
321 ptr = (__typeof__(ptr)) (__list->list[__i]); \
322 do {
324 #define END_FOR_EACH_PTR } while (0); \
325 } while (0); \
326 __flag = 0; \
328 } while ((__list = __list->next) != __head); \
330 } while (0)
332 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
333 struct ptr_list *__head = (struct ptr_list *) (head); \
334 struct ptr_list *__list = __head; \
335 int __flag = ITERATE_FIRST; \
336 if (__head) { \
337 do { int __i; \
338 __list = __list->prev; \
339 __i = __list->nr; \
340 while (--__i >= 0) { \
341 if (__i == 0 && __list == __head) \
342 __flag |= ITERATE_LAST; \
343 do { \
344 ptr = (__typeof__(ptr)) (__list->list[__i]); \
345 do {
347 #define END_FOR_EACH_PTR_REVERSE } while (0); \
348 } while (0); \
349 __flag = 0; \
351 } while (__list != __head); \
353 } while (0)
355 #define THIS_ADDRESS(x) \
356 ((__typeof__(&(x))) (__list->list + __i))
358 #endif