util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. #include "all.h"
  2. #include <stdarg.h>
  3. typedef struct Bitset Bitset;
  4. typedef struct Vec Vec;
  5. typedef struct Bucket Bucket;
  6. struct Vec {
  7. ulong mag;
  8. Pool pool;
  9. size_t esz;
  10. ulong cap;
  11. union {
  12. long long ll;
  13. long double ld;
  14. void *ptr;
  15. } align[];
  16. };
  17. struct Bucket {
  18. uint nstr;
  19. char **str;
  20. };
  21. enum {
  22. VMin = 2,
  23. VMag = 0xcabba9e,
  24. NPtr = 256,
  25. IBits = 12,
  26. IMask = (1<<IBits) - 1,
  27. };
  28. Typ *typ;
  29. Ins insb[NIns], *curi;
  30. static void *ptr[NPtr];
  31. static void **pool = ptr;
  32. static int nptr = 1;
  33. static Bucket itbl[IMask+1]; /* string interning table */
  34. uint32_t
  35. hash(char *s)
  36. {
  37. uint32_t h;
  38. for (h=0; *s; ++s)
  39. h = *s + 17*h;
  40. return h;
  41. }
  42. void
  43. die_(char *file, char *s, ...)
  44. {
  45. va_list ap;
  46. fprintf(stderr, "%s: dying: ", file);
  47. va_start(ap, s);
  48. vfprintf(stderr, s, ap);
  49. va_end(ap);
  50. fputc('\n', stderr);
  51. abort();
  52. }
  53. void *
  54. emalloc(size_t n)
  55. {
  56. void *p;
  57. p = calloc(1, n);
  58. if (!p)
  59. die("emalloc, out of memory");
  60. return p;
  61. }
  62. void *
  63. alloc(size_t n)
  64. {
  65. void **pp;
  66. if (n == 0)
  67. return 0;
  68. if (nptr >= NPtr) {
  69. pp = emalloc(NPtr * sizeof(void *));
  70. pp[0] = pool;
  71. pool = pp;
  72. nptr = 1;
  73. }
  74. return pool[nptr++] = emalloc(n);
  75. }
  76. void
  77. freeall()
  78. {
  79. void **pp;
  80. for (;;) {
  81. for (pp = &pool[1]; pp < &pool[nptr]; pp++)
  82. free(*pp);
  83. pp = pool[0];
  84. if (!pp)
  85. break;
  86. free(pool);
  87. pool = pp;
  88. nptr = NPtr;
  89. }
  90. nptr = 1;
  91. }
  92. void *
  93. vnew(ulong len, size_t esz, Pool pool)
  94. {
  95. void *(*f)(size_t);
  96. ulong cap;
  97. Vec *v;
  98. for (cap=VMin; cap<len; cap*=2)
  99. ;
  100. f = pool == PHeap ? emalloc : alloc;
  101. v = f(cap * esz + sizeof(Vec));
  102. v->mag = VMag;
  103. v->cap = cap;
  104. v->esz = esz;
  105. v->pool = pool;
  106. return v + 1;
  107. }
  108. void
  109. vfree(void *p)
  110. {
  111. Vec *v;
  112. v = (Vec *)p - 1;
  113. assert(v->mag == VMag);
  114. if (v->pool == PHeap) {
  115. v->mag = 0;
  116. free(v);
  117. }
  118. }
  119. void
  120. vgrow(void *vp, ulong len)
  121. {
  122. Vec *v;
  123. void *v1;
  124. v = *(Vec **)vp - 1;
  125. assert(v+1 && v->mag == VMag);
  126. if (v->cap >= len)
  127. return;
  128. v1 = vnew(len, v->esz, v->pool);
  129. memcpy(v1, v+1, v->cap * v->esz);
  130. vfree(v+1);
  131. *(Vec **)vp = v1;
  132. }
  133. void
  134. addins(Ins **pvins, uint *pnins, Ins *i)
  135. {
  136. if (i->op == Onop)
  137. return;
  138. vgrow(pvins, ++(*pnins));
  139. (*pvins)[(*pnins)-1] = *i;
  140. }
  141. void
  142. addbins(Blk *b, Ins **pvins, uint *pnins)
  143. {
  144. Ins *i;
  145. for (i=b->ins; i<&b->ins[b->nins]; i++)
  146. addins(pvins, pnins, i);
  147. }
  148. void
  149. strf(char str[NString], char *s, ...)
  150. {
  151. va_list ap;
  152. va_start(ap, s);
  153. vsnprintf(str, NString, s, ap);
  154. va_end(ap);
  155. }
  156. uint32_t
  157. intern(char *s)
  158. {
  159. Bucket *b;
  160. uint32_t h;
  161. uint i, n;
  162. h = hash(s) & IMask;
  163. b = &itbl[h];
  164. n = b->nstr;
  165. for (i=0; i<n; i++)
  166. if (strcmp(s, b->str[i]) == 0)
  167. return h + (i<<IBits);
  168. if (n == 1<<(32-IBits))
  169. die("interning table overflow");
  170. if (n == 0)
  171. b->str = vnew(1, sizeof b->str[0], PHeap);
  172. else if ((n & (n-1)) == 0)
  173. vgrow(&b->str, n+n);
  174. b->str[n] = emalloc(strlen(s)+1);
  175. b->nstr = n + 1;
  176. strcpy(b->str[n], s);
  177. return h + (n<<IBits);
  178. }
  179. char *
  180. str(uint32_t id)
  181. {
  182. assert(id>>IBits < itbl[id&IMask].nstr);
  183. return itbl[id&IMask].str[id>>IBits];
  184. }
  185. int
  186. isreg(Ref r)
  187. {
  188. return rtype(r) == RTmp && r.val < Tmp0;
  189. }
  190. int
  191. iscmp(int op, int *pk, int *pc)
  192. {
  193. if (Ocmpw <= op && op <= Ocmpw1) {
  194. *pc = op - Ocmpw;
  195. *pk = Kw;
  196. }
  197. else if (Ocmpl <= op && op <= Ocmpl1) {
  198. *pc = op - Ocmpl;
  199. *pk = Kl;
  200. }
  201. else if (Ocmps <= op && op <= Ocmps1) {
  202. *pc = NCmpI + op - Ocmps;
  203. *pk = Ks;
  204. }
  205. else if (Ocmpd <= op && op <= Ocmpd1) {
  206. *pc = NCmpI + op - Ocmpd;
  207. *pk = Kd;
  208. }
  209. else
  210. return 0;
  211. return 1;
  212. }
  213. void
  214. igroup(Blk *b, Ins *i, Ins **i0, Ins **i1)
  215. {
  216. Ins *ib, *ie;
  217. ib = b->ins;
  218. ie = ib + b->nins;
  219. switch (i->op) {
  220. case Oblit0:
  221. *i0 = i;
  222. *i1 = i + 2;
  223. return;
  224. case Oblit1:
  225. *i0 = i - 1;
  226. *i1 = i + 1;
  227. return;
  228. case_Opar:
  229. for (; i>ib && ispar((i-1)->op); i--)
  230. ;
  231. *i0 = i;
  232. for (; i<ie && ispar(i->op); i++)
  233. ;
  234. *i1 = i;
  235. return;
  236. case Ocall:
  237. case_Oarg:
  238. for (; i>ib && isarg((i-1)->op); i--)
  239. ;
  240. *i0 = i;
  241. for (; i<ie && i->op != Ocall; i++)
  242. ;
  243. assert(i < ie);
  244. *i1 = i + 1;
  245. return;
  246. default:
  247. if (ispar(i->op))
  248. goto case_Opar;
  249. if (isarg(i->op))
  250. goto case_Oarg;
  251. *i0 = i;
  252. *i1 = i + 1;
  253. return;
  254. }
  255. }
  256. int
  257. argcls(Ins *i, int n)
  258. {
  259. return optab[i->op].argcls[n][i->cls];
  260. }
  261. void
  262. emit(int op, int k, Ref to, Ref arg0, Ref arg1)
  263. {
  264. if (curi == insb)
  265. die("emit, too many instructions");
  266. *--curi = (Ins){
  267. .op = op, .cls = k,
  268. .to = to, .arg = {arg0, arg1}
  269. };
  270. }
  271. void
  272. emiti(Ins i)
  273. {
  274. emit(i.op, i.cls, i.to, i.arg[0], i.arg[1]);
  275. }
  276. void
  277. idup(Blk *b, Ins *s, ulong n)
  278. {
  279. vgrow(&b->ins, n);
  280. icpy(b->ins, s, n);
  281. b->nins = n;
  282. }
  283. Ins *
  284. icpy(Ins *d, Ins *s, ulong n)
  285. {
  286. if (n)
  287. memmove(d, s, n * sizeof(Ins));
  288. return d + n;
  289. }
  290. static int cmptab[][2] ={
  291. /* negation swap */
  292. [Ciule] = {Ciugt, Ciuge},
  293. [Ciult] = {Ciuge, Ciugt},
  294. [Ciugt] = {Ciule, Ciult},
  295. [Ciuge] = {Ciult, Ciule},
  296. [Cisle] = {Cisgt, Cisge},
  297. [Cislt] = {Cisge, Cisgt},
  298. [Cisgt] = {Cisle, Cislt},
  299. [Cisge] = {Cislt, Cisle},
  300. [Cieq] = {Cine, Cieq},
  301. [Cine] = {Cieq, Cine},
  302. [NCmpI+Cfle] = {NCmpI+Cfgt, NCmpI+Cfge},
  303. [NCmpI+Cflt] = {NCmpI+Cfge, NCmpI+Cfgt},
  304. [NCmpI+Cfgt] = {NCmpI+Cfle, NCmpI+Cflt},
  305. [NCmpI+Cfge] = {NCmpI+Cflt, NCmpI+Cfle},
  306. [NCmpI+Cfeq] = {NCmpI+Cfne, NCmpI+Cfeq},
  307. [NCmpI+Cfne] = {NCmpI+Cfeq, NCmpI+Cfne},
  308. [NCmpI+Cfo] = {NCmpI+Cfuo, NCmpI+Cfo},
  309. [NCmpI+Cfuo] = {NCmpI+Cfo, NCmpI+Cfuo},
  310. };
  311. int
  312. cmpneg(int c)
  313. {
  314. assert(0 <= c && c < NCmp);
  315. return cmptab[c][0];
  316. }
  317. int
  318. cmpop(int c)
  319. {
  320. assert(0 <= c && c < NCmp);
  321. return cmptab[c][1];
  322. }
  323. int
  324. cmpwlneg(int op)
  325. {
  326. if (INRANGE(op, Ocmpw, Ocmpw1))
  327. return cmpneg(op - Ocmpw) + Ocmpw;
  328. if (INRANGE(op, Ocmpl, Ocmpl1))
  329. return cmpneg(op - Ocmpl) + Ocmpl;
  330. die("not a wl comparison");
  331. }
  332. int
  333. clsmerge(short *pk, short k)
  334. {
  335. short k1;
  336. k1 = *pk;
  337. if (k1 == Kx) {
  338. *pk = k;
  339. return 0;
  340. }
  341. if ((k1 == Kw && k == Kl) || (k1 == Kl && k == Kw)) {
  342. *pk = Kw;
  343. return 0;
  344. }
  345. return k1 != k;
  346. }
  347. int
  348. phicls(int t, Tmp *tmp)
  349. {
  350. int t1;
  351. t1 = tmp[t].phi;
  352. if (!t1)
  353. return t;
  354. t1 = phicls(t1, tmp);
  355. tmp[t].phi = t1;
  356. return t1;
  357. }
  358. uint
  359. phiargn(Phi *p, Blk *b)
  360. {
  361. uint n;
  362. if (p)
  363. for (n=0; n<p->narg; n++)
  364. if (p->blk[n] == b)
  365. return n;
  366. return -1;
  367. }
  368. Ref
  369. phiarg(Phi *p, Blk *b)
  370. {
  371. uint n;
  372. n = phiargn(p, b);
  373. assert(n != -1u && "block not found");
  374. return p->arg[n];
  375. }
  376. Ref
  377. newtmp(char *prfx, int k, Fn *fn)
  378. {
  379. static int n;
  380. int t;
  381. t = fn->ntmp++;
  382. vgrow(&fn->tmp, fn->ntmp);
  383. memset(&fn->tmp[t], 0, sizeof(Tmp));
  384. if (prfx)
  385. strf(fn->tmp[t].name, "%s.%d", prfx, ++n);
  386. fn->tmp[t].cls = k;
  387. fn->tmp[t].slot = -1;
  388. fn->tmp[t].nuse = +1;
  389. fn->tmp[t].ndef = +1;
  390. return TMP(t);
  391. }
  392. void
  393. chuse(Ref r, int du, Fn *fn)
  394. {
  395. if (rtype(r) == RTmp)
  396. fn->tmp[r.val].nuse += du;
  397. }
  398. int
  399. symeq(Sym s0, Sym s1)
  400. {
  401. return s0.type == s1.type && s0.id == s1.id;
  402. }
  403. Ref
  404. newcon(Con *c0, Fn *fn)
  405. {
  406. Con *c1;
  407. int i;
  408. for (i=1; i<fn->ncon; i++) {
  409. c1 = &fn->con[i];
  410. if (c0->type == c1->type
  411. && symeq(c0->sym, c1->sym)
  412. && c0->bits.i == c1->bits.i)
  413. return CON(i);
  414. }
  415. vgrow(&fn->con, ++fn->ncon);
  416. fn->con[i] = *c0;
  417. return CON(i);
  418. }
  419. Ref
  420. getcon(int64_t val, Fn *fn)
  421. {
  422. int c;
  423. for (c=1; c<fn->ncon; c++)
  424. if (fn->con[c].type == CBits
  425. && fn->con[c].bits.i == val)
  426. return CON(c);
  427. vgrow(&fn->con, ++fn->ncon);
  428. fn->con[c] = (Con){.type = CBits, .bits.i = val};
  429. return CON(c);
  430. }
  431. int
  432. addcon(Con *c0, Con *c1, int m)
  433. {
  434. if (m != 1 && c1->type == CAddr)
  435. return 0;
  436. if (c0->type == CUndef) {
  437. *c0 = *c1;
  438. c0->bits.i *= m;
  439. } else {
  440. if (c1->type == CAddr) {
  441. if (c0->type == CAddr)
  442. return 0;
  443. c0->type = CAddr;
  444. c0->sym = c1->sym;
  445. }
  446. c0->bits.i += c1->bits.i * m;
  447. }
  448. return 1;
  449. }
  450. int
  451. isconbits(Fn *fn, Ref r, int64_t *v)
  452. {
  453. Con *c;
  454. if (rtype(r) == RCon) {
  455. c = &fn->con[r.val];
  456. if (c->type == CBits) {
  457. *v = c->bits.i;
  458. return 1;
  459. }
  460. }
  461. return 0;
  462. }
  463. void
  464. salloc(Ref rt, Ref rs, Fn *fn)
  465. {
  466. Ref r0, r1;
  467. int64_t sz;
  468. /* we need to make sure
  469. * the stack remains aligned
  470. * (rsp = 0) mod 16
  471. */
  472. fn->dynalloc = 1;
  473. if (rtype(rs) == RCon) {
  474. sz = fn->con[rs.val].bits.i;
  475. if (sz < 0 || sz >= INT_MAX-15)
  476. err("invalid alloc size %"PRId64, sz);
  477. sz = (sz + 15) & -16;
  478. emit(Osalloc, Kl, rt, getcon(sz, fn), R);
  479. } else {
  480. /* r0 = (r + 15) & -16 */
  481. r0 = newtmp("isel", Kl, fn);
  482. r1 = newtmp("isel", Kl, fn);
  483. emit(Osalloc, Kl, rt, r0, R);
  484. emit(Oand, Kl, r0, r1, getcon(-16, fn));
  485. emit(Oadd, Kl, r1, rs, getcon(15, fn));
  486. if (fn->tmp[rs.val].slot != -1)
  487. err("unlikely alloc argument %%%s for %%%s",
  488. fn->tmp[rs.val].name, fn->tmp[rt.val].name);
  489. }
  490. }
  491. void
  492. bsinit(BSet *bs, uint n)
  493. {
  494. n = (n + NBit-1) / NBit;
  495. bs->nt = n;
  496. bs->t = alloc(n * sizeof bs->t[0]);
  497. }
  498. MAKESURE(NBit_is_64, NBit == 64);
  499. inline static uint
  500. popcnt(bits b)
  501. {
  502. b = (b & 0x5555555555555555) + ((b>>1) & 0x5555555555555555);
  503. b = (b & 0x3333333333333333) + ((b>>2) & 0x3333333333333333);
  504. b = (b & 0x0f0f0f0f0f0f0f0f) + ((b>>4) & 0x0f0f0f0f0f0f0f0f);
  505. b += (b>>8);
  506. b += (b>>16);
  507. b += (b>>32);
  508. return b & 0xff;
  509. }
  510. inline static int
  511. firstbit(bits b)
  512. {
  513. int n;
  514. n = 0;
  515. if (!(b & 0xffffffff)) {
  516. n += 32;
  517. b >>= 32;
  518. }
  519. if (!(b & 0xffff)) {
  520. n += 16;
  521. b >>= 16;
  522. }
  523. if (!(b & 0xff)) {
  524. n += 8;
  525. b >>= 8;
  526. }
  527. if (!(b & 0xf)) {
  528. n += 4;
  529. b >>= 4;
  530. }
  531. n += (char[16]){4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0}[b & 0xf];
  532. return n;
  533. }
  534. uint
  535. bscount(BSet *bs)
  536. {
  537. uint i, n;
  538. n = 0;
  539. for (i=0; i<bs->nt; i++)
  540. n += popcnt(bs->t[i]);
  541. return n;
  542. }
  543. static inline uint
  544. bsmax(BSet *bs)
  545. {
  546. return bs->nt * NBit;
  547. }
  548. void
  549. bsset(BSet *bs, uint elt)
  550. {
  551. assert(elt < bsmax(bs));
  552. bs->t[elt/NBit] |= BIT(elt%NBit);
  553. }
  554. void
  555. bsclr(BSet *bs, uint elt)
  556. {
  557. assert(elt < bsmax(bs));
  558. bs->t[elt/NBit] &= ~BIT(elt%NBit);
  559. }
  560. #define BSOP(f, op) \
  561. void \
  562. f(BSet *a, BSet *b) \
  563. { \
  564. uint i; \
  565. \
  566. assert(a->nt == b->nt); \
  567. for (i=0; i<a->nt; i++) \
  568. a->t[i] op b->t[i]; \
  569. }
  570. BSOP(bscopy, =)
  571. BSOP(bsunion, |=)
  572. BSOP(bsinter, &=)
  573. BSOP(bsdiff, &= ~)
  574. int
  575. bsequal(BSet *a, BSet *b)
  576. {
  577. uint i;
  578. assert(a->nt == b->nt);
  579. for (i=0; i<a->nt; i++)
  580. if (a->t[i] != b->t[i])
  581. return 0;
  582. return 1;
  583. }
  584. void
  585. bszero(BSet *bs)
  586. {
  587. memset(bs->t, 0, bs->nt * sizeof bs->t[0]);
  588. }
  589. /* iterates on a bitset, use as follows
  590. *
  591. * for (i=0; bsiter(set, &i); i++)
  592. * use(i);
  593. *
  594. */
  595. int
  596. bsiter(BSet *bs, int *elt)
  597. {
  598. bits b;
  599. uint t, i;
  600. i = *elt;
  601. t = i/NBit;
  602. if (t >= bs->nt)
  603. return 0;
  604. b = bs->t[t];
  605. b &= ~(BIT(i%NBit) - 1);
  606. while (!b) {
  607. ++t;
  608. if (t >= bs->nt)
  609. return 0;
  610. b = bs->t[t];
  611. }
  612. *elt = NBit*t + firstbit(b);
  613. return 1;
  614. }
  615. void
  616. dumpts(BSet *bs, Tmp *tmp, FILE *f)
  617. {
  618. int t;
  619. fprintf(f, "[");
  620. for (t=Tmp0; bsiter(bs, &t); t++)
  621. fprintf(f, " %s", tmp[t].name);
  622. fprintf(f, " ]\n");
  623. }
  624. void
  625. runmatch(uchar *code, Num *tn, Ref ref, Ref *var)
  626. {
  627. Ref stkbuf[20], *stk;
  628. uchar *s, *pc;
  629. int bc, i;
  630. int n, nl, nr;
  631. assert(rtype(ref) == RTmp);
  632. stk = stkbuf;
  633. pc = code;
  634. while ((bc = *pc))
  635. switch (bc) {
  636. case 1: /* pushsym */
  637. case 2: /* push */
  638. assert(stk < &stkbuf[20]);
  639. assert(rtype(ref) == RTmp);
  640. nl = tn[ref.val].nl;
  641. nr = tn[ref.val].nr;
  642. if (bc == 1 && nl > nr) {
  643. *stk++ = tn[ref.val].l;
  644. ref = tn[ref.val].r;
  645. } else {
  646. *stk++ = tn[ref.val].r;
  647. ref = tn[ref.val].l;
  648. }
  649. pc++;
  650. break;
  651. case 3: /* set */
  652. var[*++pc] = ref;
  653. if (*(pc + 1) == 0)
  654. return;
  655. /* fall through */
  656. case 4: /* pop */
  657. assert(stk > &stkbuf[0]);
  658. ref = *--stk;
  659. pc++;
  660. break;
  661. case 5: /* switch */
  662. assert(rtype(ref) == RTmp);
  663. n = tn[ref.val].n;
  664. s = pc + 1;
  665. for (i=*s++; i>0; i--, s++)
  666. if (n == *s++)
  667. break;
  668. pc += *s;
  669. break;
  670. default: /* jump */
  671. assert(bc >= 10);
  672. pc = code + (bc - 10);
  673. break;
  674. }
  675. }