process.c 4.7 KB

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