reorder some sections in doc
[qbe.git] / util.c
blob8997f6cbddee3a68e768b7d707ebc32892e4cea9
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 if (n)
250 memcpy(*pd, s, n * sizeof(Ins));
253 Ins *
254 icpy(Ins *d, Ins *s, ulong n)
256 if (n)
257 memcpy(d, s, n * sizeof(Ins));
258 return d + n;
261 static int cmptab[][2] ={
262 /* negation swap */
263 [Ciule] = {Ciugt, Ciuge},
264 [Ciult] = {Ciuge, Ciugt},
265 [Ciugt] = {Ciule, Ciult},
266 [Ciuge] = {Ciult, Ciule},
267 [Cisle] = {Cisgt, Cisge},
268 [Cislt] = {Cisge, Cisgt},
269 [Cisgt] = {Cisle, Cislt},
270 [Cisge] = {Cislt, Cisle},
271 [Cieq] = {Cine, Cieq},
272 [Cine] = {Cieq, Cine},
273 [NCmpI+Cfle] = {NCmpI+Cfgt, NCmpI+Cfge},
274 [NCmpI+Cflt] = {NCmpI+Cfge, NCmpI+Cfgt},
275 [NCmpI+Cfgt] = {NCmpI+Cfle, NCmpI+Cflt},
276 [NCmpI+Cfge] = {NCmpI+Cflt, NCmpI+Cfle},
277 [NCmpI+Cfeq] = {NCmpI+Cfne, NCmpI+Cfeq},
278 [NCmpI+Cfne] = {NCmpI+Cfeq, NCmpI+Cfne},
279 [NCmpI+Cfo] = {NCmpI+Cfuo, NCmpI+Cfo},
280 [NCmpI+Cfuo] = {NCmpI+Cfo, NCmpI+Cfuo},
284 cmpneg(int c)
286 assert(0 <= c && c < NCmp);
287 return cmptab[c][0];
291 cmpop(int c)
293 assert(0 <= c && c < NCmp);
294 return cmptab[c][1];
298 clsmerge(short *pk, short k)
300 short k1;
302 k1 = *pk;
303 if (k1 == Kx) {
304 *pk = k;
305 return 0;
307 if ((k1 == Kw && k == Kl) || (k1 == Kl && k == Kw)) {
308 *pk = Kw;
309 return 0;
311 return k1 != k;
315 phicls(int t, Tmp *tmp)
317 int t1;
319 t1 = tmp[t].phi;
320 if (!t1)
321 return t;
322 t1 = phicls(t1, tmp);
323 tmp[t].phi = t1;
324 return t1;
328 newtmp(char *prfx, int k, Fn *fn)
330 static int n;
331 int t;
333 t = fn->ntmp++;
334 vgrow(&fn->tmp, fn->ntmp);
335 memset(&fn->tmp[t], 0, sizeof(Tmp));
336 if (prfx)
337 sprintf(fn->tmp[t].name, "%s.%d", prfx, ++n);
338 fn->tmp[t].cls = k;
339 fn->tmp[t].slot = -1;
340 fn->tmp[t].nuse = +1;
341 fn->tmp[t].ndef = +1;
342 return TMP(t);
345 void
346 chuse(Ref r, int du, Fn *fn)
348 if (rtype(r) == RTmp)
349 fn->tmp[r.val].nuse += du;
353 symeq(Sym s0, Sym s1)
355 return s0.type == s1.type && s0.id == s1.id;
359 newcon(Con *c0, Fn *fn)
361 Con *c1;
362 int i;
364 for (i=1; i<fn->ncon; i++) {
365 c1 = &fn->con[i];
366 if (c0->type == c1->type
367 && symeq(c0->sym, c1->sym)
368 && c0->bits.i == c1->bits.i)
369 return CON(i);
371 vgrow(&fn->con, ++fn->ncon);
372 fn->con[i] = *c0;
373 return CON(i);
377 getcon(int64_t val, Fn *fn)
379 int c;
381 for (c=1; c<fn->ncon; c++)
382 if (fn->con[c].type == CBits
383 && fn->con[c].bits.i == val)
384 return CON(c);
385 vgrow(&fn->con, ++fn->ncon);
386 fn->con[c] = (Con){.type = CBits, .bits.i = val};
387 return CON(c);
391 addcon(Con *c0, Con *c1)
393 if (c0->type == CUndef)
394 *c0 = *c1;
395 else {
396 if (c1->type == CAddr) {
397 if (c0->type == CAddr)
398 return 0;
399 c0->type = CAddr;
400 c0->sym = c1->sym;
402 c0->bits.i += c1->bits.i;
404 return 1;
407 void
408 salloc(Ref rt, Ref rs, Fn *fn)
410 Ref r0, r1;
411 int64_t sz;
413 /* we need to make sure
414 * the stack remains aligned
415 * (rsp = 0) mod 16
417 fn->dynalloc = 1;
418 if (rtype(rs) == RCon) {
419 sz = fn->con[rs.val].bits.i;
420 if (sz < 0 || sz >= INT_MAX-15)
421 err("invalid alloc size %"PRId64, sz);
422 sz = (sz + 15) & -16;
423 emit(Osalloc, Kl, rt, getcon(sz, fn), R);
424 } else {
425 /* r0 = (r + 15) & -16 */
426 r0 = newtmp("isel", Kl, fn);
427 r1 = newtmp("isel", Kl, fn);
428 emit(Osalloc, Kl, rt, r0, R);
429 emit(Oand, Kl, r0, r1, getcon(-16, fn));
430 emit(Oadd, Kl, r1, rs, getcon(15, fn));
431 if (fn->tmp[rs.val].slot != -1)
432 err("unlikely alloc argument %%%s for %%%s",
433 fn->tmp[rs.val].name, fn->tmp[rt.val].name);
437 void
438 bsinit(BSet *bs, uint n)
440 n = (n + NBit-1) / NBit;
441 bs->nt = n;
442 bs->t = alloc(n * sizeof bs->t[0]);
445 MAKESURE(NBit_is_64, NBit == 64);
446 inline static uint
447 popcnt(bits b)
449 b = (b & 0x5555555555555555) + ((b>>1) & 0x5555555555555555);
450 b = (b & 0x3333333333333333) + ((b>>2) & 0x3333333333333333);
451 b = (b & 0x0f0f0f0f0f0f0f0f) + ((b>>4) & 0x0f0f0f0f0f0f0f0f);
452 b += (b>>8);
453 b += (b>>16);
454 b += (b>>32);
455 return b & 0xff;
458 inline static int
459 firstbit(bits b)
461 int n;
463 n = 0;
464 if (!(b & 0xffffffff)) {
465 n += 32;
466 b >>= 32;
468 if (!(b & 0xffff)) {
469 n += 16;
470 b >>= 16;
472 if (!(b & 0xff)) {
473 n += 8;
474 b >>= 8;
476 if (!(b & 0xf)) {
477 n += 4;
478 b >>= 4;
480 n += (char[16]){4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0}[b & 0xf];
481 return n;
484 uint
485 bscount(BSet *bs)
487 uint i, n;
489 n = 0;
490 for (i=0; i<bs->nt; i++)
491 n += popcnt(bs->t[i]);
492 return n;
495 static inline uint
496 bsmax(BSet *bs)
498 return bs->nt * NBit;
501 void
502 bsset(BSet *bs, uint elt)
504 assert(elt < bsmax(bs));
505 bs->t[elt/NBit] |= BIT(elt%NBit);
508 void
509 bsclr(BSet *bs, uint elt)
511 assert(elt < bsmax(bs));
512 bs->t[elt/NBit] &= ~BIT(elt%NBit);
515 #define BSOP(f, op) \
516 void \
517 f(BSet *a, BSet *b) \
519 uint i; \
521 assert(a->nt == b->nt); \
522 for (i=0; i<a->nt; i++) \
523 a->t[i] op b->t[i]; \
526 BSOP(bscopy, =)
527 BSOP(bsunion, |=)
528 BSOP(bsinter, &=)
529 BSOP(bsdiff, &= ~)
532 bsequal(BSet *a, BSet *b)
534 uint i;
536 assert(a->nt == b->nt);
537 for (i=0; i<a->nt; i++)
538 if (a->t[i] != b->t[i])
539 return 0;
540 return 1;
543 void
544 bszero(BSet *bs)
546 memset(bs->t, 0, bs->nt * sizeof bs->t[0]);
549 /* iterates on a bitset, use as follows
551 * for (i=0; bsiter(set, &i); i++)
552 * use(i);
556 bsiter(BSet *bs, int *elt)
558 bits b;
559 uint t, i;
561 i = *elt;
562 t = i/NBit;
563 if (t >= bs->nt)
564 return 0;
565 b = bs->t[t];
566 b &= ~(BIT(i%NBit) - 1);
567 while (!b) {
568 ++t;
569 if (t >= bs->nt)
570 return 0;
571 b = bs->t[t];
573 *elt = NBit*t + firstbit(b);
574 return 1;
577 void
578 dumpts(BSet *bs, Tmp *tmp, FILE *f)
580 int t;
582 fprintf(f, "[");
583 for (t=Tmp0; bsiter(bs, &t); t++)
584 fprintf(f, " %s", tmp[t].name);
585 fprintf(f, " ]\n");