cstoper.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* C O N S T A N T E X P R E S S I O N H A N D L I N G */
  7. #include "target_sizes.h"
  8. #include "idf.h"
  9. #include "arith.h"
  10. #include "type.h"
  11. #include "label.h"
  12. #include "expr.h"
  13. #include "sizes.h"
  14. #include "Lpars.h"
  15. #include "assert.h"
  16. long mach_long_sign; /* sign bit of the machine long */
  17. int mach_long_size; /* size of long on this machine == sizeof(long) */
  18. long full_mask[MAXSIZE];/* full_mask[1] == 0XFF, full_mask[2] == 0XFFFF, .. */
  19. arith max_int; /* maximum integer on target machine */
  20. arith max_unsigned; /* maximum unsigned on target machine */
  21. cstbin(expp, oper, expr)
  22. register struct expr **expp, *expr;
  23. {
  24. /* The operation oper is performed on the constant
  25. expressions *expp(ld) and expr(ct), and the result restored in
  26. *expp.
  27. */
  28. register arith o1 = (*expp)->VL_VALUE;
  29. register arith o2 = expr->VL_VALUE;
  30. int uns = (*expp)->ex_type->tp_unsigned;
  31. ASSERT(is_ld_cst(*expp) && is_cp_cst(expr));
  32. switch (oper) {
  33. case '*':
  34. o1 *= o2;
  35. break;
  36. case '/':
  37. if (o2 == 0) {
  38. expr_error(expr, "division by 0");
  39. break;
  40. }
  41. if (uns) {
  42. /* this is more of a problem than you might
  43. think on C compilers which do not have
  44. unsigned long.
  45. */
  46. if (o2 & mach_long_sign) {/* o2 > max_long */
  47. o1 = ! (o1 >= 0 || o1 < o2);
  48. /* this is the unsigned test
  49. o1 < o2 for o2 > max_long
  50. */
  51. }
  52. else { /* o2 <= max_long */
  53. long half, bit, hdiv, hrem, rem;
  54. half = (o1 >> 1) & ~mach_long_sign;
  55. bit = o1 & 01;
  56. /* now o1 == 2 * half + bit
  57. and half <= max_long
  58. and bit <= max_long
  59. */
  60. hdiv = half / o2;
  61. hrem = half % o2;
  62. rem = 2 * hrem + bit;
  63. o1 = 2 * hdiv + (rem < 0 || rem >= o2);
  64. /* that is the unsigned compare
  65. rem >= o2 for o2 <= max_long
  66. */
  67. }
  68. }
  69. else
  70. o1 /= o2;
  71. break;
  72. case '%':
  73. if (o2 == 0) {
  74. expr_error(expr, "modulo by 0");
  75. break;
  76. }
  77. if (uns) {
  78. if (o2 & mach_long_sign) {/* o2 > max_long */
  79. o1 = (o1 >= 0 || o1 < o2) ? o1 : o1 - o2;
  80. /* this is the unsigned test
  81. o1 < o2 for o2 > max_long
  82. */
  83. }
  84. else { /* o2 <= max_long */
  85. long half, bit, hrem, rem;
  86. half = (o1 >> 1) & ~mach_long_sign;
  87. bit = o1 & 01;
  88. /* now o1 == 2 * half + bit
  89. and half <= max_long
  90. and bit <= max_long
  91. */
  92. hrem = half % o2;
  93. rem = 2 * hrem + bit;
  94. o1 = (rem < 0 || rem >= o2) ? rem - o2 : rem;
  95. }
  96. }
  97. else
  98. o1 %= o2;
  99. break;
  100. case '+':
  101. o1 += o2;
  102. break;
  103. case '-':
  104. o1 -= o2;
  105. break;
  106. case LEFT:
  107. o1 <<= o2;
  108. break;
  109. case RIGHT:
  110. if (o2 == 0)
  111. break;
  112. if (uns) {
  113. o1 >>= 1;
  114. o1 &= ~mach_long_sign;
  115. o1 >>= (o2-1);
  116. }
  117. else
  118. o1 >>= o2;
  119. break;
  120. case '<':
  121. {
  122. arith tmp = o1;
  123. o1 = o2;
  124. o2 = tmp;
  125. }
  126. /* Fall through */
  127. case '>':
  128. if (uns) {
  129. o1 = (o1 & mach_long_sign ?
  130. (o2 & mach_long_sign ? o1 > o2 : 1) :
  131. (o2 & mach_long_sign ? 0 : o1 > o2)
  132. );
  133. }
  134. else
  135. o1 = o1 > o2;
  136. break;
  137. case LESSEQ:
  138. {
  139. arith tmp = o1;
  140. o1 = o2;
  141. o2 = tmp;
  142. }
  143. /* Fall through */
  144. case GREATEREQ:
  145. if (uns) {
  146. o1 = (o1 & mach_long_sign ?
  147. (o2 & mach_long_sign ? o1 >= o2 : 1) :
  148. (o2 & mach_long_sign ? 0 : o1 >= o2)
  149. );
  150. }
  151. else
  152. o1 = o1 >= o2;
  153. break;
  154. case EQUAL:
  155. o1 = o1 == o2;
  156. break;
  157. case NOTEQUAL:
  158. o1 = o1 != o2;
  159. break;
  160. case '&':
  161. o1 &= o2;
  162. break;
  163. case '|':
  164. o1 |= o2;
  165. break;
  166. case '^':
  167. o1 ^= o2;
  168. break;
  169. }
  170. (*expp)->VL_VALUE = o1;
  171. cut_size(*expp);
  172. (*expp)->ex_flags |= expr->ex_flags;
  173. (*expp)->ex_flags &= ~EX_PARENS;
  174. free_expression(expr);
  175. }
  176. cut_size(expr)
  177. register struct expr *expr;
  178. {
  179. /* The constant value of the expression expr is made to
  180. conform to the size of the type of the expression.
  181. */
  182. register arith o1 = expr->VL_VALUE;
  183. int uns = expr->ex_type->tp_unsigned;
  184. int size = (int) expr->ex_type->tp_size;
  185. ASSERT(expr->ex_class == Value);
  186. if (expr->ex_type->tp_fund == POINTER) {
  187. /* why warn on "ptr-3" ?
  188. This quick hack fixes it
  189. */
  190. uns = 0;
  191. }
  192. if (uns) {
  193. if (o1 & ~full_mask[size])
  194. expr_warning(expr,
  195. "overflow in unsigned constant expression");
  196. o1 &= full_mask[size];
  197. }
  198. else {
  199. int nbits = (int) (mach_long_size - size) * 8;
  200. long remainder = o1 & ~full_mask[size];
  201. if (remainder != 0 && remainder != ~full_mask[size])
  202. expr_warning(expr, "overflow in constant expression");
  203. o1 <<= nbits; /* ??? */
  204. o1 >>= nbits;
  205. }
  206. expr->VL_VALUE = o1;
  207. }
  208. init_cst()
  209. {
  210. register int i = 0;
  211. register arith bt = (arith)0;
  212. while (!(bt < 0)) {
  213. bt = (bt << 8) + 0377, i++;
  214. if (i == MAXSIZE)
  215. fatal("array full_mask too small for this machine");
  216. full_mask[i] = bt;
  217. }
  218. mach_long_size = i;
  219. mach_long_sign = 1L << (mach_long_size * 8 - 1);
  220. if ((int)long_size < mach_long_size)
  221. fatal("sizeof (long) insufficient on this machine");
  222. max_int = full_mask[(int)int_size] & ~(1L << ((int)int_size * 8 - 1));
  223. max_unsigned = full_mask[(int)int_size];
  224. }