sgen.c 13 KB

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