sgen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 t;
  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 OASLMUL:
  88. case OASMUL:
  89. xcom(l);
  90. xcom(r);
  91. t = vlog(r);
  92. if(t >= 0) {
  93. n->op = OASASHL;
  94. r->vconst = t;
  95. r->type = types[TINT];
  96. }
  97. break;
  98. case OMUL:
  99. case OLMUL:
  100. xcom(l);
  101. xcom(r);
  102. t = vlog(l);
  103. if(t >= 0) {
  104. n->left = r;
  105. n->right = l;
  106. l = r;
  107. r = n->right;
  108. }
  109. t = vlog(r);
  110. if(t >= 0) {
  111. n->op = OASHL;
  112. r->vconst = t;
  113. r->type = types[TINT];
  114. simplifyshift(n);
  115. }
  116. break;
  117. case OASLDIV:
  118. xcom(l);
  119. xcom(r);
  120. t = vlog(r);
  121. if(t >= 0) {
  122. n->op = OASLSHR;
  123. r->vconst = t;
  124. r->type = types[TINT];
  125. }
  126. break;
  127. case OLDIV:
  128. xcom(l);
  129. xcom(r);
  130. t = vlog(r);
  131. if(t >= 0) {
  132. n->op = OLSHR;
  133. r->vconst = t;
  134. r->type = types[TINT];
  135. simplifyshift(n);
  136. }
  137. break;
  138. case OASLMOD:
  139. xcom(l);
  140. xcom(r);
  141. t = vlog(r);
  142. if(t >= 0) {
  143. n->op = OASAND;
  144. r->vconst--;
  145. }
  146. break;
  147. case OLMOD:
  148. xcom(l);
  149. xcom(r);
  150. t = vlog(r);
  151. if(t >= 0) {
  152. n->op = OAND;
  153. r->vconst--;
  154. }
  155. break;
  156. case OLSHR:
  157. case OASHL:
  158. case OASHR:
  159. xcom(l);
  160. xcom(r);
  161. simplifyshift(n);
  162. break;
  163. default:
  164. if(l != Z)
  165. xcom(l);
  166. if(r != Z)
  167. xcom(r);
  168. break;
  169. }
  170. if(n->addable >= 10)
  171. return;
  172. if(l != Z)
  173. n->complex = l->complex;
  174. if(r != Z) {
  175. if(r->complex == n->complex)
  176. n->complex = r->complex+1;
  177. else
  178. if(r->complex > n->complex)
  179. n->complex = r->complex;
  180. }
  181. if(n->complex == 0)
  182. n->complex++;
  183. if(com64(n))
  184. return;
  185. switch(n->op) {
  186. case OFUNC:
  187. n->complex = FNX;
  188. break;
  189. case OADD:
  190. case OXOR:
  191. case OAND:
  192. case OOR:
  193. case OEQ:
  194. case ONE:
  195. /*
  196. * immediate operators, make const on right
  197. */
  198. if(l->op == OCONST) {
  199. n->left = r;
  200. n->right = l;
  201. }
  202. break;
  203. }
  204. }