sgen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. /*
  353. * calculate addressability as follows
  354. * NAME ==> 10/11 name+value(SB/SP)
  355. * REGISTER ==> 12 register
  356. * CONST ==> 20 $value
  357. * *(20) ==> 21 value
  358. * &(10) ==> 13 $name+value(SB)
  359. * &(11) ==> 1 $name+value(SP)
  360. * (13) + (20) ==> 13 fold constants
  361. * (1) + (20) ==> 1 fold constants
  362. * *(13) ==> 10 back to name
  363. * *(1) ==> 11 back to name
  364. *
  365. * (20) * (X) ==> 7 multiplier in indexing
  366. * (X,7) + (13,1) ==> 8 adder in indexing (addresses)
  367. * (8) ==> &9(OINDEX) index, almost addressable
  368. *
  369. * calculate complexity (number of registers)
  370. */
  371. void
  372. xcom(Node *n)
  373. {
  374. Node *l, *r;
  375. int g;
  376. if(n == Z)
  377. return;
  378. l = n->left;
  379. r = n->right;
  380. n->complex = 0;
  381. n->addable = 0;
  382. switch(n->op) {
  383. case OCONST:
  384. n->addable = 20;
  385. break;
  386. case ONAME:
  387. n->addable = 10;
  388. if(n->class == CPARAM || n->class == CAUTO)
  389. n->addable = 11;
  390. break;
  391. case OREGISTER:
  392. n->addable = 12;
  393. break;
  394. case OINDREG:
  395. n->addable = 12;
  396. break;
  397. case OADDR:
  398. xcom(l);
  399. if(l->addable == 10)
  400. n->addable = 13;
  401. else
  402. if(l->addable == 11)
  403. n->addable = 1;
  404. break;
  405. case OADD:
  406. xcom(l);
  407. xcom(r);
  408. if(n->type->etype != TIND)
  409. break;
  410. switch(r->addable) {
  411. case 20:
  412. switch(l->addable) {
  413. case 1:
  414. case 13:
  415. commadd:
  416. l->type = n->type;
  417. *n = *l;
  418. l = new(0, Z, Z);
  419. *l = *(n->left);
  420. l->xoffset += r->vconst;
  421. n->left = l;
  422. r = n->right;
  423. goto brk;
  424. }
  425. break;
  426. case 1:
  427. case 13:
  428. case 10:
  429. case 11:
  430. // l is the base, r is the index
  431. if(l->addable != 20)
  432. n->addable = 8;
  433. break;
  434. }
  435. switch(l->addable) {
  436. case 20:
  437. switch(r->addable) {
  438. case 13:
  439. case 1:
  440. r = n->left;
  441. l = n->right;
  442. n->left = l;
  443. n->right = r;
  444. goto commadd;
  445. }
  446. break;
  447. case 13:
  448. case 1:
  449. case 10:
  450. case 11:
  451. // r is the base, l is the index
  452. if(r->addable != 20)
  453. n->addable = 8;
  454. break;
  455. }
  456. if(n->addable == 8 && !side(n)) {
  457. indx(n);
  458. l = new1(OINDEX, idx.basetree, idx.regtree);
  459. l->scale = idx.scale;
  460. l->addable = 9;
  461. l->complex = l->right->complex;
  462. l->type = l->left->type;
  463. n->op = OADDR;
  464. n->left = l;
  465. n->right = Z;
  466. n->addable = 8;
  467. break;
  468. }
  469. break;
  470. case OINDEX:
  471. xcom(l);
  472. xcom(r);
  473. n->addable = 9;
  474. break;
  475. case OIND:
  476. xcom(l);
  477. if(l->op == OADDR) {
  478. l = l->left;
  479. l->type = n->type;
  480. *n = *l;
  481. return;
  482. }
  483. switch(l->addable) {
  484. case 20:
  485. n->addable = 21;
  486. break;
  487. case 1:
  488. n->addable = 11;
  489. break;
  490. case 13:
  491. n->addable = 10;
  492. break;
  493. }
  494. break;
  495. case OASHL:
  496. xcom(l);
  497. xcom(r);
  498. g = vconst(r);
  499. if(g >= 0 && g < 4)
  500. n->addable = 7;
  501. break;
  502. case OMUL:
  503. case OLMUL:
  504. xcom(l);
  505. xcom(r);
  506. g = vlog(l);
  507. if(g >= 0) {
  508. n->left = r;
  509. n->right = l;
  510. l = r;
  511. r = n->right;
  512. }
  513. g = vlog(r);
  514. if(g >= 0) {
  515. n->op = OASHL;
  516. r->vconst = g;
  517. if(g < 4)
  518. n->addable = 7;
  519. r->type = types[TINT];
  520. break;
  521. }
  522. break;
  523. case OASLDIV:
  524. xcom(l);
  525. xcom(r);
  526. g = vlog(r);
  527. if(g >= 0) {
  528. n->op = OASLSHR;
  529. r->vconst = g;
  530. r->type = types[TINT];
  531. }
  532. break;
  533. case OLDIV:
  534. xcom(l);
  535. xcom(r);
  536. g = vlog(r);
  537. if(g >= 0) {
  538. n->op = OLSHR;
  539. r->vconst = g;
  540. r->type = types[TINT];
  541. break;
  542. }
  543. break;
  544. case OASLMOD:
  545. xcom(l);
  546. xcom(r);
  547. g = vlog(r);
  548. if(g >= 0) {
  549. n->op = OASAND;
  550. r->vconst--;
  551. }
  552. break;
  553. case OLMOD:
  554. xcom(l);
  555. xcom(r);
  556. g = vlog(r);
  557. if(g >= 0) {
  558. n->op = OAND;
  559. r->vconst--;
  560. }
  561. break;
  562. case OASMUL:
  563. case OASLMUL:
  564. xcom(l);
  565. xcom(r);
  566. g = vlog(r);
  567. if(g >= 0) {
  568. n->op = OASASHL;
  569. r->vconst = g;
  570. }
  571. break;
  572. case OLSHR:
  573. case OASHR:
  574. xcom(l);
  575. xcom(r);
  576. break;
  577. default:
  578. if(l != Z)
  579. xcom(l);
  580. if(r != Z)
  581. xcom(r);
  582. break;
  583. }
  584. brk:
  585. if(n->addable >= 10)
  586. return;
  587. if(l != Z)
  588. n->complex = l->complex;
  589. if(r != Z) {
  590. if(r->complex == n->complex)
  591. n->complex = r->complex+1;
  592. else
  593. if(r->complex > n->complex)
  594. n->complex = r->complex;
  595. }
  596. if(n->complex == 0)
  597. n->complex++;
  598. if(com64(n))
  599. return;
  600. switch(n->op) {
  601. case OFUNC:
  602. n->complex = FNX;
  603. break;
  604. case OLMOD:
  605. case OMOD:
  606. case OLMUL:
  607. case OLDIV:
  608. case OMUL:
  609. case ODIV:
  610. case OASLMUL:
  611. case OASLDIV:
  612. case OASLMOD:
  613. case OASMUL:
  614. case OASDIV:
  615. case OASMOD:
  616. if(r->complex >= l->complex) {
  617. n->complex = l->complex + 3;
  618. if(r->complex > n->complex)
  619. n->complex = r->complex;
  620. } else {
  621. n->complex = r->complex + 3;
  622. if(l->complex > n->complex)
  623. n->complex = l->complex;
  624. }
  625. break;
  626. case OLSHR:
  627. case OASHL:
  628. case OASHR:
  629. case OASLSHR:
  630. case OASASHL:
  631. case OASASHR:
  632. if(r->complex >= l->complex) {
  633. n->complex = l->complex + 2;
  634. if(r->complex > n->complex)
  635. n->complex = r->complex;
  636. } else {
  637. n->complex = r->complex + 2;
  638. if(l->complex > n->complex)
  639. n->complex = l->complex;
  640. }
  641. break;
  642. case OADD:
  643. case OXOR:
  644. case OAND:
  645. case OOR:
  646. /*
  647. * immediate operators, make const on right
  648. */
  649. if(l->op == OCONST) {
  650. n->left = r;
  651. n->right = l;
  652. }
  653. break;
  654. case OEQ:
  655. case ONE:
  656. case OLE:
  657. case OLT:
  658. case OGE:
  659. case OGT:
  660. case OHI:
  661. case OHS:
  662. case OLO:
  663. case OLS:
  664. /*
  665. * compare operators, make const on left
  666. */
  667. if(r->op == OCONST) {
  668. n->left = r;
  669. n->right = l;
  670. n->op = invrel[relindex(n->op)];
  671. }
  672. break;
  673. }
  674. }
  675. void
  676. indx(Node *n)
  677. {
  678. Node *l, *r;
  679. if(debug['x'])
  680. prtree(n, "indx");
  681. l = n->left;
  682. r = n->right;
  683. if(l->addable == 1 || l->addable == 13 || r->complex > l->complex) {
  684. n->right = l;
  685. n->left = r;
  686. l = r;
  687. r = n->right;
  688. }
  689. if(l->addable != 7) {
  690. idx.regtree = l;
  691. idx.scale = 1;
  692. } else
  693. if(l->right->addable == 20) {
  694. idx.regtree = l->left;
  695. idx.scale = 1 << l->right->vconst;
  696. } else
  697. if(l->left->addable == 20) {
  698. idx.regtree = l->right;
  699. idx.scale = 1 << l->left->vconst;
  700. } else
  701. diag(n, "bad index");
  702. idx.basetree = r;
  703. if(debug['x']) {
  704. print("scale = %d\n", idx.scale);
  705. prtree(idx.regtree, "index");
  706. prtree(idx.basetree, "base");
  707. }
  708. }
  709. int
  710. bcomplex(Node *n, Node *c)
  711. {
  712. complex(n);
  713. if(n->type != T)
  714. if(tcompat(n, T, n->type, tnot))
  715. n->type = T;
  716. if(n->type != T) {
  717. if(c != Z && n->op == OCONST && deadheads(c))
  718. return 1;
  719. bool64(n);
  720. boolgen(n, 1, Z);
  721. } else
  722. gbranch(OGOTO);
  723. return 0;
  724. }