sgen.c 11 KB

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