ch7bin.c 9.0 KB

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