main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* M A I N P R O G R A M */
  8. /* $Id$ */
  9. #include "debug.h"
  10. #include <system.h>
  11. #include <em_arith.h>
  12. #include <em_label.h>
  13. #include <em_code.h>
  14. #include <alloc.h>
  15. #include <assert.h>
  16. #include <stb.h>
  17. #include "strict3rd.h"
  18. #include "dbsymtab.h"
  19. #include "input.h"
  20. #include "f_info.h"
  21. #include "idf.h"
  22. #include "LLlex.h"
  23. #include "Lpars.h"
  24. #include "type.h"
  25. #include "def.h"
  26. #include "scope.h"
  27. #include "standards.h"
  28. #include "tokenname.h"
  29. #include "node.h"
  30. #include "warning.h"
  31. #include "SYSTEM.h"
  32. int state; /* either IMPLEMENTATION or PROGRAM */
  33. char options[128];
  34. int DefinitionModule;
  35. char *ProgName;
  36. char **DEFPATH;
  37. int nDEF = 2, mDEF = 10;
  38. int pass_1 = 1;
  39. t_def *Defined;
  40. extern int err_occurred;
  41. extern int fp_used; /* set if floating point used */
  42. static t_node _emptystat = { Stat, 0, NULLTYPE, { ';' }};
  43. t_node *EmptyStatement = &_emptystat;
  44. main(argc, argv)
  45. register char **argv;
  46. {
  47. register int Nargc = 1;
  48. register char **Nargv = &argv[0];
  49. ProgName = *argv++;
  50. DEFPATH = (char **) Malloc((unsigned)mDEF * sizeof(char *));
  51. DEFPATH[1] = 0;
  52. while (--argc > 0) {
  53. if (**argv == '-')
  54. DoOption((*argv++) + 1);
  55. else
  56. Nargv[Nargc++] = *argv++;
  57. }
  58. Nargv[Nargc] = 0; /* terminate the arg vector */
  59. if (Nargc < 2) {
  60. fprint(STDERR, "%s: Use a file argument\n", ProgName);
  61. sys_stop(S_EXIT);
  62. }
  63. sys_stop(Compile(Nargv[1], Nargv[2]) ? S_END : S_EXIT);
  64. /*NOTREACHED*/
  65. }
  66. Compile(src, dst)
  67. char *src, *dst;
  68. {
  69. extern struct tokenname tkidf[];
  70. extern char *getwdir();
  71. if (! InsertFile(src, (char **) 0, &src)) {
  72. fprint(STDERR,"%s: cannot open %s\n", ProgName, src);
  73. return 0;
  74. }
  75. LineNumber = 1;
  76. FileName = src;
  77. WorkingDir = getwdir(src);
  78. C_init(word_size, pointer_size);
  79. if (! C_open(dst)) fatal("could not open output file");
  80. C_magic();
  81. C_ms_emx(word_size, pointer_size);
  82. #ifdef DBSYMTAB
  83. if (options['g']) {
  84. C_ms_std(FileName, N_SO, 0);
  85. }
  86. #endif /* DBSYMTAB */
  87. init_idf();
  88. InitCst();
  89. reserve(tkidf);
  90. InitScope();
  91. InitTypes();
  92. AddStandards();
  93. #ifdef DEBUG
  94. if (options['t']) {
  95. LexScan();
  96. return 1;
  97. }
  98. #endif /* DEBUG */
  99. open_scope(OPENSCOPE);
  100. GlobalVis = CurrVis;
  101. close_scope(0);
  102. CheckForLineDirective();
  103. CompUnit();
  104. C_ms_src((int)LineNumber - 1, FileName);
  105. if (!err_occurred) {
  106. pass_1 = 0;
  107. C_exp(Defined->mod_vis->sc_scope->sc_name);
  108. WalkModule(Defined);
  109. if (fp_used) C_ms_flt();
  110. }
  111. C_close();
  112. #ifdef DEBUG
  113. if (options['i']) Info();
  114. #endif
  115. return ! err_occurred;
  116. }
  117. #ifdef DEBUG
  118. LexScan()
  119. {
  120. register t_token *tkp = &dot;
  121. extern char *symbol2str();
  122. while (LLlex() > 0) {
  123. print(">>> %s ", symbol2str(tkp->tk_symb));
  124. switch(tkp->tk_symb) {
  125. case IDENT:
  126. print("%s\n", tkp->TOK_IDF->id_text);
  127. break;
  128. case INTEGER:
  129. print("%ld\n", tkp->TOK_INT);
  130. break;
  131. case REAL:
  132. print("%s\n", tkp->TOK_RSTR);
  133. break;
  134. case STRING:
  135. print("\"%s\"\n", tkp->TOK_STR);
  136. break;
  137. default:
  138. print("\n");
  139. }
  140. }
  141. }
  142. #endif
  143. static struct stdproc {
  144. char *st_nam;
  145. int st_con;
  146. } stdprocs[] = {
  147. { "ABS", S_ABS },
  148. { "CAP", S_CAP },
  149. { "CHR", S_CHR },
  150. { "FLOAT", S_FLOAT },
  151. { "HIGH", S_HIGH },
  152. { "HALT", S_HALT },
  153. { "EXCL", S_EXCL },
  154. { "DEC", S_DEC },
  155. { "INC", S_INC },
  156. { "VAL", S_VAL },
  157. #ifndef STRICT_3RD_ED
  158. { "NEW", S_NEW },
  159. { "DISPOSE", S_DISPOSE },
  160. #endif
  161. { "TRUNC", S_TRUNC },
  162. { "SIZE", S_SIZE },
  163. { "ORD", S_ORD },
  164. { "ODD", S_ODD },
  165. { "MAX", S_MAX },
  166. { "MIN", S_MIN },
  167. { "INCL", S_INCL },
  168. { "LONG", S_LONG },
  169. { "SHORT", S_SHORT },
  170. { "TRUNCD", S_TRUNCD },
  171. { "FLOATD", S_FLOATD },
  172. { 0, 0 }
  173. };
  174. static struct stdproc sysprocs[] = {
  175. { "TSIZE", S_TSIZE },
  176. { "ADR", S_ADR },
  177. { 0, 0 }
  178. };
  179. extern t_def *Enter(), *EnterType();
  180. AddProcs(p)
  181. register struct stdproc *p;
  182. {
  183. for (; p->st_nam != 0; p++) {
  184. if (! Enter(p->st_nam, D_PROCEDURE, std_type, p->st_con)) {
  185. assert(0);
  186. }
  187. }
  188. }
  189. AddStandards()
  190. {
  191. register t_def *df;
  192. static t_token nilconst = { INTEGER, 0};
  193. AddProcs(stdprocs);
  194. EnterType("CHAR", char_type);
  195. EnterType("INTEGER", int_type);
  196. EnterType("LONGINT", longint_type);
  197. EnterType("REAL", real_type);
  198. EnterType("LONGREAL", longreal_type);
  199. EnterType("CARDINAL", card_type);
  200. if (options['l']) {
  201. /* local extension: LONGCARD. */
  202. EnterType("LONGCARD", longcard_type);
  203. }
  204. EnterType("(void)", void_type);
  205. df = Enter("NIL", D_CONST, address_type, 0);
  206. df->con_const = nilconst;
  207. EnterType("PROC", construct_type(T_PROCEDURE, NULLTYPE));
  208. EnterType("BITSET", bitset_type);
  209. df = Enter("FALSE", D_ENUM, bool_type, 0);
  210. bool_type->enm_enums = df;
  211. df->enm_next = Enter("TRUE", D_ENUM, bool_type, 0);
  212. df->enm_next->enm_val = 1;
  213. assert(df->enm_val == 0 && df->enm_next->enm_next == 0);
  214. EnterType("BOOLEAN", bool_type);
  215. }
  216. do_SYSTEM()
  217. {
  218. /* Simulate the reading of the SYSTEM definition module
  219. */
  220. static char systemtext[] = SYSTEMTEXT;
  221. EnterType("WORD", word_type);
  222. EnterType("BYTE", byte_type);
  223. EnterType("ADDRESS",address_type);
  224. AddProcs(sysprocs);
  225. if (!InsertText(systemtext, sizeof(systemtext) - 1)) {
  226. fatal("could not insert text");
  227. }
  228. DefModule();
  229. }
  230. #ifdef DEBUG
  231. int cntlines;
  232. Info()
  233. {
  234. extern int cnt_def, cnt_node, cnt_paramlist, cnt_type,
  235. cnt_switch_hdr, cnt_case_entry,
  236. cnt_scope, cnt_scopelist, cnt_tmpvar;
  237. print("\
  238. %6d def\n%6d node\n%6d paramlist\n%6d type\n%6d switch_hdr\n\
  239. %6d case_entry\n%6d scope\n%6d scopelist\n%6d tmpvar\n",
  240. cnt_def, cnt_node, cnt_paramlist, cnt_type,
  241. cnt_switch_hdr, cnt_case_entry,
  242. cnt_scope, cnt_scopelist, cnt_tmpvar);
  243. print("\nNumber of lines read: %d\n", cntlines);
  244. }
  245. #endif
  246. void
  247. No_Mem()
  248. {
  249. fatal("out of memory");
  250. }
  251. void
  252. C_failed()
  253. {
  254. fatal("write failed");
  255. }