ch7bin.c 8.4 KB

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