get.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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(id)
  76. loop_id id;
  77. {
  78. /* Map a loop identifier onto a loop struct.
  79. * If no struct was alocated yet for this identifier then
  80. * allocate one now and update the loop-map table.
  81. */
  82. assert (id > 0 && id <=lplength);
  83. if (lpmap[id] == (loop_p) 0) {
  84. lpmap[id] = newloop();
  85. lpmap[id]->lp_id = id;
  86. }
  87. return (lpmap[id]);
  88. }
  89. bblock_p getblock(id)
  90. block_id id;
  91. {
  92. /* Map a basic block identifier onto a block struct
  93. * If no struct was alocated yet for this identifier then
  94. * allocate one now and update the block-map table.
  95. */
  96. assert (id >= 0 && id <=blength);
  97. if (id == 0) return (bblock_p) 0;
  98. if (bmap[id] == (bblock_p) 0) {
  99. bmap[id] = newbblock();
  100. bmap[id]->b_id = id;
  101. }
  102. return (bmap[id]);
  103. }
  104. lset getlset(p)
  105. char *((*p) ());
  106. {
  107. /* Read a 'long' set. Such a set is represented externally
  108. * as a sequence of identifying numbers terminated by a 0.
  109. * The procedural parameter p maps such a number onto a
  110. * pointer to a struct (bblock_p, loop_p etc.).
  111. */
  112. lset s;
  113. int id;
  114. s = Lempty_set();
  115. while (id = getshort()) {
  116. Ladd( (*p) (id), &s);
  117. }
  118. return s;
  119. }
  120. cset getcset()
  121. {
  122. /* Read a 'compact' set. Such a set is represented externally
  123. * a row of bytes (its bitvector) preceded by its length.
  124. */
  125. cset s;
  126. register short i;
  127. s = Cempty_set(getshort());
  128. for (i = 0; i <= DIVWL(s->v_size-1);i++) {
  129. s->v_bits[i] = getint();
  130. }
  131. return s;
  132. }
  133. proc_p getptable(pname)
  134. 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(dname)
  180. char *dname;
  181. {
  182. /* Read the data block table. Every data block may
  183. * have a list of objects and a list of values (arguments),
  184. * each of which is also represented by a structure.
  185. * So the input file contains a mixture of dblock,
  186. * obj and arg records, each one having its own
  187. * attributes. A mark indicates which one comes next.
  188. * We assume that the syntactic structure of the input
  189. * is correct.
  190. */
  191. dblock_p head, d = 0, *dp = &head;
  192. obj_p obj, *op = 0;
  193. arg_p arg, *ap = 0;
  194. /* dp, op an ap tell how the next dblock/obj/arg
  195. * has to be linked.
  196. */
  197. int n;
  198. head = (dblock_p) 0;
  199. if ((curinp = fopen(dname,"r")) == NULL) {
  200. error("cannot open %s", dname);
  201. }
  202. olength = getshort();
  203. assert(olength >= 0);
  204. assert(olength < 5000); /* See if its a reasonable number */
  205. /* total number of objects */
  206. omap = (obj_p *) newmap(olength); /* allocate omap table */
  207. while (TRUE) {
  208. n = getmark();
  209. if (feof(curinp)) break;
  210. switch(n) {
  211. case MARK_DBLOCK:
  212. d = *dp = newdblock();
  213. op = &d->d_objlist;
  214. ap = &d->d_values;
  215. dp = &d->d_next;
  216. d->d_id = getshort();
  217. d->d_pseudo = getbyte();
  218. d->d_size = getoff();
  219. d->d_fragmnr = getshort();
  220. d->d_flags1 = getbyte();
  221. break;
  222. case MARK_OBJ:
  223. obj = *op = newobject();
  224. op = &obj->o_next;
  225. obj->o_dblock = d;
  226. obj->o_id = getshort();
  227. assert(obj->o_id >0);
  228. assert(obj->o_id <= olength);
  229. omap[obj->o_id] = obj;
  230. obj->o_size = getoff();
  231. obj->o_off = getoff();
  232. break;
  233. case MARK_ARG:
  234. arg = *ap = newarg(ARGOFF);
  235. ap = &arg->a_next;
  236. arg->a_a.a_offset = getoff();
  237. break;
  238. default:
  239. assert(FALSE);
  240. }
  241. }
  242. OUTTRACE("have read data table, %d objects",olength);
  243. return head;
  244. }
  245. /* getbblocks */
  246. STATIC argstring(length,abp)
  247. short length;
  248. register argb_p abp;
  249. {
  250. while (length--) {
  251. if (abp->ab_index == NARGBYTES)
  252. abp = abp->ab_next = newargb();
  253. abp->ab_contents[abp->ab_index++] = getbyte();
  254. }
  255. }
  256. STATIC arg_p readargs()
  257. {
  258. /* Read a list of arguments and allocate structures
  259. * for them. Return a pointer to the head of the list.
  260. */
  261. arg_p head, arg, *ap;
  262. byte t;
  263. short length;
  264. ap = &head;
  265. for (;;) {
  266. /* every argument list is terminated by an
  267. * ARGCEND byte in Intermediate Code.
  268. */
  269. t = getbyte();
  270. if (t == (byte) ARGCEND) {
  271. return head;
  272. }
  273. arg = *ap = newarg(t);
  274. ap = &arg->a_next;
  275. switch((short) t) {
  276. case ARGOFF:
  277. arg->a_a.a_offset = getoff();
  278. break;
  279. case ARGINSTRLAB:
  280. arg->a_a.a_instrlab = getshort();
  281. break;
  282. case ARGOBJECT:
  283. arg->a_a.a_obj = omap[getshort()];
  284. /* Read an object identifier (o_id)
  285. * and use the omap table to obtain
  286. * a pointer to the rigth obj struct.
  287. */
  288. break;
  289. case ARGPROC:
  290. arg->a_a.a_proc = pmap[getshort()];
  291. /* Read a procedure identifier (p_id) */
  292. break;
  293. case ARGSTRING:
  294. length = getshort();
  295. argstring(length, &arg->a_a.a_string);
  296. break;
  297. case ARGICN:
  298. case ARGUCN:
  299. case ARGFCN:
  300. length = getshort();
  301. arg->a_a.a_con.ac_length = length;
  302. /* size of the constant */
  303. argstring(getshort(),
  304. &arg->a_a.a_con.ac_con);
  305. break;
  306. default:
  307. assert(FALSE);
  308. }
  309. }
  310. }
  311. line_p read_line(p_out)
  312. proc_p *p_out;
  313. {
  314. /* Read a line of EM code (i.e. one instruction)
  315. * and its arguments (if any).
  316. * In Intermediate Code, the first byte is the
  317. * instruction code and the second byte denotes the kind
  318. * of operand(s) that follow.
  319. */
  320. line_p lnp;
  321. byte instr;
  322. instr = getbyte();
  323. if (feof(curinp)) return (line_p) 0;
  324. lnp = newline(getbyte());
  325. linecount++;
  326. lnp->l_instr = instr;
  327. switch(TYPE(lnp)) {
  328. /* read the operand(s) */
  329. case OPSHORT:
  330. SHORT(lnp) = getshort();
  331. break;
  332. case OPOFFSET:
  333. OFFSET(lnp) = getoff();
  334. break;
  335. case OPINSTRLAB:
  336. INSTRLAB(lnp) = getshort();
  337. if ((instr & BMASK) == op_lab) {
  338. /* defining occurrence of an
  339. * instruction label.
  340. */
  341. lmap[INSTRLAB(lnp)] = lnp;
  342. }
  343. break;
  344. case OPOBJECT:
  345. OBJ(lnp) = omap[getshort()];
  346. break;
  347. case OPPROC:
  348. PROC(lnp) = pmap[getshort()];
  349. if ((instr & BMASK) == ps_pro) {
  350. /* enter new procedure: allocate a
  351. * label map and a label-block map table.
  352. */
  353. *p_out = PROC(lnp);
  354. llength = (*p_out)->p_nrlabels;
  355. lmap = (line_p *) newmap(llength);
  356. /* maps lab_id to line structure */
  357. lbmap = (bblock_p *) newmap(llength);
  358. /* maps lab_id to bblock structure */
  359. lastlabid = llength;
  360. }
  361. break;
  362. case OPLIST:
  363. ARG(lnp) = readargs();
  364. break;
  365. default:
  366. assert(TYPE(lnp) == OPNO);
  367. }
  368. return lnp;
  369. }
  370. message(lnp)
  371. line_p lnp;
  372. {
  373. /* See if lnp is some useful message.
  374. * (e.g. a message telling that a certain local variable
  375. * will never be referenced indirectly, so it may be put
  376. * in a register. If so, add it to the mesregs set.)
  377. */
  378. assert(ARG(lnp)->a_type == ARGOFF);
  379. switch((int) aoff(ARG(lnp),0)) {
  380. case ms_reg:
  381. if (ARG(lnp)->a_next != (arg_p) 0) {
  382. /* take only "mes 3" with further arguments */
  383. Ladd((Lelem_t) lnp,&mesregs);
  384. }
  385. break;
  386. case ms_err:
  387. error("ms_err encountered");
  388. case ms_opt:
  389. error("ms_opt encountered");
  390. case ms_emx:
  391. ws = aoff(ARG(lnp),1);
  392. ps = aoff(ARG(lnp),2);
  393. break;
  394. }
  395. }
  396. line_p getlines(lf,n,p_out,collect_mes)
  397. FILE *lf;
  398. int n;
  399. proc_p *p_out;
  400. bool collect_mes;
  401. {
  402. /* Read n lines of EM text and doubly link them.
  403. * Also process messages.
  404. */
  405. line_p head, *pp, l, lprev;
  406. curinp = lf; /* EM input file */
  407. pp = &head;
  408. lprev = (line_p) 0;
  409. while (n--) {
  410. l = *pp = read_line(p_out);
  411. PREV(l) = lprev;
  412. pp = &l->l_next;
  413. lprev = l;
  414. if (collect_mes && INSTR(l) == ps_mes) {
  415. message(l);
  416. }
  417. }
  418. *pp = (line_p) 0;
  419. return head;
  420. }
  421. bool getunit(gf,lf,kind_out,g_out,l_out,p_out,collect_mes)
  422. FILE *gf,*lf;
  423. short *kind_out;
  424. bblock_p *g_out;
  425. line_p *l_out;
  426. proc_p *p_out;
  427. bool collect_mes;
  428. {
  429. /* Read control flow graph (gf) and EM text (lf) of the next procedure.
  430. * A pointer to the proctable entry of the read procedure is
  431. * returned via p_out.
  432. * This routine also constructs the bmap and lpmap tables.
  433. * Note that we allocate structs for basic blocks and loops
  434. * at their first reference rather than at when we read them.
  435. */
  436. int n,i;
  437. bblock_p head, *pp, b;
  438. loop_p lp;
  439. curinp = gf;
  440. blength = getshort(); /* # basic blocks in this procedure */
  441. if (feof(curinp)) return FALSE;
  442. if (blength == 0) {
  443. /* data unit */
  444. *kind_out = LDATA;
  445. n = getshort();
  446. *l_out = getlines(lf,n,p_out,collect_mes);
  447. return TRUE;
  448. }
  449. *kind_out = LTEXT;
  450. bmap = (bblock_p *) newmap(blength); /* maps block_id on bblock_p */
  451. lplength = getshort(); /* # loops in this procedure */
  452. lpmap = (loop_p *) newmap(lplength); /* maps loop_id on loop_p */
  453. /* Read the basic blocks and the EM text */
  454. pp = &head; /* we use a pointer-to-a-pointer to link the structs */
  455. for (i = 0; i < blength; i++) {
  456. b = getblock(getshort());
  457. n = getshort(); /* #instructions in the block */
  458. b->b_succ = getlset(getblock);
  459. b->b_pred = getlset(getblock);
  460. b->b_idom = getblock(getshort());
  461. b->b_loops = getlset(getloop);
  462. b->b_flags = getshort();
  463. b->b_start = getlines(lf,n,p_out,collect_mes); /* read EM text */
  464. *pp = b;
  465. pp = &b->b_next;
  466. curinp = gf;
  467. }
  468. lastbid = blength; /* last block_id */
  469. /* read the information about loops */
  470. curproc->p_loops = Lempty_set();
  471. for (i = 0; i < lplength; i++) {
  472. lp = getloop(getshort());
  473. lp->lp_level = getshort(); /* nesting level */
  474. lp->lp_entry = getblock(getshort()); /* entry block of the loop */
  475. lp->lp_end = getblock(getshort()); /* tail of back edge of loop */
  476. Ladd((Lelem_t)lp,&curproc->p_loops);
  477. }
  478. *g_out = head;
  479. return TRUE;
  480. }