cstoper.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* $Header$ */
  2. /* 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 */
  3. #include "target_sizes.h" /* UF */
  4. #include "idf.h"
  5. #include "arith.h"
  6. #include "type.h"
  7. #include "label.h"
  8. #include "expr.h"
  9. #include "sizes.h"
  10. #include "Lpars.h"
  11. long mach_long_sign; /* sign bit of the machine long */
  12. int mach_long_size; /* size of long on this machine == sizeof(long) */
  13. long full_mask[MAXSIZE];/* full_mask[1] == 0XFF, full_mask[2] == 0XFFFF, .. */
  14. arith max_int; /* maximum integer on target machine */
  15. arith max_unsigned; /* maximum unsigned on target machine */
  16. cstbin(expp, oper, expr)
  17. struct expr **expp, *expr;
  18. {
  19. /* The operation oper is performed on the constant
  20. expressions *expp and expr, and the result restored in
  21. *expp.
  22. */
  23. arith o1 = (*expp)->VL_VALUE;
  24. arith o2 = expr->VL_VALUE;
  25. int uns = (*expp)->ex_type->tp_unsigned;
  26. switch (oper) {
  27. case '*':
  28. o1 *= o2;
  29. break;
  30. case '/':
  31. if (o2 == 0) {
  32. error("division by 0");
  33. break;
  34. }
  35. if (uns) {
  36. /* this is more of a problem than you might
  37. think on C compilers which do not have
  38. unsigned long.
  39. */
  40. if (o2 & mach_long_sign) {/* o2 > max_long */
  41. o1 = ! (o1 >= 0 || o1 < o2);
  42. /* this is the unsigned test
  43. o1 < o2 for o2 > max_long
  44. */
  45. }
  46. else { /* o2 <= max_long */
  47. long half, bit, hdiv, hrem, rem;
  48. half = (o1 >> 1) & ~mach_long_sign;
  49. bit = o1 & 01;
  50. /* now o1 == 2 * half + bit
  51. and half <= max_long
  52. and bit <= max_long
  53. */
  54. hdiv = half / o2;
  55. hrem = half % o2;
  56. rem = 2 * hrem + bit;
  57. o1 = 2 * hdiv + (rem < 0 || rem >= o2);
  58. /* that is the unsigned compare
  59. rem >= o2 for o2 <= max_long
  60. */
  61. }
  62. }
  63. else
  64. o1 /= o2;
  65. break;
  66. case '%':
  67. if (o2 == 0) {
  68. error("modulo by 0");
  69. break;
  70. }
  71. if (uns) {
  72. if (o2 & mach_long_sign) {/* o2 > max_long */
  73. o1 = (o1 >= 0 || o1 < o2) ? o1 : o1 - o2;
  74. /* this is the unsigned test
  75. o1 < o2 for o2 > max_long
  76. */
  77. }
  78. else { /* o2 <= max_long */
  79. long half, bit, hrem, rem;
  80. half = (o1 >> 1) & ~mach_long_sign;
  81. bit = o1 & 01;
  82. /* now o1 == 2 * half + bit
  83. and half <= max_long
  84. and bit <= max_long
  85. */
  86. hrem = half % o2;
  87. rem = 2 * hrem + bit;
  88. o1 = (rem < 0 || rem >= o2) ? rem - o2 : rem;
  89. }
  90. }
  91. else
  92. o1 %= o2;
  93. break;
  94. case '+':
  95. o1 += o2;
  96. break;
  97. case '-':
  98. o1 -= o2;
  99. break;
  100. case LEFT:
  101. o1 <<= o2;
  102. break;
  103. case RIGHT:
  104. if (o2 == 0)
  105. break;
  106. if (uns) {
  107. o1 >>= 1;
  108. o1 & = ~mach_long_sign;
  109. o1 >>= (o2-1);
  110. }
  111. else
  112. o1 >>= o2;
  113. break;
  114. case '<':
  115. if (uns) {
  116. o1 = (o1 & mach_long_sign ?
  117. (o2 & mach_long_sign ? o1 < o2 : 0) :
  118. (o2 & mach_long_sign ? 1 : o1 < o2)
  119. );
  120. }
  121. else
  122. o1 = o1 < o2;
  123. break;
  124. case '>':
  125. if (uns) {
  126. o1 = (o1 & mach_long_sign ?
  127. (o2 & mach_long_sign ? o1 > o2 : 1) :
  128. (o2 & mach_long_sign ? 0 : o1 > o2)
  129. );
  130. }
  131. else
  132. o1 = o1 > o2;
  133. break;
  134. case LESSEQ:
  135. if (uns) {
  136. o1 = (o1 & mach_long_sign ?
  137. (o2 & mach_long_sign ? o1 <= o2 : 0) :
  138. (o2 & mach_long_sign ? 1 : o1 <= o2)
  139. );
  140. }
  141. else
  142. o1 = o1 <= o2;
  143. break;
  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. }
  175. cut_size(expr)
  176. struct expr *expr;
  177. {
  178. /* The constant value of the expression expr is made to
  179. conform to the size of the type of the expression.
  180. */
  181. arith o1 = expr->VL_VALUE;
  182. int uns = expr->ex_type->tp_unsigned;
  183. int size = (int) expr->ex_type->tp_size;
  184. if (uns) {
  185. if (o1 & ~full_mask[size])
  186. expr_warning(expr,
  187. "overflow in unsigned constant expression");
  188. o1 &= full_mask[size];
  189. }
  190. else {
  191. int nbits = (int) (mach_long_size - size) * 8;
  192. long remainder = o1 & ~full_mask[size];
  193. if (remainder != 0 && remainder != ~full_mask[size])
  194. expr_warning(expr, "overflow in constant expression");
  195. o1 <<= nbits; /* ??? */
  196. o1 >>= nbits;
  197. }
  198. expr->VL_VALUE = o1;
  199. }
  200. init_cst()
  201. {
  202. int i = 0;
  203. arith bt = (arith)0;
  204. while (!(bt < 0)) {
  205. bt = (bt << 8) + 0377, i++;
  206. if (i == MAXSIZE)
  207. fatal("array full_mask too small for this machine");
  208. full_mask[i] = bt;
  209. }
  210. mach_long_size = i;
  211. mach_long_sign = 1 << (mach_long_size * 8 - 1);
  212. if (long_size < mach_long_size)
  213. fatal("sizeof (long) insufficient on this machine");
  214. max_int = full_mask[int_size] & ~(1 << (int_size * 8 - 1));
  215. max_unsigned = full_mask[int_size];
  216. }