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"
34 int line_nr
; /* nr complete lines written, curr line is line_nr+1 */
39 lf_file_references references
;
47 lf_file_references references
,
51 /* create a file object */
52 lf
*new_lf
= ZALLOC(lf
);
53 ASSERT(new_lf
!= NULL
);
54 new_lf
->references
= references
;
56 new_lf
->name
= (real_name
== NULL
? name
: real_name
);
57 new_lf
->program
= program
;
58 /* attach to stdout if pipe */
59 if (!strcmp(name
, "-")) {
60 new_lf
->stream
= stdout
;
63 /* create a new file */
64 new_lf
->stream
= fopen(name
, "w");
65 if (new_lf
->stream
== NULL
) {
77 if (file
->stream
!= stdout
) {
78 if (fclose(file
->stream
)) {
79 perror("lf_close.fclose");
96 else if (file
->line_blank
) {
98 for (pad
= file
->indent
; pad
> 0; pad
--)
99 putc(' ', file
->stream
);
101 file
->line_blank
= 0;
103 putc(chr
, file
->stream
);
109 lf_indent_suppress(lf
*file
)
111 file
->line_blank
= 0;
121 if (string
!= NULL
) {
122 for (chp
= string
; *chp
!= '\0'; chp
++) {
123 nr
+= lf_putchr(file
, *chp
);
130 do_lf_putunsigned(lf
*file
,
135 nr
+= do_lf_putunsigned(file
, u
/ 10);
136 nr
+= lf_putchr(file
, (u
% 10) + '0');
148 nr
+= lf_putchr(file
, '0');
149 else if (decimal
< 0) {
150 nr
+= lf_putchr(file
, '-');
151 nr
+= do_lf_putunsigned(file
, -decimal
);
153 else if (decimal
> 0) {
154 nr
+= do_lf_putunsigned(file
, decimal
);
172 vsprintf(buf
, fmt
, ap
);
173 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
174 ASSERT(strlen(buf
) > 0 && strlen(buf
) < sizeof(buf
));
175 nr
+= lf_putstr(file
, buf
);
182 lf_print__c_code(lf
*file
,
186 const char *chp
= code
;
187 int in_bit_field
= 0;
188 while (*chp
!= '\0') {
192 lf_indent_suppress(file
);
193 while (*chp
!= '\0' && *chp
!= '\n') {
194 if (chp
[0] == '{' && !isspace(chp
[1])) {
196 nr
+= lf_putchr(file
, '_');
198 else if (in_bit_field
&& chp
[0] == ':') {
199 nr
+= lf_putchr(file
, '_');
201 else if (in_bit_field
&& *chp
== '}') {
202 nr
+= lf_putchr(file
, '_');
206 nr
+= lf_putchr(file
, *chp
);
211 error("bit field paren miss match some where\n");
213 nr
+= lf_putchr(file
, '\n');
217 nr
+= lf_putchr(file
, '\n');
223 lf_print__external_reference(lf
*file
,
225 const char *file_name
)
228 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
:
244 lf_print__internal_reference(lf
*file
)
247 nr
+= lf_print__external_reference(file
, file
->line_nr
+2, file
->name
);
248 /* line_nr == last_line, want to number from next */
253 lf_indent(lf
*file
, int delta
)
255 file
->indent
+= delta
;
260 lf_print__gnu_copyleft(lf
*file
)
263 switch (file
->type
) {
266 nr
+= lf_printf(file
, "\n\
267 /* This file is part of the program psim.\n\
269 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>\n\
271 This program is free software; you can redistribute it and/or modify\n\
272 it under the terms of the GNU General Public License as published by\n\
273 the Free Software Foundation; either version 3 of the License, or\n\
274 (at your option) any later version.\n\
276 This program is distributed in the hope that it will be useful,\n\
277 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
278 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
279 GNU General Public License for more details.\n\
281 You should have received a copy of the GNU General Public License\n\
282 along with this program; if not, see <http://www.gnu.org/licenses/>.\n\
286 This file was generated by the program %s */\n\
287 ", filter_filename(file
->program
));
298 lf_putbin(lf
*file
, int decimal
, int width
)
303 for (bit
= 1 << (width
-1); bit
!= 0; bit
>>= 1) {
305 nr
+= lf_putchr(file
, '1');
307 nr
+= lf_putchr(file
, '0');
313 lf_print__this_file_is_empty(lf
*file
)
316 switch (file
->type
) {
319 nr
+= lf_printf(file
,
320 "/* This generated file (%s) is intentionally left blank */\n",
330 lf_print__ucase_filename(lf
*file
)
333 const char *chp
= file
->name
;
334 while (*chp
!= '\0') {
337 nr
+= lf_putchr(file
, toupper(ch
));
340 nr
+= lf_putchr(file
, '_');
342 nr
+= lf_putchr(file
, ch
);
349 lf_print__file_start(lf
*file
)
352 switch (file
->type
) {
355 nr
+= lf_print__gnu_copyleft(file
);
356 nr
+= lf_printf(file
, "\n");
357 nr
+= lf_printf(file
, "#ifndef _");
358 nr
+= lf_print__ucase_filename(file
);
359 nr
+= lf_printf(file
, "_\n");
360 nr
+= lf_printf(file
, "#define _");
361 nr
+= lf_print__ucase_filename(file
);
362 nr
+= lf_printf(file
, "_\n");
363 nr
+= lf_printf(file
, "\n");
373 lf_print__file_finish(lf
*file
)
376 switch (file
->type
) {
379 nr
+= lf_printf(file
, "\n");
380 nr
+= lf_printf(file
, "#endif /* _");
381 nr
+= lf_print__ucase_filename(file
);
382 nr
+= lf_printf(file
, "_*/\n");
392 lf_print_function_type(lf
*file
,
395 const char *trailing_space
)
398 nr
+= lf_printf(file
, "%s\\\n(%s)", prefix
, type
);
399 if (trailing_space
!= NULL
)
400 nr
+= lf_printf(file
, "%s", trailing_space
);
402 const char *type_pointer
= strrchr(type
, '*');
403 int type_pointer_offset
= (type_pointer
!= NULL
404 ? type_pointer
- type
406 if (type_pointer
== NULL
) {
407 lf_printf(file
, "%s %s", type
, prefix
);
410 char *munged_type
= (char*)zalloc(strlen(type
)
414 strcpy(munged_type
, type
);
415 munged_type
[type_pointer_offset
] = '\0';
416 if (type_pointer_offset
> 0 && type
[type_pointer_offset
-1] != ' ')
417 strcat(munged_type
, " ");
418 strcat(munged_type
, prefix
);
419 strcat(munged_type
, " ");
420 strcat(munged_type
, type
+ type_pointer_offset
);
421 lf_printf(file
, "%s", munged_type
);
424 if (trailing_space
!= NULL
&& type_pointer_offset
< strlen(type
) - 1)
425 lf_printf(file
, trailing_space
);