coerc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <stdio.h>
  6. #include "assert.h"
  7. #include "param.h"
  8. #include "set.h"
  9. #include "property.h"
  10. #include "reg.h"
  11. #include "token.h"
  12. #include "varinfo.h"
  13. #include "instruct.h"
  14. #include "iocc.h"
  15. #include "error.h"
  16. #include "output.h"
  17. #include "subr.h"
  18. #include "hall.h"
  19. #include <cgg_cg.h>
  20. #include "pseudo.h"
  21. #include "extern.h"
  22. extern set_t l_sets[];
  23. int nmoves;
  24. move_t l_moves[MAXMOVES];
  25. short posmoves[MAXREGS+MAXTOKENS][SETSIZE];
  26. void n_split(int ti, int be, struct varinfo *al, struct varinfo *ge, struct varinfo *rp, int n);
  27. void n_move(int s1, int e1,int s2, int e2, struct varinfo *vi)
  28. {
  29. move_p mp;
  30. int i,j;
  31. NEXT(nmoves,MAXMOVES,"Moves");
  32. mp = &l_moves[nmoves-1];
  33. mp->m_set1 = s1;
  34. mp->m_expr1 = e1;
  35. mp->m_set2 = s2;
  36. mp->m_expr2 = e2;
  37. mp->m_cindex = codeindex;
  38. dopattern(0,VI_NULL,VI_NULL,vi,VI_NULL,VI_NULL);
  39. if (mp->m_expr1!=0 || mp->m_expr2!=0)
  40. return;
  41. for (i=0;i<MAXREGS+MAXTOKENS;i++)
  42. if (BIT(l_sets[mp->m_set1].set_val,i))
  43. for(j=0;j<SETSIZE;j++)
  44. posmoves[i][j] |= l_sets[mp->m_set2].set_val[j];
  45. }
  46. int existmove(iocc_t from, short *sp)
  47. {
  48. int i;
  49. for (i=0;i<MAXREGS+MAXTOKENS;i++)
  50. if(BIT(from.in_set,i))
  51. if (!subset(sp,posmoves[i],SETSIZE))
  52. return(0);
  53. return(1);
  54. }
  55. int existalmove(iocc_t from, int prpno)
  56. {
  57. short s[SETSIZE];
  58. int i;
  59. for (i=0;i<SETSIZE;i++)
  60. s[i] = i<SZOFSET(MAXREGS) ? l_props[prpno].pr_regset[i] : 0;
  61. return(existmove(from,s));
  62. }
  63. struct varinfo *gen_move(iocc_t from, iocc_t to)
  64. {
  65. struct varinfo *vp;
  66. if (existmove(from,to.in_set)==0) {
  67. error("No such move defined");
  68. return(VI_NULL);
  69. }
  70. NEW(vp,struct varinfo);
  71. vp->vi_int[0] = INSMOVE;
  72. vp->vi_int[1] = from.in_index;
  73. vp->vi_int[2] = to.in_index;
  74. return(vp);
  75. }
  76. int ntests;
  77. test_t l_tests[MAXTESTS];
  78. short postests[SETSIZE];
  79. void n_test(int s, int e, struct varinfo *vi)
  80. {
  81. test_p tp;
  82. int i;
  83. NEXT(ntests,MAXTESTS,"Tests");
  84. tp = &l_tests[ntests-1];
  85. tp->t_set = s;
  86. tp->t_expr = e;
  87. tp->t_cindex = codeindex;
  88. dopattern(0,VI_NULL,VI_NULL,vi,VI_NULL,VI_NULL);
  89. if (tp->t_expr!=0)
  90. return;
  91. for(i=0;i<SETSIZE;i++)
  92. postests[i] |= l_sets[tp->t_set].set_val[i];
  93. }
  94. struct varinfo *gen_test(iocc_t from)
  95. {
  96. struct varinfo *vp;
  97. if (!subset(from.in_set,postests,SETSIZE)) {
  98. error("No such test");
  99. return(0);
  100. }
  101. NEW(vp,struct varinfo);
  102. vp->vi_int[0] = INSTEST;
  103. vp->vi_int[1] = from.in_index;
  104. return(vp);
  105. }
  106. struct varinfo *gen_label(int arg)
  107. {
  108. struct varinfo *vp;
  109. NEW(vp,struct varinfo);
  110. vp->vi_int[0] = INSLABDEF;
  111. vp->vi_int[1] = arg;
  112. return(vp);
  113. }
  114. struct varinfo *gen_preturn()
  115. {
  116. struct varinfo *vp;
  117. NEW(vp,struct varinfo);
  118. vp->vi_int[0] = INSPRETURN;
  119. return(vp);
  120. }
  121. struct varinfo *gen_tlab(int n)
  122. {
  123. struct varinfo *vp;
  124. assert((n>=0) && (n<=9));
  125. NEW(vp,struct varinfo);
  126. vp->vi_int[0] = INSTLAB;
  127. vp->vi_int[1] = n;
  128. return(vp);
  129. }
  130. int nstacks;
  131. c1_t l_stacks[MAXSTACKS];
  132. set_t ustackset,cstackset;
  133. void n_stack(int s, int e, int p, struct varinfo *vi)
  134. {
  135. c1_p c1p;
  136. short *sp;
  137. int i;
  138. NEXT(nstacks,MAXSTACKS,"Stacks");
  139. c1p= & l_stacks[nstacks-1];
  140. c1p->c1_texpno = s;
  141. c1p->c1_expr = e;
  142. c1p->c1_prop = p;
  143. c1p->c1_codep = codeindex;
  144. dopattern(0,VI_NULL,VI_NULL,vi,VI_NULL,VI_NULL);
  145. if (e==0 && p== -1)
  146. sp = ustackset.set_val;
  147. else
  148. sp = cstackset.set_val;
  149. for(i=0;i<SETSIZE;i++)
  150. sp[i] |= l_sets[s].set_val[i];
  151. }
  152. void checkstacking(short *sp)
  153. {
  154. int i;
  155. short *chkset;
  156. char *warn;
  157. if (subset(sp,ustackset.set_val,SETSIZE))
  158. return;
  159. chkset = ustackset.set_val; warn = "";
  160. for (i=1;i<nregs;i++)
  161. if (BIT(sp,i) && !BIT(chkset,i))
  162. error("No %sstacking rule for register %s",warn,
  163. l_regs[i].ri_name);
  164. for(;i<nregs+MAXTOKENS;i++)
  165. if (BIT(sp,i) && !BIT(chkset,i))
  166. error("No %sstacking rule for token %s",warn,
  167. l_tokens[i-nregs]->tk_name);
  168. }
  169. int ncoercs;
  170. c3_t l_coercs[MAXCOERCS];
  171. set_t unstackset;
  172. /*VARARGS5*/
  173. void n_coerc(int ti, int be, struct varinfo *al, struct varinfo *ge, struct varinfo *rp, iocc_t in)
  174. {
  175. c3_p c3p;
  176. int i;
  177. struct varinfo *vi;
  178. if (ti!=0) {
  179. for(i=0,vi=rp;vi!=0;vi=vi->vi_next,i++)
  180. ;
  181. if (i>1) {
  182. n_split(ti,be,al,ge,rp,i);
  183. return;
  184. } else {
  185. if (i==0) {
  186. error("Coercion should have a result!");
  187. return;
  188. }
  189. }
  190. } else {
  191. NEW(rp,struct varinfo);
  192. rp->vi_next = 0;
  193. rp->vi_int[0] = in.in_index;
  194. }
  195. if (nallreg>1)
  196. error("More than 1 register may not be allocated");
  197. NEXT(ncoercs,MAXCOERCS,"Coercions");
  198. c3p = & l_coercs[ncoercs-1];
  199. c3p->c3_texpno = ti;
  200. c3p->c3_expr = be;
  201. c3p->c3_prop = nallreg==0 ? -1 : allreg[0];
  202. c3p->c3_repl = rp->vi_int[0];
  203. c3p->c3_codep = codeindex;
  204. dopattern(ti==0,VI_NULL,al,ge,rp,VI_NULL);
  205. if (ti==0)
  206. for(i=0;i<SETSIZE;i++)
  207. unstackset.set_val[i] |= in.in_set[i];
  208. freevi(rp);
  209. }
  210. void checkunstacking(int setno) {
  211. short *sp;
  212. int i;
  213. short hallset[SETSIZE];
  214. sp = l_sets[setno].set_val;
  215. for (i=0;i<SETSIZE;i++)
  216. hallset[i]=sp[i]&unstackset.set_val[i];
  217. nexthall(hallset);
  218. }
  219. int nsplit,maxsplit;
  220. c2_t l_split[MAXSPLCOERC];
  221. void n_split(int ti, int be, struct varinfo *al, struct varinfo *ge, struct varinfo *rp, int n)
  222. {
  223. c2_p c2p;
  224. int i;
  225. struct varinfo *vi;
  226. NEXT(nsplit,MAXSPLCOERC,"Splitting coercions");
  227. c2p = &l_split[nsplit-1];
  228. if (n>MAXSPLIT) {
  229. error("Maximum split factor is %d",MAXSPLIT);
  230. n = MAXSPLIT;
  231. }
  232. if (n>maxsplit) maxsplit=n;
  233. c2p->c2_texpno = ti;
  234. c2p->c2_expr = be;
  235. if (nallreg)
  236. error("No register uses allowed in splitting coercion");
  237. c2p->c2_nsplit = n;
  238. for (i=0,vi=rp; i<n; i++,vi=vi->vi_next)
  239. c2p->c2_repl[i] = vi->vi_int[0];
  240. c2p->c2_codep = codeindex;
  241. dopattern(0,VI_NULL,al,ge,rp,VI_NULL);
  242. }