get.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. /* S H A R E D F I L E
  7. *
  8. * G E T . C
  9. */
  10. #include <stdio.h>
  11. #include <em_spec.h>
  12. #include <em_mnem.h>
  13. #include <em_pseu.h>
  14. #include <em_mes.h>
  15. #include "types.h"
  16. #include "def.h"
  17. #include "debug.h"
  18. #include "global.h"
  19. #include "lset.h"
  20. #include "cset.h"
  21. #include "get.h"
  22. #include "alloc.h"
  23. #include "map.h"
  24. #include "aux.h"
  25. FILE *curinp;
  26. block_id lastbid; /* block identifying number */
  27. lab_id lastlabid; /* last label identifier */
  28. /* creating new identifying numbers, i.e. numbers that did not
  29. * appear in the input.
  30. */
  31. bblock_p freshblock()
  32. {
  33. bblock_p b;
  34. b = newbblock();
  35. b->b_id = ++lastbid;
  36. return b;
  37. }
  38. lab_id freshlabel()
  39. {
  40. curproc->p_nrlabels++;
  41. return ++lastlabid;
  42. }
  43. #define getmark() getbyte()
  44. short getshort() {
  45. register int l_byte, h_byte;
  46. l_byte = getbyte();
  47. h_byte = getbyte();
  48. if ( h_byte>=128 ) h_byte -= 256 ;
  49. return l_byte | (h_byte*256) ;
  50. }
  51. offset getoff() {
  52. register long l;
  53. register int h_byte;
  54. l = getbyte();
  55. l |= ((unsigned) getbyte())*256 ;
  56. l |= getbyte()*256L*256L ;
  57. h_byte = getbyte() ;
  58. if ( h_byte>=128 ) h_byte -= 256 ;
  59. return l | (h_byte*256L*256*256L) ;
  60. }
  61. static int getint()
  62. {
  63. /* Read an integer from the input file. This routine is
  64. * only used when reading a bitvector-set. We expect an
  65. * integer to be either a short or a long.
  66. */
  67. if (sizeof(int) == sizeof(short)) {
  68. return getshort();
  69. } else {
  70. assert (sizeof(int) == sizeof(offset));
  71. return getoff();
  72. }
  73. }
  74. /* getptable */
  75. /* loop_p getloop(loop_id id) */
  76. void *getloop(short lid)
  77. {
  78. loop_id id = lid;
  79. /* Map a loop identifier onto a loop struct.
  80. * If no struct was alocated yet for this identifier then
  81. * allocate one now and update the loop-map table.
  82. */
  83. assert (id > 0 && id <=lplength);
  84. if (lpmap[id] == (loop_p) 0) {
  85. lpmap[id] = newloop();
  86. lpmap[id]->lp_id = id;
  87. }
  88. return (lpmap[id]);
  89. }
  90. /* bblock_p getblock(block_id id) */
  91. void *getblock(short bid)
  92. {
  93. block_id id = bid;
  94. /* Map a basic block identifier onto a block struct
  95. * If no struct was alocated yet for this identifier then
  96. * allocate one now and update the block-map table.
  97. */
  98. assert (id >= 0 && id <=blength);
  99. if (id == 0) return (bblock_p) 0;
  100. if (bmap[id] == (bblock_p) 0) {
  101. bmap[id] = newbblock();
  102. bmap[id]->b_id = id;
  103. }
  104. return (bmap[id]);
  105. }
  106. lset getlset(void *(*p)(short))
  107. {
  108. /* Read a 'long' set. Such a set is represented externally
  109. * as a sequence of identifying numbers terminated by a 0.
  110. * The procedural parameter p maps such a number onto a
  111. * pointer to a struct (bblock_p, loop_p etc.).
  112. */
  113. lset s;
  114. int id;
  115. s = Lempty_set();
  116. while ((id = getshort())) {
  117. Ladd( (*p) (id), &s);
  118. }
  119. return s;
  120. }
  121. cset getcset()
  122. {
  123. /* Read a 'compact' set. Such a set is represented externally
  124. * a row of bytes (its bitvector) preceded by its length.
  125. */
  126. cset s;
  127. register short i;
  128. s = Cempty_set(getshort());
  129. for (i = 0; i <= DIVWL(s->v_size-1);i++) {
  130. s->v_bits[i] = getint();
  131. }
  132. return s;
  133. }
  134. proc_p getptable(char *pname)
  135. {
  136. short i;
  137. proc_p head, p, *pp;
  138. short all;
  139. if ((curinp = fopen(pname,"r")) == NULL) {
  140. error("cannot open %s",pname);
  141. }
  142. plength = getshort(); /* table is preceded by its length */
  143. assert(plength >= 0);
  144. assert(plength < 1000); /* See if its a reasonable number */
  145. pmap = (proc_p *) newmap(plength); /* allocate the pmap table */
  146. all = getshort();
  147. head = (proc_p) 0;
  148. pp = &head;
  149. for (i = 0; i < plength; i++) {
  150. if (feof(curinp)) {
  151. error("unexpected eof %s", pname);
  152. }
  153. p = newproc();
  154. p->p_id = getshort();
  155. assert(p->p_id > 0 && p->p_id <= plength);
  156. pmap[p->p_id] = p;
  157. p->p_flags1 = getbyte();
  158. if (p->p_flags1 & PF_BODYSEEN) {
  159. p->p_nrlabels = getshort();
  160. p->p_localbytes = getoff();
  161. p->p_nrformals = getoff();
  162. if (all) {
  163. p->p_change = newchange();
  164. p->p_change->c_ext = getcset();
  165. p->p_change->c_flags = getshort();
  166. p->p_use = newuse();
  167. p->p_use->u_flags = getshort();
  168. p->p_calling = getcset();
  169. }
  170. }
  171. *pp = p;
  172. pp = &(p->p_next);
  173. }
  174. fclose(curinp);
  175. OUTTRACE("have read proc table of length %d",plength);
  176. return head; /* pointer to first structure of list */
  177. }
  178. /* getdtable */
  179. dblock_p getdtable(char *dname)
  180. {
  181. /* Read the data block table. Every data block may
  182. * have a list of objects and a list of values (arguments),
  183. * each of which is also represented by a structure.
  184. * So the input file contains a mixture of dblock,
  185. * obj and arg records, each one having its own
  186. * attributes. A mark indicates which one comes next.
  187. * We assume that the syntactic structure of the input
  188. * is correct.
  189. */
  190. dblock_p head, d = 0, *dp = &head;
  191. obj_p obj, *op = 0;
  192. arg_p arg, *ap = 0;
  193. /* dp, op an ap tell how the next dblock/obj/arg
  194. * has to be linked.
  195. */
  196. int n;
  197. head = (dblock_p) 0;
  198. if ((curinp = fopen(dname,"r")) == NULL) {
  199. error("cannot open %s", dname);
  200. }
  201. olength = getshort();
  202. assert(olength >= 0);
  203. assert(olength < 5000); /* See if its a reasonable number */
  204. /* total number of objects */
  205. omap = (obj_p *) newmap(olength); /* allocate omap table */
  206. while (TRUE) {
  207. n = getmark();
  208. if (feof(curinp)) break;
  209. switch(n) {
  210. case MARK_DBLOCK:
  211. d = *dp = newdblock();
  212. op = &d->d_objlist;
  213. ap = &d->d_values;
  214. dp = &d->d_next;
  215. d->d_id = getshort();
  216. d->d_pseudo = getbyte();
  217. d->d_size = getoff();
  218. d->d_fragmnr = getshort();
  219. d->d_flags1 = getbyte();
  220. break;
  221. case MARK_OBJ:
  222. obj = *op = newobject();
  223. op = &obj->o_next;
  224. obj->o_dblock = d;
  225. obj->o_id = getshort();
  226. assert(obj->o_id >0);
  227. assert(obj->o_id <= olength);
  228. omap[obj->o_id] = obj;
  229. obj->o_size = getoff();
  230. obj->o_off = getoff();
  231. break;
  232. case MARK_ARG:
  233. arg = *ap = newarg(ARGOFF);
  234. ap = &arg->a_next;
  235. arg->a_a.a_offset = getoff();
  236. break;
  237. default:
  238. assert(FALSE);
  239. }
  240. }
  241. OUTTRACE("have read data table, %d objects",olength);
  242. return head;
  243. }
  244. /* getbblocks */
  245. static void argstring(short length, argb_p abp)
  246. {
  247. while (length--) {
  248. if (abp->ab_index == NARGBYTES)
  249. abp = abp->ab_next = newargb();
  250. abp->ab_contents[abp->ab_index++] = getbyte();
  251. }
  252. }
  253. static arg_p readargs()
  254. {
  255. /* Read a list of arguments and allocate structures
  256. * for them. Return a pointer to the head of the list.
  257. */
  258. arg_p head, arg, *ap;
  259. byte t;
  260. short length;
  261. ap = &head;
  262. for (;;) {
  263. /* every argument list is terminated by an
  264. * ARGCEND byte in Intermediate Code.
  265. */
  266. t = getbyte();
  267. if (t == (byte) ARGCEND) {
  268. return head;
  269. }
  270. arg = *ap = newarg(t);
  271. ap = &arg->a_next;
  272. switch((short) t) {
  273. case ARGOFF:
  274. arg->a_a.a_offset = getoff();
  275. break;
  276. case ARGINSTRLAB:
  277. arg->a_a.a_instrlab = getshort();
  278. break;
  279. case ARGOBJECT:
  280. arg->a_a.a_obj = omap[getshort()];
  281. /* Read an object identifier (o_id)
  282. * and use the omap table to obtain
  283. * a pointer to the rigth obj struct.
  284. */
  285. break;
  286. case ARGPROC:
  287. arg->a_a.a_proc = pmap[getshort()];
  288. /* Read a procedure identifier (p_id) */
  289. break;
  290. case ARGSTRING:
  291. length = getshort();
  292. argstring(length, &arg->a_a.a_string);
  293. break;
  294. case ARGICN:
  295. case ARGUCN:
  296. case ARGFCN:
  297. length = getshort();
  298. arg->a_a.a_con.ac_length = length;
  299. /* size of the constant */
  300. argstring(getshort(),
  301. &arg->a_a.a_con.ac_con);
  302. break;
  303. default:
  304. assert(FALSE);
  305. }
  306. }
  307. }
  308. line_p read_line(proc_p *p_out)
  309. {
  310. /* Read a line of EM code (i.e. one instruction)
  311. * and its arguments (if any).
  312. * In Intermediate Code, the first byte is the
  313. * instruction code and the second byte denotes the kind
  314. * of operand(s) that follow.
  315. */
  316. line_p lnp;
  317. byte instr;
  318. instr = getbyte();
  319. if (feof(curinp)) return (line_p) 0;
  320. lnp = newline(getbyte());
  321. linecount++;
  322. lnp->l_instr = instr;
  323. switch(TYPE(lnp)) {
  324. /* read the operand(s) */
  325. case OPSHORT:
  326. SHORT(lnp) = getshort();
  327. break;
  328. case OPOFFSET:
  329. OFFSET(lnp) = getoff();
  330. break;
  331. case OPINSTRLAB:
  332. INSTRLAB(lnp) = getshort();
  333. if ((instr & BMASK) == op_lab) {
  334. /* defining occurrence of an
  335. * instruction label.
  336. */
  337. lmap[INSTRLAB(lnp)] = lnp;
  338. }
  339. break;
  340. case OPOBJECT:
  341. OBJ(lnp) = omap[getshort()];
  342. break;
  343. case OPPROC:
  344. PROC(lnp) = pmap[getshort()];
  345. if ((instr & BMASK) == ps_pro) {
  346. /* enter new procedure: allocate a
  347. * label map and a label-block map table.
  348. */
  349. *p_out = PROC(lnp);
  350. llength = (*p_out)->p_nrlabels;
  351. lmap = (line_p *) newmap(llength);
  352. /* maps lab_id to line structure */
  353. lbmap = (bblock_p *) newmap(llength);
  354. /* maps lab_id to bblock structure */
  355. lastlabid = llength;
  356. }
  357. break;
  358. case OPLIST:
  359. ARG(lnp) = readargs();
  360. break;
  361. default:
  362. assert(TYPE(lnp) == OPNO);
  363. }
  364. return lnp;
  365. }
  366. void message(line_p lnp)
  367. {
  368. /* See if lnp is some useful message.
  369. * (e.g. a message telling that a certain local variable
  370. * will never be referenced indirectly, so it may be put
  371. * in a register. If so, add it to the mesregs set.)
  372. */
  373. assert(ARG(lnp)->a_type == ARGOFF);
  374. switch((int) aoff(ARG(lnp),0)) {
  375. case ms_reg:
  376. if (ARG(lnp)->a_next != (arg_p) 0) {
  377. /* take only "mes 3" with further arguments */
  378. Ladd((Lelem_t) lnp,&mesregs);
  379. }
  380. break;
  381. case ms_err:
  382. error("ms_err encountered");
  383. case ms_opt:
  384. error("ms_opt encountered");
  385. case ms_emx:
  386. ws = aoff(ARG(lnp),1);
  387. ps = aoff(ARG(lnp),2);
  388. break;
  389. }
  390. }
  391. line_p getlines(FILE *lf, int n, proc_p *p_out, bool collect_mes)
  392. {
  393. /* Read n lines of EM text and doubly link them.
  394. * Also process messages.
  395. */
  396. line_p head, *pp, l, lprev;
  397. curinp = lf; /* EM input file */
  398. pp = &head;
  399. lprev = (line_p) 0;
  400. while (n--) {
  401. l = *pp = read_line(p_out);
  402. PREV(l) = lprev;
  403. pp = &l->l_next;
  404. lprev = l;
  405. if (collect_mes && INSTR(l) == ps_mes) {
  406. message(l);
  407. }
  408. }
  409. *pp = (line_p) 0;
  410. return head;
  411. }
  412. bool getunit(FILE *gf, FILE *lf, short *kind_out, bblock_p *g_out, line_p *l_out, proc_p *p_out, bool collect_mes)
  413. {
  414. /* Read control flow graph (gf) and EM text (lf) of the next procedure.
  415. * A pointer to the proctable entry of the read procedure is
  416. * returned via p_out.
  417. * This routine also constructs the bmap and lpmap tables.
  418. * Note that we allocate structs for basic blocks and loops
  419. * at their first reference rather than at when we read them.
  420. */
  421. int n,i;
  422. bblock_p head, *pp, b;
  423. loop_p lp;
  424. curinp = gf;
  425. blength = getshort(); /* # basic blocks in this procedure */
  426. if (feof(curinp)) return FALSE;
  427. if (blength == 0) {
  428. /* data unit */
  429. *kind_out = LDATA;
  430. n = getshort();
  431. *l_out = getlines(lf,n,p_out,collect_mes);
  432. return TRUE;
  433. }
  434. *kind_out = LTEXT;
  435. bmap = (bblock_p *) newmap(blength); /* maps block_id on bblock_p */
  436. lplength = getshort(); /* # loops in this procedure */
  437. lpmap = (loop_p *) newmap(lplength); /* maps loop_id on loop_p */
  438. /* Read the basic blocks and the EM text */
  439. pp = &head; /* we use a pointer-to-a-pointer to link the structs */
  440. for (i = 0; i < blength; i++) {
  441. b = getblock(getshort());
  442. n = getshort(); /* #instructions in the block */
  443. b->b_succ = getlset(getblock);
  444. b->b_pred = getlset(getblock);
  445. b->b_idom = getblock(getshort());
  446. b->b_loops = getlset(getloop);
  447. b->b_flags = getshort();
  448. b->b_start = getlines(lf,n,p_out,collect_mes); /* read EM text */
  449. *pp = b;
  450. pp = &b->b_next;
  451. curinp = gf;
  452. }
  453. lastbid = blength; /* last block_id */
  454. /* read the information about loops */
  455. curproc->p_loops = Lempty_set();
  456. for (i = 0; i < lplength; i++) {
  457. lp = getloop(getshort());
  458. lp->lp_level = getshort(); /* nesting level */
  459. lp->lp_entry = getblock(getshort()); /* entry block of the loop */
  460. lp->lp_end = getblock(getshort()); /* tail of back edge of loop */
  461. Ladd((Lelem_t)lp,&curproc->p_loops);
  462. }
  463. *g_out = head;
  464. return TRUE;
  465. }