]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/igen/lf.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / sim / igen / lf.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3 Copyright 2002, 2007 Free Software Foundation, Inc.
4
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. */
23
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
46 struct _lf
47 {
48 FILE *stream;
49 int line_nr; /* nr complete lines written, curr line is line_nr+1 */
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
59 lf *
60 lf_open (char *name,
61 char *real_name,
62 lf_file_references references,
63 lf_file_type type, const char *program)
64 {
65 /* create a file object */
66 lf *new_lf = ZALLOC (lf);
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 */
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 }
86 }
87 return new_lf;
88 }
89
90
91 void
92 lf_close (lf *file)
93 {
94 if (file->stream != stdout)
95 {
96 if (fclose (file->stream))
97 {
98 perror ("lf_close.fclose");
99 exit (1);
100 }
101 free (file);
102 }
103 }
104
105
106 int
107 lf_putchr (lf *file, const char chr)
108 {
109 int nr = 0;
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);
124 nr += 1;
125 return nr;
126 }
127
128 int
129 lf_write (lf *file, const char *string, int strlen_string)
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
139 void
140 lf_indent_suppress (lf *file)
141 {
142 file->line_blank = 0;
143 }
144
145
146 int
147 lf_putstr (lf *file, const char *string)
148 {
149 int nr = 0;
150 const char *chp;
151 if (string != NULL)
152 {
153 for (chp = string; *chp != '\0'; chp++)
154 {
155 nr += lf_putchr (file, *chp);
156 }
157 }
158 return nr;
159 }
160
161 static int
162 do_lf_putunsigned (lf *file, unsigned u)
163 {
164 int nr = 0;
165 if (u > 0)
166 {
167 nr += do_lf_putunsigned (file, u / 10);
168 nr += lf_putchr (file, (u % 10) + '0');
169 }
170 return nr;
171 }
172
173
174 int
175 lf_putint (lf *file, int decimal)
176 {
177 int nr = 0;
178 if (decimal == 0)
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 }
189 else
190 ASSERT (0);
191 return nr;
192 }
193
194
195 int
196 lf_printf (lf *file, const char *fmt, ...)
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);
207 va_end (ap);
208 return nr;
209 }
210
211
212 int
213 lf_print__line_ref (lf *file, line_ref *line)
214 {
215 return lf_print__external_ref (file, line->line_nr, line->file_name);
216 }
217
218 int
219 lf_print__external_ref (lf *file, int line_nr, const char *file_name)
220 {
221 int nr = 0;
222 switch (file->references)
223 {
224 case lf_include_references:
225 lf_indent_suppress (file);
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
243 int
244 lf_print__internal_ref (lf *file)
245 {
246 int nr = 0;
247 nr += lf_print__external_ref (file, file->line_nr + 2, file->name);
248 /* line_nr == last_line, want to number from next */
249 return nr;
250 }
251
252 void
253 lf_indent (lf *file, int delta)
254 {
255 file->indent += delta;
256 }
257
258
259 int
260 lf_print__gnu_copyleft (lf *file)
261 {
262 int nr = 0;
263 switch (file->type)
264 {
265 case lf_is_c:
266 case lf_is_h:
267 nr += lf_printf (file, "\
268 /* This file is part of GDB.\n\
269 \n\
270 Copyright 2002 Free Software Foundation, Inc.\n\
271 \n\
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\
276 \n\
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\
281 \n\
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\
286 \n\
287 --\n\
288 \n\
289 This file was generated by the program %s */\n\
290 ", filter_filename (file->program));
291 break;
292 default:
293 ASSERT (0);
294 break;
295 }
296 return nr;
297 }
298
299
300 int
301 lf_putbin (lf *file, int decimal, int width)
302 {
303 int nr = 0;
304 int bit;
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 }
313 return nr;
314 }
315
316 int
317 lf_print__this_file_is_empty (lf *file, const char *reason)
318 {
319 int nr = 0;
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 }
334 return nr;
335 }
336
337 int
338 lf_print__ucase_filename (lf *file)
339 {
340 int nr = 0;
341 const char *chp = file->name;
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++;
354 }
355 return nr;
356 }
357
358 int
359 lf_print__file_start (lf *file)
360 {
361 int nr = 0;
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 }
379 return nr;
380 }
381
382
383 int
384 lf_print__file_finish (lf *file)
385 {
386 int nr = 0;
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 }
399 return nr;
400 }
401
402
403 int
404 lf_print__function_type (lf *file,
405 const char *type,
406 const char *prefix, const char *trailing_space)
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
415 int
416 lf_print__function_type_function (lf *file,
417 print_function * print_type,
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 }