1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#ifndef LIB_H
#define LIB_H
/*
* Basic helper routine descriptions for 'sparse'.
*
* Copyright (C) 2003 Transmeta Corp.
* 2003 Linus Torvalds
*
* Licensed under the Open Software License version 1.1
*/
extern unsigned int hexval(unsigned int c);
struct position {
unsigned int type:6,
stream:10,
pos:14,
newline:1,
whitespace:1;
unsigned int line;
};
struct ident;
struct token;
struct symbol;
struct symbol_list;
struct statement;
struct statement_list;
struct expression;
struct expression_list;
struct token *skip_to(struct token *, int);
struct token *expect(struct token *, int, const char *);
extern void warn(struct position, const char *, ...);
extern void error(struct position, const char *, ...);
#define __DECLARE_ALLOCATOR(type, x) \
extern type *__alloc_##x(int); \
extern void show_##x##_alloc(void); \
extern void clear_##x##_alloc(void);
#define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
DECLARE_ALLOCATOR(ident);
DECLARE_ALLOCATOR(token);
DECLARE_ALLOCATOR(symbol);
DECLARE_ALLOCATOR(expression);
DECLARE_ALLOCATOR(statement);
DECLARE_ALLOCATOR(string);
DECLARE_ALLOCATOR(scope);
__DECLARE_ALLOCATOR(void, bytes);
#define LIST_NODE_NR (29)
struct ptr_list {
int nr;
struct ptr_list *prev;
struct ptr_list *next;
void *list[LIST_NODE_NR];
};
#define ITERATE_FIRST 1
#define ITERATE_LAST 2
void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
extern void add_ptr_list(struct ptr_list **, void *);
extern int ptr_list_size(struct ptr_list *);
#define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
#define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
#define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
{
add_ptr_list((struct ptr_list **)list, sym);
}
static inline void add_statement(struct statement_list **list, struct statement *stmt)
{
add_ptr_list((struct ptr_list **)list, stmt);
}
static inline void add_expression(struct expression_list **list, struct expression *expr)
{
add_ptr_list((struct ptr_list **)list, expr);
}
static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
{
iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
}
static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
{
iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
}
static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
{
iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
}
#define PREPARE_PTR_LIST(head, ptr) \
do { \
struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
struct ptr_list *__list##ptr = __head##ptr; \
int __nr##ptr = 0; \
if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
else ptr = NULL
#define NEXT_PTR_LIST(ptr) \
if (ptr) { \
if (++__nr##ptr < __list##ptr->nr) { \
ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
} else { \
__list##ptr = __list##ptr->next; \
ptr = NULL; \
if (__list##ptr != __head##ptr) { \
__nr##ptr = 0; \
ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
} \
} \
}
#define RESET_PTR_LIST(ptr) do { \
__nr##ptr = 0; \
__list##ptr = __head##ptr; \
if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
} while (0)
#define FINISH_PTR_LIST(ptr) \
(void)(__nr##ptr); /* Sanity-check nesting */ \
} while (0)
#define FOR_EACH_PTR(head, ptr) do { \
struct ptr_list *__head = (struct ptr_list *) (head); \
struct ptr_list *__list = __head; \
int __flag = ITERATE_FIRST; \
if (__head) { \
do { int __i; \
for (__i = 0; __i < __list->nr; __i++) { \
if (__i == __list->nr-1 && __list->next == __head) \
__flag |= ITERATE_LAST; \
do { \
ptr = (__typeof__(ptr)) (__list->list[__i]); \
do {
#define END_FOR_EACH_PTR } while (0); \
} while (0); \
__flag = 0; \
} \
} while ((__list = __list->next) != __head); \
} \
} while (0)
#define FOR_EACH_PTR_REVERSE(head, ptr) do { \
struct ptr_list *__head = (struct ptr_list *) (head); \
struct ptr_list *__list = __head; \
int __flag = ITERATE_FIRST; \
if (__head) { \
do { int __i; \
__list = __list->prev; \
__i = __list->nr; \
while (--__i >= 0) { \
if (__i == 0 && __list == __head) \
__flag |= ITERATE_LAST; \
do { \
ptr = (__typeof__(ptr)) (__list->list[__i]); \
do {
#define END_FOR_EACH_PTR_REVERSE } while (0); \
} while (0); \
__flag = 0; \
} \
} while (__list != __head); \
} \
} while (0)
#define THIS_ADDRESS(x) \
((__typeof__(&(x))) (__list->list + __i))
#endif
|