expr.c 10 KB

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