Pristine Ack-5.5
[Ack-5.5.git] / util / int / main.c
1 /*
2         Main loop
3 */
4
5 /* $Id: main.c,v 2.3 1994/06/24 10:48:06 ceriel Exp $ */
6
7 #include        <stdio.h>
8 #include        <setjmp.h>
9
10 #include        <em_abs.h>
11 #include        "e.out.h"
12 #include        "logging.h"
13 #include        "nofloat.h"
14 #include        "global.h"
15 #include        "log.h"
16 #include        "trap.h"
17 #include        "warn.h"
18 #include        "text.h"
19 #include        "read.h"
20 #include        "opcode.h"
21 #include        "rsb.h"
22
23 extern int atoi();
24 extern long atol();
25 extern char *strcpy();
26
27 char mess_file[64] = "int.mess";        /* name of message file */
28
29 jmp_buf trapbuf;
30 char *prog_name;
31 int running;                            /* set if EM machine is running */
32
33 size maxstack;                          /* if set, max stack size */
34 size maxheap;                           /* if set, max heap size */
35
36 #ifdef  LOGGING
37 extern long inr;                        /* from log.c */
38 #endif  /* LOGGING */
39
40 PRIVATE char *dflt_av[] = {"e.out", 0}; /* default arguments */
41
42 main(argc, argv)
43         int argc;
44         char *argv[];
45 {
46         register int i;
47         register int nosetjmp = 1;
48         int must_disassemble = 0;
49         int must_tally = 0;
50         
51         prog_name = argv[0];
52
53         /* Initialize the EM machine */
54         PreIgnMask = 0;
55         FRALimit = FRALIMIT;
56         
57         for (i = 1; i < argc; i++) {
58                 if (*(argv[i]) == '-') {
59                         switch (*(argv[i] + 1)) {
60                         case 'd':       /* disassembly */
61                                 must_disassemble = 1;
62                                 break;
63                         case 'h':       /* limit heap size */
64                                 maxheap = atol(argv[i] + 2);
65                                 break;
66                         case 'I':       /* IgnMask pre-setting */
67                                 if (atoi(argv[i] + 2) < 16)
68                                         PreIgnMask = BIT(atoi(argv[i] + 2));
69                                 break;
70                         case 'm':       /* messagefile name override */
71                                 strcpy(mess_file, argv[i] + 2);
72                                 break;
73                         case 'r':       /* FRALimit override */
74                                 FRALimit = atoi(argv[i] + 2);
75                                 break;
76                         case 's':       /* limit stack size */
77                                 maxstack = atol(argv[i] + 2);
78                                 break;
79                         case 't':       /* switch on tallying */
80                                 must_tally= 1;
81                                 break;
82                         case 'W':       /* disable warning */
83                                 set_wmask(atoi(argv[i] + 2));
84                                 break;
85                         default:
86                                 fprintf(stderr,
87                                         "%s: bad option: %s\n",
88                                         prog_name,
89                                         argv[i]
90                                 );
91                                 exit(1);
92                         }
93                 }
94 #ifdef  LOGGING
95                 else if (logarg(argv[i])) {
96                         /* interesting for the logging machine */
97                 }
98 #endif  /* LOGGING */
99                 else break;
100         }
101
102 #ifdef  LOGGING
103         /* Initialize the logging machine */
104         init_log();
105 #endif  /* LOGGING */
106
107         if (argc > i)
108                 init(argc - i, argv + i);
109         else
110                 init(1, dflt_av);
111
112         /* Text dump only? */
113         if (must_disassemble) {
114                 message(
115                     "text segment disassembly produced; program was not run");
116                 disassemble();
117                 close_down(0);
118         }
119
120         /* Analyse FLAGS word */
121         if (FLAGS&FB_TEST)
122                 must_test = 1;
123
124         if ((FLAGS&FB_PROFILE) || (FLAGS&FB_FLOW) || (FLAGS&FB_COUNT))
125                 must_tally = 1;
126
127 #ifdef  NOFLOAT
128         if (FLAGS&FB_REALS)
129                 warning(WFLUSED);
130 #endif  /* NOFLOAT */
131
132         if (FLAGS&FB_EXTRA)
133                 warning(WEXTRIGN);
134
135         /* Call first procedure */
136         running = 1;                    /* start the machine */
137         OnTrap = TR_HALT;               /* default trap handling */
138         call(ENTRY, RSB_STP);
139
140         /* Run the machine */
141         while (running) {
142 #ifdef  LOGGING
143                 inr++;
144                 if (must_log && inr >= log_start) {
145                         /* log this instruction */
146                         logging = 1;
147                 }
148 #endif  /* LOGGING */
149
150                 LOG(("@x9 PC = %lu OPCODE = %lu", PC,
151                         btol(text_loc(PC)) < SECONDARY ?
152                                 btol(text_loc(PC)) :
153                                 btol(text_loc(PC)) + btol(text_loc(PC+1))
154                 ));
155
156                 newPC(PC);              /* just check for validity */
157                 do_instr(nextPCbyte()); /* here it happens */
158
159                 if (must_tally) {
160                         tally();
161                 }
162
163                 if (signalled) {
164                         /* a signal has come in during this instruction */
165                         LOG(("@t1 signal %d caught by EM machine", signalled));
166                         trap_signal();
167                 }
168
169                 if (nosetjmp) {
170                         /* entry point after a trap occurred */
171                         setjmp(trapbuf);
172                         nosetjmp = 0;
173                 }
174
175 #ifdef  LOGGING
176                 log_eoi();
177 #endif  /* LOGGING */
178         }
179         
180         if (must_tally) {
181                 out_tally();
182         }
183         
184         if (ES_def == DEFINED) {
185                 message("program exits with status %ld", ES);
186                 close_down((int) ES);
187         }
188         else {
189                 message("program exits with undefined status");
190                 close_down(0);
191         }
192         /*NOTREACHED*/
193 }
194