main.c 9.9 KB

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