cs_avail.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* M O D U L E F O R A C C E S S S I N G T H E L I S T
  7. *
  8. * O F A V A I L A B L E E X P R E S S I O N S
  9. */
  10. #include <em_mnem.h>
  11. #include "../share/types.h"
  12. #include "../share/debug.h"
  13. #include "../share/aux.h"
  14. #include "../share/lset.h"
  15. #include "../share/global.h"
  16. #include "cs.h"
  17. #include "cs_aux.h"
  18. #include "cs_debug.h"
  19. #include "cs_alloc.h"
  20. #include "cs_getent.h"
  21. avail_p avails; /* The list of available expressions. */
  22. static bool commutative(int instr)
  23. {
  24. /* Is instr a commutative operator? */
  25. switch (instr) {
  26. case op_adf: case op_adi: case op_adu: case op_and:
  27. case op_cms: case op_ior: case op_mlf: case op_mli:
  28. case op_mlu:
  29. return TRUE;
  30. default:
  31. return FALSE;
  32. }
  33. }
  34. static bool same_avail(byte kind, avail_p avp1, avail_p avp2)
  35. {
  36. /* Two expressions are the same if they have the same operator,
  37. * the same size, and their operand(s) have the same value.
  38. * Only if the operator is commutative, the order of the operands
  39. * does not matter.
  40. */
  41. if (avp1->av_instr != avp2->av_instr) return FALSE;
  42. if (avp1->av_size != avp2->av_size) return FALSE;
  43. switch (kind) {
  44. default:
  45. assert(FALSE);
  46. break;
  47. case EXPENSIVE_LOAD:
  48. case UNAIR_OP:
  49. return avp1->av_operand == avp2->av_operand;
  50. case BINAIR_OP:
  51. if (commutative(avp1->av_instr & BMASK))
  52. return ( (avp1->av_oleft == avp2->av_oleft) &&
  53. (avp1->av_oright == avp2->av_oright) )
  54. ||
  55. ( (avp1->av_oleft == avp2->av_oright) &&
  56. (avp1->av_oright == avp2->av_oleft) )
  57. ;
  58. else
  59. return (avp1->av_oleft == avp2->av_oleft) &&
  60. (avp1->av_oright == avp2->av_oright);
  61. case TERNAIR_OP:
  62. return (avp1->av_ofirst == avp2->av_ofirst) &&
  63. (avp1->av_osecond == avp2->av_osecond) &&
  64. (avp1->av_othird == avp2->av_othird);
  65. }
  66. /* NOTREACHED */
  67. return 0;
  68. }
  69. static void check_local(avail_p avp)
  70. {
  71. /* Check if the local in which the result of avp was stored,
  72. * still holds this result. Update if not.
  73. */
  74. if (avp->av_saveloc == (entity_p) 0) return; /* Nothing to check. */
  75. if (avp->av_saveloc->en_vn != avp->av_result) {
  76. OUTTRACE("save local changed value", 0);
  77. avp->av_saveloc = (entity_p) 0;
  78. }
  79. }
  80. static entity_p result_local(offset size, line_p l)
  81. {
  82. /* If the result of an expression of size bytes is stored into a
  83. * local for which a registermessage was generated, return a pointer
  84. * to this local.
  85. */
  86. line_p dummy;
  87. entity_p enp;
  88. if (l == (line_p) 0)
  89. return (entity_p) 0;
  90. if ( ((INSTR(l)==op_stl) && (size==ws)) || ((INSTR(l)==op_sdl) && (size==2*ws)) ) {
  91. enp = getentity(l, &dummy);
  92. if (is_regvar(enp->en_loc)) {
  93. OUTTRACE("save local found, %ld(LB)", enp->en_loc);
  94. return enp;
  95. }
  96. }
  97. return (entity_p) 0;
  98. }
  99. static void copy_avail(int kind, avail_p src, avail_p dst)
  100. {
  101. /* Copy some attributes from src to dst. */
  102. dst->av_instr = src->av_instr;
  103. dst->av_size = src->av_size;
  104. switch (kind) {
  105. default:
  106. assert(FALSE);
  107. break;
  108. case EXPENSIVE_LOAD:
  109. case UNAIR_OP:
  110. dst->av_operand = src->av_operand;
  111. break;
  112. case BINAIR_OP:
  113. dst->av_oleft = src->av_oleft;
  114. dst->av_oright = src->av_oright;
  115. break;
  116. case TERNAIR_OP:
  117. dst->av_ofirst = src->av_ofirst;
  118. dst->av_osecond = src->av_osecond;
  119. dst->av_othird = src->av_othird;
  120. break;
  121. }
  122. }
  123. avail_p av_enter(avail_p avp, occur_p ocp, int kind)
  124. {
  125. /* Put the available expression avp in the list,
  126. * if it is not already there.
  127. * Add ocp to the set of occurrences of this expression.
  128. */
  129. avail_p ravp;
  130. line_p last = ocp->oc_llast;
  131. for (ravp = avails; ravp != (avail_p) 0; ravp = ravp->av_before) {
  132. if (same_avail(kind, ravp, avp)) { /* It was there. */
  133. Ladd(ocp, &ravp->av_occurs);
  134. /* Can we still use the local in which
  135. * the result was stored?
  136. */
  137. check_local(ravp);
  138. return ravp;
  139. }
  140. }
  141. /* A new available axpression. */
  142. ravp = newavail();
  143. /* Remember local, if any, that holds result. */
  144. if (avp->av_instr != (byte) INSTR(last)) {
  145. /* Only possible when instr is the implicit AAR in
  146. * a LAR or SAR.
  147. */
  148. ravp->av_saveloc = (entity_p) 0;
  149. } else {
  150. ravp->av_saveloc = result_local(avp->av_size, last->l_next);
  151. }
  152. ravp->av_found = last;
  153. ravp->av_result = kind == EXPENSIVE_LOAD? avp->av_operand: newvalnum();
  154. copy_avail(kind, avp, ravp);
  155. oldoccur(ocp);
  156. ravp->av_before = avails;
  157. avails = ravp;
  158. return ravp;
  159. }
  160. void clr_avails()
  161. {
  162. /* Throw away the information about the available expressions. */
  163. avail_p ravp, next;
  164. Lindex i;
  165. lset s;
  166. for (ravp = avails; ravp != (avail_p) 0; ravp = next) {
  167. next = ravp->av_before;
  168. s = ravp->av_occurs;
  169. for (i = Lfirst(s); i != (Lindex) 0; i = Lnext(i, s)) {
  170. oldoccur(occ_elem(i));
  171. }
  172. Ldeleteset(s);
  173. oldavail(ravp);
  174. }
  175. avails = (avail_p) 0;
  176. }