1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
26 #include <sys/types.h>
45 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
,
70 table_entry_type type
)
73 entry
= ZALLOC (table_entry
);
74 entry
->file
= file
->root
;
75 entry
->line
= current_line (file
);
81 set_nr_table_entry_fields (table_entry
*entry
,
84 entry
->field
= NZALLOC (char*, nr_fields
+ 1);
85 entry
->nr_fields
= nr_fields
;
90 table_push (table
*root
,
92 table_include
*includes
,
93 const char *file_name
)
98 table_include
*include
= &dummy
;
100 /* dummy up a search of this directory */
101 dummy
.next
= includes
;
104 /* create a file descriptor */
105 file
= ZALLOC (open_table
);
112 file
->parent
= root
->current
;
113 root
->current
= file
;
117 /* save the file name */
118 char *dup_name
= NZALLOC (char, strlen (include
->dir
) + strlen (file_name
) + 2);
119 if (dup_name
== NULL
)
124 if (include
->dir
[0] != '\0')
126 strcat (dup_name
, include
->dir
);
127 strcat (dup_name
, "/");
129 strcat (dup_name
, file_name
);
130 file
->real_line
.file_name
= dup_name
;
131 file
->pseudo_line
.file_name
= dup_name
;
134 ff
= fopen (dup_name
, "rb");
137 /* zfree (dup_name); */
138 if (include
->next
== NULL
)
141 error (line
, "Problem opening file `%s'\n", file_name
);
145 include
= include
->next
;
149 /* determine the size */
150 fseek (ff
, 0, SEEK_END
);
151 file
->size
= ftell (ff
);
152 fseek (ff
, 0, SEEK_SET
);
154 /* allocate this much memory */
155 file
->buffer
= (char*) zalloc (file
->size
+ 1);
156 if (file
->buffer
== NULL
)
161 file
->pos
= file
->buffer
;
164 if (fread (file
->buffer
, 1, file
->size
, ff
) < file
->size
) {
168 file
->buffer
[file
->size
] = '\0';
170 /* set the initial line numbering */
171 file
->real_line
.line_nr
= 1; /* specifies current line */
172 file
->pseudo_line
.line_nr
= 1; /* specifies current line */
179 table_open (const char *file_name
)
183 /* create a file descriptor */
184 root
= ZALLOC (table
);
191 table_push (root
, NULL
, NULL
, file_name
);
196 skip_spaces (char *chp
)
210 back_spaces (char *start
, char *chp
)
215 || !isspace (chp
[-1]))
222 skip_digits (char *chp
)
235 skip_to_separator (char *chp
,
240 char *sep
= separators
;
254 skip_to_null (char *chp
)
256 return skip_to_separator (chp
, "");
261 skip_to_nl (char * chp
)
263 return skip_to_separator (chp
, "\n");
268 next_line (open_table
*file
)
270 file
->pos
= skip_to_nl (file
->pos
);
271 if (*file
->pos
== '0')
272 error (&file
->pseudo_line
, "Missing <nl> at end of line\n");
275 file
->real_line
.line_nr
+= 1;
276 file
->pseudo_line
.line_nr
+= 1;
281 table_read (table
*root
)
283 open_table
*file
= root
->current
;
284 table_entry
*entry
= NULL
;
289 while (*file
->pos
== '\0')
291 if (file
->parent
!= NULL
)
294 root
->current
= file
;
301 if (*file
->pos
== '{')
304 next_line (file
); /* discard leading brace */
305 entry
= new_table_entry (file
, table_code_entry
);
307 /* determine how many lines are involved - look for <nl> "}" */
310 while (*file
->pos
!= '}')
315 set_nr_table_entry_fields (entry
, nr_lines
);
317 /* now enter each line */
320 for (line_nr
= 0; line_nr
< entry
->nr_fields
; line_nr
++)
322 if (strncmp (chp
, " ", 2) == 0)
323 entry
->field
[line_nr
] = chp
+ 2;
325 entry
->field
[line_nr
] = chp
;
326 chp
= skip_to_null (chp
) + 1;
328 /* skip trailing brace */
329 ASSERT (*file
->pos
== '}');
336 if (*file
->pos
== '\t')
338 char *chp
= file
->pos
;
339 entry
= new_table_entry (file
, table_code_entry
);
340 /* determine how many lines are involved - look for <nl> !<tab> */
343 int nr_blank_lines
= 0;
346 if (*file
->pos
== '\t')
348 nr_lines
= nr_lines
+ nr_blank_lines
+ 1;
354 file
->pos
= skip_spaces (file
->pos
);
355 if (*file
->pos
!= '\n')
361 set_nr_table_entry_fields (entry
, nr_lines
);
363 /* now enter each line */
366 for (line_nr
= 0; line_nr
< entry
->nr_fields
; line_nr
++)
369 entry
->field
[line_nr
] = chp
+ 1;
371 entry
->field
[line_nr
] = ""; /* blank */
372 chp
= skip_to_null (chp
) + 1;
379 if (file
->pos
[0] == '#')
381 char *chp
= skip_spaces (file
->pos
+ 1);
383 /* cpp line-nr directive - # <line-nr> "<file>" */
385 && *skip_digits (chp
) == ' '
386 && *skip_spaces (skip_digits (chp
)) == '"')
391 /* parse the number */
392 line_nr
= atoi(file
->pos
) - 1;
393 /* skip to the file name */
394 while (file
->pos
[0] != '0'
395 && file
->pos
[0] != '"'
396 && file
->pos
[0] != '\0')
398 if (file
->pos
[0] != '"')
399 error (&file
->real_line
, "Missing opening quote in cpp directive\n");
400 /* parse the file name */
402 file_name
= file
->pos
;
403 while (file
->pos
[0] != '"'
404 && file
->pos
[0] != '\0')
406 if (file
->pos
[0] != '"')
407 error (&file
->real_line
, "Missing closing quote in cpp directive\n");
410 file
->pos
= skip_to_nl (file
->pos
);
411 if (file
->pos
[0] != '\n')
412 error (&file
->real_line
, "Missing newline in cpp directive\n");
413 file
->pseudo_line
.file_name
= file_name
;
414 file
->pseudo_line
.line_nr
= line_nr
;
419 /* #define and #undef - not implemented yet */
421 /* Old style # comment */
426 /* blank line or end-of-file? */
427 file
->pos
= skip_spaces (file
->pos
);
428 if (*file
->pos
== '\0')
429 error (&file
->pseudo_line
, "Missing <nl> at end of file\n");
430 if (*file
->pos
== '\n')
436 /* comment - leading // or # - skip */
437 if ((file
->pos
[0] == '/' && file
->pos
[1] == '/')
438 || (file
->pos
[0] == '#'))
446 char *chp
= file
->pos
;
447 entry
= new_table_entry (file
, table_colon_entry
);
449 /* figure out how many fields */
455 tmpch
= skip_to_separator (tmpch
, "\\:");
458 /* eat the escaped character */
460 while (cp
[1] != '\0')
468 else if (*tmpch
!= ':')
477 set_nr_table_entry_fields (entry
, nr_fields
);
482 for (field_nr
= 0; field_nr
< entry
->nr_fields
; field_nr
++)
484 chp
= skip_spaces (chp
);
485 entry
->field
[field_nr
] = chp
;
486 chp
= skip_to_null (chp
);
487 *back_spaces (entry
->field
[field_nr
], chp
) = '\0';
496 ASSERT (entry
== NULL
|| entry
->field
[entry
->nr_fields
] == NULL
);
501 table_print_code (lf
*file
,
507 field_nr
< entry
->nr_fields
;
510 char *chp
= entry
->field
[field_nr
];
511 int in_bit_field
= 0;
513 lf_indent_suppress(file
);
521 nr
+= lf_putchr(file
, '_');
523 else if (in_bit_field
&& chp
[0] == ':')
525 nr
+= lf_putchr(file
, '_');
527 else if (in_bit_field
&& *chp
== '}')
529 nr
+= lf_putchr(file
, '_');
534 nr
+= lf_putchr(file
, *chp
);
540 line_ref line
= *entry
->line
;
541 line
.line_nr
+= field_nr
;
542 error (&line
, "Bit field brace miss match\n");
544 nr
+= lf_putchr(file
, '\n');
551 dump_line_ref (lf
*file
,
553 const line_ref
*line
,
556 lf_printf (file
, "%s(line_ref*) 0x%lx", prefix
, (long) line
);
559 lf_indent (file
, +1);
560 lf_printf (file
, "\n(line_nr %d)", line
->line_nr
);
561 lf_printf (file
, "\n(file_name %s)", line
->file_name
);
562 lf_indent (file
, -1);
564 lf_printf (file
, "%s", suffix
);
569 table_entry_type_to_str (table_entry_type type
)
573 case table_code_entry
: return "code-entry";
574 case table_colon_entry
: return "colon-entry";
580 dump_table_entry(lf
*file
,
582 const table_entry
*entry
,
585 lf_printf (file
, "%s(table_entry*) 0x%lx", prefix
, (long) entry
);
589 lf_indent (file
, +1);
590 dump_line_ref (file
, "\n(line ", entry
->line
, ")");
591 lf_printf (file
, "\n(type %s)", table_entry_type_to_str (entry
->type
));
592 lf_printf (file
, "\n(nr_fields %d)", entry
->nr_fields
);
593 lf_printf (file
, "\n(fields");
594 lf_indent (file
, +1);
595 for (field
= 0; field
< entry
->nr_fields
; field
++)
596 lf_printf (file
, "\n\"%s\"", entry
->field
[field
]);
597 lf_indent (file
, -1);
598 lf_printf (file
, ")");
599 lf_indent (file
, -1);
601 lf_printf (file
, "%s", suffix
);
607 main(int argc
, char **argv
)
616 printf("Usage: table <file>\n");
620 t
= table_open (argv
[1]);
621 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-table");
627 entry
= table_read (t
);
629 sprintf (line
, "(%d ", line_nr
);
630 dump_table_entry (l
, line
, entry
, ")\n");
632 while (entry
!= NULL
);
This page took 0.047223 seconds and 4 git commands to generate.