-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcfg_node.c
410 lines (323 loc) · 11.6 KB
/
cfg_node.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg_node.h"
CfgNode *createCFG(AstNode *node_ast)
{
CfgNode *entrynode = newCfgNode(Entry);
CfgNode *exitnode = newCfgNode(Exit);
NodeParameterList *first, *last;
NodeParameterList *p; /*用于节点释放空间时*/
CfgNodeList *pred;
/*抽象语法树是从1开始编号的,这里把控制流图的入口和出口节点的id_of_ast的值都设为0,以示特殊*/
entrynode->node_of_ast = exitnode->node_of_ast = NULL;
/*为入口节点创建一个后继节点,路径值为true*/
entrynode->successor = (CfgNodeList *)malloc(sizeof(CfgNodeList));
entrynode->successor->path_value = true;
entrynode->successor->next = NULL;
first = (NodeParameterList *)malloc(sizeof(NodeParameterList));
first->node_succ = entrynode->successor;
first->node_cfg = entrynode;
first->next = NULL;
last = createNodeRecursively(first, node_ast);
while (last)
{
last->node_succ->node_cfg = exitnode; /*将上一个节点的后继节点赋为当前节点*/
pred = (CfgNodeList *)malloc(sizeof(CfgNodeList));
pred->path_value = last->node_succ->path_value;
pred->node_cfg = last->node_cfg; /*将此节点的前驱节点指向上一个节点*/
/*头插法插入前驱节点链表中*/
pred->next = exitnode->predecessor;
exitnode->predecessor = pred;
p = last;
last = last->next;
free(p);
}
return entrynode;
}
NodeParameterList *createNodeRecursively(NodeParameterList *succlist, AstNode *node_ast)
{
NodeParameterList *path_return;
AstNode *p;
if (!node_ast) /*node_ast为NULL*/
return succlist;
if (node_ast->nodetype_ast == Statement)
{
/*赋值语句或表达式语句*/
if (strcmp(node_ast->value.value_string, "assignment_statement") == 0
|| strcmp(node_ast->value.value_string, "expression_statement") == 0
|| strcmp(node_ast->value.value_string, "jump_statement") == 0)
path_return = assign_express_jump_case(succlist, node_ast);
/*选择语句*/
if (strcmp(node_ast->value.value_string, "selection_statement_no_else") == 0)
path_return = select_no_else_case(succlist, node_ast);
if (strcmp(node_ast->value.value_string, "selection_statement_with_else") == 0)
path_return = select_with_else_case(succlist, node_ast);
/*循环语句*/
if (strcmp(node_ast->value.value_string, "iteration_statement") == 0)
path_return = iteration_case(succlist, node_ast);
}
else
{
path_return = createNodeRecursively(succlist, node_ast->firstchild);
if (node_ast->firstchild) /*有第一个孩子*/
{
p = node_ast->firstchild->nextsibling;
while (p)
{
path_return = createNodeRecursively(path_return, p);
p = p->nextsibling;
}
}
}
return path_return;
}
NodeParameterList *assign_express_jump_case(NodeParameterList *succlist, AstNode *node_ast)
{
CfgNode *temp = newCfgNode(Assign_express_jump);
NodeParameterList *path_return;
NodeParameterList *p; /*用于节点释放空间时*/
CfgNodeList *pred;
temp->node_of_ast = node_ast;
if (strcmp(node_ast->value.value_string, "assignment_statement") == 0)
{
collectSymbol(node_ast->firstchild, &temp->def_s);
collectSymbol(node_ast->firstchild->nextsibling->nextsibling, &temp->use_s);
}
/*创建后继节点*/
temp->successor = (CfgNodeList *)malloc(sizeof(CfgNodeList));
temp->successor->path_value = true;
temp->successor->next = NULL;
while (succlist)
{
succlist->node_succ->node_cfg = temp; /*将上一个节点的后继节点赋为当前节点*/
pred = (CfgNodeList *)malloc(sizeof(CfgNodeList));
pred->path_value = succlist->node_succ->path_value;
pred->node_cfg = succlist->node_cfg; /*将此节点的前驱节点指向上一个节点*/
/*头插法插入前驱节点链表中*/
pred->next = temp->predecessor;
temp->predecessor = pred;
p = succlist;
succlist = succlist->next;
free(p);
}
path_return = (NodeParameterList *)malloc(sizeof(NodeParameterList));
path_return->node_succ = temp->successor;
path_return->node_cfg = temp;
path_return->next = NULL;
return path_return;
}
NodeParameterList *select_no_else_case(NodeParameterList *succlist, AstNode *node_ast)
{
CfgNode *ifnode = newCfgNode(Select);
NodeParameterList *to_true_path, *true_path_return, *false_path;
NodeParameterList *p; /*用于节点释放空间时*/
CfgNodeList *pred;
ifnode->node_of_ast = node_ast;
/*选择语句有两个后继节点,一个路径值为true,另一个为false*/
ifnode->successor = (CfgNodeList *)malloc(sizeof(CfgNodeList));
ifnode->successor->path_value = true;
ifnode->successor->next = (CfgNodeList *)malloc(sizeof(CfgNodeList));
ifnode->successor->next->path_value = false;
ifnode->successor->next->next = NULL;
while (succlist)
{
succlist->node_succ->node_cfg = ifnode; /*将上一个节点的后继节点赋为当前节点*/
pred = (CfgNodeList *)malloc(sizeof(CfgNodeList));
pred->path_value = succlist->node_succ->path_value;
pred->node_cfg = succlist->node_cfg; /*将此节点的前驱节点指向上一个节点*/
/*头插法插入前驱节点链表中*/
pred->next = ifnode->predecessor;
ifnode->predecessor = pred;
p = succlist;
succlist = succlist->next;
free(p);
}
to_true_path = (NodeParameterList *)malloc(sizeof(NodeParameterList));
to_true_path->node_succ = ifnode->successor;
to_true_path->node_cfg = ifnode;
to_true_path->next = NULL;
true_path_return = createNodeRecursively(to_true_path,
/*第五个孩子*/ node_ast->firstchild->nextsibling->nextsibling->nextsibling->nextsibling);
/*将if节点的false路径插入true路径的返回链表中,即得到整个if语句的返回链表*/
false_path = (NodeParameterList *)malloc(sizeof(NodeParameterList));
false_path->node_succ = ifnode->successor->next;
false_path->node_cfg = ifnode;
/*头插法*/
false_path->next = true_path_return;
true_path_return = false_path;
return true_path_return;
}
NodeParameterList *select_with_else_case(NodeParameterList *succlist, AstNode *node_ast)
{
CfgNode *ifnode = newCfgNode(Select);
NodeParameterList *to_true_path, *to_false_path, *true_path_return, *false_path_return, *q;
NodeParameterList *p; /*用于节点释放空间时*/
CfgNodeList *pred;
ifnode->node_of_ast = node_ast;
/*if节点有两个后继节点,一个路径值为true,另一个为false*/
ifnode->successor = (CfgNodeList *)malloc(sizeof(CfgNodeList));
ifnode->successor->path_value = true;
ifnode->successor->next = (CfgNodeList *)malloc(sizeof(CfgNodeList));
ifnode->successor->next->path_value = false;
ifnode->successor->next->next = NULL;
while (succlist)
{
succlist->node_succ->node_cfg = ifnode; /*将上一个节点的后继节点赋为当前节点*/
pred = (CfgNodeList *)malloc(sizeof(CfgNodeList));
pred->path_value = succlist->node_succ->path_value;
pred->node_cfg = succlist->node_cfg; /*将此节点的前驱节点指向上一个节点*/
/*头插法插入前驱节点链表中*/
pred->next = ifnode->predecessor;
ifnode->predecessor = pred;
p = succlist;
succlist = succlist->next;
free(p);
}
to_true_path = (NodeParameterList *)malloc(sizeof(NodeParameterList));
to_true_path->node_succ = ifnode->successor;
to_true_path->node_cfg = ifnode;
to_true_path->next = NULL;
to_false_path = (NodeParameterList *)malloc(sizeof(NodeParameterList));
to_false_path->node_succ = ifnode->successor->next;
to_false_path->node_cfg = ifnode;
to_false_path->next = NULL;
true_path_return = createNodeRecursively(to_true_path,
/*第五个孩子*/ node_ast->firstchild->nextsibling->nextsibling->nextsibling->nextsibling);
false_path_return= createNodeRecursively(to_false_path,
/*第七个孩子*/ node_ast->firstchild->nextsibling->nextsibling->nextsibling->nextsibling->nextsibling->nextsibling);
/*找到最后一个节点*/
for (q = true_path_return; q->next; q = q->next);
/*将false_path_return连接在其后,即得到整个if语句的返回链表*/
q->next = false_path_return;
return true_path_return;
}
NodeParameterList *iteration_case(NodeParameterList *succlist, AstNode *node_ast)
{
CfgNode *whilenode = newCfgNode(Iteration);
NodeParameterList *to_true_path, *to_false_path, *true_path_return;
NodeParameterList *p; /*用于节点释放空间时*/
CfgNodeList *pred;
whilenode->node_of_ast = node_ast;
/*while节点有两个后继节点,一个路径值为true,另一个为false*/
whilenode->successor = (CfgNodeList *)malloc(sizeof(CfgNodeList));
whilenode->successor->path_value = true;
whilenode->successor->next = (CfgNodeList *)malloc(sizeof(CfgNodeList));
whilenode->successor->next->path_value = false;
whilenode->successor->next->next = NULL;
while (succlist)
{
succlist->node_succ->node_cfg = whilenode; /*将上一个节点的后继节点赋为当前节点*/
pred = (CfgNodeList *)malloc(sizeof(CfgNodeList));
pred->path_value = succlist->node_succ->path_value;
pred->node_cfg = succlist->node_cfg; /*将此节点的前驱节点指向上一个节点*/
/*头插法插入前驱节点链表中*/
pred->next = whilenode->predecessor;
whilenode->predecessor = pred;
p = succlist;
succlist = succlist->next;
free(p);
}
to_true_path = (NodeParameterList *)malloc(sizeof(NodeParameterList));
to_true_path->node_succ = whilenode->successor;
to_true_path->node_cfg = whilenode;
to_true_path->next = NULL;
to_false_path = (NodeParameterList *)malloc(sizeof(NodeParameterList));
to_false_path->node_succ = whilenode->successor->next;
to_false_path->node_cfg = whilenode;
to_false_path->next = NULL;
true_path_return = createNodeRecursively(to_true_path,
/*第五个孩子*/ node_ast->firstchild->nextsibling->nextsibling->nextsibling->nextsibling);
/*循环结尾指向自己*/
while (true_path_return)
{
true_path_return->node_succ->node_cfg = whilenode; /*将上一个节点的后继节点赋为当前节点*/
pred = (CfgNodeList *)malloc(sizeof(CfgNodeList));
pred->path_value = true_path_return->node_succ->path_value;
pred->node_cfg = true_path_return->node_cfg; /*将此节点的前驱节点指向上一个节点*/
/*头插法插入前驱节点链表中*/
pred->next = whilenode->predecessor;
whilenode->predecessor = pred;
p = true_path_return;
true_path_return = true_path_return->next;
free(p);
}
return to_false_path;
}
void collectSymbol(AstNode *node_ast, Symbol **symbol_list)
{
Symbol *p;
AstNode *q;
if (!node_ast)
return;
if (node_ast->nodetype_ast == Identifier)
{
p = (Symbol *)malloc(sizeof(Symbol));
strcpy(p->name, node_ast->value.value_string);
/*头插法*/
p->next = *symbol_list;
*symbol_list = p;
}
/*两个if只会执行其中一个,因为Identifier型的节点没有孩子*/
if (node_ast->firstchild)
{
collectSymbol(node_ast->firstchild, symbol_list);
q = node_ast->firstchild->nextsibling;
while (q)
{
collectSymbol(q, symbol_list);
q = q->nextsibling;
}
}
}
CfgNode *newCfgNode(enum CfgNodeType type)
{
CfgNode *node = (CfgNode *)malloc(sizeof(CfgNode));
node->nodetype_cfg = type;
node->node_of_ast = NULL;
node->visited = false;
node->def_s = node->use_s = NULL;
node->predecessor = node->successor = NULL;
return node;
}
void freeCfgNode(CfgNode *node)
{
CfgNodeList *p, *q;
Symbol *m, *n;
node->visited = true;
p = node->successor;
while (p)
{
if (!p->node_cfg->visited)
freeCfgNode(p->node_cfg);
q = p;
p = p->next;
free(q); /*释放后继节点列表所占空间*/
}
/*释放前驱节点链表所占空间*/
p = node->predecessor;
while (p)
{
q = p;
p = p->next;
free(q);
}
/*释放节点定义变量链表*/
m = node->def_s;
while (m)
{
n = m;
m = m->next;
free(n);
}
/*释放节点使用变量链表*/
m = node->use_s;
while (m)
{
n = m;
m = m->next;
free(n);
}
/*退出之前释放当前节点所占空间*/
free(node);
}