locals.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /*
  7. * L O C A L S . C
  8. */
  9. #include <stdio.h>
  10. #include <em_mnem.h>
  11. #include <em_spec.h>
  12. #include <em_pseu.h>
  13. #include <em_mes.h>
  14. #include "types.h"
  15. #include "debug.h"
  16. #include "global.h"
  17. #include "lset.h"
  18. #include "cset.h"
  19. #include "def.h"
  20. #include "get.h"
  21. #include "aux.h"
  22. #include "alloc.h"
  23. #include "locals.h"
  24. short nrglobals;
  25. short nrlocals;
  26. local_p *locals; /* dynamic array */
  27. STATIC localvar(off,size,locs,reg,score)
  28. offset off;
  29. short size;
  30. local_p *locs;
  31. bool reg;
  32. offset score;
  33. {
  34. /* process a reference to a local variable.
  35. * A local is characterized by a (offset,size) pair.
  36. * We first collect all locals in a list, sorted
  37. * by offset. Later we will construct a table
  38. * out of this list.
  39. */
  40. local_p lc, x, *prevp;
  41. prevp = locs;
  42. for (lc = *locs; lc != (local_p) 0; lc = lc->lc_next) {
  43. if (lc->lc_off == off && lc->lc_size == size) {
  44. if (reg) {
  45. REGVAR(lc); /* register variable */
  46. lc->lc_score = score;
  47. }
  48. return; /* local already present */
  49. }
  50. if (lc->lc_off > off) break;
  51. prevp = &lc->lc_next;
  52. }
  53. /* the local was not seen before; create an entry
  54. * for it in the list.
  55. */
  56. x = *prevp = newlocal();
  57. x->lc_off = off;
  58. x->lc_size = size;
  59. x->lc_next = lc;
  60. if (reg) {
  61. REGVAR(x);
  62. x->lc_score = score;
  63. }
  64. }
  65. STATIC check_message(l,locs)
  66. line_p l;
  67. local_p *locs;
  68. {
  69. /* See if l is a register message */
  70. arg_p arg;
  71. arg = ARG(l);
  72. if (aoff(arg,0) == ms_reg && arg->a_next != (arg_p) 0) {
  73. localvar(aoff(arg,1), (short) aoff(arg,2), locs, TRUE,
  74. aoff(arg,4));
  75. }
  76. }
  77. STATIC check_local_use(l,locs)
  78. line_p l;
  79. local_p *locs;
  80. {
  81. short sz;
  82. switch(INSTR(l)) {
  83. case op_lol:
  84. case op_stl:
  85. case op_inl:
  86. case op_del:
  87. case op_zrl:
  88. sz = ws;
  89. break;
  90. case op_ldl:
  91. case op_sdl:
  92. sz = 2 * ws;
  93. break;
  94. case op_lil:
  95. case op_sil:
  96. sz = ps;
  97. break;
  98. case ps_mes:
  99. check_message(l,locs);
  100. /* fall through .. */
  101. default:
  102. return;
  103. }
  104. if (l->l_next && INSTR(l->l_next) == op_nop) {
  105. /* volatile */
  106. return;
  107. }
  108. localvar(off_set(l),sz,locs,FALSE,(offset) 0);
  109. }
  110. make_localtab(p)
  111. proc_p p;
  112. {
  113. /* Make a table of local variables.
  114. * This table is used to associate a
  115. * unique number with a local. If two
  116. * locals overlap (e.g. LDL 4 and LDL 2)
  117. * none of them is considered any further,
  118. * i.e. we don't compute ud-info for them.
  119. */
  120. local_p prev = 0, next, lc;
  121. local_p locallist = (local_p) 0;
  122. short cnt = 0;
  123. offset x, ill_zone = 0;
  124. register bblock_p b;
  125. register line_p l;
  126. /* first make a list of all locals used */
  127. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  128. for (l = b->b_start; l != (line_p) 0; l = l->l_next) {
  129. check_local_use(l,&locallist);
  130. }
  131. }
  132. /* Now remove overlapping locals, count useful ones on the fly */
  133. for (lc = locallist; lc != (local_p) 0; lc = lc->lc_next) {
  134. if (ill_zone != 0 && lc->lc_off < ill_zone) {
  135. /* this local overlaps with a previous one */
  136. BADLC(lc);
  137. if (!IS_BADLC(prev)) {
  138. BADLC(prev);
  139. cnt--;
  140. }
  141. } else {
  142. cnt++;
  143. }
  144. x = lc->lc_off + lc->lc_size;
  145. if (ill_zone == 0 || x > ill_zone) {
  146. ill_zone = x;
  147. }
  148. prev = lc;
  149. }
  150. /* Now we know how many local variables there are */
  151. nrlocals = cnt;
  152. locals = (local_p *) newmap(cnt);
  153. cnt = 1;
  154. for (lc = locallist; lc != (local_p) 0; lc = next) {
  155. next = lc->lc_next;
  156. if (IS_BADLC(lc)) {
  157. oldlocal(lc);
  158. } else {
  159. locals[cnt++] = lc;
  160. lc->lc_next = (local_p) 0;
  161. }
  162. }
  163. assert (cnt == nrlocals+1);
  164. }
  165. find_local(off,nr_out,found_out)
  166. offset off;
  167. short *nr_out;
  168. bool *found_out;
  169. {
  170. /* Try to find the local variable at the given
  171. * offset. Return its local-number.
  172. */
  173. short v;
  174. for (v = 1; v <= nrlocals; v++) {
  175. if (locals[v]->lc_off > off) break;
  176. if (locals[v]->lc_off == off) {
  177. *found_out = TRUE;
  178. *nr_out = v;
  179. return;
  180. }
  181. }
  182. *found_out = FALSE;
  183. }
  184. var_nr(l,nr_out,found_out)
  185. line_p l;
  186. short *nr_out;
  187. bool *found_out;
  188. {
  189. /* Determine the number of the variable referenced
  190. * by EM instruction l.
  191. */
  192. offset off = 0;
  193. short nr;
  194. switch(TYPE(l)) {
  195. case OPOBJECT:
  196. /* global variable */
  197. if (OBJ(l)->o_globnr == 0) {
  198. /* We don't maintain ud-info for this var */
  199. *found_out = FALSE;
  200. } else {
  201. *nr_out = GLOB_TO_VARNR(OBJ(l)->o_globnr);
  202. *found_out = TRUE;
  203. }
  204. return;
  205. case OPSHORT:
  206. off = (offset) SHORT(l);
  207. break;
  208. case OPOFFSET:
  209. off = OFFSET(l);
  210. break;
  211. default:
  212. assert(FALSE);
  213. }
  214. /* Its's a local variable */
  215. find_local(off,&nr,found_out);
  216. if (*found_out) {
  217. *nr_out = LOC_TO_VARNR(nr);
  218. }
  219. }