sgen.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 v, nr;
  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 OASMUL:
  96. case OASLMUL:
  97. xcom(l);
  98. xcom(r);
  99. v = vlog(r);
  100. if(v >= 0) {
  101. n->op = OASASHL;
  102. r->vconst = v;
  103. r->type = types[TINT];
  104. }
  105. break;
  106. case OMUL:
  107. case OLMUL:
  108. xcom(l);
  109. xcom(r);
  110. v = vlog(r);
  111. if(v >= 0) {
  112. n->op = OASHL;
  113. r->vconst = v;
  114. r->type = types[TINT];
  115. }
  116. v = vlog(l);
  117. if(v >= 0) {
  118. n->op = OASHL;
  119. n->left = r;
  120. n->right = l;
  121. r = l;
  122. l = n->left;
  123. r->vconst = v;
  124. r->type = types[TINT];
  125. simplifyshift(n);
  126. }
  127. break;
  128. case OASLDIV:
  129. xcom(l);
  130. xcom(r);
  131. v = vlog(r);
  132. if(v >= 0) {
  133. n->op = OASLSHR;
  134. r->vconst = v;
  135. r->type = types[TINT];
  136. }
  137. break;
  138. case OLDIV:
  139. xcom(l);
  140. xcom(r);
  141. v = vlog(r);
  142. if(v >= 0) {
  143. n->op = OLSHR;
  144. r->vconst = v;
  145. r->type = types[TINT];
  146. simplifyshift(n);
  147. }
  148. break;
  149. case OASLMOD:
  150. xcom(l);
  151. xcom(r);
  152. v = vlog(r);
  153. if(v >= 0) {
  154. n->op = OASAND;
  155. r->vconst--;
  156. }
  157. break;
  158. case OLMOD:
  159. xcom(l);
  160. xcom(r);
  161. v = vlog(r);
  162. if(v >= 0) {
  163. n->op = OAND;
  164. r->vconst--;
  165. }
  166. break;
  167. case OLSHR:
  168. case OASHL:
  169. case OASHR:
  170. xcom(l);
  171. xcom(r);
  172. simplifyshift(n);
  173. break;
  174. default:
  175. if(l != Z)
  176. xcom(l);
  177. if(r != Z)
  178. xcom(r);
  179. break;
  180. }
  181. if(n->addable >= 10)
  182. return;
  183. if(l != Z)
  184. n->complex = l->complex;
  185. if(r != Z) {
  186. nr = 1;
  187. if(r->type != T && typev[r->type->etype] || n->type != T && typev[n->type->etype]) {
  188. nr = 2;
  189. if(n->op == OMUL || n->op == OLMUL)
  190. nr += 3;
  191. }
  192. if(r->complex == n->complex)
  193. n->complex = r->complex+nr;
  194. else
  195. if(r->complex > n->complex)
  196. n->complex = r->complex;
  197. }
  198. if(n->complex == 0){
  199. n->complex++;
  200. if(n->type != T && typev[n->type->etype])
  201. n->complex++;
  202. }
  203. if(com64(n))
  204. return;
  205. switch(n->op) {
  206. case OFUNC:
  207. n->complex = FNX;
  208. break;
  209. case OEQ:
  210. case ONE:
  211. case OLE:
  212. case OLT:
  213. case OGE:
  214. case OGT:
  215. case OHI:
  216. case OHS:
  217. case OLO:
  218. case OLS:
  219. /*
  220. * immediate operators, make const on right
  221. */
  222. if(l->op == OCONST) {
  223. n->left = r;
  224. n->right = l;
  225. n->op = invrel[relindex(n->op)];
  226. }
  227. break;
  228. case OADD:
  229. case OXOR:
  230. case OAND:
  231. case OOR:
  232. /*
  233. * immediate operators, make const on right
  234. */
  235. if(l->op == OCONST) {
  236. n->left = r;
  237. n->right = l;
  238. }
  239. break;
  240. }
  241. }