txt.c 20 KB

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