-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-match.c
612 lines (493 loc) · 14.4 KB
/
tree-match.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* Library for tree matching.
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
Contributed by Nic Volanschi <[email protected]>
This file is part of GCC.
GCC 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, or (at your option)
any later version.
GCC 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 GCC; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include <stdio.h>
#include "tree-match.h"
/* The tree matching user interface is described in tree-pattern.h. */
hole global_holes[GLOBAL_MAX];
hole local_holes[LOCAL_MAX];
/* Check whether a variable is a temporary introduced by the compiler. */
static bool is_tmp_var(struct statement *var)
{
return false;
#if 0
const char *name;
if (TREE_CODE(var) != VAR_DECL)
return false;
/* Artificial decls like return values have no name, so don't get hung. */
if (!DECL_NAME(var))
return true;
name = IDENTIFIER_POINTER(DECL_NAME(var));
return !strncmp(name, "D.", 2) || strchr(name, '.');
#endif
}
/* If t is a cast expression, return the value without the cast. */
static tree substitute_cast_expr(struct statement *t)
{
#if 0
if (TREE_CODE(t) == CONVERT_EXPR || TREE_CODE(t) == NOP_EXPR)
return TREE_OPERAND(t, 0);
#endif
return NULL;
}
/* If t is a temporary and you can find its def from current node
upwards in the same block, (including the current node or not),
return its value; otherwise return NULL. INCLUDING_CRT is set to
true on recursive calls. */
static struct statement *substitute_tmp_var(struct statement *var, cfg_node ctx_node, bool including_crt)
{
return NULL;
#if 0
struct statement *val;
if (!is_tmp_var(var))
return NULL;
/* We are at the beginning of the block, or the context is unknown. */
if (!ctx_node)
return NULL;
if ((val = NULL, tree_scanf(cfg_node_stmt(ctx_node), "%t = %t",
NULL, &var, &val))
|| (val = NULL,
tree_scanf(cfg_node_stmt(ctx_node), "%t = (%_)%t",
NULL, &var, &val))) {
if (including_crt) {
return val;
} else
/* We are on a def, but exclude it => don't go up. */
return NULL;
} else
/* We are not on a def => go up and try again. */
return substitute_tmp_var(var, ctx_node->prev, true);
#endif
}
static struct tree_chunk *alloc_tree_chunk(void)
{
tree_chunk *res = __alloc_tree_chunk(0);
memset(res, 0, sizeof(*res));
return res;
}
static void free_tree_chunk(struct tree_chunk *tc)
{
__free_tree_chunk(tc);
}
static int tree_chunk_list_size(struct tree_chunk_list *list)
{
return ptr_list_size(list);
}
/* FIXME: These should actually DO something. */
static struct tree_chunk_list *lazy_dump_generic_node(struct statement *stmt)
{
struct tree_chunk_list *res = NULL;
struct tree_chunk *tc = alloc_tree_chunk();
add_ptr_list(res, tc);
return res;
}
static void pp_free_list(struct tree_chunk_list *list)
{
struct tree_chunk *tc;
FOR_EACH_PTR(list, tc) {
free_tree_chunk(tc);
} END_FOR_EACH_PTR(tc);
}
static bool tree_equal_mod_tmps(struct statement *, struct statement *, cfg_node, cfg_node);
/* Worker function for tree_equal_mod_tmps. */
static bool tree_equal(struct statement *t1, struct statement *t2, cfg_node ctx_node1, cfg_node ctx_node2)
{
struct tree_chunk_list *chunks1 = NULL;
struct tree_chunk_list *chunks2 = NULL;
struct tree_chunk chunk1, chunk2;
int len1, len2, i;
bool res = false;
if ((!t1 || !t2))
return (t1 == t2);
if (t1 == t2)
return true;
chunks1 = lazy_dump_generic_node(t1);
chunks2 = lazy_dump_generic_node(t2);
len1 = tree_chunk_list_size(chunks1);
len2 = tree_chunk_list_size(chunks2);
if (len1 != len2)
goto mismatch;
/* XXX: This loop will iterate through tree chunks and
* compare them. I think that with sparse, we could just
* write a function to compare the trees and be done with it.
*/
for (i = 0; VEC_iterate(tree_chunk, chunks1, i, chunk1); i++) {
chunk2 = VEC_index(tree_chunk, chunks2, i);
if (((chunk1->t || chunk2->t)
&& (!(chunk1->t && chunk2->t)
|| !tree_equal_mod_tmps(chunk1->t, chunk2->t,
ctx_node1, ctx_node2)))
|| ((chunk1->s || chunk2->s)
&& (!(chunk1->s && chunk2->s)
|| strcmp(chunk1->s, chunk2->s)))
|| (chunk1->c != chunk2->c))
goto mismatch;
}
res = true;
mismatch:
pp_free_list(chunks1);
pp_free_list(chunks2);
return res;
}
/* Check if two trees are equal, modulo casts and substitutions of
tmp vars with their values. */
static bool
tree_equal_mod_tmps(struct statement *t1, struct statement *t2, cfg_node ctx_node1, cfg_node ctx_node2)
{
//struct statement *val;
if ((!t1 || !t2))
return (t1 == t2);
return tree_equal(t1, t2, ctx_node1, ctx_node2);
#if 0
return (tree_equal(t1, t2, ctx_node1, ctx_node2)
|| ((val = substitute_tmp_var(t1, ctx_node1, false)) != NULL
&& tree_equal_mod_tmps(val, t2, ctx_node1, ctx_node2))
|| ((val = substitute_tmp_var(t2, ctx_node2, false)) != NULL
&& tree_equal_mod_tmps(t1, val, ctx_node1, ctx_node2))
|| ((val = substitute_cast_expr(t1)) != NULL
&& tree_equal_mod_tmps(val, t2, ctx_node1, ctx_node2))
|| ((val = substitute_cast_expr(t2)) != NULL
&& tree_equal_mod_tmps(t1, val, ctx_node1, ctx_node2)));
#endif
}
static char tree_1st_char(struct statement *);
/* Get the first character of (the printed form of) a tree chunk. */
static char chunk_1st_char(tree_chunk chunk)
{
if (chunk->t)
return tree_1st_char(chunk->t);
else if (chunk->s)
return *chunk->s;
else
return chunk->c;
}
/* Search the first chunk of a lazy list not consisting of whitespace. */
static tree_chunk
chunks_lookahead(VEC(tree_chunk, heap) * chunks, unsigned int i)
{
tree_chunk chunk;
for (; VEC_iterate(tree_chunk, chunks, i, chunk); i++)
if (chunk->c == 0 || chunk->c != ' ')
break;
return chunk;
}
/* Get the first character of (the printed form of) a tree. */
static char tree_1st_char(struct statement *t)
{
VEC(tree_chunk, heap) * chunks;
tree_chunk chunk;
/* XXX: Uhm.. hopefuly, this won't be missing.. */
#if 0
/* Don't hung on unnamed vars, etc. Cannot dump these nodes. */
if (TREE_CODE(t) == VAR_DECL || TREE_CODE_CLASS(TREE_CODE(t)) == 'x')
return '\0';
#endif
chunks = lazy_dump_generic_node(t);
chunk = chunks_lookahead(chunks, 0);
pp_free_list(chunks);
return chunk_1st_char(chunk);
}
/* Get the first non-space character in a pattern. */
static char pattern_lookahead(patt_info * patt, int n)
{
const char *s = patt->format_spec + n;
int skip = 0;
do {
if (s[0] == '\\' && s[1] == ')')
skip = 2;
else if (s[0] == ' ')
skip = 1;
else
skip = 0;
s += skip;
} while (skip);
return s[0];
}
static bool match_tree_pattinfo(struct statement *, patt_info *, const char *, cfg_node);
/* Worker function for match_tree_pattinfo. Matches a lazy list with
a pattern. */
static bool
match_chunks_pattinfo(VEC(tree_chunk, heap) * chunks, patt_info * patt,
const char *delim, cfg_node ctx_node)
{
unsigned int i;
tree_chunk chunk;
for (i = 0; VEC_iterate(tree_chunk, chunks, i, chunk); i++) {
if (chunk->t) {
/* XXX: Get next char from a tree. */
/* Compute delimiter for t. */
char next_char = (i + 1 == VEC_length(tree_chunk,
chunks) ? *delim :
chunk_1st_char(chunks_lookahead
(chunks, i + 1)));
if (!match_tree_pattinfo
(chunk->t, patt, &next_char, ctx_node)) {
return 0;
}
} else if (chunk->s) {
if (*patt->format_spec == '%') {
return 0;
} else {
if (memcmp
(patt->format_spec, chunk->s,
strlen(chunk->s))) {
return 0;
}
patt->format_spec += strlen(chunk->s);
}
} else {
/* one-character chunk */
if (chunk->c == ' ') {
/* whitespace */
while (*patt->format_spec == ' ')
patt->format_spec++;
} else {
/* not whitespace */
if (*patt->format_spec == '%') {
return 0;
} else {
if (*patt->format_spec != chunk->c) {
return 0;
}
patt->format_spec++;
}
}
}
}
return 1;
}
/* Match a tree t, consuming the pattern. If the args in patt_info is
not null, the pattern has anonymous holes, indicated by the
va_args, so consume the args as you go. Otherwise (args is null),
you have a pattern with named holes. The ctx_node (if non-null)
indicates the cfg_node where the tree is supposed to occur, in case
some tmp vars are to be searched for from this point backwards. */
static bool
match_tree_pattinfo(struct statement *t, patt_info * patt, const char *delim,
cfg_node ctx_node)
{
struct statement **pt;
hole *ph = NULL;
bool res;
int parskip = 0;
struct statement *val;
info(t->pos, "tree pattinfo");
if (patt->format_spec[0] == '%'
&& *delim == pattern_lookahead(patt, 2)) {
/* lookahead(1) ok */
if (patt->format_spec[1] != '_') {
/* not "any" hole */
if (patt->args_ptr) /* anonymous holes */
pt = va_arg(*patt->args_ptr, struct statement **);
else {
/* named holes */
ph = get_hole_named(patt->format_spec[1]);
if (!ph)
die
("Invalid pattern variable: %%%c\n",
patt->format_spec[1]);
pt = &ph->tree;
}
if (!*pt) {
/* var hole */
/* refuse to catch a tmpvar def */
/* XXX: We don't do temporary variables.. */
#if 0
if (is_tmp_var(t) && ctx_node &&
(tree_scanf
(cfg_node_stmt(ctx_node), "%t = %_", NULL,
&t)
|| tree_scanf(cfg_node_stmt(ctx_node),
"%t = (%_)%_", NULL, &t))) {
return 0;
}
#endif
*pt = t;
if (ph)
ph->ctx = ctx_node;
} else {
/* instantiated hole */
if (!tree_equal_mod_tmps
(*pt, t, ph ? ph->ctx : NULL, ctx_node)) {
return 0;
}
}
}
/* else (%_) just go on */
patt->format_spec += 2; /* consume %h */
return 1;
} else {
/* Can't swallow a whole tree, must recurse on it. */
VEC(tree_chunk, heap) * chunks;
/* Check an eventual pattern-only '(' to be skipped. */
if (patt->format_spec[0] == '\\' && patt->format_spec[1] == '(') {
patt->format_spec += 2;
parskip = 1;
}
/* XXX: Commented out for now */
#if 0
/* On a tmpvar or a cast, there is no point to recurse directly (they
cannot be in the pattern), so substitute it before. */
while ((val = substitute_tmp_var(t, ctx_node, false)) != NULL
|| (val = substitute_cast_expr(t)) != NULL) {
t = val;
}
#endif
//maybe_init_pretty_print(stdout);
chunks = lazy_dump_generic_node(t);
res = match_chunks_pattinfo(chunks, patt, delim, ctx_node);
pp_free_list(chunks);
/* if needed, look for corresponding pattern-only ')' */
if (res && parskip) {
if (patt->format_spec[0] == '\\'
&& patt->format_spec[1] == ')') {
patt->format_spec += 2;
} else {
res = 0;
}
}
return res;
}
}
/* Check whether a hole name represents a global variable. */
inline bool is_global_hole(char c)
{
return 'A' <= c && c <= 'Z';
}
/* Get the hole named by a (local or global) variable name. */
hole *get_hole_named(char c)
{
if ('a' <= c && c <= 'z')
return &local_holes[c - 'a'];
if ('A' <= c && c <= 'Z')
return &global_holes[c - 'A'];
return NULL;
}
/* Unbind all global variables. */
void reset_global_holes(void)
{
int i;
for (i = 0; i < GLOBAL_MAX; i++) {
global_holes[i].tree = NULL;
global_holes[i].ctx = NULL;
}
}
/* Unbind all local variables. */
void reset_local_holes(void)
{
int i;
for (i = 0; i < LOCAL_MAX; i++) {
local_holes[i].tree = NULL;
local_holes[i].ctx = NULL;
}
}
/* Save the values of all global variables in a buffer. */
hole *save_global_holes(void)
{
hole *buf = xmalloc(sizeof(global_holes));
memcpy(buf, global_holes, sizeof(global_holes));
return buf;
}
/* Restore the values of all global variables from a buffer. */
void restore_global_holes(hole_p saved)
{
memcpy(global_holes, saved, sizeof(global_holes));
free(saved);
}
/* Compare two sets of global variables. */
bool eq_global_holes(hole * holes1, hole * holes2)
{
int i;
for (i = 0; i < GLOBAL_MAX; i++)
if (!tree_equal_mod_tmps(holes1[i].tree, holes2[i].tree,
holes1[i].ctx, holes2[i].ctx))
return 0;
return 1;
}
/* Print the list of bounded local variables. */
void print_local_holes(void)
{
int i;
for (i = 0; i < LOCAL_MAX; i++) {
if (local_holes[i].tree) {
fprintf(stderr, "local_holes[%d] == ", i);
//print_generic_expr(stderr, local_holes[i].tree, 0);
fprintf(stderr, "\n");
}
}
}
/* Print the list of bounded global variables. */
void print_global_holes(void)
{
int i, state = 0;
fprintf(stderr, "{");
for (i = 0; i < GLOBAL_MAX; i++) {
if (global_holes[i].tree) {
if (state)
fprintf(stderr, ", ");
fprintf(stderr, "%c <- ", 'A' + i);
//print_generic_expr(stderr, global_holes[i].tree, 0);
state = 1;
}
}
fprintf(stderr, "}");
}
/* Match a tree with a pattern with anonymous holes, and bind the
corresponding subtrees to the list of extra arguments. Returns
true if the tree completely matched the pattern, false otherwise.
Even on unsuccessful match, some holes might be filled in. */
bool tree_scanf(struct statement *t, const char *fmt, cfg_node ctx_node, ...)
{
patt_info patt;
va_list ap;
bool res;
va_start(ap, ctx_node);
patt.args_ptr = ≈
patt.format_spec = fmt;
res = match_tree_pattinfo(t, &patt, "", ctx_node);
va_end(ap);
/* nothing left in the pattern ? */
if (res && (*patt.format_spec == '\0'))
return true;
else
return false;
}
/* Match a tree against an atomic pattern with named holes. */
bool tree_match(struct statement *t, const char *fmt, cfg_node ctx_node)
{
patt_info patt;
bool res;
hole *old_global_holes = save_global_holes();
patt.args_ptr = NULL;
patt.format_spec = fmt;
reset_local_holes();
res = match_tree_pattinfo(t, &patt, "", ctx_node);
/* nothing left in the pattern ? */
if (res && (*patt.format_spec == '\0'))
return true;
else {
restore_global_holes(old_global_holes); /* unbind global holes */
return false;
}
}
/* Match a tree against a disjunctive pattern with named holes. */
bool tree_match_disj(struct statement *t, pattern patt, cfg_node ctx_node)
{
if (!patt)
return false;
return (tree_match(t, patt->format_spec, ctx_node)
|| (patt->next && tree_match_disj(t, patt->next, ctx_node)));
}