arith.c 15 KB

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