-
Notifications
You must be signed in to change notification settings - Fork 0
/
condate.y
299 lines (264 loc) · 7.05 KB
/
condate.y
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
/* Condate language for tree/CFG checks.
Copyright (C) 2006 Free Software Foundation, Inc.
Contributed by Nic Volanschi <nic.volanschi@free.fr>
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. */
/* The Condate language for expressing user-defined properties
of a program. These properties, called "condates"
blend control-flow, data-flow, syntactic and semantic information.
*/
%{
#define YYSTYPE void *
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree-match.h"
int yylex (void);
void yyerror (char const *);
/* TODO: Figure out how to pass arguments to bison-generated parse()
* function and rename tree_check_file and use it instead.
*/
const char tree_check_file[] = "no_name";
%}
/* Bison declarations. */
%name-prefix="condate_"
%token CONDATE
%token FROM
%token TO
%token AVOID
%token IDENT
%right OR
%token STR
%token CCODE
%token WARNING
%% /* The grammar follows. */
start: /* empty */
| start condate {/* print_cond($2); */
normalize_condate($2);
name_condate($2);
add_condate($2);};
condate: CONDATE IDENT '{' crq '}'
WARNING '(' STR ')' ';' {$$ = $4;
((condate)$$)->name = $2; ((condate)$$)->msg = $8;}
| crq ';' {$$ = $1;};
/* Constraint Reachability Queries */
crq: patexp {$$ = mkcond(NULL, $1, NULL, NULL, NULL, NULL);}
| FROM patexp TO patexp {$$ = mkcond(NULL, $2, $4, NULL, NULL, NULL);}
| FROM patexp TO patexp AVOID patexp {$$ = mkcond(NULL, $2, $4, $6, NULL, NULL);};
patexp: edgepat {$$ = $1;}
| patexp OR patexp {$$ = pat_or($1, $3);}
| '(' patexp ')' {$$ = $2;};
edgepat: pat {$$ = $1; ((pattern)$$)->sign = 0;}
| '+' pat {$$ = $2; ((pattern)$$)->sign = +1;}
| '-' pat {$$ = $2; ((pattern)$$)->sign = -1;};
pat: STR sempat {$$ = mkpat($1); free($1);};
sempat: /* empty */
| '|' CCODE {fprintf(stderr, "%s: warning: semantic patterns NYI: {%s}\n",
tree_check_file, (char *)$2);};
%%
/* The lexical analyzer returns a double floating point
number on the stack and the token NUM, or the numeric code
of the character read if not a number. It skips all blanks
and tabs, and returns 0 for end-of-input. */
#include <ctype.h>
/* The folowing should be bigger than all of the folowing:
- the maximal keyword length
- the maximal pattern length
- the maximal length of a CCODE block.
Note: this ugly limit should be eliminated by writing the lexer in Flex.
*/
#define MAX_KEYWORD_LEN 1024
int
yylex (void)
{
int c;
static char buf[MAX_KEYWORD_LEN + 1];
int len;
static int afterbar = 0;
c = getc (checkfile);
/* Skip white space and comments. */
do
{
while (c == ' ' || c == '\t' || c == '\n')
c = getc (checkfile);
if(c == '#')
{
while ((c = getc (checkfile)) != '\n' && c != EOF)
;
if (c == '\n')
c = getc (checkfile);
}
} while(c == ' ' || c == '\t' || c == '#' || c == '\n');
/* Return end-of-input. */
if (c == EOF)
return 0;
/* recognize one-character keywords */
if (c == '+' || c == '-' || c == ';'
|| c == '}' || c == '(' || c == ')')
return c;
if (c == '|')
{
afterbar = 1;
return c;
}
/* Process strings. */
if (c == '"')
{
len = 0;
while ((c = getc (checkfile)) != '"' && c != EOF && len < MAX_KEYWORD_LEN)
{
buf[len++] = c;
}
buf[len] = 0;
if (c == EOF || len == MAX_KEYWORD_LEN)
return 0;
yylval = xstrdup(buf);
return STR;
}
/* Meaning of '{' is context-dependent: */
if (c == '{')
{
if (!afterbar)
return c;
else
{ /* Process C code. */
len = 0;
while ((c = getc (checkfile)) != '}' && c != EOF && len < MAX_KEYWORD_LEN)
{
buf[len++] = c;
}
if (c == EOF || len == MAX_KEYWORD_LEN)
return 0;
buf[len] = 0;
afterbar = 0;
yylval = xstrdup(buf);
return CCODE;
}
}
/* Recognize keywords & identifiers */
if (isalpha(c))
{
len = 0;
buf[len++] = c;
while ((isalnum((c = getc (checkfile))) || c == '_') && len < MAX_KEYWORD_LEN)
{
buf[len++] = c;
}
if (c == EOF || len == MAX_KEYWORD_LEN)
return 0;
buf[len] = 0;
ungetc (c, checkfile);
/* try keywords */
if (!strcmp (buf, "condate"))
return CONDATE;
else if (!strcmp (buf, "from"))
return FROM;
else if (!strcmp (buf, "to"))
return TO;
else if (!strcmp (buf, "avoid"))
return AVOID;
else if (!strcmp (buf, "or"))
return OR;
else if (!strcmp (buf, "warning"))
return WARNING;
/* identifier */
yylval = xstrdup (buf);
return IDENT;
}
/* Return a single char. */
fprintf (stderr, "Illegal character: '%c'\n", c);
return 0;
}
/* Called by yyparse on error. */
void
yyerror (char const *s)
{
char buf[32];
buf[0] = '\0';
fprintf (stderr, "%s: %s\n", tree_check_file, s);
if (feof(checkfile))
strcpy(buf, "end of file");
else
fgets (buf, 32, checkfile);
fprintf (stderr, "%s: before or near: \"%s\"\n",
tree_check_file, buf);
}
struct split_pattern_s split_pattern(pattern p);
/* Structure to return a pattern splitted in: unsigned, positive, and negative
edge patterns. */
struct split_pattern_s {pattern p1, p2, p3;};
struct split_pattern_s
split_pattern (pattern p)
{
struct split_pattern_s sp;
if (!p)
sp.p1 = sp.p2 = sp.p3 = NULL;
else
{
sp = split_pattern(p->next);
if (p->sign == 0)
{
p->next = sp.p1;
sp.p1 = p;
}
else if (p->sign > 0)
{
p->next = sp.p2;
sp.p2 = p;
}
else
{
p->next = sp.p3;
sp.p3 = p;
}
}
return sp;
}
/* Normalize a condate by separating the avoid patterns into:
- avoid (unsigned edge patterns),
- avoid_then (positive edge patterns), and
- avoid_else (negative edge patterns).
Normalization conserves the meaning of a condate, but optimizes its matching.
Note: we assume that the initial condate contains only 'avoid' patterns.
*/
void
normalize_condate (condate cond)
{
struct split_pattern_s sp = split_pattern (cond->avoid);
cond->avoid = sp.p1;
cond->avoid_then = sp.p2;
cond->avoid_else = sp.p3;
}
void
name_condate (condate cond)
{
if(!cond->name)
{
cond->name = xmalloc (strlen (tree_check_file) + 6);
strcpy (cond->name, tree_check_file);
sprintf (cond->name + strlen (tree_check_file), "[%d]", n_conds + 1);
}
}
void
add_condate (condate cond)
{
static int warned = 0;
if (n_conds == CONDMAX && !warned)
{
fprintf (stderr, "Warning: ignoring checks beyond %d", CONDMAX);
warned = 1;
return;
}
conds[n_conds++] = cond;
}