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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 int line_nr
; /* nr complete lines written, curr line is line_nr+1 */
49 lf_file_references references
;
57 lf_file_references references
,
61 /* create a file object */
62 lf
*new_lf
= ZALLOC(lf
);
63 ASSERT(new_lf
!= NULL
);
64 new_lf
->references
= references
;
66 new_lf
->name
= (real_name
== NULL
? name
: real_name
);
67 new_lf
->program
= program
;
68 /* attach to stdout if pipe */
69 if (!strcmp(name
, "-")) {
70 new_lf
->stream
= stdout
;
73 /* create a new file */
74 new_lf
->stream
= fopen(name
, "w");
75 if (new_lf
->stream
== NULL
) {
87 if (file
->stream
!= stdout
) {
88 if (fclose(file
->stream
)) {
89 perror("lf_close.fclose");
104 file
->line_blank
= 1;
106 else if (file
->line_blank
) {
108 for (pad
= file
->indent
; pad
> 0; pad
--)
109 putc(' ', file
->stream
);
111 file
->line_blank
= 0;
113 putc(chr
, file
->stream
);
119 lf_indent_suppress(lf
*file
)
121 file
->line_blank
= 0;
131 if (string
!= NULL
) {
132 for (chp
= string
; *chp
!= '\0'; chp
++) {
133 nr
+= lf_putchr(file
, *chp
);
140 do_lf_putunsigned(lf
*file
,
145 nr
+= do_lf_putunsigned(file
, u
/ 10);
146 nr
+= lf_putchr(file
, (u
% 10) + '0');
158 nr
+= lf_putchr(file
, '0');
159 else if (decimal
< 0) {
160 nr
+= lf_putchr(file
, '-');
161 nr
+= do_lf_putunsigned(file
, -decimal
);
163 else if (decimal
> 0) {
164 nr
+= do_lf_putunsigned(file
, decimal
);
182 vsprintf(buf
, fmt
, ap
);
183 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
184 ASSERT(strlen(buf
) > 0 && strlen(buf
) < sizeof(buf
));
185 nr
+= lf_putstr(file
, buf
);
192 lf_print__c_code(lf
*file
,
196 const char *chp
= code
;
197 int in_bit_field
= 0;
198 while (*chp
!= '\0') {
202 lf_indent_suppress(file
);
203 while (*chp
!= '\0' && *chp
!= '\n') {
204 if (chp
[0] == '{' && !isspace(chp
[1])) {
206 nr
+= lf_putchr(file
, '_');
208 else if (in_bit_field
&& chp
[0] == ':') {
209 nr
+= lf_putchr(file
, '_');
211 else if (in_bit_field
&& *chp
== '}') {
212 nr
+= lf_putchr(file
, '_');
216 nr
+= lf_putchr(file
, *chp
);
221 error("bit field paren miss match some where\n");
223 nr
+= lf_putchr(file
, '\n');
227 nr
+= lf_putchr(file
, '\n');
233 lf_print__external_reference(lf
*file
,
235 const char *file_name
)
238 switch (file
->references
) {
239 case lf_include_references
:
240 lf_indent_suppress(file
);
241 nr
+= lf_putstr(file
, "#line ");
242 nr
+= lf_putint(file
, line_nr
);
243 nr
+= lf_putstr(file
, " \"");
244 nr
+= lf_putstr(file
, file_name
);
245 nr
+= lf_putstr(file
, "\"\n");
247 case lf_omit_references
:
254 lf_print__internal_reference(lf
*file
)
257 nr
+= lf_print__external_reference(file
, file
->line_nr
+2, file
->name
);
258 /* line_nr == last_line, want to number from next */
263 lf_indent(lf
*file
, int delta
)
265 file
->indent
+= delta
;
270 lf_print__gnu_copyleft(lf
*file
)
273 switch (file
->type
) {
276 nr
+= lf_printf(file
, "\n\
277 /* This file is part of the program psim.\n\
279 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>\n\
281 This program is free software; you can redistribute it and/or modify\n\
282 it under the terms of the GNU General Public License as published by\n\
283 the Free Software Foundation; either version 2 of the License, or\n\
284 (at your option) any later version.\n\
286 This program is distributed in the hope that it will be useful,\n\
287 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
288 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
289 GNU General Public License for more details.\n\
291 You should have received a copy of the GNU General Public License\n\
292 along with this program; if not, write to the Free Software\n\
293 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\
297 This file was generated by the program %s */\n\
298 ", filter_filename(file
->program
));
309 lf_putbin(lf
*file
, int decimal
, int width
)
314 for (bit
= 1 << (width
-1); bit
!= 0; bit
>>= 1) {
316 nr
+= lf_putchr(file
, '1');
318 nr
+= lf_putchr(file
, '0');
324 lf_print__this_file_is_empty(lf
*file
)
327 switch (file
->type
) {
330 nr
+= lf_printf(file
,
331 "/* This generated file (%s) is intentionally left blank */\n",
341 lf_print__ucase_filename(lf
*file
)
344 const char *chp
= file
->name
;
345 while (*chp
!= '\0') {
348 nr
+= lf_putchr(file
, toupper(ch
));
351 nr
+= lf_putchr(file
, '_');
353 nr
+= lf_putchr(file
, ch
);
360 lf_print__file_start(lf
*file
)
363 switch (file
->type
) {
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");
384 lf_print__file_finish(lf
*file
)
387 switch (file
->type
) {
390 nr
+= lf_printf(file
, "\n");
391 nr
+= lf_printf(file
, "#endif /* _");
392 nr
+= lf_print__ucase_filename(file
);
393 nr
+= lf_printf(file
, "_*/\n");
403 lf_print_function_type(lf
*file
,
406 const char *trailing_space
)
409 nr
+= lf_printf(file
, "%s\\\n(%s)", prefix
, type
);
410 if (trailing_space
!= NULL
)
411 nr
+= lf_printf(file
, "%s", trailing_space
);
413 const char *type_pointer
= strrchr(type
, '*');
414 int type_pointer_offset
= (type_pointer
!= NULL
415 ? type_pointer
- type
417 if (type_pointer
== NULL
) {
418 lf_printf(file
, "%s %s", type
, prefix
);
421 char *munged_type
= (char*)zalloc(strlen(type
)
425 strcpy(munged_type
, type
);
426 munged_type
[type_pointer_offset
] = '\0';
427 if (type_pointer_offset
> 0 && type
[type_pointer_offset
-1] != ' ')
428 strcat(munged_type
, " ");
429 strcat(munged_type
, prefix
);
430 strcat(munged_type
, " ");
431 strcat(munged_type
, type
+ type_pointer_offset
);
432 lf_printf(file
, "%s", munged_type
);
435 if (trailing_space
!= NULL
&& type_pointer_offset
< strlen(type
) - 1)
436 lf_printf(file
, trailing_space
);