txt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. #include "gc.h"
  2. static uchar resvreg[sizeof(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. /*
  370. if(a.type == D_CONST && a.offset == 0) {
  371. a.type = D_REG;
  372. a.reg = REGZERO;
  373. }
  374. */
  375. if(a.type != D_REG && a.type != D_FREG) {
  376. if(n)
  377. diag(n, "bad in raddr: %O", n->op);
  378. else
  379. diag(n, "bad in raddr: <null>");
  380. p->reg = NREG;
  381. } else
  382. p->reg = a.reg;
  383. }
  384. void
  385. naddr(Node *n, Adr *a)
  386. {
  387. long v;
  388. a->type = D_NONE;
  389. if(n == Z)
  390. return;
  391. switch(n->op) {
  392. default:
  393. bad:
  394. diag(n, "bad in naddr: %O", n->op);
  395. break;
  396. case OREGISTER:
  397. a->type = D_REG;
  398. a->sym = S;
  399. a->reg = n->reg;
  400. if(a->reg >= NREG) {
  401. a->type = D_FREG;
  402. a->reg -= NREG;
  403. }
  404. break;
  405. case OIND:
  406. naddr(n->left, a);
  407. if(a->type == D_REG) {
  408. a->type = D_OREG;
  409. break;
  410. }
  411. if(a->type == D_CONST) {
  412. a->type = D_OREG;
  413. break;
  414. }
  415. goto bad;
  416. case OINDREG:
  417. a->type = D_OREG;
  418. a->sym = S;
  419. a->offset = n->xoffset;
  420. a->reg = n->reg;
  421. break;
  422. case ONAME:
  423. a->etype = n->etype;
  424. a->type = D_OREG;
  425. a->name = D_STATIC;
  426. a->sym = n->sym;
  427. a->offset = n->xoffset;
  428. if(n->class == CSTATIC)
  429. break;
  430. if(n->class == CEXTERN || n->class == CGLOBL) {
  431. a->name = D_EXTERN;
  432. break;
  433. }
  434. if(n->class == CAUTO) {
  435. a->name = D_AUTO;
  436. break;
  437. }
  438. if(n->class == CPARAM) {
  439. a->name = D_PARAM;
  440. break;
  441. }
  442. goto bad;
  443. case OCONST:
  444. a->sym = S;
  445. a->reg = NREG;
  446. if(typefd[n->type->etype]) {
  447. a->type = D_FCONST;
  448. a->dval = n->fconst;
  449. } else {
  450. a->type = D_CONST;
  451. a->offset = n->vconst;
  452. }
  453. break;
  454. case OADDR:
  455. naddr(n->left, a);
  456. if(a->type == D_OREG) {
  457. a->type = D_CONST;
  458. break;
  459. }
  460. goto bad;
  461. case OADD:
  462. if(n->left->op == OCONST) {
  463. naddr(n->left, a);
  464. v = a->offset;
  465. naddr(n->right, a);
  466. } else {
  467. naddr(n->right, a);
  468. v = a->offset;
  469. naddr(n->left, a);
  470. }
  471. a->offset += v;
  472. break;
  473. }
  474. }
  475. void
  476. fop(int as, int f1, int f2, Node *t)
  477. {
  478. Node nod1, nod2, nod3;
  479. nodreg(&nod1, t, NREG+f1);
  480. nodreg(&nod2, t, NREG+f2);
  481. regalloc(&nod3, t, t);
  482. gopcode(as, &nod1, &nod2, &nod3);
  483. gmove(&nod3, t);
  484. regfree(&nod3);
  485. }
  486. void
  487. gmove(Node *f, Node *t)
  488. {
  489. int ft, tt, a;
  490. Node nod, fxc0, fxc1, fxc2;
  491. Prog *p1;
  492. double d;
  493. ft = f->type->etype;
  494. tt = t->type->etype;
  495. if(ft == TDOUBLE && f->op == OCONST) {
  496. d = f->fconst;
  497. if(d == 0.0) {
  498. a = FREGZERO;
  499. goto ffreg;
  500. }
  501. if(d == 0.5) {
  502. a = FREGHALF;
  503. goto ffreg;
  504. }
  505. if(d == 1.0) {
  506. a = FREGONE;
  507. goto ffreg;
  508. }
  509. if(d == 2.0) {
  510. a = FREGTWO;
  511. goto ffreg;
  512. }
  513. if(d == -.5) {
  514. fop(OSUB, FREGHALF, FREGZERO, t);
  515. return;
  516. }
  517. if(d == -1.0) {
  518. fop(OSUB, FREGONE, FREGZERO, t);
  519. return;
  520. }
  521. if(d == -2.0) {
  522. fop(OSUB, FREGTWO, FREGZERO, t);
  523. return;
  524. }
  525. if(d == 1.5) {
  526. fop(OADD, FREGONE, FREGHALF, t);
  527. return;
  528. }
  529. if(d == 2.5) {
  530. fop(OADD, FREGTWO, FREGHALF, t);
  531. return;
  532. }
  533. if(d == 3.0) {
  534. fop(OADD, FREGTWO, FREGONE, t);
  535. return;
  536. }
  537. }
  538. if(ft == TFLOAT && f->op == OCONST) {
  539. d = f->fconst;
  540. if(d == 0) {
  541. a = FREGZERO;
  542. ffreg:
  543. nodreg(&nod, f, NREG+a);
  544. gmove(&nod, t);
  545. return;
  546. }
  547. }
  548. /*
  549. * a load --
  550. * put it into a register then
  551. * worry what to do with it.
  552. */
  553. if(f->op == ONAME || f->op == OINDREG || f->op == OIND) {
  554. switch(ft) {
  555. default:
  556. a = AMOVW;
  557. break;
  558. case TFLOAT:
  559. a = AFMOVS;
  560. break;
  561. case TDOUBLE:
  562. a = AFMOVD;
  563. break;
  564. case TCHAR:
  565. a = AMOVB;
  566. break;
  567. case TUCHAR:
  568. a = AMOVBZ;
  569. break;
  570. case TSHORT:
  571. a = AMOVH;
  572. break;
  573. case TUSHORT:
  574. a = AMOVHZ;
  575. break;
  576. }
  577. regalloc(&nod, f, t);
  578. gins(a, f, &nod);
  579. gmove(&nod, t);
  580. regfree(&nod);
  581. return;
  582. }
  583. /*
  584. * a store --
  585. * put it into a register then
  586. * store it.
  587. */
  588. if(t->op == ONAME || t->op == OINDREG || t->op == OIND) {
  589. switch(tt) {
  590. default:
  591. a = AMOVW;
  592. break;
  593. case TUCHAR:
  594. case TCHAR:
  595. a = AMOVB;
  596. break;
  597. case TUSHORT:
  598. case TSHORT:
  599. a = AMOVH;
  600. break;
  601. case TFLOAT:
  602. a = AFMOVS;
  603. break;
  604. case TVLONG:
  605. case TDOUBLE:
  606. a = AFMOVD;
  607. break;
  608. }
  609. /*
  610. if(!typefd[ft] && vconst(f) == 0) {
  611. gins(a, f, t);
  612. return;
  613. }
  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. gins(AFCTIWZ, f, &nod);
  656. gins(AFMOVD, &nod, nodrat);
  657. regfree(&nod);
  658. nod = *nodrat;
  659. nod.xoffset += 4;
  660. gins(AMOVW, &nod, t);
  661. gmove(t, t);
  662. if(nrathole < SZ_DOUBLE)
  663. nrathole = SZ_DOUBLE;
  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. gins(AMOVW, nodconst(0x43300000L), &fxc0);
  768. gins(AMOVW, f, &fxc2);
  769. gins(AMOVW, &fxc0, nodrat);
  770. gins(AXOR, nodconst(0x80000000L), &fxc2);
  771. fxc1 = *nodrat;
  772. fxc1.xoffset += SZ_LONG;
  773. gins(AMOVW, &fxc2, &fxc1);
  774. regfree(&fxc2);
  775. regfree(&fxc0);
  776. regalloc(&nod, t, t); /* should be type float */
  777. gins(AFMOVD, nodrat, &nod);
  778. nodreg(&fxc1, t, NREG+FREGCVI);
  779. gins(AFSUB, &fxc1, &nod);
  780. a = AFMOVD;
  781. if(tt == TFLOAT)
  782. a = AFRSP;
  783. gins(a, &nod, t);
  784. regfree(&nod);
  785. if(nrathole < SZ_DOUBLE)
  786. nrathole = SZ_DOUBLE;
  787. if(ft == TULONG) {
  788. regalloc(&nod, t, Z);
  789. if(tt == TFLOAT) {
  790. gins(AFCMPU, t, Z);
  791. p->to.type = D_FREG;
  792. p->to.reg = FREGZERO;
  793. gins(ABGE, Z, Z);
  794. p1 = p;
  795. gins(AFMOVS, nodfconst(4294967296.), &nod);
  796. gins(AFADDS, &nod, t);
  797. } else {
  798. gins(AFCMPU, t, Z);
  799. p->to.type = D_FREG;
  800. p->to.reg = FREGZERO;
  801. gins(ABGE, Z, Z);
  802. p1 = p;
  803. gins(AFMOVD, nodfconst(4294967296.), &nod);
  804. gins(AFADD, &nod, t);
  805. }
  806. patch(p1, pc);
  807. regfree(&nod);
  808. }
  809. return;
  810. case TINT:
  811. case TUINT:
  812. case TLONG:
  813. case TULONG:
  814. case TIND:
  815. case TSHORT:
  816. case TUSHORT:
  817. a = AMOVBZ;
  818. break;
  819. case TCHAR:
  820. case TUCHAR:
  821. a = AMOVW;
  822. break;
  823. }
  824. break;
  825. }
  826. if(a == AGOK)
  827. diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type);
  828. if(a == AMOVW || a == AFMOVS || a == AFMOVD)
  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. int uns;
  851. uns = 0;
  852. et = TLONG;
  853. if(f1 != Z && f1->type != T)
  854. et = f1->type->etype;
  855. a = AGOK;
  856. switch(o) {
  857. case OAS:
  858. gmove(f1, t);
  859. return;
  860. case OASADD:
  861. case OADD:
  862. a = AADD;
  863. if(et == TFLOAT)
  864. a = AFADDS;
  865. else
  866. if(et == TDOUBLE || et == TVLONG)
  867. a = AFADD;
  868. break;
  869. case OASSUB:
  870. case OSUB:
  871. a = ASUB;
  872. if(et == TFLOAT)
  873. a = AFSUBS;
  874. else
  875. if(et == TDOUBLE || et == TVLONG)
  876. a = AFSUB;
  877. break;
  878. case OASOR:
  879. case OOR:
  880. a = AOR;
  881. break;
  882. case OASAND:
  883. case OAND:
  884. a = AAND;
  885. if(f1->op == OCONST)
  886. a = AANDCC;
  887. break;
  888. case OASXOR:
  889. case OXOR:
  890. a = AXOR;
  891. break;
  892. case OASLSHR:
  893. case OLSHR:
  894. a = ASRW;
  895. break;
  896. case OASASHR:
  897. case OASHR:
  898. a = ASRAW;
  899. break;
  900. case OASASHL:
  901. case OASHL:
  902. a = ASLW; /* BUG? */
  903. break;
  904. case OFUNC:
  905. a = ABL;
  906. break;
  907. case OASLMUL:
  908. case OLMUL:
  909. case OASMUL:
  910. case OMUL:
  911. if(et == TFLOAT) {
  912. a = AFMULS;
  913. break;
  914. } else
  915. if(et == TDOUBLE || et == TVLONG) {
  916. a = AFMUL;
  917. break;
  918. }
  919. a = AMULLW;
  920. break;
  921. case OASDIV:
  922. case ODIV:
  923. if(et == TFLOAT) {
  924. a = AFDIVS;
  925. break;
  926. } else
  927. if(et == TDOUBLE || et == TVLONG) {
  928. a = AFDIV;
  929. break;
  930. }
  931. a = ADIVW;
  932. break;
  933. case OASMOD:
  934. case OMOD:
  935. a = AREM;
  936. break;
  937. case OASLMOD:
  938. case OLMOD:
  939. a = AREMU;
  940. break;
  941. case OASLDIV:
  942. case OLDIV:
  943. a = ADIVWU;
  944. break;
  945. case OCOM:
  946. a = ANOR;
  947. break;
  948. case ONEG:
  949. a = ANEG;
  950. if(et == TFLOAT || et == TDOUBLE || et == TVLONG)
  951. a = AFNEG;
  952. break;
  953. case OEQ:
  954. a = ABEQ;
  955. goto cmp;
  956. case ONE:
  957. a = ABNE;
  958. goto cmp;
  959. case OLT:
  960. a = ABLT;
  961. goto cmp;
  962. case OLE:
  963. a = ABLE;
  964. goto cmp;
  965. case OGE:
  966. a = ABGE;
  967. goto cmp;
  968. case OGT:
  969. a = ABGT;
  970. goto cmp;
  971. case OLO:
  972. a = ABLT;
  973. goto cmpu;
  974. case OLS:
  975. a = ABLE;
  976. goto cmpu;
  977. case OHS:
  978. a = ABGE;
  979. goto cmpu;
  980. case OHI:
  981. a = ABGT;
  982. goto cmpu;
  983. cmpu:
  984. uns = 1;
  985. cmp:
  986. nextpc();
  987. p->as = uns? ACMPU: ACMP;
  988. if(et == TFLOAT)
  989. p->as = AFCMPU;
  990. else
  991. if(et == TDOUBLE || et == TVLONG)
  992. p->as = AFCMPU;
  993. if(f1 != Z)
  994. naddr(f1, &p->from);
  995. if(t != Z)
  996. naddr(t, &p->to);
  997. if(f1 == Z || t == Z || f2 != Z)
  998. diag(Z, "bad cmp in gopcode %O", o);
  999. if(debug['g'])
  1000. print("%P\n", p);
  1001. f1 = Z;
  1002. f2 = Z;
  1003. t = Z;
  1004. break;
  1005. }
  1006. if(a == AGOK)
  1007. diag(Z, "bad in gopcode %O", o);
  1008. nextpc();
  1009. p->as = a;
  1010. if(f1 != Z)
  1011. naddr(f1, &p->from);
  1012. if(f2 != Z) {
  1013. naddr(f2, &ta);
  1014. p->reg = ta.reg;
  1015. if(ta.type == D_CONST && ta.offset == 0)
  1016. diag(Z, "REGZERO in gopcode %O", o);/*p->reg = REGZERO;*/
  1017. }
  1018. if(t != Z)
  1019. naddr(t, &p->to);
  1020. if(debug['g'])
  1021. print("%P\n", p);
  1022. }
  1023. samaddr(Node *f, Node *t)
  1024. {
  1025. if(f->op != t->op)
  1026. return 0;
  1027. switch(f->op) {
  1028. case OREGISTER:
  1029. if(f->reg != t->reg)
  1030. break;
  1031. return 1;
  1032. }
  1033. return 0;
  1034. }
  1035. void
  1036. gbranch(int o)
  1037. {
  1038. int a;
  1039. a = AGOK;
  1040. switch(o) {
  1041. case ORETURN:
  1042. a = ARETURN;
  1043. break;
  1044. case OGOTO:
  1045. a = ABR;
  1046. break;
  1047. }
  1048. nextpc();
  1049. if(a == AGOK) {
  1050. diag(Z, "bad in gbranch %O", o);
  1051. nextpc();
  1052. }
  1053. p->as = a;
  1054. }
  1055. void
  1056. patch(Prog *op, long pc)
  1057. {
  1058. op->to.offset = pc;
  1059. op->to.type = D_BRANCH;
  1060. }
  1061. void
  1062. gpseudo(int a, Sym *s, Node *n)
  1063. {
  1064. nextpc();
  1065. p->as = a;
  1066. p->from.type = D_OREG;
  1067. p->from.sym = s;
  1068. p->from.name = D_EXTERN;
  1069. if(s->class == CSTATIC)
  1070. p->from.name = D_STATIC;
  1071. naddr(n, &p->to);
  1072. if(a == ADATA || a == AGLOBL)
  1073. pc--;
  1074. }
  1075. int
  1076. sval(long v)
  1077. {
  1078. if(v >= -(1<<15) && v < (1<<15))
  1079. return 1;
  1080. return 0;
  1081. }
  1082. int
  1083. sconst(Node *n)
  1084. {
  1085. vlong vv;
  1086. if(n->op == OCONST) {
  1087. if(!typefd[n->type->etype]) {
  1088. vv = n->vconst;
  1089. if(vv >= -(1LL<<15) && vv < (1LL<<15))
  1090. return 1;
  1091. }
  1092. }
  1093. return 0;
  1094. }
  1095. int
  1096. uconst(Node *n)
  1097. {
  1098. vlong vv;
  1099. if(n->op == OCONST) {
  1100. if(!typefd[n->type->etype]) {
  1101. vv = n->vconst;
  1102. if(vv >= 0 && vv < (1LL<<16))
  1103. return 1;
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. long
  1109. exreg(Type *t)
  1110. {
  1111. long o;
  1112. if(typechlp[t->etype]) {
  1113. if(exregoffset <= 3)
  1114. return 0;
  1115. o = exregoffset;
  1116. exregoffset--;
  1117. return o;
  1118. }
  1119. if(typefd[t->etype]) {
  1120. if(exfregoffset <= 16)
  1121. return 0;
  1122. o = exfregoffset + NREG;
  1123. exfregoffset--;
  1124. return o;
  1125. }
  1126. return 0;
  1127. }
  1128. schar ewidth[NTYPE] =
  1129. {
  1130. -1, /* [TXXX] */
  1131. SZ_CHAR, /* [TCHAR] */
  1132. SZ_CHAR, /* [TUCHAR] */
  1133. SZ_SHORT, /* [TSHORT] */
  1134. SZ_SHORT, /* [TUSHORT] */
  1135. SZ_INT, /* [TINT] */
  1136. SZ_INT, /* [TUINT] */
  1137. SZ_LONG, /* [TLONG] */
  1138. SZ_LONG, /* [TULONG] */
  1139. SZ_VLONG, /* [TVLONG] */
  1140. SZ_VLONG, /* [TUVLONG] */
  1141. SZ_FLOAT, /* [TFLOAT] */
  1142. SZ_DOUBLE, /* [TDOUBLE] */
  1143. SZ_IND, /* [TIND] */
  1144. 0, /* [TFUNC] */
  1145. -1, /* [TARRAY] */
  1146. 0, /* [TVOID] */
  1147. -1, /* [TSTRUCT] */
  1148. -1, /* [TUNION] */
  1149. SZ_INT, /* [TENUM] */
  1150. };
  1151. long ncast[NTYPE] =
  1152. {
  1153. 0, /* [TXXX] */
  1154. BCHAR|BUCHAR, /* [TCHAR] */
  1155. BCHAR|BUCHAR, /* [TUCHAR] */
  1156. BSHORT|BUSHORT, /* [TSHORT] */
  1157. BSHORT|BUSHORT, /* [TUSHORT] */
  1158. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1159. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1160. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1161. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1162. BVLONG|BUVLONG, /* [TVLONG] */
  1163. BVLONG|BUVLONG, /* [TUVLONG] */
  1164. BFLOAT, /* [TFLOAT] */
  1165. BDOUBLE, /* [TDOUBLE] */
  1166. BLONG|BULONG|BIND, /* [TIND] */
  1167. 0, /* [TFUNC] */
  1168. 0, /* [TARRAY] */
  1169. 0, /* [TVOID] */
  1170. BSTRUCT, /* [TSTRUCT] */
  1171. BUNION, /* [TUNION] */
  1172. 0, /* [TENUM] */
  1173. };