main.c 8.0 KB

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