4 * Basic helper routine descriptions for 'sparse'.
6 * Copyright (C) 2003 Transmeta Corp.
10 * Licensed under the Open Software License version 1.1
13 extern unsigned int hexval(unsigned int c
);
30 struct statement_list
;
32 struct expression_list
;
34 struct basic_block_list
;
37 struct instruction_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)
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
;
87 enum iterator_br_state
{
94 struct terminator_iterator
{
95 struct instruction
*terminator
;
97 struct list_iterator multijmp
;
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
;
133 extern int Wdefault_bitfield_sign
;
135 extern void create_builtin_stream(void);
137 static inline int symbol_list_size(struct symbol_list
* list
)
139 return ptr_list_size((struct ptr_list
*)(list
));
142 static inline int statement_list_size(struct statement_list
* list
)
144 return ptr_list_size((struct ptr_list
*)(list
));
147 static inline int expression_list_size(struct expression_list
* list
)
149 return ptr_list_size((struct ptr_list
*)(list
));
152 static inline int instruction_list_size(struct instruction_list
* list
)
154 return ptr_list_size((struct ptr_list
*)(list
));
157 static inline int phi_list_size(struct phi_list
* list
)
159 return ptr_list_size((struct ptr_list
*)(list
));
162 static inline int bb_list_size(struct basic_block_list
* list
)
164 return ptr_list_size((struct ptr_list
*)(list
));
167 static inline struct basic_block
* next_basic_block(struct list_iterator
*iterator
)
169 return next_iterator(iterator
);
172 static inline struct multijmp
* next_multijmp(struct list_iterator
*iterator
)
174 return next_iterator(iterator
);
177 static inline void free_instruction_list(struct instruction_list
**head
)
179 free_ptr_list((struct ptr_list
**)head
);
182 static inline void init_multijmp_iterator(struct multijmp_list
**head
, struct list_iterator
*iterator
, int flags
)
184 init_iterator((struct ptr_list
**)head
, iterator
, flags
);
187 static inline void init_bb_iterator(struct basic_block_list
**head
, struct list_iterator
*iterator
, int flags
)
189 init_iterator((struct ptr_list
**)head
, iterator
, flags
);
192 static inline struct instruction
* delete_last_instruction(struct instruction_list
**head
)
194 return delete_ptr_list_last((struct ptr_list
**)head
);
197 static inline struct basic_block
* delete_last_basic_block(struct basic_block_list
**head
)
199 return delete_ptr_list_last((struct ptr_list
**)head
);
202 static inline void *first_ptr_list(struct ptr_list
*list
)
206 return list
->list
[0];
209 static inline void *last_ptr_list(struct ptr_list
*list
)
215 return list
->list
[list
->nr
-1];
218 static inline void * current_iterator(struct list_iterator
*iterator
)
220 struct ptr_list
*list
= iterator
->active
;
221 return list
? list
->list
[iterator
->index
] : NULL
;
224 static inline struct basic_block
*first_basic_block(struct basic_block_list
*head
)
226 return last_ptr_list((struct ptr_list
*)head
);
228 static inline struct instruction
*last_instruction(struct instruction_list
*head
)
230 return last_ptr_list((struct ptr_list
*)head
);
233 static inline struct instruction
*first_instruction(struct instruction_list
*head
)
235 return first_ptr_list((struct ptr_list
*)head
);
238 static inline struct phi
*first_phi(struct phi_list
*head
)
240 return first_ptr_list((struct ptr_list
*)head
);
243 static inline int replace_basic_block_list(struct basic_block_list
**head
, struct basic_block
*from
, struct basic_block
*to
)
245 return replace_ptr_list((struct ptr_list
**)head
, (void*)from
, (void*)to
);
248 static inline void concat_symbol_list(struct symbol_list
*from
, struct symbol_list
**to
)
250 concat_ptr_list((struct ptr_list
*)from
, (struct ptr_list
**)to
);
253 static inline void concat_basic_block_list(struct basic_block_list
*from
, struct basic_block_list
**to
)
255 concat_ptr_list((struct ptr_list
*)from
, (struct ptr_list
**)to
);
258 static inline void concat_instruction_list(struct instruction_list
*from
, struct instruction_list
**to
)
260 concat_ptr_list((struct ptr_list
*)from
, (struct ptr_list
**)to
);
263 static inline void add_symbol(struct symbol_list
**list
, struct symbol
*sym
)
265 add_ptr_list((struct ptr_list
**)list
, sym
);
268 static inline void add_statement(struct statement_list
**list
, struct statement
*stmt
)
270 add_ptr_list((struct ptr_list
**)list
, stmt
);
273 static inline void add_expression(struct expression_list
**list
, struct expression
*expr
)
275 add_ptr_list((struct ptr_list
**)list
, expr
);
278 static inline void symbol_iterate(struct symbol_list
*list
, void (*callback
)(struct symbol
*, void *, int), void *data
)
280 iterate((struct ptr_list
*)list
, (void (*)(void *, void *, int))callback
, data
);
283 static inline void statement_iterate(struct statement_list
*list
, void (*callback
)(struct statement
*, void *, int), void *data
)
285 iterate((struct ptr_list
*)list
, (void (*)(void *, void *, int))callback
, data
);
288 static inline void expression_iterate(struct expression_list
*list
, void (*callback
)(struct expression
*, void *, int), void *data
)
290 iterate((struct ptr_list
*)list
, (void (*)(void *, void *, int))callback
, data
);
293 #define PREPARE_PTR_LIST(head, ptr) \
295 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
296 struct ptr_list *__list##ptr = __head##ptr; \
298 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
301 #define NEXT_PTR_LIST(ptr) \
303 if (++__nr##ptr < __list##ptr->nr) { \
304 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
306 __list##ptr = __list##ptr->next; \
308 if (__list##ptr != __head##ptr) { \
310 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
315 #define RESET_PTR_LIST(ptr) do { \
317 __list##ptr = __head##ptr; \
318 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
322 #define FINISH_PTR_LIST(ptr) \
323 (void)(__nr##ptr); /* Sanity-check nesting */ \
326 #define FOR_EACH_PTR(head, ptr) do { \
327 struct ptr_list *__head = (struct ptr_list *) (head); \
328 struct ptr_list *__list = __head; \
329 int __flag = ITERATE_FIRST; \
332 for (__i = 0; __i < __list->nr; __i++) { \
333 if (__i == __list->nr-1 && __list->next == __head) \
334 __flag |= ITERATE_LAST; \
336 ptr = (__typeof__(ptr)) (__list->list[__i]); \
339 #define END_FOR_EACH_PTR } while (0); \
343 } while ((__list = __list->next) != __head); \
347 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
348 struct ptr_list *__head = (struct ptr_list *) (head); \
349 struct ptr_list *__list = __head; \
350 int __flag = ITERATE_FIRST; \
353 __list = __list->prev; \
355 while (--__i >= 0) { \
356 if (__i == 0 && __list == __head) \
357 __flag |= ITERATE_LAST; \
359 ptr = (__typeof__(ptr)) (__list->list[__i]); \
362 #define END_FOR_EACH_PTR_REVERSE } while (0); \
366 } while (__list != __head); \
370 #define THIS_ADDRESS(x) \
371 ((__typeof__(&(x))) (__list->list + __i))