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
|
/* MD reader for GCC.
Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2010
Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "bconfig.h"
#include "system.h"
#include "coretypes.h"
#include "hashtab.h"
#include "read-md.h"
/* Associates PTR (which can be a string, etc.) with the file location
specified by FILENAME and LINENO. */
struct ptr_loc {
const void *ptr;
const char *filename;
int lineno;
};
/* Obstack used for allocating MD strings. */
struct obstack string_obstack;
/* A table of ptr_locs, hashed on the PTR field. */
static htab_t ptr_locs;
/* An obstack for the above. Plain xmalloc is a bit heavyweight for a
small structure like ptr_loc. */
static struct obstack ptr_loc_obstack;
/* A hash table of triples (A, B, C), where each of A, B and C is a condition
and A is equivalent to "B && C". This is used to keep track of the source
of conditions that are made up of separate MD strings (such as the split
condition of a define_insn_and_split). */
static htab_t joined_conditions;
/* An obstack for allocating joined_conditions entries. */
static struct obstack joined_conditions_obstack;
/* The file we are reading. */
FILE *read_md_file;
/* The filename of READ_MD_FILE. */
const char *read_md_filename;
/* The current line number in READ_MD_FILE. */
int read_md_lineno;
/* Return a hash value for the pointer pointed to by DEF. */
static hashval_t
leading_ptr_hash (const void *def)
{
return htab_hash_pointer (*(const void *const *) def);
}
/* Return true if DEF1 and DEF2 are pointers to the same pointer. */
static int
leading_ptr_eq_p (const void *def1, const void *def2)
{
return *(const void *const *) def1 == *(const void *const *) def2;
}
/* Associate PTR with the file position given by FILENAME and LINENO. */
static void
set_md_ptr_loc (const void *ptr, const char *filename, int lineno)
{
struct ptr_loc *loc;
loc = (struct ptr_loc *) obstack_alloc (&ptr_loc_obstack,
sizeof (struct ptr_loc));
loc->ptr = ptr;
loc->filename = filename;
loc->lineno = lineno;
*htab_find_slot (ptr_locs, loc, INSERT) = loc;
}
/* Return the position associated with pointer PTR. Return null if no
position was set. */
static const struct ptr_loc *
get_md_ptr_loc (const void *ptr)
{
return (const struct ptr_loc *) htab_find (ptr_locs, &ptr);
}
/* Associate NEW_PTR with the same file position as OLD_PTR. */
void
copy_md_ptr_loc (const void *new_ptr, const void *old_ptr)
{
const struct ptr_loc *loc = get_md_ptr_loc (old_ptr);
if (loc != 0)
set_md_ptr_loc (new_ptr, loc->filename, loc->lineno);
}
/* If PTR is associated with a known file position, print a #line
directive for it. */
void
print_md_ptr_loc (const void *ptr)
{
const struct ptr_loc *loc = get_md_ptr_loc (ptr);
if (loc != 0)
printf ("#line %d \"%s\"\n", loc->lineno, loc->filename);
}
/* Return a condition that satisfies both COND1 and COND2. Either string
may be null or empty. */
const char *
join_c_conditions (const char *cond1, const char *cond2)
{
char *result;
const void **entry;
if (cond1 == 0 || cond1[0] == 0)
return cond2;
if (cond2 == 0 || cond2[0] == 0)
return cond1;
if (strcmp (cond1, cond2) == 0)
return cond1;
result = concat ("(", cond1, ") && (", cond2, ")", NULL);
obstack_ptr_grow (&joined_conditions_obstack, result);
obstack_ptr_grow (&joined_conditions_obstack, cond1);
obstack_ptr_grow (&joined_conditions_obstack, cond2);
entry = XOBFINISH (&joined_conditions_obstack, const void **);
*htab_find_slot (joined_conditions, entry, INSERT) = entry;
return result;
}
/* Print condition COND, wrapped in brackets. If COND was created by
join_c_conditions, recursively invoke this function for the original
conditions and join the result with "&&". Otherwise print a #line
directive for COND if its original file position is known. */
void
print_c_condition (const char *cond)
{
const char **halves = (const char **) htab_find (joined_conditions, &cond);
if (halves != 0)
{
printf ("(");
print_c_condition (halves[1]);
printf (" && ");
print_c_condition (halves[2]);
printf (")");
}
else
{
putc ('\n', stdout);
print_md_ptr_loc (cond);
printf ("(%s)", cond);
}
}
/* A printf-like function for reporting an error against line LINENO
in the current MD file. */
void
message_with_line (int lineno, const char *msg, ...)
{
va_list ap;
va_start (ap, msg);
fprintf (stderr, "%s:%d: ", read_md_filename, lineno);
vfprintf (stderr, msg, ap);
fputc ('\n', stderr);
va_end (ap);
}
/* A printf-like function for reporting an error against the current
position in the MD file. */
void
fatal_with_file_and_line (const char *msg, ...)
{
char context[64];
size_t i;
int c;
va_list ap;
va_start (ap, msg);
fprintf (stderr, "%s:%d: ", read_md_filename, read_md_lineno);
vfprintf (stderr, msg, ap);
putc ('\n', stderr);
/* Gather some following context. */
for (i = 0; i < sizeof (context)-1; ++i)
{
c = read_char ();
if (c == EOF)
break;
if (c == '\r' || c == '\n')
break;
context[i] = c;
}
context[i] = '\0';
fprintf (stderr, "%s:%d: following context is `%s'\n",
read_md_filename, read_md_lineno, context);
va_end (ap);
exit (1);
}
/* Report that we found character ACTUAL when we expected to find
character EXPECTED. */
void
fatal_expected_char (int expected, int actual)
{
if (actual == EOF)
fatal_with_file_and_line ("expected character `%c', found EOF",
expected);
else
fatal_with_file_and_line ("expected character `%c', found `%c'",
expected, actual);
}
/* Read chars from the MD file until a non-whitespace char and return that.
Comments, both Lisp style and C style, are treated as whitespace. */
int
read_skip_spaces (void)
{
int c;
while (1)
{
c = read_char ();
switch (c)
{
case '\n':
read_md_lineno++;
break;
case ' ': case '\t': case '\f': case '\r':
break;
case ';':
do
c = read_char ();
while (c != '\n' && c != EOF);
read_md_lineno++;
break;
case '/':
{
int prevc;
c = read_char ();
if (c != '*')
fatal_expected_char ('*', c);
prevc = 0;
while ((c = read_char ()) && c != EOF)
{
if (c == '\n')
read_md_lineno++;
else if (prevc == '*' && c == '/')
break;
prevc = c;
}
}
break;
default:
return c;
}
}
}
/* Subroutine of the string readers. Handles backslash escapes.
Caller has read the backslash, but not placed it into the obstack. */
static void
read_escape (void)
{
int c = read_char ();
switch (c)
{
/* Backslash-newline is replaced by nothing, as in C. */
case '\n':
read_md_lineno++;
return;
/* \" \' \\ are replaced by the second character. */
case '\\':
case '"':
case '\'':
break;
/* Standard C string escapes:
\a \b \f \n \r \t \v
\[0-7] \x
all are passed through to the output string unmolested.
In normal use these wind up in a string constant processed
by the C compiler, which will translate them appropriately.
We do not bother checking that \[0-7] are followed by up to
two octal digits, or that \x is followed by N hex digits.
\? \u \U are left out because they are not in traditional C. */
case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
case '7': case 'x':
obstack_1grow (&string_obstack, '\\');
break;
/* \; makes stuff for a C string constant containing
newline and tab. */
case ';':
obstack_grow (&string_obstack, "\\n\\t", 4);
return;
/* pass anything else through, but issue a warning. */
default:
fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
read_md_filename, read_md_lineno, c);
obstack_1grow (&string_obstack, '\\');
break;
}
obstack_1grow (&string_obstack, c);
}
/* Read a double-quoted string onto the obstack. Caller has scanned
the leading quote. */
char *
read_quoted_string (void)
{
int c;
while (1)
{
c = read_char (); /* Read the string */
if (c == '\n')
read_md_lineno++;
else if (c == '\\')
{
read_escape ();
continue;
}
else if (c == '"' || c == EOF)
break;
obstack_1grow (&string_obstack, c);
}
obstack_1grow (&string_obstack, 0);
return XOBFINISH (&string_obstack, char *);
}
/* Read a braced string (a la Tcl) onto the string obstack. Caller
has scanned the leading brace. Note that unlike quoted strings,
the outermost braces _are_ included in the string constant. */
static char *
read_braced_string (void)
{
int c;
int brace_depth = 1; /* caller-processed */
unsigned long starting_read_md_lineno = read_md_lineno;
obstack_1grow (&string_obstack, '{');
while (brace_depth)
{
c = read_char (); /* Read the string */
if (c == '\n')
read_md_lineno++;
else if (c == '{')
brace_depth++;
else if (c == '}')
brace_depth--;
else if (c == '\\')
{
read_escape ();
continue;
}
else if (c == EOF)
fatal_with_file_and_line
("missing closing } for opening brace on line %lu",
starting_read_md_lineno);
obstack_1grow (&string_obstack, c);
}
obstack_1grow (&string_obstack, 0);
return XOBFINISH (&string_obstack, char *);
}
/* Read some kind of string constant. This is the high-level routine
used by read_rtx. It handles surrounding parentheses, leading star,
and dispatch to the appropriate string constant reader. */
char *
read_string (int star_if_braced)
{
char *stringbuf;
int saw_paren = 0;
int c, old_lineno;
c = read_skip_spaces ();
if (c == '(')
{
saw_paren = 1;
c = read_skip_spaces ();
}
old_lineno = read_md_lineno;
if (c == '"')
stringbuf = read_quoted_string ();
else if (c == '{')
{
if (star_if_braced)
obstack_1grow (&string_obstack, '*');
stringbuf = read_braced_string ();
}
else
fatal_with_file_and_line ("expected `\"' or `{', found `%c'", c);
if (saw_paren)
{
c = read_skip_spaces ();
if (c != ')')
fatal_expected_char (')', c);
}
set_md_ptr_loc (stringbuf, read_md_filename, old_lineno);
return stringbuf;
}
/* Given a string, return the number of comma-separated elements in it.
Return 0 for the null string. */
int
n_comma_elts (const char *s)
{
int n;
if (*s == '\0')
return 0;
for (n = 1; *s; s++)
if (*s == ',')
n++;
return n;
}
/* Given a pointer to a (char *), return a pointer to the beginning of the
next comma-separated element in the string. Advance the pointer given
to the end of that element. Return NULL if at end of string. Caller
is responsible for copying the string if necessary. White space between
a comma and an element is ignored. */
const char *
scan_comma_elt (const char **pstr)
{
const char *start;
const char *p = *pstr;
if (*p == ',')
p++;
while (ISSPACE(*p))
p++;
if (*p == '\0')
return NULL;
start = p;
while (*p != ',' && *p != '\0')
p++;
*pstr = p;
return start;
}
/* Initialize this file's static data. */
void
init_md_reader (void)
{
obstack_init (&string_obstack);
ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
obstack_init (&ptr_loc_obstack);
joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
obstack_init (&joined_conditions_obstack);
}
|