arith.c 13 KB

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