arith.c 11 KB

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