swt.c 10 KB

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