main.c 8.8 KB

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