1 /* The IGEN simulator generator for GDB, the GNU Debugger.
3 Copyright 2002-2020 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/>. */
47 int line_nr
; /* nr complete lines written, curr line is line_nr+1 */
52 lf_file_references references
;
60 lf_file_references references
,
61 lf_file_type type
, const char *program
)
63 /* create a file object */
64 lf
*new_lf
= ZALLOC (lf
);
65 ASSERT (new_lf
!= NULL
);
66 new_lf
->references
= references
;
68 new_lf
->name
= (real_name
== NULL
? name
: real_name
);
69 new_lf
->program
= program
;
70 /* attach to stdout if pipe */
71 if (!strcmp (name
, "-"))
73 new_lf
->stream
= stdout
;
77 /* create a new file */
78 new_lf
->stream
= fopen (name
, "w");
79 if (new_lf
->stream
== NULL
)
90 lf_get_file_type (const lf
*file
)
99 if (file
->stream
!= stdout
)
101 if (fclose (file
->stream
))
103 perror ("lf_close.fclose");
112 lf_putchr (lf
*file
, const char chr
)
118 file
->line_blank
= 1;
120 else if (file
->line_blank
)
123 for (pad
= file
->indent
; pad
> 0; pad
--)
124 putc (' ', file
->stream
);
126 file
->line_blank
= 0;
128 putc (chr
, file
->stream
);
134 lf_write (lf
*file
, const char *string
, int strlen_string
)
138 for (i
= 0; i
< strlen_string
; i
++)
139 nr
+= lf_putchr (file
, string
[i
]);
145 lf_indent_suppress (lf
*file
)
147 file
->line_blank
= 0;
152 lf_putstr (lf
*file
, const char *string
)
158 for (chp
= string
; *chp
!= '\0'; chp
++)
160 nr
+= lf_putchr (file
, *chp
);
167 do_lf_putunsigned (lf
*file
, unsigned u
)
172 nr
+= do_lf_putunsigned (file
, u
/ 10);
173 nr
+= lf_putchr (file
, (u
% 10) + '0');
180 lf_putint (lf
*file
, int decimal
)
184 nr
+= lf_putchr (file
, '0');
185 else if (decimal
< 0)
187 nr
+= lf_putchr (file
, '-');
188 nr
+= do_lf_putunsigned (file
, -decimal
);
190 else if (decimal
> 0)
192 nr
+= do_lf_putunsigned (file
, decimal
);
201 lf_printf (lf
*file
, const char *fmt
, ...)
208 vsprintf (buf
, fmt
, ap
);
209 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
210 ASSERT (strlen (buf
) < sizeof (buf
));
211 nr
+= lf_putstr (file
, buf
);
218 lf_print__line_ref (lf
*file
, line_ref
*line
)
220 return lf_print__external_ref (file
, line
->line_nr
, line
->file_name
);
224 lf_print__external_ref (lf
*file
, int line_nr
, const char *file_name
)
227 switch (file
->references
)
229 case lf_include_references
:
230 lf_indent_suppress (file
);
231 nr
+= lf_putstr (file
, "#line ");
232 nr
+= lf_putint (file
, line_nr
);
233 nr
+= lf_putstr (file
, " \"");
234 nr
+= lf_putstr (file
, file_name
);
235 nr
+= lf_putstr (file
, "\"\n");
237 case lf_omit_references
:
238 nr
+= lf_putstr (file
, "/* ");
239 nr
+= lf_putstr (file
, file_name
);
240 nr
+= lf_putstr (file
, ":");
241 nr
+= lf_putint (file
, line_nr
);
242 nr
+= lf_putstr (file
, "*/\n");
249 lf_print__internal_ref (lf
*file
)
252 nr
+= lf_print__external_ref (file
, file
->line_nr
+ 2, file
->name
);
253 /* line_nr == last_line, want to number from next */
258 lf_indent (lf
*file
, int delta
)
260 file
->indent
+= delta
;
265 lf_print__gnu_copyleft (lf
*file
)
272 nr
+= lf_printf (file
, "\
273 /* This file is part of GDB.\n\
275 Copyright 2002, 2007 Free Software Foundation, Inc.\n\
277 This program is free software; you can redistribute it and/or modify\n\
278 it under the terms of the GNU General Public License as published by\n\
279 the Free Software Foundation; either version 3 of the License, or\n\
280 (at your option) any later version.\n\
282 This program is distributed in the hope that it will be useful,\n\
283 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
284 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
285 GNU General Public License for more details.\n\
287 You should have received a copy of the GNU General Public License\n\
288 along with this program. If not, see <http://www.gnu.org/licenses/>.\n\
292 This file was generated by the program %s */\n\
293 ", filter_filename (file
->program
));
304 lf_putbin (lf
*file
, int decimal
, int width
)
309 for (bit
= 1 << (width
- 1); bit
!= 0; bit
>>= 1)
312 nr
+= lf_putchr (file
, '1');
314 nr
+= lf_putchr (file
, '0');
320 lf_print__this_file_is_empty (lf
*file
, const char *reason
)
327 nr
+= lf_printf (file
,
328 "/* This generated file (%s) is intentionally left blank",
331 nr
+= lf_printf (file
, " - %s", reason
);
332 nr
+= lf_printf (file
, " */\n");
335 ERROR ("Bad switch");
341 lf_print__ucase_filename (lf
*file
)
344 const char *chp
= file
->name
;
350 nr
+= lf_putchr (file
, toupper (ch
));
353 nr
+= lf_putchr (file
, '_');
355 nr
+= lf_putchr (file
, ch
);
362 lf_print__file_start (lf
*file
)
369 nr
+= lf_print__gnu_copyleft (file
);
370 nr
+= lf_printf (file
, "\n");
371 nr
+= lf_printf (file
, "#ifndef ");
372 nr
+= lf_print__ucase_filename (file
);
373 nr
+= lf_printf (file
, "\n");
374 nr
+= lf_printf (file
, "#define ");
375 nr
+= lf_print__ucase_filename (file
);
376 nr
+= lf_printf (file
, "\n");
377 nr
+= lf_printf (file
, "\n");
387 lf_print__file_finish (lf
*file
)
394 nr
+= lf_printf (file
, "\n");
395 nr
+= lf_printf (file
, "#endif /* _");
396 nr
+= lf_print__ucase_filename (file
);
397 nr
+= lf_printf (file
, "_*/\n");
407 lf_print__function_type (lf
*file
,
409 const char *prefix
, const char *trailing_space
)
412 nr
+= lf_printf (file
, "%s\\\n(%s)", prefix
, type
);
413 if (trailing_space
!= NULL
)
414 nr
+= lf_printf (file
, "%s", trailing_space
);
419 lf_print__function_type_function (lf
*file
,
420 print_function
* print_type
,
422 const char *trailing_space
)
425 nr
+= lf_printf (file
, "%s\\\n(", prefix
);
426 nr
+= print_type (file
);
427 nr
+= lf_printf (file
, ")");
428 if (trailing_space
!= NULL
)
429 nr
+= lf_printf (file
, "%s", trailing_space
);