put.c 8.8 KB

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