swt.c 13 KB

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