PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/utils/5c/txt.c

https://bitbucket.org/floren/inferno/
C | 1250 lines | 1128 code | 103 blank | 19 comment | 237 complexity | 3a54fc9ecdf0d23b68ea650b75201ad4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. #include "gc.h"
  2. void
  3. ginit(void)
  4. {
  5. Type *t;
  6. thechar = '5';
  7. thestring = "arm";
  8. exregoffset = REGEXT;
  9. exfregoffset = FREGEXT;
  10. listinit();
  11. nstring = 0;
  12. mnstring = 0;
  13. nrathole = 0;
  14. pc = 0;
  15. breakpc = -1;
  16. continpc = -1;
  17. cases = C;
  18. firstp = P;
  19. lastp = P;
  20. tfield = types[TLONG];
  21. zprog.link = P;
  22. zprog.as = AGOK;
  23. zprog.reg = NREG;
  24. zprog.from.type = D_NONE;
  25. zprog.from.name = D_NONE;
  26. zprog.from.reg = NREG;
  27. zprog.to = zprog.from;
  28. zprog.scond = 0xE;
  29. regnode.op = OREGISTER;
  30. regnode.class = CEXREG;
  31. regnode.reg = REGTMP;
  32. regnode.complex = 0;
  33. regnode.addable = 11;
  34. regnode.type = types[TLONG];
  35. constnode.op = OCONST;
  36. constnode.class = CXXX;
  37. constnode.complex = 0;
  38. constnode.addable = 20;
  39. constnode.type = types[TLONG];
  40. fconstnode.op = OCONST;
  41. fconstnode.class = CXXX;
  42. fconstnode.complex = 0;
  43. fconstnode.addable = 20;
  44. fconstnode.type = types[TDOUBLE];
  45. nodsafe = new(ONAME, Z, Z);
  46. nodsafe->sym = slookup(".safe");
  47. nodsafe->type = types[TINT];
  48. nodsafe->etype = types[TINT]->etype;
  49. nodsafe->class = CAUTO;
  50. complex(nodsafe);
  51. t = typ(TARRAY, types[TCHAR]);
  52. symrathole = slookup(".rathole");
  53. symrathole->class = CGLOBL;
  54. symrathole->type = t;
  55. nodrat = new(ONAME, Z, Z);
  56. nodrat->sym = symrathole;
  57. nodrat->type = types[TIND];
  58. nodrat->etype = TVOID;
  59. nodrat->class = CGLOBL;
  60. complex(nodrat);
  61. nodrat->type = t;
  62. nodret = new(ONAME, Z, Z);
  63. nodret->sym = slookup(".ret");
  64. nodret->type = types[TIND];
  65. nodret->etype = TIND;
  66. nodret->class = CPARAM;
  67. nodret = new(OIND, nodret, Z);
  68. complex(nodret);
  69. com64init();
  70. memset(reg, 0, sizeof(reg));
  71. }
  72. void
  73. gclean(void)
  74. {
  75. int i;
  76. Sym *s;
  77. for(i=0; i<NREG; i++)
  78. if(reg[i])
  79. diag(Z, "reg %d left allocated", i);
  80. for(i=NREG; i<NREG+NFREG; i++)
  81. if(reg[i])
  82. diag(Z, "freg %d left allocated", i-NREG);
  83. while(mnstring)
  84. outstring("", 1L);
  85. symstring->type->width = nstring;
  86. symrathole->type->width = nrathole;
  87. for(i=0; i<NHASH; i++)
  88. for(s = hash[i]; s != S; s = s->link) {
  89. if(s->type == T)
  90. continue;
  91. if(s->type->width == 0)
  92. continue;
  93. if(s->class != CGLOBL && s->class != CSTATIC)
  94. continue;
  95. if(s->type == types[TENUM])
  96. continue;
  97. gpseudo(AGLOBL, s, nodconst(s->type->width));
  98. }
  99. nextpc();
  100. p->as = AEND;
  101. outcode();
  102. }
  103. void
  104. nextpc(void)
  105. {
  106. p = alloc(sizeof(*p));
  107. *p = zprog;
  108. p->lineno = nearln;
  109. pc++;
  110. if(firstp == P) {
  111. firstp = p;
  112. lastp = p;
  113. return;
  114. }
  115. lastp->link = p;
  116. lastp = p;
  117. }
  118. void
  119. gargs(Node *n, Node *tn1, Node *tn2)
  120. {
  121. long regs;
  122. Node fnxargs[20], *fnxp;
  123. regs = cursafe;
  124. fnxp = fnxargs;
  125. garg1(n, tn1, tn2, 0, &fnxp); /* compile fns to temps */
  126. curarg = 0;
  127. fnxp = fnxargs;
  128. garg1(n, tn1, tn2, 1, &fnxp); /* compile normal args and temps */
  129. cursafe = regs;
  130. }
  131. void
  132. garg1(Node *n, Node *tn1, Node *tn2, int f, Node **fnxp)
  133. {
  134. Node nod;
  135. if(n == Z)
  136. return;
  137. if(n->op == OLIST) {
  138. garg1(n->left, tn1, tn2, f, fnxp);
  139. garg1(n->right, tn1, tn2, f, fnxp);
  140. return;
  141. }
  142. if(f == 0) {
  143. if(n->complex >= FNX) {
  144. regsalloc(*fnxp, n);
  145. nod = znode;
  146. nod.op = OAS;
  147. nod.left = *fnxp;
  148. nod.right = n;
  149. nod.type = n->type;
  150. cgen(&nod, Z);
  151. (*fnxp)++;
  152. }
  153. return;
  154. }
  155. if(typesuv[n->type->etype]) {
  156. regaalloc(tn2, n);
  157. if(n->complex >= FNX) {
  158. sugen(*fnxp, tn2, n->type->width);
  159. (*fnxp)++;
  160. } else
  161. sugen(n, tn2, n->type->width);
  162. return;
  163. }
  164. if(REGARG >= 0 && curarg == 0 && typechlp[n->type->etype]) {
  165. regaalloc1(tn1, n);
  166. if(n->complex >= FNX) {
  167. cgen(*fnxp, tn1);
  168. (*fnxp)++;
  169. } else
  170. cgen(n, tn1);
  171. return;
  172. }
  173. regalloc(tn1, n, Z);
  174. if(n->complex >= FNX) {
  175. cgen(*fnxp, tn1);
  176. (*fnxp)++;
  177. } else
  178. cgen(n, tn1);
  179. regaalloc(tn2, n);
  180. gopcode(OAS, tn1, Z, tn2);
  181. regfree(tn1);
  182. }
  183. Node*
  184. nodconst(long v)
  185. {
  186. constnode.vconst = v;
  187. return &constnode;
  188. }
  189. Node*
  190. nod32const(vlong v)
  191. {
  192. constnode.vconst = v & MASK(32);
  193. return &constnode;
  194. }
  195. Node*
  196. nodfconst(double d)
  197. {
  198. fconstnode.fconst = d;
  199. return &fconstnode;
  200. }
  201. void
  202. nodreg(Node *n, Node *nn, int reg)
  203. {
  204. *n = regnode;
  205. n->reg = reg;
  206. n->type = nn->type;
  207. n->lineno = nn->lineno;
  208. }
  209. void
  210. regret(Node *n, Node *nn)
  211. {
  212. int r;
  213. r = REGRET;
  214. if(typefd[nn->type->etype])
  215. r = FREGRET+NREG;
  216. nodreg(n, nn, r);
  217. reg[r]++;
  218. }
  219. int
  220. tmpreg(void)
  221. {
  222. int i;
  223. for(i=REGRET+1; i<NREG; i++)
  224. if(reg[i] == 0)
  225. return i;
  226. diag(Z, "out of fixed registers");
  227. return 0;
  228. }
  229. void
  230. regalloc(Node *n, Node *tn, Node *o)
  231. {
  232. int i, j;
  233. static int lasti;
  234. switch(tn->type->etype) {
  235. case TCHAR:
  236. case TUCHAR:
  237. case TSHORT:
  238. case TUSHORT:
  239. case TINT:
  240. case TUINT:
  241. case TLONG:
  242. case TULONG:
  243. case TIND:
  244. if(o != Z && o->op == OREGISTER) {
  245. i = o->reg;
  246. if(i >= 0 && i < NREG)
  247. goto out;
  248. }
  249. j = lasti + REGRET+1;
  250. for(i=REGRET+1; i<NREG; i++) {
  251. if(j >= NREG)
  252. j = REGRET+1;
  253. if(reg[j] == 0) {
  254. i = j;
  255. goto out;
  256. }
  257. j++;
  258. }
  259. diag(tn, "out of fixed registers");
  260. goto err;
  261. case TFLOAT:
  262. case TDOUBLE:
  263. case TVLONG:
  264. if(o != Z && o->op == OREGISTER) {
  265. i = o->reg;
  266. if(i >= NREG && i < NREG+NFREG)
  267. goto out;
  268. }
  269. j = 0*2 + NREG;
  270. for(i=NREG; i<NREG+NFREG; i++) {
  271. if(j >= NREG+NFREG)
  272. j = NREG;
  273. if(reg[j] == 0) {
  274. i = j;
  275. goto out;
  276. }
  277. j++;
  278. }
  279. diag(tn, "out of float registers");
  280. goto err;
  281. }
  282. diag(tn, "unknown type in regalloc: %T", tn->type);
  283. err:
  284. nodreg(n, tn, 0);
  285. return;
  286. out:
  287. reg[i]++;
  288. /* lasti++; *** StrongARM does register forwarding */
  289. if(lasti >= 5)
  290. lasti = 0;
  291. nodreg(n, tn, i);
  292. }
  293. void
  294. regialloc(Node *n, Node *tn, Node *o)
  295. {
  296. Node nod;
  297. nod = *tn;
  298. nod.type = types[TIND];
  299. regalloc(n, &nod, o);
  300. }
  301. void
  302. regfree(Node *n)
  303. {
  304. int i;
  305. i = 0;
  306. if(n->op != OREGISTER && n->op != OINDREG)
  307. goto err;
  308. i = n->reg;
  309. if(i < 0 || i >= sizeof(reg))
  310. goto err;
  311. if(reg[i] <= 0)
  312. goto err;
  313. reg[i]--;
  314. return;
  315. err:
  316. diag(n, "error in regfree: %d", i);
  317. }
  318. void
  319. regsalloc(Node *n, Node *nn)
  320. {
  321. cursafe = align(cursafe, nn->type, Aaut3);
  322. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  323. *n = *nodsafe;
  324. n->xoffset = -(stkoff + cursafe);
  325. n->type = nn->type;
  326. n->etype = nn->type->etype;
  327. n->lineno = nn->lineno;
  328. }
  329. void
  330. regaalloc1(Node *n, Node *nn)
  331. {
  332. nodreg(n, nn, REGARG);
  333. reg[REGARG]++;
  334. curarg = align(curarg, nn->type, Aarg1);
  335. curarg = align(curarg, nn->type, Aarg2);
  336. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  337. }
  338. void
  339. regaalloc(Node *n, Node *nn)
  340. {
  341. curarg = align(curarg, nn->type, Aarg1);
  342. *n = *nn;
  343. n->op = OINDREG;
  344. n->reg = REGSP;
  345. n->xoffset = curarg + SZ_LONG;
  346. n->complex = 0;
  347. n->addable = 20;
  348. curarg = align(curarg, nn->type, Aarg2);
  349. maxargsafe = maxround(maxargsafe, cursafe+curarg);
  350. }
  351. void
  352. regind(Node *n, Node *nn)
  353. {
  354. if(n->op != OREGISTER) {
  355. diag(n, "regind not OREGISTER");
  356. return;
  357. }
  358. n->op = OINDREG;
  359. n->type = nn->type;
  360. }
  361. void
  362. raddr(Node *n, Prog *p)
  363. {
  364. Adr a;
  365. naddr(n, &a);
  366. if(R0ISZERO && a.type == D_CONST && a.offset == 0) {
  367. a.type = D_REG;
  368. a.reg = 0;
  369. }
  370. if(a.type != D_REG && a.type != D_FREG) {
  371. if(n)
  372. diag(n, "bad in raddr: %O", n->op);
  373. else
  374. diag(n, "bad in raddr: <null>");
  375. p->reg = NREG;
  376. } else
  377. p->reg = a.reg;
  378. }
  379. void
  380. naddr(Node *n, Adr *a)
  381. {
  382. long v;
  383. a->type = D_NONE;
  384. if(n == Z)
  385. return;
  386. switch(n->op) {
  387. default:
  388. bad:
  389. diag(n, "bad in naddr: %O", n->op);
  390. break;
  391. case OREGISTER:
  392. a->type = D_REG;
  393. a->sym = S;
  394. a->reg = n->reg;
  395. if(a->reg >= NREG) {
  396. a->type = D_FREG;
  397. a->reg -= NREG;
  398. }
  399. break;
  400. case OIND:
  401. naddr(n->left, a);
  402. if(a->type == D_REG) {
  403. a->type = D_OREG;
  404. break;
  405. }
  406. if(a->type == D_CONST) {
  407. a->type = D_OREG;
  408. break;
  409. }
  410. goto bad;
  411. case OINDREG:
  412. a->type = D_OREG;
  413. a->sym = S;
  414. a->offset = n->xoffset;
  415. a->reg = n->reg;
  416. break;
  417. case ONAME:
  418. a->etype = n->etype;
  419. a->type = D_OREG;
  420. a->name = D_STATIC;
  421. a->sym = n->sym;
  422. a->offset = n->xoffset;
  423. if(n->class == CSTATIC)
  424. break;
  425. if(n->class == CEXTERN || n->class == CGLOBL) {
  426. a->name = D_EXTERN;
  427. break;
  428. }
  429. if(n->class == CAUTO) {
  430. a->name = D_AUTO;
  431. break;
  432. }
  433. if(n->class == CPARAM) {
  434. a->name = D_PARAM;
  435. break;
  436. }
  437. goto bad;
  438. case OCONST:
  439. a->sym = S;
  440. a->reg = NREG;
  441. if(typefd[n->type->etype]) {
  442. a->type = D_FCONST;
  443. a->dval = n->fconst;
  444. } else {
  445. a->type = D_CONST;
  446. a->offset = n->vconst;
  447. }
  448. break;
  449. case OADDR:
  450. naddr(n->left, a);
  451. if(a->type == D_OREG) {
  452. a->type = D_CONST;
  453. break;
  454. }
  455. goto bad;
  456. case OADD:
  457. if(n->left->op == OCONST) {
  458. naddr(n->left, a);
  459. v = a->offset;
  460. naddr(n->right, a);
  461. } else {
  462. naddr(n->right, a);
  463. v = a->offset;
  464. naddr(n->left, a);
  465. }
  466. a->offset += v;
  467. break;
  468. }
  469. }
  470. void
  471. fop(int as, int f1, int f2, Node *t)
  472. {
  473. Node nod1, nod2, nod3;
  474. nodreg(&nod1, t, NREG+f1);
  475. nodreg(&nod2, t, NREG+f2);
  476. regalloc(&nod3, t, t);
  477. gopcode(as, &nod1, &nod2, &nod3);
  478. gmove(&nod3, t);
  479. regfree(&nod3);
  480. }
  481. void
  482. gmovm(Node *f, Node *t, int w)
  483. {
  484. gins(AMOVM, f, t);
  485. p->scond |= C_UBIT;
  486. if(w)
  487. p->scond |= C_WBIT;
  488. }
  489. void
  490. gmove(Node *f, Node *t)
  491. {
  492. int ft, tt, a;
  493. Node nod;
  494. ft = f->type->etype;
  495. tt = t->type->etype;
  496. if(ft == TDOUBLE && f->op == OCONST) {
  497. }
  498. if(ft == TFLOAT && f->op == OCONST) {
  499. }
  500. /*
  501. * a load --
  502. * put it into a register then
  503. * worry what to do with it.
  504. */
  505. if(f->op == ONAME || f->op == OINDREG || f->op == OIND) {
  506. switch(ft) {
  507. default:
  508. a = AMOVW;
  509. break;
  510. case TFLOAT:
  511. a = AMOVF;
  512. break;
  513. case TDOUBLE:
  514. a = AMOVD;
  515. break;
  516. case TCHAR:
  517. a = AMOVB;
  518. break;
  519. case TUCHAR:
  520. a = AMOVBU;
  521. break;
  522. case TSHORT:
  523. a = AMOVH;
  524. break;
  525. case TUSHORT:
  526. a = AMOVHU;
  527. break;
  528. }
  529. if(typechlp[ft] && typeilp[tt])
  530. regalloc(&nod, t, t);
  531. else
  532. regalloc(&nod, f, t);
  533. gins(a, f, &nod);
  534. gmove(&nod, t);
  535. regfree(&nod);
  536. return;
  537. }
  538. /*
  539. * a store --
  540. * put it into a register then
  541. * store it.
  542. */
  543. if(t->op == ONAME || t->op == OINDREG || t->op == OIND) {
  544. switch(tt) {
  545. default:
  546. a = AMOVW;
  547. break;
  548. case TUCHAR:
  549. a = AMOVBU;
  550. break;
  551. case TCHAR:
  552. a = AMOVB;
  553. break;
  554. case TUSHORT:
  555. a = AMOVHU;
  556. break;
  557. case TSHORT:
  558. a = AMOVH;
  559. break;
  560. case TFLOAT:
  561. a = AMOVF;
  562. break;
  563. case TVLONG:
  564. case TDOUBLE:
  565. a = AMOVD;
  566. break;
  567. }
  568. if(ft == tt)
  569. regalloc(&nod, t, f);
  570. else
  571. regalloc(&nod, t, Z);
  572. gmove(f, &nod);
  573. gins(a, &nod, t);
  574. regfree(&nod);
  575. return;
  576. }
  577. /*
  578. * type x type cross table
  579. */
  580. a = AGOK;
  581. switch(ft) {
  582. case TDOUBLE:
  583. case TVLONG:
  584. case TFLOAT:
  585. switch(tt) {
  586. case TDOUBLE:
  587. case TVLONG:
  588. a = AMOVD;
  589. if(ft == TFLOAT)
  590. a = AMOVFD;
  591. break;
  592. case TFLOAT:
  593. a = AMOVDF;
  594. if(ft == TFLOAT)
  595. a = AMOVF;
  596. break;
  597. case TINT:
  598. case TUINT:
  599. case TLONG:
  600. case TULONG:
  601. case TIND:
  602. a = AMOVDW;
  603. if(ft == TFLOAT)
  604. a = AMOVFW;
  605. break;
  606. case TSHORT:
  607. case TUSHORT:
  608. case TCHAR:
  609. case TUCHAR:
  610. a = AMOVDW;
  611. if(ft == TFLOAT)
  612. a = AMOVFW;
  613. break;
  614. }
  615. break;
  616. case TUINT:
  617. case TINT:
  618. case TULONG:
  619. case TLONG:
  620. case TIND:
  621. switch(tt) {
  622. case TDOUBLE:
  623. case TVLONG:
  624. gins(AMOVWD, f, t);
  625. if(ft == TULONG) {
  626. }
  627. return;
  628. case TFLOAT:
  629. gins(AMOVWF, f, t);
  630. if(ft == TULONG) {
  631. }
  632. return;
  633. case TINT:
  634. case TUINT:
  635. case TLONG:
  636. case TULONG:
  637. case TIND:
  638. case TSHORT:
  639. case TUSHORT:
  640. case TCHAR:
  641. case TUCHAR:
  642. a = AMOVW;
  643. break;
  644. }
  645. break;
  646. case TSHORT:
  647. switch(tt) {
  648. case TDOUBLE:
  649. case TVLONG:
  650. regalloc(&nod, f, Z);
  651. gins(AMOVH, f, &nod);
  652. gins(AMOVWD, &nod, t);
  653. regfree(&nod);
  654. return;
  655. case TFLOAT:
  656. regalloc(&nod, f, Z);
  657. gins(AMOVH, f, &nod);
  658. gins(AMOVWF, &nod, t);
  659. regfree(&nod);
  660. return;
  661. case TUINT:
  662. case TINT:
  663. case TULONG:
  664. case TLONG:
  665. case TIND:
  666. a = AMOVH;
  667. break;
  668. case TSHORT:
  669. case TUSHORT:
  670. case TCHAR:
  671. case TUCHAR:
  672. a = AMOVW;
  673. break;
  674. }
  675. break;
  676. case TUSHORT:
  677. switch(tt) {
  678. case TDOUBLE:
  679. case TVLONG:
  680. regalloc(&nod, f, Z);
  681. gins(AMOVHU, f, &nod);
  682. gins(AMOVWD, &nod, t);
  683. regfree(&nod);
  684. return;
  685. case TFLOAT:
  686. regalloc(&nod, f, Z);
  687. gins(AMOVHU, f, &nod);
  688. gins(AMOVWF, &nod, t);
  689. regfree(&nod);
  690. return;
  691. case TINT:
  692. case TUINT:
  693. case TLONG:
  694. case TULONG:
  695. case TIND:
  696. a = AMOVHU;
  697. break;
  698. case TSHORT:
  699. case TUSHORT:
  700. case TCHAR:
  701. case TUCHAR:
  702. a = AMOVW;
  703. break;
  704. }
  705. break;
  706. case TCHAR:
  707. switch(tt) {
  708. case TDOUBLE:
  709. case TVLONG:
  710. regalloc(&nod, f, Z);
  711. gins(AMOVB, f, &nod);
  712. gins(AMOVWD, &nod, t);
  713. regfree(&nod);
  714. return;
  715. case TFLOAT:
  716. regalloc(&nod, f, Z);
  717. gins(AMOVB, f, &nod);
  718. gins(AMOVWF, &nod, t);
  719. regfree(&nod);
  720. return;
  721. case TINT:
  722. case TUINT:
  723. case TLONG:
  724. case TULONG:
  725. case TIND:
  726. case TSHORT:
  727. case TUSHORT:
  728. a = AMOVB;
  729. break;
  730. case TCHAR:
  731. case TUCHAR:
  732. a = AMOVW;
  733. break;
  734. }
  735. break;
  736. case TUCHAR:
  737. switch(tt) {
  738. case TDOUBLE:
  739. case TVLONG:
  740. regalloc(&nod, f, Z);
  741. gins(AMOVBU, f, &nod);
  742. gins(AMOVWD, &nod, t);
  743. regfree(&nod);
  744. return;
  745. case TFLOAT:
  746. regalloc(&nod, f, Z);
  747. gins(AMOVBU, f, &nod);
  748. gins(AMOVWF, &nod, t);
  749. regfree(&nod);
  750. return;
  751. case TINT:
  752. case TUINT:
  753. case TLONG:
  754. case TULONG:
  755. case TIND:
  756. case TSHORT:
  757. case TUSHORT:
  758. a = AMOVBU;
  759. break;
  760. case TCHAR:
  761. case TUCHAR:
  762. a = AMOVW;
  763. break;
  764. }
  765. break;
  766. }
  767. if(a == AGOK)
  768. diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type);
  769. if(a == AMOVW || a == AMOVF || a == AMOVD)
  770. if(samaddr(f, t))
  771. return;
  772. gins(a, f, t);
  773. }
  774. void
  775. gmover(Node *f, Node *t)
  776. {
  777. int ft, tt, a;
  778. ft = f->type->etype;
  779. tt = t->type->etype;
  780. a = AGOK;
  781. if(typechlp[ft] && typechlp[tt] && ewidth[ft] >= ewidth[tt]){
  782. switch(tt){
  783. case TSHORT:
  784. a = AMOVH;
  785. break;
  786. case TUSHORT:
  787. a = AMOVHU;
  788. break;
  789. case TCHAR:
  790. a = AMOVB;
  791. break;
  792. case TUCHAR:
  793. a = AMOVBU;
  794. break;
  795. }
  796. }
  797. if(a == AGOK)
  798. gmove(f, t);
  799. else
  800. gins(a, f, t);
  801. }
  802. void
  803. gins(int a, Node *f, Node *t)
  804. {
  805. nextpc();
  806. p->as = a;
  807. if(f != Z)
  808. naddr(f, &p->from);
  809. if(t != Z)
  810. naddr(t, &p->to);
  811. if(debug['g'])
  812. print("%P\n", p);
  813. }
  814. void
  815. gopcode(int o, Node *f1, Node *f2, Node *t)
  816. {
  817. int a, et;
  818. Adr ta;
  819. et = TLONG;
  820. if(f1 != Z && f1->type != T)
  821. et = f1->type->etype;
  822. a = AGOK;
  823. switch(o) {
  824. case OAS:
  825. gmove(f1, t);
  826. return;
  827. case OASADD:
  828. case OADD:
  829. a = AADD;
  830. if(et == TFLOAT)
  831. a = AADDF;
  832. else
  833. if(et == TDOUBLE || et == TVLONG)
  834. a = AADDD;
  835. break;
  836. case OASSUB:
  837. case OSUB:
  838. if(f2 && f2->op == OCONST) {
  839. Node *t = f1;
  840. f1 = f2;
  841. f2 = t;
  842. a = ARSB;
  843. } else
  844. a = ASUB;
  845. if(et == TFLOAT)
  846. a = ASUBF;
  847. else
  848. if(et == TDOUBLE || et == TVLONG)
  849. a = ASUBD;
  850. break;
  851. case OASOR:
  852. case OOR:
  853. a = AORR;
  854. break;
  855. case OASAND:
  856. case OAND:
  857. a = AAND;
  858. break;
  859. case OASXOR:
  860. case OXOR:
  861. a = AEOR;
  862. break;
  863. case OASLSHR:
  864. case OLSHR:
  865. a = ASRL;
  866. break;
  867. case OASASHR:
  868. case OASHR:
  869. a = ASRA;
  870. break;
  871. case OASASHL:
  872. case OASHL:
  873. a = ASLL;
  874. break;
  875. case OFUNC:
  876. a = ABL;
  877. break;
  878. case OASMUL:
  879. case OMUL:
  880. a = AMUL;
  881. if(et == TFLOAT)
  882. a = AMULF;
  883. else
  884. if(et == TDOUBLE || et == TVLONG)
  885. a = AMULD;
  886. break;
  887. case OASDIV:
  888. case ODIV:
  889. a = ADIV;
  890. if(et == TFLOAT)
  891. a = ADIVF;
  892. else
  893. if(et == TDOUBLE || et == TVLONG)
  894. a = ADIVD;
  895. break;
  896. case OASMOD:
  897. case OMOD:
  898. a = AMOD;
  899. break;
  900. case OASLMUL:
  901. case OLMUL:
  902. a = AMULU;
  903. break;
  904. case OASLMOD:
  905. case OLMOD:
  906. a = AMODU;
  907. break;
  908. case OASLDIV:
  909. case OLDIV:
  910. a = ADIVU;
  911. break;
  912. case OCASE:
  913. case OEQ:
  914. case ONE:
  915. case OLT:
  916. case OLE:
  917. case OGE:
  918. case OGT:
  919. case OLO:
  920. case OLS:
  921. case OHS:
  922. case OHI:
  923. a = ACMP;
  924. if(et == TFLOAT)
  925. a = ACMPF;
  926. else
  927. if(et == TDOUBLE || et == TVLONG)
  928. a = ACMPD;
  929. nextpc();
  930. p->as = a;
  931. naddr(f1, &p->from);
  932. if(a == ACMP && f1->op == OCONST && p->from.offset < 0) {
  933. p->as = ACMN;
  934. p->from.offset = -p->from.offset;
  935. }
  936. raddr(f2, p);
  937. switch(o) {
  938. case OEQ:
  939. a = ABEQ;
  940. break;
  941. case ONE:
  942. a = ABNE;
  943. break;
  944. case OLT:
  945. a = ABLT;
  946. break;
  947. case OLE:
  948. a = ABLE;
  949. break;
  950. case OGE:
  951. a = ABGE;
  952. break;
  953. case OGT:
  954. a = ABGT;
  955. break;
  956. case OLO:
  957. a = ABLO;
  958. break;
  959. case OLS:
  960. a = ABLS;
  961. break;
  962. case OHS:
  963. a = ABHS;
  964. break;
  965. case OHI:
  966. a = ABHI;
  967. break;
  968. case OCASE:
  969. nextpc();
  970. p->as = ACASE;
  971. p->scond = 0x9;
  972. naddr(f2, &p->from);
  973. a = ABHI;
  974. break;
  975. }
  976. f1 = Z;
  977. f2 = Z;
  978. break;
  979. }
  980. if(a == AGOK)
  981. diag(Z, "bad in gopcode %O", o);
  982. nextpc();
  983. p->as = a;
  984. if(f1 != Z)
  985. naddr(f1, &p->from);
  986. if(f2 != Z) {
  987. naddr(f2, &ta);
  988. p->reg = ta.reg;
  989. }
  990. if(t != Z)
  991. naddr(t, &p->to);
  992. if(debug['g'])
  993. print("%P\n", p);
  994. }
  995. int
  996. samaddr(Node *f, Node *t)
  997. {
  998. if(f->op != t->op)
  999. return 0;
  1000. switch(f->op) {
  1001. case OREGISTER:
  1002. if(f->reg != t->reg)
  1003. break;
  1004. return 1;
  1005. }
  1006. return 0;
  1007. }
  1008. void
  1009. gbranch(int o)
  1010. {
  1011. int a;
  1012. a = AGOK;
  1013. switch(o) {
  1014. case ORETURN:
  1015. a = ARET;
  1016. break;
  1017. case OGOTO:
  1018. a = AB;
  1019. break;
  1020. }
  1021. nextpc();
  1022. if(a == AGOK) {
  1023. diag(Z, "bad in gbranch %O", o);
  1024. nextpc();
  1025. }
  1026. p->as = a;
  1027. }
  1028. void
  1029. patch(Prog *op, long pc)
  1030. {
  1031. op->to.offset = pc;
  1032. op->to.type = D_BRANCH;
  1033. }
  1034. void
  1035. gpseudo(int a, Sym *s, Node *n)
  1036. {
  1037. nextpc();
  1038. p->as = a;
  1039. p->from.type = D_OREG;
  1040. p->from.sym = s;
  1041. p->from.name = D_EXTERN;
  1042. if(a == ATEXT)
  1043. p->reg = (profileflg ? 0 : NOPROF);
  1044. if(s->class == CSTATIC)
  1045. p->from.name = D_STATIC;
  1046. naddr(n, &p->to);
  1047. if(a == ADATA || a == AGLOBL)
  1048. pc--;
  1049. }
  1050. int
  1051. sconst(Node *n)
  1052. {
  1053. vlong vv;
  1054. if(n->op == OCONST) {
  1055. if(!typefd[n->type->etype]) {
  1056. vv = n->vconst;
  1057. if(vv >= (vlong)(-32766) && vv < (vlong)32766)
  1058. return 1;
  1059. /*
  1060. * should be specialised for constant values which will
  1061. * fit in different instructionsl; for now, let 5l
  1062. * sort it out
  1063. */
  1064. return 1;
  1065. }
  1066. }
  1067. return 0;
  1068. }
  1069. int
  1070. sval(long v)
  1071. {
  1072. int i;
  1073. for(i=0; i<16; i++) {
  1074. if((v & ~0xff) == 0)
  1075. return 1;
  1076. if((~v & ~0xff) == 0)
  1077. return 1;
  1078. v = (v<<2) | ((ulong)v>>30);
  1079. }
  1080. return 0;
  1081. }
  1082. long
  1083. exreg(Type *t)
  1084. {
  1085. long o;
  1086. if(typechlp[t->etype]) {
  1087. if(exregoffset <= REGEXT-4)
  1088. return 0;
  1089. o = exregoffset;
  1090. exregoffset--;
  1091. return o;
  1092. }
  1093. if(typefd[t->etype]) {
  1094. if(exfregoffset <= NFREG-1)
  1095. return 0;
  1096. o = exfregoffset + NREG;
  1097. exfregoffset--;
  1098. return o;
  1099. }
  1100. return 0;
  1101. }
  1102. schar ewidth[NTYPE] =
  1103. {
  1104. -1, /* [TXXX] */
  1105. SZ_CHAR, /* [TCHAR] */
  1106. SZ_CHAR, /* [TUCHAR] */
  1107. SZ_SHORT, /* [TSHORT] */
  1108. SZ_SHORT, /* [TUSHORT] */
  1109. SZ_INT, /* [TINT] */
  1110. SZ_INT, /* [TUINT] */
  1111. SZ_LONG, /* [TLONG] */
  1112. SZ_LONG, /* [TULONG] */
  1113. SZ_VLONG, /* [TVLONG] */
  1114. SZ_VLONG, /* [TUVLONG] */
  1115. SZ_FLOAT, /* [TFLOAT] */
  1116. SZ_DOUBLE, /* [TDOUBLE] */
  1117. SZ_IND, /* [TIND] */
  1118. 0, /* [TFUNC] */
  1119. -1, /* [TARRAY] */
  1120. 0, /* [TVOID] */
  1121. -1, /* [TSTRUCT] */
  1122. -1, /* [TUNION] */
  1123. SZ_INT, /* [TENUM] */
  1124. };
  1125. long ncast[NTYPE] =
  1126. {
  1127. 0, /* [TXXX] */
  1128. BCHAR|BUCHAR, /* [TCHAR] */
  1129. BCHAR|BUCHAR, /* [TUCHAR] */
  1130. BSHORT|BUSHORT, /* [TSHORT] */
  1131. BSHORT|BUSHORT, /* [TUSHORT] */
  1132. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1133. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1134. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1135. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1136. BVLONG|BUVLONG, /* [TVLONG] */
  1137. BVLONG|BUVLONG, /* [TUVLONG] */
  1138. BFLOAT, /* [TFLOAT] */
  1139. BDOUBLE, /* [TDOUBLE] */
  1140. BLONG|BULONG|BIND, /* [TIND] */
  1141. 0, /* [TFUNC] */
  1142. 0, /* [TARRAY] */
  1143. 0, /* [TVOID] */
  1144. BSTRUCT, /* [TSTRUCT] */
  1145. BUNION, /* [TUNION] */
  1146. 0, /* [TENUM] */
  1147. };