arith.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. if ((*expp)->ex_type != error_type)
  102. error("%s on %s and pointer",
  103. symbol2str(oper),
  104. symbol2str((*expp)->ex_type->tp_fund)
  105. );
  106. (*expp)->ex_type = error_type;
  107. ch7cast(expp, oper, tp);
  108. }
  109. }
  110. int
  111. any2arith(expp, oper)
  112. register struct expr **expp;
  113. {
  114. /* Turns any expression into int_type, long_type or
  115. double_type.
  116. */
  117. int fund = (*expp)->ex_type->tp_fund;
  118. switch (fund) {
  119. case CHAR:
  120. case SHORT:
  121. int2int(expp,
  122. (*expp)->ex_type->tp_unsigned ? uint_type : int_type);
  123. break;
  124. case INT:
  125. case LONG:
  126. break;
  127. case ENUM:
  128. if ( is_test_op(oper) || oper == '=' || oper == PARCOMMA ||
  129. oper == ',' || oper == ':' ||
  130. ( !options['R'] &&
  131. (is_arith_op(oper) || is_asgn_op(oper))
  132. )
  133. )
  134. {}
  135. else
  136. warning("%s on enum", symbol2str(oper));
  137. int2int(expp, int_type);
  138. break;
  139. case FLOAT:
  140. float2float(expp, double_type);
  141. break;
  142. case DOUBLE:
  143. break;
  144. #ifndef NOBITFIELD
  145. case FIELD:
  146. field2arith(expp);
  147. break;
  148. #endif NOBITFIELD
  149. default:
  150. error("operator %s on non-numerical operand (%s)",
  151. symbol2str(oper), symbol2str(fund));
  152. case ERRONEOUS:
  153. free_expression(*expp);
  154. *expp = intexpr((arith)1, INT);
  155. break;
  156. }
  157. return (*expp)->ex_type->tp_fund;
  158. }
  159. struct expr *
  160. arith2arith(tp, oper, expr)
  161. struct type *tp;
  162. int oper;
  163. struct expr *expr;
  164. {
  165. /* arith2arith constructs a new expression containing a
  166. run-time conversion between some arithmetic types.
  167. */
  168. register struct expr *new = new_expr();
  169. clear((char *)new, sizeof(struct expr));
  170. new->ex_file = expr->ex_file;
  171. new->ex_line = expr->ex_line;
  172. new->ex_type = tp;
  173. new->ex_class = Type;
  174. return new_oper(tp, new, oper, expr);
  175. }
  176. int
  177. int2int(expp, tp)
  178. register struct expr **expp;
  179. struct type *tp;
  180. {
  181. /* The expression *expp, which is of some integral type, is
  182. converted to the integral type tp.
  183. */
  184. if (is_cp_cst(*expp)) {
  185. (*expp)->ex_type = tp;
  186. cut_size(*expp);
  187. }
  188. else {
  189. *expp = arith2arith(tp, INT2INT, *expp);
  190. }
  191. return (*expp)->ex_type->tp_fund;
  192. }
  193. int
  194. int2float(expp, tp)
  195. struct expr **expp;
  196. struct type *tp;
  197. {
  198. /* The expression *expp, which is of some integral type, is
  199. converted to the floating type tp.
  200. */
  201. fp_used = 1;
  202. *expp = arith2arith(tp, INT2FLOAT, *expp);
  203. return (*expp)->ex_type->tp_fund;
  204. }
  205. float2int(expp, tp)
  206. struct expr **expp;
  207. struct type *tp;
  208. {
  209. /* The expression *expp, which is of some floating type, is
  210. converted to the integral type tp.
  211. */
  212. fp_used = 1;
  213. *expp = arith2arith(tp, FLOAT2INT, *expp);
  214. }
  215. float2float(expp, tp)
  216. struct expr **expp;
  217. struct type *tp;
  218. {
  219. /* The expression *expp, which is of some floating type, is
  220. converted to the floating type tp.
  221. There is no need for an explicit conversion operator
  222. if the expression is a constant.
  223. */
  224. fp_used = 1;
  225. if ((*expp)->ex_class == Float) {
  226. (*expp)->ex_type = tp;
  227. }
  228. else {
  229. *expp = arith2arith(tp, FLOAT2FLOAT, *expp);
  230. }
  231. }
  232. array2pointer(expp)
  233. struct expr **expp;
  234. {
  235. /* The expression, which must be an array, it is converted
  236. to a pointer.
  237. */
  238. (*expp)->ex_type =
  239. construct_type(POINTER, (*expp)->ex_type->tp_up, (arith)0);
  240. }
  241. function2pointer(expp)
  242. struct expr **expp;
  243. {
  244. /* The expression, which must be a function, it is converted
  245. to a pointer to the function.
  246. */
  247. (*expp)->ex_type =
  248. construct_type(POINTER, (*expp)->ex_type, (arith)0);
  249. }
  250. opnd2integral(expp, oper)
  251. struct expr **expp;
  252. int oper;
  253. {
  254. register int fund = (*expp)->ex_type->tp_fund;
  255. if (fund != INT && fund != LONG) {
  256. if (fund != ERRONEOUS)
  257. error("%s operand to %s",
  258. symbol2str(fund), symbol2str(oper));
  259. *expp = intexpr((arith)1, INT);
  260. /* fund = INT; */
  261. }
  262. }
  263. opnd2logical(expp, oper)
  264. struct expr **expp;
  265. int oper;
  266. {
  267. register int fund;
  268. if ((*expp)->ex_type->tp_fund == FUNCTION)
  269. function2pointer(expp);
  270. #ifndef NOBITFIELD
  271. else
  272. if ((*expp)->ex_type->tp_fund == FIELD)
  273. field2arith(expp);
  274. #endif NOBITFIELD
  275. fund = (*expp)->ex_type->tp_fund;
  276. switch (fund) {
  277. case CHAR:
  278. case SHORT:
  279. case INT:
  280. case LONG:
  281. case ENUM:
  282. case POINTER:
  283. case FLOAT:
  284. case DOUBLE:
  285. break;
  286. default:
  287. error("%s operand to %s",
  288. symbol2str(fund), symbol2str(oper));
  289. case ERRONEOUS:
  290. *expp = intexpr((arith)1, INT);
  291. break;
  292. }
  293. }
  294. opnd2test(expp, oper)
  295. struct expr **expp;
  296. {
  297. opnd2logical(expp, oper);
  298. if ((*expp)->ex_class == Oper && is_test_op((*expp)->OP_OPER))
  299. { /* It is already a test */ }
  300. else
  301. ch7bin(expp, NOTEQUAL, intexpr((arith)0, INT));
  302. }
  303. int
  304. is_test_op(oper)
  305. {
  306. switch (oper) {
  307. case '<':
  308. case '>':
  309. case LESSEQ:
  310. case GREATEREQ:
  311. case EQUAL:
  312. case NOTEQUAL:
  313. case '!':
  314. case AND:
  315. case OR: /* && and || also impose a test */
  316. return 1;
  317. default:
  318. return 0;
  319. }
  320. /*NOTREACHED*/
  321. }
  322. int
  323. is_arith_op(oper)
  324. {
  325. switch (oper) {
  326. case '*':
  327. case '/':
  328. case '%':
  329. case '+':
  330. case '-':
  331. case LEFT:
  332. case RIGHT:
  333. case '&':
  334. case '^':
  335. case '|':
  336. return 1;
  337. default:
  338. return 0;
  339. }
  340. }
  341. int
  342. is_asgn_op(oper)
  343. {
  344. switch (oper) {
  345. case '=':
  346. case PLUSAB:
  347. case MINAB:
  348. case TIMESAB:
  349. case DIVAB:
  350. case MODAB:
  351. case LEFTAB:
  352. case RIGHTAB:
  353. case ANDAB:
  354. case ORAB:
  355. case XORAB:
  356. return 1;
  357. default:
  358. return 0;
  359. }
  360. }
  361. any2opnd(expp, oper)
  362. struct expr **expp;
  363. {
  364. if (!*expp)
  365. return;
  366. switch ((*expp)->ex_type->tp_fund) { /* RM 7.1 */
  367. case CHAR:
  368. case SHORT:
  369. case ENUM:
  370. case FLOAT:
  371. any2arith(expp, oper);
  372. break;
  373. case ARRAY:
  374. array2pointer(expp);
  375. break;
  376. #ifndef NOBITFIELD
  377. case FIELD:
  378. field2arith(expp);
  379. break;
  380. #endif NOBITFIELD
  381. }
  382. }
  383. #ifndef NOBITFIELD
  384. field2arith(expp)
  385. struct expr **expp;
  386. {
  387. /* The expression to extract the bitfield value from the
  388. memory word is put in the tree.
  389. */
  390. register struct type *tp = (*expp)->ex_type->tp_up;
  391. register struct field *fd = (*expp)->ex_type->tp_field;
  392. register struct type *atype = tp->tp_unsigned ? uword_type : word_type;
  393. (*expp)->ex_type = atype;
  394. if (atype->tp_unsigned) { /* don't worry about the sign bit */
  395. ch7bin(expp, RIGHT, intexpr((arith)fd->fd_shift, INT));
  396. ch7bin(expp, '&', intexpr(fd->fd_mask, INT));
  397. }
  398. else { /* take care of the sign bit: sign extend if needed */
  399. register arith bits_in_type = atype->tp_size * 8;
  400. ch7bin(expp, LEFT,
  401. intexpr(bits_in_type - fd->fd_width - fd->fd_shift, INT)
  402. );
  403. ch7bin(expp, RIGHT, intexpr(bits_in_type - fd->fd_width, INT));
  404. }
  405. ch7cast(expp, CAST, tp); /* restore its original type */
  406. }
  407. #endif NOBITFIELD
  408. /* switch_sign_fp() negates the given floating constant expression
  409. The lexical analyser has reserved an extra byte of space in front
  410. of the string containing the representation of the floating
  411. constant. This byte contains the '-' character and we have to
  412. take care of the first byte the fl_value pointer points to.
  413. */
  414. switch_sign_fp(expr)
  415. struct expr *expr;
  416. {
  417. if (*(expr->FL_VALUE) == '-')
  418. ++(expr->FL_VALUE);
  419. else
  420. --(expr->FL_VALUE);
  421. }