home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
gnu
/
f2c-1993.04.28-src.lha
/
f2c-1993.04.28
/
src
/
format.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-28
|
53KB
|
2,226 lines
/****************************************************************
Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T Bell Laboratories or
Bellcore or any of their entities not be used in advertising or
publicity pertaining to distribution of the software without
specific, written prior permission.
AT&T and Bellcore disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall AT&T or Bellcore be liable for
any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
****************************************************************/
/* Format.c -- this file takes an intermediate file (generated by pass 1
of the translator) and some state information about the contents of that
file, and generates C program text. */
#include "defs.h"
#include "p1defs.h"
#include "format.h"
#include "output.h"
#include "names.h"
#include "iob.h"
int c_output_line_length = DEF_C_LINE_LENGTH;
int last_was_label; /* Boolean used to generate semicolons
when a label terminates a block */
static char this_proc_name[52]; /* Name of the current procedure. This is
probably too simplistic to handle
multiple entry points */
static int p1getd(), p1gets(), p1getf(), get_p1_token();
static int p1get_const(), p1getn();
static expptr do_format(), do_p1_name_pointer(), do_p1_const();
static expptr do_p1_expr(), do_p1_ident(), do_p1_charp(), do_p1_extern();
static expptr do_p1_head(), do_p1_list(), do_p1_literal();
static void do_p1_label(), do_p1_asgoto(), do_p1_goto();
static void do_p1_if(), do_p1_else(), do_p1_elif(), do_p1_endif();
static void do_p1_endelse(), do_p1_subr_ret(), do_p1_comp_goto();
static void do_p1_for(), do_p1_end_for(), do_p1_fortran();
static void do_p1_1while(), do_p1_2while(), do_p1_elseifstart();
static void do_p1_comment(), do_p1_set_line();
static expptr do_p1_addr();
static void proto();
void list_arg_types();
chainp length_comp();
void listargs();
extern chainp assigned_fmts;
static char filename[P1_FILENAME_MAX];
extern int gflag;
int gflag1;
extern char *parens;
start_formatting ()
{
FILE *infile;
static int wrote_one = 0;
extern int usedefsforcommon;
extern char *p1_file, *p1_bakfile;
this_proc_name[0] = '\0';
last_was_label = 0;
ei_next = ei_first;
wh_next = wh_first;
(void) fclose (pass1_file);
if ((infile = fopen (p1_file, binread)) == NULL)
Fatal("start_formatting: couldn't open the intermediate file\n");
if (wrote_one)
nice_printf (c_file, "\n");
while (!feof (infile)) {
expptr this_expr;
this_expr = do_format (infile, c_file);
if (this_expr) {
out_and_free_statement (c_file, this_expr);
} /* if this_expr */
} /* while !feof infile */
(void) fclose (infile);
if (last_was_label)
nice_printf (c_file, ";\n");
prev_tab (c_file);
gflag1 = 0;
if (this_proc_name[0])
nice_printf (c_file, "} /* %s */\n", this_proc_name);
/* Write the #undefs for common variable reference */
if (usedefsforcommon) {
Extsym *ext;
int did_one = 0;
for (ext = extsymtab; ext < nextext; ext++)
if (ext -> extstg == STGCOMMON && ext -> used_here) {
ext -> used_here = 0;
if (!did_one)
nice_printf (c_file, "\n");
wr_abbrevs(c_file, 0, ext->extp);
did_one = 1;
ext -> extp = CHNULL;
} /* if */
if (did_one)
nice_printf (c_file, "\n");
} /* if usedefsforcommon */
other_undefs(c_file);
wrote_one = 1;
/* For debugging only */
if (debugflag && (pass1_file = fopen (p1_bakfile, binwrite)))
if (infile = fopen (p1_file, binread)) {
ffilecopy (infile, pass1_file);
fclose (infile);
fclose (pass1_file);
} /* if infile */
/* End of "debugging only" */
scrub(p1_file); /* optionally unlink */
if ((pass1_file = fopen (p1_file, binwrite)) == NULL)
err ("start_formatting: couldn't reopen the pass1 file");
} /* start_formatting */
static void
put_semi(outfile)
FILE *outfile;
{
nice_printf (outfile, ";\n");
last_was_label = 0;
}
#define SEM_CHECK(x) if (last_was_label) put_semi(x)
/* do_format -- takes an input stream (a file in pass1 format) and writes
the appropriate C code to outfile when possible. When reading an
expression, the expression tree is returned instead. */
static expptr do_format (infile, outfile)
FILE *infile, *outfile;
{
int token_type, was_c_token;
expptr retval = ENULL;
token_type = get_p1_token (infile);
was_c_token = 1;
switch (token_type) {
case P1_COMMENT:
do_p1_comment (infile, outfile);
was_c_token = 0;
break;
case P1_SET_LINE:
do_p1_set_line (infile);
was_c_token = 0;
break;
case P1_FILENAME:
p1gets(infile, filename, P1_FILENAME_MAX);
was_c_token = 0;
break;
case P1_NAME_POINTER:
retval = do_p1_name_pointer (infile);
break;
case P1_CONST:
retval = do_p1_const (infile);
break;
case P1_EXPR:
retval = do_p1_expr (infile, outfile);
break;
case P1_IDENT:
retval = do_p1_ident(infile);
break;
case P1_CHARP:
retval = do_p1_charp(infile);
break;
case P1_EXTERN:
retval = do_p1_extern (infile);
break;
case P1_HEAD:
gflag1 = 0;
retval = do_p1_head (infile, outfile);
gflag1 = gflag;
break;
case P1_LIST:
retval = do_p1_list (infile, outfile);
break;
case P1_LITERAL:
retval = do_p1_literal (infile);
break;
case P1_LABEL:
do_p1_label (infile, outfile);
/* last_was_label = 1; -- now set in do_p1_label */
was_c_token = 0;
break;
case P1_ASGOTO:
do_p1_asgoto (infile, outfile);
break;
case P1_GOTO:
do_p1_goto (infile, outfile);
break;
case P1_IF:
do_p1_if (infile, outfile);
break;
case P1_ELSE:
SEM_CHECK(outfile);
do_p1_else (outfile);
break;
case P1_ELIF:
SEM_CHECK(outfile);
do_p1_elif (infile, outfile);
break;
case P1_ENDIF:
SEM_CHECK(outfile);
do_p1_endif (outfile);
break;
case P1_ENDELSE:
SEM_CHECK(outfile);
do_p1_endelse (outfile);
break;
case P1_ADDR:
retval = do_p1_addr (infile, outfile);
break;
case P1_SUBR_RET:
do_p1_subr_ret (infile, outfile);
break;
case P1_COMP_GOTO:
do_p1_comp_goto (infile, outfile);
break;
case P1_FOR:
do_p1_for (infile, outfile);
break;
case P1_ENDFOR:
SEM_CHECK(outfile);
do_p1_end_for (outfile);
break;
case P1_WHILE1START:
do_p1_1while(outfile);
break;
case P1_WHILE2START:
do_p1_2while(infile, outfile);
break;
case P1_PROCODE:
procode(outfile);
break;
case P1_ELSEIFSTART:
SEM_CHECK(outfile);
do_p1_elseifstart(outfile);
break;
case P1_FORTRAN:
do_p1_fortran(infile, outfile);
/* no break; */
case P1_EOF:
was_c_token = 0;
break;
case P1_UNKNOWN:
Fatal("do_format: Unknown token type in intermediate file");
break;
default:
Fatal("do_format: Bad token type in intermediate file");
break;
} /* switch */
if (was_c_token)
last_was_label = 0;
return retval;
} /* do_format */
static void
do_p1_comment (infile, outfile)
FILE *infile, *outfile;
{
extern int c_output_line_length, in_comment;
char storage[COMMENT_BUFFER_SIZE + 1];
int length;
if (!p1gets(infile, storage, COMMENT_BUFFER_SIZE + 1))
return;
length = strlen (storage);
gflag1 = 0;
in_comment = 1;
if (length > c_output_line_length - 6)
margin_printf (outfile, "/*%s*/\n", storage);
else
margin_printf (outfile, length ? "/* %s */\n" : "\n", storage);
in_comment =