ch7bin.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* $Header$ */
  2. /* SEMANTIC ANALYSIS (CHAPTER 7RM) -- BINARY OPERATORS */
  3. #include "botch_free.h" /* UF */
  4. #include "idf.h"
  5. #include "arith.h"
  6. #include "type.h"
  7. #include "struct.h"
  8. #include "label.h"
  9. #include "expr.h"
  10. #include "Lpars.h"
  11. #include "storage.h"
  12. extern char options[];
  13. extern char *symbol2str();
  14. /* This chapter asks for the repeated application of code to handle
  15. an operation that may be executed at compile time or at run time,
  16. depending on the constancy of the operands.
  17. */
  18. ch7bin(expp, oper, expr)
  19. register struct expr **expp;
  20. struct expr *expr;
  21. {
  22. /* apply binary operator oper between *expp and expr.
  23. NB: don't swap operands if op is one of the op= operators!!!
  24. */
  25. any2opnd(expp, oper);
  26. any2opnd(&expr, oper);
  27. switch (oper) {
  28. int fund;
  29. case '[': /* RM 7.1 */
  30. /* RM 14.3 states that indexing follows the commutative laws */
  31. switch ((*expp)->ex_type->tp_fund) {
  32. case POINTER:
  33. case ARRAY:
  34. break;
  35. case ERRONEOUS:
  36. return;
  37. default: /* unindexable */
  38. switch (expr->ex_type->tp_fund) {
  39. case POINTER:
  40. case ARRAY:
  41. break;
  42. case ERRONEOUS:
  43. return;
  44. default:
  45. expr_error(*expp,
  46. "indexing an object of type %s",
  47. symbol2str((*expp)->ex_type->tp_fund));
  48. return;
  49. }
  50. break;
  51. }
  52. ch7bin(expp, '+', expr);
  53. ch7mon('*', expp);
  54. break;
  55. case '(': /* RM 7.1 */
  56. if ( (*expp)->ex_type->tp_fund == POINTER &&
  57. (*expp)->ex_type->tp_up->tp_fund == FUNCTION
  58. ) {
  59. if (options['R'])
  60. warning("function pointer called");
  61. ch7mon('*', expp);
  62. }
  63. if ((*expp)->ex_type->tp_fund != FUNCTION) {
  64. expr_error(*expp, "call of non-function (%s)",
  65. symbol2str((*expp)->ex_type->tp_fund));
  66. /* leave the expression; it may still serve */
  67. free_expression(expr); /* there go the parameters */
  68. }
  69. else
  70. *expp = new_oper((*expp)->ex_type->tp_up,
  71. *expp, '(', expr);
  72. break;
  73. case PARCOMMA: /* RM 7.1 */
  74. if ((*expp)->ex_type->tp_fund == FUNCTION)
  75. function2pointer(expp);
  76. *expp = new_oper(expr->ex_type, *expp, PARCOMMA, expr);
  77. break;
  78. case '%':
  79. case MODAB:
  80. /*** NB "not float" means "integral" !!!
  81. fund = arithbalance(expp, oper, &expr);
  82. if (fund == DOUBLE) {
  83. expr_error(*expp, "floating operand to %s",
  84. symbol2str(oper));
  85. erroneous2int(expp);
  86. }
  87. else
  88. non_commutative_binop(expp, oper, expr);
  89. ***/
  90. opnd2integral(expp, oper);
  91. opnd2integral(&expr, oper);
  92. fund = arithbalance(expp, oper, &expr);
  93. non_commutative_binop(expp, oper, expr);
  94. break;
  95. case '/':
  96. case DIVAB:
  97. fund = arithbalance(expp, oper, &expr);
  98. non_commutative_binop(expp, oper, expr);
  99. break;
  100. case '*':
  101. fund = arithbalance(expp, oper, &expr);
  102. commutative_binop(expp, oper, expr);
  103. break;
  104. case TIMESAB:
  105. fund = arithbalance(expp, oper, &expr);
  106. non_commutative_binop(expp, oper, expr);
  107. break;
  108. case '+':
  109. if (expr->ex_type->tp_fund == POINTER) {
  110. /* swap operands */
  111. struct expr *etmp = expr;
  112. expr = *expp;
  113. *expp = etmp;
  114. }
  115. /*FALLTHROUGH*/
  116. case PLUSAB:
  117. if ((*expp)->ex_type->tp_fund == POINTER) {
  118. pointer_arithmetic(expp, oper, &expr);
  119. if ( expr->ex_type->tp_size !=
  120. (*expp)->ex_type->tp_size
  121. ) {
  122. ch7cast(&expr, CAST, (*expp)->ex_type);
  123. }
  124. pointer_binary(expp, oper, expr);
  125. }
  126. else {
  127. fund = arithbalance(expp, oper, &expr);
  128. if (oper == '+')
  129. commutative_binop(expp, oper, expr);
  130. else
  131. non_commutative_binop(expp, oper, expr);
  132. }
  133. break;
  134. case '-':
  135. case MINAB:
  136. if ((*expp)->ex_type->tp_fund == POINTER) {
  137. if (expr->ex_type->tp_fund == POINTER)
  138. pntminuspnt(expp, oper, expr);
  139. else {
  140. pointer_arithmetic(expp, oper, &expr);
  141. pointer_binary(expp, oper, expr);
  142. }
  143. }
  144. else {
  145. fund = arithbalance(expp, oper, &expr);
  146. non_commutative_binop(expp, oper, expr);
  147. }
  148. break;
  149. case LEFT:
  150. case LEFTAB:
  151. case RIGHT:
  152. case RIGHTAB:
  153. opnd2integral(expp, oper);
  154. opnd2integral(&expr, oper);
  155. fund = arithbalance(expp, oper, &expr); /* ch. 7.5 */
  156. ch7cast(&expr, oper, int_type); /* cvt. rightop to int */
  157. non_commutative_binop(expp, oper, expr);
  158. break;
  159. case '<':
  160. case '>':
  161. case LESSEQ:
  162. case GREATEREQ:
  163. case EQUAL:
  164. case NOTEQUAL:
  165. relbalance(expp, oper, &expr);
  166. non_commutative_binop(expp, oper, expr);
  167. (*expp)->ex_type = int_type;
  168. break;
  169. case '&':
  170. case '^':
  171. case '|':
  172. opnd2integral(expp, oper);
  173. opnd2integral(&expr, oper);
  174. fund = arithbalance(expp, oper, &expr);
  175. commutative_binop(expp, oper, expr);
  176. break;
  177. case ANDAB:
  178. case XORAB:
  179. case ORAB:
  180. opnd2integral(expp, oper);
  181. opnd2integral(&expr, oper);
  182. fund = arithbalance(expp, oper, &expr);
  183. non_commutative_binop(expp, oper, expr);
  184. break;
  185. case AND:
  186. case OR:
  187. opnd2test(expp, oper);
  188. opnd2test(&expr, oper);
  189. if (is_cp_cst(*expp)) {
  190. struct expr *ex = *expp;
  191. /* the following condition is a short-hand for
  192. ((oper == AND) && o1) || ((oper == OR) && !o1)
  193. where o1 == (*expp)->VL_VALUE;
  194. and ((oper == AND) || (oper == OR))
  195. */
  196. if ((oper == AND) == ((*expp)->VL_VALUE != (arith)0))
  197. *expp = expr;
  198. else {
  199. free_expression(expr);
  200. *expp = intexpr((arith)((oper == AND) ? 0 : 1),
  201. INT);
  202. }
  203. free_expression(ex);
  204. }
  205. else
  206. if (is_cp_cst(expr)) {
  207. /* Note!!!: the following condition is a short-hand for
  208. ((oper == AND) && o2) || ((oper == OR) && !o2)
  209. where o2 == expr->VL_VALUE
  210. and ((oper == AND) || (oper == OR))
  211. */
  212. if ((oper == AND) == (expr->VL_VALUE != (arith)0))
  213. free_expression(expr);
  214. else {
  215. if (oper == OR)
  216. expr->VL_VALUE = (arith)1;
  217. ch7bin(expp, ',', expr);
  218. }
  219. }
  220. else
  221. *expp = new_oper(int_type, *expp, oper, expr);
  222. (*expp)->ex_flags |= EX_LOGICAL;
  223. break;
  224. case ':':
  225. if ( is_struct_or_union((*expp)->ex_type->tp_fund)
  226. || is_struct_or_union(expr->ex_type->tp_fund)
  227. ) {
  228. if ((*expp)->ex_type != expr->ex_type) {
  229. expr_error(*expp, "illegal balance");
  230. }
  231. }
  232. else {
  233. relbalance(expp, oper, &expr);
  234. }
  235. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  236. break;
  237. case '?':
  238. opnd2logical(expp, oper);
  239. if (is_cp_cst(*expp))
  240. *expp = (*expp)->VL_VALUE ?
  241. expr->OP_LEFT : expr->OP_RIGHT;
  242. else
  243. *expp = new_oper(expr->ex_type, *expp, oper, expr);
  244. break;
  245. case ',':
  246. if (is_cp_cst(*expp))
  247. *expp = expr;
  248. else
  249. *expp = new_oper(expr->ex_type, *expp, oper, expr);
  250. (*expp)->ex_flags |= EX_COMMA;
  251. break;
  252. }
  253. }
  254. pntminuspnt(expp, oper, expr)
  255. register struct expr **expp, *expr;
  256. {
  257. /* Subtracting two pointers is so complicated it merits a
  258. routine of its own.
  259. */
  260. struct type *up_type = (*expp)->ex_type->tp_up;
  261. if (up_type != expr->ex_type->tp_up) {
  262. expr_error(*expp, "subtracting incompatible pointers");
  263. free_expression(expr);
  264. erroneous2int(expp);
  265. return;
  266. }
  267. /* we hope the optimizer will eliminate the load-time
  268. pointer subtraction
  269. */
  270. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  271. ch7cast(expp, CAST, pa_type); /* ptr-ptr: result has pa_type */
  272. ch7bin(expp, '/',
  273. intexpr(size_of_type(up_type, "object"), pa_type->tp_fund));
  274. ch7cast(expp, CAST, int_type); /* result will be an integer expr */
  275. }
  276. non_commutative_binop(expp, oper, expr)
  277. register struct expr **expp, *expr;
  278. {
  279. /* Constructs in *expp the operation indicated by the operands.
  280. "oper" is a non-commutative operator
  281. */
  282. if (is_cp_cst(expr) && is_cp_cst(*expp))
  283. cstbin(expp, oper, expr);
  284. else
  285. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  286. }
  287. commutative_binop(expp, oper, expr)
  288. register struct expr **expp, *expr;
  289. {
  290. /* Constructs in *expp the operation indicated by the operands.
  291. "oper" is a commutative operator
  292. */
  293. if (is_cp_cst(expr) && is_cp_cst(*expp))
  294. cstbin(expp, oper, expr);
  295. else
  296. if ((*expp)->ex_depth > expr->ex_depth)
  297. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  298. else
  299. *expp = new_oper((*expp)->ex_type, expr, oper, *expp);
  300. }
  301. pointer_arithmetic(expp1, oper, expp2)
  302. register struct expr **expp1, **expp2;
  303. {
  304. /* prepares the integral expression expp2 in order to
  305. apply it to the pointer expression expp1
  306. */
  307. #ifndef NOFLOAT
  308. if (any2arith(expp2, oper) == DOUBLE) {
  309. expr_error(*expp2,
  310. "illegal combination of float and pointer");
  311. erroneous2int(expp2);
  312. }
  313. #endif NOFLOAT
  314. ch7bin( expp2, '*',
  315. intexpr(size_of_type((*expp1)->ex_type->tp_up, "object"),
  316. pa_type->tp_fund)
  317. );
  318. }
  319. pointer_binary(expp, oper, expr)
  320. register struct expr **expp, *expr;
  321. {
  322. /* constructs the pointer arithmetic expression out of
  323. a pointer expression, a binary operator and an integral
  324. expression.
  325. */
  326. if (is_ld_cst(expr) && is_ld_cst(*expp))
  327. cstbin(expp, oper, expr);
  328. else
  329. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  330. }