expr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. /* EXPRESSION TREE HANDLING */
  7. #include "lint.h"
  8. #include "debug.h"
  9. #include "assert.h"
  10. #include "botch_free.h"
  11. #include <alloc.h>
  12. #include "idf.h"
  13. #include <flt_arith.h>
  14. #include "arith.h"
  15. #include "def.h"
  16. #include "type.h"
  17. #include "label.h"
  18. #include "expr.h"
  19. #include "LLlex.h"
  20. #include "Lpars.h"
  21. #include "decspecs.h"
  22. #include "declar.h"
  23. #include "sizes.h"
  24. #include "level.h"
  25. #include "use_tmp.h"
  26. #include "cstoper.h"
  27. #include <symbol2str.h>
  28. extern char options[];
  29. extern int InSizeof;
  30. int
  31. rank_of(oper)
  32. int oper;
  33. {
  34. /* The rank of the operator oper is returned.
  35. */
  36. switch (oper) {
  37. default:
  38. return 0; /* INT2INT etc. */
  39. case '[':
  40. case '(':
  41. case '.':
  42. case ARROW:
  43. case PARCOMMA:
  44. return 1;
  45. case '!':
  46. case PLUSPLUS:
  47. case MINMIN:
  48. case CAST:
  49. case SIZEOF:
  50. case ADDRESSOF:
  51. return 2; /* monadic */
  52. case '*':
  53. case '/':
  54. case '%':
  55. return 3;
  56. case '+':
  57. case '-':
  58. return 4;
  59. case LEFT:
  60. case RIGHT:
  61. return 5;
  62. case '<':
  63. case '>':
  64. case LESSEQ:
  65. case GREATEREQ:
  66. return 6;
  67. case EQUAL:
  68. case NOTEQUAL:
  69. return 7;
  70. case '&':
  71. return 8;
  72. case '^':
  73. return 9;
  74. case '|':
  75. return 10;
  76. case AND:
  77. return 11;
  78. case OR:
  79. return 12;
  80. case '?':
  81. case ':':
  82. return 13;
  83. case '=':
  84. case PLUSAB:
  85. case MINAB:
  86. case TIMESAB:
  87. case DIVAB:
  88. case MODAB:
  89. case RIGHTAB:
  90. case LEFTAB:
  91. case ANDAB:
  92. case XORAB:
  93. case ORAB:
  94. return 14;
  95. case ',':
  96. return 15;
  97. }
  98. /*NOTREACHED*/
  99. }
  100. dot2expr(expp)
  101. struct expr **expp;
  102. {
  103. /* The token in dot is converted into an expression, a
  104. pointer to which is stored in *expp.
  105. */
  106. register struct expr *ex = new_expr();
  107. *expp = ex;
  108. ex->ex_file = dot.tk_file;
  109. ex->ex_line = dot.tk_line;
  110. switch (DOT) {
  111. case IDENTIFIER:
  112. idf2expr(ex);
  113. break;
  114. case INTEGER:
  115. int2expr(ex);
  116. break;
  117. case FLOATING:
  118. float2expr(ex);
  119. break;
  120. default:
  121. crash("bad conversion to expression");
  122. /*NOTREACHED*/
  123. }
  124. }
  125. idf2expr(expr)
  126. register struct expr *expr;
  127. {
  128. /* Dot contains an identifier which is turned into an
  129. expression.
  130. Note that this constitutes an applied occurrence of
  131. the identifier.
  132. */
  133. register struct idf *idf = dot.tk_idf; /* != 0*/
  134. register struct def *def = idf->id_def;
  135. if (def == 0) {
  136. if (AHEAD == '(') {
  137. /* function call, declare name implicitly (3.3.2.2) */
  138. if (!options['o'])
  139. warning("implicit declaration of function %s"
  140. , idf->id_text);
  141. add_def(idf, EXTERN, funint_type, level);
  142. } else {
  143. if (!is_anon_idf(idf))
  144. error("%s undefined", idf->id_text);
  145. /* declare idf anyway */
  146. add_def(idf, 0, error_type, level);
  147. }
  148. def = idf->id_def;
  149. }
  150. /* now def != 0 */
  151. #ifndef LINT
  152. if (!InSizeof) {
  153. if (! def->df_used) {
  154. #ifndef PREPEND_SCOPES
  155. code_scope(idf->id_text, def);
  156. #endif /* PREPEND_SCOPES */
  157. def->df_used = 1;
  158. }
  159. }
  160. #endif /* LINT */
  161. expr->ex_type = def->df_type;
  162. if (expr->ex_type == error_type) {
  163. expr->ex_flags |= EX_ERROR;
  164. }
  165. expr->ex_lvalue =
  166. ( def->df_type->tp_fund == FUNCTION ||
  167. def->df_type->tp_fund == ARRAY ||
  168. def->df_sc == ENUM
  169. ) ? 0 : 1;
  170. if (def->df_type->tp_typequal & TQ_CONST)
  171. expr->ex_flags |= EX_READONLY;
  172. if (def->df_type->tp_typequal & TQ_VOLATILE)
  173. expr->ex_flags |= EX_VOLATILE;
  174. expr->ex_class = Value;
  175. if (def->df_sc == ENUM) {
  176. expr->VL_CLASS = Const;
  177. expr->VL_VALUE = def->df_address;
  178. }
  179. #ifndef LINT
  180. else
  181. if (def->df_sc == STATIC && def->df_level >= L_LOCAL) {
  182. expr->VL_CLASS = Label;
  183. expr->VL_LBL = def->df_address;
  184. expr->VL_VALUE = (arith)0;
  185. }
  186. #endif /* LINT */
  187. else {
  188. expr->VL_CLASS = Name;
  189. expr->VL_IDF = idf;
  190. expr->VL_VALUE = (arith)0;
  191. }
  192. }
  193. string2expr(expp, str, len)
  194. register struct expr **expp;
  195. int len;
  196. char *str;
  197. {
  198. /* The string in the argument is converted into an expression,
  199. a pointer to which is stored in *expp.
  200. */
  201. register struct expr *ex = new_expr();
  202. *expp = ex;
  203. ex->ex_file = dot.tk_file;
  204. ex->ex_line = dot.tk_line;
  205. ex->ex_type = string_type;
  206. /* ex->ex_type = qualifier_type(ex->ex_type, TQ_CONST); */
  207. ex->ex_flags |= EX_READONLY;
  208. /* ex->ex_lvalue = 0; */
  209. ex->ex_class = String;
  210. ex->SG_VALUE = str;
  211. ex->SG_LEN = len;
  212. }
  213. int2expr(expr)
  214. struct expr *expr;
  215. {
  216. /* Dot contains an integer constant which is turned
  217. into an expression.
  218. */
  219. fill_int_expr(expr, dot.tk_ival, dot.tk_fund);
  220. }
  221. float2expr(expr)
  222. register struct expr *expr;
  223. {
  224. /* Dot contains a floating point constant which is turned
  225. into an expression.
  226. */
  227. register int fund;
  228. fund = dot.tk_fund;
  229. switch (fund) {
  230. case FLOAT:
  231. expr->ex_type = float_type;
  232. break;
  233. case DOUBLE:
  234. expr->ex_type = double_type;
  235. break;
  236. case LNGDBL:
  237. expr->ex_type = lngdbl_type;
  238. break;
  239. default:
  240. crash("(float2expr) bad fund %s\n", symbol2str(fund));
  241. }
  242. expr->ex_class = Float;
  243. flt_str2flt(dot.tk_fval, &(expr->FL_ARITH));
  244. free(dot.tk_fval);
  245. ASSERT(flt_status != FLT_NOFLT);
  246. if (flt_status == FLT_OVFL)
  247. expr_warning(expr,"internal floating point overflow");
  248. }
  249. struct expr*
  250. intexpr(ivalue, fund)
  251. arith ivalue;
  252. int fund;
  253. {
  254. /* The value ivalue is turned into an integer expression of
  255. the size indicated by fund.
  256. */
  257. register struct expr *expr = new_expr();
  258. expr->ex_file = dot.tk_file;
  259. expr->ex_line = dot.tk_line;
  260. fill_int_expr(expr, ivalue, fund);
  261. return expr;
  262. }
  263. fill_int_expr(ex, ivalue, fund)
  264. register struct expr *ex;
  265. arith ivalue;
  266. int fund;
  267. {
  268. /* Details derived from ivalue and fund are put into the
  269. constant integer expression ex.
  270. */
  271. switch (fund) {
  272. case INT:
  273. ex->ex_type = int_type;
  274. break;
  275. case UNSIGNED:
  276. ex->ex_type = uint_type;
  277. break;
  278. case LONG:
  279. ex->ex_type = long_type;
  280. break;
  281. case ULONG:
  282. ex->ex_type = ulong_type;
  283. break;
  284. default:
  285. crash("(fill_int_expr) bad fund %s\n", symbol2str(fund));
  286. /*NOTREACHED*/
  287. }
  288. ex->ex_class = Value;
  289. ex->VL_CLASS = Const;
  290. ex->VL_VALUE = ivalue;
  291. cut_size(ex);
  292. }
  293. struct expr *
  294. new_oper(tp, e1, oper, e2)
  295. struct type *tp;
  296. register struct expr *e1, *e2;
  297. {
  298. /* A new expression is constructed which consists of the
  299. operator oper which has e1 and e2 as operands; for a
  300. monadic operator e1 == NILEXPR.
  301. During the construction of the right recursive initialisation
  302. tree it is possible for e2 to be NILEXPR.
  303. */
  304. register struct expr *expr = new_expr();
  305. register struct oper *op;
  306. if (e2) {
  307. register struct expr *e = e2;
  308. while (e->ex_class == Oper && e->OP_LEFT)
  309. e = e->OP_LEFT;
  310. expr->ex_file = e->ex_file;
  311. expr->ex_line = e->ex_line;
  312. }
  313. else
  314. if (e1) {
  315. register struct expr *e = e1;
  316. while (e->ex_class == Oper && e->OP_RIGHT)
  317. e = e->OP_RIGHT;
  318. expr->ex_file = e->ex_file;
  319. expr->ex_line = e->ex_line;
  320. }
  321. else {
  322. expr->ex_file = dot.tk_file;
  323. expr->ex_line = dot.tk_line;
  324. }
  325. expr->ex_type = tp;
  326. expr->ex_class = Oper;
  327. /* combine depths and flags of both expressions */
  328. if (e2) {
  329. int e1_depth = e1 ? e1->ex_depth : 0;
  330. int e1_flags = e1 ? e1->ex_flags : 0;
  331. expr->ex_depth =
  332. (e1_depth > e2->ex_depth ? e1_depth : e2->ex_depth) + 1;
  333. expr->ex_flags = (e1_flags | e2->ex_flags)
  334. & ~(EX_PARENS | EX_READONLY | EX_VOLATILE );
  335. }
  336. /*
  337. * A function call should be evaluated first when possible. Just say
  338. * that the expression tree is very deep.
  339. */
  340. if (oper == '(') {
  341. expr->ex_depth = 50;
  342. }
  343. op = &expr->ex_object.ex_oper;
  344. op->op_type = tp;
  345. op->op_oper = oper;
  346. op->op_left = e1;
  347. op->op_right = e2;
  348. #ifdef LINT
  349. lint_new_oper(expr);
  350. #endif /* LINT */
  351. return expr;
  352. }
  353. chk_cst_expr(expp)
  354. struct expr **expp;
  355. {
  356. /* The expression expr is checked for constancy.
  357. There are 6 places where constant expressions occur in C:
  358. 1. after #if
  359. 2. in a global initialization
  360. 3. as size in an array declaration
  361. 4. as value in an enum declaration
  362. 5. as width in a bit field
  363. 6. as case value in a switch
  364. The constant expression in a global initialization is
  365. handled separately (by IVAL()).
  366. There are various disparate restrictions on each of
  367. the others in the various C compilers. I have tried some
  368. hypotheses to unify them, but all have failed.
  369. Special problems (of which there is only one, sizeof in
  370. Preprocessor #if) have to be dealt with locally
  371. */
  372. register struct expr *expr = *expp;
  373. #ifdef DEBUG
  374. print_expr("constant_expression", expr);
  375. #endif /* DEBUG */
  376. switch(expr->ex_type->tp_fund) {
  377. case CHAR:
  378. case SHORT:
  379. case INT:
  380. case ENUM:
  381. case LONG:
  382. if (is_ld_cst(expr)) {
  383. return;
  384. }
  385. expr_error(expr, "expression is not constant");
  386. break;
  387. default:
  388. expr_error(expr, "non-numerical constant expression");
  389. break;
  390. }
  391. erroneous2int(expp);
  392. }
  393. init_expression(eppp, expr)
  394. register struct expr ***eppp;
  395. struct expr *expr;
  396. {
  397. /* The expression expr is added to the tree designated
  398. indirectly by **eppp.
  399. The natural form of a tree representing an
  400. initial_value_list is right-recursive, ie. with the
  401. left-most comma as main operator. The iterative grammar in
  402. expression.g, however, tends to produce a left-recursive
  403. tree, ie. one with the right-most comma as its main
  404. operator.
  405. To produce a right-recursive tree from the iterative
  406. grammar, we keep track of the address of the pointer where
  407. the next expression must be hooked in.
  408. */
  409. **eppp = new_oper(void_type, expr, INITCOMMA, NILEXPR);
  410. *eppp = &(**eppp)->OP_RIGHT;
  411. }
  412. int
  413. is_ld_cst(expr)
  414. register struct expr *expr;
  415. {
  416. /* An expression is a `load-time constant' if it is of the form
  417. <idf> +/- <integral> or <integral>.
  418. */
  419. #ifdef LINT
  420. if (expr->ex_class == String)
  421. return 1;
  422. #endif /* LINT */
  423. return expr->ex_lvalue == 0 && expr->ex_class == Value;
  424. }
  425. int
  426. is_cp_cst(expr)
  427. struct expr *expr;
  428. {
  429. /* An expression is a `compile-time constant' if it is a
  430. load-time constant, and the idf is not there.
  431. */
  432. return is_ld_cst(expr) && expr->VL_CLASS == Const;
  433. }
  434. int
  435. is_fp_cst(expr)
  436. struct expr *expr;
  437. {
  438. /* An expression is a `floating-point constant' if it consists
  439. of the float only.
  440. */
  441. return expr->ex_class == Float;
  442. }
  443. int
  444. is_zero_cst(expr)
  445. register struct expr *expr;
  446. {
  447. flt_arith var;
  448. switch(expr->ex_class) {
  449. case Value:
  450. return expr->VL_VALUE == 0;
  451. case Float:
  452. flt_arith2flt((arith) 0, &var, 0);
  453. return flt_cmp(&var, &(expr->FL_ARITH)) == 0;
  454. }
  455. /*NOTREACHED*/
  456. }
  457. free_expression(expr)
  458. register struct expr *expr;
  459. {
  460. /* The expression expr is freed recursively.
  461. */
  462. if (expr) {
  463. if (expr->ex_class == Oper) {
  464. free_expression(expr->OP_LEFT);
  465. free_expression(expr->OP_RIGHT);
  466. }
  467. free_expr(expr);
  468. }
  469. }