put.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. /* P U T . C */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <em_pseu.h>
  10. #include <em_spec.h>
  11. #include "types.h"
  12. #include "global.h"
  13. #include "debug.h"
  14. #include "def.h"
  15. #include "map.h"
  16. #include "lset.h"
  17. #include "alloc.h"
  18. #include "put.h"
  19. FILE *curoutp;
  20. /* The output can be either 'typed' or 'untyped'. Typed data
  21. * consists of a value preceded by a byte specifying what kind
  22. * of value it is (e.g. 2 bytes constant, 4 bytes constant,
  23. * proc-id, lab-id, string etc.). Untyped data consists
  24. * of the value only. We use typed data for the EM text and
  25. * untyped data for all other files.
  26. */
  27. /* putlines */
  28. STATIC putstr();
  29. STATIC outlab();
  30. STATIC outobject();
  31. STATIC putargs(ap)
  32. register arg_p ap;
  33. {
  34. while (ap != (arg_p) 0) {
  35. outbyte((byte) ap->a_type & BMASK);
  36. switch(ap->a_type) {
  37. case ARGOFF:
  38. outoff(ap->a_a.a_offset);
  39. break;
  40. case ARGINSTRLAB:
  41. outlab(ap->a_a.a_instrlab);
  42. break;
  43. case ARGOBJECT:
  44. outobject(ap->a_a.a_obj);
  45. break;
  46. case ARGPROC:
  47. outproc(ap->a_a.a_proc);
  48. break;
  49. case ARGSTRING:
  50. putstr(&ap->a_a.a_string);
  51. break;
  52. case ARGICN:
  53. case ARGUCN:
  54. case ARGFCN:
  55. outshort(ap->a_a.a_con.ac_length);
  56. putstr(&ap->a_a.a_con.ac_con);
  57. break;
  58. }
  59. ap = ap->a_next;
  60. }
  61. outbyte((byte) ARGCEND);
  62. }
  63. STATIC putstr(abp) register argb_p abp; {
  64. register argb_p tbp;
  65. register length;
  66. length = 0;
  67. tbp = abp;
  68. while (tbp!= (argb_p) 0) {
  69. length += tbp->ab_index;
  70. tbp = tbp->ab_next;
  71. }
  72. outshort(length);
  73. while (abp != (argb_p) 0) {
  74. for (length=0;length<abp->ab_index;length++)
  75. outbyte( (byte) abp->ab_contents[length] );
  76. abp = abp->ab_next;
  77. }
  78. }
  79. outoff(off) offset off; {
  80. outshort( (short) (off&0177777L) );
  81. outshort( (short) (off>>16) );
  82. }
  83. outshort(i) short i; {
  84. outbyte( (byte) (i&BMASK) );
  85. outbyte( (byte) (i>>8) );
  86. }
  87. STATIC outint(i)
  88. int i;
  89. {
  90. /* Write an integer to the output file. This routine is
  91. * only used when outputting a bitvector-set. We expect an
  92. * integer to be either a short or a long.
  93. */
  94. if (sizeof(int) == sizeof(short)) {
  95. outshort(i);
  96. } else {
  97. assert (sizeof(int) == sizeof(offset));
  98. outoff((offset) i);
  99. }
  100. }
  101. STATIC outlab(lid) lab_id lid; {
  102. outshort((short) lid);
  103. }
  104. STATIC outobject(obj) obj_p obj; {
  105. outshort((short) obj->o_id);
  106. }
  107. outproc(p) proc_p p; {
  108. outshort((short) p->p_id);
  109. }
  110. short putlines(l,lf)
  111. line_p l;
  112. FILE *lf;
  113. {
  114. /* Output the list of em instructions headed by l.
  115. * Return the number of instruction written.
  116. */
  117. register line_p lnp;
  118. line_p next;
  119. short instr;
  120. short count= 0;
  121. curoutp = lf; /* Set f to the EM-text output file */
  122. for (lnp = l; lnp != (line_p) 0; lnp = next) {
  123. VL(lnp);
  124. count++;
  125. next = lnp->l_next;
  126. instr = INSTR(lnp);
  127. outbyte((byte) instr);
  128. outbyte((byte) TYPE(lnp));
  129. switch(TYPE(lnp)) {
  130. case OPSHORT:
  131. outshort(SHORT(lnp));
  132. break;
  133. case OPOFFSET:
  134. outoff(OFFSET(lnp));
  135. break;
  136. case OPINSTRLAB:
  137. outlab(INSTRLAB(lnp));
  138. break;
  139. case OPOBJECT:
  140. outobject(OBJ(lnp));
  141. break;
  142. case OPPROC:
  143. outproc(PROC(lnp));
  144. break;
  145. case OPLIST:
  146. putargs(ARG(lnp));
  147. break;
  148. }
  149. oldline(lnp);
  150. }
  151. return count;
  152. }
  153. /* putdtable */
  154. #define outmark(m) outbyte((byte) m)
  155. STATIC putobjects(obj)
  156. register obj_p obj;
  157. {
  158. while (obj != (obj_p) 0) {
  159. outmark(MARK_OBJ);
  160. outshort(obj->o_id);
  161. outoff(obj->o_size);
  162. outoff(obj->o_off);
  163. obj = obj->o_next;
  164. }
  165. }
  166. STATIC putvalues(arg)
  167. register arg_p arg;
  168. {
  169. while (arg != (arg_p) 0) {
  170. assert(arg->a_type == ARGOFF);
  171. outmark(MARK_ARG);
  172. outoff(arg->a_a.a_offset);
  173. arg = arg->a_next;
  174. }
  175. }
  176. putdtable(head,df)
  177. dblock_p head;
  178. FILE *df;
  179. {
  180. /* Write the datablock table to the data block file df. */
  181. register dblock_p dbl;
  182. register obj_p obj;
  183. dblock_p next;
  184. register short n = 0;
  185. curoutp = df; /* set f to the data block output file */
  186. /* Count the number of objects */
  187. for (dbl = head; dbl != (dblock_p) 0; dbl = dbl->d_next) {
  188. for (obj = dbl->d_objlist; obj != (obj_p) 0;
  189. obj = obj->o_next) {
  190. n++;
  191. }
  192. }
  193. outshort(n); /* The table is preceded by #objects . */
  194. for (dbl = head; dbl != (dblock_p) 0; dbl = next) {
  195. next = dbl->d_next;
  196. outmark(MARK_DBLOCK);
  197. outshort(dbl->d_id);
  198. outbyte(dbl->d_pseudo);
  199. outoff(dbl->d_size);
  200. outshort(dbl->d_fragmnr);
  201. outbyte(dbl->d_flags1);
  202. putobjects(dbl->d_objlist);
  203. putvalues(dbl->d_values);
  204. olddblock(dbl);
  205. }
  206. fclose(curoutp);
  207. if (omap != (obj_p *) 0) {
  208. oldmap((short **) omap,olength); /* release memory for omap */
  209. }
  210. }
  211. /* putptable */
  212. STATIC outcset(s)
  213. cset s;
  214. {
  215. /* A 'compact' set is represented externally as a row of words
  216. * (its bitvector) preceded by its length.
  217. */
  218. register short i;
  219. outshort(s->v_size);
  220. for (i = 0; i <= DIVWL(s->v_size - 1); i++) {
  221. outint(s->v_bits[i]);
  222. }
  223. }
  224. putptable(head,pf,all)
  225. proc_p head;
  226. FILE *pf;
  227. bool all;
  228. {
  229. register proc_p p;
  230. proc_p next;
  231. register short n = 0;
  232. /* Write the proc table */
  233. curoutp = pf;
  234. /* Determine the number of procs */
  235. for (p = head; p != (proc_p) 0; p = p->p_next) {
  236. n++;
  237. }
  238. outshort(n); /* The table is preceded by its length. */
  239. outshort ((all?1:0)); /* if all=false, only some of the attributes
  240. are written. */
  241. for (p = head; p != (proc_p) 0; p = next) {
  242. next = p->p_next;
  243. outshort(p->p_id);
  244. outbyte(p->p_flags1);
  245. if (p->p_flags1 & PF_BODYSEEN) {
  246. /* If we have no access to the EM text of the
  247. * body of a procedure, we have no information
  248. * about it whatsoever, so there is nothing
  249. * to output in that case.
  250. */
  251. outshort(p->p_nrlabels);
  252. outoff(p->p_localbytes);
  253. outoff(p->p_nrformals);
  254. if (all) {
  255. outcset(p->p_change->c_ext);
  256. outshort(p->p_change->c_flags);
  257. outshort(p->p_use->u_flags);
  258. outcset(p->p_calling);
  259. Cdeleteset(p->p_change->c_ext);
  260. oldchange(p->p_change);
  261. olduse(p->p_use);
  262. Cdeleteset(p->p_calling);
  263. }
  264. }
  265. oldproc(p);
  266. }
  267. fclose(curoutp);
  268. if (pmap != (proc_p *) 0) {
  269. oldmap((short **) pmap,plength); /* release memory for pmap */
  270. }
  271. }
  272. /* putunit */
  273. STATIC outloop(l)
  274. loop_p l;
  275. {
  276. outshort((short) l->lp_id);
  277. }
  278. STATIC outblock(b)
  279. bblock_p b;
  280. {
  281. if (b == (bblock_p) 0) {
  282. outshort((short) 0);
  283. } else {
  284. outshort((short) b->b_id);
  285. }
  286. }
  287. STATIC outid(e,p)
  288. Lelem_t e;
  289. int (*p) ();
  290. {
  291. /* Auxiliary routine used by outlset. */
  292. /* NOSTRICT */
  293. (*p) (e);
  294. }
  295. STATIC outlset(s,p)
  296. lset s;
  297. int (*p) ();
  298. {
  299. /* A 'long' set is represented externally as a
  300. * a sequence of elements terminated by a 0 word.
  301. * The procedural parameter p is a routine that
  302. * prints an id (proc_id, obj_id etc.).
  303. */
  304. register Lindex i;
  305. for (i = Lfirst(s); i != (Lindex) 0; i = Lnext(i,s)) {
  306. outid(Lelem(i),p);
  307. }
  308. outshort((short) 0);
  309. }
  310. putunit(kind,p,l,gf,lf)
  311. short kind;
  312. proc_p p;
  313. line_p l;
  314. FILE *gf, *lf;
  315. {
  316. register bblock_p b;
  317. register short n = 0;
  318. Lindex pi;
  319. bblock_p nextb;
  320. loop_p lp;
  321. curoutp = gf;
  322. if (kind == LDATA) {
  323. outshort(0); /* No basic blocks */
  324. n = putlines(l,lf);
  325. curoutp = gf;
  326. outshort(n);
  327. return;
  328. }
  329. /* Determine the number of basic blocks */
  330. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  331. n++;
  332. }
  333. outshort(n); /* # basic blocks */
  334. outshort(Lnrelems(p->p_loops)); /* # loops */
  335. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  336. n = putlines(b->b_start,lf);
  337. curoutp = gf;
  338. outblock(b); /* put its block_id */
  339. outshort(n); /* #instructions of the block */
  340. outlset(b->b_succ, outblock); /* put succ set */
  341. outlset(b->b_pred, outblock); /* put pred set */
  342. outblock(b->b_idom); /* put id of immediate dominator */
  343. outlset(b->b_loops, outloop); /* put loop set */
  344. outshort(b->b_flags);
  345. }
  346. /* The Control Flow Graph of every procedure is followed
  347. * by a description of the loops of the procedure.
  348. * Every loop contains an id, an entry block and a level.
  349. */
  350. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  351. pi = Lnext(pi,p->p_loops)) {
  352. lp = (loop_p) Lelem(pi);
  353. outloop(lp); /* id */
  354. outshort(lp->lp_level); /* nesting level */
  355. outblock(lp->lp_entry); /* loop entry block */
  356. outblock(lp->lp_end);
  357. oldloop(lp);
  358. }
  359. Ldeleteset(p->p_loops);
  360. /* We will now release the memory of the basic blocks.
  361. * Note that it would be incorrect to release a basic block
  362. * after it has been written, because there may be references
  363. * to it from other (later) blocks.
  364. */
  365. for (b = p->p_start; b != (bblock_p) 0; b = nextb) {
  366. Ldeleteset(b->b_loops);
  367. Ldeleteset(b->b_succ);
  368. Ldeleteset(b->b_pred);
  369. /* Must read b->b_next before releasing b */
  370. nextb = b->b_next;
  371. oldbblock(b);
  372. }
  373. /* Release the memory for the lmap, lbmap, bmap, lpmap tables */
  374. if (lmap != (line_p *) 0) oldmap((short **) lmap,llength);
  375. if (lbmap != (bblock_p *) 0) oldmap((short **) lbmap,llength);
  376. if (bmap != (bblock_p *) 0) oldmap((short **) bmap,blength);
  377. if (lpmap != (loop_p *) 0) oldmap((short **) lpmap,lplength);
  378. curoutp = lf;
  379. }