backward.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "line.h"
  12. #include "lookup.h"
  13. #include "alloc.h"
  14. #include "proinf.h"
  15. #include <em_spec.h>
  16. #include <em_pseu.h>
  17. #include <em_mnem.h>
  18. #include <em_mes.h>
  19. #include "ext.h"
  20. #include "reg.h"
  21. #include "util.h"
  22. #include "getline.h"
  23. #define local(x) ((((x)->s_flags&SYMKNOWN) == 0 && \
  24. ((x)->s_flags &= ~ SYMGLOBAL)),\
  25. (x)->s_flags |= SYMSEEN)
  26. #define global(x) ((((x)->s_flags&SYMKNOWN) == 0 && \
  27. ((x)->s_flags |= SYMGLOBAL)), \
  28. (x)->s_flags |= SYMSEEN)
  29. #define DTYPHOL 1
  30. #define DTYPBSS 2
  31. #define DTYPCON 3
  32. #define DTYPROM 4
  33. byte curdtyp;
  34. bool goodrom;
  35. short curfrag = 3; /* see also peephole.c */
  36. offset rombuf[MAXROM];
  37. int rc;
  38. void backward()
  39. {
  40. line_p lnp;
  41. line_p next;
  42. arg_p ap;
  43. line_p i,p;
  44. int n;
  45. sym_p sp;
  46. i = p = (line_p) 0;
  47. curdtyp=0;
  48. for (lnp = curpro.lastline; lnp != (line_p) 0; lnp = next) {
  49. next = lnp->l_next;
  50. switch(lnp->l_optyp) {
  51. case OPSYMBOL:
  52. global(lnp->l_a.la_sp);
  53. break;
  54. case OPSVAL:
  55. global(lnp->l_a.la_sval.lasv_sp);
  56. break;
  57. case OPLVAL:
  58. global(lnp->l_a.la_lval.lalv_sp);
  59. break;
  60. case OPLIST:
  61. ap = lnp->l_a.la_arg;
  62. while (ap != (arg_p) 0 ) {
  63. switch(ap->a_typ) {
  64. case ARGSYM:
  65. global(ap->a_a.a_sp);
  66. break;
  67. case ARGVAL:
  68. global(ap->a_a.a_val.av_sp);
  69. }
  70. ap = ap->a_next;
  71. }
  72. break;
  73. }
  74. /*
  75. * references to symbols are processed now.
  76. * for plain instructions nothing else is needed
  77. */
  78. switch(lnp->l_instr&BMASK) {
  79. /*
  80. * count all local occurences for register counts;
  81. * op_lal is omitted and not by accident.
  82. */
  83. case op_del:
  84. case op_inl:
  85. case op_ldl:
  86. case op_lil:
  87. case op_lol:
  88. case op_sdl:
  89. case op_sil:
  90. case op_stl:
  91. case op_zrl:
  92. switch(lnp->l_optyp) {
  93. case OPNO:
  94. case OPNUMLAB:
  95. case OPSYMBOL:
  96. case OPSVAL:
  97. case OPLVAL:
  98. case OPLIST:
  99. break;
  100. case OPOFFSET:
  101. incregusage(lnp->l_a.la_offset);
  102. break;
  103. case OPSHORT:
  104. incregusage((offset)lnp->l_a.la_short);
  105. break;
  106. default:
  107. incregusage((offset)(lnp->l_optyp&BMASK)-Z_OPMINI);
  108. break;
  109. }
  110. /* fall through !! */
  111. default:
  112. assert((lnp->l_instr&BMASK)<=op_last);
  113. lnp->l_next = i;
  114. i = lnp;
  115. continue;
  116. case ps_sym:
  117. sp = lnp->l_a.la_sp;
  118. local(sp);
  119. if (curdtyp == DTYPROM && goodrom) {
  120. sp->s_rom = newrom();
  121. for (n=0;n<rc;n++)
  122. sp->s_rom[n] = rombuf[n];
  123. }
  124. sp->s_frag = curfrag;
  125. break;
  126. case ps_hol:
  127. curdtyp = DTYPHOL;
  128. curfrag++;
  129. break;
  130. case ps_bss:
  131. curdtyp = DTYPBSS;
  132. curfrag++;
  133. break;
  134. case ps_con:
  135. if (curdtyp != DTYPCON) {
  136. curdtyp = DTYPCON;
  137. curfrag++;
  138. }
  139. break;
  140. case ps_rom:
  141. if (curdtyp != DTYPROM) {
  142. curdtyp = DTYPROM;
  143. curfrag++;
  144. }
  145. ap = lnp->l_a.la_arg;
  146. rc = 0;
  147. while (ap != (arg_p) 0 && rc < MAXROM) {
  148. if (ap->a_typ == ARGOFF) {
  149. rombuf[rc++] = ap->a_a.a_offset;
  150. ap = ap->a_next;
  151. } else
  152. ap = (arg_p) 0;
  153. }
  154. goodrom = (rc >= 2);
  155. break;
  156. case ps_mes:
  157. if (prodepth != 0 &&
  158. ((int) aoff(lnp->l_a.la_arg, 0) == ms_std ||
  159. (int) aoff(lnp->l_a.la_arg, 0) == ms_stb ||
  160. (int) aoff(lnp->l_a.la_arg, 0) == ms_ego)) {
  161. lnp->l_next = i;
  162. i = lnp;
  163. continue;
  164. }
  165. break;
  166. case ps_inp:
  167. case ps_ina:
  168. local(lnp->l_a.la_sp);
  169. case ps_exp:
  170. case ps_exa:
  171. case ps_exc:
  172. oldline(lnp);
  173. continue;
  174. }
  175. lnp->l_next = p;
  176. p = lnp;
  177. }
  178. if (prodepth != 0)
  179. local(curpro.symbol);
  180. instrs = i; pseudos = p; curpro.lastline = (line_p) 0;
  181. }