main.c 9.4 KB

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