forked from chiphackers/covered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_iter.c
executable file
·365 lines (272 loc) · 9.95 KB
/
func_iter.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Copyright (c) 2006-2010 Trevor Williams
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program;
if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*!
\file func_iter.c
\author Trevor Williams ([email protected])
\date 4/2/2007
*/
#include <stdlib.h>
#include <assert.h>
#include "defines.h"
#include "expr.h"
#include "func_iter.h"
#include "func_unit.h"
#include "util.h"
/*!
Displays the given function iterator to standard output.
*/
void func_iter_display(
func_iter* fi /*!< Pointer to functional unit iterator to display */
) { PROFILE(FUNC_ITER_DISPLAY);
int i; /* Loop iterator */
printf( "Functional unit iterator (scopes: %u, sl_num: %u):\n", fi->scopes, fi->sl_num );
if( fi->sls != NULL ) {
for( i=0; i<fi->sl_num; i++ ) {
if( fi->sls[i] != NULL ) {
stmt_link_display( fi->sls[i] );
}
}
}
if( fi->sigs != NULL ) {
for( i=0; i<fi->sig_num; i++ ) {
if( fi->sigs[i] != NULL ) {
sig_link_display( fi->sigs[i], fi->sig_size[i] );
}
}
}
PROFILE_END;
}
/*!
Performs a bubble sort of the first element such that the first line is in location 0 of the sis array.
*/
static void func_iter_sort(
func_iter* fi /*!< Pointer to functional unit iterator to sort */
) { PROFILE(FUNC_ITER_SORT);
stmt_link* tmp; /* Temporary statement link */
int i; /* Loop iterator */
assert( fi != NULL );
assert( fi->sl_num > 0 );
tmp = fi->sls[0];
/*
If the statement iterator at the top of the list is NULL, shift all valid statement iterators
towards the top and store this statement iterator after them
*/
if( tmp == NULL ) {
for( i=0; i<(fi->sl_num - 1); i++ ) {
fi->sls[i] = fi->sls[i+1];
}
fi->sls[i] = tmp;
(fi->sl_num)--;
/* Otherwise, re-sort them based on line number and then column number (if there is a tie). */
} else {
i = 0;
while( (i < (fi->sl_num - 1)) &&
((tmp->stmt->exp->ppfline > fi->sls[i+1]->stmt->exp->ppfline) ||
((tmp->stmt->exp->ppfline == fi->sls[i+1]->stmt->exp->ppfline) &&
(tmp->stmt->exp->col.part.last > fi->sls[i+1]->stmt->exp->col.part.last))) ) {
fi->sls[i] = fi->sls[i+1];
i++;
}
fi->sls[i] = tmp;
}
PROFILE_END;
}
/*!
\return Returns the number of statement iterators found in all of the unnamed functional units
within a named functional unit.
*/
static int func_iter_count_scopes(
func_unit* funit, /*!< Pointer to current functional unit being examined */
bool inc_all /*!< If set to TRUE, counts all scopes (even named scopes) */
) { PROFILE(FUNC_ITER_COUNT_STMT_ITERS);
int count = 1; /* Number of statement iterators within this functional unit */
funit_link* child; /* Pointer to child functional unit */
func_unit* parent; /* Parent module of this functional unit */
assert( funit != NULL );
/* Get parent module */
parent = funit_get_curr_module( funit );
/* Iterate through children functional units, counting all of the unnamed scopes */
child = parent->tf_head;
while( child != NULL ) {
if( (inc_all || funit_is_unnamed( child->funit )) && (child->funit->parent == funit) ) {
count += func_iter_count_scopes( child->funit, inc_all );
}
child = child->next;
}
PROFILE_END;
return( count );
}
/*!
Recursively iterates through functional units, adding their statement iterators to the func_iter structure's array.
*/
static void func_iter_add_stmt_links(
func_iter* fi, /*!< Pointer to functional unit iterator to populate */
func_unit* funit, /*!< Pointer to current functional unit */
bool inc_all /*!< If TRUE, uses all statements in the functional unit (even in named scopes) */
) { PROFILE(FUNC_ITER_ADD_STMT_ITERS);
funit_link* child; /* Pointer to child functional unit */
func_unit* parent; /* Pointer to parent module of this functional unit */
int i; /* Loop iterator */
/* First, shift all current statement iterators down by one */
for( i=(fi->scopes - 2); i>=0; i-- ) {
fi->sls[i+1] = fi->sls[i];
}
/* Set the sls pointer to the head of the functional unit statement list */
fi->sls[0] = funit->stmt_head;
/* Increment the si_num */
(fi->sl_num)++;
/* Now sort the new statement iterator */
func_iter_sort( fi );
/* Get the parent module */
parent = funit_get_curr_module( funit );
/* Now traverse down all of the child functional units doing the same */
child = parent->tf_head;
while( child != NULL ) {
if( (inc_all || funit_is_unnamed( child->funit )) && (child->funit->parent == funit) ) {
func_iter_add_stmt_links( fi, child->funit, inc_all );
}
child = child->next;
}
PROFILE_END;
}
/*!
Recursively iterates through the functional units, adding their signal link pointers to the func_iter structure's array.
*/
static void func_iter_add_sig_links(
func_iter* fi, /*!< Pointer to functional unit iterator to populate */
func_unit* funit, /*!< Pointer to current functional unit */
bool inc_all /*!< If set to TRUE, includes all scopes */
) { PROFILE(FUNC_ITER_ADD_SIG_LINKS);
funit_link* child; /* Pointer to child functional unit */
func_unit* parent; /* Pointer to parent module of this functional unit */
/* Add the pointer */
fi->sigs[fi->sig_num] = funit->sigs;
fi->sig_size[fi->sig_num] = funit->sig_size;
fi->sig_num++;
/* Get the parent module */
parent = funit_get_curr_module( funit );
/* Now traverse down all of the child functional units doing the same */
child = parent->tf_head;
while( child != NULL ) {
if( (inc_all || funit_is_unnamed( child->funit )) && (child->funit->parent == funit) ) {
func_iter_add_sig_links( fi, child->funit, inc_all );
}
child = child->next;
}
PROFILE_END;
}
/*!
Initializes the contents of the functional unit iterator structure. This should be called before
the functional unit iterator structure is populated with either statement iterator or signal
information.
*/
void func_iter_init(
func_iter* fi, /*!< Pointer to functional unit iterator to initializes */
func_unit* funit, /*!< Pointer to main functional unit to create iterator for (must be named) */
bool stmts, /*!< Set to TRUE if we want statements to be included in the iterator */
bool sigs, /*!< Set to TRUE if we want signals to be included in the iterator */
bool inc_all /*!< Set to TRUE to use all statements/signals including those in named scopes */
) { PROFILE(FUNC_ITER_INIT);
assert( fi != NULL );
assert( funit != NULL );
/* Count the number of scopes that are within the functional unit iterator */
fi->scopes = func_iter_count_scopes( funit, inc_all );
fi->sls = NULL;
fi->sigs = NULL;
fi->sig_size = NULL;
fi->sig_num = 0;
/* Add statement iterators */
if( stmts ) {
fi->sls = (stmt_link**)malloc_safe( sizeof( stmt_link* ) * fi->scopes );
fi->sl_num = 0;
func_iter_add_stmt_links( fi, funit, inc_all );
}
/* Add signal lists */
if( sigs ) {
fi->sigs = (vsignal***)malloc_safe( sizeof( vsignal** ) * fi->scopes );
fi->sig_size = (int*)malloc_safe( sizeof( int ) * fi->scopes );
fi->sig_num = 0;
func_iter_add_sig_links( fi, funit, inc_all );
fi->sig_num = 0;
fi->curr_sig = 0;
}
PROFILE_END;
}
/*!
\return Returns pointer to next statement in line order (or NULL if there are no more
statements in the given functional unit)
*/
statement* func_iter_get_next_statement(
func_iter* fi /*!< Pointer to functional unit iterator to use */
) { PROFILE(FUNC_ITER_GET_NEXT_STATEMENT);
statement* stmt; /* Pointer to next statement in line order */
assert( fi != NULL );
if( fi->sl_num == 0 ) {
stmt = NULL;
} else {
assert( fi->sls[0] != NULL );
/* Get the statement at the head of the sorted list */
stmt = fi->sls[0]->stmt;
/* Go to the next statement in the current statement list */
fi->sls[0] = fi->sls[0]->next;
/* Resort */
func_iter_sort( fi );
}
PROFILE_END;
return( stmt );
}
/*!
\return Returns pointer to next signal in order (or NULL if there are no more signals in the
given functional unit)
*/
vsignal* func_iter_get_next_signal(
func_iter* fi /*!< Pointer to functional unit iterator to use */
) { PROFILE(FUNC_ITER_GET_NEXT_SIGNAL);
vsignal* sig;
assert( fi != NULL );
if( fi->curr_sig < fi->sig_size[fi->sig_num] ) {
sig = fi->sigs[fi->sig_num][fi->curr_sig];
fi->curr_sig++;
} else {
do {
fi->sig_num++;
} while( (fi->sig_num < fi->scopes) && (fi->sigs[fi->sig_num] == NULL) );
if( fi->sig_num < fi->scopes ) {
sig = fi->sigs[fi->sig_num][0];
fi->curr_sig = 1;
} else {
sig = NULL;
fi->curr_sig = 0;
}
}
PROFILE_END;
return( sig );
}
/*!
Deallocates all allocated memory for the given functional unit iterator.
*/
void func_iter_dealloc(
func_iter* fi /*!< Pointer to functional unit iterator to deallocate */
) { PROFILE(FUNC_ITER_DEALLOC);
int i; /* Loop iterator */
if( fi != NULL ) {
/* Deallocate statement iterators */
free_safe( fi->sls, (sizeof( stmt_link* ) * fi->scopes) );
fi->sls = NULL;
/* Deallocate signal arrays */
free_safe( fi->sigs, (sizeof( vsignal** ) * fi->scopes) );
free_safe( fi->sig_size, (sizeof( unsigned int ) * fi->scopes) );
fi->sigs = NULL;
fi->sig_size = NULL;
}
PROFILE_END;
}