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 3.44 1994/06/24 12:05:17 ceriel Exp $ */
12 #include "target_sizes.h"
15 #include "inputtype.h"
22 #include "tokenname.h"
27 #include "noRoption.h"
34 extern struct tokenname tkidf[], tkother[];
35 extern char *symbol2str();
36 extern char options[128];
40 int inc_pos = 1; /* place where next -I goes */
45 extern int do_dependencies;
46 extern char *dep_file;
47 static File *dep_fd = STDOUT;
49 extern char *getwdir();
52 struct sp_id special_ids[] = {
53 {"setjmp", SP_SETJMP}, /* non-local goto's are registered */
59 short_size = SZ_SHORT,
61 dword_size = (2 * SZ_WORD),
65 float_size = SZ_FLOAT,
66 double_size = SZ_DOUBLE,
68 pointer_size = SZ_POINTER;
71 short_align = AL_SHORT,
76 float_align = AL_FLOAT,
77 double_align = AL_DOUBLE,
79 pointer_align = AL_POINTER,
80 struct_align = AL_STRUCT,
81 union_align = AL_UNION;
85 arith ifval; /* ifval will contain the result of the #if expression */
93 /* parse and interpret the command line options */
98 inctable = (char **) Malloc(10 * sizeof(char *));
100 inctable[1] = "/usr/include";
105 init_pp(); /* initialise the preprocessor macros */
108 /* Note: source file "-" indicates that the source is supplied
109 as standard input. This is only allowed if INP_READ_IN_ONE is
112 #ifdef INP_READ_IN_ONE
113 while (argc > 1 && *argv[1] == '-')
114 #else /* INP_READ_IN_ONE */
115 while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0')
116 #endif /* INP_READ_IN_ONE */
118 char *par = &argv[1][1];
126 compile(argc - 1, &argv[1]);
133 if (do_dependencies) {
136 list_dependencies(source);
139 sys_stop(err_occurred ? S_EXIT : S_END);
145 struct idf *file_head;
146 extern char *strrindex();
148 list_dependencies(source)
151 register struct idf *p = file_head;
154 register char *s = strrindex(source, '.');
160 /* the source may be in another directory than the
161 * object generated, so don't include the pathname
164 if (s = strrindex(source, '/')) {
170 if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
171 fatal("could not open %s", dep_file);
174 ASSERT(p->id_resmac == K_FILE);
175 dependency(p->id_text, source);
176 p = (struct idf *) (p->id_file);
183 register struct idf *p = str2idf(s);
185 if (! p->id_resmac) {
186 p->id_resmac = K_FILE;
187 p->id_file = (char *) file_head;
192 dependency(s, source)
195 if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
198 if (options['m'] && source) {
199 fprint(dep_fd, "%s: %s\n", source, s);
201 else fprint(dep_fd, "%s\n", s);
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];
242 destination = argv[1];
248 fatal("use: %s source destination [namelist]", prog_name);
250 fatal("use: %s source", prog_name);
255 if (strcmp(argv[0], "-"))
256 FileName = source = argv[0];
259 FileName = "standard input";
262 if (!InsertFile(source, (char **) 0, &result)) /* read the source file */
263 fatal("%s: no source file %s\n", prog_name, FileName);
269 init_code(destination && strcmp(destination, "-") != 0 ?
273 WorkingDir = getwdir(source);
275 PushLex(); /* initialize lex machine */
279 if (pp_only) /* run the preprocessor as if it is stand-alone */
286 /* compile the source text */
288 #ifdef PREPEND_SCOPES
290 #endif /* PREPEND_SCOPES */
293 /* lint the source text */
299 unstack_level(); /* unstack L_GLOBAL */
301 if (options['f'] || options['t'])
302 dumpidftab("end of main", options['f'] ? 0 : 0);
310 init_cst(); /* initialize variables of "cstoper.c" */
311 reserve(tkidf); /* mark the C reserved words as such */
312 init_specials(special_ids); /* mark special ids as such */
319 char_type = standard_type(CHAR, 0, 1, (arith)1);
320 uchar_type = standard_type(CHAR, UNSIGNED, 1, (arith)1);
322 short_type = standard_type(SHORT, 0, short_align, short_size);
323 ushort_type = standard_type(SHORT, UNSIGNED, short_align, short_size);
325 /* Treat type `word' as `int', having its own size and
326 alignment requirements.
327 This type is transparent to the user.
329 word_type = standard_type(INT, 0, word_align, word_size);
330 uword_type = standard_type(INT, UNSIGNED, word_align, word_size);
332 int_type = standard_type(INT, 0, int_align, int_size);
333 uint_type = standard_type(INT, UNSIGNED, int_align, int_size);
335 long_type = standard_type(LONG, 0, long_align, long_size);
336 ulong_type = standard_type(LONG, UNSIGNED, long_align, long_size);
339 float_type = standard_type(FLOAT, 0, float_align, float_size);
340 double_type = standard_type(DOUBLE, 0, double_align, double_size);
342 void_type = standard_type(VOID, 0, 1, (arith)0);
343 label_type = standard_type(LABEL, 0, 0, (arith)0);
344 error_type = standard_type(ERRONEOUS, 0, 1, (arith)1);
346 /* Pointer Arithmetic type: all arithmetics concerning
347 pointers is supposed to be performed in the
348 pointer arithmetic type which is equal to either
349 int_type or long_type, depending on the pointer_size
351 if ((int)pointer_size == (int)word_size)
354 if ((int)pointer_size == (int)short_size)
355 pa_type = short_type;
357 if ((int)pointer_size == (int)int_size)
360 if ((int)pointer_size == (int)long_size)
363 fatal("pointer size incompatible with any integral size");
365 if ((int)int_size != (int)word_size)
366 fatal("int_size and word_size are not equal");
367 if ((int)short_size > (int)int_size || (int)int_size > (int)long_size)
368 fatal("sizes of short/int/long decreasing");
370 /* Build a type for function returning int, RM 13 */
371 funint_type = construct_type(FUNCTION, int_type, (arith)0);
372 string_type = construct_type(POINTER, char_type, (arith)0);
374 /* Define the standard type identifiers. */
375 add_def(str2idf("char"), TYPEDEF, char_type, L_UNIVERSAL);
376 add_def(str2idf("int"), TYPEDEF, int_type, L_UNIVERSAL);
378 add_def(str2idf("float"), TYPEDEF, float_type, L_UNIVERSAL);
379 add_def(str2idf("double"), TYPEDEF, double_type, L_UNIVERSAL);
381 add_def(str2idf("void"), TYPEDEF, void_type, L_UNIVERSAL);
386 register struct sp_id *si;
388 while (si->si_identifier) {
389 struct idf *idf = str2idf(si->si_identifier);
392 fatal("maximum identifier length insufficient");
393 idf->id_special = si->si_flag;
402 /* preprocess() is the "stand-alone" preprocessor which
403 consecutively calls the lexical analyzer LLlex() to get
404 the tokens and prints them in a suitable way.
406 static unsigned int lastlineno = 0;
407 static char *lastfilenm = "";
409 while (LLlex() != EOI) {
410 if (lastlineno != dot.tk_line) {
411 if (strcmp(lastfilenm, dot.tk_file) == 0) {
412 if (dot.tk_line - lastlineno <= 1) {
417 lastlineno = dot.tk_line;
419 print("\n#line %ld \"%s\"\n",
426 lastfilenm = dot.tk_file;
427 lastlineno = dot.tk_line;
429 print("\n#line %ld \"%s\"\n",
430 lastlineno, lastfilenm);
434 if (strcmp(lastfilenm, dot.tk_file) != 0) {
435 lastfilenm = dot.tk_file;
437 print("\n#line %ld \"%s\"\n",
438 lastlineno, lastfilenm);
442 case TYPE_IDENTIFIER:
443 print("%s ", dot.tk_idf->id_text);
447 char sbuf[1024]; /* a transient buffer */
450 print("\"%s\" ", bts2str(dot.tk_bts, dot.tk_len, sbuf));
454 print("%ld ", dot.tk_ival);
458 print("%s ", dot.tk_fval);
464 default: /* very expensive... */
465 print("%s ", symbol2str(DOT));
473 No_Mem() /* called by alloc package */
475 fatal("out of memory");
479 C_failed() /* called by EM_code module */
481 fatal("write failed");