2
0

util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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(Ins **pvins, uint *pnins, Blk *b)
  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. case Osel1:
  247. for (; i>ib && (i-1)->op == Osel1; i--)
  248. ;
  249. assert(i->op == Osel0);
  250. /* fall through */
  251. case Osel0:
  252. *i0 = i++;
  253. for (; i<ie && i->op == Osel1; i++)
  254. ;
  255. *i1 = i;
  256. return;
  257. default:
  258. if (ispar(i->op))
  259. goto case_Opar;
  260. if (isarg(i->op))
  261. goto case_Oarg;
  262. *i0 = i;
  263. *i1 = i + 1;
  264. return;
  265. }
  266. }
  267. int
  268. argcls(Ins *i, int n)
  269. {
  270. return optab[i->op].argcls[n][i->cls];
  271. }
  272. void
  273. emit(int op, int k, Ref to, Ref arg0, Ref arg1)
  274. {
  275. if (curi == insb)
  276. die("emit, too many instructions");
  277. *--curi = (Ins){
  278. .op = op, .cls = k,
  279. .to = to, .arg = {arg0, arg1}
  280. };
  281. }
  282. void
  283. emiti(Ins i)
  284. {
  285. emit(i.op, i.cls, i.to, i.arg[0], i.arg[1]);
  286. }
  287. void
  288. idup(Blk *b, Ins *s, ulong n)
  289. {
  290. vgrow(&b->ins, n);
  291. icpy(b->ins, s, n);
  292. b->nins = n;
  293. }
  294. Ins *
  295. icpy(Ins *d, Ins *s, ulong n)
  296. {
  297. if (n)
  298. memmove(d, s, n * sizeof(Ins));
  299. return d + n;
  300. }
  301. static int cmptab[][2] ={
  302. /* negation swap */
  303. [Ciule] = {Ciugt, Ciuge},
  304. [Ciult] = {Ciuge, Ciugt},
  305. [Ciugt] = {Ciule, Ciult},
  306. [Ciuge] = {Ciult, Ciule},
  307. [Cisle] = {Cisgt, Cisge},
  308. [Cislt] = {Cisge, Cisgt},
  309. [Cisgt] = {Cisle, Cislt},
  310. [Cisge] = {Cislt, Cisle},
  311. [Cieq] = {Cine, Cieq},
  312. [Cine] = {Cieq, Cine},
  313. [NCmpI+Cfle] = {NCmpI+Cfgt, NCmpI+Cfge},
  314. [NCmpI+Cflt] = {NCmpI+Cfge, NCmpI+Cfgt},
  315. [NCmpI+Cfgt] = {NCmpI+Cfle, NCmpI+Cflt},
  316. [NCmpI+Cfge] = {NCmpI+Cflt, NCmpI+Cfle},
  317. [NCmpI+Cfeq] = {NCmpI+Cfne, NCmpI+Cfeq},
  318. [NCmpI+Cfne] = {NCmpI+Cfeq, NCmpI+Cfne},
  319. [NCmpI+Cfo] = {NCmpI+Cfuo, NCmpI+Cfo},
  320. [NCmpI+Cfuo] = {NCmpI+Cfo, NCmpI+Cfuo},
  321. };
  322. int
  323. cmpneg(int c)
  324. {
  325. assert(0 <= c && c < NCmp);
  326. return cmptab[c][0];
  327. }
  328. int
  329. cmpop(int c)
  330. {
  331. assert(0 <= c && c < NCmp);
  332. return cmptab[c][1];
  333. }
  334. int
  335. cmpwlneg(int op)
  336. {
  337. if (INRANGE(op, Ocmpw, Ocmpw1))
  338. return cmpneg(op - Ocmpw) + Ocmpw;
  339. if (INRANGE(op, Ocmpl, Ocmpl1))
  340. return cmpneg(op - Ocmpl) + Ocmpl;
  341. die("not a wl comparison");
  342. }
  343. int
  344. clsmerge(short *pk, short k)
  345. {
  346. short k1;
  347. k1 = *pk;
  348. if (k1 == Kx) {
  349. *pk = k;
  350. return 0;
  351. }
  352. if ((k1 == Kw && k == Kl) || (k1 == Kl && k == Kw)) {
  353. *pk = Kw;
  354. return 0;
  355. }
  356. return k1 != k;
  357. }
  358. int
  359. phicls(int t, Tmp *tmp)
  360. {
  361. int t1;
  362. t1 = tmp[t].phi;
  363. if (!t1)
  364. return t;
  365. t1 = phicls(t1, tmp);
  366. tmp[t].phi = t1;
  367. return t1;
  368. }
  369. uint
  370. phiargn(Phi *p, Blk *b)
  371. {
  372. uint n;
  373. if (p)
  374. for (n=0; n<p->narg; n++)
  375. if (p->blk[n] == b)
  376. return n;
  377. return -1;
  378. }
  379. Ref
  380. phiarg(Phi *p, Blk *b)
  381. {
  382. uint n;
  383. n = phiargn(p, b);
  384. assert(n != -1u && "block not found");
  385. return p->arg[n];
  386. }
  387. Ref
  388. newtmp(char *prfx, int k, Fn *fn)
  389. {
  390. static int n;
  391. int t;
  392. t = fn->ntmp++;
  393. vgrow(&fn->tmp, fn->ntmp);
  394. memset(&fn->tmp[t], 0, sizeof(Tmp));
  395. if (prfx)
  396. strf(fn->tmp[t].name, "%s.%d", prfx, ++n);
  397. fn->tmp[t].cls = k;
  398. fn->tmp[t].slot = -1;
  399. fn->tmp[t].nuse = +1;
  400. fn->tmp[t].ndef = +1;
  401. return TMP(t);
  402. }
  403. void
  404. chuse(Ref r, int du, Fn *fn)
  405. {
  406. if (rtype(r) == RTmp)
  407. fn->tmp[r.val].nuse += du;
  408. }
  409. int
  410. symeq(Sym s0, Sym s1)
  411. {
  412. return s0.type == s1.type && s0.id == s1.id;
  413. }
  414. Ref
  415. newcon(Con *c0, Fn *fn)
  416. {
  417. Con *c1;
  418. int i;
  419. for (i=1; i<fn->ncon; i++) {
  420. c1 = &fn->con[i];
  421. if (c0->type == c1->type
  422. && symeq(c0->sym, c1->sym)
  423. && c0->bits.i == c1->bits.i)
  424. return CON(i);
  425. }
  426. vgrow(&fn->con, ++fn->ncon);
  427. fn->con[i] = *c0;
  428. return CON(i);
  429. }
  430. Ref
  431. getcon(int64_t val, Fn *fn)
  432. {
  433. int c;
  434. for (c=1; c<fn->ncon; c++)
  435. if (fn->con[c].type == CBits
  436. && fn->con[c].bits.i == val)
  437. return CON(c);
  438. vgrow(&fn->con, ++fn->ncon);
  439. fn->con[c] = (Con){.type = CBits, .bits.i = val};
  440. return CON(c);
  441. }
  442. int
  443. addcon(Con *c0, Con *c1, int m)
  444. {
  445. if (m != 1 && c1->type == CAddr)
  446. return 0;
  447. if (c0->type == CUndef) {
  448. *c0 = *c1;
  449. c0->bits.i *= m;
  450. } else {
  451. if (c1->type == CAddr) {
  452. if (c0->type == CAddr)
  453. return 0;
  454. c0->type = CAddr;
  455. c0->sym = c1->sym;
  456. }
  457. c0->bits.i += c1->bits.i * m;
  458. }
  459. return 1;
  460. }
  461. int
  462. isconbits(Fn *fn, Ref r, int64_t *v)
  463. {
  464. Con *c;
  465. if (rtype(r) == RCon) {
  466. c = &fn->con[r.val];
  467. if (c->type == CBits) {
  468. *v = c->bits.i;
  469. return 1;
  470. }
  471. }
  472. return 0;
  473. }
  474. void
  475. salloc(Ref rt, Ref rs, Fn *fn)
  476. {
  477. Ref r0, r1;
  478. int64_t sz;
  479. /* we need to make sure
  480. * the stack remains aligned
  481. * (rsp = 0) mod 16
  482. */
  483. fn->dynalloc = 1;
  484. if (rtype(rs) == RCon) {
  485. sz = fn->con[rs.val].bits.i;
  486. if (sz < 0 || sz >= INT_MAX-15)
  487. err("invalid alloc size %"PRId64, sz);
  488. sz = (sz + 15) & -16;
  489. emit(Osalloc, Kl, rt, getcon(sz, fn), R);
  490. } else {
  491. /* r0 = (r + 15) & -16 */
  492. r0 = newtmp("isel", Kl, fn);
  493. r1 = newtmp("isel", Kl, fn);
  494. emit(Osalloc, Kl, rt, r0, R);
  495. emit(Oand, Kl, r0, r1, getcon(-16, fn));
  496. emit(Oadd, Kl, r1, rs, getcon(15, fn));
  497. if (fn->tmp[rs.val].slot != -1)
  498. err("unlikely alloc argument %%%s for %%%s",
  499. fn->tmp[rs.val].name, fn->tmp[rt.val].name);
  500. }
  501. }
  502. void
  503. bsinit(BSet *bs, uint n)
  504. {
  505. n = (n + NBit-1) / NBit;
  506. bs->nt = n;
  507. bs->t = alloc(n * sizeof bs->t[0]);
  508. }
  509. MAKESURE(NBit_is_64, NBit == 64);
  510. inline static uint
  511. popcnt(bits b)
  512. {
  513. b = (b & 0x5555555555555555) + ((b>>1) & 0x5555555555555555);
  514. b = (b & 0x3333333333333333) + ((b>>2) & 0x3333333333333333);
  515. b = (b & 0x0f0f0f0f0f0f0f0f) + ((b>>4) & 0x0f0f0f0f0f0f0f0f);
  516. b += (b>>8);
  517. b += (b>>16);
  518. b += (b>>32);
  519. return b & 0xff;
  520. }
  521. inline static int
  522. firstbit(bits b)
  523. {
  524. int n;
  525. n = 0;
  526. if (!(b & 0xffffffff)) {
  527. n += 32;
  528. b >>= 32;
  529. }
  530. if (!(b & 0xffff)) {
  531. n += 16;
  532. b >>= 16;
  533. }
  534. if (!(b & 0xff)) {
  535. n += 8;
  536. b >>= 8;
  537. }
  538. if (!(b & 0xf)) {
  539. n += 4;
  540. b >>= 4;
  541. }
  542. n += (char[16]){4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0}[b & 0xf];
  543. return n;
  544. }
  545. uint
  546. bscount(BSet *bs)
  547. {
  548. uint i, n;
  549. n = 0;
  550. for (i=0; i<bs->nt; i++)
  551. n += popcnt(bs->t[i]);
  552. return n;
  553. }
  554. static inline uint
  555. bsmax(BSet *bs)
  556. {
  557. return bs->nt * NBit;
  558. }
  559. void
  560. bsset(BSet *bs, uint elt)
  561. {
  562. assert(elt < bsmax(bs));
  563. bs->t[elt/NBit] |= BIT(elt%NBit);
  564. }
  565. void
  566. bsclr(BSet *bs, uint elt)
  567. {
  568. assert(elt < bsmax(bs));
  569. bs->t[elt/NBit] &= ~BIT(elt%NBit);
  570. }
  571. #define BSOP(f, op) \
  572. void \
  573. f(BSet *a, BSet *b) \
  574. { \
  575. uint i; \
  576. \
  577. assert(a->nt == b->nt); \
  578. for (i=0; i<a->nt; i++) \
  579. a->t[i] op b->t[i]; \
  580. }
  581. BSOP(bscopy, =)
  582. BSOP(bsunion, |=)
  583. BSOP(bsinter, &=)
  584. BSOP(bsdiff, &= ~)
  585. int
  586. bsequal(BSet *a, BSet *b)
  587. {
  588. uint i;
  589. assert(a->nt == b->nt);
  590. for (i=0; i<a->nt; i++)
  591. if (a->t[i] != b->t[i])
  592. return 0;
  593. return 1;
  594. }
  595. void
  596. bszero(BSet *bs)
  597. {
  598. memset(bs->t, 0, bs->nt * sizeof bs->t[0]);
  599. }
  600. /* iterates on a bitset, use as follows
  601. *
  602. * for (i=0; bsiter(set, &i); i++)
  603. * use(i);
  604. *
  605. */
  606. int
  607. bsiter(BSet *bs, int *elt)
  608. {
  609. bits b;
  610. uint t, i;
  611. i = *elt;
  612. t = i/NBit;
  613. if (t >= bs->nt)
  614. return 0;
  615. b = bs->t[t];
  616. b &= ~(BIT(i%NBit) - 1);
  617. while (!b) {
  618. ++t;
  619. if (t >= bs->nt)
  620. return 0;
  621. b = bs->t[t];
  622. }
  623. *elt = NBit*t + firstbit(b);
  624. return 1;
  625. }
  626. void
  627. dumpts(BSet *bs, Tmp *tmp, FILE *f)
  628. {
  629. int t;
  630. fprintf(f, "[");
  631. for (t=Tmp0; bsiter(bs, &t); t++)
  632. fprintf(f, " %s", tmp[t].name);
  633. fprintf(f, " ]\n");
  634. }
  635. void
  636. runmatch(uchar *code, Num *tn, Ref ref, Ref *var)
  637. {
  638. Ref stkbuf[20], *stk;
  639. uchar *s, *pc;
  640. int bc, i;
  641. int n, nl, nr;
  642. assert(rtype(ref) == RTmp);
  643. stk = stkbuf;
  644. pc = code;
  645. while ((bc = *pc))
  646. switch (bc) {
  647. case 1: /* pushsym */
  648. case 2: /* push */
  649. assert(stk < &stkbuf[20]);
  650. assert(rtype(ref) == RTmp);
  651. nl = tn[ref.val].nl;
  652. nr = tn[ref.val].nr;
  653. if (bc == 1 && nl > nr) {
  654. *stk++ = tn[ref.val].l;
  655. ref = tn[ref.val].r;
  656. } else {
  657. *stk++ = tn[ref.val].r;
  658. ref = tn[ref.val].l;
  659. }
  660. pc++;
  661. break;
  662. case 3: /* set */
  663. var[*++pc] = ref;
  664. if (*(pc + 1) == 0)
  665. return;
  666. /* fall through */
  667. case 4: /* pop */
  668. assert(stk > &stkbuf[0]);
  669. ref = *--stk;
  670. pc++;
  671. break;
  672. case 5: /* switch */
  673. assert(rtype(ref) == RTmp);
  674. n = tn[ref.val].n;
  675. s = pc + 1;
  676. for (i=*s++; i>0; i--, s++)
  677. if (n == *s++)
  678. break;
  679. pc += *s;
  680. break;
  681. default: /* jump */
  682. assert(bc >= 10);
  683. pc = code + (bc - 10);
  684. break;
  685. }
  686. }