1 /* This file is part of the program psim.
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "build-config.h"
43 int line_nr; /* nr complete lines written, curr line is line_nr+1 */
48 lf_file_references references;
56 lf_file_references references,
60 /* create a file object */
61 lf *new_lf = ZALLOC(lf);
62 ASSERT(new_lf != NULL);
63 new_lf->references = references;
65 new_lf->name = (real_name == NULL ? name : real_name);
66 new_lf->program = program;
67 /* attach to stdout if pipe */
68 if (!strcmp(name, "-")) {
69 new_lf->stream = stdout;
72 /* create a new file */
73 new_lf->stream = fopen(name, "w");
74 if (new_lf->stream == NULL) {
86 if (file->stream != stdout) {
87 if (fclose(file->stream)) {
88 perror("lf_close.fclose");
103 file->line_blank = 1;
105 else if (file->line_blank) {
107 for (pad = file->indent; pad > 0; pad--)
108 putc(' ', file->stream);
110 file->line_blank = 0;
112 putc(chr, file->stream);
118 lf_indent_suppress(lf *file)
120 file->line_blank = 0;
130 if (string != NULL) {
131 for (chp = string; *chp != '\0'; chp++) {
132 nr += lf_putchr(file, *chp);
139 do_lf_putunsigned(lf *file,
144 nr += do_lf_putunsigned(file, u / 10);
145 nr += lf_putchr(file, (u % 10) + '0');
157 nr += lf_putchr(file, '0');
158 else if (decimal < 0) {
159 nr += lf_putchr(file, '-');
160 nr += do_lf_putunsigned(file, -decimal);
162 else if (decimal > 0) {
163 nr += do_lf_putunsigned(file, decimal);
181 vsprintf(buf, fmt, ap);
182 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
183 ASSERT(strlen(buf) > 0 && strlen(buf) < sizeof(buf));
184 nr += lf_putstr(file, buf);
191 lf_print__c_code(lf *file,
195 const char *chp = code;
196 int in_bit_field = 0;
197 while (*chp != '\0') {
201 lf_indent_suppress(file);
202 while (*chp != '\0' && *chp != '\n') {
203 if (chp[0] == '{' && !isspace(chp[1])) {
205 nr += lf_putchr(file, '_');
207 else if (in_bit_field && chp[0] == ':') {
208 nr += lf_putchr(file, '_');
210 else if (in_bit_field && *chp == '}') {
211 nr += lf_putchr(file, '_');
215 nr += lf_putchr(file, *chp);
220 error("bit field paren miss match some where\n");
222 nr += lf_putchr(file, '\n');
226 nr += lf_putchr(file, '\n');
232 lf_print__external_reference(lf *file,
234 const char *file_name)
237 switch (file->references) {
238 case lf_include_references:
239 lf_indent_suppress(file);
240 nr += lf_putstr(file, "#line ");
241 nr += lf_putint(file, line_nr);
242 nr += lf_putstr(file, " \"");
243 nr += lf_putstr(file, file_name);
244 nr += lf_putstr(file, "\"\n");
246 case lf_omit_references:
253 lf_print__internal_reference(lf *file)
256 nr += lf_print__external_reference(file, file->line_nr+2, file->name);
257 /* line_nr == last_line, want to number from next */
262 lf_indent(lf *file, int delta)
264 file->indent += delta;
269 lf_print__gnu_copyleft(lf *file)
272 switch (file->type) {
275 nr += lf_printf(file, "\n\
276 /* This file is part of the program psim.\n\
278 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>\n\
280 This program is free software; you can redistribute it and/or modify\n\
281 it under the terms of the GNU General Public License as published by\n\
282 the Free Software Foundation; either version 3 of the License, or\n\
283 (at your option) any later version.\n\
285 This program is distributed in the hope that it will be useful,\n\
286 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
287 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
288 GNU General Public License for more details.\n\
290 You should have received a copy of the GNU General Public License\n\
291 along with this program; if not, see <http://www.gnu.org/licenses/>.\n\
295 This file was generated by the program %s */\n\
296 ", filter_filename(file->program));
307 lf_putbin(lf *file, int decimal, int width)
312 for (bit = 1 << (width-1); bit != 0; bit >>= 1) {
314 nr += lf_putchr(file, '1');
316 nr += lf_putchr(file, '0');
322 lf_print__this_file_is_empty(lf *file)
325 switch (file->type) {
328 nr += lf_printf(file,
329 "/* This generated file (%s) is intentionally left blank */\n",
339 lf_print__ucase_filename(lf *file)
342 const char *chp = file->name;
343 while (*chp != '\0') {
346 nr += lf_putchr(file, toupper(ch));
349 nr += lf_putchr(file, '_');
351 nr += lf_putchr(file, ch);
358 lf_print__file_start(lf *file)
361 switch (file->type) {
364 nr += lf_print__gnu_copyleft(file);
365 nr += lf_printf(file, "\n");
366 nr += lf_printf(file, "#ifndef _");
367 nr += lf_print__ucase_filename(file);
368 nr += lf_printf(file, "_\n");
369 nr += lf_printf(file, "#define _");
370 nr += lf_print__ucase_filename(file);
371 nr += lf_printf(file, "_\n");
372 nr += lf_printf(file, "\n");
382 lf_print__file_finish(lf *file)
385 switch (file->type) {
388 nr += lf_printf(file, "\n");
389 nr += lf_printf(file, "#endif /* _");
390 nr += lf_print__ucase_filename(file);
391 nr += lf_printf(file, "_*/\n");
401 lf_print_function_type(lf *file,
404 const char *trailing_space)
407 nr += lf_printf(file, "%s\\\n(%s)", prefix, type);
408 if (trailing_space != NULL)
409 nr += lf_printf(file, "%s", trailing_space);
411 const char *type_pointer = strrchr(type, '*');
412 int type_pointer_offset = (type_pointer != NULL
413 ? type_pointer - type
415 if (type_pointer == NULL) {
416 lf_printf(file, "%s %s", type, prefix);
419 char *munged_type = (char*)zalloc(strlen(type)
423 strcpy(munged_type, type);
424 munged_type[type_pointer_offset] = '\0';
425 if (type_pointer_offset > 0 && type[type_pointer_offset-1] != ' ')
426 strcat(munged_type, " ");
427 strcat(munged_type, prefix);
428 strcat(munged_type, " ");
429 strcat(munged_type, type + type_pointer_offset);
430 lf_printf(file, "%s", munged_type);
433 if (trailing_space != NULL && type_pointer_offset < strlen(type) - 1)
434 lf_printf(file, trailing_space);