txt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. #include "gc.h"
  2. static char resvreg[nelem(reg)];
  3. void
  4. ginit(void)
  5. {
  6. Type *t;
  7. thechar = '5';
  8. thestring = "arm";
  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. zprog.scond = 0xE;
  30. regnode.op = OREGISTER;
  31. regnode.class = CEXREG;
  32. regnode.reg = REGTMP;
  33. regnode.complex = 0;
  34. regnode.addable = 11;
  35. regnode.type = types[TLONG];
  36. constnode.op = OCONST;
  37. constnode.class = CXXX;
  38. constnode.complex = 0;
  39. constnode.addable = 20;
  40. constnode.type = types[TLONG];
  41. fconstnode.op = OCONST;
  42. fconstnode.class = CXXX;
  43. fconstnode.complex = 0;
  44. fconstnode.addable = 20;
  45. fconstnode.type = types[TDOUBLE];
  46. nodsafe = new(ONAME, Z, Z);
  47. nodsafe->sym = slookup(".safe");
  48. nodsafe->type = types[TINT];
  49. nodsafe->etype = types[TINT]->etype;
  50. nodsafe->class = CAUTO;
  51. complex(nodsafe);
  52. t = typ(TARRAY, types[TCHAR]);
  53. symrathole = slookup(".rathole");
  54. symrathole->class = CGLOBL;
  55. symrathole->type = t;
  56. nodrat = new(ONAME, Z, Z);
  57. nodrat->sym = symrathole;
  58. nodrat->type = types[TIND];
  59. nodrat->etype = TVOID;
  60. nodrat->class = CGLOBL;
  61. complex(nodrat);
  62. nodrat->type = t;
  63. nodret = new(ONAME, Z, Z);
  64. nodret->sym = slookup(".ret");
  65. nodret->type = types[TIND];
  66. nodret->etype = TIND;
  67. nodret->class = CPARAM;
  68. nodret = new(OIND, nodret, Z);
  69. complex(nodret);
  70. com64init();
  71. memset(reg, 0, sizeof(reg));
  72. /* don't allocate */
  73. reg[REGTMP] = 1;
  74. reg[REGSB] = 1;
  75. reg[REGSP] = 1;
  76. reg[REGLINK] = 1;
  77. reg[REGPC] = 1;
  78. /* keep two external registers */
  79. reg[REGEXT] = 1;
  80. reg[REGEXT-1] = 1;
  81. memmove(resvreg, reg, sizeof(reg));
  82. }
  83. void
  84. gclean(void)
  85. {
  86. int i;
  87. Sym *s;
  88. for(i=0; i<NREG; i++)
  89. if(reg[i] && !resvreg[i])
  90. diag(Z, "reg %d left allocated", i);
  91. for(i=NREG; i<NREG+NFREG; i++)
  92. if(reg[i] && !resvreg[i])
  93. diag(Z, "freg %d left allocated", i-NREG);
  94. while(mnstring)
  95. outstring("", 1L);
  96. symstring->type->width = nstring;
  97. symrathole->type->width = nrathole;
  98. for(i=0; i<NHASH; i++)
  99. for(s = hash[i]; s != S; s = s->link) {
  100. if(s->type == T)
  101. continue;
  102. if(s->type->width == 0)
  103. continue;
  104. if(s->class != CGLOBL && s->class != CSTATIC)
  105. continue;
  106. if(s->type == types[TENUM])
  107. continue;
  108. gpseudo(AGLOBL, s, nodconst(s->type->width));
  109. }
  110. nextpc();
  111. p->as = AEND;
  112. outcode();
  113. }
  114. void
  115. nextpc(void)
  116. {
  117. p = alloc(sizeof(*p));
  118. *p = zprog;
  119. p->lineno = nearln;
  120. pc++;
  121. if(firstp == P) {
  122. firstp = p;
  123. lastp = p;
  124. return;
  125. }
  126. lastp->link = p;
  127. lastp = p;
  128. }
  129. void
  130. gargs(Node *n, Node *tn1, Node *tn2)
  131. {
  132. long regs;
  133. Node fnxargs[20], *fnxp;
  134. regs = cursafe;
  135. fnxp = fnxargs;
  136. garg1(n, tn1, tn2, 0, &fnxp); /* compile fns to temps */
  137. curarg = 0;
  138. fnxp = fnxargs;
  139. garg1(n, tn1, tn2, 1, &fnxp); /* compile normal args and temps */
  140. cursafe = regs;
  141. }
  142. void
  143. garg1(Node *n, Node *tn1, Node *tn2, int f, Node **fnxp)
  144. {
  145. Node nod;
  146. if(n == Z)
  147. return;
  148. if(n->op == OLIST) {
  149. garg1(n->left, tn1, tn2, f, fnxp);
  150. garg1(n->right, tn1, tn2, f, fnxp);
  151. return;
  152. }
  153. if(f == 0) {
  154. if(n->complex >= FNX) {
  155. regsalloc(*fnxp, n);
  156. nod = znode;
  157. nod.op = OAS;
  158. nod.left = *fnxp;
  159. nod.right = n;
  160. nod.type = n->type;
  161. cgen(&nod, Z);
  162. (*fnxp)++;
  163. }
  164. return;
  165. }
  166. if(typesuv[n->type->etype]) {
  167. regaalloc(tn2, n);
  168. if(n->complex >= FNX) {
  169. sugen(*fnxp, tn2, n->type->width);
  170. (*fnxp)++;
  171. } else
  172. sugen(n, tn2, n->type->width);
  173. return;
  174. }
  175. if(REGARG >= 0 && curarg == 0 && typechlp[n->type->etype]) {
  176. regaalloc1(tn1, n);
  177. if(n->complex >= FNX) {
  178. cgen(*fnxp, tn1);
  179. (*fnxp)++;
  180. } else
  181. cgen(n, tn1);
  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 && resvreg[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+NFREG)
  278. goto out;
  279. }
  280. j = 0*2 + NREG;
  281. for(i=NREG; i<NREG+NFREG; i++) {
  282. if(j >= NREG+NFREG)
  283. j = NREG;
  284. if(reg[j] == 0) {
  285. i = j;
  286. goto out;
  287. }
  288. j++;
  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(R0ISZERO && 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. gmovm(Node *f, Node *t, int w)
  494. {
  495. gins(AMOVM, f, t);
  496. p->scond |= C_UBIT;
  497. if(w)
  498. p->scond |= C_WBIT;
  499. }
  500. void
  501. gmove(Node *f, Node *t)
  502. {
  503. int ft, tt, a;
  504. Node nod, nod1;
  505. Prog *p1;
  506. ft = f->type->etype;
  507. tt = t->type->etype;
  508. if(ft == TDOUBLE && f->op == OCONST) {
  509. }
  510. if(ft == TFLOAT && f->op == OCONST) {
  511. }
  512. /*
  513. * a load --
  514. * put it into a register then
  515. * worry what to do with it.
  516. */
  517. if(f->op == ONAME || f->op == OINDREG || f->op == OIND) {
  518. switch(ft) {
  519. default:
  520. a = AMOVW;
  521. break;
  522. case TFLOAT:
  523. a = AMOVF;
  524. break;
  525. case TDOUBLE:
  526. a = AMOVD;
  527. break;
  528. case TCHAR:
  529. a = AMOVB;
  530. break;
  531. case TUCHAR:
  532. a = AMOVBU;
  533. break;
  534. case TSHORT:
  535. a = AMOVH;
  536. break;
  537. case TUSHORT:
  538. a = AMOVHU;
  539. break;
  540. }
  541. if(typechlp[ft] && typeilp[tt])
  542. regalloc(&nod, t, t);
  543. else
  544. regalloc(&nod, f, t);
  545. gins(a, f, &nod);
  546. gmove(&nod, t);
  547. regfree(&nod);
  548. return;
  549. }
  550. /*
  551. * a store --
  552. * put it into a register then
  553. * store it.
  554. */
  555. if(t->op == ONAME || t->op == OINDREG || t->op == OIND) {
  556. switch(tt) {
  557. default:
  558. a = AMOVW;
  559. break;
  560. case TUCHAR:
  561. a = AMOVBU;
  562. break;
  563. case TCHAR:
  564. a = AMOVB;
  565. break;
  566. case TUSHORT:
  567. a = AMOVHU;
  568. break;
  569. case TSHORT:
  570. a = AMOVH;
  571. break;
  572. case TFLOAT:
  573. a = AMOVF;
  574. break;
  575. case TVLONG:
  576. case TDOUBLE:
  577. a = AMOVD;
  578. break;
  579. }
  580. if(ft == tt)
  581. regalloc(&nod, t, f);
  582. else
  583. regalloc(&nod, t, Z);
  584. gmove(f, &nod);
  585. gins(a, &nod, t);
  586. regfree(&nod);
  587. return;
  588. }
  589. /*
  590. * type x type cross table
  591. */
  592. a = AGOK;
  593. switch(ft) {
  594. case TDOUBLE:
  595. case TVLONG:
  596. case TFLOAT:
  597. switch(tt) {
  598. case TDOUBLE:
  599. case TVLONG:
  600. a = AMOVD;
  601. if(ft == TFLOAT)
  602. a = AMOVFD;
  603. break;
  604. case TFLOAT:
  605. a = AMOVDF;
  606. if(ft == TFLOAT)
  607. a = AMOVF;
  608. break;
  609. case TINT:
  610. case TUINT:
  611. case TLONG:
  612. case TULONG:
  613. case TIND:
  614. a = AMOVDW;
  615. if(ft == TFLOAT)
  616. a = AMOVFW;
  617. break;
  618. case TSHORT:
  619. case TUSHORT:
  620. case TCHAR:
  621. case TUCHAR:
  622. a = AMOVDW;
  623. if(ft == TFLOAT)
  624. a = AMOVFW;
  625. break;
  626. }
  627. break;
  628. case TUINT:
  629. case TULONG:
  630. if(tt == TFLOAT || tt == TDOUBLE) {
  631. // ugly and probably longer than necessary,
  632. // but vfp has a single instruction for this,
  633. // so hopefully it won't last long.
  634. //
  635. // tmp = f
  636. // tmp1 = tmp & 0x80000000
  637. // tmp ^= tmp1
  638. // t = float(int32(tmp))
  639. // if(tmp1)
  640. // t += 2147483648.
  641. //
  642. regalloc(&nod, f, Z);
  643. regalloc(&nod1, f, Z);
  644. gins(AMOVW, f, &nod);
  645. gins(AMOVW, &nod, &nod1);
  646. gins(AAND, nodconst(0x80000000), &nod1);
  647. gins(AEOR, &nod1, &nod);
  648. if(tt == TFLOAT)
  649. gins(AMOVWF, &nod, t);
  650. else
  651. gins(AMOVWD, &nod, t);
  652. gins(ACMP, nodconst(0), Z);
  653. raddr(&nod1, p);
  654. gins(ABEQ, Z, Z);
  655. regfree(&nod);
  656. regfree(&nod1);
  657. p1 = p;
  658. regalloc(&nod, t, Z);
  659. if(tt == TFLOAT) {
  660. gins(AMOVF, nodfconst(2147483648.), &nod);
  661. gins(AADDF, &nod, t);
  662. } else {
  663. gins(AMOVD, nodfconst(2147483648.), &nod);
  664. gins(AADDD, &nod, t);
  665. }
  666. regfree(&nod);
  667. patch(p1, pc);
  668. return;
  669. }
  670. // fall through
  671. case TINT:
  672. case TLONG:
  673. case TIND:
  674. switch(tt) {
  675. case TDOUBLE:
  676. gins(AMOVWD, f, t);
  677. return;
  678. case TFLOAT:
  679. gins(AMOVWF, f, t);
  680. return;
  681. case TINT:
  682. case TUINT:
  683. case TLONG:
  684. case TULONG:
  685. case TIND:
  686. case TSHORT:
  687. case TUSHORT:
  688. case TCHAR:
  689. case TUCHAR:
  690. a = AMOVW;
  691. break;
  692. }
  693. break;
  694. case TSHORT:
  695. switch(tt) {
  696. case TDOUBLE:
  697. regalloc(&nod, f, Z);
  698. gins(AMOVH, f, &nod);
  699. gins(AMOVWD, &nod, t);
  700. regfree(&nod);
  701. return;
  702. case TFLOAT:
  703. regalloc(&nod, f, Z);
  704. gins(AMOVH, f, &nod);
  705. gins(AMOVWF, &nod, t);
  706. regfree(&nod);
  707. return;
  708. case TUINT:
  709. case TINT:
  710. case TULONG:
  711. case TLONG:
  712. case TIND:
  713. a = AMOVH;
  714. break;
  715. case TSHORT:
  716. case TUSHORT:
  717. case TCHAR:
  718. case TUCHAR:
  719. a = AMOVW;
  720. break;
  721. }
  722. break;
  723. case TUSHORT:
  724. switch(tt) {
  725. case TDOUBLE:
  726. regalloc(&nod, f, Z);
  727. gins(AMOVHU, f, &nod);
  728. gins(AMOVWD, &nod, t);
  729. regfree(&nod);
  730. return;
  731. case TFLOAT:
  732. regalloc(&nod, f, Z);
  733. gins(AMOVHU, f, &nod);
  734. gins(AMOVWF, &nod, t);
  735. regfree(&nod);
  736. return;
  737. case TINT:
  738. case TUINT:
  739. case TLONG:
  740. case TULONG:
  741. case TIND:
  742. a = AMOVHU;
  743. break;
  744. case TSHORT:
  745. case TUSHORT:
  746. case TCHAR:
  747. case TUCHAR:
  748. a = AMOVW;
  749. break;
  750. }
  751. break;
  752. case TCHAR:
  753. switch(tt) {
  754. case TDOUBLE:
  755. regalloc(&nod, f, Z);
  756. gins(AMOVB, f, &nod);
  757. gins(AMOVWD, &nod, t);
  758. regfree(&nod);
  759. return;
  760. case TFLOAT:
  761. regalloc(&nod, f, Z);
  762. gins(AMOVB, f, &nod);
  763. gins(AMOVWF, &nod, t);
  764. regfree(&nod);
  765. return;
  766. case TINT:
  767. case TUINT:
  768. case TLONG:
  769. case TULONG:
  770. case TIND:
  771. case TSHORT:
  772. case TUSHORT:
  773. a = AMOVB;
  774. break;
  775. case TCHAR:
  776. case TUCHAR:
  777. a = AMOVW;
  778. break;
  779. }
  780. break;
  781. case TUCHAR:
  782. switch(tt) {
  783. case TDOUBLE:
  784. regalloc(&nod, f, Z);
  785. gins(AMOVBU, f, &nod);
  786. gins(AMOVWD, &nod, t);
  787. regfree(&nod);
  788. return;
  789. case TFLOAT:
  790. regalloc(&nod, f, Z);
  791. gins(AMOVBU, f, &nod);
  792. gins(AMOVWF, &nod, t);
  793. regfree(&nod);
  794. return;
  795. case TINT:
  796. case TUINT:
  797. case TLONG:
  798. case TULONG:
  799. case TIND:
  800. case TSHORT:
  801. case TUSHORT:
  802. a = AMOVBU;
  803. break;
  804. case TCHAR:
  805. case TUCHAR:
  806. a = AMOVW;
  807. break;
  808. }
  809. break;
  810. }
  811. if(a == AGOK)
  812. diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type);
  813. if(a == AMOVW || a == AMOVF || a == AMOVD)
  814. if(samaddr(f, t))
  815. return;
  816. gins(a, f, t);
  817. }
  818. void
  819. gmover(Node *f, Node *t)
  820. {
  821. int ft, tt, a;
  822. ft = f->type->etype;
  823. tt = t->type->etype;
  824. a = AGOK;
  825. if(typechlp[ft] && typechlp[tt] && ewidth[ft] >= ewidth[tt]){
  826. switch(tt){
  827. case TSHORT:
  828. a = AMOVH;
  829. break;
  830. case TUSHORT:
  831. a = AMOVHU;
  832. break;
  833. case TCHAR:
  834. a = AMOVB;
  835. break;
  836. case TUCHAR:
  837. a = AMOVBU;
  838. break;
  839. }
  840. }
  841. if(a == AGOK)
  842. gmove(f, t);
  843. else
  844. gins(a, f, t);
  845. }
  846. void
  847. gins(int a, Node *f, Node *t)
  848. {
  849. nextpc();
  850. p->as = a;
  851. if(f != Z)
  852. naddr(f, &p->from);
  853. if(t != Z)
  854. naddr(t, &p->to);
  855. if(debug['g'])
  856. print("%P\n", p);
  857. }
  858. void
  859. gopcode(int o, Node *f1, Node *f2, Node *t)
  860. {
  861. int a, et;
  862. Adr ta;
  863. et = TLONG;
  864. if(f1 != Z && f1->type != T)
  865. et = f1->type->etype;
  866. a = AGOK;
  867. switch(o) {
  868. case OAS:
  869. gmove(f1, t);
  870. return;
  871. case OASADD:
  872. case OADD:
  873. a = AADD;
  874. if(et == TFLOAT)
  875. a = AADDF;
  876. else
  877. if(et == TDOUBLE || et == TVLONG)
  878. a = AADDD;
  879. break;
  880. case OASSUB:
  881. case OSUB:
  882. if(f2 && f2->op == OCONST) {
  883. Node *t = f1;
  884. f1 = f2;
  885. f2 = t;
  886. a = ARSB;
  887. } else
  888. a = ASUB;
  889. if(et == TFLOAT)
  890. a = ASUBF;
  891. else
  892. if(et == TDOUBLE || et == TVLONG)
  893. a = ASUBD;
  894. break;
  895. case OASOR:
  896. case OOR:
  897. a = AORR;
  898. break;
  899. case OASAND:
  900. case OAND:
  901. a = AAND;
  902. break;
  903. case OASXOR:
  904. case OXOR:
  905. a = AEOR;
  906. break;
  907. case OASLSHR:
  908. case OLSHR:
  909. a = ASRL;
  910. break;
  911. case OASASHR:
  912. case OASHR:
  913. a = ASRA;
  914. break;
  915. case OASASHL:
  916. case OASHL:
  917. a = ASLL;
  918. break;
  919. case OFUNC:
  920. a = ABL;
  921. break;
  922. case OASMUL:
  923. case OMUL:
  924. a = AMUL;
  925. if(et == TFLOAT)
  926. a = AMULF;
  927. else
  928. if(et == TDOUBLE || et == TVLONG)
  929. a = AMULD;
  930. break;
  931. case OASDIV:
  932. case ODIV:
  933. a = ADIV;
  934. if(et == TFLOAT)
  935. a = ADIVF;
  936. else
  937. if(et == TDOUBLE || et == TVLONG)
  938. a = ADIVD;
  939. break;
  940. case OASMOD:
  941. case OMOD:
  942. a = AMOD;
  943. break;
  944. case OASLMUL:
  945. case OLMUL:
  946. a = AMULU;
  947. break;
  948. case OASLMOD:
  949. case OLMOD:
  950. a = AMODU;
  951. break;
  952. case OASLDIV:
  953. case OLDIV:
  954. a = ADIVU;
  955. break;
  956. case OCASE:
  957. case OEQ:
  958. case ONE:
  959. case OLT:
  960. case OLE:
  961. case OGE:
  962. case OGT:
  963. case OLO:
  964. case OLS:
  965. case OHS:
  966. case OHI:
  967. a = ACMP;
  968. if(et == TFLOAT)
  969. a = ACMPF;
  970. else
  971. if(et == TDOUBLE || et == TVLONG)
  972. a = ACMPD;
  973. nextpc();
  974. p->as = a;
  975. naddr(f1, &p->from);
  976. if(a == ACMP && f1->op == OCONST && p->from.offset < 0 &&
  977. p->from.offset != 0x80000000) {
  978. p->as = ACMN;
  979. p->from.offset = -p->from.offset;
  980. }
  981. raddr(f2, p);
  982. switch(o) {
  983. case OEQ:
  984. a = ABEQ;
  985. break;
  986. case ONE:
  987. a = ABNE;
  988. break;
  989. case OLT:
  990. a = ABLT;
  991. break;
  992. case OLE:
  993. a = ABLE;
  994. break;
  995. case OGE:
  996. a = ABGE;
  997. break;
  998. case OGT:
  999. a = ABGT;
  1000. break;
  1001. case OLO:
  1002. a = ABLO;
  1003. break;
  1004. case OLS:
  1005. a = ABLS;
  1006. break;
  1007. case OHS:
  1008. a = ABHS;
  1009. break;
  1010. case OHI:
  1011. a = ABHI;
  1012. break;
  1013. case OCASE:
  1014. nextpc();
  1015. p->as = ACASE;
  1016. p->scond = 0x9;
  1017. naddr(f2, &p->from);
  1018. a = ABHI;
  1019. break;
  1020. }
  1021. f1 = Z;
  1022. f2 = Z;
  1023. break;
  1024. }
  1025. if(a == AGOK)
  1026. diag(Z, "bad in gopcode %O", o);
  1027. nextpc();
  1028. p->as = a;
  1029. if(f1 != Z)
  1030. naddr(f1, &p->from);
  1031. if(f2 != Z) {
  1032. naddr(f2, &ta);
  1033. p->reg = ta.reg;
  1034. }
  1035. if(t != Z)
  1036. naddr(t, &p->to);
  1037. if(debug['g'])
  1038. print("%P\n", p);
  1039. }
  1040. samaddr(Node *f, Node *t)
  1041. {
  1042. if(f->op != t->op)
  1043. return 0;
  1044. switch(f->op) {
  1045. case OREGISTER:
  1046. if(f->reg != t->reg)
  1047. break;
  1048. return 1;
  1049. }
  1050. return 0;
  1051. }
  1052. void
  1053. gbranch(int o)
  1054. {
  1055. int a;
  1056. a = AGOK;
  1057. switch(o) {
  1058. case ORETURN:
  1059. a = ARET;
  1060. break;
  1061. case OGOTO:
  1062. a = AB;
  1063. break;
  1064. }
  1065. nextpc();
  1066. if(a == AGOK) {
  1067. diag(Z, "bad in gbranch %O", o);
  1068. nextpc();
  1069. }
  1070. p->as = a;
  1071. }
  1072. void
  1073. patch(Prog *op, long pc)
  1074. {
  1075. op->to.offset = pc;
  1076. op->to.type = D_BRANCH;
  1077. }
  1078. void
  1079. gpseudo(int a, Sym *s, Node *n)
  1080. {
  1081. nextpc();
  1082. p->as = a;
  1083. p->from.type = D_OREG;
  1084. p->from.sym = s;
  1085. p->from.name = D_EXTERN;
  1086. if(a == ATEXT)
  1087. p->reg = (profileflg ? 0 : NOPROF);
  1088. if(s->class == CSTATIC)
  1089. p->from.name = D_STATIC;
  1090. naddr(n, &p->to);
  1091. if(a == ADATA || a == AGLOBL)
  1092. pc--;
  1093. }
  1094. int
  1095. sconst(Node *n)
  1096. {
  1097. vlong vv;
  1098. if(n->op == OCONST) {
  1099. if(!typefd[n->type->etype]) {
  1100. vv = n->vconst;
  1101. if(vv >= (vlong)(-32766) && vv < (vlong)32766)
  1102. return 1;
  1103. /*
  1104. * should be specialised for constant values which will
  1105. * fit in different instructionsl; for now, let 5l
  1106. * sort it out
  1107. */
  1108. return 1;
  1109. }
  1110. }
  1111. return 0;
  1112. }
  1113. int
  1114. sval(long v)
  1115. {
  1116. int i;
  1117. for(i=0; i<16; i++) {
  1118. if((v & ~0xff) == 0)
  1119. return 1;
  1120. if((~v & ~0xff) == 0)
  1121. return 1;
  1122. v = (v<<2) | ((ulong)v>>30);
  1123. }
  1124. return 0;
  1125. }
  1126. long
  1127. exreg(Type *t)
  1128. {
  1129. long o;
  1130. if(typechlp[t->etype]) {
  1131. if(exregoffset <= REGEXT-2)
  1132. return 0;
  1133. o = exregoffset;
  1134. if(reg[o] && !resvreg[o])
  1135. return 0;
  1136. resvreg[o] = reg[o] = 1;
  1137. exregoffset--;
  1138. return o;
  1139. }
  1140. if(typefd[t->etype]) {
  1141. if(exfregoffset <= NFREG-1)
  1142. return 0;
  1143. o = exfregoffset + NREG;
  1144. if(reg[o] && !resvreg[o])
  1145. return 0;
  1146. resvreg[o] = reg[o] = 1;
  1147. exfregoffset--;
  1148. return o;
  1149. }
  1150. return 0;
  1151. }
  1152. schar ewidth[NTYPE] =
  1153. {
  1154. -1, /* [TXXX] */
  1155. SZ_CHAR, /* [TCHAR] */
  1156. SZ_CHAR, /* [TUCHAR] */
  1157. SZ_SHORT, /* [TSHORT] */
  1158. SZ_SHORT, /* [TUSHORT] */
  1159. SZ_INT, /* [TINT] */
  1160. SZ_INT, /* [TUINT] */
  1161. SZ_LONG, /* [TLONG] */
  1162. SZ_LONG, /* [TULONG] */
  1163. SZ_VLONG, /* [TVLONG] */
  1164. SZ_VLONG, /* [TUVLONG] */
  1165. SZ_FLOAT, /* [TFLOAT] */
  1166. SZ_DOUBLE, /* [TDOUBLE] */
  1167. SZ_IND, /* [TIND] */
  1168. 0, /* [TFUNC] */
  1169. -1, /* [TARRAY] */
  1170. 0, /* [TVOID] */
  1171. -1, /* [TSTRUCT] */
  1172. -1, /* [TUNION] */
  1173. SZ_INT, /* [TENUM] */
  1174. };
  1175. long ncast[NTYPE] =
  1176. {
  1177. 0, /* [TXXX] */
  1178. BCHAR|BUCHAR, /* [TCHAR] */
  1179. BCHAR|BUCHAR, /* [TUCHAR] */
  1180. BSHORT|BUSHORT, /* [TSHORT] */
  1181. BSHORT|BUSHORT, /* [TUSHORT] */
  1182. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1183. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1184. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1185. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1186. BVLONG|BUVLONG, /* [TVLONG] */
  1187. BVLONG|BUVLONG, /* [TUVLONG] */
  1188. BFLOAT, /* [TFLOAT] */
  1189. BDOUBLE, /* [TDOUBLE] */
  1190. BLONG|BULONG|BIND, /* [TIND] */
  1191. 0, /* [TFUNC] */
  1192. 0, /* [TARRAY] */
  1193. 0, /* [TVOID] */
  1194. BSTRUCT, /* [TSTRUCT] */
  1195. BUNION, /* [TUNION] */
  1196. 0, /* [TENUM] */
  1197. };