txt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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, 0);
  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, 0);
  168. (*fnxp)++;
  169. } else
  170. cgen(n, tn1, 0);
  171. return;
  172. }
  173. regalloc(tn1, n, Z);
  174. if(n->complex >= FNX) {
  175. cgen(*fnxp, tn1, 0);
  176. (*fnxp)++;
  177. } else
  178. cgen(n, tn1, 0);
  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 int 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. a = AMOVBU;
  550. break;
  551. case TCHAR:
  552. a = AMOVB;
  553. break;
  554. case TUSHORT:
  555. a = AMOVHU;
  556. break;
  557. case TSHORT:
  558. a = AMOVH;
  559. break;
  560. case TFLOAT:
  561. a = AMOVF;
  562. break;
  563. case TVLONG:
  564. case TDOUBLE:
  565. a = AMOVD;
  566. break;
  567. }
  568. if(ft == tt)
  569. regalloc(&nod, t, f);
  570. else
  571. regalloc(&nod, t, Z);
  572. gmove(f, &nod);
  573. gins(a, &nod, t);
  574. regfree(&nod);
  575. return;
  576. }
  577. /*
  578. * type x type cross table
  579. */
  580. a = AGOK;
  581. switch(ft) {
  582. case TDOUBLE:
  583. case TVLONG:
  584. case TFLOAT:
  585. switch(tt) {
  586. case TDOUBLE:
  587. case TVLONG:
  588. a = AMOVD;
  589. if(ft == TFLOAT)
  590. a = AMOVFD;
  591. break;
  592. case TFLOAT:
  593. a = AMOVDF;
  594. if(ft == TFLOAT)
  595. a = AMOVF;
  596. break;
  597. case TINT:
  598. case TUINT:
  599. case TLONG:
  600. case TULONG:
  601. case TIND:
  602. a = AMOVDW;
  603. if(ft == TFLOAT)
  604. a = AMOVFW;
  605. break;
  606. case TSHORT:
  607. case TUSHORT:
  608. case TCHAR:
  609. case TUCHAR:
  610. a = AMOVDW;
  611. if(ft == TFLOAT)
  612. a = AMOVFW;
  613. break;
  614. }
  615. break;
  616. case TUINT:
  617. case TINT:
  618. case TULONG:
  619. case TLONG:
  620. case TIND:
  621. switch(tt) {
  622. case TDOUBLE:
  623. case TVLONG:
  624. gins(AMOVWD, f, t);
  625. if(ft == TULONG) {
  626. }
  627. return;
  628. case TFLOAT:
  629. gins(AMOVWF, f, t);
  630. if(ft == TULONG) {
  631. }
  632. return;
  633. case TINT:
  634. case TUINT:
  635. case TLONG:
  636. case TULONG:
  637. case TIND:
  638. case TSHORT:
  639. case TUSHORT:
  640. case TCHAR:
  641. case TUCHAR:
  642. a = AMOVW;
  643. break;
  644. }
  645. break;
  646. case TSHORT:
  647. switch(tt) {
  648. case TDOUBLE:
  649. case TVLONG:
  650. regalloc(&nod, f, Z);
  651. gins(AMOVH, f, &nod);
  652. gins(AMOVWD, &nod, t);
  653. regfree(&nod);
  654. return;
  655. case TFLOAT:
  656. regalloc(&nod, f, Z);
  657. gins(AMOVH, f, &nod);
  658. gins(AMOVWF, &nod, t);
  659. regfree(&nod);
  660. return;
  661. case TUINT:
  662. case TINT:
  663. case TULONG:
  664. case TLONG:
  665. case TIND:
  666. a = AMOVH;
  667. break;
  668. case TSHORT:
  669. case TUSHORT:
  670. case TCHAR:
  671. case TUCHAR:
  672. a = AMOVW;
  673. break;
  674. }
  675. break;
  676. case TUSHORT:
  677. switch(tt) {
  678. case TDOUBLE:
  679. case TVLONG:
  680. regalloc(&nod, f, Z);
  681. gins(AMOVHU, f, &nod);
  682. gins(AMOVWD, &nod, t);
  683. regfree(&nod);
  684. return;
  685. case TFLOAT:
  686. regalloc(&nod, f, Z);
  687. gins(AMOVHU, f, &nod);
  688. gins(AMOVWF, &nod, t);
  689. regfree(&nod);
  690. return;
  691. case TINT:
  692. case TUINT:
  693. case TLONG:
  694. case TULONG:
  695. case TIND:
  696. a = AMOVHU;
  697. break;
  698. case TSHORT:
  699. case TUSHORT:
  700. case TCHAR:
  701. case TUCHAR:
  702. a = AMOVW;
  703. break;
  704. }
  705. break;
  706. case TCHAR:
  707. switch(tt) {
  708. case TDOUBLE:
  709. case TVLONG:
  710. regalloc(&nod, f, Z);
  711. gins(AMOVB, f, &nod);
  712. gins(AMOVWD, &nod, t);
  713. regfree(&nod);
  714. return;
  715. case TFLOAT:
  716. regalloc(&nod, f, Z);
  717. gins(AMOVB, f, &nod);
  718. gins(AMOVWF, &nod, t);
  719. regfree(&nod);
  720. return;
  721. case TINT:
  722. case TUINT:
  723. case TLONG:
  724. case TULONG:
  725. case TIND:
  726. case TSHORT:
  727. case TUSHORT:
  728. a = AMOVB;
  729. break;
  730. case TCHAR:
  731. case TUCHAR:
  732. a = AMOVW;
  733. break;
  734. }
  735. break;
  736. case TUCHAR:
  737. switch(tt) {
  738. case TDOUBLE:
  739. case TVLONG:
  740. regalloc(&nod, f, Z);
  741. gins(AMOVBU, f, &nod);
  742. gins(AMOVWD, &nod, t);
  743. regfree(&nod);
  744. return;
  745. case TFLOAT:
  746. regalloc(&nod, f, Z);
  747. gins(AMOVBU, f, &nod);
  748. gins(AMOVWF, &nod, t);
  749. regfree(&nod);
  750. return;
  751. case TINT:
  752. case TUINT:
  753. case TLONG:
  754. case TULONG:
  755. case TIND:
  756. case TSHORT:
  757. case TUSHORT:
  758. a = AMOVBU;
  759. break;
  760. case TCHAR:
  761. case TUCHAR:
  762. a = AMOVW;
  763. break;
  764. }
  765. break;
  766. }
  767. if(a == AGOK)
  768. diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type);
  769. if(a == AMOVW || a == AMOVF || a == AMOVD)
  770. if(samaddr(f, t))
  771. return;
  772. gins(a, f, t);
  773. }
  774. void
  775. gmover(Node *f, Node *t)
  776. {
  777. int ft, tt, a;
  778. ft = f->type->etype;
  779. tt = t->type->etype;
  780. a = AGOK;
  781. if(typechlp[ft] && typechlp[tt] && ewidth[ft] >= ewidth[tt]){
  782. switch(tt){
  783. case TSHORT:
  784. a = AMOVH;
  785. break;
  786. case TUSHORT:
  787. a = AMOVHU;
  788. break;
  789. case TCHAR:
  790. a = AMOVB;
  791. break;
  792. case TUCHAR:
  793. a = AMOVBU;
  794. break;
  795. }
  796. }
  797. if(a == AGOK)
  798. gmove(f, t);
  799. else
  800. gins(a, f, t);
  801. }
  802. void
  803. gins(int a, Node *f, Node *t)
  804. {
  805. nextpc();
  806. p->as = a;
  807. if(f != Z)
  808. naddr(f, &p->from);
  809. if(t != Z)
  810. naddr(t, &p->to);
  811. if(debug['g'])
  812. print("%P\n", p);
  813. }
  814. void
  815. gopcode(int o, Node *f1, Node *f2, Node *t)
  816. {
  817. int a, et;
  818. Adr ta;
  819. et = TLONG;
  820. if(f1 != Z && f1->type != T)
  821. et = f1->type->etype;
  822. a = AGOK;
  823. switch(o) {
  824. case OAS:
  825. gmove(f1, t);
  826. return;
  827. case OASADD:
  828. case OADD:
  829. a = AADD;
  830. if(et == TFLOAT)
  831. a = AADDF;
  832. else
  833. if(et == TDOUBLE || et == TVLONG)
  834. a = AADDD;
  835. break;
  836. case OASSUB:
  837. case OSUB:
  838. if(f2 && f2->op == OCONST) {
  839. Node *t = f1;
  840. f1 = f2;
  841. f2 = t;
  842. a = ARSB;
  843. } else
  844. a = ASUB;
  845. if(et == TFLOAT)
  846. a = ASUBF;
  847. else
  848. if(et == TDOUBLE || et == TVLONG)
  849. a = ASUBD;
  850. break;
  851. case OASOR:
  852. case OOR:
  853. a = AORR;
  854. break;
  855. case OASAND:
  856. case OAND:
  857. a = AAND;
  858. break;
  859. case OASXOR:
  860. case OXOR:
  861. a = AEOR;
  862. break;
  863. case OASLSHR:
  864. case OLSHR:
  865. a = ASRL;
  866. break;
  867. case OASASHR:
  868. case OASHR:
  869. a = ASRA;
  870. break;
  871. case OASASHL:
  872. case OASHL:
  873. a = ASLL;
  874. break;
  875. case OFUNC:
  876. a = ABL;
  877. break;
  878. case OASMUL:
  879. case OMUL:
  880. a = AMUL;
  881. if(et == TFLOAT)
  882. a = AMULF;
  883. else
  884. if(et == TDOUBLE || et == TVLONG)
  885. a = AMULD;
  886. break;
  887. case OASDIV:
  888. case ODIV:
  889. a = ADIV;
  890. if(et == TFLOAT)
  891. a = ADIVF;
  892. else
  893. if(et == TDOUBLE || et == TVLONG)
  894. a = ADIVD;
  895. break;
  896. case OASMOD:
  897. case OMOD:
  898. a = AMOD;
  899. break;
  900. case OASLMUL:
  901. case OLMUL:
  902. a = AMULU;
  903. break;
  904. case OASLMOD:
  905. case OLMOD:
  906. a = AMODU;
  907. break;
  908. case OASLDIV:
  909. case OLDIV:
  910. a = ADIVU;
  911. break;
  912. case OCASE:
  913. case OEQ:
  914. case ONE:
  915. case OLT:
  916. case OLE:
  917. case OGE:
  918. case OGT:
  919. case OLO:
  920. case OLS:
  921. case OHS:
  922. case OHI:
  923. a = ACMP;
  924. if(et == TFLOAT)
  925. a = ACMPF;
  926. else
  927. if(et == TDOUBLE || et == TVLONG)
  928. a = ACMPD;
  929. nextpc();
  930. p->as = a;
  931. naddr(f1, &p->from);
  932. if(a == ACMP && f1->op == OCONST && p->from.offset < 0) {
  933. p->as = ACMN;
  934. p->from.offset = -p->from.offset;
  935. }
  936. raddr(f2, p);
  937. switch(o) {
  938. case OEQ:
  939. a = ABEQ;
  940. break;
  941. case ONE:
  942. a = ABNE;
  943. break;
  944. case OLT:
  945. a = ABLT;
  946. break;
  947. case OLE:
  948. a = ABLE;
  949. break;
  950. case OGE:
  951. a = ABGE;
  952. break;
  953. case OGT:
  954. a = ABGT;
  955. break;
  956. case OLO:
  957. a = ABLO;
  958. break;
  959. case OLS:
  960. a = ABLS;
  961. break;
  962. case OHS:
  963. a = ABHS;
  964. break;
  965. case OHI:
  966. a = ABHI;
  967. break;
  968. case OCASE:
  969. nextpc();
  970. p->as = ACASE;
  971. p->scond = 0x9;
  972. naddr(f2, &p->from);
  973. a = ABHI;
  974. break;
  975. }
  976. f1 = Z;
  977. f2 = Z;
  978. break;
  979. }
  980. if(a == AGOK)
  981. diag(Z, "bad in gopcode %O", o);
  982. nextpc();
  983. p->as = a;
  984. if(f1 != Z)
  985. naddr(f1, &p->from);
  986. if(f2 != Z) {
  987. naddr(f2, &ta);
  988. p->reg = ta.reg;
  989. }
  990. if(t != Z)
  991. naddr(t, &p->to);
  992. if(debug['g'])
  993. print("%P\n", p);
  994. }
  995. samaddr(Node *f, Node *t)
  996. {
  997. if(f->op != t->op)
  998. return 0;
  999. switch(f->op) {
  1000. case OREGISTER:
  1001. if(f->reg != t->reg)
  1002. break;
  1003. return 1;
  1004. }
  1005. return 0;
  1006. }
  1007. void
  1008. gbranch(int o)
  1009. {
  1010. int a;
  1011. a = AGOK;
  1012. switch(o) {
  1013. case ORETURN:
  1014. a = ARET;
  1015. break;
  1016. case OGOTO:
  1017. a = AB;
  1018. break;
  1019. }
  1020. nextpc();
  1021. if(a == AGOK) {
  1022. diag(Z, "bad in gbranch %O", o);
  1023. nextpc();
  1024. }
  1025. p->as = a;
  1026. }
  1027. void
  1028. patch(Prog *op, long pc)
  1029. {
  1030. op->to.offset = pc;
  1031. op->to.type = D_BRANCH;
  1032. }
  1033. void
  1034. gpseudo(int a, Sym *s, Node *n)
  1035. {
  1036. nextpc();
  1037. p->as = a;
  1038. p->from.type = D_OREG;
  1039. p->from.sym = s;
  1040. p->from.name = D_EXTERN;
  1041. p->reg = (profileflg ? 0 : NOPROF);
  1042. if(s->class == CSTATIC)
  1043. p->from.name = D_STATIC;
  1044. naddr(n, &p->to);
  1045. if(a == ADATA || a == AGLOBL)
  1046. pc--;
  1047. }
  1048. int
  1049. sconst(Node *n)
  1050. {
  1051. vlong vv;
  1052. if(n->op == OCONST) {
  1053. if(!typefd[n->type->etype]) {
  1054. vv = n->vconst;
  1055. if(vv >= (vlong)(-32766) && vv < (vlong)32766)
  1056. return 1;
  1057. /*
  1058. * should be specialised for constant values which will
  1059. * fit in different instructionsl; for now, let 5l
  1060. * sort it out
  1061. */
  1062. return 1;
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. int
  1068. sval(long v)
  1069. {
  1070. int i;
  1071. for(i=0; i<16; i++) {
  1072. if((v & ~0xff) == 0)
  1073. return 1;
  1074. if((~v & ~0xff) == 0)
  1075. return 1;
  1076. v = (v<<2) | ((ulong)v>>30);
  1077. }
  1078. return 0;
  1079. }
  1080. long
  1081. exreg(Type *t)
  1082. {
  1083. long o;
  1084. if(typechlp[t->etype]) {
  1085. if(exregoffset <= REGEXT-4)
  1086. return 0;
  1087. o = exregoffset;
  1088. exregoffset--;
  1089. return o;
  1090. }
  1091. if(typefd[t->etype]) {
  1092. if(exfregoffset <= NFREG-1)
  1093. return 0;
  1094. o = exfregoffset + NREG;
  1095. exfregoffset--;
  1096. return o;
  1097. }
  1098. return 0;
  1099. }
  1100. schar ewidth[NTYPE] =
  1101. {
  1102. -1, /* [TXXX] */
  1103. SZ_CHAR, /* [TCHAR] */
  1104. SZ_CHAR, /* [TUCHAR] */
  1105. SZ_SHORT, /* [TSHORT] */
  1106. SZ_SHORT, /* [TUSHORT] */
  1107. SZ_INT, /* [TINT] */
  1108. SZ_INT, /* [TUINT] */
  1109. SZ_LONG, /* [TLONG] */
  1110. SZ_LONG, /* [TULONG] */
  1111. SZ_VLONG, /* [TVLONG] */
  1112. SZ_VLONG, /* [TUVLONG] */
  1113. SZ_FLOAT, /* [TFLOAT] */
  1114. SZ_DOUBLE, /* [TDOUBLE] */
  1115. SZ_IND, /* [TIND] */
  1116. 0, /* [TFUNC] */
  1117. -1, /* [TARRAY] */
  1118. 0, /* [TVOID] */
  1119. -1, /* [TSTRUCT] */
  1120. -1, /* [TUNION] */
  1121. SZ_INT, /* [TENUM] */
  1122. };
  1123. long ncast[NTYPE] =
  1124. {
  1125. 0, /* [TXXX] */
  1126. BCHAR|BUCHAR, /* [TCHAR] */
  1127. BCHAR|BUCHAR, /* [TUCHAR] */
  1128. BSHORT|BUSHORT, /* [TSHORT] */
  1129. BSHORT|BUSHORT, /* [TUSHORT] */
  1130. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1131. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1132. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1133. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1134. BVLONG|BUVLONG, /* [TVLONG] */
  1135. BVLONG|BUVLONG, /* [TUVLONG] */
  1136. BFLOAT, /* [TFLOAT] */
  1137. BDOUBLE, /* [TDOUBLE] */
  1138. BLONG|BULONG|BIND, /* [TIND] */
  1139. 0, /* [TFUNC] */
  1140. 0, /* [TARRAY] */
  1141. 0, /* [TVOID] */
  1142. BSTRUCT, /* [TSTRUCT] */
  1143. BUNION, /* [TUNION] */
  1144. 0, /* [TENUM] */
  1145. };