This repository has been archived by the owner on Dec 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
support_iter.c
364 lines (339 loc) · 13.6 KB
/
support_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
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.
NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms. If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.
THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.
IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
// support_iter.c
//
// Routines to iterate over the expr/stmt graph
//
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "slglobals.h"
/*
* ApplyToNodes() - Walk an expression tree and apply "pre" and "post" to
* each node. pre is applied in prefix order, and post in postfix
*
*/
expr *ApplyToNodes(expr *(*pre)(expr *, void *, int),
expr *(*post)(expr *, void *, int),
expr *fExpr, void *arg1, int arg2)
{
if (fExpr) {
if (pre) fExpr = pre(fExpr, arg1, arg2);
switch (fExpr->common.kind) {
case DECL_N:
case SYMB_N:
case CONST_N:
break;
case UNARY_N:
fExpr->un.arg = ApplyToNodes(pre, post, fExpr->un.arg, arg1, arg2);
break;
case BINARY_N:
fExpr->bin.left = ApplyToNodes(pre, post, fExpr->bin.left, arg1, arg2);
fExpr->bin.right = ApplyToNodes(pre, post, fExpr->bin.right, arg1, arg2);
break;
case TRINARY_N:
fExpr->tri.arg1 = ApplyToNodes(pre, post, fExpr->tri.arg1, arg1, arg2);
fExpr->tri.arg2 = ApplyToNodes(pre, post, fExpr->tri.arg2, arg1, arg2);
fExpr->tri.arg3 = ApplyToNodes(pre, post, fExpr->tri.arg3, arg1, arg2);
break;
default:
assert(!"bad kind to ApplyToNodes()");
break;
}
if (post) fExpr = post(fExpr, arg1, arg2);
}
return fExpr;
} // ApplyToNodes
/*
* ApplyToExpressions() - Walk a source tree and apply "fun" to each node in each
* expression in prefix order.
*/
void ApplyToExpressions(expr *(*pre)(expr *, void *, int),
expr *(*post)(expr *, void *, int),
stmt *fStmt, void *arg1, int arg2)
{
while (fStmt) {
Cg->lastSourceLoc = fStmt->commonst.loc;
switch (fStmt->exprst.kind) {
case EXPR_STMT:
fStmt->exprst.exp = ApplyToNodes(pre, post, fStmt->exprst.exp, arg1, arg2);
break;
case IF_STMT:
fStmt->ifst.cond = ApplyToNodes(pre, post, fStmt->ifst.cond, arg1, arg2);
ApplyToExpressions(pre, post, fStmt->ifst.thenstmt, arg1, arg2);
ApplyToExpressions(pre, post, fStmt->ifst.elsestmt, arg1, arg2);
break;
case WHILE_STMT:
case DO_STMT:
fStmt->whilest.cond = ApplyToNodes(pre, post, fStmt->whilest.cond, arg1, arg2);
ApplyToExpressions(pre, post, fStmt->whilest.body, arg1, arg2);
break;
case FOR_STMT:
ApplyToExpressions(pre, post, fStmt->forst.init, arg1, arg2);
fStmt->forst.cond = ApplyToNodes(pre, post, fStmt->forst.cond, arg1, arg2);
ApplyToExpressions(pre, post, fStmt->forst.body, arg1, arg2);
ApplyToExpressions(pre, post, fStmt->forst.step, arg1, arg2);
break;
case BLOCK_STMT:
ApplyToExpressions(pre, post, fStmt->blockst.body, arg1, arg2);
break;
case RETURN_STMT:
fStmt->returnst.exp = ApplyToNodes(pre, post, fStmt->returnst.exp, arg1, arg2);
break;
case DISCARD_STMT:
fStmt->discardst.cond = ApplyToNodes(pre, post, fStmt->discardst.cond, arg1, arg2);
break;
case COMMENT_STMT:
break;
default:
assert(0);
break;
}
fStmt = fStmt->exprst.next;
}
} // ApplyToExpressions
/*
* ApplyToExpressionsLocal() - Apply a function to each node in the expressions contained in
* a single statement.
*/
void ApplyToExpressionsLocal(expr *(*pre)(expr *, void *, int),
expr *(*post)(expr *, void *, int),
stmt *fStmt, void *arg1, int arg2)
{
if (fStmt) {
Cg->lastSourceLoc = fStmt->commonst.loc;
switch (fStmt->exprst.kind) {
case EXPR_STMT:
fStmt->exprst.exp = ApplyToNodes(pre, post, fStmt->exprst.exp, arg1, arg2);
break;
case IF_STMT:
fStmt->ifst.cond = ApplyToNodes(pre, post, fStmt->ifst.cond, arg1, arg2);
break;
case WHILE_STMT:
case DO_STMT:
fStmt->whilest.cond = ApplyToNodes(pre, post, fStmt->whilest.cond, arg1, arg2);
break;
case FOR_STMT:
fStmt->forst.cond = ApplyToNodes(pre, post, fStmt->forst.cond, arg1, arg2);
break;
case BLOCK_STMT:
break;
case RETURN_STMT:
fStmt->returnst.exp = ApplyToNodes(pre, post, fStmt->returnst.exp, arg1, arg2);
break;
case DISCARD_STMT:
fStmt->discardst.cond = ApplyToNodes(pre, post, fStmt->discardst.cond, arg1, arg2);
break;
case COMMENT_STMT:
break;
default:
assert(0);
break;
}
//fStmt = fStmt->exprst.next;
}
} // ApplyToExpressionsLocal
/*
* ApplyToTopExpressions() - Walk a source tree and apply a function to each expression.
*
*/
void ApplyToTopExpressions(expr *(*fun)(expr *, void *, int), stmt *fStmt, void *arg1, int arg2)
{
while (fStmt) {
Cg->lastSourceLoc = fStmt->commonst.loc;
switch (fStmt->exprst.kind) {
case EXPR_STMT:
fStmt->exprst.exp = fun(fStmt->exprst.exp, arg1, arg2);
break;
case IF_STMT:
fStmt->ifst.cond = fun(fStmt->ifst.cond, arg1, arg2);
ApplyToTopExpressions(fun, fStmt->ifst.thenstmt, arg1, arg2);
ApplyToTopExpressions(fun, fStmt->ifst.elsestmt, arg1, arg2);
break;
case WHILE_STMT:
case DO_STMT:
fStmt->whilest.cond = fun(fStmt->whilest.cond, arg1, arg2);
ApplyToTopExpressions(fun, fStmt->whilest.body, arg1, arg2);
break;
case FOR_STMT:
ApplyToTopExpressions(fun, fStmt->forst.init, arg1, arg2);
fStmt->forst.cond = fun(fStmt->forst.cond, arg1, arg2);
ApplyToTopExpressions(fun, fStmt->forst.body, arg1, arg2);
ApplyToTopExpressions(fun, fStmt->forst.step, arg1, arg2);
break;
case BLOCK_STMT:
ApplyToTopExpressions(fun, fStmt->blockst.body, arg1, arg2);
break;
case RETURN_STMT:
fStmt->returnst.exp = fun(fStmt->returnst.exp, arg1, arg2);
break;
case DISCARD_STMT:
fStmt->discardst.cond = fun(fStmt->discardst.cond, arg1, arg2);
break;
case COMMENT_STMT:
break;
default:
assert(0);
break;
}
fStmt = fStmt->exprst.next;
}
} // ApplyToTopExpressions
/*
* ApplyToStatements() - Walk a source tree and apply a transformations to
* each statememt
*/
stmt *ApplyToStatements(stmt *(*pre)(stmt *, void *, int),
stmt *(*post)(stmt *, void *, int),
stmt *fStmt, void *arg1, int arg2)
{
stmt *head = NULL, *last = NULL, *lStmt, *next, *rest = fStmt;
while (fStmt) {
// Transform each statement into a possible NULL list of statements:
// Prepend any statements returned to the list to be processed, and
// remember what the next one to be done is (rest), so we don't
// rerun pre on any of the returned statements directly.
if (pre && rest == fStmt) {
Cg->lastSourceLoc = fStmt->commonst.loc;
rest = fStmt->commonst.next;
fStmt->commonst.next = NULL;
lStmt = pre(fStmt, arg1, arg2);
if (lStmt) {
fStmt = lStmt;
while (lStmt->commonst.next && lStmt->commentst.next != rest) {
lStmt = lStmt->commonst.next;
}
lStmt->commonst.next = rest;
} else {
// Nothing returned - go to next statement:
fStmt = rest;
continue;
}
}
// Now apply transformation to substatements:
switch (fStmt->exprst.kind) {
case EXPR_STMT:
break;
case IF_STMT:
fStmt->ifst.thenstmt = ApplyToStatements(pre, post, fStmt->ifst.thenstmt, arg1, arg2);
fStmt->ifst.elsestmt = ApplyToStatements(pre, post, fStmt->ifst.elsestmt, arg1, arg2);
break;
case WHILE_STMT:
case DO_STMT:
fStmt->whilest.body = ApplyToStatements(pre, post, fStmt->whilest.body, arg1, arg2);
break;
case FOR_STMT:
fStmt->forst.init = ApplyToStatements(pre, post, fStmt->forst.init, arg1, arg2);
fStmt->forst.body = ApplyToStatements(pre, post, fStmt->forst.body, arg1, arg2);
fStmt->forst.step = ApplyToStatements(pre, post, fStmt->forst.step, arg1, arg2);
break;
case BLOCK_STMT:
fStmt->blockst.body = ApplyToStatements(pre, post, fStmt->blockst.body, arg1, arg2);
break;
case RETURN_STMT:
case DISCARD_STMT:
case COMMENT_STMT:
break;
default:
assert(0);
break;
}
// Append any statements returned by "post" to the end of the list:
next = fStmt->commonst.next;
if (post) {
Cg->lastSourceLoc = fStmt->commonst.loc;
lStmt = post(fStmt, arg1, arg2);
} else {
lStmt = fStmt;
}
if (lStmt) {
if (head) {
last->commonst.next = lStmt;
} else {
head = lStmt;
}
last = lStmt;
while (last->commonst.next && last->commentst.next != next) {
last = last->commonst.next;
}
last->commonst.next = NULL;
}
fStmt = next;
}
return head;
} // ApplyToStatements
/*
* PostApplyToChildStatements() - Apply a postfix order transformation to each child
* statememt of this statement.
*/
void PostApplyToChildStatements(stmt *(*fun)(stmt *, void *, int), stmt *fStmt, void *arg1, int arg2)
{
if (fStmt) {
// Apply a transformation to each nested statement, but not the top level statements:
Cg->lastSourceLoc = fStmt->commonst.loc;
switch (fStmt->exprst.kind) {
case EXPR_STMT:
break;
case IF_STMT:
fStmt->ifst.thenstmt = PostApplyToStatements(fun, fStmt->ifst.thenstmt, arg1, arg2);
fStmt->ifst.elsestmt = PostApplyToStatements(fun, fStmt->ifst.elsestmt, arg1, arg2);
break;
case WHILE_STMT:
case DO_STMT:
fStmt->whilest.body = PostApplyToStatements(fun, fStmt->whilest.body, arg1, arg2);
break;
case FOR_STMT:
fStmt->forst.init = PostApplyToStatements(fun, fStmt->forst.init, arg1, arg2);
fStmt->forst.body = PostApplyToStatements(fun, fStmt->forst.body, arg1, arg2);
fStmt->forst.step = PostApplyToStatements(fun, fStmt->forst.step, arg1, arg2);
break;
case BLOCK_STMT:
fStmt->blockst.body = PostApplyToStatements(fun, fStmt->blockst.body, arg1, arg2);
break;
case RETURN_STMT:
case DISCARD_STMT:
case COMMENT_STMT:
break;
default:
assert(0);
break;
}
}
} // PostApplyToChildStatements