swt.c 10 KB

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