main.c 8.7 KB

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