swt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. #include "gc.h"
  2. int
  3. swcmp(void *a1, void *a2)
  4. {
  5. C1 *p1, *p2;
  6. p1 = a1;
  7. p2 = a2;
  8. if(p1->val < p2->val)
  9. return -1;
  10. return p1->val > p2->val;
  11. }
  12. void
  13. doswit(Node *n)
  14. {
  15. Case *c;
  16. C1 *q, *iq;
  17. long def, nc, i;
  18. Node tn;
  19. def = 0;
  20. nc = 0;
  21. for(c = cases; c->link != C; c = c->link) {
  22. if(c->def) {
  23. if(def)
  24. diag(n, "more than one default in switch");
  25. def = c->label;
  26. continue;
  27. }
  28. nc++;
  29. }
  30. iq = alloc(nc*sizeof(C1));
  31. q = iq;
  32. for(c = cases; c->link != C; c = c->link) {
  33. if(c->def)
  34. continue;
  35. q->label = c->label;
  36. q->val = c->val;
  37. q++;
  38. }
  39. qsort(iq, nc, sizeof(C1), swcmp);
  40. if(def == 0)
  41. def = breakpc;
  42. for(i=0; i<nc-1; i++)
  43. if(iq[i].val == iq[i+1].val)
  44. diag(n, "duplicate cases in switch %ld", iq[i].val);
  45. regalloc(&tn, &regnode, Z);
  46. swit1(iq, nc, def, n, &tn);
  47. regfree(&tn);
  48. }
  49. void
  50. swit1(C1 *q, int nc, long def, Node *n, Node *tn)
  51. {
  52. C1 *r;
  53. int i;
  54. Prog *sp;
  55. if(nc < 5) {
  56. for(i=0; i<nc; i++) {
  57. if(sval(q->val)) {
  58. gopcode(OEQ, n, Z, nodconst(q->val));
  59. } else {
  60. gopcode(OSUB, nodconst(q->val), n, tn);
  61. gopcode(OEQ, tn, Z, nodconst(0));
  62. }
  63. patch(p, q->label);
  64. q++;
  65. }
  66. gbranch(OGOTO);
  67. patch(p, def);
  68. return;
  69. }
  70. i = nc / 2;
  71. r = q+i;
  72. if(sval(r->val)) {
  73. gopcode(OGT, n, Z, nodconst(r->val));
  74. sp = p;
  75. } else {
  76. gopcode(OSUB, nodconst(r->val), n, tn);
  77. gopcode(OGT, tn, Z, nodconst(0));
  78. sp = p;
  79. }
  80. gbranch(OGOTO);
  81. p->as = ABEQ;
  82. patch(p, r->label);
  83. swit1(q, i, def, n, tn);
  84. patch(sp, pc);
  85. swit1(r+1, nc-i-1, def, n, tn);
  86. }
  87. void
  88. cas(void)
  89. {
  90. Case *c;
  91. c = alloc(sizeof(*c));
  92. c->link = cases;
  93. cases = c;
  94. }
  95. void
  96. bitload(Node *b, Node *n1, Node *n2, Node *n3, Node *nn)
  97. {
  98. int sh;
  99. long v;
  100. Node *l;
  101. /*
  102. * n1 gets adjusted/masked value
  103. * n2 gets address of cell
  104. * n3 gets contents of cell
  105. */
  106. l = b->left;
  107. if(n2 != Z) {
  108. regalloc(n1, l, nn);
  109. reglcgen(n2, l, Z);
  110. regalloc(n3, l, Z);
  111. gopcode(OAS, n2, Z, n3);
  112. gopcode(OAS, n3, Z, n1);
  113. } else {
  114. regalloc(n1, l, nn);
  115. cgen(l, n1);
  116. }
  117. if(b->type->shift == 0 && typeu[b->type->etype]) {
  118. v = ~0 + (1L << b->type->nbits);
  119. gopcode(OAND, nodconst(v), Z, n1);
  120. } else {
  121. sh = 32 - b->type->shift - b->type->nbits;
  122. if(sh > 0)
  123. gopcode(OASHL, nodconst(sh), Z, n1);
  124. sh += b->type->shift;
  125. if(sh > 0)
  126. if(typeu[b->type->etype])
  127. gopcode(OLSHR, nodconst(sh), Z, n1);
  128. else
  129. gopcode(OASHR, nodconst(sh), Z, n1);
  130. }
  131. }
  132. void
  133. bitstore(Node *b, Node *n1, Node *n2, Node *n3, Node *nn)
  134. {
  135. long v;
  136. Node nod, *l;
  137. int sh;
  138. /*
  139. * n1 has adjusted/masked value
  140. * n2 has address of cell
  141. * n3 has contents of cell
  142. */
  143. l = b->left;
  144. regalloc(&nod, l, Z);
  145. v = ~0 + (1L << b->type->nbits);
  146. gopcode(OAND, nodconst(v), Z, n1);
  147. gopcode(OAS, n1, Z, &nod);
  148. if(nn != Z)
  149. gopcode(OAS, n1, Z, nn);
  150. sh = b->type->shift;
  151. if(sh > 0)
  152. gopcode(OASHL, nodconst(sh), Z, &nod);
  153. v <<= sh;
  154. gopcode(OAND, nodconst(~v), Z, n3);
  155. gopcode(OOR, n3, Z, &nod);
  156. gopcode(OAS, &nod, Z, n2);
  157. regfree(&nod);
  158. regfree(n1);
  159. regfree(n2);
  160. regfree(n3);
  161. }
  162. long
  163. outstring(char *s, long n)
  164. {
  165. long r;
  166. r = nstring;
  167. while(n) {
  168. string[mnstring] = *s++;
  169. mnstring++;
  170. nstring++;
  171. if(mnstring >= NSNAME) {
  172. gpseudo(ADATA, symstring, nodconst(0L));
  173. p->from.offset += nstring - NSNAME;
  174. p->reg = NSNAME;
  175. p->to.type = D_SCONST;
  176. memmove(p->to.sval, string, NSNAME);
  177. mnstring = 0;
  178. }
  179. n--;
  180. }
  181. return r;
  182. }
  183. long
  184. outlstring(ushort *s, long n)
  185. {
  186. char buf[2];
  187. int c;
  188. long r;
  189. while(nstring & 1)
  190. outstring("", 1);
  191. r = nstring;
  192. while(n > 0) {
  193. c = *s++;
  194. if(align(0, types[TCHAR], Aarg1)) {
  195. buf[0] = c>>8;
  196. buf[1] = c;
  197. } else {
  198. buf[0] = c;
  199. buf[1] = c>>8;
  200. }
  201. outstring(buf, 2);
  202. n -= sizeof(ushort);
  203. }
  204. return r;
  205. }
  206. int
  207. mulcon(Node *n, Node *nn)
  208. {
  209. Node *l, *r, nod1, nod2;
  210. Multab *m;
  211. long v;
  212. int o;
  213. char code[sizeof(m->code)+2], *p;
  214. if(typefd[n->type->etype])
  215. return 0;
  216. l = n->left;
  217. r = n->right;
  218. if(l->op == OCONST) {
  219. l = r;
  220. r = n->left;
  221. }
  222. if(r->op != OCONST)
  223. return 0;
  224. v = convvtox(r->vconst, n->type->etype);
  225. if(v != r->vconst) {
  226. if(debug['M'])
  227. print("%L multiply conv: %lld\n", n->lineno, r->vconst);
  228. return 0;
  229. }
  230. m = mulcon0(n, v);
  231. if(!m) {
  232. if(debug['M'])
  233. print("%L multiply table: %lld\n", n->lineno, r->vconst);
  234. return 0;
  235. }
  236. memmove(code, m->code, sizeof(m->code));
  237. code[sizeof(m->code)] = 0;
  238. p = code;
  239. if(p[1] == 'i')
  240. p += 2;
  241. regalloc(&nod1, n, nn);
  242. cgen(l, &nod1);
  243. if(v < 0)
  244. gopcode(ONEG, &nod1, Z, &nod1);
  245. regalloc(&nod2, n, Z);
  246. loop:
  247. switch(*p) {
  248. case 0:
  249. regfree(&nod2);
  250. gopcode(OAS, &nod1, Z, nn);
  251. regfree(&nod1);
  252. return 1;
  253. case '+':
  254. o = OADD;
  255. goto addsub;
  256. case '-':
  257. o = OSUB;
  258. addsub: /* number is r,n,l */
  259. v = p[1] - '0';
  260. r = &nod1;
  261. if(v&4)
  262. r = &nod2;
  263. n = &nod1;
  264. if(v&2)
  265. n = &nod2;
  266. l = &nod1;
  267. if(v&1)
  268. l = &nod2;
  269. gopcode(o, l, n, r);
  270. break;
  271. default: /* op is shiftcount, number is r,l */
  272. v = p[1] - '0';
  273. r = &nod1;
  274. if(v&2)
  275. r = &nod2;
  276. l = &nod1;
  277. if(v&1)
  278. l = &nod2;
  279. v = *p - 'a';
  280. if(v < 0 || v >= 32) {
  281. diag(n, "mulcon unknown op: %c%c", p[0], p[1]);
  282. break;
  283. }
  284. gopcode(OASHL, nodconst(v), l, r);
  285. break;
  286. }
  287. p += 2;
  288. goto loop;
  289. }
  290. void
  291. nullwarn(Node *l, Node *r)
  292. {
  293. warn(Z, "result of operation not used");
  294. if(l != Z)
  295. cgen(l, Z);
  296. if(r != Z)
  297. cgen(r, Z);
  298. }
  299. void
  300. sextern(Sym *s, Node *a, long o, long w)
  301. {
  302. long e, lw;
  303. for(e=0; e<w; e+=NSNAME) {
  304. lw = NSNAME;
  305. if(w-e < lw)
  306. lw = w-e;
  307. gpseudo(ADATA, s, nodconst(0));
  308. p->from.offset += o+e;
  309. p->reg = lw;
  310. p->to.type = D_SCONST;
  311. memmove(p->to.sval, a->cstring+e, lw);
  312. }
  313. }
  314. void
  315. gextern(Sym *s, Node *a, long o, long w)
  316. {
  317. if(a->op == OCONST && typev[a->type->etype]) {
  318. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  319. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  320. else
  321. gpseudo(ADATA, s, nod32const(a->vconst));
  322. p->from.offset += o;
  323. p->reg = 4;
  324. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  325. gpseudo(ADATA, s, nod32const(a->vconst));
  326. else
  327. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  328. p->from.offset += o + 4;
  329. p->reg = 4;
  330. return;
  331. }
  332. gpseudo(ADATA, s, a);
  333. p->from.offset += o;
  334. p->reg = w;
  335. if(p->to.type == D_OREG)
  336. p->to.type = D_CONST;
  337. }
  338. void zname(Biobuf*, char*, int, int);
  339. char* zaddr(char*, Adr*, int);
  340. void zwrite(Biobuf*, Prog*, int, int);
  341. void outhist(Biobuf*);
  342. void
  343. outcode(void)
  344. {
  345. struct { Sym *sym; short type; } h[NSYM];
  346. Prog *p;
  347. Sym *s;
  348. int sf, st, t, sym;
  349. if(debug['S']) {
  350. for(p = firstp; p != P; p = p->link)
  351. if(p->as != ADATA && p->as != AGLOBL)
  352. pc--;
  353. for(p = firstp; p != P; p = p->link) {
  354. print("%P\n", p);
  355. if(p->as != ADATA && p->as != AGLOBL)
  356. pc++;
  357. }
  358. }
  359. outhist(&outbuf);
  360. for(sym=0; sym<NSYM; sym++) {
  361. h[sym].sym = S;
  362. h[sym].type = 0;
  363. }
  364. sym = 1;
  365. for(p = firstp; p != P; p = p->link) {
  366. jackpot:
  367. sf = 0;
  368. s = p->from.sym;
  369. while(s != S) {
  370. sf = s->sym;
  371. if(sf < 0 || sf >= NSYM)
  372. sf = 0;
  373. t = p->from.name;
  374. if(h[sf].type == t)
  375. if(h[sf].sym == s)
  376. break;
  377. zname(&outbuf, s->name, t, sym);
  378. s->sym = sym;
  379. h[sym].sym = s;
  380. h[sym].type = t;
  381. sf = sym;
  382. sym++;
  383. if(sym >= NSYM)
  384. sym = 1;
  385. break;
  386. }
  387. st = 0;
  388. s = p->to.sym;
  389. while(s != S) {
  390. st = s->sym;
  391. if(st < 0 || st >= NSYM)
  392. st = 0;
  393. t = p->to.name;
  394. if(h[st].type == t)
  395. if(h[st].sym == s)
  396. break;
  397. zname(&outbuf, s->name, t, sym);
  398. s->sym = sym;
  399. h[sym].sym = s;
  400. h[sym].type = t;
  401. st = sym;
  402. sym++;
  403. if(sym >= NSYM)
  404. sym = 1;
  405. if(st == sf)
  406. goto jackpot;
  407. break;
  408. }
  409. zwrite(&outbuf, p, sf, st);
  410. }
  411. firstp = P;
  412. lastp = P;
  413. }
  414. void
  415. zwrite(Biobuf *b, Prog *p, int sf, int st)
  416. {
  417. char bf[100], *bp;
  418. long l;
  419. bf[0] = p->as;
  420. bf[1] = p->reg;
  421. l = p->lineno;
  422. bf[2] = l;
  423. bf[3] = l>>8;
  424. bf[4] = l>>16;
  425. bf[5] = l>>24;
  426. bp = zaddr(bf+6, &p->from, sf);
  427. bp = zaddr(bp, &p->to, st);
  428. Bwrite(b, bf, bp-bf);
  429. }
  430. void
  431. outhist(Biobuf *b)
  432. {
  433. Hist *h;
  434. char *p, *q, *op;
  435. Prog pg;
  436. int n;
  437. pg = zprog;
  438. pg.as = AHISTORY;
  439. for(h = hist; h != H; h = h->link) {
  440. p = h->name;
  441. op = 0;
  442. if(p && p[0] != '/' && h->offset == 0 && pathname && pathname[0] == '/') {
  443. op = p;
  444. p = pathname;
  445. }
  446. while(p) {
  447. q = utfrune(p, '/');
  448. if(q) {
  449. n = q-p;
  450. if(n == 0)
  451. n = 1; /* leading "/" */
  452. q++;
  453. } else {
  454. n = strlen(p);
  455. q = 0;
  456. }
  457. if(n) {
  458. Bputc(b, ANAME);
  459. Bputc(b, D_FILE);
  460. Bputc(b, 1);
  461. Bputc(b, '<');
  462. Bwrite(b, p, n);
  463. Bputc(b, 0);
  464. }
  465. p = q;
  466. if(p == 0 && op) {
  467. p = op;
  468. op = 0;
  469. }
  470. }
  471. pg.lineno = h->line;
  472. pg.to.type = zprog.to.type;
  473. pg.to.offset = h->offset;
  474. if(h->offset)
  475. pg.to.type = D_CONST;
  476. zwrite(b, &pg, 0, 0);
  477. }
  478. }
  479. void
  480. zname(Biobuf *b, char *n, int t, int s)
  481. {
  482. char bf[3];
  483. bf[0] = ANAME;
  484. bf[1] = t; /* type */
  485. bf[2] = s; /* sym */
  486. Bwrite(b, bf, 3);
  487. Bwrite(b, n, strlen(n)+1);
  488. }
  489. char*
  490. zaddr(char *bp, Adr *a, int s)
  491. {
  492. long l;
  493. Ieee e;
  494. bp[0] = a->type;
  495. bp[1] = a->reg;
  496. bp[2] = s;
  497. bp[3] = a->name;
  498. bp += 4;
  499. switch(a->type) {
  500. default:
  501. diag(Z, "unknown type %d in zaddr", a->type);
  502. case D_NONE:
  503. case D_REG:
  504. case D_FREG:
  505. case D_CREG:
  506. break;
  507. case D_OREG:
  508. case D_CONST:
  509. case D_BRANCH:
  510. l = a->offset;
  511. bp[0] = l;
  512. bp[1] = l>>8;
  513. bp[2] = l>>16;
  514. bp[3] = l>>24;
  515. bp += 4;
  516. break;
  517. case D_SCONST:
  518. memmove(bp, a->sval, NSNAME);
  519. bp += NSNAME;
  520. break;
  521. case D_FCONST:
  522. ieeedtod(&e, a->dval);
  523. l = e.l;
  524. bp[0] = l;
  525. bp[1] = l>>8;
  526. bp[2] = l>>16;
  527. bp[3] = l>>24;
  528. bp += 4;
  529. l = e.h;
  530. bp[0] = l;
  531. bp[1] = l>>8;
  532. bp[2] = l>>16;
  533. bp[3] = l>>24;
  534. bp += 4;
  535. break;
  536. }
  537. return bp;
  538. }
  539. void
  540. ieeedtod(Ieee *ieee, double native)
  541. {
  542. double fr, ho, f;
  543. int exp;
  544. if(native < 0) {
  545. ieeedtod(ieee, -native);
  546. ieee->h |= 0x80000000L;
  547. return;
  548. }
  549. if(native == 0) {
  550. ieee->l = 0;
  551. ieee->h = 0;
  552. return;
  553. }
  554. fr = frexp(native, &exp);
  555. f = 2097152L; /* shouldnt use fp constants here */
  556. fr = modf(fr*f, &ho);
  557. ieee->h = ho;
  558. ieee->h &= 0xfffffL;
  559. ieee->h |= (exp+1022L) << 20;
  560. f = 65536L;
  561. fr = modf(fr*f, &ho);
  562. ieee->l = ho;
  563. ieee->l <<= 16;
  564. ieee->l |= (long)(fr*f);
  565. }
  566. long
  567. align(long i, Type *t, int op)
  568. {
  569. long o;
  570. Type *v;
  571. int w;
  572. o = i;
  573. w = 1;
  574. switch(op) {
  575. default:
  576. diag(Z, "unknown align opcode %d", op);
  577. break;
  578. case Asu2: /* padding at end of a struct */
  579. w = SZ_LONG;
  580. break;
  581. case Ael1: /* initial allign of struct element */
  582. for(v=t; v->etype==TARRAY; v=v->link)
  583. ;
  584. w = ewidth[v->etype];
  585. if(w <= 0 || w >= SZ_LONG)
  586. w = SZ_LONG;
  587. break;
  588. case Ael2: /* width of a struct element */
  589. o += t->width;
  590. break;
  591. case Aarg0: /* initial passbyptr argument in arg list */
  592. if(typesuv[t->etype]) {
  593. o = align(o, types[TIND], Aarg1);
  594. o = align(o, types[TIND], Aarg2);
  595. }
  596. break;
  597. case Aarg1: /* initial allign of parameter */
  598. w = ewidth[t->etype];
  599. if(w <= 0 || w >= SZ_LONG) {
  600. w = SZ_LONG;
  601. break;
  602. }
  603. o += SZ_LONG - w; /* big endian adjustment */
  604. w = 1;
  605. break;
  606. case Aarg2: /* width of a parameter */
  607. o += t->width;
  608. w = SZ_LONG;
  609. break;
  610. case Aaut3: /* total allign of automatic */
  611. o = align(o, t, Ael1);
  612. o = align(o, t, Ael2);
  613. break;
  614. }
  615. o = round(o, w);
  616. if(debug['A'])
  617. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  618. return o;
  619. }
  620. long
  621. maxround(long max, long v)
  622. {
  623. v += SZ_LONG-1;
  624. if(v > max)
  625. max = round(v, SZ_LONG);
  626. return max;
  627. }