eval.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Evaluate expressions for GDB.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include "defs.h"
  18. #include "initialize.h"
  19. #include "symtab.h"
  20. #include "value.h"
  21. #include "expression.h"
  22. START_FILE
  23. /* Parse the string EXP as a C expression, evaluate it,
  24. and return the result as a number. */
  25. CORE_ADDR
  26. parse_and_eval_address (exp)
  27. char *exp;
  28. {
  29. struct expression *expr = parse_c_expression (exp);
  30. register CORE_ADDR addr;
  31. register struct cleanup *old_chain
  32. = make_cleanup (free_current_contents, &expr);
  33. addr = value_as_long (evaluate_expression (expr));
  34. do_cleanups (old_chain);
  35. return addr;
  36. }
  37. /* Like parse_and_eval_address but takes a pointer to a char * variable
  38. and advanced that variable across the characters parsed. */
  39. CORE_ADDR
  40. parse_and_eval_address_1 (expptr)
  41. char **expptr;
  42. {
  43. struct expression *expr = parse_c_1 (expptr, 0);
  44. register CORE_ADDR addr;
  45. register struct cleanup *old_chain
  46. = make_cleanup (free_current_contents, &expr);
  47. addr = value_as_long (evaluate_expression (expr));
  48. do_cleanups (old_chain);
  49. return addr;
  50. }
  51. value
  52. parse_and_eval (exp)
  53. char *exp;
  54. {
  55. struct expression *expr = parse_c_expression (exp);
  56. register value val;
  57. register struct cleanup *old_chain
  58. = make_cleanup (free_current_contents, &expr);
  59. val = evaluate_expression (expr);
  60. do_cleanups (old_chain);
  61. return val;
  62. }
  63. /* Evaluate an expression in internal prefix form
  64. such as is constructed by expread.y.
  65. See expression.h for info on the format of an expression. */
  66. static value evaluate_subexp ();
  67. static value evaluate_subexp_for_address ();
  68. static value evaluate_subexp_for_sizeof ();
  69. static value evaluate_subexp_with_coercion ();
  70. /* Values of NOSIDE argument to eval_subexp. */
  71. enum noside
  72. { EVAL_NORMAL,
  73. EVAL_SKIP,
  74. EVAL_AVOID_SIDE_EFFECTS,
  75. };
  76. value
  77. evaluate_expression (exp)
  78. struct expression *exp;
  79. {
  80. int pc = 0;
  81. return evaluate_subexp (exp, &pc, EVAL_NORMAL);
  82. }
  83. /* Evaluate an expression, avoiding all memory references
  84. and getting a value whose type alone is correct. */
  85. value
  86. evaluate_type (exp)
  87. struct expression *exp;
  88. {
  89. int pc = 0;
  90. return evaluate_subexp (exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
  91. }
  92. static value
  93. evaluate_subexp (exp, pos, noside)
  94. register struct expression *exp;
  95. register int *pos;
  96. enum noside noside;
  97. {
  98. enum exp_opcode op;
  99. int tem;
  100. register int pc;
  101. register value arg1, arg2;
  102. int nargs;
  103. value *argvec;
  104. pc = (*pos)++;
  105. op = exp->elts[pc].opcode;
  106. switch (op)
  107. {
  108. case OP_LONG:
  109. (*pos) += 3;
  110. return value_from_long (exp->elts[pc + 1].type,
  111. exp->elts[pc + 2].longconst);
  112. case OP_DOUBLE:
  113. (*pos) += 3;
  114. return value_from_double (exp->elts[pc + 1].type,
  115. exp->elts[pc + 2].doubleconst);
  116. case OP_VAR_VALUE:
  117. (*pos) += 2;
  118. if (noside == EVAL_SKIP)
  119. goto nosideret;
  120. return value_of_variable (exp->elts[pc + 1].symbol);
  121. case OP_LAST:
  122. (*pos) += 2;
  123. return access_value_history (exp->elts[pc + 1].longconst);
  124. case OP_REGISTER:
  125. (*pos) += 2;
  126. return value_of_register (exp->elts[pc + 1].longconst);
  127. case OP_INTERNALVAR:
  128. (*pos) += 2;
  129. return value_of_internalvar (exp->elts[pc + 1].internalvar);
  130. case OP_FUNCALL:
  131. (*pos) += 2;
  132. nargs = exp->elts[pc + 1].longconst;
  133. argvec = (value *) alloca (sizeof (value) * (nargs + 1));
  134. for (tem = 0; tem <= nargs; tem++)
  135. argvec[tem] = evaluate_subexp (exp, pos, noside);
  136. if (noside == EVAL_SKIP)
  137. goto nosideret;
  138. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  139. return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
  140. return call_function (argvec[0], nargs, argvec + 1);
  141. case OP_STRING:
  142. tem = strlen (&exp->elts[pc + 1].string);
  143. (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
  144. if (noside == EVAL_SKIP)
  145. goto nosideret;
  146. return value_string (&exp->elts[pc + 1].string, tem);
  147. case TERNOP_COND:
  148. /* Skip third and second args to evaluate the first one. */
  149. arg1 = evaluate_subexp (exp, pos, noside);
  150. if (value_zerop (arg1))
  151. {
  152. evaluate_subexp (exp, pos, EVAL_SKIP);
  153. return evaluate_subexp (exp, pos, noside);
  154. }
  155. else
  156. {
  157. arg2 = evaluate_subexp (exp, pos, noside);
  158. evaluate_subexp (exp, pos, EVAL_SKIP);
  159. return arg2;
  160. }
  161. case STRUCTOP_STRUCT:
  162. tem = strlen (&exp->elts[pc + 1].string);
  163. (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
  164. arg1 = evaluate_subexp (exp, pos, noside);
  165. if (noside == EVAL_SKIP)
  166. goto nosideret;
  167. return value_struct_elt (arg1, &exp->elts[pc + 1].string,
  168. "structure");
  169. case STRUCTOP_PTR:
  170. tem = strlen (&exp->elts[pc + 1].string);
  171. (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
  172. arg1 = evaluate_subexp (exp, pos, noside);
  173. if (noside == EVAL_SKIP)
  174. goto nosideret;
  175. return value_struct_elt (arg1, &exp->elts[pc + 1].string,
  176. "structure pointer");
  177. case BINOP_ASSIGN:
  178. arg1 = evaluate_subexp (exp, pos, noside);
  179. arg2 = evaluate_subexp (exp, pos, noside);
  180. if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  181. return arg1;
  182. return value_assign (arg1, arg2);
  183. case BINOP_ASSIGN_MODIFY:
  184. (*pos) += 2;
  185. arg1 = evaluate_subexp (exp, pos, noside);
  186. arg2 = evaluate_subexp (exp, pos, noside);
  187. if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  188. return arg1;
  189. op = exp->elts[pc + 1].opcode;
  190. if (op == BINOP_ADD)
  191. arg2 = value_add (arg1, arg2);
  192. else if (op == BINOP_SUB)
  193. arg2 = value_sub (arg1, arg2);
  194. else
  195. arg2 = value_binop (arg1, arg2, op);
  196. return value_assign (arg1, arg2);
  197. case BINOP_ADD:
  198. arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
  199. arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
  200. if (noside == EVAL_SKIP)
  201. goto nosideret;
  202. return value_add (arg1, arg2);
  203. case BINOP_SUB:
  204. arg1 = evaluate_subexp (exp, pos, noside);
  205. arg2 = evaluate_subexp (exp, pos, noside);
  206. if (noside == EVAL_SKIP)
  207. goto nosideret;
  208. return value_sub (arg1, arg2);
  209. case BINOP_MUL:
  210. case BINOP_DIV:
  211. case BINOP_REM:
  212. case BINOP_LSH:
  213. case BINOP_RSH:
  214. case BINOP_LOGAND:
  215. case BINOP_LOGIOR:
  216. case BINOP_LOGXOR:
  217. arg1 = evaluate_subexp (exp, pos, noside);
  218. arg2 = evaluate_subexp (exp, pos, noside);
  219. if (noside == EVAL_SKIP)
  220. goto nosideret;
  221. return value_binop (arg1, arg2, op);
  222. case BINOP_SUBSCRIPT:
  223. arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
  224. arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
  225. if (noside == EVAL_SKIP)
  226. goto nosideret;
  227. return value_subscript (arg1, arg2, op);
  228. case BINOP_AND:
  229. arg1 = evaluate_subexp (exp, pos, noside);
  230. tem = value_zerop (arg1);
  231. arg2 = evaluate_subexp (exp, pos,
  232. (tem ? EVAL_SKIP : noside));
  233. return value_from_long (builtin_type_int,
  234. !tem && !value_zerop (arg2));
  235. case BINOP_OR:
  236. arg1 = evaluate_subexp (exp, pos, noside);
  237. tem = value_zerop (arg1);
  238. arg2 = evaluate_subexp (exp, pos,
  239. (!tem ? EVAL_SKIP : noside));
  240. return value_from_long (builtin_type_int,
  241. !tem || !value_zerop (arg2));
  242. case BINOP_EQUAL:
  243. arg1 = evaluate_subexp (exp, pos, noside);
  244. arg2 = evaluate_subexp (exp, pos, noside);
  245. if (noside == EVAL_SKIP)
  246. goto nosideret;
  247. tem = value_equal (arg1, arg2);
  248. return value_from_long (builtin_type_int, tem);
  249. case BINOP_NOTEQUAL:
  250. arg1 = evaluate_subexp (exp, pos, noside);
  251. arg2 = evaluate_subexp (exp, pos, noside);
  252. if (noside == EVAL_SKIP)
  253. goto nosideret;
  254. tem = value_equal (arg1, arg2);
  255. return value_from_long (builtin_type_int, ! tem);
  256. case BINOP_LESS:
  257. arg1 = evaluate_subexp (exp, pos, noside);
  258. arg2 = evaluate_subexp (exp, pos, noside);
  259. if (noside == EVAL_SKIP)
  260. goto nosideret;
  261. tem = value_less (arg1, arg2);
  262. return value_from_long (builtin_type_int, tem);
  263. case BINOP_GTR:
  264. arg1 = evaluate_subexp (exp, pos, noside);
  265. arg2 = evaluate_subexp (exp, pos, noside);
  266. if (noside == EVAL_SKIP)
  267. goto nosideret;
  268. tem = value_less (arg2, arg1);
  269. return value_from_long (builtin_type_int, tem);
  270. case BINOP_GEQ:
  271. arg1 = evaluate_subexp (exp, pos, noside);
  272. arg2 = evaluate_subexp (exp, pos, noside);
  273. if (noside == EVAL_SKIP)
  274. goto nosideret;
  275. tem = value_less (arg1, arg2);
  276. return value_from_long (builtin_type_int, ! tem);
  277. case BINOP_LEQ:
  278. arg1 = evaluate_subexp (exp, pos, noside);
  279. arg2 = evaluate_subexp (exp, pos, noside);
  280. if (noside == EVAL_SKIP)
  281. goto nosideret;
  282. tem = value_less (arg2, arg1);
  283. return value_from_long (builtin_type_int, ! tem);
  284. case BINOP_REPEAT:
  285. arg1 = evaluate_subexp (exp, pos, noside);
  286. arg2 = evaluate_subexp (exp, pos, noside);
  287. if (noside == EVAL_SKIP)
  288. goto nosideret;
  289. return value_repeat (arg1, value_as_long (arg2));
  290. case BINOP_COMMA:
  291. evaluate_subexp (exp, pos, noside);
  292. return evaluate_subexp (exp, pos, noside);
  293. case UNOP_NEG:
  294. arg1 = evaluate_subexp (exp, pos, noside);
  295. if (noside == EVAL_SKIP)
  296. goto nosideret;
  297. return value_neg (arg1);
  298. case UNOP_LOGNOT:
  299. arg1 = evaluate_subexp (exp, pos, noside);
  300. if (noside == EVAL_SKIP)
  301. goto nosideret;
  302. return value_lognot (arg1);
  303. case UNOP_ZEROP:
  304. arg1 = evaluate_subexp (exp, pos, noside);
  305. if (noside == EVAL_SKIP)
  306. goto nosideret;
  307. return value_from_long (builtin_type_int, value_zerop (arg1));
  308. case UNOP_IND:
  309. arg1 = evaluate_subexp (exp, pos, noside);
  310. if (noside == EVAL_SKIP)
  311. goto nosideret;
  312. return value_ind (arg1);
  313. case UNOP_ADDR:
  314. if (noside == EVAL_SKIP)
  315. {
  316. evaluate_subexp (exp, pos, EVAL_SKIP);
  317. goto nosideret;
  318. }
  319. return evaluate_subexp_for_address (exp, pos, noside);
  320. case UNOP_SIZEOF:
  321. if (noside == EVAL_SKIP)
  322. {
  323. evaluate_subexp (exp, pos, EVAL_SKIP);
  324. goto nosideret;
  325. }
  326. return evaluate_subexp_for_sizeof (exp, pos);
  327. case UNOP_CAST:
  328. (*pos) += 2;
  329. arg1 = evaluate_subexp (exp, pos, noside);
  330. if (noside == EVAL_SKIP)
  331. goto nosideret;
  332. return value_cast (exp->elts[pc + 1].type, arg1);
  333. case UNOP_MEMVAL:
  334. (*pos) += 2;
  335. arg1 = evaluate_subexp (exp, pos, noside);
  336. if (noside == EVAL_SKIP)
  337. goto nosideret;
  338. return value_at (exp->elts[pc + 1].type, value_as_long (arg1));
  339. case UNOP_PREINCREMENT:
  340. arg1 = evaluate_subexp (exp, pos, noside);
  341. arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
  342. if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  343. return arg1;
  344. return value_assign (arg1, arg2);
  345. case UNOP_PREDECREMENT:
  346. arg1 = evaluate_subexp (exp, pos, noside);
  347. arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
  348. if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  349. return arg1;
  350. return value_assign (arg1, arg2);
  351. case UNOP_POSTINCREMENT:
  352. arg1 = evaluate_subexp (exp, pos, noside);
  353. arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
  354. if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  355. return arg1;
  356. value_assign (arg1, arg2);
  357. return arg1;
  358. case UNOP_POSTDECREMENT:
  359. arg1 = evaluate_subexp (exp, pos, noside);
  360. arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
  361. if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  362. return arg1;
  363. value_assign (arg1, arg2);
  364. return arg1;
  365. }
  366. nosideret:
  367. return value_from_long (builtin_type_long, 1);
  368. }
  369. /* Evaluate a subexpression of EXP, at index *POS,
  370. and return the address of that subexpression.
  371. Advance *POS over the subexpression.
  372. If the subexpression isn't an lvalue, get an error.
  373. NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
  374. then only the type of the result need be correct. */
  375. static value
  376. evaluate_subexp_for_address (exp, pos, noside)
  377. register struct expression *exp;
  378. register int *pos;
  379. enum noside noside;
  380. {
  381. enum exp_opcode op;
  382. register int pc;
  383. pc = (*pos);
  384. op = exp->elts[pc].opcode;
  385. switch (op)
  386. {
  387. case UNOP_IND:
  388. (*pos)++;
  389. return evaluate_subexp (exp, pos, noside);
  390. case UNOP_MEMVAL:
  391. (*pos) += 3;
  392. return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
  393. evaluate_subexp (exp, pos, noside));
  394. case OP_VAR_VALUE:
  395. (*pos) += 3;
  396. return locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
  397. default:
  398. return value_addr (evaluate_subexp (exp, pos, noside));
  399. }
  400. }
  401. /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
  402. When used in contexts where arrays will be coerced anyway,
  403. this is equivalent to `evaluate_subexp'
  404. but much faster because it avoids actually fetching array contents. */
  405. static value
  406. evaluate_subexp_with_coercion (exp, pos, noside)
  407. register struct expression *exp;
  408. register int *pos;
  409. enum noside noside;
  410. {
  411. register enum exp_opcode op;
  412. register int pc;
  413. register value val;
  414. pc = (*pos);
  415. op = exp->elts[pc].opcode;
  416. switch (op)
  417. {
  418. case OP_VAR_VALUE:
  419. if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
  420. {
  421. (*pos) += 3;
  422. val = locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
  423. return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
  424. val);
  425. }
  426. }
  427. return evaluate_subexp (exp, pos, noside);
  428. }
  429. /* Evaluate a subexpression of EXP, at index *POS,
  430. and return a value for the size of that subexpression.
  431. Advance *POS over the subexpression. */
  432. static value
  433. evaluate_subexp_for_sizeof (exp, pos)
  434. register struct expression *exp;
  435. register int *pos;
  436. {
  437. enum exp_opcode op;
  438. register int pc;
  439. value val;
  440. pc = (*pos);
  441. op = exp->elts[pc].opcode;
  442. switch (op)
  443. {
  444. /* This case is handled specially
  445. so that we avoid creating a value for the result type.
  446. If the result type is very big, it's desirable not to
  447. create a value unnecessarily. */
  448. case UNOP_IND:
  449. (*pos)++;
  450. val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  451. return value_from_long (builtin_type_int,
  452. TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
  453. case UNOP_MEMVAL:
  454. (*pos) += 3;
  455. return value_from_long (builtin_type_int,
  456. TYPE_LENGTH (exp->elts[pc + 1].type));
  457. case OP_VAR_VALUE:
  458. (*pos) += 3;
  459. return value_from_long (builtin_type_int,
  460. TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
  461. default:
  462. val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  463. return value_from_long (builtin_type_int,
  464. TYPE_LENGTH (VALUE_TYPE (val)));
  465. }
  466. }
  467. static
  468. initialize ()
  469. { }
  470. END_FILE