ch3bin.c 10 KB

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