swt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. #include "gc.h"
  2. int
  3. swcmp(const void *a1, const void *a2)
  4. {
  5. C1 *p1, *p2;
  6. p1 = (C1*)a1;
  7. p2 = (C1*)a2;
  8. if(p1->val < p2->val)
  9. return -1;
  10. return p1->val > p2->val;
  11. }
  12. void
  13. doswit(int g, Node *n)
  14. {
  15. Case *c;
  16. C1 *q, *iq;
  17. long def, nc, i;
  18. def = 0;
  19. nc = 0;
  20. for(c = cases; c->link != C; c = c->link) {
  21. if(c->def) {
  22. if(def)
  23. diag(n, "more than one default in switch");
  24. def = c->label;
  25. continue;
  26. }
  27. nc++;
  28. }
  29. iq = alloc(nc*sizeof(C1));
  30. q = iq;
  31. for(c = cases; c->link != C; c = c->link) {
  32. if(c->def)
  33. continue;
  34. q->label = c->label;
  35. q->val = c->val;
  36. q++;
  37. }
  38. qsort(iq, nc, sizeof(C1), swcmp);
  39. if(def == 0)
  40. def = breakpc;
  41. for(i=0; i<nc-1; i++)
  42. if(iq[i].val == iq[i+1].val)
  43. diag(n, "duplicate cases in switch %ld", iq[i].val);
  44. swit1(iq, nc, def, g, n);
  45. }
  46. #define N1 4 /* ncase: always linear */
  47. /* else binary */
  48. void
  49. swit1(C1 *q, int nc, long def, int g, Node *n)
  50. {
  51. C1 *r;
  52. int i;
  53. long v;
  54. Prog *sp1, *sp2;
  55. /* note that g and g+1 are not allocated */
  56. if(nc <= N1)
  57. goto linear;
  58. /*
  59. * divide and conquer
  60. */
  61. i = nc / 2;
  62. r = q+i;
  63. v = r->val;
  64. /* compare median */
  65. if(v >= -128 && v < 128) {
  66. gopcode(OAS, n->type, D_CONST, nodconst(v), g+1, n);
  67. gopcode(OEQ, n->type, g, n, g+1, n);
  68. } else
  69. gopcode(OEQ, n->type, g, n, D_CONST, nodconst(v));
  70. gbranch(OLT);
  71. sp1 = p;
  72. gbranch(OGT);
  73. sp2 = p;
  74. gbranch(OGOTO);
  75. patch(p, r->label);
  76. patch(sp1, pc);
  77. swit1(q, i, def, g, n);
  78. patch(sp2, pc);
  79. swit1(r+1, nc-i-1, def, g, n);
  80. return;
  81. linear:
  82. for(i=0; i<nc; i++) {
  83. v = q->val;
  84. if(v >= -128 && v < 128) {
  85. gopcode(OAS, n->type, D_CONST, nodconst(v), g+1, n);
  86. gopcode(OEQ, n->type, g+1, n, g, n);
  87. } else
  88. gopcode(OEQ, n->type, g, n, D_CONST, nodconst(v));
  89. gbranch(OEQ);
  90. patch(p, q->label);
  91. q++;
  92. }
  93. gbranch(OGOTO);
  94. patch(p, def);
  95. }
  96. void
  97. cas(void)
  98. {
  99. Case *c;
  100. c = alloc(sizeof(*c));
  101. c->link = cases;
  102. cases = c;
  103. }
  104. int
  105. bitload(Node *b, int n1, int n2, int n3, Node *nn)
  106. {
  107. int sh, g, gs;
  108. long v;
  109. Node *l;
  110. Type *t;
  111. /*
  112. * n1 gets adjusted/masked value
  113. * n2 gets address of cell
  114. * n3 gets contents of cell
  115. */
  116. gs = 0;
  117. t = tfield;
  118. l = b->left;
  119. g = regalloc(t, n3);
  120. if(n2 != D_NONE) {
  121. lcgen(l, n2, Z);
  122. n2 |= I_INDIR;
  123. gmove(t, t, n2, l, g, l);
  124. gmove(t, t, g, l, n1, l);
  125. } else
  126. cgen(l, g, nn);
  127. if(b->type->shift == 0 && typeu[b->type->etype]) {
  128. v = ~0 + (1L << b->type->nbits);
  129. gopcode(OAND, t, D_CONST, nodconst(v), g, l);
  130. } else {
  131. sh = 32 - b->type->shift - b->type->nbits;
  132. if(sh > 0)
  133. if(sh >= 8) {
  134. gs = regalloc(t, D_NONE);
  135. gmove(t, t, D_CONST, nodconst(sh), gs, l);
  136. gopcode(OASHL, t, gs, l, g, l);
  137. if(b->type->shift)
  138. regfree(gs);
  139. } else
  140. gopcode(OASHL, t, D_CONST, nodconst(sh), g, l);
  141. sh += b->type->shift;
  142. if(sh > 0) {
  143. if(sh >= 8) {
  144. if(b->type->shift) {
  145. gs = regalloc(t, D_NONE);
  146. gmove(t, t, D_CONST, nodconst(sh), gs, l);
  147. }
  148. if(typeu[b->type->etype])
  149. gopcode(OLSHR, t, gs, l, g, l);
  150. else
  151. gopcode(OASHR, t, gs, l, g, l);
  152. regfree(gs);
  153. } else {
  154. if(typeu[b->type->etype])
  155. gopcode(OLSHR, t, D_CONST, nodconst(sh), g, l);
  156. else
  157. gopcode(OASHR, t, D_CONST, nodconst(sh), g, l);
  158. }
  159. }
  160. }
  161. return g;
  162. }
  163. void
  164. bitstore(Node *b, int n1, int n2, int n3, int result, Node *nn)
  165. {
  166. long v;
  167. Node *l;
  168. Type *t;
  169. int sh, g, gs;
  170. /*
  171. * n1 has adjusted/masked value
  172. * n2 has address of cell
  173. * n3 has contents of cell
  174. */
  175. t = tfield;
  176. l = b->left;
  177. g = regalloc(t, D_NONE);
  178. v = ~0 + (1L << b->type->nbits);
  179. gopcode(OAND, t, D_CONST, nodconst(v), n1, l);
  180. gmove(t, t, n1, l, g, l);
  181. if(result != D_NONE)
  182. gmove(t, nn->type, n1, l, result, nn);
  183. sh = b->type->shift;
  184. if(sh > 0) {
  185. if(sh >= 8) {
  186. gs = regalloc(t, D_NONE);
  187. gmove(t, t, D_CONST, nodconst(sh), gs, l);
  188. gopcode(OASHL, t, gs, l, g, l);
  189. regfree(gs);
  190. } else
  191. gopcode(OASHL, t, D_CONST, nodconst(sh), g, l);
  192. }
  193. v <<= sh;
  194. gopcode(OAND, t, D_CONST, nodconst(~v), n3, l);
  195. gopcode(OOR, t, n3, l, g, l);
  196. gmove(t, t, g, l, n2|I_INDIR, l);
  197. regfree(g);
  198. regfree(n1);
  199. regfree(n2);
  200. regfree(n3);
  201. }
  202. long
  203. outstring(char *s, long n)
  204. {
  205. long r;
  206. r = nstring;
  207. while(n) {
  208. string[mnstring] = *s++;
  209. mnstring++;
  210. nstring++;
  211. if(mnstring >= NSNAME) {
  212. gpseudo(ADATA, symstring, D_SCONST, 0L);
  213. memmove(p->to.sval, string, NSNAME);
  214. p->from.offset = nstring - NSNAME;
  215. p->from.displace = NSNAME;
  216. mnstring = 0;
  217. }
  218. n--;
  219. }
  220. return r;
  221. }
  222. long
  223. outlstring(ushort *s, long n)
  224. {
  225. char buf[2];
  226. int c;
  227. long r;
  228. while(nstring & 1)
  229. outstring("", 1);
  230. r = nstring;
  231. while(n > 0) {
  232. c = *s++;
  233. if(align(0, types[TCHAR], Aarg1)) {
  234. buf[0] = c>>8;
  235. buf[1] = c;
  236. } else {
  237. buf[0] = c;
  238. buf[1] = c>>8;
  239. }
  240. outstring(buf, 2);
  241. n -= sizeof(ushort);
  242. }
  243. return r;
  244. }
  245. int
  246. doinc(Node *n, int f)
  247. {
  248. Node *l;
  249. int a;
  250. loop:
  251. if(n == Z)
  252. return 0;
  253. l = n->left;
  254. switch(n->op) {
  255. case OPOSTINC:
  256. case OPOSTDEC:
  257. if(f & POST) {
  258. a = n->addable;
  259. if(a >= INDEXED) {
  260. if(f & TEST)
  261. return 1;
  262. n->addable = 0;
  263. cgen(n, D_NONE, n);
  264. n->addable = a;
  265. }
  266. }
  267. break;
  268. case OAS:
  269. case OASLMUL:
  270. case OASLDIV:
  271. case OASLMOD:
  272. case OASMUL:
  273. case OASDIV:
  274. case OASMOD:
  275. case OASXOR:
  276. case OASOR:
  277. case OASADD:
  278. case OASSUB:
  279. case OASLSHR:
  280. case OASASHR:
  281. case OASASHL:
  282. case OASAND:
  283. case OPREINC:
  284. case OPREDEC:
  285. if(f & PRE) {
  286. a = n->addable;
  287. if(a >= INDEXED) {
  288. if(f & TEST)
  289. return 1;
  290. n->addable = 0;
  291. doinc(n, PRE);
  292. cgen(n, D_NONE, n);
  293. n->addable = a;
  294. return 0;
  295. }
  296. }
  297. break;
  298. case OFUNC:
  299. if(f & PRE)
  300. break;
  301. return 0;
  302. case ONAME:
  303. case OREGISTER:
  304. case OSTRING:
  305. case OCONST:
  306. case OANDAND:
  307. case OOROR:
  308. return 0;
  309. case OCOND:
  310. return 0;
  311. case OCOMMA:
  312. n = n->right;
  313. if(f & PRE)
  314. n = l;
  315. goto loop;
  316. }
  317. if(l != Z)
  318. if(doinc(l, f))
  319. return 1;
  320. n = n->right;
  321. goto loop;
  322. }
  323. void
  324. setsp(void)
  325. {
  326. nextpc();
  327. p->as = AADJSP;
  328. p->from.type = D_CONST;
  329. p->from.offset = 0;
  330. }
  331. void
  332. adjsp(long o)
  333. {
  334. if(o != 0) {
  335. nextpc();
  336. p->as = AADJSP;
  337. p->from.type = D_CONST;
  338. p->from.offset = o;
  339. argoff += o;
  340. }
  341. }
  342. int
  343. simplv(Node *n)
  344. {
  345. if(n->addable <= INDEXED)
  346. return 0;
  347. while(n->op == OIND)
  348. n = n->left;
  349. if(n->op == ONAME)
  350. return 1;
  351. return 0;
  352. }
  353. int
  354. eval(Node *n, int g)
  355. {
  356. if(n->addable >= INDEXED)
  357. return D_TREE;
  358. g = regalloc(n->type, g);
  359. cgen(n, g, n);
  360. return g;
  361. }
  362. void outhist(Biobuf*);
  363. void zname(Biobuf*, char*, int, int);
  364. void zaddr(Biobuf*, Adr*, int);
  365. void zwrite(Biobuf*, Prog*, int, int);
  366. void
  367. outcode(void)
  368. {
  369. struct { Sym *sym; short type; } h[NSYM];
  370. Prog *p;
  371. Sym *s;
  372. int f, sf, st, t, sym;
  373. Biobuf b;
  374. if(debug['S']) {
  375. for(p = firstp; p != P; p = p->link)
  376. if(p->as != ADATA && p->as != AGLOBL)
  377. pc--;
  378. for(p = firstp; p != P; p = p->link) {
  379. print("%P\n", p);
  380. if(p->as != ADATA && p->as != AGLOBL)
  381. pc++;
  382. }
  383. }
  384. f = open(outfile, OWRITE);
  385. if(f < 0) {
  386. diag(Z, "cant open %s", outfile);
  387. errorexit();
  388. }
  389. Binit(&b, f, OWRITE);
  390. Bseek(&b, 0L, 2);
  391. outhist(&b);
  392. for(sym=0; sym<NSYM; sym++) {
  393. h[sym].sym = S;
  394. h[sym].type = 0;
  395. }
  396. sym = 1;
  397. for(p = firstp; p != P; p = p->link) {
  398. jackpot:
  399. sf = 0;
  400. s = p->from.sym;
  401. while(s != S) {
  402. sf = s->sym;
  403. if(sf < 0 || sf >= NSYM)
  404. sf = 0;
  405. t = p->from.type & D_MASK;
  406. if(h[sf].type == t)
  407. if(h[sf].sym == s)
  408. break;
  409. zname(&b, s->name, t, sym);
  410. s->sym = sym;
  411. h[sym].sym = s;
  412. h[sym].type = t;
  413. sf = sym;
  414. sym++;
  415. if(sym >= NSYM)
  416. sym = 1;
  417. break;
  418. }
  419. st = 0;
  420. s = p->to.sym;
  421. while(s != S) {
  422. st = s->sym;
  423. if(st < 0 || st >= NSYM)
  424. st = 0;
  425. t = p->to.type & D_MASK;
  426. if(h[st].type == t)
  427. if(h[st].sym == s)
  428. break;
  429. zname(&b, s->name, t, sym);
  430. s->sym = sym;
  431. h[sym].sym = s;
  432. h[sym].type = t;
  433. st = sym;
  434. sym++;
  435. if(sym >= NSYM)
  436. sym = 1;
  437. if(st == sf)
  438. goto jackpot;
  439. break;
  440. }
  441. zwrite(&b, p, sf, st);
  442. }
  443. Bflush(&b);
  444. close(f);
  445. firstp = P;
  446. lastp = P;
  447. }
  448. void
  449. zwrite(Biobuf *b, Prog *p, int sf, int st)
  450. {
  451. long l;
  452. l = p->as;
  453. Bputc(b, l);
  454. Bputc(b, l>>8);
  455. l = p->lineno;
  456. Bputc(b, l);
  457. Bputc(b, l>>8);
  458. Bputc(b, l>>16);
  459. Bputc(b, l>>24);
  460. zaddr(b, &p->from, sf);
  461. zaddr(b, &p->to, st);
  462. }
  463. void
  464. zname(Biobuf *b, char *n, int t, int s)
  465. {
  466. Bputc(b, ANAME); /* as */
  467. Bputc(b, ANAME>>8);
  468. Bputc(b, t); /* type */
  469. Bputc(b, s); /* sym */
  470. while(*n) {
  471. Bputc(b, *n);
  472. n++;
  473. }
  474. Bputc(b, 0);
  475. }
  476. void
  477. zaddr(Biobuf *b, Adr *a, int s)
  478. {
  479. long l;
  480. int i, t;
  481. char *n;
  482. Ieee e;
  483. t = 0;
  484. if(a->field)
  485. t |= T_FIELD;
  486. if(s)
  487. t |= T_SYM;
  488. switch(a->type) {
  489. default:
  490. if(a->offset)
  491. t |= T_OFFSET;
  492. if(a->displace)
  493. t |= T_INDEX;
  494. if(a->type & ~0xff)
  495. t |= T_TYPE;
  496. break;
  497. case D_FCONST:
  498. t |= T_FCONST;
  499. break;
  500. case D_SCONST:
  501. t |= T_SCONST;
  502. break;
  503. }
  504. Bputc(b, t);
  505. if(t & T_FIELD) { /* implies field */
  506. i = a->field;
  507. Bputc(b, i);
  508. Bputc(b, i>>8);
  509. }
  510. if(t & T_INDEX) { /* implies index, scale, displace */
  511. i = D_NONE;
  512. Bputc(b, i);
  513. Bputc(b, i>>8);
  514. Bputc(b, 0);
  515. l = a->displace;
  516. Bputc(b, l);
  517. Bputc(b, l>>8);
  518. Bputc(b, l>>16);
  519. Bputc(b, l>>24);
  520. }
  521. if(t & T_OFFSET) { /* implies offset */
  522. l = a->offset;
  523. Bputc(b, l);
  524. Bputc(b, l>>8);
  525. Bputc(b, l>>16);
  526. Bputc(b, l>>24);
  527. }
  528. if(t & T_SYM) /* implies sym */
  529. Bputc(b, s);
  530. if(t & T_FCONST) {
  531. ieeedtod(&e, a->dval);
  532. l = e.l;
  533. Bputc(b, l);
  534. Bputc(b, l>>8);
  535. Bputc(b, l>>16);
  536. Bputc(b, l>>24);
  537. l = e.h;
  538. Bputc(b, l);
  539. Bputc(b, l>>8);
  540. Bputc(b, l>>16);
  541. Bputc(b, l>>24);
  542. return;
  543. }
  544. if(t & T_SCONST) {
  545. n = a->sval;
  546. for(i=0; i<NSNAME; i++) {
  547. Bputc(b, *n);
  548. n++;
  549. }
  550. return;
  551. }
  552. i = a->type;
  553. Bputc(b, i);
  554. if(t & T_TYPE)
  555. Bputc(b, i>>8);
  556. }
  557. void
  558. outhist(Biobuf *b)
  559. {
  560. Hist *h;
  561. char *p, *q, *op, c;
  562. Prog pg;
  563. int n;
  564. pg = zprog;
  565. pg.as = AHISTORY;
  566. c = pathchar();
  567. for(h = hist; h != H; h = h->link) {
  568. p = h->name;
  569. op = 0;
  570. if(p && p[0] != c && h->offset == 0 && pathname){
  571. /* on windows skip drive specifier in pathname */
  572. if(systemtype(Windows) && pathname[2] == c) {
  573. op = p;
  574. p = pathname+2;
  575. *p = '/';
  576. } else if(pathname[0] == c){
  577. op = p;
  578. p = pathname;
  579. }
  580. }
  581. while(p) {
  582. q = utfrune(p, c);
  583. if(q) {
  584. n = q-p;
  585. if(n == 0)
  586. n = 1; /* leading "/" */
  587. q++;
  588. } else {
  589. n = strlen(p);
  590. q = 0;
  591. }
  592. if(n) {
  593. Bputc(b, ANAME);
  594. Bputc(b, ANAME>>8);
  595. Bputc(b, D_FILE);
  596. Bputc(b, 1);
  597. Bputc(b, '<');
  598. Bwrite(b, p, n);
  599. Bputc(b, 0);
  600. }
  601. p = q;
  602. if(p == 0 && op) {
  603. p = op;
  604. op = 0;
  605. }
  606. }
  607. pg.lineno = h->line;
  608. pg.to.type = zprog.to.type;
  609. pg.to.offset = h->offset;
  610. if(h->offset)
  611. pg.to.type = D_CONST;
  612. zwrite(b, &pg, 0, 0);
  613. }
  614. }
  615. void
  616. ieeedtod(Ieee *ieee, double native)
  617. {
  618. double fr, ho, f;
  619. int exp;
  620. if(native < 0) {
  621. ieeedtod(ieee, -native);
  622. ieee->h |= 0x80000000L;
  623. return;
  624. }
  625. if(native == 0) {
  626. ieee->l = 0;
  627. ieee->h = 0;
  628. return;
  629. }
  630. fr = frexp(native, &exp);
  631. f = 2097152L; /* shouldnt use fp constants here */
  632. fr = modf(fr*f, &ho);
  633. ieee->h = ho;
  634. ieee->h &= 0xfffffL;
  635. ieee->h |= (exp+1022L) << 20;
  636. f = 65536L;
  637. fr = modf(fr*f, &ho);
  638. ieee->l = ho;
  639. ieee->l <<= 16;
  640. ieee->l |= (long)(fr*f);
  641. }
  642. int
  643. nodalloc(Type *t, int g, Node *n)
  644. {
  645. n->type = t;
  646. n->op = OREGISTER;
  647. n->addable = 12;
  648. n->complex = 0;
  649. g = regaddr(g);
  650. n->reg = g | I_INDIR;
  651. n->xoffset = 0;
  652. return g;
  653. }
  654. int
  655. mulcon(Node *n, Node *c, int result, Node *nn)
  656. {
  657. long v;
  658. if(typefd[n->type->etype])
  659. return 0;
  660. v = c->vconst;
  661. if(mulcon1(n, v, result, nn))
  662. return 1;
  663. return 0;
  664. }
  665. int
  666. shlcon(Node *n, Node *c, int result, Node *nn)
  667. {
  668. long v;
  669. v = 1L << c->vconst;
  670. return mulcon1(n, v, result, nn);
  671. }
  672. int
  673. mulcon1(Node *n, long v, int result, Node *nn)
  674. {
  675. int g, g1, a1, a2, neg;
  676. int o;
  677. char code[10], *p;
  678. if(result == D_NONE)
  679. return 0;
  680. neg = 0;
  681. if(v < 0) {
  682. v = -v;
  683. neg++;
  684. }
  685. a1 = 0;
  686. a2 = multabsize;
  687. for(;;) {
  688. if(a1 >= a2)
  689. return 0;
  690. g1 = (a2 + a1)/2;
  691. if(v < multab[g1].val) {
  692. a2 = g1;
  693. continue;
  694. }
  695. if(v > multab[g1].val) {
  696. a1 = g1+1;
  697. continue;
  698. }
  699. break;
  700. }
  701. strcpy(code, "0");
  702. strncat(code, multab[g1].code, sizeof(multab[0].code));
  703. p = code;
  704. if(p[1] == 'i')
  705. p += 2;
  706. g = regalloc(n->type, result);
  707. cgen(n, g, n);
  708. if(neg)
  709. gopcode(ONEG, n->type, D_NONE, n, g, n);
  710. g1 = regalloc(n->type, D_NONE);
  711. loop:
  712. switch(*p) {
  713. case 0:
  714. regfree(g1);
  715. gmove(n->type, nn->type, g, n, result, nn);
  716. regfree(g);
  717. return 1;
  718. case '0':
  719. o = OAS;
  720. *p -= '0';
  721. goto com;
  722. case '1':
  723. case '2':
  724. o = OSUB;
  725. *p -= '1';
  726. goto com;
  727. case '3':
  728. case '4':
  729. case '5':
  730. case '6':
  731. o = OADD;
  732. *p -= '3';
  733. com:
  734. a1 = g;
  735. if(*p == 1 || *p == 3)
  736. a1 = g1;
  737. a2 = g;
  738. if(*p == 0 || *p == 3)
  739. a2 = g1;
  740. gopcode(o, n->type, a1, n, a2, n);
  741. p++;
  742. break;
  743. default:
  744. a1 = *p++ - 'a' + 1;
  745. a2 = g;
  746. if(a1 > 8) {
  747. a2 = g1;
  748. a1 -= 8;
  749. }
  750. gopcode(OASHL, n->type, D_CONST, nodconst(a1), a2, n);
  751. break;
  752. }
  753. goto loop;
  754. }
  755. void
  756. nullwarn(Node *l, Node *r)
  757. {
  758. warn(Z, "result of operation not used");
  759. if(l != Z)
  760. cgen(l, D_NONE, Z);
  761. if(r != Z)
  762. cgen(r, D_NONE, Z);
  763. }
  764. void
  765. sextern(Sym *s, Node *a, long o, long w)
  766. {
  767. long e, lw;
  768. for(e=0; e<w; e+=NSNAME) {
  769. lw = NSNAME;
  770. if(w-e < lw)
  771. lw = w-e;
  772. gpseudo(ADATA, s, D_SCONST, 0L);
  773. p->from.offset += o+e;
  774. p->from.displace = lw;
  775. memmove(p->to.sval, a->cstring+e, lw);
  776. }
  777. }
  778. void
  779. gextern(Sym *s, Node *a, long o, long w)
  780. {
  781. if(a->op == OCONST && typev[a->type->etype]) {
  782. gpseudo(ADATA, s, D_CONST, (long)(a->vconst>>32));
  783. p->from.offset += o;
  784. p->from.displace = 4;
  785. gpseudo(ADATA, s, D_CONST, (long)(a->vconst));
  786. p->from.offset += o + 4;
  787. p->from.displace = 4;
  788. return;
  789. }
  790. gpseudo(ADATA, s, D_TREE, (long)a);
  791. p->from.offset += o;
  792. p->from.displace = w;
  793. }
  794. long
  795. align(long i, Type *t, int op)
  796. {
  797. long o;
  798. Type *v;
  799. int w;
  800. o = i;
  801. w = 1;
  802. switch(op) {
  803. default:
  804. diag(Z, "unknown align opcode %d", op);
  805. break;
  806. case Asu2: /* padding at end of a struct */
  807. w = SZ_LONG;
  808. break;
  809. case Ael1: /* initial allign of struct element */
  810. for(v=t; v->etype==TARRAY; v=v->link)
  811. ;
  812. w = ewidth[v->etype];
  813. if(w <= 0 || w >= SZ_SHORT)
  814. w = SZ_SHORT;
  815. break;
  816. case Ael2: /* width of a struct element */
  817. o += t->width;
  818. break;
  819. case Aarg0: /* initial passbyptr argument in arg list */
  820. if(typesuv[t->etype]) {
  821. o = align(o, types[TIND], Aarg1);
  822. o = align(o, types[TIND], Aarg2);
  823. }
  824. break;
  825. case Aarg1: /* initial allign of parameter */
  826. w = ewidth[t->etype];
  827. if(w <= 0 || w >= SZ_LONG) {
  828. w = SZ_LONG;
  829. break;
  830. }
  831. o += SZ_LONG - w; /* big endian adjustment */
  832. w = 1;
  833. break;
  834. case Aarg2: /* width of a parameter */
  835. o += t->width;
  836. w = SZ_LONG;
  837. break;
  838. case Aaut3: /* total allign of automatic */
  839. o = align(o, t, Ael1);
  840. o = align(o, t, Ael2);
  841. break;
  842. }
  843. o = round(o, w);
  844. if(debug['A'])
  845. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  846. return o;
  847. }
  848. long
  849. maxround(long max, long v)
  850. {
  851. v += SZ_LONG-1;
  852. if(v > max)
  853. max = round(v, SZ_LONG);
  854. return max;
  855. }