txt.c 20 KB

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