txt.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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, true;
  862. Adr ta;
  863. et = TLONG;
  864. if(f1 != Z && f1->type != T)
  865. et = f1->type->etype;
  866. true = o & BTRUE;
  867. o &= ~BTRUE;
  868. a = AGOK;
  869. switch(o) {
  870. case OAS:
  871. gmove(f1, t);
  872. return;
  873. case OASADD:
  874. case OADD:
  875. a = AADD;
  876. if(et == TFLOAT)
  877. a = AADDF;
  878. else
  879. if(et == TDOUBLE || et == TVLONG)
  880. a = AADDD;
  881. break;
  882. case OASSUB:
  883. case OSUB:
  884. if(f2 && f2->op == OCONST) {
  885. Node *t = f1;
  886. f1 = f2;
  887. f2 = t;
  888. a = ARSB;
  889. } else
  890. a = ASUB;
  891. if(et == TFLOAT)
  892. a = ASUBF;
  893. else
  894. if(et == TDOUBLE || et == TVLONG)
  895. a = ASUBD;
  896. break;
  897. case OASOR:
  898. case OOR:
  899. a = AORR;
  900. break;
  901. case OASAND:
  902. case OAND:
  903. a = AAND;
  904. break;
  905. case OASXOR:
  906. case OXOR:
  907. a = AEOR;
  908. break;
  909. case OASLSHR:
  910. case OLSHR:
  911. a = ASRL;
  912. break;
  913. case OASASHR:
  914. case OASHR:
  915. a = ASRA;
  916. break;
  917. case OASASHL:
  918. case OASHL:
  919. a = ASLL;
  920. break;
  921. case OFUNC:
  922. a = ABL;
  923. break;
  924. case OASMUL:
  925. case OMUL:
  926. a = AMUL;
  927. if(et == TFLOAT)
  928. a = AMULF;
  929. else
  930. if(et == TDOUBLE || et == TVLONG)
  931. a = AMULD;
  932. break;
  933. case OASDIV:
  934. case ODIV:
  935. a = ADIV;
  936. if(et == TFLOAT)
  937. a = ADIVF;
  938. else
  939. if(et == TDOUBLE || et == TVLONG)
  940. a = ADIVD;
  941. break;
  942. case OASMOD:
  943. case OMOD:
  944. a = AMOD;
  945. break;
  946. case OASLMUL:
  947. case OLMUL:
  948. a = AMULU;
  949. break;
  950. case OASLMOD:
  951. case OLMOD:
  952. a = AMODU;
  953. break;
  954. case OASLDIV:
  955. case OLDIV:
  956. a = ADIVU;
  957. break;
  958. case OCASE:
  959. case OEQ:
  960. case ONE:
  961. case OLT:
  962. case OLE:
  963. case OGE:
  964. case OGT:
  965. case OLO:
  966. case OLS:
  967. case OHS:
  968. case OHI:
  969. a = ACMP;
  970. if(et == TFLOAT)
  971. a = ACMPF;
  972. else
  973. if(et == TDOUBLE || et == TVLONG)
  974. a = ACMPD;
  975. nextpc();
  976. p->as = a;
  977. naddr(f1, &p->from);
  978. if(a == ACMP && f1->op == OCONST && p->from.offset < 0 &&
  979. p->from.offset != 0x80000000) {
  980. p->as = ACMN;
  981. p->from.offset = -p->from.offset;
  982. }
  983. raddr(f2, p);
  984. switch(o) {
  985. case OEQ:
  986. a = ABEQ;
  987. break;
  988. case ONE:
  989. a = ABNE;
  990. break;
  991. case OLT:
  992. a = ABLT;
  993. /* ensure NaN comparison is always false */
  994. if(typefd[et] && !true)
  995. a = ABMI;
  996. break;
  997. case OLE:
  998. a = ABLE;
  999. if(typefd[et] && !true)
  1000. a = ABLS;
  1001. break;
  1002. case OGE:
  1003. a = ABGE;
  1004. if(typefd[et] && true)
  1005. a = ABPL;
  1006. break;
  1007. case OGT:
  1008. a = ABGT;
  1009. if(typefd[et] && true)
  1010. a = ABHI;
  1011. break;
  1012. case OLO:
  1013. a = ABLO;
  1014. break;
  1015. case OLS:
  1016. a = ABLS;
  1017. break;
  1018. case OHS:
  1019. a = ABHS;
  1020. break;
  1021. case OHI:
  1022. a = ABHI;
  1023. break;
  1024. case OCASE:
  1025. nextpc();
  1026. p->as = ACASE;
  1027. p->scond = 0x9;
  1028. naddr(f2, &p->from);
  1029. a = ABHI;
  1030. break;
  1031. }
  1032. f1 = Z;
  1033. f2 = Z;
  1034. break;
  1035. }
  1036. if(a == AGOK)
  1037. diag(Z, "bad in gopcode %O", o);
  1038. nextpc();
  1039. p->as = a;
  1040. if(f1 != Z)
  1041. naddr(f1, &p->from);
  1042. if(f2 != Z) {
  1043. naddr(f2, &ta);
  1044. p->reg = ta.reg;
  1045. }
  1046. if(t != Z)
  1047. naddr(t, &p->to);
  1048. if(debug['g'])
  1049. print("%P\n", p);
  1050. }
  1051. samaddr(Node *f, Node *t)
  1052. {
  1053. if(f->op != t->op)
  1054. return 0;
  1055. switch(f->op) {
  1056. case OREGISTER:
  1057. if(f->reg != t->reg)
  1058. break;
  1059. return 1;
  1060. }
  1061. return 0;
  1062. }
  1063. void
  1064. gbranch(int o)
  1065. {
  1066. int a;
  1067. a = AGOK;
  1068. switch(o) {
  1069. case ORETURN:
  1070. a = ARET;
  1071. break;
  1072. case OGOTO:
  1073. a = AB;
  1074. break;
  1075. }
  1076. nextpc();
  1077. if(a == AGOK) {
  1078. diag(Z, "bad in gbranch %O", o);
  1079. nextpc();
  1080. }
  1081. p->as = a;
  1082. }
  1083. void
  1084. patch(Prog *op, long pc)
  1085. {
  1086. op->to.offset = pc;
  1087. op->to.type = D_BRANCH;
  1088. }
  1089. void
  1090. gpseudo(int a, Sym *s, Node *n)
  1091. {
  1092. nextpc();
  1093. p->as = a;
  1094. p->from.type = D_OREG;
  1095. p->from.sym = s;
  1096. p->from.name = D_EXTERN;
  1097. if(a == ATEXT)
  1098. p->reg = (profileflg ? 0 : NOPROF);
  1099. if(s->class == CSTATIC)
  1100. p->from.name = D_STATIC;
  1101. naddr(n, &p->to);
  1102. if(a == ADATA || a == AGLOBL)
  1103. pc--;
  1104. }
  1105. int
  1106. sconst(Node *n)
  1107. {
  1108. vlong vv;
  1109. if(n->op == OCONST) {
  1110. if(!typefd[n->type->etype]) {
  1111. vv = n->vconst;
  1112. if(vv >= (vlong)(-32766) && vv < (vlong)32766)
  1113. return 1;
  1114. /*
  1115. * should be specialised for constant values which will
  1116. * fit in different instructionsl; for now, let 5l
  1117. * sort it out
  1118. */
  1119. return 1;
  1120. }
  1121. }
  1122. return 0;
  1123. }
  1124. int
  1125. sval(long v)
  1126. {
  1127. int i;
  1128. for(i=0; i<16; i++) {
  1129. if((v & ~0xff) == 0)
  1130. return 1;
  1131. if((~v & ~0xff) == 0)
  1132. return 1;
  1133. v = (v<<2) | ((ulong)v>>30);
  1134. }
  1135. return 0;
  1136. }
  1137. long
  1138. exreg(Type *t)
  1139. {
  1140. long o;
  1141. if(typechlp[t->etype]) {
  1142. if(exregoffset <= REGEXT-2)
  1143. return 0;
  1144. o = exregoffset;
  1145. if(reg[o] && !resvreg[o])
  1146. return 0;
  1147. resvreg[o] = reg[o] = 1;
  1148. exregoffset--;
  1149. return o;
  1150. }
  1151. if(typefd[t->etype]) {
  1152. if(exfregoffset <= NFREG-1)
  1153. return 0;
  1154. o = exfregoffset + NREG;
  1155. if(reg[o] && !resvreg[o])
  1156. return 0;
  1157. resvreg[o] = reg[o] = 1;
  1158. exfregoffset--;
  1159. return o;
  1160. }
  1161. return 0;
  1162. }
  1163. schar ewidth[NTYPE] =
  1164. {
  1165. -1, /* [TXXX] */
  1166. SZ_CHAR, /* [TCHAR] */
  1167. SZ_CHAR, /* [TUCHAR] */
  1168. SZ_SHORT, /* [TSHORT] */
  1169. SZ_SHORT, /* [TUSHORT] */
  1170. SZ_INT, /* [TINT] */
  1171. SZ_INT, /* [TUINT] */
  1172. SZ_LONG, /* [TLONG] */
  1173. SZ_LONG, /* [TULONG] */
  1174. SZ_VLONG, /* [TVLONG] */
  1175. SZ_VLONG, /* [TUVLONG] */
  1176. SZ_FLOAT, /* [TFLOAT] */
  1177. SZ_DOUBLE, /* [TDOUBLE] */
  1178. SZ_IND, /* [TIND] */
  1179. 0, /* [TFUNC] */
  1180. -1, /* [TARRAY] */
  1181. 0, /* [TVOID] */
  1182. -1, /* [TSTRUCT] */
  1183. -1, /* [TUNION] */
  1184. SZ_INT, /* [TENUM] */
  1185. };
  1186. long ncast[NTYPE] =
  1187. {
  1188. 0, /* [TXXX] */
  1189. BCHAR|BUCHAR, /* [TCHAR] */
  1190. BCHAR|BUCHAR, /* [TUCHAR] */
  1191. BSHORT|BUSHORT, /* [TSHORT] */
  1192. BSHORT|BUSHORT, /* [TUSHORT] */
  1193. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1194. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1195. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1196. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1197. BVLONG|BUVLONG, /* [TVLONG] */
  1198. BVLONG|BUVLONG, /* [TUVLONG] */
  1199. BFLOAT, /* [TFLOAT] */
  1200. BDOUBLE, /* [TDOUBLE] */
  1201. BLONG|BULONG|BIND, /* [TIND] */
  1202. 0, /* [TFUNC] */
  1203. 0, /* [TARRAY] */
  1204. 0, /* [TVOID] */
  1205. BSTRUCT, /* [TSTRUCT] */
  1206. BUNION, /* [TUNION] */
  1207. 0, /* [TENUM] */
  1208. };