txt.c 21 KB

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