txt.c 19 KB

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