sgen.c 12 KB

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