process.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Id$";
  3. #endif
  4. #include "param.h"
  5. #include "types.h"
  6. #include "tes.h"
  7. #include "assert.h"
  8. #include <em_spec.h>
  9. #include <em_pseu.h>
  10. #include "alloc.h"
  11. #include "line.h"
  12. #include "lookup.h"
  13. #include "proinf.h"
  14. #include "ext.h"
  15. /*
  16. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  17. * See the copyright notice in the ACK home directory, in the file "Copyright".
  18. *
  19. * Author: Hans van Staveren
  20. */
  21. void relabel();
  22. void symknown();
  23. void cleanlocals();
  24. void checklocs();
  25. void symvalue();
  26. void do_tes();
  27. void process()
  28. {
  29. if (wordsize == 0 || pointersize == 0)
  30. error("No MES EMX encountered");
  31. backward(); /* reverse and cleanup list */
  32. symknown(); /* symbol scope is now known */
  33. if (!nflag)
  34. symvalue(); /* give symbols value */
  35. if (prodepth != 0) {
  36. if (!nflag) {
  37. int npasses = 0;
  38. bool madeopt;
  39. checklocs(); /* check definition of locals */
  40. do {
  41. madeopt = peephole(); /* local optimization */
  42. relabel(); /* relabel local labels */
  43. flow(); /* throw away unreachable code */
  44. } while (madeopt && ++npasses < 5000);
  45. assert(!madeopt);
  46. }
  47. do_tes(); /* top elt. size computation phase */
  48. outpro(); /* generate PRO pseudo */
  49. outregs(); /* generate MES ms_reg pseudos */
  50. outtes(); /* generate MES ms_tes pseudos */
  51. }
  52. putlines(pseudos); /* pseudos first */
  53. if (prodepth != 0) {
  54. putlines(instrs); /* instructions next */
  55. outend(); /* generate END pseudo */
  56. cleanlocals(); /* forget instruction labels */
  57. } else if(instrs != (line_p) 0)
  58. error("instructions outside procedure");
  59. #ifdef COREDEBUG
  60. coreverbose();
  61. #endif
  62. }
  63. void relabel()
  64. {
  65. num_p *npp,np,tp;
  66. num_p repl,ttp;
  67. /*
  68. * For each label find its final destination after crossjumping.
  69. * Care has to be taken to prevent a loop in the program to
  70. * cause same in the optimizer.
  71. */
  72. for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++)
  73. for (np = *npp; np != (num_p) 0; np = np->n_next) {
  74. assert(! np->n_line ||
  75. ((np->n_line->l_instr&BMASK) == op_lab
  76. && np->n_line->l_a.la_np == np));
  77. for(tp=np; (tp->n_flags&(NUMKNOWN|NUMMARK))==0;
  78. tp = tp->n_repl)
  79. tp->n_flags |= NUMMARK;
  80. repl = tp->n_repl;
  81. for(tp=np; tp->n_flags&NUMMARK; tp = ttp) {
  82. ttp = tp->n_repl;
  83. tp->n_repl = repl;
  84. tp->n_flags &= ~ NUMMARK;
  85. tp->n_flags |= NUMKNOWN;
  86. }
  87. }
  88. for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++)
  89. for (np = *npp; np != (num_p) 0; np = np->n_next) {
  90. np->n_flags &= ~(NUMKNOWN|NUMSCAN|NUMREACH);
  91. np->n_jumps = 0;
  92. }
  93. }
  94. void symknown()
  95. {
  96. sym_p *spp,sp;
  97. for (spp = symhash; spp < &symhash[NSYMHASH]; spp++)
  98. for (sp = *spp; sp != (sym_p) 0; sp = sp->s_next)
  99. if (sp->s_flags & SYMSEEN)
  100. sp->s_flags |= SYMKNOWN;
  101. }
  102. void cleanlocals()
  103. {
  104. num_p *npp,np,tp;
  105. for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) {
  106. np = *npp;
  107. while (np != (num_p) 0) {
  108. tp = np->n_next;
  109. oldnum(np);
  110. np = tp;
  111. }
  112. *npp = (num_p) 0;
  113. }
  114. }
  115. void checklocs()
  116. {
  117. num_p *npp,np;
  118. for (npp=curpro.numhash; npp < & curpro.numhash[NNUMHASH]; npp++)
  119. for (np = *npp; np != (num_p) 0; np=np->n_next)
  120. if (np->n_line == (line_p) 0)
  121. error("local label %u undefined",
  122. (unsigned) np->n_number);
  123. }
  124. offset align(offset count, offset alignment)
  125. {
  126. assert(alignment==1||alignment==2||alignment==4);
  127. return((count+alignment-1)&~(alignment-1));
  128. }
  129. void symvalue()
  130. {
  131. line_p lp;
  132. sym_p sp;
  133. arg_p ap;
  134. argb_p abp;
  135. short curfrag = 0;
  136. offset count;
  137. for (lp=pseudos; lp != (line_p) 0; lp = lp->l_next)
  138. switch(lp->l_instr&BMASK) {
  139. default:
  140. assert(FALSE);
  141. case ps_sym:
  142. sp = lp->l_a.la_sp;
  143. if (sp->s_frag != curfrag) {
  144. count = 0;
  145. curfrag = sp->s_frag;
  146. }
  147. count = align(count,wordsize);
  148. sp->s_value = count;
  149. break;
  150. case ps_bss:
  151. case ps_hol:
  152. /* nothing to do, all bss pseudos are in diff frags */
  153. case ps_mes:
  154. break;
  155. case ps_con:
  156. case ps_rom:
  157. for (ap=lp->l_a.la_arg; ap != (arg_p) 0; ap = ap->a_next)
  158. switch(ap->a_typ) {
  159. default:
  160. assert(FALSE);
  161. case ARGOFF:
  162. count = align(count,wordsize)+wordsize;
  163. break;
  164. case ARGNUM:
  165. case ARGSYM:
  166. case ARGVAL:
  167. count = align(count,wordsize)+pointersize;
  168. break;
  169. case ARGICN:
  170. case ARGUCN:
  171. case ARGFCN:
  172. if (ap->a_a.a_con.ac_length < wordsize)
  173. count = align(count,(offset)ap->a_a.a_con.ac_length);
  174. else
  175. count = align(count,wordsize);
  176. count += ap->a_a.a_con.ac_length;
  177. break;
  178. case ARGSTR:
  179. for (abp = &ap->a_a.a_string; abp != (argb_p) 0;
  180. abp = abp->ab_next)
  181. count += abp->ab_index;
  182. break;
  183. }
  184. }
  185. }
  186. void do_tes()
  187. {
  188. line_p insptr = instrs, oldlin = NULL, oldlin2 = NULL;
  189. init_state();
  190. tes_pseudos();
  191. while (insptr != NULL) {
  192. tes_instr(insptr, oldlin, oldlin2);
  193. oldlin2 = oldlin;
  194. oldlin = insptr;
  195. insptr = insptr->l_next;
  196. }
  197. }