swt.c 11 KB

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