txt.c 20 KB

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