swt.c 10 KB

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