arith.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* $Header$ */
  2. /* A R I T H M E T I C C O N V E R S I O N S */
  3. /* This file contains the routines for the various conversions that
  4. may befall operands in C. It is structurally a mess, but I haven't
  5. decided yet whether I can't find the right structure or the
  6. semantics of C is a mess.
  7. */
  8. #include "botch_free.h"
  9. #include "nobitfield.h"
  10. #include "alloc.h"
  11. #include "idf.h"
  12. #include "arith.h"
  13. #include "type.h"
  14. #include "label.h"
  15. #include "expr.h"
  16. #include "Lpars.h"
  17. #include "storage.h"
  18. #include "field.h"
  19. #include "mes.h"
  20. extern char *symbol2str();
  21. extern char options[];
  22. int
  23. arithbalance(e1p, oper, e2p) /* RM 6.6 */
  24. struct expr **e1p, **e2p;
  25. {
  26. /* The expressions *e1p and *e2p are balanced to be operands
  27. of the arithmetic operator oper.
  28. */
  29. register int t1, t2, u1, u2;
  30. t1 = any2arith(e1p, oper);
  31. t2 = any2arith(e2p, oper);
  32. /* Now t1 and t2 are either INT or LONG or DOUBLE */
  33. if (t1 == DOUBLE && t2 != DOUBLE)
  34. t2 = int2float(e2p, double_type);
  35. else
  36. if (t2 == DOUBLE && t1 != DOUBLE)
  37. t1 = int2float(e1p, double_type);
  38. else
  39. if (t1 == DOUBLE)
  40. return DOUBLE;
  41. /* Now they are INT or LONG */
  42. u1 = (*e1p)->ex_type->tp_unsigned;
  43. u2 = (*e2p)->ex_type->tp_unsigned;
  44. /* if either is long, the other will be */
  45. if (t1 == LONG && t2 != LONG)
  46. t2 = int2int(e2p, u2 ? ulong_type : long_type);
  47. else
  48. if (t2 == LONG && t1 != LONG)
  49. t1 = int2int(e1p, u1 ? ulong_type : long_type);
  50. /* if either is unsigned, the other will be */
  51. if (u1 && !u2)
  52. t2 = int2int(e2p, (t1 == LONG) ? ulong_type : uint_type);
  53. else
  54. if (!u1 && u2)
  55. t1 = int2int(e1p, (t2 == LONG) ? ulong_type : uint_type);
  56. return t1;
  57. }
  58. relbalance(e1p, oper, e2p)
  59. register struct expr **e1p, **e2p;
  60. {
  61. /* The expressions *e1p and *e2p are balanced to be operands
  62. of the relational operator oper.
  63. */
  64. if ((*e1p)->ex_type->tp_fund == FUNCTION)
  65. function2pointer(e1p);
  66. if ((*e2p)->ex_type->tp_fund == FUNCTION)
  67. function2pointer(e2p);
  68. if ((*e1p)->ex_type->tp_fund == POINTER)
  69. ch76pointer(e2p, oper, (*e1p)->ex_type);
  70. else
  71. if ((*e2p)->ex_type->tp_fund == POINTER)
  72. ch76pointer(e1p, oper, (*e2p)->ex_type);
  73. else
  74. if ( (*e1p)->ex_type == (*e2p)->ex_type &&
  75. (*e1p)->ex_type->tp_fund == ENUM
  76. )
  77. {}
  78. else
  79. arithbalance(e1p, oper, e2p);
  80. }
  81. ch76pointer(expp, oper, tp)
  82. register struct expr **expp;
  83. register struct type *tp;
  84. {
  85. /* Checks whether *expp may be compared to tp using oper,
  86. as described in chapter 7.6 and 7.7.
  87. tp is known to be a pointer.
  88. */
  89. if ((*expp)->ex_type->tp_fund == POINTER) {
  90. if ((*expp)->ex_type != tp)
  91. ch7cast(expp, oper, tp);
  92. }
  93. else
  94. if ( is_integral_type((*expp)->ex_type) &&
  95. ( !options['R'] /* we don't care */ ||
  96. (oper == EQUAL || oper == NOTEQUAL || oper == ':')
  97. )
  98. ) /* ch 7.7 */
  99. ch7cast(expp, CAST, tp);
  100. else {
  101. expr_error(*expp, "%s on %s and pointer",
  102. symbol2str(oper),
  103. symbol2str((*expp)->ex_type->tp_fund)
  104. );
  105. ch7cast(expp, oper, tp);
  106. }
  107. }
  108. int
  109. any2arith(expp, oper)
  110. register struct expr **expp;
  111. {
  112. /* Turns any expression into int_type, long_type or
  113. double_type.
  114. */
  115. int fund = (*expp)->ex_type->tp_fund;
  116. switch (fund) {
  117. case CHAR:
  118. case SHORT:
  119. int2int(expp,
  120. (*expp)->ex_type->tp_unsigned ? uint_type : int_type);
  121. break;
  122. case INT:
  123. case LONG:
  124. break;
  125. case ENUM:
  126. if ( is_test_op(oper) || oper == '=' || oper == PARCOMMA ||
  127. oper == ',' || oper == ':' ||
  128. ( !options['R'] &&
  129. (is_arith_op(oper) || is_asgn_op(oper))
  130. )
  131. )
  132. {}
  133. else
  134. expr_warning(*expp, "%s on enum", symbol2str(oper));
  135. int2int(expp, int_type);
  136. break;
  137. case FLOAT:
  138. float2float(expp, double_type);
  139. break;
  140. case DOUBLE:
  141. break;
  142. #ifndef NOBITFIELD
  143. case FIELD:
  144. field2arith(expp);
  145. break;
  146. #endif NOBITFIELD
  147. default:
  148. expr_error(*expp, "operator %s on non-numerical operand (%s)",
  149. symbol2str(oper), symbol2str(fund));
  150. case ERRONEOUS:
  151. erroneous2int(expp);
  152. break;
  153. }
  154. return (*expp)->ex_type->tp_fund;
  155. }
  156. erroneous2int(expp)
  157. struct expr **expp;
  158. {
  159. /* the (erroneous) expression *expp is replaced by an
  160. int expression
  161. */
  162. int flags = (*expp)->ex_flags;
  163. free_expression(*expp);
  164. *expp = intexpr((arith)0, INT);
  165. (*expp)->ex_flags = (flags | EX_ERROR);
  166. }
  167. struct expr *
  168. arith2arith(tp, oper, expr)
  169. struct type *tp;
  170. int oper;
  171. struct expr *expr;
  172. {
  173. /* arith2arith constructs a new expression containing a
  174. run-time conversion between some arithmetic types.
  175. */
  176. register struct expr *new = new_expr();
  177. clear((char *)new, sizeof(struct expr));
  178. new->ex_file = expr->ex_file;
  179. new->ex_line = expr->ex_line;
  180. new->ex_type = tp;
  181. new->ex_class = Type;
  182. return new_oper(tp, new, oper, expr);
  183. }
  184. int
  185. int2int(expp, tp)
  186. register struct expr **expp;
  187. struct type *tp;
  188. {
  189. /* The expression *expp, which is of some integral type, is
  190. converted to the integral type tp.
  191. */
  192. if (is_cp_cst(*expp)) {
  193. (*expp)->ex_type = tp;
  194. cut_size(*expp);
  195. }
  196. else {
  197. *expp = arith2arith(tp, INT2INT, *expp);
  198. }
  199. return (*expp)->ex_type->tp_fund;
  200. }
  201. int
  202. int2float(expp, tp)
  203. struct expr **expp;
  204. struct type *tp;
  205. {
  206. /* The expression *expp, which is of some integral type, is
  207. converted to the floating type tp.
  208. */
  209. fp_used = 1;
  210. *expp = arith2arith(tp, INT2FLOAT, *expp);
  211. return (*expp)->ex_type->tp_fund;
  212. }
  213. float2int(expp, tp)
  214. struct expr **expp;
  215. struct type *tp;
  216. {
  217. /* The expression *expp, which is of some floating type, is
  218. converted to the integral type tp.
  219. */
  220. fp_used = 1;
  221. *expp = arith2arith(tp, FLOAT2INT, *expp);
  222. }
  223. float2float(expp, tp)
  224. struct expr **expp;
  225. struct type *tp;
  226. {
  227. /* The expression *expp, which is of some floating type, is
  228. converted to the floating type tp.
  229. There is no need for an explicit conversion operator
  230. if the expression is a constant.
  231. */
  232. fp_used = 1;
  233. if (is_fp_cst(*expp)) {
  234. (*expp)->ex_type = tp;
  235. }
  236. else {
  237. *expp = arith2arith(tp, FLOAT2FLOAT, *expp);
  238. }
  239. }
  240. array2pointer(expp)
  241. struct expr **expp;
  242. {
  243. /* The expression, which must be an array, it is converted
  244. to a pointer.
  245. */
  246. (*expp)->ex_type =
  247. construct_type(POINTER, (*expp)->ex_type->tp_up, (arith)0);
  248. }
  249. function2pointer(expp)
  250. struct expr **expp;
  251. {
  252. /* The expression, which must be a function, it is converted
  253. to a pointer to the function.
  254. */
  255. (*expp)->ex_type =
  256. construct_type(POINTER, (*expp)->ex_type, (arith)0);
  257. }
  258. opnd2integral(expp, oper)
  259. struct expr **expp;
  260. int oper;
  261. {
  262. register int fund = (*expp)->ex_type->tp_fund;
  263. if (fund != INT && fund != LONG) {
  264. expr_error(*expp, "%s operand to %s",
  265. symbol2str(fund), symbol2str(oper));
  266. erroneous2int(expp);
  267. /* fund = INT; */
  268. }
  269. }
  270. opnd2logical(expp, oper)
  271. struct expr **expp;
  272. int oper;
  273. {
  274. register int fund;
  275. if ((*expp)->ex_type->tp_fund == FUNCTION)
  276. function2pointer(expp);
  277. #ifndef NOBITFIELD
  278. else
  279. if ((*expp)->ex_type->tp_fund == FIELD)
  280. field2arith(expp);
  281. #endif NOBITFIELD
  282. fund = (*expp)->ex_type->tp_fund;
  283. switch (fund) {
  284. case CHAR:
  285. case SHORT:
  286. case INT:
  287. case LONG:
  288. case ENUM:
  289. case POINTER:
  290. case FLOAT:
  291. case DOUBLE:
  292. break;
  293. default:
  294. expr_error(*expp, "%s operand to %s",
  295. symbol2str(fund), symbol2str(oper));
  296. case ERRONEOUS:
  297. erroneous2int(expp);
  298. break;
  299. }
  300. }
  301. opnd2test(expp, oper)
  302. struct expr **expp;
  303. {
  304. opnd2logical(expp, oper);
  305. if ((*expp)->ex_class == Oper && is_test_op((*expp)->OP_OPER))
  306. { /* It is already a test */ }
  307. else
  308. ch7bin(expp, NOTEQUAL, intexpr((arith)0, INT));
  309. }
  310. int
  311. is_test_op(oper)
  312. {
  313. switch (oper) {
  314. case '<':
  315. case '>':
  316. case LESSEQ:
  317. case GREATEREQ:
  318. case EQUAL:
  319. case NOTEQUAL:
  320. case '!':
  321. case AND:
  322. case OR: /* && and || also impose a test */
  323. return 1;
  324. default:
  325. return 0;
  326. }
  327. /*NOTREACHED*/
  328. }
  329. int
  330. is_arith_op(oper)
  331. {
  332. switch (oper) {
  333. case '*':
  334. case '/':
  335. case '%':
  336. case '+':
  337. case '-':
  338. case LEFT:
  339. case RIGHT:
  340. case '&':
  341. case '^':
  342. case '|':
  343. return 1;
  344. default:
  345. return 0;
  346. }
  347. }
  348. int
  349. is_asgn_op(oper)
  350. {
  351. switch (oper) {
  352. case '=':
  353. case PLUSAB:
  354. case MINAB:
  355. case TIMESAB:
  356. case DIVAB:
  357. case MODAB:
  358. case LEFTAB:
  359. case RIGHTAB:
  360. case ANDAB:
  361. case ORAB:
  362. case XORAB:
  363. return 1;
  364. default:
  365. return 0;
  366. }
  367. }
  368. any2opnd(expp, oper)
  369. struct expr **expp;
  370. {
  371. if (!*expp)
  372. return;
  373. switch ((*expp)->ex_type->tp_fund) { /* RM 7.1 */
  374. case CHAR:
  375. case SHORT:
  376. case ENUM:
  377. case FLOAT:
  378. any2arith(expp, oper);
  379. break;
  380. case ARRAY:
  381. array2pointer(expp);
  382. break;
  383. #ifndef NOBITFIELD
  384. case FIELD:
  385. field2arith(expp);
  386. break;
  387. #endif NOBITFIELD
  388. }
  389. }
  390. #ifndef NOBITFIELD
  391. field2arith(expp)
  392. struct expr **expp;
  393. {
  394. /* The expression to extract the bitfield value from the
  395. memory word is put in the tree.
  396. */
  397. register struct type *tp = (*expp)->ex_type->tp_up;
  398. register struct field *fd = (*expp)->ex_type->tp_field;
  399. register struct type *atype = tp->tp_unsigned ? uword_type : word_type;
  400. (*expp)->ex_type = atype;
  401. if (atype->tp_unsigned) { /* don't worry about the sign bit */
  402. ch7bin(expp, RIGHT, intexpr((arith)fd->fd_shift, INT));
  403. ch7bin(expp, '&', intexpr(fd->fd_mask, INT));
  404. }
  405. else { /* take care of the sign bit: sign extend if needed */
  406. register arith bits_in_type = atype->tp_size * 8;
  407. ch7bin(expp, LEFT,
  408. intexpr(bits_in_type - fd->fd_width - fd->fd_shift,
  409. INT)
  410. );
  411. ch7bin(expp, RIGHT, intexpr(bits_in_type - fd->fd_width, INT));
  412. }
  413. ch7cast(expp, CAST, tp); /* restore its original type */
  414. }
  415. #endif NOBITFIELD
  416. /* switch_sign_fp() negates the given floating constant expression
  417. The lexical analyser has reserved an extra byte of space in front
  418. of the string containing the representation of the floating
  419. constant. This byte contains the '-' character and we have to
  420. take care of the first byte the fl_value pointer points to.
  421. */
  422. switch_sign_fp(expr)
  423. struct expr *expr;
  424. {
  425. if (*(expr->FL_VALUE) == '-')
  426. ++(expr->FL_VALUE);
  427. else
  428. --(expr->FL_VALUE);
  429. }