]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/igen/lf.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / sim / igen / lf.c
CommitLineData
feaee4bd 1/* The IGEN simulator generator for GDB, the GNU Debugger.
c906108c 2
6aba47ca 3 Copyright 2002, 2007 Free Software Foundation, Inc.
c906108c 4
feaee4bd
AC
5 Contributed by Andrew Cagney.
6
7 This file is part of GDB.
8
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.
13
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.
18
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. */
c906108c 23
c906108c
SS
24
25
26#include <stdio.h>
27#include <stdarg.h>
28#include <ctype.h>
29
30#include "config.h"
31#include "misc.h"
32#include "lf.h"
33
34#ifdef HAVE_STDLIB_H
35#include <stdlib.h>
36#endif
37
38#ifdef HAVE_STRING_H
39#include <string.h>
40#else
41#ifdef HAVE_STRINGS_H
42#include <strings.h>
43#endif
44#endif
45
4e0bf4c4
AC
46struct _lf
47{
c906108c 48 FILE *stream;
4e0bf4c4 49 int line_nr; /* nr complete lines written, curr line is line_nr+1 */
c906108c
SS
50 int indent;
51 int line_blank;
52 const char *name;
53 const char *program;
54 lf_file_references references;
55 lf_file_type type;
56};
57
58
59lf *
60lf_open (char *name,
61 char *real_name,
62 lf_file_references references,
4e0bf4c4 63 lf_file_type type, const char *program)
c906108c
SS
64{
65 /* create a file object */
4e0bf4c4 66 lf *new_lf = ZALLOC (lf);
c906108c
SS
67 ASSERT (new_lf != NULL);
68 new_lf->references = references;
69 new_lf->type = type;
70 new_lf->name = (real_name == NULL ? name : real_name);
71 new_lf->program = program;
72 /* attach to stdout if pipe */
4e0bf4c4
AC
73 if (!strcmp (name, "-"))
74 {
75 new_lf->stream = stdout;
76 }
77 else
78 {
79 /* create a new file */
80 new_lf->stream = fopen (name, "w");
81 if (new_lf->stream == NULL)
82 {
83 perror (name);
84 exit (1);
85 }
c906108c 86 }
c906108c
SS
87 return new_lf;
88}
89
90
91void
4e0bf4c4 92lf_close (lf *file)
c906108c 93{
4e0bf4c4
AC
94 if (file->stream != stdout)
95 {
96 if (fclose (file->stream))
97 {
98 perror ("lf_close.fclose");
99 exit (1);
100 }
101 free (file);
c906108c 102 }
c906108c
SS
103}
104
105
106int
4e0bf4c4 107lf_putchr (lf *file, const char chr)
c906108c
SS
108{
109 int nr = 0;
4e0bf4c4
AC
110 if (chr == '\n')
111 {
112 file->line_nr += 1;
113 file->line_blank = 1;
114 }
115 else if (file->line_blank)
116 {
117 int pad;
118 for (pad = file->indent; pad > 0; pad--)
119 putc (' ', file->stream);
120 nr += file->indent;
121 file->line_blank = 0;
122 }
123 putc (chr, file->stream);
c906108c
SS
124 nr += 1;
125 return nr;
126}
127
128int
4e0bf4c4 129lf_write (lf *file, const char *string, int strlen_string)
c906108c
SS
130{
131 int nr = 0;
132 int i;
133 for (i = 0; i < strlen_string; i++)
134 nr += lf_putchr (file, string[i]);
135 return nr;
136}
137
138
139void
4e0bf4c4 140lf_indent_suppress (lf *file)
c906108c
SS
141{
142 file->line_blank = 0;
143}
144
145
146int
4e0bf4c4 147lf_putstr (lf *file, const char *string)
c906108c
SS
148{
149 int nr = 0;
150 const char *chp;
4e0bf4c4
AC
151 if (string != NULL)
152 {
153 for (chp = string; *chp != '\0'; chp++)
154 {
155 nr += lf_putchr (file, *chp);
156 }
c906108c 157 }
c906108c
SS
158 return nr;
159}
160
161static int
4e0bf4c4 162do_lf_putunsigned (lf *file, unsigned u)
c906108c
SS
163{
164 int nr = 0;
4e0bf4c4
AC
165 if (u > 0)
166 {
167 nr += do_lf_putunsigned (file, u / 10);
168 nr += lf_putchr (file, (u % 10) + '0');
169 }
c906108c
SS
170 return nr;
171}
172
173
174int
4e0bf4c4 175lf_putint (lf *file, int decimal)
c906108c
SS
176{
177 int nr = 0;
178 if (decimal == 0)
4e0bf4c4
AC
179 nr += lf_putchr (file, '0');
180 else if (decimal < 0)
181 {
182 nr += lf_putchr (file, '-');
183 nr += do_lf_putunsigned (file, -decimal);
184 }
185 else if (decimal > 0)
186 {
187 nr += do_lf_putunsigned (file, decimal);
188 }
c906108c 189 else
4e0bf4c4 190 ASSERT (0);
c906108c
SS
191 return nr;
192}
193
194
195int
4e0bf4c4 196lf_printf (lf *file, const char *fmt, ...)
c906108c
SS
197{
198 int nr = 0;
199 char buf[1024];
200 va_list ap;
201
202 va_start (ap, fmt);
203 vsprintf (buf, fmt, ap);
204 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
205 ASSERT (strlen (buf) < sizeof (buf));
206 nr += lf_putstr (file, buf);
4e0bf4c4 207 va_end (ap);
c906108c
SS
208 return nr;
209}
210
211
212int
4e0bf4c4 213lf_print__line_ref (lf *file, line_ref *line)
c906108c
SS
214{
215 return lf_print__external_ref (file, line->line_nr, line->file_name);
216}
217
218int
4e0bf4c4 219lf_print__external_ref (lf *file, int line_nr, const char *file_name)
c906108c
SS
220{
221 int nr = 0;
222 switch (file->references)
223 {
224 case lf_include_references:
4e0bf4c4 225 lf_indent_suppress (file);
c906108c
SS
226 nr += lf_putstr (file, "#line ");
227 nr += lf_putint (file, line_nr);
228 nr += lf_putstr (file, " \"");
229 nr += lf_putstr (file, file_name);
230 nr += lf_putstr (file, "\"\n");
231 break;
232 case lf_omit_references:
233 nr += lf_putstr (file, "/* ");
234 nr += lf_putstr (file, file_name);
235 nr += lf_putstr (file, ":");
236 nr += lf_putint (file, line_nr);
237 nr += lf_putstr (file, "*/\n");
238 break;
239 }
240 return nr;
241}
242
243int
244lf_print__internal_ref (lf *file)
245{
246 int nr = 0;
4e0bf4c4 247 nr += lf_print__external_ref (file, file->line_nr + 2, file->name);
c906108c
SS
248 /* line_nr == last_line, want to number from next */
249 return nr;
250}
251
252void
253lf_indent (lf *file, int delta)
254{
255 file->indent += delta;
256}
257
258
259int
260lf_print__gnu_copyleft (lf *file)
261{
262 int nr = 0;
4e0bf4c4
AC
263 switch (file->type)
264 {
265 case lf_is_c:
266 case lf_is_h:
267 nr += lf_printf (file, "\
feaee4bd 268/* This file is part of GDB.\n\
4e62efb8 269\n\
feaee4bd 270 Copyright 2002 Free Software Foundation, Inc.\n\
4e62efb8 271\n\
feaee4bd
AC
272 This program is free software; you can redistribute it and/or modify\n\
273 it under the terms of the GNU General Public License as published by\n\
274 the Free Software Foundation; either version 2 of the License, or\n\
275 (at your option) any later version.\n\
4e62efb8 276\n\
feaee4bd
AC
277 This program is distributed in the hope that it will be useful,\n\
278 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
279 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
280 GNU General Public License for more details.\n\
4e62efb8 281 \n\
feaee4bd
AC
282 You should have received a copy of the GNU General Public License\n\
283 along with this program; if not, write to the Free Software\n\
284 Foundation, Inc., 59 Temple Place - Suite 330,\n\
285 Boston, MA 02111-1307, USA.\n\
4e62efb8 286\n\
feaee4bd 287 --\n\
4e62efb8 288\n\
feaee4bd 289 This file was generated by the program %s */\n\
4e0bf4c4
AC
290", filter_filename (file->program));
291 break;
292 default:
293 ASSERT (0);
294 break;
295 }
c906108c
SS
296 return nr;
297}
298
299
300int
4e0bf4c4 301lf_putbin (lf *file, int decimal, int width)
c906108c
SS
302{
303 int nr = 0;
304 int bit;
4e0bf4c4
AC
305 ASSERT (width > 0);
306 for (bit = 1 << (width - 1); bit != 0; bit >>= 1)
307 {
308 if (decimal & bit)
309 nr += lf_putchr (file, '1');
310 else
311 nr += lf_putchr (file, '0');
312 }
c906108c
SS
313 return nr;
314}
315
316int
4e0bf4c4 317lf_print__this_file_is_empty (lf *file, const char *reason)
c906108c
SS
318{
319 int nr = 0;
4e0bf4c4
AC
320 switch (file->type)
321 {
322 case lf_is_c:
323 case lf_is_h:
324 nr += lf_printf (file,
325 "/* This generated file (%s) is intentionally left blank",
326 file->name);
327 if (reason != NULL)
328 nr += lf_printf (file, " - %s", reason);
329 nr += lf_printf (file, " */\n");
330 break;
331 default:
332 ERROR ("Bad switch");
333 }
c906108c
SS
334 return nr;
335}
336
337int
4e0bf4c4 338lf_print__ucase_filename (lf *file)
c906108c
SS
339{
340 int nr = 0;
341 const char *chp = file->name;
4e0bf4c4
AC
342 while (*chp != '\0')
343 {
344 char ch = *chp;
345 if (islower (ch))
346 {
347 nr += lf_putchr (file, toupper (ch));
348 }
349 else if (ch == '.')
350 nr += lf_putchr (file, '_');
351 else
352 nr += lf_putchr (file, ch);
353 chp++;
c906108c 354 }
c906108c
SS
355 return nr;
356}
357
358int
4e0bf4c4 359lf_print__file_start (lf *file)
c906108c
SS
360{
361 int nr = 0;
4e0bf4c4
AC
362 switch (file->type)
363 {
364 case lf_is_h:
365 case lf_is_c:
366 nr += lf_print__gnu_copyleft (file);
367 nr += lf_printf (file, "\n");
368 nr += lf_printf (file, "#ifndef ");
369 nr += lf_print__ucase_filename (file);
370 nr += lf_printf (file, "\n");
371 nr += lf_printf (file, "#define ");
372 nr += lf_print__ucase_filename (file);
373 nr += lf_printf (file, "\n");
374 nr += lf_printf (file, "\n");
375 break;
376 default:
377 ASSERT (0);
378 }
c906108c
SS
379 return nr;
380}
381
382
383int
4e0bf4c4 384lf_print__file_finish (lf *file)
c906108c
SS
385{
386 int nr = 0;
4e0bf4c4
AC
387 switch (file->type)
388 {
389 case lf_is_h:
390 case lf_is_c:
391 nr += lf_printf (file, "\n");
392 nr += lf_printf (file, "#endif /* _");
393 nr += lf_print__ucase_filename (file);
394 nr += lf_printf (file, "_*/\n");
395 break;
396 default:
397 ASSERT (0);
398 }
c906108c
SS
399 return nr;
400}
401
402
403int
404lf_print__function_type (lf *file,
405 const char *type,
4e0bf4c4 406 const char *prefix, const char *trailing_space)
c906108c
SS
407{
408 int nr = 0;
409 nr += lf_printf (file, "%s\\\n(%s)", prefix, type);
410 if (trailing_space != NULL)
411 nr += lf_printf (file, "%s", trailing_space);
412 return nr;
413}
414
415int
416lf_print__function_type_function (lf *file,
4e0bf4c4 417 print_function * print_type,
c906108c
SS
418 const char *prefix,
419 const char *trailing_space)
420{
421 int nr = 0;
422 nr += lf_printf (file, "%s\\\n(", prefix);
423 nr += print_type (file);
424 nr += lf_printf (file, ")");
425 if (trailing_space != NULL)
426 nr += lf_printf (file, "%s", trailing_space);
427 return nr;
428}