txt.c 22 KB

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