process.c 4.7 KB

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