swt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. int
  133. mulcon(Node *n, Node *nn)
  134. {
  135. Node *l, *r, nod1, nod2;
  136. Multab *m;
  137. long v;
  138. int o;
  139. char code[sizeof(m->code)+2], *p;
  140. if(typefd[n->type->etype])
  141. return 0;
  142. l = n->left;
  143. r = n->right;
  144. if(l->op == OCONST) {
  145. l = r;
  146. r = n->left;
  147. }
  148. if(r->op != OCONST)
  149. return 0;
  150. v = convvtox(r->vconst, n->type->etype);
  151. if(v != r->vconst) {
  152. if(debug['M'])
  153. print("%L multiply conv: %lld\n", n->lineno, r->vconst);
  154. return 0;
  155. }
  156. m = mulcon0(v);
  157. if(!m) {
  158. if(debug['M'])
  159. print("%L multiply table: %lld\n", n->lineno, r->vconst);
  160. return 0;
  161. }
  162. if(debug['M'] && debug['v'])
  163. print("%L multiply: %ld\n", n->lineno, v);
  164. memmove(code, m->code, sizeof(m->code));
  165. code[sizeof(m->code)] = 0;
  166. p = code;
  167. if(p[1] == 'i')
  168. p += 2;
  169. regalloc(&nod1, n, nn);
  170. cgen(l, &nod1);
  171. if(v < 0)
  172. gopcode(OSUB, &nod1, nodconst(0), &nod1);
  173. regalloc(&nod2, n, Z);
  174. loop:
  175. switch(*p) {
  176. case 0:
  177. regfree(&nod2);
  178. gopcode(OAS, &nod1, Z, nn);
  179. regfree(&nod1);
  180. return 1;
  181. case '+':
  182. o = OADD;
  183. goto addsub;
  184. case '-':
  185. o = OSUB;
  186. addsub: /* number is r,n,l */
  187. v = p[1] - '0';
  188. r = &nod1;
  189. if(v&4)
  190. r = &nod2;
  191. n = &nod1;
  192. if(v&2)
  193. n = &nod2;
  194. l = &nod1;
  195. if(v&1)
  196. l = &nod2;
  197. gopcode(o, l, n, r);
  198. break;
  199. default: /* op is shiftcount, number is r,l */
  200. v = p[1] - '0';
  201. r = &nod1;
  202. if(v&2)
  203. r = &nod2;
  204. l = &nod1;
  205. if(v&1)
  206. l = &nod2;
  207. v = *p - 'a';
  208. if(v < 0 || v >= 32) {
  209. diag(n, "mulcon unknown op: %c%c", p[0], p[1]);
  210. break;
  211. }
  212. gopcode(OASHL, nodconst(v), l, r);
  213. break;
  214. }
  215. p += 2;
  216. goto loop;
  217. }
  218. void
  219. sextern(Sym *s, Node *a, long o, long w)
  220. {
  221. long e, lw;
  222. for(e=0; e<w; e+=NSNAME) {
  223. lw = NSNAME;
  224. if(w-e < lw)
  225. lw = w-e;
  226. gpseudo(ADATA, s, nodconst(0));
  227. p->from.offset += o+e;
  228. p->reg = lw;
  229. p->to.type = D_SCONST;
  230. memmove(p->to.sval, a->cstring+e, lw);
  231. }
  232. }
  233. void
  234. gextern(Sym *s, Node *a, long o, long w)
  235. {
  236. if(a->op == OCONST && typev[a->type->etype]) {
  237. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  238. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  239. else
  240. gpseudo(ADATA, s, nod32const(a->vconst));
  241. p->from.offset += o;
  242. p->reg = 4;
  243. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  244. gpseudo(ADATA, s, nod32const(a->vconst));
  245. else
  246. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  247. p->from.offset += o + 4;
  248. p->reg = 4;
  249. return;
  250. }
  251. gpseudo(ADATA, s, a);
  252. p->from.offset += o;
  253. p->reg = w;
  254. if(p->to.type == D_OREG)
  255. p->to.type = D_CONST;
  256. }
  257. void zname(Biobuf*, Sym*, int);
  258. char* zaddr(char*, Adr*, int);
  259. void zwrite(Biobuf*, Prog*, int, int);
  260. void outhist(Biobuf*);
  261. void
  262. zwrite(Biobuf *b, Prog *p, int sf, int st)
  263. {
  264. char bf[100], *bp;
  265. bf[0] = p->as;
  266. bf[1] = p->reg;
  267. bf[2] = p->lineno;
  268. bf[3] = p->lineno>>8;
  269. bf[4] = p->lineno>>16;
  270. bf[5] = p->lineno>>24;
  271. bp = zaddr(bf+6, &p->from, sf);
  272. bp = zaddr(bp, &p->to, st);
  273. Bwrite(b, bf, bp-bf);
  274. }
  275. void
  276. outcode(void)
  277. {
  278. struct { Sym *sym; short type; } h[NSYM];
  279. Prog *p;
  280. Sym *s;
  281. int sf, st, t, sym;
  282. if(debug['S']) {
  283. for(p = firstp; p != P; p = p->link)
  284. if(p->as != ADATA && p->as != AGLOBL)
  285. pc--;
  286. for(p = firstp; p != P; p = p->link) {
  287. print("%P\n", p);
  288. if(p->as != ADATA && p->as != AGLOBL)
  289. pc++;
  290. }
  291. }
  292. outhist(&outbuf);
  293. for(sym=0; sym<NSYM; sym++) {
  294. h[sym].sym = S;
  295. h[sym].type = 0;
  296. }
  297. sym = 1;
  298. for(p = firstp; p != P; p = p->link) {
  299. jackpot:
  300. sf = 0;
  301. s = p->from.sym;
  302. while(s != S) {
  303. sf = s->sym;
  304. if(sf < 0 || sf >= NSYM)
  305. sf = 0;
  306. t = p->from.name;
  307. if(h[sf].type == t)
  308. if(h[sf].sym == s)
  309. break;
  310. s->sym = sym;
  311. zname(&outbuf, s, t);
  312. h[sym].sym = s;
  313. h[sym].type = t;
  314. sf = sym;
  315. sym++;
  316. if(sym >= NSYM)
  317. sym = 1;
  318. break;
  319. }
  320. st = 0;
  321. s = p->to.sym;
  322. while(s != S) {
  323. st = s->sym;
  324. if(st < 0 || st >= NSYM)
  325. st = 0;
  326. t = p->to.name;
  327. if(h[st].type == t)
  328. if(h[st].sym == s)
  329. break;
  330. s->sym = sym;
  331. zname(&outbuf, s, t);
  332. h[sym].sym = s;
  333. h[sym].type = t;
  334. st = sym;
  335. sym++;
  336. if(sym >= NSYM)
  337. sym = 1;
  338. if(st == sf)
  339. goto jackpot;
  340. break;
  341. }
  342. zwrite(&outbuf, p, sf, st);
  343. }
  344. firstp = P;
  345. lastp = P;
  346. }
  347. void
  348. outhist(Biobuf *b)
  349. {
  350. Hist *h;
  351. char *p, *q, *op, c;
  352. Prog pg;
  353. int n;
  354. pg = zprog;
  355. pg.as = AHISTORY;
  356. c = pathchar();
  357. for(h = hist; h != H; h = h->link) {
  358. p = h->name;
  359. op = 0;
  360. /* on windows skip drive specifier in pathname */
  361. if(systemtype(Windows) && p && p[1] == ':'){
  362. p += 2;
  363. c = *p;
  364. }
  365. if(p && p[0] != c && h->offset == 0 && pathname){
  366. /* on windows skip drive specifier in pathname */
  367. if(systemtype(Windows) && pathname[1] == ':') {
  368. op = p;
  369. p = pathname+2;
  370. c = *p;
  371. } else if(pathname[0] == c){
  372. op = p;
  373. p = pathname;
  374. }
  375. }
  376. while(p) {
  377. q = utfrune(p, c);
  378. if(q) {
  379. n = q-p;
  380. if(n == 0){
  381. n = 1; /* leading "/" */
  382. *p = '/'; /* don't emit "\" on windows */
  383. }
  384. q++;
  385. } else {
  386. n = strlen(p);
  387. q = 0;
  388. }
  389. if(n) {
  390. Bputc(b, ANAME);
  391. Bputc(b, D_FILE);
  392. Bputc(b, 1);
  393. Bputc(b, '<');
  394. Bwrite(b, p, n);
  395. Bputc(b, 0);
  396. }
  397. p = q;
  398. if(p == 0 && op) {
  399. p = op;
  400. op = 0;
  401. }
  402. }
  403. pg.lineno = h->line;
  404. pg.to.type = zprog.to.type;
  405. pg.to.offset = h->offset;
  406. if(h->offset)
  407. pg.to.type = D_CONST;
  408. zwrite(b, &pg, 0, 0);
  409. }
  410. }
  411. void
  412. zname(Biobuf *b, Sym *s, int t)
  413. {
  414. char *n, bf[7];
  415. ulong sig;
  416. n = s->name;
  417. if(debug['T'] && t == D_EXTERN && s->sig != SIGDONE && s->type != types[TENUM] && s != symrathole){
  418. sig = sign(s);
  419. bf[0] = ASIGNAME;
  420. bf[1] = sig;
  421. bf[2] = sig>>8;
  422. bf[3] = sig>>16;
  423. bf[4] = sig>>24;
  424. bf[5] = t;
  425. bf[6] = s->sym;
  426. Bwrite(b, bf, 7);
  427. s->sig = SIGDONE;
  428. }
  429. else{
  430. bf[0] = ANAME;
  431. bf[1] = t; /* type */
  432. bf[2] = s->sym; /* sym */
  433. Bwrite(b, bf, 3);
  434. }
  435. Bwrite(b, n, strlen(n)+1);
  436. }
  437. char*
  438. zaddr(char *bp, Adr *a, int s)
  439. {
  440. long l;
  441. Ieee e;
  442. bp[0] = a->type;
  443. bp[1] = a->reg;
  444. bp[2] = s;
  445. bp[3] = a->name;
  446. bp += 4;
  447. switch(a->type) {
  448. default:
  449. diag(Z, "unknown type %d in zaddr", a->type);
  450. case D_NONE:
  451. case D_REG:
  452. case D_FREG:
  453. case D_MREG:
  454. case D_FCREG:
  455. case D_LO:
  456. case D_HI:
  457. break;
  458. case D_OREG:
  459. case D_CONST:
  460. case D_BRANCH:
  461. l = a->offset;
  462. bp[0] = l;
  463. bp[1] = l>>8;
  464. bp[2] = l>>16;
  465. bp[3] = l>>24;
  466. bp += 4;
  467. break;
  468. case D_SCONST:
  469. memmove(bp, a->sval, NSNAME);
  470. bp += NSNAME;
  471. break;
  472. case D_FCONST:
  473. ieeedtod(&e, a->dval);
  474. l = e.l;
  475. bp[0] = l;
  476. bp[1] = l>>8;
  477. bp[2] = l>>16;
  478. bp[3] = l>>24;
  479. bp += 4;
  480. l = e.h;
  481. bp[0] = l;
  482. bp[1] = l>>8;
  483. bp[2] = l>>16;
  484. bp[3] = l>>24;
  485. bp += 4;
  486. break;
  487. }
  488. return bp;
  489. }
  490. long
  491. align(long i, Type *t, int op)
  492. {
  493. long o;
  494. Type *v;
  495. int w;
  496. o = i;
  497. w = 1;
  498. switch(op) {
  499. default:
  500. diag(Z, "unknown align opcode %d", op);
  501. break;
  502. case Asu2: /* padding at end of a struct */
  503. w = SZ_LONG;
  504. if(packflg)
  505. w = packflg;
  506. break;
  507. case Ael1: /* initial allign of struct element */
  508. for(v=t; v->etype==TARRAY; v=v->link)
  509. ;
  510. w = ewidth[v->etype];
  511. if(w <= 0 || w >= SZ_LONG)
  512. w = SZ_LONG;
  513. if(packflg)
  514. w = packflg;
  515. break;
  516. case Ael2: /* width of a struct element */
  517. o += t->width;
  518. break;
  519. case Aarg0: /* initial passbyptr argument in arg list */
  520. if(typesuv[t->etype]) {
  521. o = align(o, types[TIND], Aarg1);
  522. o = align(o, types[TIND], Aarg2);
  523. }
  524. break;
  525. case Aarg1: /* initial allign of parameter */
  526. w = ewidth[t->etype];
  527. if(w <= 0 || w >= SZ_LONG) {
  528. w = SZ_LONG;
  529. break;
  530. }
  531. if(thechar == 'v')
  532. o += SZ_LONG - w; /* big endian adjustment */
  533. w = 1;
  534. break;
  535. case Aarg2: /* width of a parameter */
  536. o += t->width;
  537. w = SZ_LONG;
  538. break;
  539. case Aaut3: /* total allign of automatic */
  540. o = align(o, t, Ael1);
  541. o = align(o, t, Ael2);
  542. break;
  543. }
  544. o = round(o, w);
  545. if(debug['A'])
  546. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  547. return o;
  548. }
  549. long
  550. maxround(long max, long v)
  551. {
  552. v += SZ_LONG-1;
  553. if(v > max)
  554. max = round(v, SZ_LONG);
  555. return max;
  556. }