pmov.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*% rm -f rega.o main.o && cc -g -std=c99 -Wall -DTEST_PMOV -o pmov % *.o
  2. *
  3. * This is a test framwork for the dopm() function
  4. * in rega.c, use it when you want to modify it or
  5. * all the parallel move functions.
  6. *
  7. * You might need to decrease NIReg to see it
  8. * terminate, I used NIReg == 7 at most.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. static void assert_test(char *, int), fail(void), iexec(int *);
  14. #include "../../rega.c"
  15. static void bsinit_(BSet *, uint);
  16. static RMap mbeg;
  17. static Ins ins[NIReg], *ip;
  18. static Blk dummyb = { .ins = ins };
  19. int
  20. main()
  21. {
  22. Ins *i1;
  23. unsigned long long tm, rm, cnt;
  24. RMap mend;
  25. int reg[NIReg], val[NIReg+1];
  26. int t, i, r, nr;
  27. tmp = (Tmp[Tmp0+NIReg]){{{0}}};
  28. for (t=0; t<Tmp0+NIReg; t++)
  29. if (t >= Tmp0) {
  30. tmp[t].cls = Kw;
  31. tmp[t].hint.r = -1;
  32. tmp[t].hint.m = 0;
  33. tmp[t].slot = -1;
  34. sprintf(tmp[t].name, "tmp%d", t-Tmp0+1);
  35. }
  36. bsinit_(mbeg.b, Tmp0+NIReg);
  37. bsinit_(mend.b, Tmp0+NIReg);
  38. cnt = 0;
  39. for (tm = 0; tm < 1ull << (2*NIReg); tm++) {
  40. mbeg.n = 0;
  41. bszero(mbeg.b);
  42. ip = ins;
  43. /* find what temporaries are in copy and
  44. * wether or not they are in register
  45. */
  46. for (t=0; t<NIReg; t++)
  47. switch ((tm >> (2*t)) & 3) {
  48. case 0:
  49. /* not in copy, not in reg */
  50. break;
  51. case 1:
  52. /* not in copy, in reg */
  53. radd(&mbeg, Tmp0+t, t+1);
  54. break;
  55. case 2:
  56. /* in copy, not in reg */
  57. *ip++ = (Ins){OCopy, Kw, TMP(Tmp0+t), {R, R}};
  58. break;
  59. case 3:
  60. /* in copy, in reg */
  61. *ip++ = (Ins){OCopy, Kw, TMP(Tmp0+t), {R, R}};
  62. radd(&mbeg, Tmp0+t, t+1);
  63. break;
  64. }
  65. if (ip == ins)
  66. /* cancel if the parallel move
  67. * is empty
  68. */
  69. goto Nxt;
  70. /* find registers for temporaries
  71. * in mbeg
  72. */
  73. nr = ip - ins;
  74. rm = (1ull << (nr+1)) - 1;
  75. for (i=0; i<nr; i++)
  76. reg[i] = i+1;
  77. for (;;) {
  78. /* set registers on copies
  79. */
  80. for (i=0, i1=ins; i1<ip; i1++, i++)
  81. i1->arg[0] = TMP(reg[i]);
  82. /* compile the parallel move
  83. */
  84. rcopy(&mend, &mbeg);
  85. dopm(&dummyb, ip-1, &mend);
  86. cnt++;
  87. /* check that mend contain mappings for
  88. * source registers and does not map any
  89. * assigned temporary, then check that
  90. * all temporaries in mend are mapped in
  91. * mbeg and not used in the copy
  92. */
  93. for (i1=ins; i1<ip; i1++) {
  94. r = i1->arg[0].val;
  95. assert(rfree(&mend, r) == r);
  96. t = i1->to.val;
  97. assert(!bshas(mend.b, t));
  98. }
  99. for (i=0; i<mend.n; i++) {
  100. t = mend.t[i];
  101. assert(bshas(mbeg.b, t));
  102. t -= Tmp0;
  103. assert(((tm >> (2*t)) & 3) == 1);
  104. }
  105. /* execute the code generated and check
  106. * that all assigned temporaries got their
  107. * value, and that all live variables's
  108. * content got preserved
  109. */
  110. for (i=1; i<=NIReg; i++)
  111. val[i] = i;
  112. iexec(val);
  113. for (i1=ins; i1<ip; i1++) {
  114. t = i1->to.val;
  115. r = rfind(&mbeg, t);
  116. if (r != -1)
  117. assert(val[r] == i1->arg[0].val);
  118. }
  119. for (i=0; i<mend.n; i++) {
  120. t = mend.t[i];
  121. r = mend.r[i];
  122. assert(val[t-Tmp0+1] == r);
  123. }
  124. /* find the next register assignment */
  125. i = nr - 1;
  126. for (;;) {
  127. r = reg[i];
  128. rm &= ~(1ull<<r);
  129. do
  130. r++;
  131. while (r <= NIReg && (rm & (1ull<<r)));
  132. if (r == NIReg+1) {
  133. if (i == 0)
  134. goto Nxt;
  135. i--;
  136. } else {
  137. rm |= (1ull<<r);
  138. reg[i++] = r;
  139. break;
  140. }
  141. }
  142. for (; i<nr; i++)
  143. for (r=1; r<=NIReg; r++)
  144. if (!(rm & (1ull<<r))) {
  145. rm |= (1ull<<r);
  146. reg[i] = r;
  147. break;
  148. }
  149. }
  150. Nxt: freeall();
  151. }
  152. printf("%llu tests successful!\n", cnt);
  153. exit(0);
  154. }
  155. /* execute what pmgen() wrote (swap, copy) */
  156. #define validr(r) \
  157. rtype(r) == RTmp && \
  158. r.val > 0 && \
  159. r.val <= NIReg
  160. static void
  161. iexec(int val[])
  162. {
  163. Ins *i;
  164. int t;
  165. for (i=insb; i<curi; i++)
  166. switch (i->op) {
  167. default:
  168. assert(!"iexec: missing case\n");
  169. exit(1);
  170. case OSwap:
  171. assert(validr(i->arg[0]));
  172. assert(validr(i->arg[1]));
  173. t = val[i->arg[0].val];
  174. val[i->arg[0].val] = val[i->arg[1].val];
  175. val[i->arg[1].val] = t;
  176. break;
  177. case OCopy:
  178. assert(validr(i->to));
  179. assert(validr(i->arg[0]));
  180. val[i->to.val] = val[i->arg[0].val];
  181. break;
  182. }
  183. }
  184. /* failure diagnostics */
  185. static int re;
  186. static void
  187. replay()
  188. {
  189. RMap mend;
  190. re = 1;
  191. bsinit_(mend.b, Tmp0+NIReg);
  192. rcopy(&mend, &mbeg);
  193. dopm(&dummyb, ip-1, &mend);
  194. }
  195. static void
  196. fail()
  197. {
  198. Ins *i1;
  199. int i;
  200. printf("\nIn registers: ");
  201. for (i=0; i<mbeg.n; i++)
  202. printf("%s(r%d) ",
  203. tmp[mbeg.t[i]].name,
  204. mbeg.r[i]);
  205. printf("\n");
  206. printf("Parallel move:\n");
  207. for (i1=ins; i1<ip; i1++)
  208. printf("\t %s <- r%d\n",
  209. tmp[i1->to.val].name,
  210. i1->arg[0].val);
  211. replay();
  212. abort();
  213. }
  214. static void
  215. assert_test(char *s, int x)
  216. {
  217. if (x)
  218. return;
  219. if (re)
  220. abort();
  221. printf("!assertion failure: %s\n", s);
  222. fail();
  223. }
  224. static void
  225. bsinit_(BSet *bs, uint n)
  226. {
  227. n = (n + NBit-1) / NBit;
  228. bs->nt = n;
  229. bs->t = emalloc(n * sizeof bs->t[0]);
  230. }
  231. /* symbols required by the linker */
  232. char debug['Z'+1];