main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 <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. #include "code_c.h"
  32. #include "cstoper.h"
  33. #include <symbol2str.h>
  34. extern struct tokenname tkidf[];
  35. extern char options[128];
  36. #ifndef NOPP
  37. int inc_pos = 1; /* place where next -I goes */
  38. int inc_total = 0;
  39. int inc_max;
  40. char **inctable;
  41. extern int do_dependencies;
  42. extern char *dep_file;
  43. static File *dep_fd = STDOUT;
  44. extern char *getwdir();
  45. #endif /* NOPP */
  46. struct sp_id special_ids[] = {
  47. {"__setjmp", SP_SETJMP}, /* non-local goto's are registered */
  48. {0, 0}
  49. };
  50. #ifndef NOCROSS
  51. arith
  52. short_size = SZ_SHORT,
  53. word_size = SZ_WORD,
  54. dword_size = (2 * SZ_WORD),
  55. int_size = SZ_INT,
  56. long_size = SZ_LONG,
  57. float_size = SZ_FLOAT,
  58. double_size = SZ_DOUBLE,
  59. lngdbl_size = SZ_LNGDBL,
  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. float_align = AL_FLOAT,
  67. double_align = AL_DOUBLE,
  68. lngdbl_align = AL_LNGDBL,
  69. pointer_align = AL_POINTER,
  70. struct_align = AL_STRUCT,
  71. union_align = AL_UNION;
  72. #endif /* NOCROSS */
  73. #ifndef NOPP
  74. arith ifval; /* ifval will contain the result of the #if expression */
  75. #endif /* NOPP */
  76. char *prog_name;
  77. main(argc, argv)
  78. char *argv[];
  79. {
  80. /* parse and interpret the command line options */
  81. prog_name = argv[0];
  82. #ifndef NOPP
  83. inctable = (char **) Malloc(10 * sizeof(char *));
  84. inctable[0] = "";
  85. inctable[1] = 0;
  86. inctable[2] = 0;
  87. inc_total = 3;
  88. inc_max = 10;
  89. init_pp(); /* initialise the preprocessor macros */
  90. #endif /* NOPP */
  91. /* Note: source file "-" indicates that the source is supplied
  92. as standard input. This is only allowed if INP_READ_IN_ONE is
  93. not defined!
  94. */
  95. #ifdef INP_READ_IN_ONE
  96. while (argc > 1 && *argv[1] == '-')
  97. #else /* INP_READ_IN_ONE */
  98. while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0')
  99. #endif /* INP_READ_IN_ONE */
  100. {
  101. char *par = &argv[1][1];
  102. do_option(par);
  103. argc--, argv++;
  104. }
  105. #ifdef LINT
  106. lint_init();
  107. #endif /* LINT */
  108. compile(argc - 1, &argv[1]);
  109. #ifdef DEBUG
  110. if (options['h']) hash_stat();
  111. if (options['m']) Info();
  112. #endif /* DEBUG */
  113. #ifndef NOPP
  114. if (do_dependencies) {
  115. extern char *source;
  116. list_dependencies(source);
  117. }
  118. #endif
  119. sys_stop(err_occurred ? S_EXIT : S_END);
  120. /*NOTREACHED*/
  121. }
  122. #ifndef NOPP
  123. struct dependency *file_head;
  124. extern char *strrchr();
  125. list_dependencies(source)
  126. char *source;
  127. {
  128. register struct dependency *p = file_head;
  129. if (source) {
  130. register char *s = strrchr(source, '.');
  131. if (s && *(s+1)) {
  132. s++;
  133. *s++ = 'o';
  134. *s = '\0';
  135. /* the source may be in another directory than the
  136. * object generated, so don't include the pathname
  137. * leading to it.
  138. */
  139. if (s = strrchr(source, '/')) {
  140. source = s + 1;
  141. }
  142. }
  143. else source = 0;
  144. }
  145. if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
  146. fatal("could not open %s", dep_file);
  147. }
  148. while (p) {
  149. dependency(p->dep_idf->id_text, source);
  150. p = p->next;
  151. }
  152. }
  153. add_dependency(s)
  154. char *s;
  155. {
  156. register struct idf *p = str2idf(s, 1);
  157. if (! p->id_resmac) {
  158. register struct dependency *q = new_dependency();
  159. p->id_resmac = K_FILE;
  160. q->dep_idf = p;
  161. q->next = file_head;
  162. file_head = q;
  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. #ifdef GEN_NM_LIST
  179. char *nmlist = 0;
  180. #endif /* GEN_NM_LIST */
  181. compile(argc, argv)
  182. char *argv[];
  183. {
  184. char *result;
  185. #ifndef LINT
  186. register char *destination = 0;
  187. #endif /* LINT */
  188. #ifdef DEBUG
  189. #ifndef NOPP
  190. int pp_only = options['E'] || options['P'] || options['C'];
  191. #endif /* NOPP */
  192. #endif
  193. switch (argc) {
  194. case 1:
  195. #ifndef LINT
  196. #ifdef DEBUG
  197. #ifndef NOPP
  198. if (!pp_only)
  199. #endif /* NOPP */
  200. #endif
  201. fatal("%s: destination file not specified", prog_name);
  202. #endif /* LINT */
  203. break;
  204. #ifndef LINT
  205. case 2:
  206. destination = argv[1];
  207. break;
  208. #ifdef GEN_NM_LIST
  209. case 3:
  210. nmlist = argv[2];
  211. destination = argv[1];
  212. break;
  213. #endif /* GEN_NM_LIST */
  214. #endif /* LINT */
  215. default:
  216. #ifndef LINT
  217. #ifdef GEN_NM_LIST
  218. fatal("use: %s source destination [namelist]", prog_name);
  219. #else /* GEN_NM_LIST */
  220. fatal("use: %s source destination", prog_name);
  221. #endif /* GEN_NM_LIST */
  222. #else /* LINT */
  223. fatal("use: %s source", prog_name);
  224. #endif /* LINT */
  225. break;
  226. }
  227. if (strcmp(argv[0], "-"))
  228. FileName = source = argv[0];
  229. else {
  230. source = 0;
  231. FileName = Salloc("standard input", (unsigned) 16);
  232. }
  233. if (!InsertFile(source, (char **) 0, &result)) /* read the source file */
  234. fatal("%s: no source file %s\n", prog_name, FileName);
  235. File_Inserted = 1;
  236. init();
  237. LineNumber = 0;
  238. nestlow = -1;
  239. #ifndef LINT
  240. init_code(destination
  241. && strcmp(destination, "-") != 0
  242. ? destination
  243. : 0);
  244. #endif /* LINT */
  245. #ifndef NOPP
  246. WorkingDir = getwdir(source);
  247. PushLex(); /* initialize lex machine */
  248. #else /* NOPP */
  249. GetToken(&ahead);
  250. #endif /* NOPP */
  251. #ifdef DEBUG
  252. #ifndef NOPP
  253. if (pp_only) /* run the preprocessor as if it is stand-alone */
  254. preprocess();
  255. else
  256. #endif /* NOPP */
  257. #endif /* DEBUG */
  258. {
  259. /* compile the source text */
  260. C_program();
  261. #ifdef PREPEND_SCOPES
  262. prepend_scopes();
  263. #endif /* PREPEND_SCOPES */
  264. #ifndef LINT
  265. end_code();
  266. #endif /* LINT */
  267. #ifdef DEBUG
  268. if (options['u']) {
  269. unstack_level(); /* unstack L_GLOBAL */
  270. }
  271. if (options['f'] || options['t'])
  272. dumpidftab("end of main", options['f'] ? 7 : 0);
  273. #endif /* DEBUG */
  274. }
  275. #ifndef NOPP
  276. PopLex();
  277. #endif /* NOPP */
  278. }
  279. init()
  280. {
  281. init_cst(); /* initialize variables of "cstoper.c" */
  282. reserve(tkidf); /* mark the C reserved words as such */
  283. init_specials(special_ids); /* mark special ids as such */
  284. schar_type = standard_type(CHAR, 0, 1, (arith)1);
  285. uchar_type = standard_type(CHAR, UNSIGNED, 1, (arith)1);
  286. short_type = standard_type(SHORT, 0, short_align, short_size);
  287. ushort_type = standard_type(SHORT, UNSIGNED, short_align, short_size);
  288. /* Treat type `word' as `int', having its own size and
  289. alignment requirements.
  290. This type is transparent to the user.
  291. */
  292. word_type = standard_type(INT, 0, word_align, word_size);
  293. uword_type = standard_type(INT, UNSIGNED, word_align, word_size);
  294. int_type = standard_type(INT, 0, int_align, int_size);
  295. uint_type = standard_type(INT, UNSIGNED, int_align, int_size);
  296. long_type = standard_type(LONG, 0, long_align, long_size);
  297. ulong_type = standard_type(LONG, UNSIGNED, long_align, long_size);
  298. float_type = standard_type(FLOAT, 0, float_align, float_size);
  299. double_type = standard_type(DOUBLE, 0, double_align, double_size);
  300. lngdbl_type = standard_type(LNGDBL, 0, lngdbl_align, lngdbl_size);
  301. void_type = standard_type(VOID, 0, 1, (arith)-1);
  302. error_type = standard_type(ERRONEOUS, 0, 1, (arith)1);
  303. error_type->tp_up = error_type;
  304. /* Pointer Arithmetic type: all arithmetics concerning
  305. pointers is supposed to be performed in the
  306. pointer arithmetic type which is equal to either
  307. int_type or long_type, depending on the pointer_size
  308. */
  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. if ((int)float_size > (int)double_size || (int)double_size > (int)lngdbl_size)
  321. fatal("sizes of float/double/long double decreasing");
  322. /* Build a type for function returning int (3.3.2.2) */
  323. funint_type = construct_type(FUNCTION, int_type, 0, (arith)0, NO_PROTO);
  324. string_type = construct_type(POINTER, schar_type, 0, (arith)0, NO_PROTO);
  325. /* Define the standard type identifiers. */
  326. add_def(str2idf("char", 0), TYPEDEF, schar_type, L_UNIVERSAL);
  327. add_def(str2idf("int", 0), TYPEDEF, int_type, L_UNIVERSAL);
  328. add_def(str2idf("float", 0), TYPEDEF, float_type, L_UNIVERSAL);
  329. add_def(str2idf("double", 0), TYPEDEF, double_type, L_UNIVERSAL);
  330. add_def(str2idf("void", 0), 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, 0);
  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 -
  395. 1, sbuf));
  396. break;
  397. }
  398. case INTEGER:
  399. print("%ld ", dot.tk_ival);
  400. break;
  401. case FLOATING:
  402. print("%s ", dot.tk_fval);
  403. break;
  404. case EOI:
  405. case EOF:
  406. return;
  407. default: /* very expensive... */
  408. print("%s ", symbol2str(DOT));
  409. }
  410. }
  411. }
  412. #endif /* NOPP */
  413. Info()
  414. {
  415. extern int cnt_string_cst, cnt_formal,
  416. cnt_decl_unary, cnt_def, cnt_expr, cnt_field,
  417. cnt_e_stack, cnt_localvar, cnt_proto, cnt_repl,
  418. cnt_args, cnt_macro, cnt_stack_level,
  419. cnt_stack_entry, cnt_stmt_block, cnt_sdef, cnt_tag,
  420. cnt_switch_hdr, cnt_case_entry, cnt_type, cnt_brace,
  421. cnt_lint_stack_entry, cnt_state, cnt_auto_def,
  422. cnt_expr_state, cnt_argument;
  423. print("\
  424. %6d string_cst\n%6d formal\n\
  425. %6d decl_unary\n%6d def\n%6d expr\n%6d field\n\
  426. %6d e_stack\n%6d localvar\n%6d proto\n%6d repl\n\
  427. %6d args\n%6d macro\n%6d stack_level\n\
  428. %6d stack_entry\n%6d stmt_block\n%6d sdef\n%6d tag\n\
  429. %6d switch_hdr\n%6d case_entry\n%6d type\n%6d brace\n\
  430. %6d lint_stack_entry\n%6d state\n%6d auto_def\n\
  431. %6d expr_state\n%6d argument\n",
  432. cnt_string_cst, cnt_formal,
  433. cnt_decl_unary, cnt_def, cnt_expr, cnt_field,
  434. cnt_e_stack, cnt_localvar, cnt_proto, cnt_repl,
  435. cnt_args, cnt_macro, cnt_stack_level,
  436. cnt_stack_entry, cnt_stmt_block, cnt_sdef, cnt_tag,
  437. cnt_switch_hdr, cnt_case_entry, cnt_type, cnt_brace,
  438. cnt_lint_stack_entry, cnt_state, cnt_auto_def,
  439. cnt_expr_state, cnt_argument);
  440. }
  441. #endif /* DEBUG */
  442. void
  443. No_Mem() /* called by alloc package */
  444. {
  445. fatal("out of memory");
  446. }
  447. void
  448. C_failed() /* called by EM_code module */
  449. {
  450. fatal("write failed");
  451. }