swt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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*, Sym*, 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. s->sym = sym;
  393. zname(&outbuf, s, t);
  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. s->sym = sym;
  413. zname(&outbuf, s, t);
  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. /* on windows skip drive specifier in pathname */
  443. if(systemtype(Windows) && p && p[1] == ':'){
  444. p += 2;
  445. c = *p;
  446. }
  447. if(p && p[0] != c && h->offset == 0 && pathname){
  448. /* on windows skip drive specifier in pathname */
  449. if(systemtype(Windows) && pathname[1] == ':') {
  450. op = p;
  451. p = pathname+2;
  452. c = *p;
  453. } else if(pathname[0] == c){
  454. op = p;
  455. p = pathname;
  456. }
  457. }
  458. while(p) {
  459. q = utfrune(p, c);
  460. if(q) {
  461. n = q-p;
  462. if(n == 0){
  463. n = 1; /* leading "/" */
  464. *p = '/'; /* don't emit "\" on windows */
  465. }
  466. q++;
  467. } else {
  468. n = strlen(p);
  469. q = 0;
  470. }
  471. if(n) {
  472. Bputc(b, ANAME);
  473. Bputc(b, D_FILE);
  474. Bputc(b, 1);
  475. Bputc(b, '<');
  476. Bwrite(b, p, n);
  477. Bputc(b, 0);
  478. }
  479. p = q;
  480. if(p == 0 && op) {
  481. p = op;
  482. op = 0;
  483. }
  484. }
  485. pg.lineno = h->line;
  486. pg.to.type = zprog.to.type;
  487. pg.to.offset = h->offset;
  488. if(h->offset)
  489. pg.to.type = D_CONST;
  490. zwrite(b, &pg, 0, 0);
  491. }
  492. }
  493. void
  494. zname(Biobuf *b, Sym *s, int t)
  495. {
  496. char *n, bf[7];
  497. ulong sig;
  498. n = s->name;
  499. if(debug['T'] && t == D_EXTERN && s->sig != SIGDONE && s->type != types[TENUM] && s != symrathole){
  500. sig = sign(s);
  501. bf[0] = ASIGNAME;
  502. bf[1] = sig;
  503. bf[2] = sig>>8;
  504. bf[3] = sig>>16;
  505. bf[4] = sig>>24;
  506. bf[5] = t;
  507. bf[6] = s->sym;
  508. Bwrite(b, bf, 7);
  509. s->sig = SIGDONE;
  510. }
  511. else{
  512. bf[0] = ANAME;
  513. bf[1] = t; /* type */
  514. bf[2] = s->sym; /* sym */
  515. Bwrite(b, bf, 3);
  516. }
  517. Bwrite(b, n, strlen(n)+1);
  518. }
  519. char*
  520. zaddr(char *bp, Adr *a, int s)
  521. {
  522. long l;
  523. Ieee e;
  524. bp[0] = a->type;
  525. bp[1] = a->reg;
  526. bp[2] = s;
  527. bp[3] = a->name;
  528. bp += 4;
  529. switch(a->type) {
  530. default:
  531. diag(Z, "unknown type %d in zaddr", a->type);
  532. case D_NONE:
  533. case D_REG:
  534. case D_FREG:
  535. case D_MREG:
  536. case D_FCREG:
  537. case D_LO:
  538. case D_HI:
  539. break;
  540. case D_OREG:
  541. case D_CONST:
  542. case D_BRANCH:
  543. l = a->offset;
  544. bp[0] = l;
  545. bp[1] = l>>8;
  546. bp[2] = l>>16;
  547. bp[3] = l>>24;
  548. bp += 4;
  549. break;
  550. case D_SCONST:
  551. memmove(bp, a->sval, NSNAME);
  552. bp += NSNAME;
  553. break;
  554. case D_FCONST:
  555. ieeedtod(&e, a->dval);
  556. l = e.l;
  557. bp[0] = l;
  558. bp[1] = l>>8;
  559. bp[2] = l>>16;
  560. bp[3] = l>>24;
  561. bp += 4;
  562. l = e.h;
  563. bp[0] = l;
  564. bp[1] = l>>8;
  565. bp[2] = l>>16;
  566. bp[3] = l>>24;
  567. bp += 4;
  568. break;
  569. }
  570. return bp;
  571. }
  572. void
  573. ieeedtod(Ieee *ieee, double native)
  574. {
  575. double fr, ho, f;
  576. int exp;
  577. if(native < 0) {
  578. ieeedtod(ieee, -native);
  579. ieee->h |= 0x80000000L;
  580. return;
  581. }
  582. if(native == 0) {
  583. ieee->l = 0;
  584. ieee->h = 0;
  585. return;
  586. }
  587. fr = frexp(native, &exp);
  588. f = 2097152L; /* shouldnt use fp constants here */
  589. fr = modf(fr*f, &ho);
  590. ieee->h = ho;
  591. ieee->h &= 0xfffffL;
  592. ieee->h |= (exp+1022L) << 20;
  593. f = 65536L;
  594. fr = modf(fr*f, &ho);
  595. ieee->l = ho;
  596. ieee->l <<= 16;
  597. ieee->l |= (long)(fr*f);
  598. }
  599. long
  600. align(long i, Type *t, int op)
  601. {
  602. long o;
  603. Type *v;
  604. int w;
  605. o = i;
  606. w = 1;
  607. switch(op) {
  608. default:
  609. diag(Z, "unknown align opcode %d", op);
  610. break;
  611. case Asu2: /* padding at end of a struct */
  612. w = SZ_LONG;
  613. if(packflg)
  614. w = packflg;
  615. break;
  616. case Ael1: /* initial allign of struct element */
  617. for(v=t; v->etype==TARRAY; v=v->link)
  618. ;
  619. w = ewidth[v->etype];
  620. if(w <= 0 || w >= SZ_LONG)
  621. w = SZ_LONG;
  622. if(packflg)
  623. w = packflg;
  624. break;
  625. case Ael2: /* width of a struct element */
  626. o += t->width;
  627. break;
  628. case Aarg0: /* initial passbyptr argument in arg list */
  629. if(typesuv[t->etype]) {
  630. o = align(o, types[TIND], Aarg1);
  631. o = align(o, types[TIND], Aarg2);
  632. }
  633. break;
  634. case Aarg1: /* initial allign of parameter */
  635. w = ewidth[t->etype];
  636. if(w <= 0 || w >= SZ_LONG) {
  637. w = SZ_LONG;
  638. break;
  639. }
  640. o += SZ_LONG - w; /* big endian adjustment */
  641. w = 1;
  642. break;
  643. case Aarg2: /* width of a parameter */
  644. o += t->width;
  645. w = SZ_LONG;
  646. break;
  647. case Aaut3: /* total allign of automatic */
  648. o = align(o, t, Ael1);
  649. o = align(o, t, Ael2);
  650. break;
  651. }
  652. o = round(o, w);
  653. if(debug['A'])
  654. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  655. return o;
  656. }
  657. long
  658. maxround(long max, long v)
  659. {
  660. v += SZ_LONG-1;
  661. if(v > max)
  662. max = round(v, SZ_LONG);
  663. return max;
  664. }