swt.c 10 KB

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