swt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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['W'])
  25. print("case = %.8lux\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['W'])
  37. print("case > %.8lux\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['W'])
  44. print("case < %.8lux\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['W'])
  56. print("case = %.8lux\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. sextern(Sym *s, Node *a, long o, long w)
  253. {
  254. long e, lw;
  255. for(e=0; e<w; e+=NSNAME) {
  256. lw = NSNAME;
  257. if(w-e < lw)
  258. lw = w-e;
  259. gpseudo(ADATA, s, nodconst(0));
  260. p->from.offset += o+e;
  261. p->reg = lw;
  262. p->to.type = D_SCONST;
  263. memmove(p->to.sval, a->cstring+e, lw);
  264. }
  265. }
  266. void
  267. gextern(Sym *s, Node *a, long o, long w)
  268. {
  269. if(a->op == OCONST && typev[a->type->etype]) {
  270. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  271. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  272. else
  273. gpseudo(ADATA, s, nod32const(a->vconst));
  274. p->from.offset += o;
  275. p->reg = 4;
  276. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  277. gpseudo(ADATA, s, nod32const(a->vconst));
  278. else
  279. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  280. p->from.offset += o + 4;
  281. p->reg = 4;
  282. return;
  283. }
  284. gpseudo(ADATA, s, a);
  285. p->from.offset += o;
  286. p->reg = w;
  287. if(p->to.type == D_OREG)
  288. p->to.type = D_CONST;
  289. }
  290. void zname(Biobuf*, Sym*, int);
  291. char* zaddr(char*, Adr*, int);
  292. void zwrite(Biobuf*, Prog*, int, int);
  293. void outhist(Biobuf*);
  294. void
  295. zwrite(Biobuf *b, Prog *p, int sf, int st)
  296. {
  297. char bf[100], *bp;
  298. bf[0] = p->as;
  299. bf[1] = p->scond;
  300. bf[2] = p->reg;
  301. bf[3] = p->lineno;
  302. bf[4] = p->lineno>>8;
  303. bf[5] = p->lineno>>16;
  304. bf[6] = p->lineno>>24;
  305. bp = zaddr(bf+7, &p->from, sf);
  306. bp = zaddr(bp, &p->to, st);
  307. Bwrite(b, bf, bp-bf);
  308. }
  309. void
  310. outcode(void)
  311. {
  312. struct { Sym *sym; short type; } h[NSYM];
  313. Prog *p;
  314. Sym *s;
  315. int sf, st, t, sym;
  316. if(debug['S']) {
  317. for(p = firstp; p != P; p = p->link)
  318. if(p->as != ADATA && p->as != AGLOBL)
  319. pc--;
  320. for(p = firstp; p != P; p = p->link) {
  321. print("%P\n", p);
  322. if(p->as != ADATA && p->as != AGLOBL)
  323. pc++;
  324. }
  325. }
  326. outhist(&outbuf);
  327. for(sym=0; sym<NSYM; sym++) {
  328. h[sym].sym = S;
  329. h[sym].type = 0;
  330. }
  331. sym = 1;
  332. for(p = firstp; p != P; p = p->link) {
  333. jackpot:
  334. sf = 0;
  335. s = p->from.sym;
  336. while(s != S) {
  337. sf = s->sym;
  338. if(sf < 0 || sf >= NSYM)
  339. sf = 0;
  340. t = p->from.name;
  341. if(h[sf].type == t)
  342. if(h[sf].sym == s)
  343. break;
  344. s->sym = sym;
  345. zname(&outbuf, s, t);
  346. h[sym].sym = s;
  347. h[sym].type = t;
  348. sf = sym;
  349. sym++;
  350. if(sym >= NSYM)
  351. sym = 1;
  352. break;
  353. }
  354. st = 0;
  355. s = p->to.sym;
  356. while(s != S) {
  357. st = s->sym;
  358. if(st < 0 || st >= NSYM)
  359. st = 0;
  360. t = p->to.name;
  361. if(h[st].type == t)
  362. if(h[st].sym == s)
  363. break;
  364. s->sym = sym;
  365. zname(&outbuf, s, t);
  366. h[sym].sym = s;
  367. h[sym].type = t;
  368. st = sym;
  369. sym++;
  370. if(sym >= NSYM)
  371. sym = 1;
  372. if(st == sf)
  373. goto jackpot;
  374. break;
  375. }
  376. zwrite(&outbuf, p, sf, st);
  377. }
  378. firstp = P;
  379. lastp = P;
  380. }
  381. void
  382. outhist(Biobuf *b)
  383. {
  384. Hist *h;
  385. char *p, *q, *op, c;
  386. Prog pg;
  387. int n;
  388. pg = zprog;
  389. pg.as = AHISTORY;
  390. c = pathchar();
  391. for(h = hist; h != H; h = h->link) {
  392. p = h->name;
  393. op = 0;
  394. /* on windows skip drive specifier in pathname */
  395. if(systemtype(Windows) && p && p[1] == ':'){
  396. p += 2;
  397. c = *p;
  398. }
  399. if(p && p[0] != c && h->offset == 0 && pathname){
  400. /* on windows skip drive specifier in pathname */
  401. if(systemtype(Windows) && pathname[1] == ':') {
  402. op = p;
  403. p = pathname+2;
  404. c = *p;
  405. } else if(pathname[0] == c){
  406. op = p;
  407. p = pathname;
  408. }
  409. }
  410. while(p) {
  411. q = utfrune(p, c);
  412. if(q) {
  413. n = q-p;
  414. if(n == 0){
  415. n = 1; /* leading "/" */
  416. *p = '/'; /* don't emit "\" on windows */
  417. }
  418. q++;
  419. } else {
  420. n = strlen(p);
  421. q = 0;
  422. }
  423. if(n) {
  424. Bputc(b, ANAME);
  425. Bputc(b, D_FILE);
  426. Bputc(b, 1);
  427. Bputc(b, '<');
  428. Bwrite(b, p, n);
  429. Bputc(b, 0);
  430. }
  431. p = q;
  432. if(p == 0 && op) {
  433. p = op;
  434. op = 0;
  435. }
  436. }
  437. pg.lineno = h->line;
  438. pg.to.type = zprog.to.type;
  439. pg.to.offset = h->offset;
  440. if(h->offset)
  441. pg.to.type = D_CONST;
  442. zwrite(b, &pg, 0, 0);
  443. }
  444. }
  445. void
  446. zname(Biobuf *b, Sym *s, int t)
  447. {
  448. char *n, bf[7];
  449. ulong sig;
  450. n = s->name;
  451. if(debug['T'] && t == D_EXTERN && s->sig != SIGDONE && s->type != types[TENUM] && s != symrathole){
  452. sig = sign(s);
  453. bf[0] = ASIGNAME;
  454. bf[1] = sig;
  455. bf[2] = sig>>8;
  456. bf[3] = sig>>16;
  457. bf[4] = sig>>24;
  458. bf[5] = t;
  459. bf[6] = s->sym;
  460. Bwrite(b, bf, 7);
  461. s->sig = SIGDONE;
  462. }
  463. else{
  464. bf[0] = ANAME;
  465. bf[1] = t; /* type */
  466. bf[2] = s->sym; /* sym */
  467. Bwrite(b, bf, 3);
  468. }
  469. Bwrite(b, n, strlen(n)+1);
  470. }
  471. char*
  472. zaddr(char *bp, Adr *a, int s)
  473. {
  474. long l;
  475. Ieee e;
  476. bp[0] = a->type;
  477. bp[1] = a->reg;
  478. bp[2] = s;
  479. bp[3] = a->name;
  480. bp += 4;
  481. switch(a->type) {
  482. default:
  483. diag(Z, "unknown type %d in zaddr", a->type);
  484. case D_NONE:
  485. case D_REG:
  486. case D_FREG:
  487. case D_PSR:
  488. break;
  489. case D_OREG:
  490. case D_CONST:
  491. case D_BRANCH:
  492. case D_SHIFT:
  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. long
  523. align(long i, Type *t, int op)
  524. {
  525. long o;
  526. Type *v;
  527. int w;
  528. o = i;
  529. w = 1;
  530. switch(op) {
  531. default:
  532. diag(Z, "unknown align opcode %d", op);
  533. break;
  534. case Asu2: /* padding at end of a struct */
  535. w = SZ_LONG;
  536. if(packflg)
  537. w = packflg;
  538. break;
  539. case Ael1: /* initial allign of struct element */
  540. for(v=t; v->etype==TARRAY; v=v->link)
  541. ;
  542. w = ewidth[v->etype];
  543. if(w <= 0 || w >= SZ_LONG)
  544. w = SZ_LONG;
  545. if(packflg)
  546. w = packflg;
  547. break;
  548. case Ael2: /* width of a struct element */
  549. o += t->width;
  550. break;
  551. case Aarg0: /* initial passbyptr argument in arg list */
  552. if(typesuv[t->etype]) {
  553. o = align(o, types[TIND], Aarg1);
  554. o = align(o, types[TIND], Aarg2);
  555. }
  556. break;
  557. case Aarg1: /* initial allign of parameter */
  558. w = ewidth[t->etype];
  559. if(w <= 0 || w >= SZ_LONG) {
  560. w = SZ_LONG;
  561. break;
  562. }
  563. w = 1; /* little endian no adjustment */
  564. break;
  565. case Aarg2: /* width of a parameter */
  566. o += t->width;
  567. w = SZ_LONG;
  568. break;
  569. case Aaut3: /* total allign of automatic */
  570. o = align(o, t, Ael2);
  571. o = align(o, t, Ael1);
  572. w = SZ_LONG; /* because of a pun in cc/dcl.c:contig() */
  573. break;
  574. }
  575. o = round(o, w);
  576. if(debug['A'])
  577. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  578. return o;
  579. }
  580. long
  581. maxround(long max, long v)
  582. {
  583. v = round(v, SZ_LONG);
  584. if(v > max)
  585. return v;
  586. return max;
  587. }