swt.c 11 KB

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