arith.c 14 KB

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