txt.c 18 KB

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