Pristine Ack-5.5
[Ack-5.5.git] / modules / src / em_opt / main.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 #ifndef NORCSID
6 static char rcsid1[] = "$Id: main.c,v 2.8 1994/06/24 11:14:10 ceriel Exp $";
7 #endif
8
9 /*      This is the main program for the stand-alone version of the
10         peephole optimizer.
11 */
12
13 #include        "nopt.h"
14
15 char *filename;                 /* Name of input file */
16 int errors;                     /* Number of errors */
17
18 main(argc,argv)
19         char **argv;
20 {
21         static struct e_instr buff;
22         register p_instr p = &buff;
23
24         if (argc >= 2) {
25                 filename = argv[1];
26         }
27         else    filename = 0;
28         if (!EM_open(filename)) {
29                 fatal(EM_error);
30         }
31         EM_getinstr(p);
32         O_init((arith) EM_wordsize, (arith) EM_pointersize);
33         if (argc >= 3) {
34                 if (!O_open(argv[2])) {
35                         fatal("O_open failed");
36                 }
37         }
38         else    if (!O_open( (char *) 0)) fatal("O_open failed");
39         O_magic();
40         C_out(p);
41
42         for(;;) {
43                 EM_getinstr(p=GETNXTPATT());
44                 switch(p->em_type) {
45                 case EM_DEFILB:
46                         p->em_opcode=op_lab;
47                         break;
48                 case EM_MNEM:
49                         switch(p->em_argtype) {
50                         case sof_ptyp:
51                                 p->em_dnam = OO_freestr(p->em_dnam);
52                                 break;
53                         case pro_ptyp:
54                                 p->em_pnam = OO_freestr(p->em_pnam);
55                                 break;
56                         case str_ptyp:
57                         case ico_ptyp:
58                         case uco_ptyp:
59                         case fco_ptyp:
60                                 p->em_string = OO_freestr(p->em_string);
61                                 break;
62                         }
63                         break;
64                 case EM_PSEU:
65                         switch(p->em_opcode) {
66                         case ps_pro:
67                         case ps_end:
68                                 break;
69                         default:
70                                 C_out(p);
71                                 OO_nxtpatt--;
72                                 continue;
73                         }
74                         break;
75                 default:
76                         C_out(p);
77                         OO_nxtpatt--;
78                         continue;
79                 case EM_EOF:
80                         goto got_eof;
81                 case EM_ERROR:
82                         error("%s", EM_error);
83                         continue;
84                 case EM_FATAL:
85                         fatal("%s", EM_error);
86                 }
87                 OO_dfa(p->em_opcode);
88         }
89  got_eof:
90         O_close();
91         EM_close();
92         exit(errors);
93 }
94
95 /*VARARGS1*/
96 error(s,a1,a2,a3,a4)
97         char *s;
98 {
99         fprintf(stderr,
100                 "%s, line %d: ",
101                 filename ? filename : "standard input",
102                 EM_lineno);
103         fprintf(stderr,s,a1,a2,a3,a4);
104         fprintf(stderr, "\n");
105         errors++;
106 }
107
108 /*VARARGS1*/
109 fatal(s,a1,a2,a3,a4)
110         char *s;
111 {
112         error(s,a1,a2,a3,a4);
113         exit(1);
114 }