arith.c 11 KB

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