Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / 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 /* $Id: main.c,v 1.21 1994/06/27 08:01:33 ceriel Exp $ */
6 /* MAIN PROGRAM */
7
8 #include        "lint.h"
9 #include        <system.h>
10 #include        "debug.h"
11 #include        "nopp.h"
12 #include        "trgt_sizes.h"
13 #include        "use_tmp.h"
14 #include        "inputtype.h"
15 #include        "input.h"
16 #include        "level.h"
17 #include        "idf.h"
18 #include        "arith.h"
19 #include        "type.h"
20 #include        "proto.h"
21 #include        "declar.h"
22 #include        "tokenname.h"
23 #include        "Lpars.h"
24 #include        "LLlex.h"
25 #include        <alloc.h>
26 #include        "specials.h"
27 #include        "nocross.h"
28 #include        "sizes.h"
29 #include        "align.h"
30 #include        "macro.h"
31 #include        "assert.h"
32
33 extern struct tokenname tkidf[];
34 extern char *symbol2str();
35 extern char options[128];
36
37 #ifndef NOPP
38 int inc_pos = 1;                        /* place where next -I goes */
39 int inc_total = 0;
40 int inc_max;
41 char **inctable;
42
43 extern int do_dependencies;
44 extern char *dep_file;
45 static File *dep_fd = STDOUT;
46
47 extern char *getwdir();
48 #endif /* NOPP */
49
50 struct sp_id special_ids[] =    {
51         {"__setjmp", SP_SETJMP},        /* non-local goto's are registered */
52         {0, 0}
53 };
54
55 #ifndef NOCROSS
56 arith
57         short_size = SZ_SHORT,
58         word_size = SZ_WORD,
59         dword_size = (2 * SZ_WORD),
60         int_size = SZ_INT,
61         long_size = SZ_LONG,
62         float_size = SZ_FLOAT,
63         double_size = SZ_DOUBLE,
64         lngdbl_size = SZ_LNGDBL,
65         pointer_size = SZ_POINTER;
66
67 int
68         short_align = AL_SHORT,
69         word_align = AL_WORD,
70         int_align = AL_INT,
71         long_align = AL_LONG,
72         float_align = AL_FLOAT,
73         double_align = AL_DOUBLE,
74         lngdbl_align = AL_LNGDBL,
75         pointer_align = AL_POINTER,
76         struct_align = AL_STRUCT,
77         union_align = AL_UNION;
78 #endif /* NOCROSS */
79
80 #ifndef NOPP
81 arith ifval;    /* ifval will contain the result of the #if expression  */
82 #endif /* NOPP */
83
84 char *prog_name;
85
86 main(argc, argv)
87         char *argv[];
88 {
89         /* parse and interpret the command line options */
90         prog_name = argv[0];
91
92 #ifndef NOPP
93         inctable = (char **) Malloc(10 * sizeof(char *));
94         inctable[0] = "";
95         inctable[1] = "/usr/include";
96         inctable[2] = 0;
97         inc_total = 3;
98         inc_max = 10;
99
100         init_pp();      /* initialise the preprocessor macros   */
101 #endif /* NOPP */
102
103         /*      Note: source file "-" indicates that the source is supplied
104                 as standard input.  This is only allowed if INP_READ_IN_ONE is
105                 not defined!
106         */
107 #ifdef INP_READ_IN_ONE
108         while (argc > 1 && *argv[1] == '-')
109 #else /* INP_READ_IN_ONE */
110         while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0')
111 #endif /* INP_READ_IN_ONE */
112         {
113                 char *par = &argv[1][1];
114
115                 do_option(par);
116                 argc--, argv++;
117         }
118 #ifdef  LINT
119         lint_init();
120 #endif  /* LINT */
121         compile(argc - 1, &argv[1]);
122
123 #ifdef  DEBUG
124         if (options['h']) hash_stat();
125         if (options['m']) Info();
126 #endif  /* DEBUG */
127
128 #ifndef NOPP
129         if (do_dependencies) {
130             extern char *source;
131
132             list_dependencies(source);
133         }
134 #endif
135         sys_stop(err_occurred ? S_EXIT : S_END);
136         /*NOTREACHED*/
137 }
138
139 #ifndef NOPP
140
141 struct dependency    *file_head;
142 extern char *strrindex();
143
144 list_dependencies(source)
145 char *source;
146 {
147     register struct dependency *p = file_head;
148
149     if (source) {
150         register char *s = strrindex(source, '.');
151
152         if (s && *(s+1)) {
153             s++;
154             *s++ = 'o';
155             *s = '\0';
156             /* the source may be in another directory than the
157              * object generated, so don't include the pathname
158              * leading to it.
159              */
160             if (s = strrindex(source, '/')) {
161                 source = s + 1;
162             }
163         }
164         else source = 0;
165     }
166     if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
167         fatal("could not open %s", dep_file);
168     }
169     while (p) {
170         dependency(p->dep_idf->id_text, source);
171         p = p->next;
172     }
173 }
174
175 add_dependency(s)
176 char *s;
177 {
178     register struct idf *p = str2idf(s, 1);
179
180     if (! p->id_resmac) {
181         register struct dependency *q = new_dependency();
182
183         p->id_resmac = K_FILE;
184         q->dep_idf = p;
185         q->next = file_head;
186         file_head = q;
187     }
188 }
189
190 dependency(s, source)
191 char *s, *source;
192 {
193     if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
194         return;
195     }
196     if (options['m'] && source) {
197         fprint(dep_fd, "%s: %s\n", source, s);
198     }
199     else    fprint(dep_fd, "%s\n", s);
200 }
201
202 #endif /* NOPP */
203
204 char *source = 0;
205
206 #ifdef GEN_NM_LIST
207 char *nmlist = 0;
208 #endif /* GEN_NM_LIST */
209
210 compile(argc, argv)
211         char *argv[];
212 {
213         char *result;
214 #ifndef LINT
215         register char *destination = 0;
216 #endif  /* LINT */
217
218 #ifdef DEBUG
219 #ifndef NOPP
220         int pp_only = options['E'] || options['P'] || options['C'];
221 #endif /* NOPP */
222 #endif
223
224         switch (argc) {
225         case 1:
226 #ifndef LINT
227 #ifdef DEBUG
228 #ifndef NOPP
229                 if (!pp_only)
230 #endif /* NOPP */
231 #endif
232                         fatal("%s: destination file not specified", prog_name);
233 #endif  /* LINT */
234                 break;
235
236 #ifndef LINT
237         case 2:
238                 destination = argv[1];
239                 break;
240 #ifdef GEN_NM_LIST
241         case 3:
242                 nmlist = argv[2];
243                 destination = argv[1];
244                 break;
245 #endif /* GEN_NM_LIST */
246 #endif  /* LINT */
247
248         default:
249 #ifndef LINT
250 #ifdef GEN_NM_LIST
251                 fatal("use: %s source destination [namelist]", prog_name);
252 #else /* GEN_NM_LIST */
253                 fatal("use: %s source destination", prog_name);
254 #endif /* GEN_NM_LIST */
255 #else   /* LINT */
256                 fatal("use: %s source", prog_name);
257 #endif  /* LINT */
258                 break;
259         }
260
261         if (strcmp(argv[0], "-"))
262                 FileName = source = argv[0];
263         else {
264                 source = 0;
265                 FileName = Salloc("standard input", (unsigned) 16);
266         }
267
268         if (!InsertFile(source, (char **) 0, &result)) /* read the source file  */
269                 fatal("%s: no source file %s\n", prog_name, FileName);
270         File_Inserted = 1;
271         init();
272         LineNumber = 0;
273         nestlow = -1;
274
275 #ifndef LINT
276         init_code(destination
277                   && strcmp(destination, "-") != 0
278                         ? destination
279                         : 0);
280 #endif  /* LINT */
281
282 #ifndef NOPP
283         WorkingDir = getwdir(source);
284         PushLex();                      /* initialize lex machine */
285 #else /* NOPP */
286         GetToken(&ahead);
287 #endif /* NOPP */
288
289 #ifdef DEBUG
290 #ifndef NOPP
291         if (pp_only) /* run the preprocessor as if it is stand-alone    */
292                 preprocess();
293         else
294 #endif /* NOPP */
295 #endif /* DEBUG */
296         {
297                 /* compile the source text                      */
298                 C_program();
299
300 #ifdef PREPEND_SCOPES
301                 prepend_scopes();
302 #endif /* PREPEND_SCOPES */
303
304 #ifndef LINT
305                 end_code();
306 #endif  /* LINT */
307
308 #ifdef  DEBUG
309                 if (options['u'])       {
310                         unstack_level();        /* unstack L_GLOBAL */
311                 }
312                 if (options['f'] || options['t'])
313                         dumpidftab("end of main", options['f'] ? 7 : 0);
314 #endif  /* DEBUG */
315         }
316 #ifndef NOPP
317         PopLex();
318 #endif  /* NOPP */
319 }
320
321 init()
322 {
323         init_cst();     /* initialize variables of "cstoper.c"          */
324         reserve(tkidf);         /* mark the C reserved words as such    */
325         init_specials(special_ids);     /* mark special ids as such     */
326
327         schar_type = standard_type(CHAR, 0, 1, (arith)1);
328         uchar_type = standard_type(CHAR, UNSIGNED, 1, (arith)1);
329
330         short_type = standard_type(SHORT, 0, short_align, short_size);
331         ushort_type = standard_type(SHORT, UNSIGNED, short_align, short_size);
332
333         /*      Treat type `word' as `int', having its own size and
334                 alignment requirements.
335                 This type is transparent to the user.
336         */
337         word_type = standard_type(INT, 0, word_align, word_size);
338         uword_type = standard_type(INT, UNSIGNED, word_align, word_size);
339
340         int_type = standard_type(INT, 0, int_align, int_size);
341         uint_type = standard_type(INT, UNSIGNED, int_align, int_size);
342
343         long_type = standard_type(LONG, 0, long_align, long_size);
344         ulong_type = standard_type(LONG, UNSIGNED, long_align, long_size);
345
346         float_type = standard_type(FLOAT, 0, float_align, float_size);
347         double_type = standard_type(DOUBLE, 0, double_align, double_size);
348         lngdbl_type = standard_type(LNGDBL, 0, lngdbl_align, lngdbl_size);
349         void_type = standard_type(VOID, 0, 1, (arith)-1);
350         error_type = standard_type(ERRONEOUS, 0, 1, (arith)1);
351         error_type->tp_up = error_type;
352
353         /*      Pointer Arithmetic type: all arithmetics concerning
354                 pointers is supposed to be performed in the
355                 pointer arithmetic type which is equal to either
356                 int_type or long_type, depending on the pointer_size
357         */
358         if ((int)pointer_size == (int)int_size)
359                 pa_type = int_type;
360         else
361         if ((int)pointer_size == (int)long_size)
362                 pa_type = long_type;
363         else
364                 fatal("pointer size incompatible with any integral size");
365
366         if ((int)int_size != (int)word_size)
367                 fatal("int_size and word_size are not equal");
368         if ((int)short_size > (int)int_size || (int)int_size > (int)long_size)
369                 fatal("sizes of short/int/long decreasing");
370         if ((int)float_size > (int)double_size || (int)double_size > (int)lngdbl_size)
371                 fatal("sizes of float/double/long double decreasing");
372
373         /* Build a type for function returning int (3.3.2.2) */
374         funint_type = construct_type(FUNCTION, int_type, 0, (arith)0, NO_PROTO);
375         string_type = construct_type(POINTER, schar_type, 0, (arith)0, NO_PROTO);
376
377         /* Define the standard type identifiers. */
378         add_def(str2idf("char", 0), TYPEDEF, schar_type, L_UNIVERSAL);
379         add_def(str2idf("int", 0), TYPEDEF, int_type, L_UNIVERSAL);
380         add_def(str2idf("float", 0), TYPEDEF, float_type, L_UNIVERSAL);
381         add_def(str2idf("double", 0), TYPEDEF, double_type, L_UNIVERSAL);
382         add_def(str2idf("void", 0), TYPEDEF, void_type, L_UNIVERSAL);
383         stack_level();
384 }
385
386 init_specials(si)
387         register struct sp_id *si;
388 {
389         while (si->si_identifier)       {
390                 struct idf *idf = str2idf(si->si_identifier, 0);
391
392                 if (idf->id_special)
393                         fatal("maximum identifier length insufficient");
394                 idf->id_special = si->si_flag;
395                 si++;
396         }
397 }
398
399 #ifdef DEBUG
400 #ifndef NOPP
401 preprocess()
402 {
403         /*      preprocess() is the "stand-alone" preprocessor which
404                 consecutively calls the lexical analyzer LLlex() to get
405                 the tokens and prints them in a suitable way.
406         */
407         static unsigned int lastlineno = 0;
408         static char *lastfilenm = "";
409
410         while (LLlex() !=  EOI) {
411                 if (lastlineno != dot.tk_line)  {
412                         if (strcmp(lastfilenm, dot.tk_file) == 0)       {
413                                 if (dot.tk_line - lastlineno <= 1)      {
414                                         lastlineno++;
415                                         print("\n");
416                                 }
417                                 else    {
418                                         lastlineno = dot.tk_line;
419                                         if (!options['P'])
420                                                 print("\n#line %ld \"%s\"\n",
421                                                         lastlineno,
422                                                         lastfilenm
423                                                 );
424                                 }
425                         }
426                         else    {
427                                 lastfilenm = dot.tk_file;
428                                 lastlineno = dot.tk_line;
429                                 if (!options['P'])
430                                         print("\n#line %ld \"%s\"\n",
431                                                 lastlineno, lastfilenm);
432                         }
433                 }
434                 else
435                 if (strcmp(lastfilenm, dot.tk_file) != 0)       {
436                         lastfilenm = dot.tk_file;
437                         if (!options['P'])
438                                 print("\n#line %ld \"%s\"\n",
439                                         lastlineno, lastfilenm);
440                 }
441                 switch (DOT)    {
442                 case IDENTIFIER:
443                 case TYPE_IDENTIFIER:
444                         print("%s ", dot.tk_idf->id_text);
445                         break;
446                 case STRING:
447                 {
448                         char sbuf[1024];        /* a transient buffer */
449                         char *bts2str();
450
451                         print("\"%s\" ", bts2str(dot.tk_bts, dot.tk_len -
452                         1, sbuf));
453                         break;
454                 }
455                 case INTEGER:
456                         print("%ld ", dot.tk_ival);
457                         break;
458                 case FLOATING:
459                         print("%s ", dot.tk_fval);
460                         break;
461                 case EOI:
462                 case EOF:
463                         return;
464                 default:        /* very expensive...    */
465                         print("%s ", symbol2str(DOT));
466                 }
467         }
468 }
469 #endif /* NOPP */
470
471 Info()
472 {
473         extern int cnt_string_cst, cnt_formal,
474                     cnt_decl_unary, cnt_def, cnt_expr, cnt_field,
475                     cnt_e_stack, cnt_localvar, cnt_proto, cnt_repl,
476                     cnt_args, cnt_macro, cnt_stack_level,
477                     cnt_stack_entry, cnt_stmt_block, cnt_sdef, cnt_tag,
478                     cnt_switch_hdr, cnt_case_entry, cnt_type, cnt_brace,
479                     cnt_lint_stack_entry, cnt_state, cnt_auto_def,
480                     cnt_expr_state, cnt_argument;
481         print("\
482 %6d string_cst\n%6d formal\n\
483 %6d decl_unary\n%6d def\n%6d expr\n%6d field\n\
484 %6d e_stack\n%6d localvar\n%6d proto\n%6d repl\n\
485 %6d args\n%6d macro\n%6d stack_level\n\
486 %6d stack_entry\n%6d stmt_block\n%6d sdef\n%6d tag\n\
487 %6d switch_hdr\n%6d case_entry\n%6d type\n%6d brace\n\
488 %6d lint_stack_entry\n%6d state\n%6d auto_def\n\
489 %6d expr_state\n%6d argument\n",
490         cnt_string_cst, cnt_formal,
491         cnt_decl_unary, cnt_def, cnt_expr, cnt_field,
492         cnt_e_stack, cnt_localvar, cnt_proto, cnt_repl,
493         cnt_args, cnt_macro, cnt_stack_level,
494         cnt_stack_entry, cnt_stmt_block, cnt_sdef, cnt_tag,
495         cnt_switch_hdr, cnt_case_entry, cnt_type, cnt_brace,
496         cnt_lint_stack_entry, cnt_state, cnt_auto_def,
497         cnt_expr_state, cnt_argument);
498
499
500 }
501 #endif /* DEBUG */
502
503 void
504 No_Mem()                                /* called by alloc package */
505 {
506         fatal("out of memory");
507 }
508
509 void
510 C_failed()                              /* called by EM_code module */
511 {
512         fatal("write failed");
513 }
514