[be] fix some of the brokenness related to non-32-bit variables
[smatch.git] / lib.h
blobdb6a8f4a9d64584f95dfda2b52d8124b7d019ddd
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;
134 extern void create_builtin_stream(void);
136 static inline int symbol_list_size(struct symbol_list* list)
138 return ptr_list_size((struct ptr_list *)(list));
141 static inline int statement_list_size(struct statement_list* list)
143 return ptr_list_size((struct ptr_list *)(list));
146 static inline int expression_list_size(struct expression_list* list)
148 return ptr_list_size((struct ptr_list *)(list));
151 static inline int instruction_list_size(struct instruction_list* list)
153 return ptr_list_size((struct ptr_list *)(list));
156 static inline int phi_list_size(struct phi_list* list)
158 return ptr_list_size((struct ptr_list *)(list));
161 static inline int bb_list_size(struct basic_block_list* list)
163 return ptr_list_size((struct ptr_list *)(list));
166 static inline struct basic_block* next_basic_block(struct list_iterator *iterator)
168 return next_iterator(iterator);
171 static inline struct multijmp* next_multijmp(struct list_iterator *iterator)
173 return next_iterator(iterator);
176 static inline void free_instruction_list(struct instruction_list **head)
178 free_ptr_list((struct ptr_list **)head);
181 static inline void init_multijmp_iterator(struct multijmp_list **head, struct list_iterator *iterator, int flags)
183 init_iterator((struct ptr_list **)head, iterator, flags);
186 static inline void init_bb_iterator(struct basic_block_list **head, struct list_iterator *iterator, int flags)
188 init_iterator((struct ptr_list **)head, iterator, flags);
191 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
193 return delete_ptr_list_last((struct ptr_list **)head);
196 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
198 return delete_ptr_list_last((struct ptr_list **)head);
201 static inline void *first_ptr_list(struct ptr_list *list)
203 if (!list)
204 return NULL;
205 return list->list[0];
208 static inline void *last_ptr_list(struct ptr_list *list)
211 if (!list)
212 return NULL;
213 list = list->prev;
214 return list->list[list->nr-1];
217 static inline void * current_iterator(struct list_iterator *iterator)
219 struct ptr_list *list = iterator->active;
220 return list ? list->list[iterator->index] : NULL;
223 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
225 return last_ptr_list((struct ptr_list *)head);
227 static inline struct instruction *last_instruction(struct instruction_list *head)
229 return last_ptr_list((struct ptr_list *)head);
232 static inline struct instruction *first_instruction(struct instruction_list *head)
234 return first_ptr_list((struct ptr_list *)head);
237 static inline struct phi *first_phi(struct phi_list *head)
239 return first_ptr_list((struct ptr_list *)head);
242 static inline int replace_basic_block_list(struct basic_block_list **head, struct basic_block *from, struct basic_block *to)
244 return replace_ptr_list((struct ptr_list **)head, (void*)from, (void*)to);
247 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
249 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
252 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
254 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
257 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
259 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
262 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
264 add_ptr_list((struct ptr_list **)list, sym);
267 static inline void add_statement(struct statement_list **list, struct statement *stmt)
269 add_ptr_list((struct ptr_list **)list, stmt);
272 static inline void add_expression(struct expression_list **list, struct expression *expr)
274 add_ptr_list((struct ptr_list **)list, expr);
277 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
279 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
282 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
284 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
287 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
289 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
292 #define PREPARE_PTR_LIST(head, ptr) \
293 do { \
294 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
295 struct ptr_list *__list##ptr = __head##ptr; \
296 int __nr##ptr = 0; \
297 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
298 else ptr = NULL
300 #define NEXT_PTR_LIST(ptr) \
301 if (ptr) { \
302 if (++__nr##ptr < __list##ptr->nr) { \
303 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
304 } else { \
305 __list##ptr = __list##ptr->next; \
306 ptr = NULL; \
307 if (__list##ptr != __head##ptr) { \
308 __nr##ptr = 0; \
309 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
314 #define RESET_PTR_LIST(ptr) do { \
315 __nr##ptr = 0; \
316 __list##ptr = __head##ptr; \
317 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
318 } while (0)
321 #define FINISH_PTR_LIST(ptr) \
322 (void)(__nr##ptr); /* Sanity-check nesting */ \
323 } while (0)
325 #define FOR_EACH_PTR(head, ptr) do { \
326 struct ptr_list *__head = (struct ptr_list *) (head); \
327 struct ptr_list *__list = __head; \
328 int __flag = ITERATE_FIRST; \
329 if (__head) { \
330 do { int __i; \
331 for (__i = 0; __i < __list->nr; __i++) { \
332 if (__i == __list->nr-1 && __list->next == __head) \
333 __flag |= ITERATE_LAST; \
334 do { \
335 ptr = (__typeof__(ptr)) (__list->list[__i]); \
336 do {
338 #define END_FOR_EACH_PTR } while (0); \
339 } while (0); \
340 __flag = 0; \
342 } while ((__list = __list->next) != __head); \
344 } while (0)
346 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
347 struct ptr_list *__head = (struct ptr_list *) (head); \
348 struct ptr_list *__list = __head; \
349 int __flag = ITERATE_FIRST; \
350 if (__head) { \
351 do { int __i; \
352 __list = __list->prev; \
353 __i = __list->nr; \
354 while (--__i >= 0) { \
355 if (__i == 0 && __list == __head) \
356 __flag |= ITERATE_LAST; \
357 do { \
358 ptr = (__typeof__(ptr)) (__list->list[__i]); \
359 do {
361 #define END_FOR_EACH_PTR_REVERSE } while (0); \
362 } while (0); \
363 __flag = 0; \
365 } while (__list != __head); \
367 } while (0)
369 #define THIS_ADDRESS(x) \
370 ((__typeof__(&(x))) (__list->list + __i))
372 #endif