swt.c 11 KB

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