process.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Header$";
  3. #endif
  4. #include "param.h"
  5. #include "types.h"
  6. #include "shc.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_shc(); /* stackheight computation phase */
  41. outpro(); /* generate PRO pseudo */
  42. outregs(); /* generate MES ms_reg pseudos */
  43. outsth(); /* generate MES ms_sth 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. delete_labels();
  96. for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) {
  97. np = *npp;
  98. while (np != (num_p) 0) {
  99. tp = np->n_next;
  100. oldnum(np);
  101. np = tp;
  102. }
  103. *npp = (num_p) 0;
  104. }
  105. }
  106. checklocs() {
  107. register num_p *npp,np;
  108. for (npp=curpro.numhash; npp < & curpro.numhash[NNUMHASH]; npp++)
  109. for (np = *npp; np != (num_p) 0; np=np->n_next)
  110. if (np->n_line == (line_p) 0)
  111. error("local label %u undefined",
  112. (unsigned) np->n_number);
  113. }
  114. offset align(count,alignment) offset count,alignment; {
  115. assert(alignment==1||alignment==2||alignment==4);
  116. return((count+alignment-1)&~(alignment-1));
  117. }
  118. symvalue() {
  119. register line_p lp;
  120. register sym_p sp;
  121. register arg_p ap;
  122. register argb_p abp;
  123. short curfrag = 0;
  124. offset count;
  125. for (lp=pseudos; lp != (line_p) 0; lp = lp->l_next)
  126. switch(lp->l_instr&BMASK) {
  127. default:
  128. assert(FALSE);
  129. case ps_sym:
  130. sp = lp->l_a.la_sp;
  131. if (sp->s_frag != curfrag) {
  132. count = 0;
  133. curfrag = sp->s_frag;
  134. }
  135. count = align(count,wordsize);
  136. sp->s_value = count;
  137. break;
  138. case ps_bss:
  139. case ps_hol:
  140. /* nothing to do, all bss pseudos are in diff frags */
  141. case ps_mes:
  142. break;
  143. case ps_con:
  144. case ps_rom:
  145. for (ap=lp->l_a.la_arg; ap != (arg_p) 0; ap = ap->a_next)
  146. switch(ap->a_typ) {
  147. default:
  148. assert(FALSE);
  149. case ARGOFF:
  150. count = align(count,wordsize)+wordsize;
  151. break;
  152. case ARGNUM:
  153. case ARGSYM:
  154. case ARGVAL:
  155. count = align(count,wordsize)+pointersize;
  156. break;
  157. case ARGICN:
  158. case ARGUCN:
  159. case ARGFCN:
  160. if (ap->a_a.a_con.ac_length < wordsize)
  161. count = align(count,(offset)ap->a_a.a_con.ac_length);
  162. else
  163. count = align(count,wordsize);
  164. count += ap->a_a.a_con.ac_length;
  165. break;
  166. case ARGSTR:
  167. for (abp = &ap->a_a.a_string; abp != (argb_p) 0;
  168. abp = abp->ab_next)
  169. count += abp->ab_index;
  170. break;
  171. }
  172. }
  173. }
  174. do_shc()
  175. {
  176. register line_p insptr = instrs, oldlin = NULL;
  177. init_state();
  178. shc_pseudos();
  179. while (insptr != NULL) {
  180. insptr->l_prev = oldlin;
  181. oldlin = insptr;
  182. shc_instr(insptr);
  183. insptr = insptr->l_next;
  184. }
  185. }