sgen.c 3.5 KB

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