expr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. /* 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. if (def->df_type->tp_fund == LABEL) {
  151. expr_error(expr, "illegal use of label %s", idf->id_text);
  152. expr->ex_type = error_type;
  153. }
  154. else {
  155. #ifndef LINT
  156. if (!InSizeof) {
  157. if (! def->df_used) {
  158. #ifndef PREPEND_SCOPES
  159. code_scope(idf->id_text, def);
  160. #endif /* PREPEND_SCOPES */
  161. def->df_used = 1;
  162. }
  163. }
  164. #endif LINT
  165. expr->ex_type = def->df_type;
  166. if (expr->ex_type == error_type) {
  167. expr->ex_flags |= EX_ERROR;
  168. }
  169. }
  170. expr->ex_lvalue =
  171. ( def->df_type->tp_fund == FUNCTION ||
  172. def->df_type->tp_fund == ARRAY ||
  173. def->df_sc == ENUM
  174. ) ? 0 : 1;
  175. if (def->df_type->tp_typequal & TQ_CONST)
  176. expr->ex_flags |= EX_READONLY;
  177. if (def->df_type->tp_typequal & TQ_VOLATILE)
  178. expr->ex_flags |= EX_VOLATILE;
  179. expr->ex_class = Value;
  180. if (def->df_sc == ENUM) {
  181. expr->VL_CLASS = Const;
  182. expr->VL_VALUE = def->df_address;
  183. }
  184. #ifndef LINT
  185. else
  186. if (def->df_sc == STATIC && def->df_level >= L_LOCAL) {
  187. expr->VL_CLASS = Label;
  188. expr->VL_LBL = def->df_address;
  189. expr->VL_VALUE = (arith)0;
  190. }
  191. #endif LINT
  192. else {
  193. expr->VL_CLASS = Name;
  194. expr->VL_IDF = idf;
  195. expr->VL_VALUE = (arith)0;
  196. }
  197. }
  198. string2expr(expp, str, len)
  199. register struct expr **expp;
  200. int len;
  201. char *str;
  202. {
  203. /* The string in the argument is converted into an expression,
  204. a pointer to which is stored in *expp.
  205. */
  206. register struct expr *ex = new_expr();
  207. *expp = ex;
  208. ex->ex_file = dot.tk_file;
  209. ex->ex_line = dot.tk_line;
  210. ex->ex_type = string_type;
  211. /* ex->ex_type = qualifier_type(ex->ex_type, TQ_CONST); */
  212. ex->ex_flags |= EX_READONLY;
  213. /* ex->ex_lvalue = 0; */
  214. ex->ex_class = String;
  215. ex->SG_VALUE = str;
  216. ex->SG_LEN = len;
  217. /* ex->SG_DATLAB = 0; */
  218. }
  219. int2expr(expr)
  220. struct expr *expr;
  221. {
  222. /* Dot contains an integer constant which is turned
  223. into an expression.
  224. */
  225. fill_int_expr(expr, dot.tk_ival, dot.tk_fund);
  226. }
  227. float2expr(expr)
  228. register struct expr *expr;
  229. {
  230. /* Dot contains a floating point constant which is turned
  231. into an expression.
  232. */
  233. register int fund;
  234. fund = dot.tk_fund;
  235. switch (fund) {
  236. case FLOAT:
  237. expr->ex_type = float_type;
  238. break;
  239. case DOUBLE:
  240. expr->ex_type = double_type;
  241. break;
  242. case LNGDBL:
  243. expr->ex_type = lngdbl_type;
  244. break;
  245. default:
  246. crash("(float2expr) bad fund %s\n", symbol2str(fund));
  247. }
  248. expr->ex_class = Float;
  249. expr->FL_VALUE = dot.tk_fval;
  250. flt_str2flt(expr->FL_VALUE, &(expr->FL_ARITH));
  251. ASSERT(flt_status != FLT_NOFLT);
  252. if (flt_status == FLT_OVFL)
  253. expr_warning(expr,"internal floating point overflow");
  254. expr->FL_DATLAB = 0;
  255. }
  256. struct expr*
  257. intexpr(ivalue, fund)
  258. arith ivalue;
  259. int fund;
  260. {
  261. /* The value ivalue is turned into an integer expression of
  262. the size indicated by fund.
  263. */
  264. register struct expr *expr = new_expr();
  265. expr->ex_file = dot.tk_file;
  266. expr->ex_line = dot.tk_line;
  267. fill_int_expr(expr, ivalue, fund);
  268. return expr;
  269. }
  270. fill_int_expr(ex, ivalue, fund)
  271. register struct expr *ex;
  272. arith ivalue;
  273. int fund;
  274. {
  275. /* Details derived from ivalue and fund are put into the
  276. constant integer expression ex.
  277. */
  278. switch (fund) {
  279. case INT:
  280. ex->ex_type = int_type;
  281. break;
  282. case UNSIGNED:
  283. ex->ex_type = uint_type;
  284. break;
  285. case LONG:
  286. ex->ex_type = long_type;
  287. break;
  288. case ULONG:
  289. ex->ex_type = ulong_type;
  290. break;
  291. default:
  292. crash("(fill_int_expr) bad fund %s\n", symbol2str(fund));
  293. /*NOTREACHED*/
  294. }
  295. ex->ex_class = Value;
  296. ex->VL_CLASS = Const;
  297. ex->VL_VALUE = ivalue;
  298. cut_size(ex);
  299. }
  300. struct expr *
  301. new_oper(tp, e1, oper, e2)
  302. struct type *tp;
  303. register struct expr *e1, *e2;
  304. {
  305. /* A new expression is constructed which consists of the
  306. operator oper which has e1 and e2 as operands; for a
  307. monadic operator e1 == NILEXPR.
  308. During the construction of the right recursive initialisation
  309. tree it is possible for e2 to be NILEXPR.
  310. */
  311. register struct expr *expr = new_expr();
  312. register struct oper *op;
  313. if (e2) {
  314. register struct expr *e = e2;
  315. while (e->ex_class == Oper && e->OP_LEFT)
  316. e = e->OP_LEFT;
  317. expr->ex_file = e->ex_file;
  318. expr->ex_line = e->ex_line;
  319. }
  320. else
  321. if (e1) {
  322. register struct expr *e = e1;
  323. while (e->ex_class == Oper && e->OP_RIGHT)
  324. e = e->OP_RIGHT;
  325. expr->ex_file = e->ex_file;
  326. expr->ex_line = e->ex_line;
  327. }
  328. else {
  329. expr->ex_file = dot.tk_file;
  330. expr->ex_line = dot.tk_line;
  331. }
  332. expr->ex_type = tp;
  333. expr->ex_class = Oper;
  334. /* combine depths and flags of both expressions */
  335. if (e2) {
  336. int e1_depth = e1 ? e1->ex_depth : 0;
  337. int e1_flags = e1 ? e1->ex_flags : 0;
  338. expr->ex_depth =
  339. (e1_depth > e2->ex_depth ? e1_depth : e2->ex_depth) + 1;
  340. expr->ex_flags = (e1_flags | e2->ex_flags)
  341. & ~(EX_PARENS | EX_READONLY | EX_VOLATILE );
  342. }
  343. /*
  344. * A function call should be evaluated first when possible. Just say
  345. * that the expression tree is very deep.
  346. */
  347. if (oper == '(') {
  348. expr->ex_depth = 50;
  349. }
  350. op = &expr->ex_object.ex_oper;
  351. op->op_type = tp;
  352. op->op_oper = oper;
  353. op->op_left = e1;
  354. op->op_right = e2;
  355. #ifdef LINT
  356. lint_new_oper(expr);
  357. #endif LINT
  358. return expr;
  359. }
  360. chk_cst_expr(expp)
  361. struct expr **expp;
  362. {
  363. /* The expression expr is checked for constancy.
  364. There are 6 places where constant expressions occur in C:
  365. 1. after #if
  366. 2. in a global initialization
  367. 3. as size in an array declaration
  368. 4. as value in an enum declaration
  369. 5. as width in a bit field
  370. 6. as case value in a switch
  371. The constant expression in a global initialization is
  372. handled separately (by IVAL()).
  373. There are various disparate restrictions on each of
  374. the others in the various C compilers. I have tried some
  375. hypotheses to unify them, but all have failed.
  376. Special problems (of which there is only one, sizeof in
  377. Preprocessor #if) have to be dealt with locally
  378. */
  379. register struct expr *expr = *expp;
  380. #ifdef DEBUG
  381. print_expr("constant_expression", expr);
  382. #endif DEBUG
  383. switch(expr->ex_type->tp_fund) {
  384. case CHAR:
  385. case SHORT:
  386. case INT:
  387. case ENUM:
  388. case LONG:
  389. if (is_ld_cst(expr)) {
  390. return;
  391. }
  392. expr_error(expr, "expression is not constant");
  393. break;
  394. default:
  395. expr_error(expr, "non-numerical constant expression");
  396. break;
  397. }
  398. erroneous2int(expp);
  399. }
  400. init_expression(eppp, expr)
  401. register struct expr ***eppp;
  402. struct expr *expr;
  403. {
  404. /* The expression expr is added to the tree designated
  405. indirectly by **eppp.
  406. The natural form of a tree representing an
  407. initial_value_list is right-recursive, ie. with the
  408. left-most comma as main operator. The iterative grammar in
  409. expression.g, however, tends to produce a left-recursive
  410. tree, ie. one with the right-most comma as its main
  411. operator.
  412. To produce a right-recursive tree from the iterative
  413. grammar, we keep track of the address of the pointer where
  414. the next expression must be hooked in.
  415. */
  416. **eppp = new_oper(void_type, expr, INITCOMMA, NILEXPR);
  417. *eppp = &(**eppp)->OP_RIGHT;
  418. }
  419. int
  420. is_ld_cst(expr)
  421. register struct expr *expr;
  422. {
  423. /* An expression is a `load-time constant' if it is of the form
  424. <idf> +/- <integral> or <integral>.
  425. */
  426. #ifdef LINT
  427. if (expr->ex_class == String)
  428. return 1;
  429. #endif LINT
  430. return expr->ex_lvalue == 0 && expr->ex_class == Value;
  431. }
  432. int
  433. is_cp_cst(expr)
  434. struct expr *expr;
  435. {
  436. /* An expression is a `compile-time constant' if it is a
  437. load-time constant, and the idf is not there.
  438. */
  439. return is_ld_cst(expr) && expr->VL_CLASS == Const;
  440. }
  441. int
  442. is_fp_cst(expr)
  443. struct expr *expr;
  444. {
  445. /* An expression is a `floating-point constant' if it consists
  446. of the float only.
  447. */
  448. return expr->ex_class == Float;
  449. }
  450. int
  451. is_zero_cst(expr)
  452. register struct expr *expr;
  453. {
  454. flt_arith var;
  455. switch(expr->ex_class) {
  456. case Value:
  457. return expr->VL_VALUE == 0;
  458. case Float:
  459. flt_arith2flt((arith) 0, &var, 0);
  460. return flt_cmp(&var, &(expr->FL_ARITH)) == 0;
  461. }
  462. /*NOTREACHED*/
  463. }
  464. free_expression(expr)
  465. register struct expr *expr;
  466. {
  467. /* The expression expr is freed recursively.
  468. */
  469. if (expr) {
  470. if (expr->ex_class == Float && expr->FL_VALUE)
  471. free(expr->FL_VALUE);
  472. if (expr->ex_class == Oper) {
  473. free_expression(expr->OP_LEFT);
  474. free_expression(expr->OP_RIGHT);
  475. }
  476. free_expr(expr);
  477. }
  478. }