arith.c 10 KB

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