main.c 8.2 KB

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