txt.c 20 KB

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