1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2016 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
43 typedef struct _open_table open_table;
61 current_line (open_table * file)
63 line_ref *entry = ZALLOC (line_ref);
64 *entry = file->pseudo_line;
69 new_table_entry (open_table * file, table_entry_type type)
72 entry = ZALLOC (table_entry);
73 entry->file = file->root;
74 entry->line = current_line (file);
80 set_nr_table_entry_fields (table_entry *entry, int nr_fields)
82 entry->field = NZALLOC (char *, nr_fields + 1);
83 entry->nr_fields = nr_fields;
88 table_push (table *root,
89 line_ref *line, table_include *includes, const char *file_name)
94 table_include *include = &dummy;
96 /* dummy up a search of this directory */
97 dummy.next = includes;
100 /* create a file descriptor */
101 file = ZALLOC (open_table);
108 file->parent = root->current;
109 root->current = file;
113 /* save the file name */
115 NZALLOC (char, strlen (include->dir) + strlen (file_name) + 2);
116 if (dup_name == NULL)
121 if (include->dir[0] != '\0')
123 strcat (dup_name, include->dir);
124 strcat (dup_name, "/");
126 strcat (dup_name, file_name);
127 file->real_line.file_name = dup_name;
128 file->pseudo_line.file_name = dup_name;
131 ff = fopen (dup_name, "rb");
134 /* free (dup_name); */
135 if (include->next == NULL)
138 error (line, "Problem opening file `%s'\n", file_name);
142 include = include->next;
146 /* determine the size */
147 fseek (ff, 0, SEEK_END);
148 file->size = ftell (ff);
149 fseek (ff, 0, SEEK_SET);
151 /* allocate this much memory */
152 file->buffer = (char *) zalloc (file->size + 1);
153 if (file->buffer == NULL)
158 file->pos = file->buffer;
161 if (fread (file->buffer, 1, file->size, ff) < file->size)
166 file->buffer[file->size] = '\0';
168 /* set the initial line numbering */
169 file->real_line.line_nr = 1; /* specifies current line */
170 file->pseudo_line.line_nr = 1; /* specifies current line */
177 table_open (const char *file_name)
181 /* create a file descriptor */
182 root = ZALLOC (table);
189 table_push (root, NULL, NULL, file_name);
194 skip_spaces (char *chp)
198 if (*chp == '\0' || *chp == '\n' || !isspace (*chp))
206 back_spaces (char *start, char *chp)
210 if (chp <= start || !isspace (chp[-1]))
217 skip_digits (char *chp)
221 if (*chp == '\0' || *chp == '\n' || !isdigit (*chp))
228 skip_to_separator (char *chp, char *separators)
232 char *sep = separators;
246 skip_to_null (char *chp)
248 return skip_to_separator (chp, "");
253 skip_to_nl (char *chp)
255 return skip_to_separator (chp, "\n");
260 next_line (open_table * file)
262 file->pos = skip_to_nl (file->pos);
263 if (*file->pos == '0')
264 error (&file->pseudo_line, "Missing <nl> at end of line\n");
267 file->real_line.line_nr += 1;
268 file->pseudo_line.line_nr += 1;
273 table_read (table *root)
275 open_table *file = root->current;
276 table_entry *entry = NULL;
281 while (*file->pos == '\0')
283 if (file->parent != NULL)
286 root->current = file;
293 if (*file->pos == '{')
296 next_line (file); /* discard leading brace */
297 entry = new_table_entry (file, table_code_entry);
299 /* determine how many lines are involved - look for <nl> "}" */
302 while (*file->pos != '}')
307 set_nr_table_entry_fields (entry, nr_lines);
309 /* now enter each line */
312 for (line_nr = 0; line_nr < entry->nr_fields; line_nr++)
314 if (strncmp (chp, " ", 2) == 0)
315 entry->field[line_nr] = chp + 2;
317 entry->field[line_nr] = chp;
318 chp = skip_to_null (chp) + 1;
320 /* skip trailing brace */
321 ASSERT (*file->pos == '}');
328 if (*file->pos == '\t')
330 char *chp = file->pos;
331 entry = new_table_entry (file, table_code_entry);
332 /* determine how many lines are involved - look for <nl> !<tab> */
335 int nr_blank_lines = 0;
338 if (*file->pos == '\t')
340 nr_lines = nr_lines + nr_blank_lines + 1;
346 file->pos = skip_spaces (file->pos);
347 if (*file->pos != '\n')
353 set_nr_table_entry_fields (entry, nr_lines);
355 /* now enter each line */
358 for (line_nr = 0; line_nr < entry->nr_fields; line_nr++)
361 entry->field[line_nr] = chp + 1;
363 entry->field[line_nr] = ""; /* blank */
364 chp = skip_to_null (chp) + 1;
371 if (file->pos[0] == '#')
373 char *chp = skip_spaces (file->pos + 1);
375 /* cpp line-nr directive - # <line-nr> "<file>" */
377 && *skip_digits (chp) == ' '
378 && *skip_spaces (skip_digits (chp)) == '"')
383 /* parse the number */
384 line_nr = atoi (file->pos) - 1;
385 /* skip to the file name */
386 while (file->pos[0] != '0'
387 && file->pos[0] != '"' && file->pos[0] != '\0')
389 if (file->pos[0] != '"')
390 error (&file->real_line,
391 "Missing opening quote in cpp directive\n");
392 /* parse the file name */
394 file_name = file->pos;
395 while (file->pos[0] != '"' && file->pos[0] != '\0')
397 if (file->pos[0] != '"')
398 error (&file->real_line,
399 "Missing closing quote in cpp directive\n");
402 file->pos = skip_to_nl (file->pos);
403 if (file->pos[0] != '\n')
404 error (&file->real_line,
405 "Missing newline in cpp directive\n");
406 file->pseudo_line.file_name = file_name;
407 file->pseudo_line.line_nr = line_nr;
412 /* #define and #undef - not implemented yet */
414 /* Old style # comment */
419 /* blank line or end-of-file? */
420 file->pos = skip_spaces (file->pos);
421 if (*file->pos == '\0')
422 error (&file->pseudo_line, "Missing <nl> at end of file\n");
423 if (*file->pos == '\n')
429 /* comment - leading // or # - skip */
430 if ((file->pos[0] == '/' && file->pos[1] == '/')
431 || (file->pos[0] == '#'))
439 char *chp = file->pos;
440 entry = new_table_entry (file, table_colon_entry);
442 /* figure out how many fields */
448 tmpch = skip_to_separator (tmpch, "\\:");
451 /* eat the escaped character */
453 while (cp[1] != '\0')
461 else if (*tmpch != ':')
470 set_nr_table_entry_fields (entry, nr_fields);
475 for (field_nr = 0; field_nr < entry->nr_fields; field_nr++)
477 chp = skip_spaces (chp);
478 entry->field[field_nr] = chp;
479 chp = skip_to_null (chp);
480 *back_spaces (entry->field[field_nr], chp) = '\0';
489 ASSERT (entry == NULL || entry->field[entry->nr_fields] == NULL);
494 table_print_code (lf *file, table_entry *entry)
498 for (field_nr = 0; field_nr < entry->nr_fields; field_nr++)
500 char *chp = entry->field[field_nr];
501 int in_bit_field = 0;
503 lf_indent_suppress (file);
506 if (chp[0] == '{' && !isspace (chp[1]) && chp[1] != '\0')
509 nr += lf_putchr (file, '_');
511 else if (in_bit_field && chp[0] == ':')
513 nr += lf_putchr (file, '_');
515 else if (in_bit_field && *chp == '}')
517 nr += lf_putchr (file, '_');
522 nr += lf_putchr (file, *chp);
528 line_ref line = *entry->line;
529 line.line_nr += field_nr;
530 error (&line, "Bit field brace miss match\n");
532 nr += lf_putchr (file, '\n');
539 dump_line_ref (lf *file, char *prefix, const line_ref *line, char *suffix)
541 lf_printf (file, "%s(line_ref*) 0x%lx", prefix, (long) line);
544 lf_indent (file, +1);
545 lf_printf (file, "\n(line_nr %d)", line->line_nr);
546 lf_printf (file, "\n(file_name %s)", line->file_name);
547 lf_indent (file, -1);
549 lf_printf (file, "%s", suffix);
554 table_entry_type_to_str (table_entry_type type)
558 case table_code_entry:
560 case table_colon_entry:
561 return "colon-entry";
567 dump_table_entry (lf *file,
568 char *prefix, const table_entry *entry, char *suffix)
570 lf_printf (file, "%s(table_entry*) 0x%lx", prefix, (long) entry);
574 lf_indent (file, +1);
575 dump_line_ref (file, "\n(line ", entry->line, ")");
576 lf_printf (file, "\n(type %s)", table_entry_type_to_str (entry->type));
577 lf_printf (file, "\n(nr_fields %d)", entry->nr_fields);
578 lf_printf (file, "\n(fields");
579 lf_indent (file, +1);
580 for (field = 0; field < entry->nr_fields; field++)
581 lf_printf (file, "\n\"%s\"", entry->field[field]);
582 lf_indent (file, -1);
583 lf_printf (file, ")");
584 lf_indent (file, -1);
586 lf_printf (file, "%s", suffix);
592 main (int argc, char **argv)
601 printf ("Usage: table <file>\n");
605 t = table_open (argv[1]);
606 l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-table");
612 entry = table_read (t);
614 sprintf (line, "(%d ", line_nr);
615 dump_table_entry (l, line, entry, ")\n");
617 while (entry != NULL);