Make our "__builtin_va_arg()" thing a bit closer to real.
[smatch.git] / lib.h
blob9fdc0f48fcc8ee7699a4acfed9ac48a0d2604d9c
1 #ifndef LIB_H
2 #define LIB_H
4 #include <stdlib.h>
6 /*
7 * Basic helper routine descriptions for 'sparse'.
9 * Copyright (C) 2003 Transmeta Corp.
10 * 2003 Linus Torvalds
11 * 2004 Christopher Li
13 * Licensed under the Open Software License version 1.1
16 #include "compat.h"
18 extern unsigned int hexval(unsigned int c);
20 struct position {
21 unsigned int type:6,
22 stream:10,
23 pos:14,
24 newline:1,
25 whitespace:1;
26 unsigned int line:31,
27 noexpand:1;
30 struct ident;
31 struct token;
32 struct symbol;
33 struct symbol_list;
34 struct statement;
35 struct statement_list;
36 struct expression;
37 struct expression_list;
38 struct basic_block;
39 struct basic_block_list;
40 struct entrypoint;
41 struct instruction;
42 struct instruction_list;
43 struct multijmp;
44 struct multijmp_list;
45 struct phi;
46 struct phi_list;
47 struct pseudo;
49 typedef struct pseudo *pseudo_t;
51 struct token *skip_to(struct token *, int);
52 struct token *expect(struct token *, int, const char *);
53 #ifdef __GNUC__
54 #define FORMAT_ATTR __attribute__ ((__format__ (__printf__, 2, 3)))
55 #else
56 #define FORMAT_ATTR
57 #endif
58 extern void info(struct position, const char *, ...) FORMAT_ATTR;
59 extern void warning(struct position, const char *, ...) FORMAT_ATTR;
60 extern void error(struct position, const char *, ...) FORMAT_ATTR;
61 extern void error_die(struct position, const char *, ...) FORMAT_ATTR;
62 #undef FORMAT_ATTR
64 #define __DECLARE_ALLOCATOR(type, x) \
65 extern type *__alloc_##x(int); \
66 extern void show_##x##_alloc(void); \
67 extern void clear_##x##_alloc(void);
68 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
70 DECLARE_ALLOCATOR(ident);
71 DECLARE_ALLOCATOR(token);
72 DECLARE_ALLOCATOR(symbol);
73 DECLARE_ALLOCATOR(expression);
74 DECLARE_ALLOCATOR(statement);
75 DECLARE_ALLOCATOR(string);
76 DECLARE_ALLOCATOR(scope);
77 __DECLARE_ALLOCATOR(void, bytes);
78 DECLARE_ALLOCATOR(basic_block);
79 DECLARE_ALLOCATOR(entrypoint);
80 DECLARE_ALLOCATOR(instruction);
81 DECLARE_ALLOCATOR(multijmp);
82 DECLARE_ALLOCATOR(phi);
83 DECLARE_ALLOCATOR(pseudo);
86 #define LIST_NODE_NR (29)
88 struct ptr_list {
89 int nr;
90 struct ptr_list *prev;
91 struct ptr_list *next;
92 void *list[LIST_NODE_NR];
95 struct list_iterator {
96 struct ptr_list **head;
97 struct ptr_list *active;
98 int index;
99 unsigned int flags;
102 enum iterator_br_state {
103 BR_INIT,
104 BR_TRUE,
105 BR_FALSE,
106 BR_END,
109 struct terminator_iterator {
110 struct instruction *terminator;
111 union {
112 struct list_iterator multijmp;
113 int branch;
117 #define ITERATOR_BACKWARDS 1
118 #define ITERATOR_CURRENT 2
120 #define ptr_list_empty(x) ((x) == NULL)
122 void init_iterator(struct ptr_list **head, struct list_iterator *iterator, int flags);
123 void * next_iterator(struct list_iterator *iterator);
124 void delete_iterator(struct list_iterator *iterator);
125 void init_terminator_iterator(struct instruction* terminator, struct terminator_iterator *iterator);
126 struct basic_block* next_terminator_bb(struct terminator_iterator *iterator);
127 void replace_terminator_bb(struct terminator_iterator *iterator, struct basic_block* bb);
128 void * delete_ptr_list_last(struct ptr_list **head);
129 int replace_ptr_list(struct ptr_list *head, void *old_ptr, void *new_ptr);
130 extern void sort_list(struct ptr_list **, int (*)(const void *, const void *));
132 extern void add_ptr_list(struct ptr_list **, void *);
133 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
134 extern void free_ptr_list(struct ptr_list **);
135 extern int ptr_list_size(struct ptr_list *);
136 extern char **handle_switch(char *arg, char **next);
137 extern void add_pre_buffer(const char *fmt, ...);
138 void * next_iterator(struct list_iterator *iterator);
139 int linearize_ptr_list(struct ptr_list *, void **, int);
141 extern unsigned int pre_buffer_size;
142 extern unsigned char pre_buffer[8192];
143 extern int include_fd;
144 extern char *include;
145 extern int preprocess_only;
146 extern int Wdefault_bitfield_sign;
147 extern int Wundefined_preprocessor;
148 extern int Wbitwise, Wtypesign, Wcontext;
150 extern void declare_builtin_functions(void);
151 extern void create_builtin_stream(void);
153 static inline int symbol_list_size(struct symbol_list* list)
155 return ptr_list_size((struct ptr_list *)(list));
158 static inline int statement_list_size(struct statement_list* list)
160 return ptr_list_size((struct ptr_list *)(list));
163 static inline int expression_list_size(struct expression_list* list)
165 return ptr_list_size((struct ptr_list *)(list));
168 static inline int instruction_list_size(struct instruction_list* list)
170 return ptr_list_size((struct ptr_list *)(list));
173 static inline int phi_list_size(struct phi_list* list)
175 return ptr_list_size((struct ptr_list *)(list));
178 static inline int bb_list_size(struct basic_block_list* list)
180 return ptr_list_size((struct ptr_list *)(list));
183 static inline struct basic_block* next_basic_block(struct list_iterator *iterator)
185 return next_iterator(iterator);
188 static inline struct multijmp* next_multijmp(struct list_iterator *iterator)
190 return next_iterator(iterator);
193 static inline void free_instruction_list(struct instruction_list **head)
195 free_ptr_list((struct ptr_list **)head);
198 static inline void init_multijmp_iterator(struct multijmp_list **head, struct list_iterator *iterator, int flags)
200 init_iterator((struct ptr_list **)head, iterator, flags);
203 static inline void init_bb_iterator(struct basic_block_list **head, struct list_iterator *iterator, int flags)
205 init_iterator((struct ptr_list **)head, iterator, flags);
208 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
210 return delete_ptr_list_last((struct ptr_list **)head);
213 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
215 return delete_ptr_list_last((struct ptr_list **)head);
218 static inline void *first_ptr_list(struct ptr_list *list)
220 if (!list)
221 return NULL;
222 return list->list[0];
225 static inline void *last_ptr_list(struct ptr_list *list)
228 if (!list)
229 return NULL;
230 list = list->prev;
231 return list->list[list->nr-1];
234 static inline void * current_iterator(struct list_iterator *iterator)
236 struct ptr_list *list = iterator->active;
237 return list ? list->list[iterator->index] : NULL;
240 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
242 return first_ptr_list((struct ptr_list *)head);
244 static inline struct instruction *last_instruction(struct instruction_list *head)
246 return last_ptr_list((struct ptr_list *)head);
249 static inline struct instruction *first_instruction(struct instruction_list *head)
251 return first_ptr_list((struct ptr_list *)head);
254 static inline struct phi *first_phi(struct phi_list *head)
256 return first_ptr_list((struct ptr_list *)head);
259 static inline int replace_basic_block_list(struct basic_block_list *head, struct basic_block *from, struct basic_block *to)
261 return replace_ptr_list((struct ptr_list *)head, (void*)from, (void*)to);
264 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
266 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
269 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
271 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
274 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
276 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
279 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
281 add_ptr_list((struct ptr_list **)list, sym);
284 static inline void add_statement(struct statement_list **list, struct statement *stmt)
286 add_ptr_list((struct ptr_list **)list, stmt);
289 static inline void add_expression(struct expression_list **list, struct expression *expr)
291 add_ptr_list((struct ptr_list **)list, expr);
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