txt.c 23 KB

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