main.c 11 KB

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