txt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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 int 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. a = AMOVBU;
  616. break;
  617. case TCHAR:
  618. a = AMOVB;
  619. break;
  620. case TUSHORT:
  621. a = AMOVHU;
  622. break;
  623. case TSHORT:
  624. a = AMOVH;
  625. break;
  626. case TFLOAT:
  627. a = AMOVF;
  628. break;
  629. case TDOUBLE:
  630. a = AMOVD;
  631. break;
  632. }
  633. if(!typefd[ft] && vconst(f) == 0) {
  634. gins(a, f, t);
  635. return;
  636. }
  637. if(ft == tt)
  638. regalloc(&nod, t, f);
  639. else
  640. regalloc(&nod, t, Z);
  641. gmove(f, &nod);
  642. gins(a, &nod, t);
  643. regfree(&nod);
  644. return;
  645. }
  646. /*
  647. * type x type cross table
  648. */
  649. a = AGOK;
  650. switch(ft) {
  651. case TDOUBLE:
  652. case TVLONG:
  653. case TFLOAT:
  654. switch(tt) {
  655. case TDOUBLE:
  656. case TVLONG:
  657. a = AMOVD;
  658. if(ft == TFLOAT)
  659. a = AMOVFD;
  660. break;
  661. case TFLOAT:
  662. a = AMOVDF;
  663. if(ft == TFLOAT)
  664. a = AMOVF;
  665. break;
  666. case TINT:
  667. case TUINT:
  668. case TLONG:
  669. case TULONG:
  670. case TIND:
  671. case TSHORT:
  672. case TUSHORT:
  673. case TCHAR:
  674. case TUCHAR:
  675. if(fproundflg) {
  676. /* convert f, t */
  677. regalloc(&nod, f, Z);
  678. gins(AMOVDW, f, &nod);
  679. if(ft == TFLOAT)
  680. p->as = AMOVFW;
  681. gins(AMOVW, &nod, t);
  682. regfree(&nod);
  683. gins(AMOVW, t, t);
  684. return;
  685. }
  686. regalloc(&nod1, &regnode, Z);
  687. regalloc(&nod2, &regnode, Z);
  688. /* movw fcr, rx */
  689. gins(AMOVW, Z, &nod1);
  690. p->from.type = D_FCREG;
  691. p->from.reg = 31;
  692. /* nop */
  693. gins(ANOR, nodconst(0), nodconst(0));
  694. p->to.type = D_REG;
  695. p->to.reg = 0;
  696. /* nop */
  697. gins(ANOR, nodconst(0), nodconst(0));
  698. p->to.type = D_REG;
  699. p->to.reg = 0;
  700. /* or $3, rx, ry */
  701. gins(AOR, nodconst(3), &nod2);
  702. p->reg = nod1.reg;
  703. /* xor $2, ry */
  704. gins(AXOR, nodconst(2), &nod2);
  705. /* movw ry, fcr */
  706. gins(AMOVW, &nod2, Z);
  707. p->to.type = D_FCREG;
  708. p->to.reg = 31;
  709. /* nop */
  710. gins(ANOR, nodconst(0), nodconst(0));
  711. p->to.type = D_REG;
  712. p->to.reg = 0;
  713. /* nop */
  714. gins(ANOR, nodconst(0), nodconst(0));
  715. p->to.type = D_REG;
  716. p->to.reg = 0;
  717. /* convert f, t */
  718. regalloc(&nod, f, Z);
  719. gins(AMOVDW, f, &nod);
  720. if(ft == TFLOAT)
  721. p->as = AMOVFW;
  722. gins(AMOVW, &nod, t);
  723. regfree(&nod);
  724. gins(AMOVW, t, t);
  725. /* movw rx, fcr */
  726. gins(AMOVW, &nod1, Z);
  727. p->to.type = D_FCREG;
  728. p->to.reg = 31;
  729. /* nop */
  730. gins(ANOR, nodconst(0), nodconst(0));
  731. p->to.type = D_REG;
  732. p->to.reg = 0;
  733. /* nop */
  734. gins(ANOR, nodconst(0), nodconst(0));
  735. p->to.type = D_REG;
  736. p->to.reg = 0;
  737. regfree(&nod1);
  738. regfree(&nod2);
  739. return;
  740. }
  741. break;
  742. case TINT:
  743. case TUINT:
  744. case TLONG:
  745. case TULONG:
  746. case TIND:
  747. switch(tt) {
  748. case TDOUBLE:
  749. case TVLONG:
  750. gins(AMOVW, f, t);
  751. gins(AMOVWD, t, t);
  752. if(ft == TULONG || ft == TUINT) {
  753. regalloc(&nod, t, Z);
  754. gins(ACMPGED, t, Z);
  755. p->reg = FREGZERO;
  756. gins(ABFPT, Z, Z);
  757. p1 = p;
  758. gins(AMOVD, nodfconst(4294967296.), &nod);
  759. gins(AADDD, &nod, t);
  760. patch(p1, pc);
  761. regfree(&nod);
  762. }
  763. return;
  764. case TFLOAT:
  765. gins(AMOVW, f, t);
  766. gins(AMOVWF, t, t);
  767. if(ft == TULONG || ft == TUINT) {
  768. regalloc(&nod, t, Z);
  769. gins(ACMPGEF, t, Z);
  770. p->reg = FREGZERO;
  771. gins(ABFPT, Z, Z);
  772. p1 = p;
  773. gins(AMOVF, nodfconst(4294967296.), &nod);
  774. gins(AADDF, &nod, t);
  775. patch(p1, pc);
  776. regfree(&nod);
  777. }
  778. return;
  779. case TINT:
  780. case TUINT:
  781. case TLONG:
  782. case TULONG:
  783. case TIND:
  784. case TSHORT:
  785. case TUSHORT:
  786. case TCHAR:
  787. case TUCHAR:
  788. a = AMOVW;
  789. break;
  790. }
  791. break;
  792. case TSHORT:
  793. switch(tt) {
  794. case TDOUBLE:
  795. case TVLONG:
  796. regalloc(&nod, f, Z);
  797. gins(AMOVH, f, &nod);
  798. gins(AMOVW, &nod, t);
  799. gins(AMOVWD, t, t);
  800. regfree(&nod);
  801. return;
  802. case TFLOAT:
  803. regalloc(&nod, f, Z);
  804. gins(AMOVH, f, &nod);
  805. gins(AMOVW, &nod, t);
  806. gins(AMOVWF, t, t);
  807. regfree(&nod);
  808. return;
  809. case TINT:
  810. case TUINT:
  811. case TLONG:
  812. case TULONG:
  813. case TIND:
  814. a = AMOVH;
  815. break;
  816. case TSHORT:
  817. case TUSHORT:
  818. case TCHAR:
  819. case TUCHAR:
  820. a = AMOVW;
  821. break;
  822. }
  823. break;
  824. case TUSHORT:
  825. switch(tt) {
  826. case TDOUBLE:
  827. case TVLONG:
  828. regalloc(&nod, f, Z);
  829. gins(AMOVHU, f, &nod);
  830. gins(AMOVW, &nod, t);
  831. gins(AMOVWD, t, t);
  832. regfree(&nod);
  833. return;
  834. case TFLOAT:
  835. regalloc(&nod, f, Z);
  836. gins(AMOVHU, f, &nod);
  837. gins(AMOVW, &nod, t);
  838. gins(AMOVWF, t, t);
  839. regfree(&nod);
  840. return;
  841. case TINT:
  842. case TUINT:
  843. case TLONG:
  844. case TULONG:
  845. case TIND:
  846. a = AMOVHU;
  847. break;
  848. case TSHORT:
  849. case TUSHORT:
  850. case TCHAR:
  851. case TUCHAR:
  852. a = AMOVW;
  853. break;
  854. }
  855. break;
  856. case TCHAR:
  857. switch(tt) {
  858. case TDOUBLE:
  859. case TVLONG:
  860. regalloc(&nod, f, Z);
  861. gins(AMOVB, f, &nod);
  862. gins(AMOVW, &nod, t);
  863. gins(AMOVWD, t, t);
  864. regfree(&nod);
  865. return;
  866. case TFLOAT:
  867. regalloc(&nod, f, Z);
  868. gins(AMOVB, f, &nod);
  869. gins(AMOVW, &nod, t);
  870. gins(AMOVWF, t, t);
  871. regfree(&nod);
  872. return;
  873. case TINT:
  874. case TUINT:
  875. case TLONG:
  876. case TULONG:
  877. case TIND:
  878. case TSHORT:
  879. case TUSHORT:
  880. a = AMOVB;
  881. break;
  882. case TCHAR:
  883. case TUCHAR:
  884. a = AMOVW;
  885. break;
  886. }
  887. break;
  888. case TUCHAR:
  889. switch(tt) {
  890. case TDOUBLE:
  891. case TVLONG:
  892. regalloc(&nod, f, Z);
  893. gins(AMOVBU, f, &nod);
  894. gins(AMOVW, &nod, t);
  895. gins(AMOVWD, t, t);
  896. regfree(&nod);
  897. return;
  898. case TFLOAT:
  899. regalloc(&nod, f, Z);
  900. gins(AMOVBU, f, &nod);
  901. gins(AMOVW, &nod, t);
  902. gins(AMOVWF, t, t);
  903. regfree(&nod);
  904. return;
  905. case TINT:
  906. case TUINT:
  907. case TLONG:
  908. case TULONG:
  909. case TIND:
  910. case TSHORT:
  911. case TUSHORT:
  912. a = AMOVBU;
  913. break;
  914. case TCHAR:
  915. case TUCHAR:
  916. a = AMOVW;
  917. break;
  918. }
  919. break;
  920. }
  921. if(a == AGOK)
  922. diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type);
  923. if(a == AMOVW || a == AMOVF || a == AMOVD)
  924. if(samaddr(f, t))
  925. return;
  926. gins(a, f, t);
  927. }
  928. void
  929. gins(int a, Node *f, Node *t)
  930. {
  931. nextpc();
  932. p->as = a;
  933. if(f != Z)
  934. naddr(f, &p->from);
  935. if(t != Z)
  936. naddr(t, &p->to);
  937. if(debug['g'])
  938. print("%P\n", p);
  939. }
  940. void
  941. gopcode(int o, Node *f1, Node *f2, Node *t)
  942. {
  943. int a, et;
  944. Adr ta;
  945. et = TLONG;
  946. if(f1 != Z && f1->type != T)
  947. et = f1->type->etype;
  948. a = AGOK;
  949. switch(o) {
  950. case OAS:
  951. gmove(f1, t);
  952. return;
  953. case OASADD:
  954. case OADD:
  955. a = AADDU;
  956. if(et == TFLOAT)
  957. a = AADDF;
  958. else
  959. if(et == TDOUBLE || et == TVLONG)
  960. a = AADDD;
  961. break;
  962. case OASSUB:
  963. case OSUB:
  964. a = ASUBU;
  965. if(et == TFLOAT)
  966. a = ASUBF;
  967. else
  968. if(et == TDOUBLE || et == TVLONG)
  969. a = ASUBD;
  970. break;
  971. case OASOR:
  972. case OOR:
  973. a = AOR;
  974. break;
  975. case OASAND:
  976. case OAND:
  977. a = AAND;
  978. break;
  979. case OASXOR:
  980. case OXOR:
  981. a = AXOR;
  982. break;
  983. case OASLSHR:
  984. case OLSHR:
  985. a = ASRL;
  986. break;
  987. case OASASHR:
  988. case OASHR:
  989. a = ASRA;
  990. break;
  991. case OASASHL:
  992. case OASHL:
  993. a = ASLL;
  994. break;
  995. case OFUNC:
  996. a = AJAL;
  997. break;
  998. case OCOND:
  999. a = ASGTU;
  1000. break;
  1001. case OCOMMA:
  1002. a = ASGT;
  1003. break;
  1004. case OASMUL:
  1005. case OMUL:
  1006. if(et == TFLOAT) {
  1007. a = AMULF;
  1008. break;
  1009. } else
  1010. if(et == TDOUBLE || et == TVLONG) {
  1011. a = AMULD;
  1012. break;
  1013. }
  1014. a = AMUL;
  1015. goto muldiv;
  1016. case OASDIV:
  1017. case ODIV:
  1018. if(et == TFLOAT) {
  1019. a = ADIVF;
  1020. break;
  1021. } else
  1022. if(et == TDOUBLE || et == TVLONG) {
  1023. a = ADIVD;
  1024. break;
  1025. }
  1026. a = ADIV;
  1027. goto muldiv;
  1028. case OASMOD:
  1029. case OMOD:
  1030. a = ADIV;
  1031. o = OMOD;
  1032. goto muldiv;
  1033. case OASLMUL:
  1034. case OLMUL:
  1035. a = AMULU;
  1036. goto muldiv;
  1037. case OASLMOD:
  1038. case OLMOD:
  1039. a = ADIVU;
  1040. o = OMOD;
  1041. goto muldiv;
  1042. case OASLDIV:
  1043. case OLDIV:
  1044. a = ADIVU;
  1045. goto muldiv;
  1046. muldiv:
  1047. nextpc();
  1048. naddr(f1, &p->from);
  1049. if(f2 == Z)
  1050. raddr(t, p);
  1051. else
  1052. raddr(f2, p);
  1053. p->as = a;
  1054. if(debug['g'])
  1055. print("%P\n", p);
  1056. nextpc();
  1057. p->as = AMOVW;
  1058. a = D_LO;
  1059. if(o == OMOD)
  1060. a = D_HI;
  1061. p->from.type = a;
  1062. naddr(t, &p->to);
  1063. if(debug['g'])
  1064. print("%P\n", p);
  1065. return;
  1066. case OEQ:
  1067. if(!typefd[et]) {
  1068. a = ABEQ;
  1069. break;
  1070. }
  1071. case ONE:
  1072. if(!typefd[et]) {
  1073. a = ABNE;
  1074. break;
  1075. }
  1076. case OLT:
  1077. case OLE:
  1078. case OGE:
  1079. case OGT:
  1080. if(typefd[et]) {
  1081. nextpc();
  1082. if(et == TFLOAT) {
  1083. a = ACMPGTF;
  1084. if(o == OEQ || o == ONE)
  1085. a = ACMPEQF;
  1086. else
  1087. if(o == OLT || o == OGE)
  1088. a = ACMPGEF;
  1089. } else {
  1090. a = ACMPGTD;
  1091. if(o == OEQ || o == ONE)
  1092. a = ACMPEQD;
  1093. else
  1094. if(o == OLT || o == OGE)
  1095. a = ACMPGED;
  1096. }
  1097. p->as = a;
  1098. naddr(f1, &p->from);
  1099. raddr(f2, p);
  1100. if(debug['g'])
  1101. print("%P\n", p);
  1102. nextpc();
  1103. a = ABFPF;
  1104. if(o == OEQ || o == OGE || o == OGT)
  1105. a = ABFPT;
  1106. p->as = a;
  1107. if(debug['g'])
  1108. print("%P\n", p);
  1109. return;
  1110. }
  1111. if(vconst(f1) == 0 || vconst(f2) == 0) {
  1112. if(vconst(f1) == 0) {
  1113. o = invrel[relindex(o)];
  1114. f1 = f2;
  1115. }
  1116. switch(o) {
  1117. case OLT:
  1118. a = ABLTZ;
  1119. break;
  1120. case OLE:
  1121. a = ABLEZ;
  1122. break;
  1123. case OGE:
  1124. a = ABGEZ;
  1125. break;
  1126. case OGT:
  1127. a = ABGTZ;
  1128. break;
  1129. }
  1130. f2 = Z;
  1131. break;
  1132. }
  1133. case OLO:
  1134. case OLS:
  1135. case OHS:
  1136. case OHI:
  1137. nextpc();
  1138. if(o == OLE || o == OGT || o == OLS || o == OHI) {
  1139. naddr(f1, &p->from);
  1140. raddr(f2, p);
  1141. } else {
  1142. naddr(f2, &p->from);
  1143. raddr(f1, p);
  1144. }
  1145. naddr(&regnode, &p->to);
  1146. p->to.reg = tmpreg();
  1147. a = ASGT;
  1148. if(o == OLO || o == OLS || o == OHS || o == OHI)
  1149. a = ASGTU;
  1150. p->as = a;
  1151. if(debug['g'])
  1152. print("%P\n", p);
  1153. nextpc();
  1154. naddr(&regnode, &p->from);
  1155. p->from.reg = tmpreg();
  1156. a = ABEQ;
  1157. if(o == OLT || o == OGT || o == OLO || o == OHI)
  1158. a = ABNE;
  1159. p->as = a;
  1160. if(debug['g'])
  1161. print("%P\n", p);
  1162. return;
  1163. }
  1164. if(a == AGOK)
  1165. diag(Z, "bad in gopcode %O", o);
  1166. nextpc();
  1167. p->as = a;
  1168. if(f1 != Z)
  1169. naddr(f1, &p->from);
  1170. if(f2 != Z) {
  1171. naddr(f2, &ta);
  1172. p->reg = ta.reg;
  1173. if(ta.type == D_CONST && ta.offset == 0)
  1174. p->reg = REGZERO;
  1175. }
  1176. if(t != Z)
  1177. naddr(t, &p->to);
  1178. if(debug['g'])
  1179. print("%P\n", p);
  1180. }
  1181. int
  1182. samaddr(Node *f, Node *t)
  1183. {
  1184. if(f->op != t->op)
  1185. return 0;
  1186. switch(f->op) {
  1187. case OREGISTER:
  1188. if(f->reg != t->reg)
  1189. break;
  1190. return 1;
  1191. }
  1192. return 0;
  1193. }
  1194. void
  1195. gbranch(int o)
  1196. {
  1197. int a;
  1198. a = AGOK;
  1199. switch(o) {
  1200. case ORETURN:
  1201. a = ARET;
  1202. break;
  1203. case OGOTO:
  1204. a = AJMP;
  1205. break;
  1206. }
  1207. nextpc();
  1208. if(a == AGOK) {
  1209. diag(Z, "bad in gbranch %O", o);
  1210. nextpc();
  1211. }
  1212. p->as = a;
  1213. }
  1214. void
  1215. patch(Prog *op, long pc)
  1216. {
  1217. op->to.offset = pc;
  1218. op->to.type = D_BRANCH;
  1219. }
  1220. void
  1221. gpseudo(int a, Sym *s, Node *n)
  1222. {
  1223. nextpc();
  1224. p->as = a;
  1225. p->from.type = D_OREG;
  1226. p->from.sym = s;
  1227. if(a == ATEXT)
  1228. p->reg = (profileflg ? 0 : NOPROF);
  1229. p->from.name = D_EXTERN;
  1230. if(s->class == CSTATIC)
  1231. p->from.name = D_STATIC;
  1232. naddr(n, &p->to);
  1233. if(a == ADATA || a == AGLOBL)
  1234. pc--;
  1235. }
  1236. int
  1237. sconst(Node *n)
  1238. {
  1239. vlong vv;
  1240. if(n->op == OCONST) {
  1241. if(!typefd[n->type->etype]) {
  1242. vv = n->vconst;
  1243. if(vv >= (vlong)(-32766) && vv < (vlong)32766)
  1244. return 1;
  1245. }
  1246. }
  1247. return 0;
  1248. }
  1249. int
  1250. sval(long v)
  1251. {
  1252. if(v >= -32766L && v < 32766L)
  1253. return 1;
  1254. return 0;
  1255. }
  1256. long
  1257. exreg(Type *t)
  1258. {
  1259. long o;
  1260. if(typechlp[t->etype]) {
  1261. if(exregoffset <= 16)
  1262. return 0;
  1263. o = exregoffset;
  1264. exregoffset--;
  1265. return o;
  1266. }
  1267. if(typefd[t->etype]) {
  1268. if(exfregoffset <= 16)
  1269. return 0;
  1270. o = exfregoffset + NREG;
  1271. exfregoffset--;
  1272. return o;
  1273. }
  1274. return 0;
  1275. }
  1276. schar ewidth[NTYPE] =
  1277. {
  1278. -1, /* [TXXX] */
  1279. SZ_CHAR, /* [TCHAR] */
  1280. SZ_CHAR, /* [TUCHAR] */
  1281. SZ_SHORT, /* [TSHORT] */
  1282. SZ_SHORT, /* [TUSHORT] */
  1283. SZ_INT, /* [TINT] */
  1284. SZ_INT, /* [TUINT] */
  1285. SZ_LONG, /* [TLONG] */
  1286. SZ_LONG, /* [TULONG] */
  1287. SZ_VLONG, /* [TVLONG] */
  1288. SZ_VLONG, /* [TUVLONG] */
  1289. SZ_FLOAT, /* [TFLOAT] */
  1290. SZ_DOUBLE, /* [TDOUBLE] */
  1291. SZ_IND, /* [TIND] */
  1292. 0, /* [TFUNC] */
  1293. -1, /* [TARRAY] */
  1294. 0, /* [TVOID] */
  1295. -1, /* [TSTRUCT] */
  1296. -1, /* [TUNION] */
  1297. SZ_INT, /* [TENUM] */
  1298. };
  1299. long ncast[NTYPE] =
  1300. {
  1301. 0, /* [TXXX] */
  1302. BCHAR|BUCHAR, /* [TCHAR] */
  1303. BCHAR|BUCHAR, /* [TUCHAR] */
  1304. BSHORT|BUSHORT, /* [TSHORT] */
  1305. BSHORT|BUSHORT, /* [TUSHORT] */
  1306. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1307. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1308. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1309. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1310. BVLONG|BUVLONG, /* [TVLONG] */
  1311. BVLONG|BUVLONG, /* [TUVLONG] */
  1312. BFLOAT, /* [TFLOAT] */
  1313. BDOUBLE, /* [TDOUBLE] */
  1314. BLONG|BULONG|BIND, /* [TIND] */
  1315. 0, /* [TFUNC] */
  1316. 0, /* [TARRAY] */
  1317. 0, /* [TVOID] */
  1318. BSTRUCT, /* [TSTRUCT] */
  1319. BUNION, /* [TUNION] */
  1320. 0, /* [TENUM] */
  1321. };