1 /*% rm -f rega.o main.o && cc -g -std=c99 -Wall -DTEST_PMOV -o pmov % *.o
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.
7 * You might need to decrease NIReg to see it
8 * terminate, I used NIReg == 7 at most.
14 static void assert_test(char *, int), fail(void), iexec(int *);
16 #include "../../rega.c"
18 static void bsinit_(BSet
*, uint
);
21 static Ins ins
[NIReg
], *ip
;
22 static Blk dummyb
= { .ins
= ins
};
28 unsigned long long tm
, rm
, cnt
;
30 int reg
[NIReg
], val
[NIReg
+1];
33 tmp
= (Tmp
[Tmp0
+NIReg
]){{{0}}};
34 for (t
=0; t
<Tmp0
+NIReg
; t
++)
40 sprintf(tmp
[t
].name
, "tmp%d", t
-Tmp0
+1);
43 bsinit_(mbeg
.b
, Tmp0
+NIReg
);
44 bsinit_(mend
.b
, Tmp0
+NIReg
);
46 for (tm
= 0; tm
< 1ull << (2*NIReg
); tm
++) {
51 /* find what temporaries are in copy and
52 * wether or not they are in register
54 for (t
=0; t
<NIReg
; t
++)
55 switch ((tm
>> (2*t
)) & 3) {
57 /* not in copy, not in reg */
60 /* not in copy, in reg */
61 radd(&mbeg
, Tmp0
+t
, t
+1);
64 /* in copy, not in reg */
65 *ip
++ = (Ins
){OCopy
, Kw
, TMP(Tmp0
+t
), {R
, R
}};
69 *ip
++ = (Ins
){OCopy
, Kw
, TMP(Tmp0
+t
), {R
, R
}};
70 radd(&mbeg
, Tmp0
+t
, t
+1);
75 /* cancel if the parallel move
80 /* find registers for temporaries
84 rm
= (1ull << (nr
+1)) - 1;
89 /* set registers on copies
91 for (i
=0, i1
=ins
; i1
<ip
; i1
++, i
++)
92 i1
->arg
[0] = TMP(reg
[i
]);
94 /* compile the parallel move
97 dopm(&dummyb
, ip
-1, &mend
);
100 /* check that mend contain mappings for
101 * source registers and does not map any
102 * assigned temporary, then check that
103 * all temporaries in mend are mapped in
104 * mbeg and not used in the copy
106 for (i1
=ins
; i1
<ip
; i1
++) {
108 assert(rfree(&mend
, r
) == r
);
110 assert(!bshas(mend
.b
, t
));
112 for (i
=0; i
<mend
.n
; i
++) {
114 assert(bshas(mbeg
.b
, t
));
116 assert(((tm
>> (2*t
)) & 3) == 1);
119 /* execute the code generated and check
120 * that all assigned temporaries got their
121 * value, and that all live variables's
122 * content got preserved
124 for (i
=1; i
<=NIReg
; i
++)
127 for (i1
=ins
; i1
<ip
; i1
++) {
131 assert(val
[r
] == i1
->arg
[0].val
);
133 for (i
=0; i
<mend
.n
; i
++) {
136 assert(val
[t
-Tmp0
+1] == r
);
139 /* find the next register assignment */
146 while (r
<= NIReg
&& (rm
& (1ull<<r
)));
158 for (r
=1; r
<=NIReg
; r
++)
159 if (!(rm
& (1ull<<r
))) {
167 printf("%llu tests successful!\n", cnt
);
172 /* execute what pmgen() wrote (swap, copy) */
175 rtype(r) == RTmp && \
185 for (i
=insb
; i
<curi
; i
++)
188 assert(!"iexec: missing case\n");
191 assert(validr(i
->arg
[0]));
192 assert(validr(i
->arg
[1]));
193 t
= val
[i
->arg
[0].val
];
194 val
[i
->arg
[0].val
] = val
[i
->arg
[1].val
];
195 val
[i
->arg
[1].val
] = t
;
198 assert(validr(i
->to
));
199 assert(validr(i
->arg
[0]));
200 val
[i
->to
.val
] = val
[i
->arg
[0].val
];
206 /* failure diagnostics */
216 bsinit_(mend
.b
, Tmp0
+NIReg
);
218 dopm(&dummyb
, ip
-1, &mend
);
227 printf("\nIn registers: ");
228 for (i
=0; i
<mbeg
.n
; i
++)
233 printf("Parallel move:\n");
234 for (i1
=ins
; i1
<ip
; i1
++)
235 printf("\t %s <- r%d\n",
236 tmp
[i1
->to
.val
].name
,
243 assert_test(char *s
, int x
)
249 printf("!assertion failure: %s\n", s
);
254 bsinit_(BSet
*bs
, uint n
)
256 n
= (n
+ NBit
-1) / NBit
;
258 bs
->t
= emalloc(n
* sizeof bs
->t
[0]);
261 /* symbols required by the linker */