arith.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. /* A R I T H M E T I C C O N V E R S I O N S */
  7. /* This file contains the routines for the various conversions that
  8. may befall operands in C. It is structurally a mess, but I haven't
  9. decided yet whether I can't find the right structure or the
  10. semantics of C is a mess.
  11. */
  12. #include <alloc.h>
  13. #include "debug.h"
  14. #include "lint.h"
  15. #include "nobitfield.h"
  16. #include "idf.h"
  17. #include <flt_arith.h>
  18. #include "arith.h"
  19. #include "sizes.h"
  20. #include "type.h"
  21. #include "proto.h"
  22. #include "label.h"
  23. #include "expr.h"
  24. #include "Lpars.h"
  25. #include "field.h"
  26. #include "mes.h"
  27. #include "assert.h"
  28. #include "ch3.h"
  29. #include "ch3bin.h"
  30. #include "code_c.h"
  31. #include "conversion.h"
  32. #include "cstoper.h"
  33. #include "expr_loc.h"
  34. #include "error.h"
  35. #include <symbol2str.h>
  36. extern char options[];
  37. void arithbalance(struct expr **e1p, int oper, struct expr **e2p) /* 3.1.2.5 */
  38. {
  39. /* The expressions *e1p and *e2p are balanced to be operands
  40. of the arithmetic operator oper.
  41. We check here if the EX_PTRDIFF flag should be retained.
  42. They are set to zero in because one of the opreands might
  43. have a floating type, in which case the flags shouldn't
  44. travel upward in the expression tree.
  45. */
  46. int t1, t2, u1, u2;
  47. int shifting = (oper == LEFT || oper == RIGHT
  48. || oper == LEFTAB || oper == RIGHTAB);
  49. int ptrdiff = 0;
  50. t1 = any2arith(e1p, oper);
  51. t2 = any2arith(e2p, oper);
  52. if (int_size != pointer_size) {
  53. if ((ptrdiff = ((*e1p)->ex_flags & EX_PTRDIFF))
  54. || ((*e2p)->ex_flags & EX_PTRDIFF)) {
  55. if (!((*e1p)->ex_flags & EX_PTRDIFF) && t1 == LONG)
  56. ptrdiff = 0;
  57. if (!((*e2p)->ex_flags & EX_PTRDIFF) && t2 == LONG
  58. && !shifting)
  59. ptrdiff = 0;
  60. }
  61. /* Now turn off ptrdiff flags */
  62. (*e1p)->ex_flags &= ~EX_PTRDIFF;
  63. (*e2p)->ex_flags &= ~EX_PTRDIFF;
  64. }
  65. /* Now t1 and t2 are either INT, LONG, FLOAT, DOUBLE, or LNGDBL */
  66. /* If any operand has the type long double, the other operand
  67. is converted to long double.
  68. */
  69. /* ??? t1 == LNGDBL, t2 == DOUBLE */
  70. if (t1 == LNGDBL) {
  71. if (t2 != LNGDBL) {
  72. if (t2 == DOUBLE || t2 == FLOAT)
  73. float2float(e2p, lngdbl_type);
  74. else
  75. int2float(e2p, lngdbl_type);
  76. }
  77. return;
  78. } else if (t2 == LNGDBL) {
  79. if (t1 != LNGDBL)
  80. {
  81. if (t1 == DOUBLE || t1 == FLOAT)
  82. {
  83. float2float(e1p, lngdbl_type);
  84. }
  85. else
  86. {
  87. int2float(e1p, lngdbl_type);
  88. }
  89. }
  90. return;
  91. }
  92. /* If any operand has the type double, the other operand
  93. is converted to double.
  94. */
  95. if (t1 == DOUBLE) {
  96. if (t2 == FLOAT)
  97. float2float(e2p, double_type);
  98. else if (t2 != DOUBLE)
  99. int2float(e2p, double_type);
  100. return;
  101. } else if (t2 == DOUBLE) {
  102. if (t1 == FLOAT)
  103. float2float(e1p, double_type);
  104. else if (t1 != DOUBLE)
  105. int2float(e1p, double_type);
  106. return;
  107. }
  108. /* If any operand has the type float, the other operand
  109. is converted to float.
  110. */
  111. if (t1 == FLOAT) {
  112. if (t2 != FLOAT)
  113. int2float(e2p, float_type);
  114. return;
  115. } else if (t2 == FLOAT) {
  116. if (t1 != FLOAT)
  117. int2float(e1p, float_type);
  118. return;
  119. }
  120. /* Now they are INT or LONG */
  121. u1 = (*e1p)->ex_type->tp_unsigned;
  122. u2 = (*e2p)->ex_type->tp_unsigned;
  123. /* If either operand has type unsigned long int, the other
  124. operand is converted to unsigned long int.
  125. */
  126. if (t1 == LONG && u1 && (t2 != LONG || !u2))
  127. t2 = int2int(e2p, ulong_type);
  128. else if (t2 == LONG && u2 && (t1 != LONG || !u1)
  129. && !shifting) /* ??? */
  130. t1 = int2int(e1p, ulong_type);
  131. /* If one operand has type long int and the other has type unsigned
  132. int, if a long int can represent all values of an unsigned int,
  133. the operand of type unsigned int is converted to long int; if
  134. a long int cannot represent all values of an unsigned int,
  135. both operands are converted to unsigned long int.
  136. */
  137. if (t1 == LONG && t2 == INT && u2)
  138. t2 = int2int(e2p, (int_size<long_size)? long_type : ulong_type);
  139. else if (t2 == LONG && t1 == INT && u1 && !shifting) /* ??? */
  140. t1 = int2int(e1p, (int_size<long_size)? long_type : ulong_type);
  141. /* If either operand has type long int, the other operand is con-
  142. verted to long int.
  143. */
  144. if (t1 == LONG && t2 != LONG)
  145. t2 = int2int(e2p, long_type);
  146. else
  147. if (t2 == LONG && t1 != LONG && !shifting) /* ??? */
  148. t1 = int2int(e1p, long_type);
  149. u1 = (*e1p)->ex_type->tp_unsigned;
  150. u2 = (*e2p)->ex_type->tp_unsigned;
  151. /* If either operand has type unsigned int, the other operand
  152. is converted to unsigned int.
  153. Otherwise, both operands have type int.
  154. */
  155. if (u1 && !u2 && !shifting)
  156. t2 = int2int(e2p, (t1 == LONG) ? ulong_type : uint_type);
  157. else
  158. if (!u1 && u2 && !shifting)
  159. t1 = int2int(e1p, (t2 == LONG) ? ulong_type : uint_type);
  160. if (int_size != pointer_size) {
  161. if (ptrdiff) {
  162. (*e1p)->ex_flags |= EX_PTRDIFF;
  163. (*e2p)->ex_flags |= EX_PTRDIFF;
  164. }
  165. }
  166. }
  167. void relbalance(struct expr **e1p, int oper, struct expr **e2p)
  168. {
  169. /* The expressions *e1p and *e2p are balanced to be operands
  170. of the relational operator oper, or the ':'.
  171. Care is taken to switch the operands in case of a
  172. null-pointer constant. This is done so that ch3cast()
  173. allows assignments of a null-pointer to a function
  174. pointer.
  175. */
  176. struct expr *e1 = *e1p, *e2 = *e2p;
  177. struct expr *tmpexpr;
  178. if (e1->ex_type->tp_fund == POINTER
  179. && is_cp_cst(e1)
  180. && e1->VL_VALUE == 0) {
  181. tmpexpr = e1;
  182. e1 = e2;
  183. e2 = tmpexpr;
  184. }
  185. if (e1->ex_type->tp_fund == POINTER)
  186. ch3pointer(e2p, oper, e1->ex_type);
  187. else if (e2->ex_type->tp_fund == POINTER)
  188. ch3pointer(e1p, oper, e2->ex_type);
  189. else if (e1->ex_type == e2->ex_type
  190. && e1->ex_type->tp_fund == ENUM) {}
  191. else if (oper == ':'
  192. && e1->ex_type->tp_fund == VOID
  193. && e2->ex_type->tp_fund == VOID) {}
  194. else
  195. arithbalance(e1p, oper, e2p);
  196. }
  197. void ch3pointer(struct expr **expp, int oper, struct type *tp)
  198. {
  199. /* Checks whether *expp may be compared to tp using oper,
  200. as described in chapter 3.3.8 and 3.3.9.
  201. tp is known to be a pointer.
  202. */
  203. struct expr *exp = *expp;
  204. if (exp->ex_type->tp_fund == POINTER) {
  205. if (exp->ex_type != tp)
  206. ch3cast(expp, oper, tp);
  207. }
  208. else
  209. if (is_integral_type(exp->ex_type)) {
  210. if ((oper != EQUAL && oper != NOTEQUAL && oper != ':')
  211. || !(is_cp_cst(exp) && exp->VL_VALUE == 0)) {
  212. expr_error(exp,"%s on %s and pointer",
  213. symbol2str(oper),
  214. symbol2str(exp->ex_type->tp_fund));
  215. }
  216. ch3cast(expp, CAST, tp);
  217. }
  218. else {
  219. expr_error(exp, "%s on %s and pointer",
  220. symbol2str(oper),
  221. symbol2str(exp->ex_type->tp_fund)
  222. );
  223. ch3cast(expp, oper, tp);
  224. }
  225. }
  226. int any2arith(struct expr **expp, int oper)
  227. {
  228. /* Turns any expression into int_type, long_type,
  229. float_type, double_type or lngdbl_type.
  230. */
  231. int fund;
  232. switch (fund = (*expp)->ex_type->tp_fund) {
  233. case CHAR:
  234. case SHORT:
  235. ASSERT((*expp)->ex_type->tp_size <= int_type->tp_size);
  236. if ((*expp)->ex_type->tp_unsigned
  237. && (*expp)->ex_type->tp_size == int_type->tp_size) {
  238. int2int(expp, uint_type);
  239. } else {
  240. int2int(expp, int_type);
  241. }
  242. break;
  243. case INT:
  244. case LONG:
  245. break;
  246. case ENUM:
  247. #ifndef LINT
  248. /* we do not want this conversion for lint, since we
  249. want to keep enums and ints separate
  250. */
  251. int2int(expp, int_type);
  252. #endif /* LINT */
  253. break;
  254. case FLOAT:
  255. /* only when it is a parameter and the default promotion should
  256. occur. Hence this code is moved to any2parameter().
  257. float2float(expp, double_type);
  258. break;
  259. */
  260. case DOUBLE:
  261. case LNGDBL:
  262. break;
  263. #ifndef NOBITFIELD
  264. case FIELD:
  265. field2arith(expp);
  266. break;
  267. #endif /* NOBITFIELD */
  268. default:
  269. expr_error(*expp, "operator %s on non-numerical operand (%s)",
  270. symbol2str(oper), symbol2str(fund));
  271. case ERRONEOUS:
  272. erroneous2int(expp);
  273. break;
  274. }
  275. return (*expp)->ex_type->tp_fund;
  276. }
  277. void erroneous2int(struct expr **expp)
  278. {
  279. /* the (erroneous) expression *expp is replaced by an
  280. int expression
  281. */
  282. register struct expr *exp = *expp;
  283. int flags = exp->ex_flags;
  284. free_expression(exp);
  285. exp = intexpr((arith)0, INT);
  286. exp->ex_flags = (flags | EX_ERROR);
  287. *expp = exp;
  288. }
  289. struct expr *arith2arith(struct type *tp, int oper, struct expr *expr)
  290. {
  291. /* arith2arith constructs a new expression containing a
  292. run-time conversion between some arithmetic types.
  293. */
  294. struct expr *new = new_expr();
  295. new->ex_file = expr->ex_file;
  296. new->ex_line = expr->ex_line;
  297. new->ex_type = tp;
  298. new->ex_class = Type;
  299. return new_oper(tp, new, oper, expr);
  300. }
  301. int int2int(struct expr **expp, struct type *tp)
  302. {
  303. /* The expression *expp, which is of some integral type, is
  304. converted to the integral type tp.
  305. */
  306. struct expr *exp = *expp;
  307. if (is_cp_cst(exp)) {
  308. struct type *tp1 = exp->ex_type;
  309. exp->ex_type = tp;
  310. if (! tp1->tp_unsigned && tp->tp_unsigned) {
  311. /* Avoid "unreal" overflow warnings, such as
  312. caused by f.i.:
  313. unsigned int x = ~0;
  314. unsigned int y = -1;
  315. */
  316. extern long full_mask[];
  317. long remainder = exp->VL_VALUE &
  318. ~full_mask[(int)(tp->tp_size)];
  319. if (remainder == 0 ||
  320. remainder == ~full_mask[(int)(tp->tp_size)]) {
  321. exp->VL_VALUE &= ~remainder;
  322. }
  323. }
  324. cut_size(exp);
  325. }
  326. else {
  327. exp = arith2arith(tp, INT2INT, exp);
  328. }
  329. *expp = exp;
  330. return exp->ex_type->tp_fund;
  331. }
  332. /* With compile-time constants, we don't set fp_used, since this is done
  333. * only when necessary in eval.c.
  334. */
  335. void int2float(struct expr **expp, struct type *tp)
  336. {
  337. /* The expression *expp, which is of some integral type, is
  338. converted to the floating type tp.
  339. */
  340. struct expr *exp = *expp;
  341. int uns = exp->ex_type->tp_unsigned;
  342. if (is_cp_cst(exp)) {
  343. exp->ex_type = tp;
  344. exp->ex_class = Float;
  345. flt_arith2flt(exp->VL_VALUE, &(exp->FL_ARITH), uns);
  346. }
  347. else {
  348. fp_used = 1;
  349. *expp = arith2arith(tp, INT2FLOAT, *expp);
  350. }
  351. }
  352. void float2int(struct expr **expp, struct type *tp)
  353. {
  354. /* The expression *expp, which is of some floating type, is
  355. converted to the integral type tp.
  356. */
  357. struct expr *ex = *expp;
  358. if (is_fp_cst(ex)) {
  359. arith ar = flt_flt2arith(&ex->FL_ARITH, tp->tp_unsigned);
  360. if (flt_status == FLT_OVFL)
  361. expr_warning(ex,"overflow in float to int conversion");
  362. else if (flt_status == FLT_UNFL)
  363. expr_warning(ex,"underflow in float to unsigned conversion");
  364. ex->ex_type = tp;
  365. /* The following lines are copied from fill_int_expr */
  366. ex->ex_class = Value;
  367. ex->VL_CLASS = Const;
  368. ex->VL_VALUE = ar;
  369. cut_size(ex);
  370. } else {
  371. fp_used = 1;
  372. *expp = arith2arith(tp, FLOAT2INT, ex);
  373. }
  374. }
  375. void float2float(struct expr**expp, struct type *tp)
  376. {
  377. /* The expression *expp, which is of some floating type, is
  378. converted to the floating type tp.
  379. There is no need for an explicit conversion operator
  380. if the expression is a constant.
  381. */
  382. if (is_fp_cst(*expp))
  383. (*expp)->ex_type = tp;
  384. else {
  385. fp_used = 1;
  386. *expp = arith2arith(tp, FLOAT2FLOAT, *expp);
  387. }
  388. }
  389. void array2pointer(struct expr *exp)
  390. {
  391. /* The expression, which must be an array, is converted
  392. to a pointer.
  393. */
  394. exp->ex_type = construct_type(POINTER, exp->ex_type->tp_up
  395. , /* exp->ex_type->tp_typequal */ 0
  396. , (arith)0, NO_PROTO);
  397. }
  398. void function2pointer(struct expr *exp)
  399. {
  400. /* The expression, which must be a function, is converted
  401. to a pointer to the function.
  402. */
  403. exp->ex_type = construct_type(POINTER, exp->ex_type, 0,
  404. (arith)0, NO_PROTO);
  405. }
  406. void string2pointer(struct expr *ex)
  407. {
  408. /* The expression, which must be a string constant, is converted
  409. to a pointer to the string-containing area.
  410. */
  411. label lbl;
  412. lbl = code_string(ex->SG_VALUE, ex->SG_LEN);
  413. ex->ex_class = Value;
  414. ex->VL_CLASS = Label;
  415. ex->VL_LBL = lbl;
  416. ex->VL_VALUE = (arith)0;
  417. }
  418. void opnd2integral(struct expr **expp, int oper)
  419. {
  420. int fund = (*expp)->ex_type->tp_fund;
  421. if (fund != INT && fund != LONG) {
  422. expr_error(*expp, "%s operand to %s",
  423. symbol2str(fund), symbol2str(oper));
  424. erroneous2int(expp);
  425. /* fund = INT; */
  426. }
  427. }
  428. void opnd2logical(struct expr **expp, int oper)
  429. {
  430. int fund = (*expp)->ex_type->tp_fund;
  431. if (fund == FUNCTION || fund == ARRAY) {
  432. expr_warning(*expp, "%s operand to %s",
  433. symbol2str(fund),
  434. symbol2str(oper));
  435. if (fund == FUNCTION) function2pointer(*expp);
  436. else array2pointer(*expp);
  437. }
  438. #ifndef NOBITFIELD
  439. else
  440. if (fund == FIELD)
  441. field2arith(expp);
  442. #endif /* NOBITFIELD */
  443. switch (fund = (*expp)->ex_type->tp_fund) {
  444. case CHAR:
  445. case SHORT:
  446. case INT:
  447. case LONG:
  448. case ENUM:
  449. case POINTER:
  450. case FLOAT:
  451. case DOUBLE:
  452. case LNGDBL:
  453. break;
  454. default:
  455. expr_error(*expp, "%s operand to %s",
  456. symbol2str(fund), symbol2str(oper));
  457. case ERRONEOUS:
  458. erroneous2int(expp);
  459. break;
  460. }
  461. }
  462. void opnd2test(struct expr **expp, int oper)
  463. {
  464. opnd2logical(expp, oper);
  465. if ((*expp)->ex_class == Oper) {
  466. switch((*expp)->OP_OPER) {
  467. case '<':
  468. case '>':
  469. case LESSEQ:
  470. case GREATEREQ:
  471. case EQUAL:
  472. case NOTEQUAL:
  473. case '!':
  474. case AND:
  475. case OR: /* && and || also impose a test */
  476. /* It is already a test */
  477. return;
  478. case ',':
  479. opnd2test(&((*expp)->OP_RIGHT), oper);
  480. return;
  481. }
  482. }
  483. ch3bin(expp, NOTEQUAL, intexpr((arith)0, INT));
  484. }
  485. void any2opnd(struct expr **expp, int oper)
  486. {
  487. if (!*expp)
  488. return;
  489. if (oper == SIZEOF || oper == ADDRESSOF) return;
  490. switch ((*expp)->ex_type->tp_fund) {
  491. case CHAR:
  492. case SHORT:
  493. case ENUM:
  494. #if 0
  495. case FLOAT: /* not necessary anymore */
  496. #endif
  497. any2arith(expp, oper);
  498. break;
  499. case ARRAY:
  500. array2pointer(*expp);
  501. break;
  502. case POINTER:
  503. if ((*expp)->ex_class == String)
  504. string2pointer(*expp);
  505. break;
  506. case FUNCTION:
  507. function2pointer(*expp);
  508. break;
  509. #ifndef NOBITFIELD
  510. case FIELD:
  511. field2arith(expp);
  512. break;
  513. #endif /* NOBITFIELD */
  514. }
  515. }
  516. void any2parameter(struct expr **expp)
  517. {
  518. /* To handle default argument promotions
  519. */
  520. any2opnd(expp, '(');
  521. if (int_size != pointer_size)
  522. if ((*expp)->ex_flags & EX_PTRDIFF)
  523. expr_warning(*expp, "pointer difference caused long expression");
  524. if ((*expp)->ex_type->tp_fund == FLOAT)
  525. float2float(expp, double_type);
  526. }
  527. #ifndef NOBITFIELD
  528. void field2arith(struct expr **expp)
  529. {
  530. /* The expression to extract the bitfield value from the
  531. memory word is put in the tree.
  532. */
  533. struct type *tp = (*expp)->ex_type->tp_up;
  534. struct field *fd = (*expp)->ex_type->tp_field;
  535. (*expp)->ex_type = word_type;
  536. if (tp->tp_unsigned) { /* don't worry about the sign bit */
  537. if (fd->fd_width >= 8 * (int)word_size)
  538. (*expp)->ex_type = uword_type;
  539. ch3bin(expp, RIGHT, intexpr((arith)fd->fd_shift, INT));
  540. ch3bin(expp, '&', intexpr(fd->fd_mask, INT));
  541. }
  542. else { /* take care of the sign bit: sign extend if needed */
  543. arith other_bits = (int)word_size * 8 - fd->fd_width;
  544. ch3bin(expp, LEFT,
  545. intexpr(other_bits - fd->fd_shift,
  546. INT)
  547. );
  548. ch3bin(expp, RIGHT, intexpr(other_bits, INT));
  549. }
  550. }
  551. #endif /* NOBITFIELD */
  552. /* switch_sign_fp() negates the given floating constant expression,
  553. * and frees the string representing the old value.
  554. */
  555. void switch_sign_fp(struct expr *expr)
  556. {
  557. flt_umin(&(expr->FL_ARITH));
  558. }