swt.c 12 KB

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