sgen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. gopcode(OTST, types[TINT], D_TREE, n, D_NONE, Z);
  278. p->as = ANOP;
  279. break;
  280. case ONAME:
  281. if(o == OSET)
  282. gopcode(OTST, types[TINT], D_NONE, Z, D_TREE, n);
  283. else
  284. gopcode(OTST, types[TINT], D_TREE, n, D_NONE, Z);
  285. p->as = ANOP;
  286. break;
  287. }
  288. }
  289. void
  290. noretval(int n)
  291. {
  292. if(n & 1) {
  293. gopcode(OTST, types[TINT], D_NONE, Z, regret(types[TLONG]), Z);
  294. p->as = ANOP;
  295. }
  296. if(n & 2) {
  297. gopcode(OTST, types[TINT], D_NONE, Z, regret(types[TDOUBLE]), Z);
  298. p->as = ANOP;
  299. }
  300. }
  301. /*
  302. * calculate addressability as follows
  303. * REGISTER ==> 12 register
  304. * NAME ==> 10/11 name+value(SB/SP)
  305. * CONST ==> 20 $value
  306. * *(20) ==> 21 value
  307. * &(10) ==> 12 $name+value(SB)
  308. * &(11) ==> 1 $name+value(SP)
  309. * (12) + (20) ==> 12 fold constants
  310. * (1) + (20) ==> 1 fold constants
  311. * *(12) ==> 10 back to name
  312. * *(1) ==> 11 back to name
  313. *
  314. * (2,10,11) + (20) ==> 2 indirect w offset
  315. * (2) ==> &13
  316. * *(10,11) ==> 13 indirect, no index
  317. *
  318. * (20) * (X) ==> 7 multiplier in indexing
  319. * (X,7) + (12,1) ==> 8 adder in indexing (addresses)
  320. * (X,7) + (10,11,2) ==> 8 adder in indexing (names)
  321. * (8) ==> &9 index, almost addressable
  322. *
  323. * (X)++ ==> X fake addressability
  324. *
  325. * calculate complexity (number of registers)
  326. */
  327. void
  328. xcom(Node *n)
  329. {
  330. Node *l, *r;
  331. int g;
  332. if(n == Z)
  333. return;
  334. l = n->left;
  335. r = n->right;
  336. n->complex = 0;
  337. n->addable = 0;
  338. switch(n->op) {
  339. case OCONST:
  340. n->addable = 20;
  341. break;
  342. case ONAME:
  343. n->addable = 10;
  344. if(n->class == CPARAM || n->class == CAUTO)
  345. n->addable = 11;
  346. break;
  347. case OREGISTER:
  348. n->addable = 12;
  349. break;
  350. case OADDR:
  351. xcom(l);
  352. if(l->addable == 10)
  353. n->addable = 12;
  354. else
  355. if(l->addable == 11)
  356. n->addable = 1;
  357. break;
  358. case OADD:
  359. xcom(l);
  360. xcom(r);
  361. if(n->type->etype != TIND)
  362. break;
  363. if(l->addable == 20)
  364. switch(r->addable) {
  365. case 12:
  366. case 1:
  367. n->addable = r->addable;
  368. goto brk;
  369. case 10:
  370. case 11:
  371. case 2:
  372. goto addr13;
  373. }
  374. if(r->addable == 20)
  375. switch(l->addable) {
  376. case 12:
  377. case 1:
  378. n->addable = l->addable;
  379. goto brk;
  380. case 10:
  381. case 11:
  382. case 2:
  383. addr13:
  384. n->addable = 2;
  385. l = new1(OXXX, Z, Z);
  386. *l = *n;
  387. n->op = OIND;
  388. n->left = l;
  389. n->right = Z;
  390. n->addable = 13;
  391. l = new1(OXXX, Z, Z);
  392. *l = *n;
  393. n->op = OADDR;
  394. n->left = l;
  395. n->right = Z;
  396. n->addable = 2;
  397. goto brk;
  398. }
  399. switch(r->addable) {
  400. case 10:
  401. case 11:
  402. case 12:
  403. case 1:
  404. n->addable = 8;
  405. }
  406. switch(l->addable) {
  407. case 10:
  408. case 11:
  409. case 12:
  410. case 1:
  411. n->addable = 8;
  412. }
  413. if(n->addable == 8) {
  414. indx(n);
  415. l = new1(OINDEX, idx.basetree, idx.regtree);
  416. l->scale = idx.scale;
  417. l->addable = 9;
  418. l->complex = l->right->complex;
  419. l->type = l->left->type;
  420. n->op = OADDR;
  421. n->left = l;
  422. n->right = Z;
  423. n->addable = 0;
  424. break;
  425. }
  426. break;
  427. case OIND:
  428. xcom(l);
  429. if(l->op == OADDR) {
  430. l = l->left;
  431. l->type = n->type;
  432. *n = *l;
  433. return;
  434. }
  435. switch(l->addable) {
  436. case 20:
  437. n->addable = 21;
  438. break;
  439. case 1:
  440. n->addable = 11;
  441. break;
  442. case 12:
  443. n->addable = 10;
  444. break;
  445. case 10:
  446. case 11:
  447. case 2:
  448. n->addable = 13;
  449. break;
  450. }
  451. break;
  452. case OASHL:
  453. xcom(l);
  454. xcom(r);
  455. if(typev[n->type->etype])
  456. break;
  457. g = vconst(r);
  458. if(g >= 0 && g < 4)
  459. n->addable = 7;
  460. break;
  461. case OMUL:
  462. case OLMUL:
  463. xcom(l);
  464. xcom(r);
  465. if(typev[n->type->etype])
  466. break;
  467. g = vlog(r);
  468. if(g >= 0) {
  469. n->op = OASHL;
  470. r->vconst = g;
  471. if(g < 4)
  472. n->addable = 7;
  473. break;
  474. }
  475. g = vlog(l);
  476. if(g >= 0) {
  477. n->left = r;
  478. n->right = l;
  479. l = r;
  480. r = n->right;
  481. n->op = OASHL;
  482. r->vconst = g;
  483. if(g < 4)
  484. n->addable = 7;
  485. break;
  486. }
  487. break;
  488. case ODIV:
  489. case OLDIV:
  490. xcom(l);
  491. xcom(r);
  492. if(typev[n->type->etype])
  493. break;
  494. g = vlog(r);
  495. if(g >= 0) {
  496. if(n->op == ODIV)
  497. n->op = OASHR;
  498. else
  499. n->op = OLSHR;
  500. r->vconst = g;
  501. }
  502. break;
  503. case OSUB:
  504. xcom(l);
  505. xcom(r);
  506. if(typev[n->type->etype])
  507. break;
  508. if(vconst(l) == 0) {
  509. n->op = ONEG;
  510. n->left = r;
  511. n->right = Z;
  512. }
  513. break;
  514. case OXOR:
  515. xcom(l);
  516. xcom(r);
  517. if(typev[n->type->etype])
  518. break;
  519. if(vconst(l) == -1) {
  520. n->op = OCOM;
  521. n->left = r;
  522. n->right = Z;
  523. }
  524. break;
  525. case OASMUL:
  526. case OASLMUL:
  527. xcom(l);
  528. xcom(r);
  529. if(typev[n->type->etype])
  530. break;
  531. g = vlog(r);
  532. if(g >= 0) {
  533. n->op = OASASHL;
  534. r->vconst = g;
  535. }
  536. goto aseae;
  537. case OASDIV:
  538. case OASLDIV:
  539. xcom(l);
  540. xcom(r);
  541. if(typev[n->type->etype])
  542. break;
  543. g = vlog(r);
  544. if(g >= 0) {
  545. if(n->op == OASDIV)
  546. n->op = OASASHR;
  547. else
  548. n->op = OASLSHR;
  549. r->vconst = g;
  550. }
  551. goto aseae;
  552. case OASLMOD:
  553. case OASMOD:
  554. xcom(l);
  555. xcom(r);
  556. if(typev[n->type->etype])
  557. break;
  558. aseae: /* hack that there are no byte/short mul/div operators */
  559. if(n->type->etype == TCHAR || n->type->etype == TSHORT) {
  560. n->right = new1(OCAST, n->right, Z);
  561. n->right->type = types[TLONG];
  562. n->type = types[TLONG];
  563. }
  564. if(n->type->etype == TUCHAR || n->type->etype == TUSHORT) {
  565. n->right = new1(OCAST, n->right, Z);
  566. n->right->type = types[TULONG];
  567. n->type = types[TULONG];
  568. }
  569. goto asop;
  570. case OASXOR:
  571. case OASOR:
  572. case OASADD:
  573. case OASSUB:
  574. case OASLSHR:
  575. case OASASHR:
  576. case OASASHL:
  577. case OASAND:
  578. case OAS:
  579. xcom(l);
  580. xcom(r);
  581. if(typev[n->type->etype])
  582. break;
  583. asop:
  584. if(l->addable > INDEXED &&
  585. l->complex < FNX &&
  586. r && r->complex < FNX)
  587. n->addable = l->addable;
  588. break;
  589. case OPOSTINC:
  590. case OPREINC:
  591. case OPOSTDEC:
  592. case OPREDEC:
  593. xcom(l);
  594. if(typev[n->type->etype])
  595. break;
  596. if(l->addable > INDEXED &&
  597. l->complex < FNX)
  598. n->addable = l->addable;
  599. break;
  600. default:
  601. if(l != Z)
  602. xcom(l);
  603. if(r != Z)
  604. xcom(r);
  605. break;
  606. }
  607. brk:
  608. n->complex = 0;
  609. if(n->addable >= 10)
  610. return;
  611. if(l != Z)
  612. n->complex = l->complex;
  613. if(r != Z) {
  614. if(r->complex == n->complex)
  615. n->complex = r->complex+1;
  616. else
  617. if(r->complex > n->complex)
  618. n->complex = r->complex;
  619. }
  620. if(n->complex == 0)
  621. n->complex++;
  622. if(com64(n))
  623. return;
  624. switch(n->op) {
  625. case OFUNC:
  626. n->complex = FNX;
  627. break;
  628. case OADD:
  629. case OMUL:
  630. case OLMUL:
  631. case OXOR:
  632. case OAND:
  633. case OOR:
  634. /*
  635. * symmetric operators, make right side simple
  636. * if same, put constant on left to get movq
  637. */
  638. if(r->complex > l->complex ||
  639. (r->complex == l->complex && r->addable == 20)) {
  640. n->left = r;
  641. n->right = l;
  642. }
  643. break;
  644. case OLE:
  645. case OLT:
  646. case OGE:
  647. case OGT:
  648. case OEQ:
  649. case ONE:
  650. /*
  651. * relational operators, make right side simple
  652. * if same, put constant on left to get movq
  653. */
  654. if(r->complex > l->complex || r->addable == 20) {
  655. n->left = r;
  656. n->right = l;
  657. n->op = invrel[relindex(n->op)];
  658. }
  659. break;
  660. }
  661. }
  662. void
  663. indx(Node *n)
  664. {
  665. Node *l, *r;
  666. int t;
  667. if(debug['x'])
  668. prtree(n, "indx");
  669. t = 0;
  670. loop:
  671. l = n->left;
  672. r = n->right;
  673. switch(r->addable) {
  674. default:
  675. if(t) {
  676. diag(n, "bad indx");
  677. break;
  678. }
  679. n->right = l;
  680. n->left = r;
  681. t++;
  682. goto loop;
  683. case 10:
  684. case 11:
  685. if(l->op == ONAME && r->op == ONAME)
  686. if(l->etype == TIND)
  687. if(r->etype != TIND) {
  688. n->right = l;
  689. n->left = r;
  690. goto loop;
  691. }
  692. if(l->addable == 1 || l->addable == 12) {
  693. n->right = l;
  694. n->left = r;
  695. goto loop;
  696. }
  697. case 1:
  698. case 12:
  699. break;
  700. }
  701. if(l->addable != 7) {
  702. idx.regtree = l;
  703. idx.scale = 0;
  704. } else
  705. if(l->right->addable == 20) {
  706. idx.regtree = l->left;
  707. idx.scale = l->right->vconst;
  708. } else {
  709. idx.regtree = l->right;
  710. idx.scale = l->left->vconst;
  711. }
  712. t = ewidth[idx.regtree->type->etype];
  713. if(t == SZ_LONG)
  714. idx.scale += 4;
  715. else
  716. if(t != SZ_SHORT)
  717. diag(n, "index not W or L");
  718. idx.basetree = r;
  719. if(debug['x']) {
  720. print("scale = %d\n", idx.scale);
  721. prtree(idx.regtree, "index");
  722. prtree(idx.basetree, "base");
  723. }
  724. }
  725. void
  726. bcomplex(Node *n)
  727. {
  728. complex(n);
  729. if(n->type != T)
  730. if(tcompat(n, T, n->type, tnot))
  731. n->type = T;
  732. if(n->type != T) {
  733. bool64(n);
  734. doinc(n, PRE);
  735. boolgen(n, 1, D_NONE, Z, n);
  736. } else
  737. gbranch(OGOTO);
  738. }
  739. Node*
  740. nodconst(long v)
  741. {
  742. return (Node*)v;
  743. }