txt.c 20 KB

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