add asm diffing in test script
[qbe.git] / util.c
blobf1a538318a76cc7ed924030f3c739fd385f0a138
1 #include "all.h"
2 #include <stdarg.h>
4 typedef struct Bitset Bitset;
5 typedef struct Vec Vec;
6 typedef struct Bucket Bucket;
8 struct Vec {
9 ulong mag;
10 Pool pool;
11 size_t esz;
12 ulong cap;
13 union {
14 long long ll;
15 long double ld;
16 void *ptr;
17 } align[];
20 struct Bucket {
21 uint nstr;
22 char **str;
25 enum {
26 VMin = 2,
27 VMag = 0xcabba9e,
28 NPtr = 256,
29 IBits = 12,
30 IMask = (1<<IBits) - 1,
33 Typ *typ;
34 Ins insb[NIns], *curi;
36 static void *ptr[NPtr];
37 static void **pool = ptr;
38 static int nptr = 1;
40 static Bucket itbl[IMask+1]; /* string interning table */
42 uint32_t
43 hash(char *s)
45 uint32_t h;
47 for (h=0; *s; ++s)
48 h = *s + 17*h;
49 return h;
52 void
53 die_(char *file, char *s, ...)
55 va_list ap;
57 fprintf(stderr, "%s: dying: ", file);
58 va_start(ap, s);
59 vfprintf(stderr, s, ap);
60 va_end(ap);
61 fputc('\n', stderr);
62 abort();
65 void *
66 emalloc(size_t n)
68 void *p;
70 p = calloc(1, n);
71 if (!p)
72 die("emalloc, out of memory");
73 return p;
76 void *
77 alloc(size_t n)
79 void **pp;
81 if (n == 0)
82 return 0;
83 if (nptr >= NPtr) {
84 pp = emalloc(NPtr * sizeof(void *));
85 pp[0] = pool;
86 pool = pp;
87 nptr = 1;
89 return pool[nptr++] = emalloc(n);
92 void
93 freeall()
95 void **pp;
97 for (;;) {
98 for (pp = &pool[1]; pp < &pool[nptr]; pp++)
99 free(*pp);
100 pp = pool[0];
101 if (!pp)
102 break;
103 free(pool);
104 pool = pp;
105 nptr = NPtr;
107 nptr = 1;
110 void *
111 vnew(ulong len, size_t esz, Pool pool)
113 void *(*f)(size_t);
114 ulong cap;
115 Vec *v;
117 for (cap=VMin; cap<len; cap*=2)
119 f = pool == Pheap ? emalloc : alloc;
120 v = f(cap * esz + sizeof(Vec));
121 v->mag = VMag;
122 v->cap = cap;
123 v->esz = esz;
124 v->pool = pool;
125 return v + 1;
128 void
129 vfree(void *p)
131 Vec *v;
133 v = (Vec *)p - 1;
134 assert(v->mag == VMag);
135 if (v->pool == Pheap) {
136 v->mag = 0;
137 free(v);
141 void
142 vgrow(void *vp, ulong len)
144 Vec *v;
145 void *v1;
147 v = *(Vec **)vp - 1;
148 assert(v+1 && v->mag == VMag);
149 if (v->cap >= len)
150 return;
151 v1 = vnew(len, v->esz, v->pool);
152 memcpy(v1, v+1, v->cap * v->esz);
153 vfree(v+1);
154 *(Vec **)vp = v1;
157 uint32_t
158 intern(char *s)
160 Bucket *b;
161 uint32_t h;
162 uint i, n;
164 h = hash(s) & IMask;
165 b = &itbl[h];
166 n = b->nstr;
168 for (i=0; i<n; i++)
169 if (strcmp(s, b->str[i]) == 0)
170 return h + (i<<IBits);
172 if (n == 1<<(32-IBits))
173 die("interning table overflow");
174 if (n == 0)
175 b->str = vnew(1, sizeof b->str[0], Pheap);
176 else if ((n & (n-1)) == 0)
177 vgrow(&b->str, n+n);
179 b->str[n] = emalloc(strlen(s)+1);
180 b->nstr = n + 1;
181 strcpy(b->str[n], s);
182 return h + (n<<IBits);
185 char *
186 str(uint32_t id)
188 assert(id>>IBits < itbl[id&IMask].nstr);
189 return itbl[id&IMask].str[id>>IBits];
193 isreg(Ref r)
195 return rtype(r) == RTmp && r.val < Tmp0;
199 iscmp(int op, int *pk, int *pc)
201 if (Ocmpw <= op && op <= Ocmpw1) {
202 *pc = op - Ocmpw;
203 *pk = Kw;
205 else if (Ocmpl <= op && op <= Ocmpl1) {
206 *pc = op - Ocmpl;
207 *pk = Kl;
209 else if (Ocmps <= op && op <= Ocmps1) {
210 *pc = NCmpI + op - Ocmps;
211 *pk = Ks;
213 else if (Ocmpd <= op && op <= Ocmpd1) {
214 *pc = NCmpI + op - Ocmpd;
215 *pk = Kd;
217 else
218 return 0;
219 return 1;
223 argcls(Ins *i, int n)
225 return optab[i->op].argcls[n][i->cls];
228 void
229 emit(int op, int k, Ref to, Ref arg0, Ref arg1)
231 if (curi == insb)
232 die("emit, too many instructions");
233 *--curi = (Ins){
234 .op = op, .cls = k,
235 .to = to, .arg = {arg0, arg1}
239 void
240 emiti(Ins i)
242 emit(i.op, i.cls, i.to, i.arg[0], i.arg[1]);
245 void
246 idup(Ins **pd, Ins *s, ulong n)
248 *pd = alloc(n * sizeof(Ins));
249 memcpy(*pd, s, n * sizeof(Ins));
252 Ins *
253 icpy(Ins *d, Ins *s, ulong n)
255 memcpy(d, s, n * sizeof(Ins));
256 return d + n;
259 static int cmptab[][2] ={
260 /* negation swap */
261 [Ciule] = {Ciugt, Ciuge},
262 [Ciult] = {Ciuge, Ciugt},
263 [Ciugt] = {Ciule, Ciult},
264 [Ciuge] = {Ciult, Ciule},
265 [Cisle] = {Cisgt, Cisge},
266 [Cislt] = {Cisge, Cisgt},
267 [Cisgt] = {Cisle, Cislt},
268 [Cisge] = {Cislt, Cisle},
269 [Cieq] = {Cine, Cieq},
270 [Cine] = {Cieq, Cine},
271 [NCmpI+Cfle] = {NCmpI+Cfgt, NCmpI+Cfge},
272 [NCmpI+Cflt] = {NCmpI+Cfge, NCmpI+Cfgt},
273 [NCmpI+Cfgt] = {NCmpI+Cfle, NCmpI+Cflt},
274 [NCmpI+Cfge] = {NCmpI+Cflt, NCmpI+Cfle},
275 [NCmpI+Cfeq] = {NCmpI+Cfne, NCmpI+Cfeq},
276 [NCmpI+Cfne] = {NCmpI+Cfeq, NCmpI+Cfne},
277 [NCmpI+Cfo] = {NCmpI+Cfuo, NCmpI+Cfo},
278 [NCmpI+Cfuo] = {NCmpI+Cfo, NCmpI+Cfuo},
282 cmpneg(int c)
284 assert(0 <= c && c < NCmp);
285 return cmptab[c][0];
289 cmpop(int c)
291 assert(0 <= c && c < NCmp);
292 return cmptab[c][1];
296 clsmerge(short *pk, short k)
298 short k1;
300 k1 = *pk;
301 if (k1 == Kx) {
302 *pk = k;
303 return 0;
305 if ((k1 == Kw && k == Kl) || (k1 == Kl && k == Kw)) {
306 *pk = Kw;
307 return 0;
309 return k1 != k;
313 phicls(int t, Tmp *tmp)
315 int t1;
317 t1 = tmp[t].phi;
318 if (!t1)
319 return t;
320 t1 = phicls(t1, tmp);
321 tmp[t].phi = t1;
322 return t1;
326 newtmp(char *prfx, int k, Fn *fn)
328 static int n;
329 int t;
331 t = fn->ntmp++;
332 vgrow(&fn->tmp, fn->ntmp);
333 memset(&fn->tmp[t], 0, sizeof(Tmp));
334 if (prfx)
335 sprintf(fn->tmp[t].name, "%s.%d", prfx, ++n);
336 fn->tmp[t].cls = k;
337 fn->tmp[t].slot = -1;
338 fn->tmp[t].nuse = +1;
339 fn->tmp[t].ndef = +1;
340 return TMP(t);
343 void
344 chuse(Ref r, int du, Fn *fn)
346 if (rtype(r) == RTmp)
347 fn->tmp[r.val].nuse += du;
351 getcon(int64_t val, Fn *fn)
353 int c;
355 for (c=0; c<fn->ncon; c++)
356 if (fn->con[c].type == CBits && fn->con[c].bits.i == val)
357 return CON(c);
358 vgrow(&fn->con, ++fn->ncon);
359 fn->con[c] = (Con){.type = CBits, .bits.i = val};
360 return CON(c);
363 void
364 addcon(Con *c0, Con *c1)
366 if (c0->type == CUndef)
367 *c0 = *c1;
368 else {
369 if (c1->type == CAddr) {
370 assert(c0->type != CAddr && "adding two addresses");
371 c0->type = CAddr;
372 c0->label = c1->label;
374 c0->bits.i += c1->bits.i;
378 void
379 blit(Ref rdst, uint doff, Ref rsrc, uint sz, Fn *fn)
381 struct { int st, ld, cls, size; } *p, tbl[] = {
382 { Ostorel, Oload, Kl, 8 },
383 { Ostorew, Oload, Kw, 8 },
384 { Ostoreh, Oloaduh, Kw, 2 },
385 { Ostoreb, Oloadub, Kw, 1 }
387 Ref r, r1;
388 uint boff, s;
390 for (boff=0, p=tbl; sz; p++)
391 for (s=p->size; sz>=s; sz-=s, doff+=s, boff+=s) {
392 r = newtmp("blt", Kl, fn);
393 r1 = newtmp("blt", Kl, fn);
394 emit(p->st, 0, R, r, r1);
395 emit(Oadd, Kl, r1, rdst, getcon(doff, fn));
396 r1 = newtmp("blt", Kl, fn);
397 emit(p->ld, p->cls, r, r1, R);
398 emit(Oadd, Kl, r1, rsrc, getcon(boff, fn));
402 void
403 bsinit(BSet *bs, uint n)
405 n = (n + NBit-1) / NBit;
406 bs->nt = n;
407 bs->t = alloc(n * sizeof bs->t[0]);
410 MAKESURE(NBit_is_64, NBit == 64);
411 inline static uint
412 popcnt(bits b)
414 b = (b & 0x5555555555555555) + ((b>>1) & 0x5555555555555555);
415 b = (b & 0x3333333333333333) + ((b>>2) & 0x3333333333333333);
416 b = (b & 0x0f0f0f0f0f0f0f0f) + ((b>>4) & 0x0f0f0f0f0f0f0f0f);
417 b += (b>>8);
418 b += (b>>16);
419 b += (b>>32);
420 return b & 0xff;
423 inline static int
424 firstbit(bits b)
426 int n;
428 n = 0;
429 if (!(b & 0xffffffff)) {
430 n += 32;
431 b >>= 32;
433 if (!(b & 0xffff)) {
434 n += 16;
435 b >>= 16;
437 if (!(b & 0xff)) {
438 n += 8;
439 b >>= 8;
441 if (!(b & 0xf)) {
442 n += 4;
443 b >>= 4;
445 n += (char[16]){4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0}[b & 0xf];
446 return n;
449 uint
450 bscount(BSet *bs)
452 uint i, n;
454 n = 0;
455 for (i=0; i<bs->nt; i++)
456 n += popcnt(bs->t[i]);
457 return n;
460 static inline uint
461 bsmax(BSet *bs)
463 return bs->nt * NBit;
466 void
467 bsset(BSet *bs, uint elt)
469 assert(elt < bsmax(bs));
470 bs->t[elt/NBit] |= BIT(elt%NBit);
473 void
474 bsclr(BSet *bs, uint elt)
476 assert(elt < bsmax(bs));
477 bs->t[elt/NBit] &= ~BIT(elt%NBit);
480 #define BSOP(f, op) \
481 void \
482 f(BSet *a, BSet *b) \
484 uint i; \
486 assert(a->nt == b->nt); \
487 for (i=0; i<a->nt; i++) \
488 a->t[i] op b->t[i]; \
491 BSOP(bscopy, =)
492 BSOP(bsunion, |=)
493 BSOP(bsinter, &=)
494 BSOP(bsdiff, &= ~)
497 bsequal(BSet *a, BSet *b)
499 uint i;
501 assert(a->nt == b->nt);
502 for (i=0; i<a->nt; i++)
503 if (a->t[i] != b->t[i])
504 return 0;
505 return 1;
508 void
509 bszero(BSet *bs)
511 memset(bs->t, 0, bs->nt * sizeof bs->t[0]);
514 /* iterates on a bitset, use as follows
516 * for (i=0; bsiter(set, &i); i++)
517 * use(i);
521 bsiter(BSet *bs, int *elt)
523 bits b;
524 uint t, i;
526 i = *elt;
527 t = i/NBit;
528 if (t >= bs->nt)
529 return 0;
530 b = bs->t[t];
531 b &= ~(BIT(i%NBit) - 1);
532 while (!b) {
533 ++t;
534 if (t >= bs->nt)
535 return 0;
536 b = bs->t[t];
538 *elt = NBit*t + firstbit(b);
539 return 1;
542 void
543 dumpts(BSet *bs, Tmp *tmp, FILE *f)
545 int t;
547 fprintf(f, "[");
548 for (t=Tmp0; bsiter(bs, &t); t++)
549 fprintf(f, " %s", tmp[t].name);
550 fprintf(f, " ]\n");