util.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /* M I S C E L L A N E O U S U T I L I T I E S */
  6. /* $Id$ */
  7. /* Code for the allocation and de-allocation of temporary variables,
  8. allowing re-use.
  9. */
  10. #include "lint.h"
  11. #ifndef LINT
  12. #include <em.h>
  13. #else
  14. #include "l_em.h"
  15. #endif /* LINT */
  16. #include <em_arith.h>
  17. #include <em_reg.h>
  18. #include <alloc.h>
  19. #include <em_mes.h>
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "use_tmp.h"
  23. #include "regcount.h"
  24. #include "sizes.h"
  25. #include "align.h"
  26. #include "stack.h"
  27. #include "Lpars.h"
  28. #include "def.h"
  29. static struct localvar *FreeTmps;
  30. #ifdef USE_TMP
  31. static int loc_id;
  32. #endif /* USE_TMP */
  33. #ifdef PEEPHOLE
  34. #undef REGCOUNT
  35. #define REGCOUNT 1
  36. #endif
  37. extern char options[];
  38. LocalInit()
  39. {
  40. #ifdef USE_TMP
  41. C_insertpart(loc_id = C_getid());
  42. #endif /* USE_TMP */
  43. }
  44. arith
  45. LocalSpace(sz, al)
  46. arith sz;
  47. {
  48. register struct stack_level *stl = local_level;
  49. stl->sl_max_block = - align(sz - stl->sl_max_block, al);
  50. return stl->sl_max_block;
  51. }
  52. #define TABSIZ 32
  53. static struct localvar *regs[TABSIZ];
  54. arith
  55. NewLocal(sz, al, regtype, sc)
  56. arith sz;
  57. {
  58. register struct localvar *tmp = FreeTmps;
  59. struct localvar *prev = 0;
  60. int index;
  61. while (tmp) {
  62. if (tmp->t_align >= al &&
  63. tmp->t_size >= sz &&
  64. tmp->t_sc == sc &&
  65. tmp->t_regtype == regtype) {
  66. if (prev) {
  67. prev->next = tmp->next;
  68. }
  69. else FreeTmps = tmp->next;
  70. break;
  71. }
  72. prev = tmp;
  73. tmp = tmp->next;
  74. }
  75. if (! tmp) {
  76. tmp = new_localvar();
  77. tmp->t_offset = LocalSpace(sz, al);
  78. tmp->t_align = al;
  79. tmp->t_size = sz;
  80. tmp->t_sc = sc;
  81. tmp->t_regtype = regtype;
  82. tmp->t_count = REG_DEFAULT;
  83. }
  84. index = (int) (tmp->t_offset >> 2) & (TABSIZ - 1);
  85. tmp->next = regs[index];
  86. regs[index] = tmp;
  87. return tmp->t_offset;
  88. }
  89. FreeLocal(off)
  90. arith off;
  91. {
  92. int index = (int) (off >> 2) & (TABSIZ - 1);
  93. register struct localvar *tmp = regs[index];
  94. struct localvar *prev = 0;
  95. while (tmp && tmp->t_offset != off) {
  96. prev = tmp;
  97. tmp = tmp->next;
  98. }
  99. if (tmp) {
  100. if (prev) prev->next = tmp->next;
  101. else regs[index] = tmp->next;
  102. tmp->next = FreeTmps;
  103. FreeTmps = tmp;
  104. }
  105. }
  106. LocalFinish()
  107. {
  108. register struct localvar *tmp, *tmp1;
  109. register int i;
  110. #ifdef USE_TMP
  111. C_beginpart(loc_id);
  112. #endif
  113. tmp = FreeTmps;
  114. while (tmp) {
  115. tmp1 = tmp;
  116. if (tmp->t_sc == REGISTER) tmp->t_count += REG_BONUS;
  117. if (! options['n'] && tmp->t_regtype >= 0) {
  118. C_ms_reg(tmp->t_offset, tmp->t_size, tmp->t_regtype, tmp->t_count);
  119. }
  120. tmp = tmp->next;
  121. free_localvar(tmp1);
  122. }
  123. FreeTmps = 0;
  124. for (i = 0; i < TABSIZ; i++) {
  125. tmp = regs[i];
  126. while (tmp) {
  127. if (tmp->t_sc == REGISTER) tmp->t_count += REG_BONUS;
  128. tmp1 = tmp;
  129. if (! options['n'] && tmp->t_regtype >= 0) {
  130. C_ms_reg(tmp->t_offset,
  131. tmp->t_size,
  132. tmp->t_regtype,
  133. tmp->t_count);
  134. }
  135. tmp = tmp->next;
  136. free_localvar(tmp1);
  137. }
  138. regs[i] = 0;
  139. }
  140. #ifdef PEEPHOLE
  141. if (! options['n']) {
  142. C_mes_begin(ms_reg);
  143. C_mes_end();
  144. }
  145. #endif
  146. #ifdef USE_TMP
  147. C_endpart(loc_id);
  148. #endif
  149. }
  150. RegisterAccount(offset, size, regtype, sc)
  151. arith offset, size;
  152. {
  153. register struct localvar *p;
  154. int index;
  155. if (regtype < 0) return;
  156. p = new_localvar();
  157. index = (int) (offset >> 2) & (TABSIZ - 1);
  158. p->t_offset = offset;
  159. p->t_regtype = regtype;
  160. p->t_count = REG_DEFAULT;
  161. p->t_sc = sc;
  162. p->t_size = size;
  163. p->next = regs[index];
  164. regs[index] = p;
  165. }
  166. static struct localvar *
  167. find_reg(off)
  168. arith off;
  169. {
  170. register struct localvar *p = regs[(int)(off >> 2) & (TABSIZ - 1)];
  171. while (p && p->t_offset != off) p = p->next;
  172. return p;
  173. }
  174. LoadLocal(off, sz)
  175. arith off, sz;
  176. {
  177. register struct localvar *p = find_reg(off);
  178. #ifdef USE_TMP
  179. #ifdef REGCOUNT
  180. if (p) p->t_count++;
  181. #endif
  182. #endif
  183. if (p && p->t_size != sz) p->t_regtype = -1;
  184. if (sz == word_size) C_lol(off);
  185. else if (sz == dword_size) C_ldl(off);
  186. else {
  187. if (p) p->t_regtype = -1;
  188. C_lal(off);
  189. C_loi(sz);
  190. }
  191. }
  192. StoreLocal(off, sz)
  193. arith off, sz;
  194. {
  195. register struct localvar *p = find_reg(off);
  196. #ifdef USE_TMP
  197. #ifdef REGCOUNT
  198. if (p) p->t_count++;
  199. #endif
  200. #endif
  201. if (p && p->t_size != sz) p->t_regtype = -1;
  202. if (sz == word_size) C_stl(off);
  203. else if (sz == dword_size) C_sdl(off);
  204. else {
  205. if (p) p->t_regtype = -1;
  206. C_lal(off);
  207. C_sti(sz);
  208. }
  209. }
  210. #ifndef LINT
  211. AddrLocal(off)
  212. arith off;
  213. {
  214. register struct localvar *p = find_reg(off);
  215. if (p) p->t_regtype = -1;
  216. C_lal(off);
  217. }
  218. #endif /* LINT */