ic_lookup.c 9.2 KB

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