sgen.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 ==> 11 name+value(SB/SP)
  303. * note that 10 is no longer generated
  304. * CONST ==> 20 $value
  305. * *(20) ==> 21 value
  306. * &(10) ==> 12 $name+value(SB)
  307. * &(11) ==> 1 $name+value(SP)
  308. * (12) + (20) ==> 12 fold constants
  309. * (1) + (20) ==> 1 fold constants
  310. * *(12) ==> 10 back to name
  311. * *(1) ==> 11 back to name
  312. *
  313. * (2,10,11) + (20) ==> 2 indirect w offset
  314. * (2) ==> &13
  315. * *(10,11) ==> 13 indirect, no index
  316. *
  317. * (20) * (X) ==> 7 multiplier in indexing
  318. * (X,7) + (12,1) ==> 8 adder in indexing (addresses)
  319. * (X,7) + (10,11,2) ==> 8 adder in indexing (names)
  320. * (8) ==> &9 index, almost addressable
  321. *
  322. * (X)++ ==> X fake addressability
  323. *
  324. * calculate complexity (number of registers)
  325. */
  326. void
  327. xcom(Node *n)
  328. {
  329. Node *l, *r;
  330. int g;
  331. if(n == Z)
  332. return;
  333. l = n->left;
  334. r = n->right;
  335. n->complex = 0;
  336. n->addable = 0;
  337. switch(n->op) {
  338. case OCONST:
  339. n->addable = 20;
  340. break;
  341. case ONAME:
  342. n->addable = 11; /* difference to make relocatable */
  343. break;
  344. case OREGISTER:
  345. n->addable = 12;
  346. break;
  347. case OADDR:
  348. xcom(l);
  349. if(l->addable == 10)
  350. n->addable = 12;
  351. else
  352. if(l->addable == 11)
  353. n->addable = 1;
  354. break;
  355. case OADD:
  356. xcom(l);
  357. xcom(r);
  358. if(n->type->etype != TIND)
  359. break;
  360. if(l->addable == 20)
  361. switch(r->addable) {
  362. case 12:
  363. case 1:
  364. n->addable = r->addable;
  365. goto brk;
  366. }
  367. if(r->addable == 20)
  368. switch(l->addable) {
  369. case 12:
  370. case 1:
  371. n->addable = l->addable;
  372. goto brk;
  373. }
  374. break;
  375. case OIND:
  376. xcom(l);
  377. if(l->op == OADDR) {
  378. l = l->left;
  379. l->type = n->type;
  380. *n = *l;
  381. return;
  382. }
  383. switch(l->addable) {
  384. case 20:
  385. n->addable = 21;
  386. break;
  387. case 1:
  388. n->addable = 11;
  389. break;
  390. case 12:
  391. n->addable = 10;
  392. break;
  393. }
  394. break;
  395. case OASHL:
  396. xcom(l);
  397. xcom(r);
  398. if(typev[n->type->etype])
  399. break;
  400. g = vconst(r);
  401. if(g >= 0 && g < 4)
  402. n->addable = 7;
  403. break;
  404. case OMUL:
  405. case OLMUL:
  406. xcom(l);
  407. xcom(r);
  408. if(typev[n->type->etype])
  409. break;
  410. g = vlog(r);
  411. if(g >= 0) {
  412. n->op = OASHL;
  413. r->vconst = g;
  414. if(g < 4)
  415. n->addable = 7;
  416. break;
  417. }
  418. g = vlog(l);
  419. if(g >= 0) {
  420. n->left = r;
  421. n->right = l;
  422. l = r;
  423. r = n->right;
  424. n->op = OASHL;
  425. r->vconst = g;
  426. if(g < 4)
  427. n->addable = 7;
  428. break;
  429. }
  430. break;
  431. case ODIV:
  432. case OLDIV:
  433. xcom(l);
  434. xcom(r);
  435. if(typev[n->type->etype])
  436. break;
  437. g = vlog(r);
  438. if(g >= 0) {
  439. if(n->op == ODIV)
  440. n->op = OASHR;
  441. else
  442. n->op = OLSHR;
  443. r->vconst = g;
  444. }
  445. break;
  446. case OSUB:
  447. xcom(l);
  448. xcom(r);
  449. if(typev[n->type->etype])
  450. break;
  451. if(vconst(l) == 0) {
  452. n->op = ONEG;
  453. n->left = r;
  454. n->right = Z;
  455. }
  456. break;
  457. case OXOR:
  458. xcom(l);
  459. xcom(r);
  460. if(typev[n->type->etype])
  461. break;
  462. if(vconst(l) == -1) {
  463. n->op = OCOM;
  464. n->left = r;
  465. n->right = Z;
  466. }
  467. break;
  468. case OASMUL:
  469. case OASLMUL:
  470. xcom(l);
  471. xcom(r);
  472. if(typev[n->type->etype])
  473. break;
  474. g = vlog(r);
  475. if(g >= 0) {
  476. n->op = OASASHL;
  477. r->vconst = g;
  478. }
  479. goto aseae;
  480. case OASDIV:
  481. case OASLDIV:
  482. xcom(l);
  483. xcom(r);
  484. if(typev[n->type->etype])
  485. break;
  486. g = vlog(r);
  487. if(g >= 0) {
  488. if(n->op == OASDIV)
  489. n->op = OASASHR;
  490. else
  491. n->op = OASLSHR;
  492. r->vconst = g;
  493. }
  494. goto aseae;
  495. case OASLMOD:
  496. case OASMOD:
  497. xcom(l);
  498. xcom(r);
  499. if(typev[n->type->etype])
  500. break;
  501. aseae: /* hack that there are no byte/short mul/div operators */
  502. if(n->type->etype == TCHAR || n->type->etype == TSHORT) {
  503. n->right = new1(OCAST, n->right, Z);
  504. n->right->type = types[TLONG];
  505. n->type = types[TLONG];
  506. }
  507. if(n->type->etype == TUCHAR || n->type->etype == TUSHORT) {
  508. n->right = new1(OCAST, n->right, Z);
  509. n->right->type = types[TULONG];
  510. n->type = types[TULONG];
  511. }
  512. goto asop;
  513. case OASXOR:
  514. case OASOR:
  515. case OASADD:
  516. case OASSUB:
  517. case OASLSHR:
  518. case OASASHR:
  519. case OASASHL:
  520. case OASAND:
  521. case OAS:
  522. xcom(l);
  523. xcom(r);
  524. if(typev[n->type->etype])
  525. break;
  526. asop:
  527. if(l->addable > INDEXED &&
  528. l->complex < FNX &&
  529. r && r->complex < FNX)
  530. n->addable = l->addable;
  531. break;
  532. case OPOSTINC:
  533. case OPREINC:
  534. case OPOSTDEC:
  535. case OPREDEC:
  536. xcom(l);
  537. if(typev[n->type->etype])
  538. break;
  539. if(l->addable > INDEXED &&
  540. l->complex < FNX)
  541. n->addable = l->addable;
  542. break;
  543. default:
  544. if(l != Z)
  545. xcom(l);
  546. if(r != Z)
  547. xcom(r);
  548. break;
  549. }
  550. brk:
  551. n->complex = 0;
  552. if(n->addable >= 10)
  553. return;
  554. if(l != Z)
  555. n->complex = l->complex;
  556. if(r != Z) {
  557. if(r->complex == n->complex)
  558. n->complex = r->complex+1;
  559. else
  560. if(r->complex > n->complex)
  561. n->complex = r->complex;
  562. }
  563. if(n->complex == 0)
  564. n->complex++;
  565. if(com64(n))
  566. return;
  567. switch(n->op) {
  568. case OFUNC:
  569. n->complex = FNX;
  570. break;
  571. case OADD:
  572. case OMUL:
  573. case OLMUL:
  574. case OXOR:
  575. case OAND:
  576. case OOR:
  577. /*
  578. * symmetric operators, make right side simple
  579. * if same, put constant on left to get movq
  580. */
  581. if(r->complex > l->complex ||
  582. (r->complex == l->complex && r->addable == 20)) {
  583. n->left = r;
  584. n->right = l;
  585. }
  586. break;
  587. case OLE:
  588. case OLT:
  589. case OGE:
  590. case OGT:
  591. case OEQ:
  592. case ONE:
  593. /*
  594. * relational operators, make right side simple
  595. * if same, put constant on left to get movq
  596. */
  597. if(r->complex > l->complex || r->addable == 20) {
  598. n->left = r;
  599. n->right = l;
  600. n->op = invrel[relindex(n->op)];
  601. }
  602. break;
  603. }
  604. }
  605. void
  606. bcomplex(Node *n)
  607. {
  608. complex(n);
  609. if(n->type != T)
  610. if(tcompat(n, T, n->type, tnot))
  611. n->type = T;
  612. if(n->type != T) {
  613. bool64(n);
  614. doinc(n, PRE);
  615. boolgen(n, 1, D_NONE, Z, n);
  616. } else
  617. gbranch(OGOTO);
  618. }
  619. Node*
  620. nodconst(long v)
  621. {
  622. return (Node*)v;
  623. }