aux.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /* S H A R E D F I L E
  7. *
  8. * A U X I L I A R Y R O U T I N E S
  9. *
  10. */
  11. #include <em_mes.h>
  12. #include <em_pseu.h>
  13. #include "types.h"
  14. #include "debug.h"
  15. #include "global.h"
  16. #include "alloc.h"
  17. #include "aux.h"
  18. #include "map.h"
  19. #include "lset.h"
  20. offset off_set(line_p lnp)
  21. {
  22. switch(lnp->l_optype) {
  23. case OPSHORT:
  24. return (offset) SHORT(lnp);
  25. case OPOFFSET:
  26. return OFFSET(lnp);
  27. default:
  28. assert(FALSE);
  29. }
  30. /* NOTREACHED */
  31. return 0;
  32. }
  33. offset aoff(arg_p ap, int n)
  34. {
  35. while (n>0) {
  36. if (ap != (arg_p) 0)
  37. ap = ap->a_next;
  38. n--;
  39. }
  40. if (ap == (arg_p) 0)
  41. error("too few parameters");
  42. if (ap->a_type != ARGOFF)
  43. error("offset expected");
  44. return(ap->a_a.a_offset);
  45. }
  46. offset tmplocal(proc_p p, offset size)
  47. {
  48. /* Allocate a new local variable in the stack frame of p */
  49. p->p_localbytes += size;
  50. return -(p->p_localbytes);
  51. }
  52. line_p int_line(offset off)
  53. {
  54. /* Allocate a line struct of type OPSHORT or OPOFFSET,
  55. * whichever one fits best.
  56. */
  57. line_p lnp;
  58. if ((short) off == off) {
  59. /* fits in a short */
  60. lnp = newline(OPSHORT);
  61. SHORT(lnp) = (short) off;
  62. } else {
  63. lnp = newline(OPOFFSET);
  64. OFFSET(lnp) = off;
  65. }
  66. return lnp;
  67. }
  68. line_p reg_mes(offset tmp, short size, int typ, int score)
  69. {
  70. /* Generate a register message */
  71. line_p l;
  72. arg_p a;
  73. #define NEXTARG(a,val) a->a_next = newarg(ARGOFF); a = a->a_next; \
  74. a->a_a.a_offset = val
  75. l = newline(OPLIST);
  76. l->l_instr = ps_mes;
  77. a = ARG(l) = newarg(ARGOFF);
  78. a->a_a.a_offset = ms_reg;
  79. NEXTARG(a,tmp);
  80. NEXTARG(a,size);
  81. NEXTARG(a,typ);
  82. NEXTARG(a,score);
  83. return l;
  84. }
  85. bool dom(bblock_p b1, bblock_p b2)
  86. {
  87. /* See if b1 dominates b2. Note that a block always
  88. * dominates itself.
  89. */
  90. register bblock_p b;
  91. for (b = b2; b != (bblock_p) 0; b = b->b_idom) {
  92. /* See if b1 is a (not necessarily proper) ancestor
  93. * of b2 in the immediate dominator tree.
  94. */
  95. if (b == b1) return TRUE;
  96. }
  97. return FALSE;
  98. }
  99. bblock_p common_dom(bblock_p a, bblock_p b)
  100. {
  101. /* find a basic block that dominates a as well as b;
  102. * note that a basic block also dominates itself.
  103. */
  104. assert (a != (bblock_p) 0);
  105. assert (b != (bblock_p) 0);
  106. if (dom(a,b)) {
  107. return a;
  108. } else {
  109. if (dom(b,a)) {
  110. return b;
  111. } else {
  112. return common_dom(a->b_idom,b->b_idom);
  113. }
  114. }
  115. }
  116. #define R time_space_ratio
  117. short add_timespace(short time, short space)
  118. {
  119. /* Add together a time and space, using the time_space_ratio
  120. * parameter that may be set by the user, indicating the need
  121. * to optimize for time, space or something in between.
  122. */
  123. return (R * time + (100 - R) * space) / 100;
  124. }
  125. void rm_line(line_p l, bblock_p b)
  126. {
  127. if (b->b_start == l) {
  128. b->b_start = l->l_next;
  129. } else {
  130. PREV(l)->l_next = l->l_next;
  131. }
  132. if (l->l_next != (line_p) 0) {
  133. PREV(l->l_next) = PREV(l);
  134. }
  135. oldline(l);
  136. }
  137. void appnd_line(line_p l1, line_p l2)
  138. {
  139. /* Put l1 after l2 */
  140. PREV(l1) = l2;
  141. l1->l_next = l2->l_next;
  142. l2->l_next = l1;
  143. if (l1->l_next != (line_p) 0) {
  144. PREV(l1->l_next) = l1;
  145. }
  146. }
  147. line_p last_instr(bblock_p b)
  148. {
  149. /* Determine the last line of a list */
  150. register line_p l = b->b_start;
  151. if (l == (line_p) 0) return (line_p) 0;
  152. while (l->l_next != (line_p) 0) l = l->l_next;
  153. return l;
  154. }
  155. line_p find_mesreg(offset off)
  156. {
  157. /* Find the register message for the local with the given offset */
  158. Lindex li;
  159. line_p l;
  160. for (li = Lfirst(mesregs); li != (Lindex) 0; li = Lnext(li,mesregs)) {
  161. l = (line_p) Lelem(li);
  162. if (aoff(ARG(l),1) == off) return l;
  163. }
  164. return (line_p) 0;
  165. }
  166. bool is_regvar(offset off)
  167. {
  168. return find_mesreg(off) != (line_p) 0;
  169. }
  170. offset regv_arg(offset off, int n)
  171. {
  172. /* fetch the n'th argument of the register message of the
  173. * local variable at offset off;
  174. */
  175. line_p x = find_mesreg(off);
  176. assert (x != (line_p) 0);
  177. return aoff(ARG(x),n);
  178. }