1
0

txt.c 22 KB

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