txt.c 20 KB

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