main.c 10 KB

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