locals.c 4.6 KB

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