1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2024 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>
37 typedef struct _open_table open_table
;
55 current_line (open_table
* file
)
57 line_ref
*entry
= ZALLOC (line_ref
);
58 *entry
= file
->pseudo_line
;
63 new_table_entry (open_table
* file
, table_entry_type type
)
66 entry
= ZALLOC (table_entry
);
67 entry
->file
= file
->root
;
68 entry
->line
= current_line (file
);
74 set_nr_table_entry_fields (table_entry
*entry
, int nr_fields
)
76 entry
->field
= NZALLOC (char *, nr_fields
+ 1);
77 entry
->nr_fields
= nr_fields
;
82 table_push (table
*root
,
84 table_include
*includes
,
85 const char *file_name
)
90 table_include
*include
= &dummy
;
92 /* dummy up a search of this directory */
93 dummy
.next
= includes
;
96 /* create a file descriptor */
97 file
= ZALLOC (open_table
);
104 file
->parent
= root
->current
;
105 root
->current
= file
;
109 /* save the file name */
111 NZALLOC (char, strlen (include
->dir
) + strlen (file_name
) + 2);
112 if (dup_name
== NULL
)
117 if (include
->dir
[0] != '\0')
119 strcat (dup_name
, include
->dir
);
120 strcat (dup_name
, "/");
122 strcat (dup_name
, file_name
);
123 file
->real_line
.file_name
= dup_name
;
124 file
->pseudo_line
.file_name
= dup_name
;
127 ff
= fopen (dup_name
, "rb");
130 /* free (dup_name); */
131 if (include
->next
== NULL
)
134 error (line
, "Problem opening file `%s'\n", file_name
);
138 include
= include
->next
;
142 /* determine the size */
143 fseek (ff
, 0, SEEK_END
);
144 file
->size
= ftell (ff
);
145 fseek (ff
, 0, SEEK_SET
);
147 /* allocate this much memory */
148 file
->buffer
= (char *) zalloc (file
->size
+ 1);
149 if (file
->buffer
== NULL
)
154 file
->pos
= file
->buffer
;
157 if (fread (file
->buffer
, 1, file
->size
, ff
) < file
->size
)
162 file
->buffer
[file
->size
] = '\0';
164 /* set the initial line numbering */
165 file
->real_line
.line_nr
= 1; /* specifies current line */
166 file
->pseudo_line
.line_nr
= 1; /* specifies current line */
173 table_open (const char *file_name
)
177 /* create a file descriptor */
178 root
= ZALLOC (table
);
185 table_push (root
, NULL
, NULL
, file_name
);
190 skip_spaces (char *chp
)
194 if (*chp
== '\0' || *chp
== '\n' || !isspace (*chp
))
202 back_spaces (char *start
, char *chp
)
206 if (chp
<= start
|| !isspace (chp
[-1]))
213 skip_digits (char *chp
)
217 if (*chp
== '\0' || *chp
== '\n' || !isdigit (*chp
))
224 skip_to_separator (char *chp
, char *separators
)
228 char *sep
= separators
;
242 skip_to_null (char *chp
)
244 return skip_to_separator (chp
, "");
249 skip_to_nl (char *chp
)
251 return skip_to_separator (chp
, "\n");
256 next_line (open_table
* file
)
258 file
->pos
= skip_to_nl (file
->pos
);
259 if (*file
->pos
== '0')
260 error (&file
->pseudo_line
, "Missing <nl> at end of line\n");
263 file
->real_line
.line_nr
+= 1;
264 file
->pseudo_line
.line_nr
+= 1;
269 table_read (table
*root
)
271 open_table
*file
= root
->current
;
272 table_entry
*entry
= NULL
;
277 while (*file
->pos
== '\0')
279 if (file
->parent
!= NULL
)
282 root
->current
= file
;
289 if (*file
->pos
== '{')
292 next_line (file
); /* discard leading brace */
293 entry
= new_table_entry (file
, table_code_entry
);
295 /* determine how many lines are involved - look for <nl> "}" */
298 while (*file
->pos
!= '}')
303 set_nr_table_entry_fields (entry
, nr_lines
);
305 /* now enter each line */
308 for (line_nr
= 0; line_nr
< entry
->nr_fields
; line_nr
++)
310 if (strncmp (chp
, " ", 2) == 0)
311 entry
->field
[line_nr
] = chp
+ 2;
313 entry
->field
[line_nr
] = chp
;
314 chp
= skip_to_null (chp
) + 1;
316 /* skip trailing brace */
317 ASSERT (*file
->pos
== '}');
324 if (*file
->pos
== '\t')
326 char *chp
= file
->pos
;
327 entry
= new_table_entry (file
, table_code_entry
);
328 /* determine how many lines are involved - look for <nl> !<tab> */
331 int nr_blank_lines
= 0;
334 if (*file
->pos
== '\t')
336 nr_lines
= nr_lines
+ nr_blank_lines
+ 1;
342 file
->pos
= skip_spaces (file
->pos
);
343 if (*file
->pos
!= '\n')
349 set_nr_table_entry_fields (entry
, nr_lines
);
351 /* now enter each line */
354 for (line_nr
= 0; line_nr
< entry
->nr_fields
; line_nr
++)
357 entry
->field
[line_nr
] = chp
+ 1;
359 entry
->field
[line_nr
] = ""; /* blank */
360 chp
= skip_to_null (chp
) + 1;
367 if (file
->pos
[0] == '#')
369 char *chp
= skip_spaces (file
->pos
+ 1);
371 /* cpp line-nr directive - # <line-nr> "<file>" */
373 && *skip_digits (chp
) == ' '
374 && *skip_spaces (skip_digits (chp
)) == '"')
379 /* parse the number */
380 line_nr
= atoi (file
->pos
) - 1;
381 /* skip to the file name */
382 while (file
->pos
[0] != '0'
383 && file
->pos
[0] != '"' && file
->pos
[0] != '\0')
385 if (file
->pos
[0] != '"')
386 error (&file
->real_line
,
387 "Missing opening quote in cpp directive\n");
388 /* parse the file name */
390 file_name
= file
->pos
;
391 while (file
->pos
[0] != '"' && file
->pos
[0] != '\0')
393 if (file
->pos
[0] != '"')
394 error (&file
->real_line
,
395 "Missing closing quote in cpp directive\n");
398 file
->pos
= skip_to_nl (file
->pos
);
399 if (file
->pos
[0] != '\n')
400 error (&file
->real_line
,
401 "Missing newline in cpp directive\n");
402 file
->pseudo_line
.file_name
= file_name
;
403 file
->pseudo_line
.line_nr
= line_nr
;
408 /* #define and #undef - not implemented yet */
410 /* Old style # comment */
415 /* blank line or end-of-file? */
416 file
->pos
= skip_spaces (file
->pos
);
417 if (*file
->pos
== '\0')
418 error (&file
->pseudo_line
, "Missing <nl> at end of file\n");
419 if (*file
->pos
== '\n')
425 /* comment - leading // or # - skip */
426 if ((file
->pos
[0] == '/' && file
->pos
[1] == '/')
427 || (file
->pos
[0] == '#'))
435 char *chp
= file
->pos
;
436 entry
= new_table_entry (file
, table_colon_entry
);
438 /* figure out how many fields */
444 tmpch
= skip_to_separator (tmpch
, "\\:");
447 /* eat the escaped character */
449 while (cp
[1] != '\0')
457 else if (*tmpch
!= ':')
466 set_nr_table_entry_fields (entry
, nr_fields
);
471 for (field_nr
= 0; field_nr
< entry
->nr_fields
; field_nr
++)
473 chp
= skip_spaces (chp
);
474 entry
->field
[field_nr
] = chp
;
475 chp
= skip_to_null (chp
);
476 *back_spaces (entry
->field
[field_nr
], chp
) = '\0';
485 ASSERT (entry
== NULL
|| entry
->field
[entry
->nr_fields
] == NULL
);
490 table_print_code (lf
*file
, const table_entry
*entry
)
493 for (field_nr
= 0; field_nr
< entry
->nr_fields
; field_nr
++)
495 char *chp
= entry
->field
[field_nr
];
496 int in_bit_field
= 0;
498 lf_indent_suppress (file
);
501 if (chp
[0] == '{' && !isspace (chp
[1]) && chp
[1] != '\0')
504 lf_putchr (file
, '_');
506 else if (in_bit_field
&& chp
[0] == ':')
508 lf_putchr (file
, '_');
510 else if (in_bit_field
&& *chp
== '}')
512 lf_putchr (file
, '_');
517 lf_putchr (file
, *chp
);
523 line_ref line
= *entry
->line
;
524 line
.line_nr
+= field_nr
;
525 error (&line
, "Bit field brace miss match\n");
527 lf_putchr (file
, '\n');
533 dump_line_ref (lf
*file
,
535 const line_ref
*line
,
538 lf_printf (file
, "%s(line_ref*) %p", prefix
, line
);
541 lf_indent (file
, +1);
542 lf_printf (file
, "\n(line_nr %d)", line
->line_nr
);
543 lf_printf (file
, "\n(file_name %s)", line
->file_name
);
544 lf_indent (file
, -1);
546 lf_printf (file
, "%s", suffix
);
551 table_entry_type_to_str (table_entry_type type
)
555 case table_code_entry
:
557 case table_colon_entry
:
558 return "colon-entry";
564 dump_table_entry (lf
*file
,
566 const table_entry
*entry
,
569 lf_printf (file
, "%s(table_entry*) %p", prefix
, entry
);
573 lf_indent (file
, +1);
574 dump_line_ref (file
, "\n(line ", entry
->line
, ")");
575 lf_printf (file
, "\n(type %s)", table_entry_type_to_str (entry
->type
));
576 lf_printf (file
, "\n(nr_fields %d)", entry
->nr_fields
);
577 lf_printf (file
, "\n(fields");
578 lf_indent (file
, +1);
579 for (field
= 0; field
< entry
->nr_fields
; field
++)
580 lf_printf (file
, "\n\"%s\"", entry
->field
[field
]);
581 lf_indent (file
, -1);
582 lf_printf (file
, ")");
583 lf_indent (file
, -1);
585 lf_printf (file
, "%s", suffix
);
591 main (int argc
, char **argv
)
600 printf ("Usage: table <file>\n");
604 t
= table_open (argv
[1]);
605 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-table");
611 entry
= table_read (t
);
613 sprintf (line
, "(%d ", line_nr
);
614 dump_table_entry (l
, line
, entry
, ")\n");
616 while (entry
!= NULL
);