get.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* $Header$ */
  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 "types.h"
  12. #include "def.h"
  13. #include "debug.h"
  14. #include "global.h"
  15. #include "lset.h"
  16. #include "cset.h"
  17. #include "get.h"
  18. #include "alloc.h"
  19. #include "map.h"
  20. #include "aux.h"
  21. #include "../../../h/em_spec.h"
  22. #include "../../../h/em_mnem.h"
  23. #include "../../../h/em_pseu.h"
  24. #include "../../../h/em_mes.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, *dp;
  192. obj_p obj, *op;
  193. arg_p arg, *ap;
  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. dp = &head;
  200. if ((curinp = fopen(dname,"r")) == NULL) {
  201. error("cannot open %s", dname);
  202. }
  203. olength = getshort();
  204. assert(olength >= 0);
  205. assert(olength < 5000); /* See if its a reasonable number */
  206. /* total number of objects */
  207. omap = (obj_p *) newmap(olength); /* allocate omap table */
  208. while (TRUE) {
  209. n = getmark();
  210. if (feof(curinp)) break;
  211. switch(n) {
  212. case MARK_DBLOCK:
  213. d = *dp = newdblock();
  214. op = &d->d_objlist;
  215. ap = &d->d_values;
  216. dp = &d->d_next;
  217. d->d_id = getshort();
  218. d->d_pseudo = getbyte();
  219. d->d_size = getoff();
  220. d->d_fragmnr = getshort();
  221. d->d_flags1 = getbyte();
  222. break;
  223. case MARK_OBJ:
  224. obj = *op = newobject();
  225. op = &obj->o_next;
  226. obj->o_dblock = d;
  227. obj->o_id = getshort();
  228. assert(obj->o_id >0);
  229. assert(obj->o_id <= olength);
  230. omap[obj->o_id] = obj;
  231. obj->o_size = getoff();
  232. obj->o_off = getoff();
  233. break;
  234. case MARK_ARG:
  235. arg = *ap = newarg(ARGOFF);
  236. ap = &arg->a_next;
  237. arg->a_a.a_offset = getoff();
  238. break;
  239. default:
  240. assert(FALSE);
  241. }
  242. }
  243. OUTTRACE("have read data table, %d objects",olength);
  244. return head;
  245. }
  246. /* getbblocks */
  247. STATIC argstring(length,abp)
  248. short length;
  249. register argb_p abp;
  250. {
  251. while (length--) {
  252. if (abp->ab_index == NARGBYTES)
  253. abp = abp->ab_next = newargb();
  254. abp->ab_contents[abp->ab_index++] = getbyte();
  255. }
  256. }
  257. STATIC arg_p readargs()
  258. {
  259. /* Read a list of arguments and allocate structures
  260. * for them. Return a pointer to the head of the list.
  261. */
  262. arg_p head, arg, *ap;
  263. byte t;
  264. short length;
  265. ap = &head;
  266. for (;;) {
  267. /* every argument list is terminated by an
  268. * ARGCEND byte in Intermediate Code.
  269. */
  270. t = getbyte();
  271. if (t == (byte) ARGCEND) {
  272. return head;
  273. }
  274. arg = *ap = newarg(t);
  275. ap = &arg->a_next;
  276. switch((short) t) {
  277. case ARGOFF:
  278. arg->a_a.a_offset = getoff();
  279. break;
  280. case ARGINSTRLAB:
  281. arg->a_a.a_instrlab = getshort();
  282. break;
  283. case ARGOBJECT:
  284. arg->a_a.a_obj = omap[getshort()];
  285. /* Read an object identifier (o_id)
  286. * and use the omap table to obtain
  287. * a pointer to the rigth obj struct.
  288. */
  289. break;
  290. case ARGPROC:
  291. arg->a_a.a_proc = pmap[getshort()];
  292. /* Read a procedure identifier (p_id) */
  293. break;
  294. case ARGSTRING:
  295. length = getshort();
  296. argstring(length, &arg->a_a.a_string);
  297. break;
  298. case ARGICN:
  299. case ARGUCN:
  300. case ARGFCN:
  301. length = getshort();
  302. arg->a_a.a_con.ac_length = length;
  303. /* size of the constant */
  304. argstring(getshort(),
  305. &arg->a_a.a_con.ac_con);
  306. break;
  307. default:
  308. assert(FALSE);
  309. }
  310. }
  311. }
  312. line_p read_line(p_out)
  313. proc_p *p_out;
  314. {
  315. /* Read a line of EM code (i.e. one instruction)
  316. * and its arguments (if any).
  317. * In Intermediate Code, the first byte is the
  318. * instruction code and the second byte denotes the kind
  319. * of operand(s) that follow.
  320. */
  321. line_p lnp;
  322. byte instr;
  323. instr = getbyte();
  324. if (feof(curinp)) return (line_p) 0;
  325. lnp = newline(getbyte());
  326. linecount++;
  327. lnp->l_instr = instr;
  328. switch(TYPE(lnp)) {
  329. /* read the operand(s) */
  330. case OPSHORT:
  331. SHORT(lnp) = getshort();
  332. break;
  333. case OPOFFSET:
  334. OFFSET(lnp) = getoff();
  335. break;
  336. case OPINSTRLAB:
  337. INSTRLAB(lnp) = getshort();
  338. if ((instr & BMASK) == op_lab) {
  339. /* defining occurrence of an
  340. * instruction label.
  341. */
  342. lmap[INSTRLAB(lnp)] = lnp;
  343. }
  344. break;
  345. case OPOBJECT:
  346. OBJ(lnp) = omap[getshort()];
  347. break;
  348. case OPPROC:
  349. PROC(lnp) = pmap[getshort()];
  350. if ((instr & BMASK) == ps_pro) {
  351. /* enter new procedure: allocate a
  352. * label map and a label-block map table.
  353. */
  354. *p_out = PROC(lnp);
  355. llength = (*p_out)->p_nrlabels;
  356. lmap = (line_p *) newmap(llength);
  357. /* maps lab_id to line structure */
  358. lbmap = (bblock_p *) newmap(llength);
  359. /* maps lab_id to bblock structure */
  360. lastlabid = llength;
  361. }
  362. break;
  363. case OPLIST:
  364. ARG(lnp) = readargs();
  365. break;
  366. default:
  367. assert(TYPE(lnp) == OPNO);
  368. }
  369. return lnp;
  370. }
  371. STATIC message(lnp)
  372. line_p lnp;
  373. {
  374. /* See if lnp is some useful message.
  375. * (e.g. a message telling that a certain local variable
  376. * will never be referenced indirectly, so it may be put
  377. * in a register. If so, add it to the mesregs set.)
  378. */
  379. assert(ARG(lnp)->a_type == ARGOFF);
  380. switch((int) aoff(ARG(lnp),0)) {
  381. case ms_reg:
  382. if (ARG(lnp)->a_next != (arg_p) 0) {
  383. /* take only "mes 3" with further arguments */
  384. Ladd(lnp,&mesregs);
  385. }
  386. break;
  387. case ms_err:
  388. error("ms_err encountered");
  389. case ms_opt:
  390. error("ms_opt encountered");
  391. case ms_emx:
  392. ws = aoff(ARG(lnp),1);
  393. ps = aoff(ARG(lnp),2);
  394. break;
  395. }
  396. }
  397. line_p getlines(lf,n,p_out,collect_mes)
  398. FILE *lf;
  399. int n;
  400. proc_p *p_out;
  401. bool collect_mes;
  402. {
  403. /* Read n lines of EM text and doubly link them.
  404. * Also process messages.
  405. */
  406. line_p head, *pp, l, lprev;
  407. curinp = lf; /* EM input file */
  408. pp = &head;
  409. lprev = (line_p) 0;
  410. while (n--) {
  411. l = *pp = read_line(p_out);
  412. PREV(l) = lprev;
  413. pp = &l->l_next;
  414. lprev = l;
  415. if (collect_mes && INSTR(l) == ps_mes) {
  416. message(l);
  417. }
  418. }
  419. *pp = (line_p) 0;
  420. return head;
  421. }
  422. bool getunit(gf,lf,kind_out,g_out,l_out,p_out,collect_mes)
  423. FILE *gf,*lf;
  424. short *kind_out;
  425. bblock_p *g_out;
  426. line_p *l_out;
  427. proc_p *p_out;
  428. bool collect_mes;
  429. {
  430. /* Read control flow graph (gf) and EM text (lf) of the next procedure.
  431. * A pointer to the proctable entry of the read procedure is
  432. * returned via p_out.
  433. * This routine also constructs the bmap and lpmap tables.
  434. * Note that we allocate structs for basic blocks and loops
  435. * at their first reference rather than at when we read them.
  436. */
  437. int n,i;
  438. bblock_p head, *pp, b;
  439. loop_p lp;
  440. curinp = gf;
  441. blength = getshort(); /* # basic blocks in this procedure */
  442. if (feof(curinp)) return FALSE;
  443. if (blength == 0) {
  444. /* data unit */
  445. *kind_out = LDATA;
  446. n = getshort();
  447. *l_out = getlines(lf,n,p_out,collect_mes);
  448. return TRUE;
  449. }
  450. *kind_out = LTEXT;
  451. bmap = (bblock_p *) newmap(blength); /* maps block_id on bblock_p */
  452. lplength = getshort(); /* # loops in this procedure */
  453. lpmap = (loop_p *) newmap(lplength); /* maps loop_id on loop_p */
  454. /* Read the basic blocks and the EM text */
  455. pp = &head; /* we use a pointer-to-a-pointer to link the structs */
  456. for (i = 0; i < blength; i++) {
  457. b = getblock(getshort());
  458. n = getshort(); /* #instructions in the block */
  459. b->b_succ = getlset(getblock);
  460. b->b_pred = getlset(getblock);
  461. b->b_idom = getblock(getshort());
  462. b->b_loops = getlset(getloop);
  463. b->b_flags = getshort();
  464. b->b_start = getlines(lf,n,p_out,collect_mes); /* read EM text */
  465. *pp = b;
  466. pp = &b->b_next;
  467. curinp = gf;
  468. }
  469. lastbid = blength; /* last block_id */
  470. /* read the information about loops */
  471. curproc->p_loops = Lempty_set();
  472. for (i = 0; i < lplength; i++) {
  473. lp = getloop(getshort());
  474. lp->lp_level = getshort(); /* nesting level */
  475. lp->lp_entry = getblock(getshort()); /* entry block of the loop */
  476. lp->lp_end = getblock(getshort()); /* tail of back edge of loop */
  477. Ladd(lp,&curproc->p_loops);
  478. }
  479. *g_out = head;
  480. return TRUE;
  481. }