swt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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*, Sym*, 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. s->sym = sym;
  410. zname(&b, s, t);
  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. s->sym = sym;
  430. zname(&b, s, t);
  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, Sym *s, int t)
  465. {
  466. char *n;
  467. ulong sig;
  468. if(debug['T'] && t == D_EXTERN && s->sig != SIGDONE && s->type != types[TENUM] && s != symrathole){
  469. sig = sign(s);
  470. Bputc(b, ASIGNAME);
  471. Bputc(b, ASIGNAME>>8);
  472. Bputc(b, sig);
  473. Bputc(b, sig>>8);
  474. Bputc(b, sig>>16);
  475. Bputc(b, sig>>24);
  476. s->sig = SIGDONE;
  477. }
  478. else{
  479. Bputc(b, ANAME); /* as */
  480. Bputc(b, ANAME>>8); /* as */
  481. }
  482. Bputc(b, t); /* type */
  483. Bputc(b, s->sym); /* sym */
  484. n = s->name;
  485. while(*n) {
  486. Bputc(b, *n);
  487. n++;
  488. }
  489. Bputc(b, 0);
  490. }
  491. void
  492. zaddr(Biobuf *b, Adr *a, int s)
  493. {
  494. long l;
  495. int i, t;
  496. char *n;
  497. Ieee e;
  498. t = 0;
  499. if(a->field)
  500. t |= T_FIELD;
  501. if(s)
  502. t |= T_SYM;
  503. switch(a->type) {
  504. default:
  505. if(a->offset)
  506. t |= T_OFFSET;
  507. if(a->displace)
  508. t |= T_INDEX;
  509. if(a->type & ~0xff)
  510. t |= T_TYPE;
  511. break;
  512. case D_FCONST:
  513. t |= T_FCONST;
  514. break;
  515. case D_SCONST:
  516. t |= T_SCONST;
  517. break;
  518. }
  519. Bputc(b, t);
  520. if(t & T_FIELD) { /* implies field */
  521. i = a->field;
  522. Bputc(b, i);
  523. Bputc(b, i>>8);
  524. }
  525. if(t & T_INDEX) { /* implies index, scale, displace */
  526. i = D_NONE;
  527. Bputc(b, i);
  528. Bputc(b, i>>8);
  529. Bputc(b, 0);
  530. l = a->displace;
  531. Bputc(b, l);
  532. Bputc(b, l>>8);
  533. Bputc(b, l>>16);
  534. Bputc(b, l>>24);
  535. }
  536. if(t & T_OFFSET) { /* implies offset */
  537. l = a->offset;
  538. Bputc(b, l);
  539. Bputc(b, l>>8);
  540. Bputc(b, l>>16);
  541. Bputc(b, l>>24);
  542. }
  543. if(t & T_SYM) /* implies sym */
  544. Bputc(b, s);
  545. if(t & T_FCONST) {
  546. ieeedtod(&e, a->dval);
  547. l = e.l;
  548. Bputc(b, l);
  549. Bputc(b, l>>8);
  550. Bputc(b, l>>16);
  551. Bputc(b, l>>24);
  552. l = e.h;
  553. Bputc(b, l);
  554. Bputc(b, l>>8);
  555. Bputc(b, l>>16);
  556. Bputc(b, l>>24);
  557. return;
  558. }
  559. if(t & T_SCONST) {
  560. n = a->sval;
  561. for(i=0; i<NSNAME; i++) {
  562. Bputc(b, *n);
  563. n++;
  564. }
  565. return;
  566. }
  567. i = a->type;
  568. Bputc(b, i);
  569. if(t & T_TYPE)
  570. Bputc(b, i>>8);
  571. }
  572. void
  573. outhist(Biobuf *b)
  574. {
  575. Hist *h;
  576. char *p, *q, *op, c;
  577. Prog pg;
  578. int n;
  579. pg = zprog;
  580. pg.as = AHISTORY;
  581. c = pathchar();
  582. for(h = hist; h != H; h = h->link) {
  583. p = h->name;
  584. op = 0;
  585. /* on windows skip drive specifier in pathname */
  586. if(systemtype(Windows) && p && p[1] == ':'){
  587. p += 2;
  588. c = *p;
  589. }
  590. if(p && p[0] != c && h->offset == 0 && pathname){
  591. /* on windows skip drive specifier in pathname */
  592. if(systemtype(Windows) && pathname[1] == ':') {
  593. op = p;
  594. p = pathname+2;
  595. c = *p;
  596. } else if(pathname[0] == c){
  597. op = p;
  598. p = pathname;
  599. }
  600. }
  601. while(p) {
  602. q = utfrune(p, c);
  603. if(q) {
  604. n = q-p;
  605. if(n == 0){
  606. n = 1; /* leading "/" */
  607. *p = '/'; /* don't emit "\" on windows */
  608. }
  609. q++;
  610. } else {
  611. n = strlen(p);
  612. q = 0;
  613. }
  614. if(n) {
  615. Bputc(b, ANAME);
  616. Bputc(b, ANAME>>8);
  617. Bputc(b, D_FILE);
  618. Bputc(b, 1);
  619. Bputc(b, '<');
  620. Bwrite(b, p, n);
  621. Bputc(b, 0);
  622. }
  623. p = q;
  624. if(p == 0 && op) {
  625. p = op;
  626. op = 0;
  627. }
  628. }
  629. pg.lineno = h->line;
  630. pg.to.type = zprog.to.type;
  631. pg.to.offset = h->offset;
  632. if(h->offset)
  633. pg.to.type = D_CONST;
  634. zwrite(b, &pg, 0, 0);
  635. }
  636. }
  637. void
  638. ieeedtod(Ieee *ieee, double native)
  639. {
  640. double fr, ho, f;
  641. int exp;
  642. if(native < 0) {
  643. ieeedtod(ieee, -native);
  644. ieee->h |= 0x80000000L;
  645. return;
  646. }
  647. if(native == 0) {
  648. ieee->l = 0;
  649. ieee->h = 0;
  650. return;
  651. }
  652. fr = frexp(native, &exp);
  653. f = 2097152L; /* shouldnt use fp constants here */
  654. fr = modf(fr*f, &ho);
  655. ieee->h = ho;
  656. ieee->h &= 0xfffffL;
  657. ieee->h |= (exp+1022L) << 20;
  658. f = 65536L;
  659. fr = modf(fr*f, &ho);
  660. ieee->l = ho;
  661. ieee->l <<= 16;
  662. ieee->l |= (long)(fr*f);
  663. }
  664. int
  665. nodalloc(Type *t, int g, Node *n)
  666. {
  667. n->type = t;
  668. n->op = OREGISTER;
  669. n->addable = 12;
  670. n->complex = 0;
  671. g = regaddr(g);
  672. n->reg = g | I_INDIR;
  673. n->xoffset = 0;
  674. return g;
  675. }
  676. int
  677. mulcon(Node *n, Node *c, int result, Node *nn)
  678. {
  679. long v;
  680. if(typefd[n->type->etype])
  681. return 0;
  682. v = c->vconst;
  683. if(mulcon1(n, v, result, nn))
  684. return 1;
  685. return 0;
  686. }
  687. int
  688. shlcon(Node *n, Node *c, int result, Node *nn)
  689. {
  690. long v;
  691. v = 1L << c->vconst;
  692. return mulcon1(n, v, result, nn);
  693. }
  694. int
  695. mulcon1(Node *n, long v, int result, Node *nn)
  696. {
  697. int g, g1, a1, a2, neg;
  698. int o;
  699. char code[10], *p;
  700. if(result == D_NONE)
  701. return 0;
  702. neg = 0;
  703. if(v < 0) {
  704. v = -v;
  705. neg++;
  706. }
  707. a1 = 0;
  708. a2 = multabsize;
  709. for(;;) {
  710. if(a1 >= a2)
  711. return 0;
  712. g1 = (a2 + a1)/2;
  713. if(v < multab[g1].val) {
  714. a2 = g1;
  715. continue;
  716. }
  717. if(v > multab[g1].val) {
  718. a1 = g1+1;
  719. continue;
  720. }
  721. break;
  722. }
  723. strcpy(code, "0");
  724. strncat(code, multab[g1].code, sizeof(multab[0].code));
  725. p = code;
  726. if(p[1] == 'i')
  727. p += 2;
  728. g = regalloc(n->type, result);
  729. cgen(n, g, n);
  730. if(neg)
  731. gopcode(ONEG, n->type, D_NONE, n, g, n);
  732. g1 = regalloc(n->type, D_NONE);
  733. loop:
  734. switch(*p) {
  735. case 0:
  736. regfree(g1);
  737. gmove(n->type, nn->type, g, n, result, nn);
  738. regfree(g);
  739. return 1;
  740. case '0':
  741. o = OAS;
  742. *p -= '0';
  743. goto com;
  744. case '1':
  745. case '2':
  746. o = OSUB;
  747. *p -= '1';
  748. goto com;
  749. case '3':
  750. case '4':
  751. case '5':
  752. case '6':
  753. o = OADD;
  754. *p -= '3';
  755. com:
  756. a1 = g;
  757. if(*p == 1 || *p == 3)
  758. a1 = g1;
  759. a2 = g;
  760. if(*p == 0 || *p == 3)
  761. a2 = g1;
  762. gopcode(o, n->type, a1, n, a2, n);
  763. p++;
  764. break;
  765. default:
  766. a1 = *p++ - 'a' + 1;
  767. a2 = g;
  768. if(a1 > 8) {
  769. a2 = g1;
  770. a1 -= 8;
  771. }
  772. gopcode(OASHL, n->type, D_CONST, nodconst(a1), a2, n);
  773. break;
  774. }
  775. goto loop;
  776. }
  777. void
  778. nullwarn(Node *l, Node *r)
  779. {
  780. warn(Z, "result of operation not used");
  781. if(l != Z)
  782. cgen(l, D_NONE, Z);
  783. if(r != Z)
  784. cgen(r, D_NONE, Z);
  785. }
  786. void
  787. sextern(Sym *s, Node *a, long o, long w)
  788. {
  789. long e, lw;
  790. for(e=0; e<w; e+=NSNAME) {
  791. lw = NSNAME;
  792. if(w-e < lw)
  793. lw = w-e;
  794. gpseudo(ADATA, s, D_SCONST, 0L);
  795. p->from.offset += o+e;
  796. p->from.displace = lw;
  797. memmove(p->to.sval, a->cstring+e, lw);
  798. }
  799. }
  800. void
  801. gextern(Sym *s, Node *a, long o, long w)
  802. {
  803. if(a->op == OCONST && typev[a->type->etype]) {
  804. gpseudo(ADATA, s, D_CONST, (long)(a->vconst>>32));
  805. p->from.offset += o;
  806. p->from.displace = 4;
  807. gpseudo(ADATA, s, D_CONST, (long)(a->vconst));
  808. p->from.offset += o + 4;
  809. p->from.displace = 4;
  810. return;
  811. }
  812. gpseudo(ADATA, s, D_TREE, (long)a);
  813. p->from.offset += o;
  814. p->from.displace = w;
  815. }
  816. long
  817. align(long i, Type *t, int op)
  818. {
  819. long o;
  820. Type *v;
  821. int w;
  822. o = i;
  823. w = 1;
  824. switch(op) {
  825. default:
  826. diag(Z, "unknown align opcode %d", op);
  827. break;
  828. case Asu2: /* padding at end of a struct */
  829. w = SZ_LONG;
  830. if(packflg)
  831. w = packflg;
  832. break;
  833. case Ael1: /* initial allign of struct element */
  834. for(v=t; v->etype==TARRAY; v=v->link)
  835. ;
  836. w = ewidth[v->etype];
  837. if(w <= 0 || w >= SZ_SHORT)
  838. w = SZ_SHORT;
  839. if(packflg)
  840. w = packflg;
  841. break;
  842. case Ael2: /* width of a struct element */
  843. o += t->width;
  844. break;
  845. case Aarg0: /* initial passbyptr argument in arg list */
  846. if(typesuv[t->etype]) {
  847. o = align(o, types[TIND], Aarg1);
  848. o = align(o, types[TIND], Aarg2);
  849. }
  850. break;
  851. case Aarg1: /* initial allign of parameter */
  852. w = ewidth[t->etype];
  853. if(w <= 0 || w >= SZ_LONG) {
  854. w = SZ_LONG;
  855. break;
  856. }
  857. o += SZ_LONG - w; /* big endian adjustment */
  858. w = 1;
  859. break;
  860. case Aarg2: /* width of a parameter */
  861. o += t->width;
  862. w = SZ_LONG;
  863. break;
  864. case Aaut3: /* total allign of automatic */
  865. o = align(o, t, Ael1);
  866. o = align(o, t, Ael2);
  867. break;
  868. }
  869. o = round(o, w);
  870. if(debug['A'])
  871. print("align %s %ld %T = %ld\n", bnames[op], i, t, o);
  872. return o;
  873. }
  874. long
  875. maxround(long max, long v)
  876. {
  877. v += SZ_LONG-1;
  878. if(v > max)
  879. max = round(v, SZ_LONG);
  880. return max;
  881. }