arith.c 11 KB

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