ic_lib.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* I N T E R M E D I A T E C O D E
  7. *
  8. * I C _ L I B . C
  9. */
  10. #include <stdio.h>
  11. #include <em_spec.h>
  12. #include <em_pseu.h>
  13. #include <em_mes.h>
  14. #include <arch.h>
  15. #include "../share/types.h"
  16. #include "../share/debug.h"
  17. #include "ic.h"
  18. #include "ic_lookup.h"
  19. #include "ic_io.h"
  20. #include "../share/global.h"
  21. #include "../share/files.h"
  22. #include "ic_lib.h"
  23. STATIC skip_string(n)
  24. offset n;
  25. {
  26. /* Read a string of length n and void it */
  27. while (n--)
  28. {
  29. readchar();
  30. }
  31. }
  32. STATIC skip_arguments()
  33. {
  34. /* Skip the arguments of a MES pseudo. The argument
  35. * list is terminated by a sp_cend byte.
  36. */
  37. for (;;)
  38. {
  39. switch (table2())
  40. {
  41. case sp_scon:
  42. get_off(); /* void */
  43. /* fall through !!! */
  44. case sp_icon:
  45. case sp_ucon:
  46. case sp_fcon:
  47. get_int(); /* void */
  48. skip_string(get_off());
  49. break;
  50. case sp_cend:
  51. return;
  52. default:
  53. break;
  54. }
  55. }
  56. }
  57. STATIC bool proc_wanted(name) char* name;
  58. {
  59. /* See if 'name' is the name of an external procedure
  60. * that has been used before, but for which no body
  61. * has been given so far.
  62. */
  63. proc_p p;
  64. if ((p = proclookup(name, IMPORTING)) != (proc_p)0 && !(p->p_flags1 & PF_BODYSEEN))
  65. {
  66. return TRUE;
  67. }
  68. else
  69. {
  70. return FALSE;
  71. }
  72. }
  73. STATIC bool data_wanted(name) char* name;
  74. {
  75. /* See if 'name' is the name of an externally visible
  76. * data block that has been used before, but for which
  77. * no defining occurrence has been given yet.
  78. */
  79. dblock_p db;
  80. if ((db = symlookup(name, IMPORTING)) != (dblock_p)0 && db->d_pseudo == DUNKNOWN)
  81. {
  82. return TRUE;
  83. }
  84. else
  85. {
  86. return FALSE;
  87. }
  88. }
  89. STATIC bool wanted_names()
  90. {
  91. /* Read the names of procedures and data labels,
  92. * appearing in a 'MES ms_ext' pseudo. Those are
  93. * the names of entities that are imported by
  94. * a library module.
  95. * If any of them is wanted, return TRUE.
  96. * A name is wanted if it is the name of a procedure
  97. * or data block for which applied occurrences but
  98. * no defining occurrence has been met.
  99. */
  100. for (;;)
  101. {
  102. switch (table2())
  103. {
  104. case DLBX:
  105. if (data_wanted(string))
  106. {
  107. return TRUE;
  108. }
  109. /* A data entity with the name
  110. * string is available.
  111. */
  112. break;
  113. case sp_pnam:
  114. if (proc_wanted(string))
  115. {
  116. return TRUE;
  117. }
  118. break;
  119. case sp_cend:
  120. return FALSE;
  121. default:
  122. error("wrong argument of MES %d", ms_ext);
  123. }
  124. }
  125. }
  126. STATIC FILE* curfile = NULL;
  127. STATIC bool useful()
  128. {
  129. /* Determine if any entity imported by the current
  130. * compact EM assembly file (which will usually be
  131. * part of an archive file) is useful to us.
  132. * The file must contain (before any other non-MES line)
  133. * a 'MES ms_ext' pseudo that has as arguments the names
  134. * of the entities imported.
  135. */
  136. for (;;)
  137. {
  138. if (table1() != PSEU || tabval != ps_mes)
  139. {
  140. error("cannot find MES %d in library file", ms_ext);
  141. }
  142. if (table2() != CSTX1)
  143. {
  144. error("message number expected");
  145. }
  146. if (tabval == ms_ext)
  147. {
  148. /* This is the one we searched */
  149. return wanted_names();
  150. /* Read the names of the imported entities
  151. * and check if any of them is wanted.
  152. */
  153. }
  154. else
  155. {
  156. skip_arguments(); /* skip remainder of this MES */
  157. }
  158. }
  159. }
  160. STATIC bool is_archive(name) char* name;
  161. {
  162. /* See if 'name' is the name of an archive file, i.e. it
  163. * should end on ".ma" and should at least be four characters
  164. * long (i.e. the name ".ma" is not accepted as an archive name!).
  165. */
  166. register char* p;
  167. for (p = name; *p; p++)
  168. ;
  169. return (p > name + 3) && (*--p == 'a') && (*--p == 'm') && (*--p == '.');
  170. }
  171. STATIC struct ar_hdr hdr;
  172. STATIC bool read_hdr()
  173. {
  174. /* Read the header of an archive module */
  175. char buf[AR_TOTAL];
  176. register char* c = buf;
  177. register char* p = hdr.ar_name;
  178. register int i;
  179. fread(c, AR_TOTAL, 1, curfile);
  180. if (feof(curfile))
  181. return 0;
  182. i = 14;
  183. while (i--)
  184. {
  185. *p++ = *c++;
  186. }
  187. #define get2(c) (((c)[0] & 0377) | ((unsigned)((c)[1] & 0377) << 8))
  188. hdr.ar_date = ((long)get2(c)) << 16;
  189. c += 2;
  190. hdr.ar_date |= ((long)get2(c)) & 0xffff;
  191. c += 2;
  192. hdr.ar_uid = *c++;
  193. hdr.ar_gid = *c++;
  194. hdr.ar_mode = get2(c);
  195. c += 2;
  196. hdr.ar_size = (long)get2(c) << 16;
  197. c += 2;
  198. hdr.ar_size |= (long)get2(c) & 0xffff;
  199. return 1;
  200. }
  201. STATIC int argcnt = 0;
  202. STATIC short arstate = NO_ARCHIVE;
  203. FILE* next_file(argc, argv) int argc;
  204. char* argv[];
  205. {
  206. /* See if there are more EM input files. The file names
  207. * are given via argv. If a file is an archive file
  208. * it is supposed to be a library of EM compact assembly
  209. * files. A module (file) contained in this archive file
  210. * is only used if it imports at least one procedure or
  211. * datalabel for which we have not yet seen a defining
  212. * occurrence, although we have seen a used occurrence.
  213. */
  214. long ptr;
  215. for (;;)
  216. {
  217. /* This loop is only exited via a return */
  218. if (arstate == ARCHIVE)
  219. {
  220. /* We were reading an archive file */
  221. if (ftell(curfile) & 1)
  222. {
  223. /* modules in an archive file always
  224. * begin on a word boundary, i.e. at
  225. * an even address.
  226. */
  227. fseek(curfile, 1L, 1);
  228. }
  229. if (read_hdr())
  230. { /* read header of next module */
  231. ptr = ftell(curfile); /* file position */
  232. file_init(curfile, ARCHIVE, hdr.ar_size);
  233. /* tell i/o package that we're reading
  234. * an archive module of given length.
  235. */
  236. if (useful())
  237. {
  238. /* re-initialize file, because 'useful'
  239. * has read some bytes too.
  240. */
  241. fseek(curfile, ptr, 0); /* start module */
  242. file_init(curfile, ARCHIVE, hdr.ar_size);
  243. return curfile;
  244. }
  245. else
  246. {
  247. /* skip this module */
  248. fseek(curfile,
  249. ptr + hdr.ar_size, 0);
  250. }
  251. }
  252. else
  253. {
  254. /* done with this archive */
  255. arstate = NO_ARCHIVE;
  256. }
  257. }
  258. else
  259. {
  260. /* open next file, close old */
  261. if (curfile != NULL)
  262. {
  263. fclose(curfile);
  264. }
  265. argcnt++;
  266. if (argcnt >= argc)
  267. {
  268. /* done with all arguments */
  269. return NULL;
  270. }
  271. filename = argv[argcnt];
  272. if ((curfile = fopen(filename, "r")) == NULL)
  273. {
  274. error("cannot open %s", filename);
  275. }
  276. if (is_archive(filename))
  277. {
  278. /* ends on '.ma' */
  279. arstate = ARCHIVE;
  280. arch_init(curfile); /* read magic ar number */
  281. }
  282. else
  283. {
  284. file_init(curfile, NO_ARCHIVE, 0L);
  285. return curfile;
  286. }
  287. }
  288. }
  289. }