sgen.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "gc.h"
  2. void
  3. noretval(int n)
  4. {
  5. if(n & 1) {
  6. gins(ANOP, Z, Z);
  7. p->to.type = D_REG;
  8. p->to.reg = REGRET;
  9. }
  10. if(n & 2) {
  11. gins(ANOP, Z, Z);
  12. p->to.type = D_FREG;
  13. p->to.reg = FREGRET;
  14. }
  15. }
  16. /*
  17. * calculate addressability as follows
  18. * CONST ==> 20 $value
  19. * NAME ==> 10 name
  20. * REGISTER ==> 11 register
  21. * INDREG ==> 12 *[(reg)+offset]
  22. * &10 ==> 2 $name
  23. * ADD(2, 20) ==> 2 $name+offset
  24. * ADD(3, 20) ==> 3 $(reg)+offset
  25. * &12 ==> 3 $(reg)+offset
  26. * *11 ==> 11 ??
  27. * *2 ==> 10 name
  28. * *3 ==> 12 *(reg)+offset
  29. * calculate complexity (number of registers)
  30. */
  31. void
  32. xcom(Node *n)
  33. {
  34. Node *l, *r;
  35. int v;
  36. if(n == Z)
  37. return;
  38. l = n->left;
  39. r = n->right;
  40. n->addable = 0;
  41. n->complex = 0;
  42. switch(n->op) {
  43. case OCONST:
  44. n->addable = 20;
  45. return;
  46. case OREGISTER:
  47. n->addable = 11;
  48. return;
  49. case OINDREG:
  50. n->addable = 12;
  51. return;
  52. case ONAME:
  53. n->addable = 10;
  54. return;
  55. case OADDR:
  56. xcom(l);
  57. if(l->addable == 10)
  58. n->addable = 2;
  59. if(l->addable == 12)
  60. n->addable = 3;
  61. break;
  62. case OIND:
  63. xcom(l);
  64. if(l->addable == 11)
  65. n->addable = 12;
  66. if(l->addable == 3)
  67. n->addable = 12;
  68. if(l->addable == 2)
  69. n->addable = 10;
  70. break;
  71. case OADD:
  72. xcom(l);
  73. xcom(r);
  74. if(l->addable == 20) {
  75. if(r->addable == 2)
  76. n->addable = 2;
  77. if(r->addable == 3)
  78. n->addable = 3;
  79. }
  80. if(r->addable == 20) {
  81. if(l->addable == 2)
  82. n->addable = 2;
  83. if(l->addable == 3)
  84. n->addable = 3;
  85. }
  86. break;
  87. case OASMUL:
  88. case OASLMUL:
  89. xcom(l);
  90. xcom(r);
  91. v = vlog(r);
  92. if(v >= 0) {
  93. n->op = OASASHL;
  94. r->vconst = v;
  95. r->type = types[TINT];
  96. }
  97. break;
  98. case OMUL:
  99. case OLMUL:
  100. xcom(l);
  101. xcom(r);
  102. v = vlog(r);
  103. if(v >= 0) {
  104. n->op = OASHL;
  105. r->vconst = v;
  106. r->type = types[TINT];
  107. }
  108. v = vlog(l);
  109. if(v >= 0) {
  110. n->op = OASHL;
  111. n->left = r;
  112. n->right = l;
  113. r = l;
  114. l = n->left;
  115. r->vconst = v;
  116. r->type = types[TINT];
  117. simplifyshift(n);
  118. }
  119. break;
  120. case OASLDIV:
  121. xcom(l);
  122. xcom(r);
  123. v = vlog(r);
  124. if(v >= 0) {
  125. n->op = OASLSHR;
  126. r->vconst = v;
  127. r->type = types[TINT];
  128. }
  129. break;
  130. case OLDIV:
  131. xcom(l);
  132. xcom(r);
  133. v = vlog(r);
  134. if(v >= 0) {
  135. n->op = OLSHR;
  136. r->vconst = v;
  137. r->type = types[TINT];
  138. simplifyshift(n);
  139. }
  140. break;
  141. case OASLMOD:
  142. xcom(l);
  143. xcom(r);
  144. v = vlog(r);
  145. if(v >= 0) {
  146. n->op = OASAND;
  147. r->vconst--;
  148. }
  149. break;
  150. case OLMOD:
  151. xcom(l);
  152. xcom(r);
  153. v = vlog(r);
  154. if(v >= 0) {
  155. n->op = OAND;
  156. r->vconst--;
  157. }
  158. break;
  159. case OLSHR:
  160. case OASHL:
  161. case OASHR:
  162. xcom(l);
  163. xcom(r);
  164. simplifyshift(n);
  165. break;
  166. default:
  167. if(l != Z)
  168. xcom(l);
  169. if(r != Z)
  170. xcom(r);
  171. break;
  172. }
  173. if(n->addable >= 10)
  174. return;
  175. if(l != Z)
  176. n->complex = l->complex;
  177. if(r != Z) {
  178. if(r->complex == n->complex)
  179. n->complex = r->complex+1;
  180. else
  181. if(r->complex > n->complex)
  182. n->complex = r->complex;
  183. }
  184. if(n->complex == 0)
  185. n->complex++;
  186. if(com64(n))
  187. return;
  188. switch(n->op) {
  189. case OFUNC:
  190. n->complex = FNX;
  191. break;
  192. case OEQ:
  193. case ONE:
  194. case OLE:
  195. case OLT:
  196. case OGE:
  197. case OGT:
  198. case OHI:
  199. case OHS:
  200. case OLO:
  201. case OLS:
  202. /*
  203. * immediate operators, make const on right
  204. */
  205. if(l->op == OCONST) {
  206. n->left = r;
  207. n->right = l;
  208. n->op = invrel[relindex(n->op)];
  209. }
  210. break;
  211. case OADD:
  212. case OXOR:
  213. case OAND:
  214. case OOR:
  215. /*
  216. * immediate operators, make const on right
  217. */
  218. if(l->op == OCONST) {
  219. n->left = r;
  220. n->right = l;
  221. }
  222. break;
  223. }
  224. }