util.c 4.2 KB

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