This add a linearization phase. It's not even close to done
[smatch.git] / lib.h
blob6a6cc11946ceb9498a22bdc5e30c468da4d55180
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
9 * Licensed under the Open Software License version 1.1
12 extern unsigned int hexval(unsigned int c);
14 struct position {
15 unsigned int type:6,
16 stream:10,
17 pos:14,
18 newline:1,
19 whitespace:1;
20 unsigned int line;
23 struct ident;
24 struct token;
25 struct symbol;
26 struct symbol_list;
27 struct statement;
28 struct statement_list;
29 struct expression;
30 struct expression_list;
31 struct basic_block;
32 struct entrypoint;
34 struct token *skip_to(struct token *, int);
35 struct token *expect(struct token *, int, const char *);
36 extern void warn(struct position, const char *, ...);
37 extern void error(struct position, const char *, ...);
39 #define __DECLARE_ALLOCATOR(type, x) \
40 extern type *__alloc_##x(int); \
41 extern void show_##x##_alloc(void); \
42 extern void clear_##x##_alloc(void);
43 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
45 DECLARE_ALLOCATOR(ident);
46 DECLARE_ALLOCATOR(token);
47 DECLARE_ALLOCATOR(symbol);
48 DECLARE_ALLOCATOR(expression);
49 DECLARE_ALLOCATOR(statement);
50 DECLARE_ALLOCATOR(string);
51 DECLARE_ALLOCATOR(scope);
52 __DECLARE_ALLOCATOR(void, bytes);
53 DECLARE_ALLOCATOR(basic_block);
54 DECLARE_ALLOCATOR(entrypoint);
57 #define LIST_NODE_NR (29)
59 struct ptr_list {
60 int nr;
61 struct ptr_list *prev;
62 struct ptr_list *next;
63 void *list[LIST_NODE_NR];
66 #define ITERATE_FIRST 1
67 #define ITERATE_LAST 2
68 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
69 extern void add_ptr_list(struct ptr_list **, void *);
70 extern void copy_ptr_list(struct ptr_list *a, struct ptr_list **b);
71 extern void free_ptr_list(struct ptr_list **);
72 extern int ptr_list_size(struct ptr_list *);
73 extern char **handle_switch(char *arg, char **next);
74 extern void add_pre_buffer(const char *fmt, ...);
76 extern unsigned int pre_buffer_size;
77 extern unsigned char pre_buffer[8192];
78 extern int include_fd;
79 extern char *include;
80 extern int preprocess_only;
82 extern void create_builtin_stream(void);
84 #define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
85 #define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
86 #define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
88 static inline void copy_symbol_list(struct symbol_list *from, struct symbol_list **to)
90 copy_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
93 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
95 add_ptr_list((struct ptr_list **)list, sym);
98 static inline void add_statement(struct statement_list **list, struct statement *stmt)
100 add_ptr_list((struct ptr_list **)list, stmt);
103 static inline void add_expression(struct expression_list **list, struct expression *expr)
105 add_ptr_list((struct ptr_list **)list, expr);
108 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
110 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
113 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
115 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
118 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
120 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
123 #define PREPARE_PTR_LIST(head, ptr) \
124 do { \
125 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
126 struct ptr_list *__list##ptr = __head##ptr; \
127 int __nr##ptr = 0; \
128 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
129 else ptr = NULL
131 #define NEXT_PTR_LIST(ptr) \
132 if (ptr) { \
133 if (++__nr##ptr < __list##ptr->nr) { \
134 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
135 } else { \
136 __list##ptr = __list##ptr->next; \
137 ptr = NULL; \
138 if (__list##ptr != __head##ptr) { \
139 __nr##ptr = 0; \
140 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
145 #define RESET_PTR_LIST(ptr) do { \
146 __nr##ptr = 0; \
147 __list##ptr = __head##ptr; \
148 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
149 } while (0)
152 #define FINISH_PTR_LIST(ptr) \
153 (void)(__nr##ptr); /* Sanity-check nesting */ \
154 } while (0)
156 #define FOR_EACH_PTR(head, ptr) do { \
157 struct ptr_list *__head = (struct ptr_list *) (head); \
158 struct ptr_list *__list = __head; \
159 int __flag = ITERATE_FIRST; \
160 if (__head) { \
161 do { int __i; \
162 for (__i = 0; __i < __list->nr; __i++) { \
163 if (__i == __list->nr-1 && __list->next == __head) \
164 __flag |= ITERATE_LAST; \
165 do { \
166 ptr = (__typeof__(ptr)) (__list->list[__i]); \
167 do {
169 #define END_FOR_EACH_PTR } while (0); \
170 } while (0); \
171 __flag = 0; \
173 } while ((__list = __list->next) != __head); \
175 } while (0)
177 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
178 struct ptr_list *__head = (struct ptr_list *) (head); \
179 struct ptr_list *__list = __head; \
180 int __flag = ITERATE_FIRST; \
181 if (__head) { \
182 do { int __i; \
183 __list = __list->prev; \
184 __i = __list->nr; \
185 while (--__i >= 0) { \
186 if (__i == 0 && __list == __head) \
187 __flag |= ITERATE_LAST; \
188 do { \
189 ptr = (__typeof__(ptr)) (__list->list[__i]); \
190 do {
192 #define END_FOR_EACH_PTR_REVERSE } while (0); \
193 } while (0); \
194 __flag = 0; \
196 } while (__list != __head); \
198 } while (0)
200 #define THIS_ADDRESS(x) \
201 ((__typeof__(&(x))) (__list->list + __i))
203 #endif