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".
5 /* $Id: main.c,v 1.21 1994/06/27 08:01:33 ceriel Exp $ */
12 #include "trgt_sizes.h"
14 #include "inputtype.h"
22 #include "tokenname.h"
33 extern struct tokenname tkidf[];
34 extern char *symbol2str();
35 extern char options[128];
38 int inc_pos = 1; /* place where next -I goes */
43 extern int do_dependencies;
44 extern char *dep_file;
45 static File *dep_fd = STDOUT;
47 extern char *getwdir();
50 struct sp_id special_ids[] = {
51 {"__setjmp", SP_SETJMP}, /* non-local goto's are registered */
57 short_size = SZ_SHORT,
59 dword_size = (2 * SZ_WORD),
62 float_size = SZ_FLOAT,
63 double_size = SZ_DOUBLE,
64 lngdbl_size = SZ_LNGDBL,
65 pointer_size = SZ_POINTER;
68 short_align = AL_SHORT,
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;
81 arith ifval; /* ifval will contain the result of the #if expression */
89 /* parse and interpret the command line options */
93 inctable = (char **) Malloc(10 * sizeof(char *));
95 inctable[1] = "/usr/include";
100 init_pp(); /* initialise the preprocessor macros */
103 /* Note: source file "-" indicates that the source is supplied
104 as standard input. This is only allowed if INP_READ_IN_ONE is
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 */
113 char *par = &argv[1][1];
121 compile(argc - 1, &argv[1]);
124 if (options['h']) hash_stat();
125 if (options['m']) Info();
129 if (do_dependencies) {
132 list_dependencies(source);
135 sys_stop(err_occurred ? S_EXIT : S_END);
141 struct dependency *file_head;
142 extern char *strrindex();
144 list_dependencies(source)
147 register struct dependency *p = file_head;
150 register char *s = strrindex(source, '.');
156 /* the source may be in another directory than the
157 * object generated, so don't include the pathname
160 if (s = strrindex(source, '/')) {
166 if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
167 fatal("could not open %s", dep_file);
170 dependency(p->dep_idf->id_text, source);
178 register struct idf *p = str2idf(s, 1);
180 if (! p->id_resmac) {
181 register struct dependency *q = new_dependency();
183 p->id_resmac = K_FILE;
190 dependency(s, source)
193 if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
196 if (options['m'] && source) {
197 fprint(dep_fd, "%s: %s\n", source, s);
199 else fprint(dep_fd, "%s\n", s);
208 #endif /* GEN_NM_LIST */
215 register char *destination = 0;
220 int pp_only = options['E'] || options['P'] || options['C'];
232 fatal("%s: destination file not specified", prog_name);
238 destination = argv[1];
243 destination = argv[1];
245 #endif /* 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 */
256 fatal("use: %s source", prog_name);
261 if (strcmp(argv[0], "-"))
262 FileName = source = argv[0];
265 FileName = Salloc("standard input", (unsigned) 16);
268 if (!InsertFile(source, (char **) 0, &result)) /* read the source file */
269 fatal("%s: no source file %s\n", prog_name, FileName);
276 init_code(destination
277 && strcmp(destination, "-") != 0
283 WorkingDir = getwdir(source);
284 PushLex(); /* initialize lex machine */
291 if (pp_only) /* run the preprocessor as if it is stand-alone */
297 /* compile the source text */
300 #ifdef PREPEND_SCOPES
302 #endif /* PREPEND_SCOPES */
310 unstack_level(); /* unstack L_GLOBAL */
312 if (options['f'] || options['t'])
313 dumpidftab("end of main", options['f'] ? 7 : 0);
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 */
327 schar_type = standard_type(CHAR, 0, 1, (arith)1);
328 uchar_type = standard_type(CHAR, UNSIGNED, 1, (arith)1);
330 short_type = standard_type(SHORT, 0, short_align, short_size);
331 ushort_type = standard_type(SHORT, UNSIGNED, short_align, short_size);
333 /* Treat type `word' as `int', having its own size and
334 alignment requirements.
335 This type is transparent to the user.
337 word_type = standard_type(INT, 0, word_align, word_size);
338 uword_type = standard_type(INT, UNSIGNED, word_align, word_size);
340 int_type = standard_type(INT, 0, int_align, int_size);
341 uint_type = standard_type(INT, UNSIGNED, int_align, int_size);
343 long_type = standard_type(LONG, 0, long_align, long_size);
344 ulong_type = standard_type(LONG, UNSIGNED, long_align, long_size);
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;
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
358 if ((int)pointer_size == (int)int_size)
361 if ((int)pointer_size == (int)long_size)
364 fatal("pointer size incompatible with any integral size");
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");
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);
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);
387 register struct sp_id *si;
389 while (si->si_identifier) {
390 struct idf *idf = str2idf(si->si_identifier, 0);
393 fatal("maximum identifier length insufficient");
394 idf->id_special = si->si_flag;
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.
407 static unsigned int lastlineno = 0;
408 static char *lastfilenm = "";
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) {
418 lastlineno = dot.tk_line;
420 print("\n#line %ld \"%s\"\n",
427 lastfilenm = dot.tk_file;
428 lastlineno = dot.tk_line;
430 print("\n#line %ld \"%s\"\n",
431 lastlineno, lastfilenm);
435 if (strcmp(lastfilenm, dot.tk_file) != 0) {
436 lastfilenm = dot.tk_file;
438 print("\n#line %ld \"%s\"\n",
439 lastlineno, lastfilenm);
443 case TYPE_IDENTIFIER:
444 print("%s ", dot.tk_idf->id_text);
448 char sbuf[1024]; /* a transient buffer */
451 print("\"%s\" ", bts2str(dot.tk_bts, dot.tk_len -
456 print("%ld ", dot.tk_ival);
459 print("%s ", dot.tk_fval);
464 default: /* very expensive... */
465 print("%s ", symbol2str(DOT));
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;
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);
504 No_Mem() /* called by alloc package */
506 fatal("out of memory");
510 C_failed() /* called by EM_code module */
512 fatal("write failed");