ch3bin.c 9.7 KB

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