1*47951Sbostic /*-
2*47951Sbostic * Copyright (c) 1980 The Regents of the University of California.
3*47951Sbostic * All rights reserved.
4*47951Sbostic *
5*47951Sbostic * %sccs.include.proprietary.c%
643214Sbostic */
743214Sbostic
843214Sbostic #ifndef lint
943214Sbostic char copyright[] =
10*47951Sbostic "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
1143214Sbostic All rights reserved.\n";
12*47951Sbostic #endif /* not lint */
1343214Sbostic
1443214Sbostic #ifndef lint
15*47951Sbostic static char sccsid[] = "@(#)main.c 5.2 (Berkeley) 04/12/91";
16*47951Sbostic #endif /* not lint */
1743214Sbostic
1843214Sbostic /*
1943214Sbostic * main.c
2043214Sbostic *
2143214Sbostic * Main routine for the f77 compiler, pass 1, 4.2 BSD.
2243214Sbostic *
2343214Sbostic * University of Utah CS Dept modification history:
2443214Sbostic *
2543214Sbostic * $Log: main.c,v $
2643214Sbostic * Revision 3.2 85/01/14 04:21:31 donn
2743214Sbostic * Added changes to implement Jerry's '-q' option.
2843214Sbostic *
2943214Sbostic * Revision 3.1 84/10/29 05:47:03 donn
3043214Sbostic * Added Jerry Berkman's change to line buffer stderr.
3143214Sbostic *
3243214Sbostic */
3343214Sbostic
3443214Sbostic char *xxxvers[] = "\n@(#) FORTRAN 77 PASS 1, VERSION 2.10, 16 AUGUST 1980\n";
3543214Sbostic
3643214Sbostic #include "defs.h"
3743214Sbostic #include <signal.h>
3843214Sbostic
3943214Sbostic #ifdef SDB
4043214Sbostic # include <a.out.h>
4143214Sbostic # ifndef N_SO
4243214Sbostic # include <stab.h>
4343214Sbostic # endif
4443214Sbostic #endif
4543214Sbostic
4643214Sbostic
4743214Sbostic LOCAL char *textname = "";
4843214Sbostic LOCAL char *asmname = "";
4943214Sbostic LOCAL char *initname = "";
5043214Sbostic
5143214Sbostic
5243214Sbostic extern intexit();
5343214Sbostic
5443214Sbostic flag namesflag = YES;
5543214Sbostic
5643214Sbostic
5743214Sbostic
main(argc,argv)5843214Sbostic main(argc, argv)
5943214Sbostic int argc;
6043214Sbostic char **argv;
6143214Sbostic {
6243214Sbostic char *s;
6343214Sbostic int k, retcode, *ip;
6443214Sbostic FILEP opf();
6543214Sbostic int flovflo();
6643214Sbostic
6743214Sbostic #define DONE(c) { retcode = c; goto finis; }
6843214Sbostic
6943214Sbostic signal(SIGFPE, flovflo); /* catch overflows */
7043214Sbostic signal(SIGINT, intexit);
7143214Sbostic
7243214Sbostic #if HERE == PDP11
7343214Sbostic ldfps(01200); /* trap on overflow */
7443214Sbostic #endif
7543214Sbostic
7643214Sbostic
7743214Sbostic setlinebuf(diagfile);
7843214Sbostic
7943214Sbostic --argc;
8043214Sbostic ++argv;
8143214Sbostic
8243214Sbostic while(argc>0 && argv[0][0]=='-')
8343214Sbostic {
8443214Sbostic for(s = argv[0]+1 ; *s ; ++s) switch(*s)
8543214Sbostic {
8643214Sbostic case 'w':
8743214Sbostic if(s[1]=='6' && s[2]=='6')
8843214Sbostic {
8943214Sbostic ftn66flag = YES;
9043214Sbostic s += 2;
9143214Sbostic }
9243214Sbostic else
9343214Sbostic nowarnflag = YES;
9443214Sbostic break;
9543214Sbostic
9643214Sbostic case 'U':
9743214Sbostic shiftcase = NO;
9843214Sbostic break;
9943214Sbostic
10043214Sbostic case 'u':
10143214Sbostic undeftype = YES;
10243214Sbostic break;
10343214Sbostic
10443214Sbostic case 'O':
10543214Sbostic optimflag = YES;
10643214Sbostic break;
10743214Sbostic
10843214Sbostic case 'd':
10943214Sbostic debugflag[0] = YES;
11043214Sbostic
11143214Sbostic while (*s == 'd' || *s == ',')
11243214Sbostic {
11343214Sbostic k = 0;
11443214Sbostic while( isdigit(*++s) )
11543214Sbostic k = 10*k + (*s - '0');
11643214Sbostic if(k < 0 || k >= MAXDEBUGFLAG)
11743214Sbostic fatali("bad debug number %d",k);
11843214Sbostic debugflag[k] = YES;
11943214Sbostic }
12043214Sbostic break;
12143214Sbostic
12243214Sbostic case 'p':
12343214Sbostic profileflag = YES;
12443214Sbostic break;
12543214Sbostic
12643214Sbostic case 'C':
12743214Sbostic checksubs = YES;
12843214Sbostic break;
12943214Sbostic
13043214Sbostic case '6':
13143214Sbostic no66flag = YES;
13243214Sbostic noextflag = YES;
13343214Sbostic break;
13443214Sbostic
13543214Sbostic case '1':
13643214Sbostic onetripflag = YES;
13743214Sbostic break;
13843214Sbostic
13943214Sbostic #ifdef SDB
14043214Sbostic case 'g':
14143214Sbostic sdbflag = YES;
14243214Sbostic break;
14343214Sbostic #endif
14443214Sbostic
14543214Sbostic case 'q':
14643214Sbostic namesflag = NO;
14743214Sbostic break;
14843214Sbostic
14943214Sbostic case 'N':
15043214Sbostic switch(*++s)
15143214Sbostic {
15243214Sbostic case 'q':
15343214Sbostic ip = &maxequiv; goto getnum;
15443214Sbostic case 'x':
15543214Sbostic ip = &maxext; goto getnum;
15643214Sbostic case 's':
15743214Sbostic ip = &maxstno; goto getnum;
15843214Sbostic case 'c':
15943214Sbostic ip = &maxctl; goto getnum;
16043214Sbostic case 'n':
16143214Sbostic ip = &maxhash; goto getnum;
16243214Sbostic
16343214Sbostic default:
16443214Sbostic fatali("invalid flag -N%c", *s);
16543214Sbostic }
16643214Sbostic getnum:
16743214Sbostic k = 0;
16843214Sbostic while( isdigit(*++s) )
16943214Sbostic k = 10*k + (*s - '0');
17043214Sbostic if(k <= 0)
17143214Sbostic fatal("Table size too small");
17243214Sbostic *ip = k;
17343214Sbostic break;
17443214Sbostic
17543214Sbostic case 'i':
17643214Sbostic if(*++s == '2')
17743214Sbostic tyint = TYSHORT;
17843214Sbostic else if(*s == '4')
17943214Sbostic {
18043214Sbostic shortsubs = NO;
18143214Sbostic tyint = TYLONG;
18243214Sbostic }
18343214Sbostic else if(*s == 's')
18443214Sbostic shortsubs = YES;
18543214Sbostic else
18643214Sbostic fatali("invalid flag -i%c\n", *s);
18743214Sbostic tylogical = tyint;
18843214Sbostic break;
18943214Sbostic
19043214Sbostic default:
19143214Sbostic fatali("invalid flag %c\n", *s);
19243214Sbostic }
19343214Sbostic --argc;
19443214Sbostic ++argv;
19543214Sbostic }
19643214Sbostic
19743214Sbostic if(argc != 4)
19843214Sbostic fatali("arg count %d", argc);
19943214Sbostic textname = argv[3];
20043214Sbostic initname = argv[2];
20143214Sbostic asmname = argv[1];
20243214Sbostic asmfile = opf(argv[1]);
20343214Sbostic initfile = opf(argv[2]);
20443214Sbostic textfile = opf(argv[3]);
20543214Sbostic
20643214Sbostic initkey();
20743214Sbostic if(inilex( copys(argv[0]) ))
20843214Sbostic DONE(1);
20943214Sbostic if(namesflag == YES)
21043214Sbostic fprintf(diagfile, "%s:\n", argv[0]);
21143214Sbostic
21243214Sbostic #ifdef SDB
21343214Sbostic filenamestab(argv[0]);
21443214Sbostic #endif
21543214Sbostic
21643214Sbostic fileinit();
21743214Sbostic procinit();
21843214Sbostic if(k = yyparse())
21943214Sbostic {
22043214Sbostic fprintf(diagfile, "Bad parse, return code %d\n", k);
22143214Sbostic DONE(1);
22243214Sbostic }
22343214Sbostic if(nerr > 0)
22443214Sbostic DONE(1);
22543214Sbostic if(parstate != OUTSIDE)
22643214Sbostic {
22743214Sbostic warn("missing END statement");
22843214Sbostic endproc();
22943214Sbostic }
23043214Sbostic doext();
23143214Sbostic preven(ALIDOUBLE);
23243214Sbostic prtail();
23343214Sbostic #if FAMILY==PCC
23443214Sbostic puteof();
23543214Sbostic #endif
23643214Sbostic
23743214Sbostic if(nerr > 0)
23843214Sbostic DONE(1);
23943214Sbostic DONE(0);
24043214Sbostic
24143214Sbostic
24243214Sbostic finis:
24343214Sbostic done(retcode);
24443214Sbostic }
24543214Sbostic
24643214Sbostic
24743214Sbostic
done(k)24843214Sbostic done(k)
24943214Sbostic int k;
25043214Sbostic {
25143214Sbostic static char *ioerror = "i/o error on intermediate file %s\n";
25243214Sbostic
25343214Sbostic if (textfile != NULL && textfile != stdout)
25443214Sbostic {
25543214Sbostic if (ferror(textfile))
25643214Sbostic {
25743214Sbostic fprintf(diagfile, ioerror, textname);
25843214Sbostic k = 3;
25943214Sbostic }
26043214Sbostic fclose(textfile);
26143214Sbostic }
26243214Sbostic
26343214Sbostic if (asmfile != NULL && asmfile != stdout)
26443214Sbostic {
26543214Sbostic if (ferror(asmfile))
26643214Sbostic {
26743214Sbostic fprintf(diagfile, ioerror, asmname);
26843214Sbostic k = 3;
26943214Sbostic }
27043214Sbostic fclose(asmfile);
27143214Sbostic }
27243214Sbostic
27343214Sbostic if (initfile != NULL && initfile != stdout)
27443214Sbostic {
27543214Sbostic if (ferror(initfile))
27643214Sbostic {
27743214Sbostic fprintf(diagfile, ioerror, initname);
27843214Sbostic k = 3;
27943214Sbostic }
28043214Sbostic fclose(initfile);
28143214Sbostic }
28243214Sbostic
28343214Sbostic rmtmpfiles();
28443214Sbostic
28543214Sbostic exit(k);
28643214Sbostic }
28743214Sbostic
28843214Sbostic
opf(fn)28943214Sbostic LOCAL FILEP opf(fn)
29043214Sbostic char *fn;
29143214Sbostic {
29243214Sbostic FILEP fp;
29343214Sbostic if( fp = fopen(fn, "w") )
29443214Sbostic return(fp);
29543214Sbostic
29643214Sbostic fatalstr("cannot open intermediate file %s", fn);
29743214Sbostic /* NOTREACHED */
29843214Sbostic }
29943214Sbostic
30043214Sbostic
30143214Sbostic
clf(p)30243214Sbostic clf(p)
30343214Sbostic FILEP *p;
30443214Sbostic {
30543214Sbostic if(p!=NULL && *p!=NULL && *p!=stdout)
30643214Sbostic {
30743214Sbostic if(ferror(*p))
30843214Sbostic fatal("writing error");
30943214Sbostic fclose(*p);
31043214Sbostic }
31143214Sbostic *p = NULL;
31243214Sbostic }
31343214Sbostic
31443214Sbostic
31543214Sbostic
31643214Sbostic
flovflo()31743214Sbostic flovflo()
31843214Sbostic {
31943214Sbostic err("floating exception during constant evaluation");
32043214Sbostic #if HERE == VAX
32143214Sbostic fatal("vax cannot recover from floating exception");
32243214Sbostic rmtmpfiles();
32343214Sbostic /* vax returns a reserved operand that generates
32443214Sbostic an illegal operand fault on next instruction,
32543214Sbostic which if ignored causes an infinite loop.
32643214Sbostic */
32743214Sbostic #endif
32843214Sbostic signal(SIGFPE, flovflo);
32943214Sbostic }
33043214Sbostic
33143214Sbostic
33243214Sbostic
rmtmpfiles()33343214Sbostic rmtmpfiles()
33443214Sbostic {
33543214Sbostic close(vdatafile);
33643214Sbostic unlink(vdatafname);
33743214Sbostic close(vchkfile);
33843214Sbostic unlink(vchkfname);
33943214Sbostic close(cdatafile);
34043214Sbostic unlink(cdatafname);
34143214Sbostic close(cchkfile);
34243214Sbostic unlink(cchkfname);
34343214Sbostic }
34443214Sbostic
34543214Sbostic
34643214Sbostic
intexit()34743214Sbostic intexit()
34843214Sbostic {
34943214Sbostic done(1);
35043214Sbostic }
351