txt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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 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. if(typefd[tt]) {
  545. /* special case can load mem to Freg */
  546. regalloc(&nod, t, t);
  547. gins(AMOVL, f, &nod);
  548. a = ACVTQT;
  549. if(tt == TFLOAT)
  550. a = ACVTQS;
  551. gins(a, &nod, &nod);
  552. gmove(&nod, t);
  553. regfree(&nod);
  554. return;
  555. }
  556. a = AMOVL;
  557. break;
  558. case TVLONG:
  559. case TUVLONG:
  560. /* todo: optimise freg case? */
  561. a = AMOVQ;
  562. break;
  563. case TFLOAT:
  564. a = AMOVS;
  565. break;
  566. case TDOUBLE:
  567. a = AMOVT;
  568. break;
  569. case TCHAR:
  570. a = AMOVB;
  571. break;
  572. case TUCHAR:
  573. a = AMOVBU;
  574. break;
  575. case TSHORT:
  576. a = AMOVW;
  577. break;
  578. case TUSHORT:
  579. a = AMOVWU;
  580. break;
  581. }
  582. if(typechlp[ft] && typeilp[tt])
  583. regalloc(&nod, t, t);
  584. else
  585. regalloc(&nod, f, t);
  586. gins(a, f, &nod);
  587. gmove(&nod, t);
  588. regfree(&nod);
  589. return;
  590. }
  591. /*
  592. * a store --
  593. * put it into a register then
  594. * store it.
  595. */
  596. if(t->op == ONAME || t->op == OINDREG || t->op == OIND) {
  597. switch(tt) {
  598. default:
  599. a = AMOVL;
  600. break;
  601. case TUCHAR:
  602. case TCHAR:
  603. a = AMOVB;
  604. break;
  605. case TUSHORT:
  606. case TSHORT:
  607. a = AMOVW;
  608. break;
  609. case TFLOAT:
  610. a = AMOVS;
  611. break;
  612. case TDOUBLE:
  613. a = AMOVT;
  614. break;
  615. case TVLONG:
  616. case TUVLONG:
  617. a = AMOVQ;
  618. break;
  619. }
  620. if(!typefd[ft] && vconst(f) == 0) {
  621. gins(a, f, t);
  622. return;
  623. }
  624. if(ft == tt)
  625. regalloc(&nod, t, f);
  626. else
  627. regalloc(&nod, t, Z);
  628. gmove(f, &nod);
  629. gins(a, &nod, t);
  630. regfree(&nod);
  631. return;
  632. }
  633. /*
  634. * type x type cross table
  635. */
  636. a = AGOK;
  637. switch(ft) {
  638. case TDOUBLE:
  639. case TFLOAT:
  640. switch(tt) {
  641. case TDOUBLE:
  642. a = AMOVT;
  643. break;
  644. case TFLOAT:
  645. a = ACVTTS;
  646. if(ft == TFLOAT)
  647. a = AMOVT;
  648. break;
  649. case TINT:
  650. case TUINT:
  651. case TLONG:
  652. case TULONG:
  653. case TUVLONG:
  654. case TVLONG:
  655. case TIND:
  656. case TSHORT:
  657. case TUSHORT:
  658. case TCHAR:
  659. case TUCHAR:
  660. /*warn(Z, "float to fix"); /**/
  661. regalloc(&nod, f, Z); /* should be type float */
  662. gins(ACVTTQ, f, &nod);
  663. gins(AMOVT, &nod, nodrat);
  664. regfree(&nod);
  665. gins(AMOVQ, nodrat, t);
  666. gmove(t, t);
  667. if(nrathole < SZ_VLONG)
  668. nrathole = SZ_VLONG;
  669. return;
  670. }
  671. break;
  672. case TVLONG:
  673. case TUVLONG:
  674. switch(tt) {
  675. case TDOUBLE:
  676. case TFLOAT:
  677. goto fxtofl;
  678. case TINT:
  679. case TUINT:
  680. case TLONG:
  681. case TULONG:
  682. case TIND:
  683. case TSHORT:
  684. case TUSHORT:
  685. case TCHAR:
  686. case TUCHAR:
  687. a = AMOVL;
  688. break;
  689. case TVLONG:
  690. case TUVLONG:
  691. a = AMOVQ;
  692. break;
  693. }
  694. break;
  695. case TINT:
  696. case TUINT:
  697. case TULONG:
  698. case TLONG:
  699. case TIND:
  700. switch(tt) {
  701. case TDOUBLE:
  702. case TFLOAT:
  703. goto fxtofl;
  704. case TVLONG:
  705. case TUVLONG:
  706. if (ft == TULONG || ft == TUINT) {
  707. a = AMOVLU;
  708. break;
  709. }
  710. case TINT:
  711. case TUINT:
  712. case TLONG:
  713. case TULONG:
  714. case TIND:
  715. case TSHORT:
  716. case TUSHORT:
  717. case TCHAR:
  718. case TUCHAR:
  719. a = AMOVQ;
  720. break;
  721. }
  722. break;
  723. case TSHORT:
  724. switch(tt) {
  725. case TDOUBLE:
  726. case TFLOAT:
  727. goto fxtofl;
  728. case TINT:
  729. case TUINT:
  730. case TULONG:
  731. case TLONG:
  732. case TVLONG:
  733. case TUVLONG:
  734. case TIND:
  735. a = AMOVW;
  736. break;
  737. case TSHORT:
  738. case TUSHORT:
  739. case TCHAR:
  740. case TUCHAR:
  741. a = AMOVQ;
  742. break;
  743. }
  744. break;
  745. case TUSHORT:
  746. switch(tt) {
  747. case TDOUBLE:
  748. case TFLOAT:
  749. goto fxtofl;
  750. case TINT:
  751. case TUINT:
  752. case TLONG:
  753. case TULONG:
  754. case TVLONG:
  755. case TUVLONG:
  756. case TIND:
  757. a = AMOVWU;
  758. break;
  759. case TSHORT:
  760. case TUSHORT:
  761. case TCHAR:
  762. case TUCHAR:
  763. a = AMOVQ;
  764. break;
  765. }
  766. break;
  767. case TCHAR:
  768. switch(tt) {
  769. case TDOUBLE:
  770. case TFLOAT:
  771. goto fxtofl;
  772. case TINT:
  773. case TUINT:
  774. case TLONG:
  775. case TULONG:
  776. case TVLONG:
  777. case TUVLONG:
  778. case TIND:
  779. case TSHORT:
  780. case TUSHORT:
  781. a = AMOVB;
  782. break;
  783. case TCHAR:
  784. case TUCHAR:
  785. a = AMOVQ;
  786. break;
  787. }
  788. break;
  789. case TUCHAR:
  790. switch(tt) {
  791. case TDOUBLE:
  792. case TFLOAT:
  793. fxtofl:
  794. /*warn(Z, "fix to float"); /**/
  795. regalloc(&nod, t, Z); /* should be type float */
  796. gins(AMOVQ, f, nodrat);
  797. gins(AMOVT, nodrat, &nod);
  798. a = ACVTQT;
  799. if(tt == TFLOAT)
  800. a = ACVTQS;
  801. gins(a, &nod, t);
  802. regfree(&nod);
  803. if(nrathole < SZ_VLONG)
  804. nrathole = SZ_VLONG;
  805. return;
  806. case TINT:
  807. case TUINT:
  808. case TLONG:
  809. case TULONG:
  810. case TVLONG:
  811. case TUVLONG:
  812. case TIND:
  813. case TSHORT:
  814. case TUSHORT:
  815. a = AMOVBU;
  816. break;
  817. case TCHAR:
  818. case TUCHAR:
  819. a = AMOVQ;
  820. break;
  821. }
  822. break;
  823. }
  824. if(a == AMOVQ || a == AMOVS || a == AMOVT)
  825. if(samaddr(f, t))
  826. return;
  827. gins(a, f, t);
  828. }
  829. void
  830. gins(int a, Node *f, Node *t)
  831. {
  832. nextpc();
  833. p->as = a;
  834. if(f != Z)
  835. naddr(f, &p->from);
  836. if(t != Z)
  837. naddr(t, &p->to);
  838. if(debug['g'])
  839. print("%P\n", p);
  840. }
  841. void
  842. gopcode(int o, Node *f1, Node *f2, Node *t)
  843. {
  844. int a, et;
  845. Adr ta;
  846. Node nod;
  847. et = TLONG;
  848. if(f1 != Z && f1->type != T) {
  849. if(f1->op == OCONST && t != Z && t->type != T)
  850. et = t->type->etype;
  851. else
  852. et = f1->type->etype;
  853. }
  854. a = AGOK;
  855. switch(o) {
  856. case OAS:
  857. gmove(f1, t);
  858. return;
  859. case OASADD:
  860. case OADD:
  861. a = AADDL;
  862. if(et == TFLOAT)
  863. a = AADDS;
  864. else
  865. if(et == TDOUBLE)
  866. a = AADDT;
  867. else
  868. if(et == TVLONG || et == TUVLONG)
  869. a = AADDQ;
  870. break;
  871. case OASSUB:
  872. case OSUB:
  873. a = ASUBL;
  874. if(et == TFLOAT)
  875. a = ASUBS;
  876. else
  877. if(et == TDOUBLE)
  878. a = ASUBT;
  879. else
  880. if(et == TVLONG || et == TUVLONG)
  881. a = ASUBQ;
  882. break;
  883. case OASOR:
  884. case OOR:
  885. a = AOR;
  886. break;
  887. case OASAND:
  888. case OAND:
  889. a = AAND;
  890. break;
  891. case OASXOR:
  892. case OXOR:
  893. a = AXOR;
  894. break;
  895. case OASLSHR:
  896. case OLSHR:
  897. a = ASRLL;
  898. if(et == TVLONG || et == TUVLONG)
  899. a = ASRLQ;
  900. break;
  901. case OASASHR:
  902. case OASHR:
  903. a = ASRAL;
  904. if(et == TVLONG || et == TUVLONG)
  905. a = ASRAQ;
  906. break;
  907. case OASASHL:
  908. case OASHL:
  909. a = ASLLL;
  910. if(et == TVLONG || et == TUVLONG)
  911. a = ASLLQ;
  912. break;
  913. case OFUNC:
  914. a = AJSR;
  915. break;
  916. case OASLMUL:
  917. case OLMUL:
  918. case OASMUL:
  919. case OMUL:
  920. a = AMULL;
  921. if(et == TFLOAT)
  922. a = AMULS;
  923. else
  924. if(et == TDOUBLE)
  925. a = AMULT;
  926. else
  927. if(et == TVLONG || et == TUVLONG)
  928. a = AMULQ;
  929. break;
  930. case OASDIV:
  931. case ODIV:
  932. a = ADIVL;
  933. if(et == TFLOAT)
  934. a = ADIVS;
  935. else
  936. if(et == TDOUBLE)
  937. a = ADIVT;
  938. else
  939. if(et == TVLONG || et == TUVLONG)
  940. a = ADIVQ;
  941. break;
  942. case OASMOD:
  943. case OMOD:
  944. a = AMODL;
  945. if(et == TVLONG || et == TUVLONG)
  946. a = AMODQ;
  947. break;
  948. case OASLMOD:
  949. case OLMOD:
  950. a = AMODLU;
  951. if(et == TVLONG || et == TUVLONG)
  952. a = AMODQU;
  953. break;
  954. case OASLDIV:
  955. case OLDIV:
  956. a = ADIVLU;
  957. if(et == TVLONG || et == TUVLONG)
  958. a = ADIVQU;
  959. break;
  960. case OEQ:
  961. case ONE:
  962. a = ACMPEQ;
  963. if(typefd[et])
  964. a = ACMPTEQ;
  965. else if (vconst(t) == 0) {
  966. a = (o == OEQ) ? ABEQ : ABNE;
  967. t = Z;
  968. break;
  969. }
  970. goto cmp;
  971. case OLT:
  972. case OGE:
  973. a = ACMPGT;
  974. if(typefd[et])
  975. a = ACMPTGT;
  976. else if (vconst(t) == 0) {
  977. a = (o == OLT) ? ABLT : ABGE;
  978. t = Z;
  979. break;
  980. }
  981. goto cmp;
  982. case OLE:
  983. case OGT:
  984. a = ACMPGE;
  985. if(typefd[et])
  986. a = ACMPTGE;
  987. else
  988. if(vconst(t) == 0) {
  989. a = (o == OLE)? ABLE: ABGT;
  990. t = Z;
  991. break;
  992. }
  993. goto cmp;
  994. case OLO:
  995. case OHS:
  996. a = ACMPUGT;
  997. goto cmp;
  998. case OLS:
  999. case OHI:
  1000. a = ACMPUGE;
  1001. goto cmp;
  1002. cmp:
  1003. nextpc();
  1004. p->as = a;
  1005. raddr(f1, p);
  1006. naddr(t, &p->from);
  1007. regalloc(&nod, t, Z);
  1008. naddr(&nod, &p->to);
  1009. if(debug['g'])
  1010. print("%P\n", p);
  1011. if(o == OEQ || o == OLT || o == OLE || o == OLO || o == OLS)
  1012. a = typefd[et]? AFBNE: ABNE;
  1013. else
  1014. a = typefd[et]? AFBEQ: ABEQ;
  1015. nextpc();
  1016. p->as = a;
  1017. naddr(&nod, &p->from);
  1018. regfree(&nod);
  1019. if(debug['g'])
  1020. print("%P\n", p);
  1021. return;
  1022. }
  1023. if(a == AGOK)
  1024. diag(Z, "bad in gopcode %O", o);
  1025. nextpc();
  1026. p->as = a;
  1027. if(f1 != Z)
  1028. naddr(f1, &p->from);
  1029. if(f2 != Z) {
  1030. naddr(f2, &ta);
  1031. p->reg = ta.reg;
  1032. if(ta.type == D_CONST && ta.offset == 0)
  1033. p->reg = REGZERO;
  1034. }
  1035. if(t != Z)
  1036. naddr(t, &p->to);
  1037. if(debug['g'])
  1038. print("%P\n", p);
  1039. }
  1040. samaddr(Node *f, Node *t)
  1041. {
  1042. if(f->op != t->op)
  1043. return 0;
  1044. switch(f->op) {
  1045. case OREGISTER:
  1046. if(f->reg != t->reg)
  1047. break;
  1048. return 1;
  1049. }
  1050. return 0;
  1051. }
  1052. void
  1053. gbranch(int o)
  1054. {
  1055. int a;
  1056. a = AGOK;
  1057. switch(o) {
  1058. case ORETURN:
  1059. a = ARET;
  1060. break;
  1061. case OGOTO:
  1062. a = AJMP;
  1063. break;
  1064. }
  1065. nextpc();
  1066. if(a == AGOK) {
  1067. diag(Z, "bad in gbranch %O", o);
  1068. nextpc();
  1069. }
  1070. p->as = a;
  1071. }
  1072. void
  1073. patch(Prog *op, long pc)
  1074. {
  1075. op->to.offset = pc;
  1076. op->to.type = D_BRANCH;
  1077. }
  1078. void
  1079. gpseudo(int a, Sym *s, Node *n)
  1080. {
  1081. nextpc();
  1082. p->as = a;
  1083. p->from.type = D_OREG;
  1084. p->from.sym = s;
  1085. p->from.name = D_EXTERN;
  1086. if(s->class == CSTATIC)
  1087. p->from.name = D_STATIC;
  1088. naddr(n, &p->to);
  1089. if(a == ADATA || a == AGLOBL)
  1090. pc--;
  1091. }
  1092. int
  1093. sconst(Node *n)
  1094. {
  1095. vlong vv;
  1096. if(n->op == OCONST) {
  1097. if(!typefd[n->type->etype]) {
  1098. vv = n->vconst;
  1099. if(vv >= -32766LL && vv < 32766LL)
  1100. return 1;
  1101. }
  1102. }
  1103. return 0;
  1104. }
  1105. int
  1106. sval(long v)
  1107. {
  1108. if(v >= -32766L && v < 32766L)
  1109. return 1;
  1110. return 0;
  1111. }
  1112. int
  1113. bval(vlong v)
  1114. {
  1115. if(v >= 0LL && v < 256LL)
  1116. return 1;
  1117. return 0;
  1118. }
  1119. int
  1120. bconst(Node *n)
  1121. {
  1122. vlong vv;
  1123. if(n->op == OCONST) {
  1124. if(!typefd[n->type->etype]) {
  1125. vv = n->vconst;
  1126. if(vv >= 0LL && vv < 256LL)
  1127. return 1;
  1128. }
  1129. }
  1130. return 0;
  1131. }
  1132. long
  1133. exreg(Type *t)
  1134. {
  1135. long o;
  1136. if(typechlp[t->etype]) {
  1137. if(exregoffset <= 12)
  1138. return 0;
  1139. o = exregoffset;
  1140. exregoffset--;
  1141. return o;
  1142. }
  1143. if(typefd[t->etype]) {
  1144. if(exfregoffset <= 19)
  1145. return 0;
  1146. o = exfregoffset + NREG;
  1147. exfregoffset--;
  1148. return o;
  1149. }
  1150. return 0;
  1151. }
  1152. schar ewidth[NTYPE] =
  1153. {
  1154. -1, /* [TXXX] */
  1155. SZ_CHAR, /* [TCHAR] */
  1156. SZ_CHAR, /* [TUCHAR] */
  1157. SZ_SHORT, /* [TSHORT] */
  1158. SZ_SHORT, /* [TUSHORT] */
  1159. SZ_INT, /* [TINT] */
  1160. SZ_INT, /* [TUINT] */
  1161. SZ_LONG, /* [TLONG] */
  1162. SZ_LONG, /* [TULONG] */
  1163. SZ_VLONG, /* [TVLONG] */
  1164. SZ_VLONG, /* [TUVLONG] */
  1165. SZ_FLOAT, /* [TFLOAT] */
  1166. SZ_DOUBLE, /* [TDOUBLE] */
  1167. SZ_IND, /* [TIND] */
  1168. 0, /* [TFUNC] */
  1169. -1, /* [TARRAY] */
  1170. 0, /* [TVOID] */
  1171. -1, /* [TSTRUCT] */
  1172. -1, /* [TUNION] */
  1173. SZ_INT, /* [TENUM] */
  1174. };
  1175. long ncast[NTYPE] =
  1176. {
  1177. 0, /* [TXXX] */
  1178. BCHAR|BUCHAR, /* [TCHAR] */
  1179. BCHAR|BUCHAR, /* [TUCHAR] */
  1180. BSHORT|BUSHORT, /* [TSHORT] */
  1181. BSHORT|BUSHORT, /* [TUSHORT] */
  1182. BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */
  1183. BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */
  1184. BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */
  1185. BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */
  1186. BVLONG|BUVLONG, /* [TVLONG] */
  1187. BVLONG|BUVLONG, /* [TUVLONG] */
  1188. BFLOAT, /* [TFLOAT] */
  1189. BDOUBLE, /* [TDOUBLE] */
  1190. BLONG|BULONG|BIND, /* [TIND] */
  1191. 0, /* [TFUNC] */
  1192. 0, /* [TARRAY] */
  1193. 0, /* [TVOID] */
  1194. BSTRUCT, /* [TSTRUCT] */
  1195. BUNION, /* [TUNION] */
  1196. 0, /* [TENUM] */
  1197. };