txt.c 19 KB

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