swt.c 12 KB

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