txt.c 19 KB

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