sgen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. #include "gc.h"
  2. void
  3. codgen(Node *n, Node *nn)
  4. {
  5. Prog *sp;
  6. Node *n1, nod, nod1;
  7. cursafe = 0;
  8. curarg = 0;
  9. maxargsafe = 0;
  10. /*
  11. * isolate name
  12. */
  13. for(n1 = nn;; n1 = n1->left) {
  14. if(n1 == Z) {
  15. diag(nn, "cant find function name");
  16. return;
  17. }
  18. if(n1->op == ONAME)
  19. break;
  20. }
  21. nearln = nn->lineno;
  22. gpseudo(ATEXT, n1->sym, nodconst(stkoff));
  23. /*
  24. * isolate first argument
  25. */
  26. if(REGARG) {
  27. if(typesuv[thisfn->link->etype]) {
  28. nod1 = *nodret->left;
  29. nodreg(&nod, &nod1, REGARG);
  30. gmove(&nod, &nod1);
  31. } else
  32. if(firstarg && typechlp[firstargtype->etype]) {
  33. nod1 = *nodret->left;
  34. nod1.sym = firstarg;
  35. nod1.type = firstargtype;
  36. nod1.xoffset = align(0, firstargtype, Aarg1);
  37. nod1.etype = firstargtype->etype;
  38. nodreg(&nod, &nod1, REGARG);
  39. gmove(&nod, &nod1);
  40. }
  41. }
  42. sp = p;
  43. retok = 0;
  44. gen(n);
  45. if(!retok)
  46. if(thisfn->link->etype != TVOID)
  47. warn(Z, "no return at end of function: %s", n1->sym->name);
  48. noretval(3);
  49. if(thisfn && thisfn->link && typefd[thisfn->link->etype])
  50. gins(AFLDZ, Z, Z);
  51. gbranch(ORETURN);
  52. if(!debug['N'] || debug['R'] || debug['P'])
  53. regopt(sp);
  54. sp->to.offset += maxargsafe;
  55. }
  56. void
  57. supgen(Node *n)
  58. {
  59. long spc;
  60. Prog *sp;
  61. if(n == Z)
  62. return;
  63. suppress++;
  64. spc = pc;
  65. sp = lastp;
  66. gen(n);
  67. lastp = sp;
  68. pc = spc;
  69. sp->link = nil;
  70. suppress--;
  71. }
  72. void
  73. gen(Node *n)
  74. {
  75. Node *l, nod;
  76. Prog *sp, *spc, *spb;
  77. Case *cn;
  78. long sbc, scc;
  79. int f, o;
  80. loop:
  81. if(n == Z)
  82. return;
  83. nearln = n->lineno;
  84. o = n->op;
  85. if(debug['G'])
  86. if(o != OLIST)
  87. print("%L %O\n", nearln, o);
  88. retok = 0;
  89. switch(o) {
  90. default:
  91. complex(n);
  92. cgen(n, Z);
  93. break;
  94. case OLIST:
  95. gen(n->left);
  96. rloop:
  97. n = n->right;
  98. goto loop;
  99. case ORETURN:
  100. retok = 1;
  101. complex(n);
  102. if(n->type == T)
  103. break;
  104. l = n->left;
  105. if(l == Z) {
  106. noretval(3);
  107. if(typefd[n->type->etype])
  108. gins(AFLDZ, Z, Z);
  109. gbranch(ORETURN);
  110. break;
  111. }
  112. if(typesuv[n->type->etype]) {
  113. sugen(l, nodret, n->type->width);
  114. noretval(3);
  115. gbranch(ORETURN);
  116. break;
  117. }
  118. regret(&nod, n);
  119. cgen(l, &nod);
  120. regfree(&nod);
  121. if(typefd[n->type->etype])
  122. noretval(1);
  123. else
  124. noretval(2);
  125. gbranch(ORETURN);
  126. break;
  127. case OLABEL:
  128. l = n->left;
  129. if(l) {
  130. l->xoffset = pc;
  131. if(l->label)
  132. patch(l->label, pc);
  133. }
  134. gbranch(OGOTO); /* prevent self reference in reg */
  135. patch(p, pc);
  136. goto rloop;
  137. case OGOTO:
  138. retok = 1;
  139. n = n->left;
  140. if(n == Z)
  141. return;
  142. if(n->complex == 0) {
  143. diag(Z, "label undefined: %s", n->sym->name);
  144. return;
  145. }
  146. if(suppress)
  147. return;
  148. gbranch(OGOTO);
  149. if(n->xoffset) {
  150. patch(p, n->xoffset);
  151. return;
  152. }
  153. if(n->label)
  154. patch(n->label, pc-1);
  155. n->label = p;
  156. return;
  157. case OCASE:
  158. l = n->left;
  159. if(cases == C)
  160. diag(n, "case/default outside a switch");
  161. if(l == Z) {
  162. cas();
  163. cases->val = 0;
  164. cases->def = 1;
  165. cases->label = pc;
  166. goto rloop;
  167. }
  168. complex(l);
  169. if(l->type == T)
  170. goto rloop;
  171. if(l->op == OCONST)
  172. if(typechl[l->type->etype]) {
  173. cas();
  174. cases->val = l->vconst;
  175. cases->def = 0;
  176. cases->label = pc;
  177. goto rloop;
  178. }
  179. diag(n, "case expression must be integer constant");
  180. goto rloop;
  181. case OSWITCH:
  182. l = n->left;
  183. complex(l);
  184. if(l->type == T)
  185. break;
  186. if(!typechl[l->type->etype]) {
  187. diag(n, "switch expression must be integer");
  188. break;
  189. }
  190. gbranch(OGOTO); /* entry */
  191. sp = p;
  192. cn = cases;
  193. cases = C;
  194. cas();
  195. sbc = breakpc;
  196. breakpc = pc;
  197. gbranch(OGOTO);
  198. spb = p;
  199. gen(n->right);
  200. gbranch(OGOTO);
  201. patch(p, breakpc);
  202. patch(sp, pc);
  203. regalloc(&nod, l, Z);
  204. nod.type = types[TLONG];
  205. cgen(l, &nod);
  206. doswit(&nod);
  207. regfree(&nod);
  208. patch(spb, pc);
  209. cases = cn;
  210. breakpc = sbc;
  211. break;
  212. case OWHILE:
  213. case ODWHILE:
  214. l = n->left;
  215. gbranch(OGOTO); /* entry */
  216. sp = p;
  217. scc = continpc;
  218. continpc = pc;
  219. gbranch(OGOTO);
  220. spc = p;
  221. sbc = breakpc;
  222. breakpc = pc;
  223. gbranch(OGOTO);
  224. spb = p;
  225. patch(spc, pc);
  226. if(n->op == OWHILE)
  227. patch(sp, pc);
  228. bcomplex(l, Z); /* test */
  229. patch(p, breakpc);
  230. if(n->op == ODWHILE)
  231. patch(sp, pc);
  232. gen(n->right); /* body */
  233. gbranch(OGOTO);
  234. patch(p, continpc);
  235. patch(spb, pc);
  236. continpc = scc;
  237. breakpc = sbc;
  238. break;
  239. case OFOR:
  240. l = n->left;
  241. gen(l->right->left); /* init */
  242. gbranch(OGOTO); /* entry */
  243. sp = p;
  244. scc = continpc;
  245. continpc = pc;
  246. gbranch(OGOTO);
  247. spc = p;
  248. sbc = breakpc;
  249. breakpc = pc;
  250. gbranch(OGOTO);
  251. spb = p;
  252. patch(spc, pc);
  253. gen(l->right->right); /* inc */
  254. patch(sp, pc);
  255. if(l->left != Z) { /* test */
  256. bcomplex(l->left, Z);
  257. patch(p, breakpc);
  258. }
  259. gen(n->right); /* body */
  260. gbranch(OGOTO);
  261. patch(p, continpc);
  262. patch(spb, pc);
  263. continpc = scc;
  264. breakpc = sbc;
  265. break;
  266. case OCONTINUE:
  267. if(continpc < 0) {
  268. diag(n, "continue not in a loop");
  269. break;
  270. }
  271. gbranch(OGOTO);
  272. patch(p, continpc);
  273. break;
  274. case OBREAK:
  275. if(breakpc < 0) {
  276. diag(n, "break not in a loop");
  277. break;
  278. }
  279. gbranch(OGOTO);
  280. patch(p, breakpc);
  281. break;
  282. case OIF:
  283. l = n->left;
  284. if(bcomplex(l, n->right)) {
  285. if(typefd[l->type->etype])
  286. f = !l->fconst;
  287. else
  288. f = !l->vconst;
  289. if(debug['c'])
  290. print("%L const if %s\n", nearln, f ? "false" : "true");
  291. if(f) {
  292. supgen(n->right->left);
  293. gen(n->right->right);
  294. }
  295. else {
  296. gen(n->right->left);
  297. supgen(n->right->right);
  298. }
  299. }
  300. else {
  301. sp = p;
  302. if(n->right->left != Z)
  303. gen(n->right->left);
  304. if(n->right->right != Z) {
  305. gbranch(OGOTO);
  306. patch(sp, pc);
  307. sp = p;
  308. gen(n->right->right);
  309. }
  310. patch(sp, pc);
  311. }
  312. break;
  313. case OSET:
  314. case OUSED:
  315. usedset(n->left, o);
  316. break;
  317. }
  318. }
  319. void
  320. usedset(Node *n, int o)
  321. {
  322. if(n->op == OLIST) {
  323. usedset(n->left, o);
  324. usedset(n->right, o);
  325. return;
  326. }
  327. complex(n);
  328. switch(n->op) {
  329. case OADDR: /* volatile */
  330. gins(ANOP, n, Z);
  331. break;
  332. case ONAME:
  333. if(o == OSET)
  334. gins(ANOP, Z, n);
  335. else
  336. gins(ANOP, n, Z);
  337. break;
  338. }
  339. }
  340. void
  341. noretval(int n)
  342. {
  343. if(n & 1) {
  344. gins(ANOP, Z, Z);
  345. p->to.type = REGRET;
  346. }
  347. if(n & 2) {
  348. gins(ANOP, Z, Z);
  349. p->to.type = FREGRET;
  350. }
  351. }
  352. /* welcome to commute */
  353. static void
  354. commute(Node *n)
  355. {
  356. Node *l, *r;
  357. l = n->left;
  358. r = n->right;
  359. if(r->complex > l->complex) {
  360. n->left = r;
  361. n->right = l;
  362. }
  363. }
  364. void
  365. indexshift(Node *n)
  366. {
  367. int g;
  368. if(!typechlp[n->type->etype])
  369. return;
  370. simplifyshift(n);
  371. if(n->op == OASHL && n->right->op == OCONST){
  372. g = vconst(n->right);
  373. if(g >= 0 && g < 4)
  374. n->addable = 7;
  375. }
  376. }
  377. /*
  378. * calculate addressability as follows
  379. * NAME ==> 10/11 name+value(SB/SP)
  380. * REGISTER ==> 12 register
  381. * CONST ==> 20 $value
  382. * *(20) ==> 21 value
  383. * &(10) ==> 13 $name+value(SB)
  384. * &(11) ==> 1 $name+value(SP)
  385. * (13) + (20) ==> 13 fold constants
  386. * (1) + (20) ==> 1 fold constants
  387. * *(13) ==> 10 back to name
  388. * *(1) ==> 11 back to name
  389. *
  390. * (20) * (X) ==> 7 multiplier in indexing
  391. * (X,7) + (13,1) ==> 8 adder in indexing (addresses)
  392. * (8) ==> &9(OINDEX) index, almost addressable
  393. *
  394. * calculate complexity (number of registers)
  395. */
  396. void
  397. xcom(Node *n)
  398. {
  399. Node *l, *r;
  400. int g;
  401. if(n == Z)
  402. return;
  403. l = n->left;
  404. r = n->right;
  405. n->complex = 0;
  406. n->addable = 0;
  407. switch(n->op) {
  408. case OCONST:
  409. n->addable = 20;
  410. break;
  411. case ONAME:
  412. n->addable = 10;
  413. if(n->class == CPARAM || n->class == CAUTO)
  414. n->addable = 11;
  415. break;
  416. case OREGISTER:
  417. n->addable = 12;
  418. break;
  419. case OINDREG:
  420. n->addable = 12;
  421. break;
  422. case OADDR:
  423. xcom(l);
  424. if(l->addable == 10)
  425. n->addable = 13;
  426. else
  427. if(l->addable == 11)
  428. n->addable = 1;
  429. break;
  430. case OADD:
  431. xcom(l);
  432. xcom(r);
  433. if(n->type->etype != TIND)
  434. break;
  435. switch(r->addable) {
  436. case 20:
  437. switch(l->addable) {
  438. case 1:
  439. case 13:
  440. commadd:
  441. l->type = n->type;
  442. *n = *l;
  443. l = new(0, Z, Z);
  444. *l = *(n->left);
  445. l->xoffset += r->vconst;
  446. n->left = l;
  447. r = n->right;
  448. goto brk;
  449. }
  450. break;
  451. case 1:
  452. case 13:
  453. case 10:
  454. case 11:
  455. /* l is the base, r is the index */
  456. if(l->addable != 20)
  457. n->addable = 8;
  458. break;
  459. }
  460. switch(l->addable) {
  461. case 20:
  462. switch(r->addable) {
  463. case 13:
  464. case 1:
  465. r = n->left;
  466. l = n->right;
  467. n->left = l;
  468. n->right = r;
  469. goto commadd;
  470. }
  471. break;
  472. case 13:
  473. case 1:
  474. case 10:
  475. case 11:
  476. /* r is the base, l is the index */
  477. if(r->addable != 20)
  478. n->addable = 8;
  479. break;
  480. }
  481. if(n->addable == 8 && !side(n)) {
  482. indx(n);
  483. l = new1(OINDEX, idx.basetree, idx.regtree);
  484. l->scale = idx.scale;
  485. l->addable = 9;
  486. l->complex = l->right->complex;
  487. l->type = l->left->type;
  488. n->op = OADDR;
  489. n->left = l;
  490. n->right = Z;
  491. n->addable = 8;
  492. break;
  493. }
  494. break;
  495. case OINDEX:
  496. xcom(l);
  497. xcom(r);
  498. n->addable = 9;
  499. break;
  500. case OIND:
  501. xcom(l);
  502. if(l->op == OADDR) {
  503. l = l->left;
  504. l->type = n->type;
  505. *n = *l;
  506. return;
  507. }
  508. switch(l->addable) {
  509. case 20:
  510. n->addable = 21;
  511. break;
  512. case 1:
  513. n->addable = 11;
  514. break;
  515. case 13:
  516. n->addable = 10;
  517. break;
  518. }
  519. break;
  520. case OASHL:
  521. xcom(l);
  522. xcom(r);
  523. indexshift(n);
  524. break;
  525. case OMUL:
  526. case OLMUL:
  527. xcom(l);
  528. xcom(r);
  529. g = vlog(l);
  530. if(g >= 0) {
  531. n->left = r;
  532. n->right = l;
  533. l = r;
  534. r = n->right;
  535. }
  536. g = vlog(r);
  537. if(g >= 0) {
  538. n->op = OASHL;
  539. r->vconst = g;
  540. r->type = types[TINT];
  541. indexshift(n);
  542. break;
  543. }
  544. commute(n);
  545. break;
  546. case OASLDIV:
  547. xcom(l);
  548. xcom(r);
  549. g = vlog(r);
  550. if(g >= 0) {
  551. n->op = OASLSHR;
  552. r->vconst = g;
  553. r->type = types[TINT];
  554. }
  555. break;
  556. case OLDIV:
  557. xcom(l);
  558. xcom(r);
  559. g = vlog(r);
  560. if(g >= 0) {
  561. n->op = OLSHR;
  562. r->vconst = g;
  563. r->type = types[TINT];
  564. indexshift(n);
  565. break;
  566. }
  567. break;
  568. case OASLMOD:
  569. xcom(l);
  570. xcom(r);
  571. g = vlog(r);
  572. if(g >= 0) {
  573. n->op = OASAND;
  574. r->vconst--;
  575. }
  576. break;
  577. case OLMOD:
  578. xcom(l);
  579. xcom(r);
  580. g = vlog(r);
  581. if(g >= 0) {
  582. n->op = OAND;
  583. r->vconst--;
  584. }
  585. break;
  586. case OASMUL:
  587. case OASLMUL:
  588. xcom(l);
  589. xcom(r);
  590. g = vlog(r);
  591. if(g >= 0) {
  592. n->op = OASASHL;
  593. r->vconst = g;
  594. }
  595. break;
  596. case OLSHR:
  597. case OASHR:
  598. xcom(l);
  599. xcom(r);
  600. indexshift(n);
  601. break;
  602. default:
  603. if(l != Z)
  604. xcom(l);
  605. if(r != Z)
  606. xcom(r);
  607. break;
  608. }
  609. brk:
  610. if(n->addable >= 10)
  611. return;
  612. if(l != Z)
  613. n->complex = l->complex;
  614. if(r != Z) {
  615. if(r->complex == n->complex)
  616. n->complex = r->complex+1;
  617. else
  618. if(r->complex > n->complex)
  619. n->complex = r->complex;
  620. }
  621. if(n->complex == 0)
  622. n->complex++;
  623. if(com64(n))
  624. return;
  625. switch(n->op) {
  626. case OFUNC:
  627. n->complex = FNX;
  628. break;
  629. case OLMOD:
  630. case OMOD:
  631. case OLMUL:
  632. case OLDIV:
  633. case OMUL:
  634. case ODIV:
  635. case OASLMUL:
  636. case OASLDIV:
  637. case OASLMOD:
  638. case OASMUL:
  639. case OASDIV:
  640. case OASMOD:
  641. if(r->complex >= l->complex) {
  642. n->complex = l->complex + 3;
  643. if(r->complex > n->complex)
  644. n->complex = r->complex;
  645. } else {
  646. n->complex = r->complex + 3;
  647. if(l->complex > n->complex)
  648. n->complex = l->complex;
  649. }
  650. break;
  651. case OLSHR:
  652. case OASHL:
  653. case OASHR:
  654. case OASLSHR:
  655. case OASASHL:
  656. case OASASHR:
  657. if(r->complex >= l->complex) {
  658. n->complex = l->complex + 2;
  659. if(r->complex > n->complex)
  660. n->complex = r->complex;
  661. } else {
  662. n->complex = r->complex + 2;
  663. if(l->complex > n->complex)
  664. n->complex = l->complex;
  665. }
  666. break;
  667. case OADD:
  668. case OXOR:
  669. case OAND:
  670. case OOR:
  671. /*
  672. * immediate operators, make const on right
  673. */
  674. if(l->op == OCONST) {
  675. n->left = r;
  676. n->right = l;
  677. }
  678. break;
  679. case OEQ:
  680. case ONE:
  681. case OLE:
  682. case OLT:
  683. case OGE:
  684. case OGT:
  685. case OHI:
  686. case OHS:
  687. case OLO:
  688. case OLS:
  689. /*
  690. * compare operators, make const on left
  691. */
  692. if(r->op == OCONST) {
  693. n->left = r;
  694. n->right = l;
  695. n->op = invrel[relindex(n->op)];
  696. }
  697. break;
  698. }
  699. }
  700. void
  701. indx(Node *n)
  702. {
  703. Node *l, *r;
  704. if(debug['x'])
  705. prtree(n, "indx");
  706. l = n->left;
  707. r = n->right;
  708. if(l->addable == 1 || l->addable == 13 || r->complex > l->complex) {
  709. n->right = l;
  710. n->left = r;
  711. l = r;
  712. r = n->right;
  713. }
  714. if(l->addable != 7) {
  715. idx.regtree = l;
  716. idx.scale = 1;
  717. } else
  718. if(l->right->addable == 20) {
  719. idx.regtree = l->left;
  720. idx.scale = 1 << l->right->vconst;
  721. } else
  722. if(l->left->addable == 20) {
  723. idx.regtree = l->right;
  724. idx.scale = 1 << l->left->vconst;
  725. } else
  726. diag(n, "bad index");
  727. idx.basetree = r;
  728. if(debug['x']) {
  729. print("scale = %d\n", idx.scale);
  730. prtree(idx.regtree, "index");
  731. prtree(idx.basetree, "base");
  732. }
  733. }
  734. int
  735. bcomplex(Node *n, Node *c)
  736. {
  737. Node *b, nod;
  738. complex(n);
  739. if(n->type != T)
  740. if(tcompat(n, T, n->type, tnot))
  741. n->type = T;
  742. if(n->type != T) {
  743. if(c != Z && n->op == OCONST && deadheads(c))
  744. return 1;
  745. if(typev[n->type->etype] && machcap(Z)) {
  746. b = &nod;
  747. b->op = ONE;
  748. b->left = n;
  749. b->right = new(0, Z, Z);
  750. *b->right = *nodconst(0);
  751. b->right->type = n->type;
  752. b->type = types[TLONG];
  753. cgen64(b, Z);
  754. return 0;
  755. }
  756. bool64(n);
  757. boolgen(n, 1, Z);
  758. } else
  759. gbranch(OGOTO);
  760. return 0;
  761. }