swt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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['K'])
  19. print("case = %.8llux\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['K'])
  32. print("case > %.8llux\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['K'])
  40. print("case < %.8llux\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. gextern(Sym *s, Node *a, long o, long w)
  220. {
  221. if(a->op == OCONST && typev[a->type->etype]) {
  222. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  223. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  224. else
  225. gpseudo(ADATA, s, nod32const(a->vconst));
  226. p->from.offset += o;
  227. p->reg = 4;
  228. if(align(0, types[TCHAR], Aarg1)) /* isbigendian */
  229. gpseudo(ADATA, s, nod32const(a->vconst));
  230. else
  231. gpseudo(ADATA, s, nod32const(a->vconst>>32));
  232. p->from.offset += o + 4;
  233. p->reg = 4;
  234. return;
  235. }
  236. gpseudo(ADATA, s, a);
  237. p->from.offset += o;
  238. p->reg = w;
  239. if(p->to.type == D_OREG)
  240. p->to.type = D_CONST;
  241. }
  242. void zname(Biobuf*, Sym*, int);
  243. char* zaddr(char*, Adr*, int);
  244. void zwrite(Biobuf*, Prog*, int, int);
  245. void outhist(Biobuf*);
  246. void
  247. zwrite(Biobuf *b, Prog *p, int sf, int st)
  248. {
  249. char bf[100], *bp;
  250. bf[0] = p->as;
  251. bf[1] = p->reg;
  252. bf[2] = p->lineno;
  253. bf[3] = p->lineno>>8;
  254. bf[4] = p->lineno>>16;
  255. bf[5] = p->lineno>>24;
  256. bp = zaddr(bf+6, &p->from, sf);
  257. bp = zaddr(bp, &p->to, st);
  258. Bwrite(b, bf, bp-bf);
  259. }
  260. void
  261. outcode(void)
  262. {
  263. struct { Sym *sym; short type; } h[NSYM];
  264. Prog *p;
  265. Sym *s;
  266. int sf, st, t, sym;
  267. if(debug['S']) {
  268. for(p = firstp; p != P; p = p->link)
  269. if(p->as != ADATA && p->as != AGLOBL)
  270. pc--;
  271. for(p = firstp; p != P; p = p->link) {
  272. print("%P\n", p);
  273. if(p->as != ADATA && p->as != AGLOBL)
  274. pc++;
  275. }
  276. }
  277. outhist(&outbuf);
  278. for(sym=0; sym<NSYM; sym++) {
  279. h[sym].sym = S;
  280. h[sym].type = 0;
  281. }
  282. sym = 1;
  283. for(p = firstp; p != P; p = p->link) {
  284. jackpot:
  285. sf = 0;
  286. s = p->from.sym;
  287. while(s != S) {
  288. sf = s->sym;
  289. if(sf < 0 || sf >= NSYM)
  290. sf = 0;
  291. t = p->from.name;
  292. if(h[sf].type == t)
  293. if(h[sf].sym == s)
  294. break;
  295. s->sym = sym;
  296. zname(&outbuf, s, t);
  297. h[sym].sym = s;
  298. h[sym].type = t;
  299. sf = sym;
  300. sym++;
  301. if(sym >= NSYM)
  302. sym = 1;
  303. break;
  304. }
  305. st = 0;
  306. s = p->to.sym;
  307. while(s != S) {
  308. st = s->sym;
  309. if(st < 0 || st >= NSYM)
  310. st = 0;
  311. t = p->to.name;
  312. if(h[st].type == t)
  313. if(h[st].sym == s)
  314. break;
  315. s->sym = sym;
  316. zname(&outbuf, s, t);
  317. h[sym].sym = s;
  318. h[sym].type = t;
  319. st = sym;
  320. sym++;
  321. if(sym >= NSYM)
  322. sym = 1;
  323. if(st == sf)
  324. goto jackpot;
  325. break;
  326. }
  327. zwrite(&outbuf, p, sf, st);
  328. }
  329. firstp = P;
  330. lastp = P;
  331. }
  332. void
  333. outhist(Biobuf *b)
  334. {
  335. Hist *h;
  336. char *p, *q, *op, c;
  337. Prog pg;
  338. int n;
  339. pg = zprog;
  340. pg.as = AHISTORY;
  341. c = pathchar();
  342. for(h = hist; h != H; h = h->link) {
  343. p = h->name;
  344. op = 0;
  345. /* on windows skip drive specifier in pathname */
  346. if(systemtype(Windows) && p && p[1] == ':'){
  347. p += 2;
  348. c = *p;
  349. }
  350. if(p && p[0] != c && h->offset == 0 && pathname){
  351. /* on windows skip drive specifier in pathname */
  352. if(systemtype(Windows) && pathname[1] == ':') {
  353. op = p;
  354. p = pathname+2;
  355. c = *p;
  356. } else if(pathname[0] == c){
  357. op = p;
  358. p = pathname;
  359. }
  360. }
  361. while(p) {
  362. q = utfrune(p, c);
  363. if(q) {
  364. n = q-p;
  365. if(n == 0){
  366. n = 1; /* leading "/" */
  367. *p = '/'; /* don't emit "\" on windows */
  368. }
  369. q++;
  370. } else {
  371. n = strlen(p);
  372. q = 0;
  373. }
  374. if(n) {
  375. Bputc(b, ANAME);
  376. Bputc(b, D_FILE);
  377. Bputc(b, 1);
  378. Bputc(b, '<');
  379. Bwrite(b, p, n);
  380. Bputc(b, 0);
  381. }
  382. p = q;
  383. if(p == 0 && op) {
  384. p = op;
  385. op = 0;
  386. }
  387. }
  388. pg.lineno = h->line;
  389. pg.to.type = zprog.to.type;
  390. pg.to.offset = h->offset;
  391. if(h->offset)
  392. pg.to.type = D_CONST;
  393. zwrite(b, &pg, 0, 0);
  394. }
  395. }
  396. void
  397. zname(Biobuf *b, Sym *s, int t)
  398. {
  399. char *n, bf[7];
  400. ulong sig;
  401. n = s->name;
  402. if(debug['T'] && t == D_EXTERN && s->sig != SIGDONE && s->type != types[TENUM] && s != symrathole){
  403. sig = sign(s);
  404. bf[0] = ASIGNAME;
  405. bf[1] = sig;
  406. bf[2] = sig>>8;
  407. bf[3] = sig>>16;
  408. bf[4] = sig>>24;
  409. bf[5] = t;
  410. bf[6] = s->sym;
  411. Bwrite(b, bf, 7);
  412. s->sig = SIGDONE;
  413. }
  414. else{
  415. bf[0] = ANAME;
  416. bf[1] = t; /* type */
  417. bf[2] = s->sym; /* sym */
  418. Bwrite(b, bf, 3);
  419. }
  420. Bwrite(b, n, strlen(n)+1);
  421. }
  422. char*
  423. zaddr(char *bp, Adr *a, int s)
  424. {
  425. long l;
  426. Ieee e;
  427. bp[0] = a->type;
  428. bp[1] = a->reg;
  429. bp[2] = s;
  430. bp[3] = a->name;
  431. bp += 4;
  432. switch(a->type) {
  433. default:
  434. diag(Z, "unknown type %d in zaddr", a->type);
  435. case D_NONE:
  436. case D_REG:
  437. case D_FREG:
  438. case D_MREG:
  439. case D_FCREG:
  440. case D_LO:
  441. case D_HI:
  442. break;
  443. case D_OREG:
  444. case D_CONST:
  445. case D_BRANCH:
  446. l = a->offset;
  447. bp[0] = l;
  448. bp[1] = l>>8;
  449. bp[2] = l>>16;
  450. bp[3] = l>>24;
  451. bp += 4;
  452. break;
  453. case D_SCONST:
  454. memmove(bp, a->sval, NSNAME);
  455. bp += NSNAME;
  456. break;
  457. case D_FCONST:
  458. ieeedtod(&e, a->dval);
  459. l = e.l;
  460. bp[0] = l;
  461. bp[1] = l>>8;
  462. bp[2] = l>>16;
  463. bp[3] = l>>24;
  464. bp += 4;
  465. l = e.h;
  466. bp[0] = l;
  467. bp[1] = l>>8;
  468. bp[2] = l>>16;
  469. bp[3] = l>>24;
  470. bp += 4;
  471. break;
  472. }
  473. return bp;
  474. }
  475. long
  476. align(long i, Type *t, int op)
  477. {
  478. long o;
  479. Type *v;
  480. int w;
  481. o = i;
  482. w = 1;
  483. switch(op) {
  484. default:
  485. diag(Z, "unknown align opcode %d", op);
  486. break;
  487. case Asu2: /* padding at end of a struct */
  488. w = SZ_LONG;
  489. if(packflg)
  490. w = packflg;
  491. break;
  492. case Ael1: /* initial allign of struct element */
  493. for(v=t; v->etype==TARRAY; v=v->link)
  494. ;
  495. w = ewidth[v->etype];
  496. if(w <= 0 || w >= SZ_LONG)
  497. w = SZ_LONG;
  498. if(packflg)
  499. w = packflg;
  500. break;
  501. case Ael2: /* width of a struct element */
  502. o += t->width;
  503. break;
  504. case Aarg0: /* initial passbyptr argument in arg list */
  505. if(typesuv[t->etype]) {
  506. o = align(o, types[TIND], Aarg1);
  507. o = align(o, types[TIND], Aarg2);
  508. }
  509. break;
  510. case Aarg1: /* initial allign of parameter */
  511. w = ewidth[t->etype];
  512. if(w <= 0 || w >= SZ_LONG) {
  513. w = SZ_LONG;
  514. break;
  515. }
  516. if(thechar == 'v')
  517. o += SZ_LONG - w; /* big endian adjustment */
  518. w = 1;
  519. break;
  520. case Aarg2: /* width of a parameter */
  521. o += t->width;
  522. w = SZ_LONG;
  523. break;
  524. case Aaut3: /* total allign of automatic */
  525. o = align(o, t, Ael1);
  526. o = align(o, t, Ael2);
  527. break;
  528. }
  529. o = round(o, w);
  530. if(debug['A'])
  531. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  532. return o;
  533. }
  534. long
  535. maxround(long max, long v)
  536. {
  537. v = round(v, SZ_LONG);
  538. if(v > max)
  539. return v;
  540. return max;
  541. }