ic_lookup.c 9.2 KB

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