ic_lookup.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 O O K U P . C
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <em_spec.h>
  14. #include "../share/types.h"
  15. #include "../share/debug.h"
  16. #include "../share/map.h"
  17. #include "ic.h"
  18. #include "ic_lookup.h"
  19. #include "../share/alloc.h"
  20. sym_p symhash[NSYMHASH];
  21. prc_p prochash[NPROCHASH];
  22. num_p numhash[NNUMHASH];
  23. char *lastname;
  24. //extern char *strcpy();
  25. #define newsym() (sym_p) newstruct(sym)
  26. #define newprc() (prc_p) newstruct(prc)
  27. #define newnum() (num_p) newstruct(num)
  28. #define oldsym(x) oldstruct(sym,x)
  29. #define oldprc(x) oldstruct(prc,x)
  30. #define oldnum(x) oldstruct(num,x)
  31. #define PF_FILE 2
  32. #define DF_FILE 2
  33. /* instr_lab */
  34. lab_id instr_lab(number)
  35. short number;
  36. {
  37. register num_p *npp, np;
  38. /* In EM assembly language, a label is an unsigned number,
  39. * e.g. 120 in 'BRA *120'. In IC the labels of a procedure
  40. * are represented by consecutive integer numbers, called
  41. * lab_id. The mapping takes place here.
  42. */
  43. npp = &numhash[number%NNUMHASH];
  44. while (*npp != (num_p) 0) {
  45. if ((*npp)->n_number == number) {
  46. return(*npp)->n_labid;
  47. } else {
  48. npp = &(*npp)->n_next;
  49. }
  50. }
  51. /* The label was not found in the hashtable, so
  52. * create a new entry for it.
  53. */
  54. *npp = np = newnum();
  55. np->n_number = number;
  56. np->n_labid = ++lastlid;
  57. /* Assign a new label identifier to the num struct.
  58. * lastlid is reset to 0 at the beginning of
  59. * every new EM procedure (by cleaninstrlabs).
  60. */
  61. return (np->n_labid);
  62. }
  63. /* symlookup */
  64. STATIC unsigned hash(string) char *string; {
  65. register char *p;
  66. register unsigned i,sum;
  67. for (sum=i=0,p=string;*p;i += 3)
  68. sum ^= (*p++)<<(i&07);
  69. return(sum);
  70. }
  71. dblock_p symlookup(name, status)
  72. char *name;
  73. int status;
  74. {
  75. /* Look up the name of a data block. The name can appear
  76. * in either a defining or applied occurrence (status is
  77. * DEFINING, OCCURRING resp.), or in a MES ms_ext instruction
  78. * as the name of a data block imported by a library module
  79. * (status is IMPORTING). Things get complicated,
  80. * because a HOL pseudo need not be preceded by a
  81. * data label, i.e. a hol block need not have a name.
  82. */
  83. register sym_p *spp, sp;
  84. register dblock_p dp;
  85. if (name == (char *) 0) {
  86. assert(status == DEFINING);
  87. dp = newdblock();
  88. } else {
  89. spp = &symhash[hash(name)%NSYMHASH];
  90. while (*spp != (sym_p) 0) {
  91. /* Every hashtable entry points to a list
  92. * of synonyms (i.e. names with the same
  93. * hash values). Try to find 'name' in its
  94. * list.
  95. */
  96. if (strcmp((*spp)->sy_name, name) == 0) {
  97. dp = (*spp)->sy_dblock;
  98. if (status != DEFINING ||
  99. (dp->d_flags1 & DF_EXTERNAL) == 0) {
  100. dp->d_flags2 |= DF_FILE;
  101. }
  102. if (dp->d_flags2 & DF_FILE) {
  103. lastname = (*spp)->sy_name;
  104. return dp;
  105. }
  106. break;
  107. } else {
  108. spp = &(*spp)->sy_next;
  109. }
  110. }
  111. /* The name is not found, so create a new entry for it.
  112. * However, if the status is IMPORTING, we just return 0,
  113. * indicating that we don't need this name.
  114. */
  115. if (status == IMPORTING) return (dblock_p) 0;
  116. sp = newsym();
  117. sp->sy_next = *spp;
  118. *spp = sp;
  119. sp->sy_name = (char *) newcore(strlen(name)+1);
  120. strcpy(sp->sy_name, name);
  121. lastname = sp->sy_name; /* quick hack to get at
  122. the name
  123. */
  124. dp = sp->sy_dblock = newdblock();
  125. }
  126. if (fdblock == (dblock_p) 0) {
  127. fdblock = dp;
  128. /* first data block */
  129. } else {
  130. ldblock->d_next = dp; /* link to last dblock */
  131. }
  132. ldblock = dp;
  133. dp->d_pseudo = DUNKNOWN; /* clear all fields */
  134. dp->d_id = ++lastdid;
  135. dp->d_size = 0;
  136. dp->d_objlist = (obj_p) 0;
  137. dp->d_values = (arg_p) 0;
  138. dp->d_next = (dblock_p) 0;
  139. dp->d_flags1 = 0;
  140. dp->d_flags2 = 0;
  141. if (status == OCCURRING) {
  142. /* This is the first occurrence of the identifier,
  143. * so if it is a used occurrence make the
  144. * identifier externally visible, else make it
  145. * internal.
  146. */
  147. dp->d_flags1 |= DF_EXTERNAL;
  148. }
  149. dp->d_flags2 |= DF_FILE;
  150. return dp;
  151. }
  152. /* getsym */
  153. dblock_p getsym(status)
  154. int status;
  155. {
  156. if (table2() != DLBX) {
  157. error("symbol expected");
  158. }
  159. return(symlookup(string,status));
  160. }
  161. /* getproc */
  162. proc_p getproc(status)
  163. int status;
  164. {
  165. if (table2() != sp_pnam) {
  166. error("proc name expected");
  167. }
  168. return(proclookup(string,status));
  169. }
  170. /* proclookup */
  171. proc_p proclookup(name, status)
  172. char *name;
  173. int status;
  174. {
  175. register prc_p *ppp, pp;
  176. register proc_p dp;
  177. ppp = &prochash[hash(name)%NPROCHASH];
  178. while (*ppp != (prc_p) 0) {
  179. /* Every hashtable entry points to a list
  180. * of synonyms (i.e. names with the same
  181. * hash values). Try to find 'name' in its
  182. * list.
  183. */
  184. if (strcmp((*ppp)->pr_name, name) == 0) {
  185. /* found */
  186. dp = (*ppp)->pr_proc;
  187. if (status != DEFINING ||
  188. (dp->p_flags1 & PF_EXTERNAL) == 0) {
  189. dp->p_flags2 |= PF_FILE;
  190. return dp;
  191. }
  192. if (dp->p_flags2 & PF_FILE) return dp;
  193. break;
  194. } else {
  195. ppp = &(*ppp)->pr_next;
  196. }
  197. }
  198. /* The name is not found, so create a new entry for it,
  199. * unless the status is IMPORTING, in which case we
  200. * return 0, indicating we don't want this proc.
  201. */
  202. if (status == IMPORTING) return (proc_p) 0;
  203. pp = newprc();
  204. pp->pr_next = *ppp;
  205. *ppp = pp;
  206. pp->pr_name = (char *) newcore(strlen(name)+1);
  207. strcpy(pp->pr_name, name);
  208. dp = pp->pr_proc = newproc();
  209. if (fproc == (proc_p) 0) {
  210. fproc = dp; /* first proc */
  211. } else {
  212. lproc->p_next = dp;
  213. }
  214. lproc = dp;
  215. dp->p_id = ++lastpid; /* create a unique proc_id */
  216. dp->p_next = (proc_p) 0;
  217. dp->p_flags1 = 0;
  218. dp->p_flags2 = 0;
  219. if (status == OCCURRING) {
  220. /* This is the first occurrence of the identifier,
  221. * so if it is a used occurrence the make the
  222. * identifier externally visible, else make it
  223. * internal.
  224. */
  225. dp->p_flags1 |= PF_EXTERNAL;
  226. }
  227. dp->p_flags2 |= PF_FILE;
  228. return dp;
  229. }
  230. /* cleaninstrlabs */
  231. cleaninstrlabs()
  232. {
  233. register num_p *npp, np, next;
  234. for (npp = numhash; npp < &numhash[NNUMHASH]; npp++) {
  235. for (np = *npp; np != (num_p) 0; np = next) {
  236. next = np->n_next;
  237. oldnum(np);
  238. }
  239. *npp = (num_p) 0;
  240. }
  241. /* Reset last label id (used by instr_lab). */
  242. lastlid = (lab_id) 0;
  243. }
  244. /* dump_procnames */
  245. dump_procnames(hash,n,f)
  246. prc_p hash[];
  247. int n;
  248. FILE *f;
  249. {
  250. /* Save the names of the EM procedures in file f.
  251. * Note that the Optimizer Intermediate Code does not
  252. * use identifiers but proc_ids, object_ids etc.
  253. * The names, however, can be used after optimization
  254. * is completed, to reconstruct Compact Assembly Language.
  255. * The output consists of tuples (proc_id, name).
  256. * This routine is called once for every input file.
  257. * To prevent names of external procedures being written
  258. * more than once, the PF_WRITTEN flag is used.
  259. */
  260. register prc_p *pp, ph;
  261. proc_p p;
  262. #define PF_WRITTEN 01
  263. for (pp = &hash[0]; pp < &hash[n]; pp++) {
  264. /* Traverse the entire hash table */
  265. for (ph = *pp; ph != (prc_p) 0; ph = ph->pr_next) {
  266. /* Traverse the list of synonyms */
  267. p = ph->pr_proc;
  268. if ((p->p_flags2 & PF_WRITTEN) == 0) {
  269. /* not been written yet */
  270. fprintf(f,"%d %s\n",p->p_id, ph->pr_name);
  271. p->p_flags2 |= PF_WRITTEN;
  272. }
  273. }
  274. }
  275. }
  276. /* cleanprocs */
  277. cleanprocs(hash,n,mask)
  278. prc_p hash[];
  279. int n,mask;
  280. {
  281. /* After an EM input file has been processed, the names
  282. * of those procedures that are internal (i.e. not visible
  283. * outside the file they are defined in) must be removed
  284. * from the procedure hash table. This is accomplished
  285. * by removing the 'prc struct' from its synonym list.
  286. * After the final input file has been processed, all
  287. * remaining prc structs are also removed.
  288. */
  289. register prc_p *pp, ph, x, next;
  290. for (pp = &hash[0]; pp < &hash[n]; pp++) {
  291. /* Traverse the hash table */
  292. x = (prc_p) 0;
  293. for (ph = *pp; ph != (prc_p) 0; ph = next) {
  294. /* Traverse the synonym list.
  295. * x points to the prc struct just before ph,
  296. * or is 0 if ph is the first struct of
  297. * the list.
  298. */
  299. ph->pr_proc->p_flags2 &= ~PF_FILE;
  300. next = ph->pr_next;
  301. if ((ph->pr_proc->p_flags1 & mask) == 0) {
  302. if (x == (prc_p) 0) {
  303. *pp = next;
  304. } else {
  305. x->pr_next = next;
  306. }
  307. oldprc(ph); /* delete the struct */
  308. } else {
  309. x = ph;
  310. }
  311. }
  312. }
  313. }
  314. /* dump_dblocknames */
  315. dump_dblocknames(hash,n,f)
  316. sym_p hash[];
  317. int n;
  318. FILE *f;
  319. {
  320. /* Save the names of the EM data blocks in file f.
  321. * The output consists of tuples (dblock_id, name).
  322. * This routine is called once for every input file.
  323. */
  324. register sym_p *sp, sh;
  325. dblock_p d;
  326. #define DF_WRITTEN 01
  327. for (sp = &hash[0]; sp < &hash[n]; sp++) {
  328. /* Traverse the entire hash table */
  329. for (sh = *sp; sh != (sym_p) 0; sh = sh->sy_next) {
  330. /* Traverse the list of synonyms */
  331. d = sh->sy_dblock;
  332. if ((d->d_flags2 & DF_WRITTEN) == 0) {
  333. /* not been written yet */
  334. fprintf(f,"%d %s\n",d->d_id, sh->sy_name);
  335. d->d_flags2 |= DF_WRITTEN;
  336. }
  337. }
  338. }
  339. }
  340. /* cleandblocks */
  341. cleandblocks(hash,n,mask)
  342. sym_p hash[];
  343. int n,mask;
  344. {
  345. /* After an EM input file has been processed, the names
  346. * of those data blocks that are internal must be removed.
  347. */
  348. register sym_p *sp, sh, x, next;
  349. for (sp = &hash[0]; sp < &hash[n]; sp++) {
  350. x = (sym_p) 0;
  351. for (sh = *sp; sh != (sym_p) 0; sh = next) {
  352. next = sh->sy_next;
  353. sh->sy_dblock->d_flags2 &= ~DF_FILE;
  354. if ((sh->sy_dblock->d_flags1 & mask) == 0) {
  355. if (x == (sym_p) 0) {
  356. *sp = next;
  357. } else {
  358. x->sy_next = next;
  359. }
  360. oldsym(sh); /* delete the struct */
  361. } else {
  362. x = sh;
  363. }
  364. }
  365. }
  366. }