1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
22 #include "initialize.h"
25 #include "expression.h"
29 /* Parse the string EXP as a C expression, evaluate it,
30 and return the result as a number. */
33 parse_and_eval_address (exp
)
36 struct expression
*expr
= parse_c_expression (exp
);
37 register CORE_ADDR addr
;
38 register struct cleanup
*old_chain
39 = make_cleanup (free_current_contents
, &expr
);
41 addr
= value_as_long (evaluate_expression (expr
));
42 do_cleanups (old_chain
);
46 /* Like parse_and_eval_address but takes a pointer to a char * variable
47 and advanced that variable across the characters parsed. */
50 parse_and_eval_address_1 (expptr
)
53 struct expression
*expr
= parse_c_1 (expptr
, 0);
54 register CORE_ADDR addr
;
55 register struct cleanup
*old_chain
56 = make_cleanup (free_current_contents
, &expr
);
58 addr
= value_as_long (evaluate_expression (expr
));
59 do_cleanups (old_chain
);
67 struct expression
*expr
= parse_c_expression (exp
);
69 register struct cleanup
*old_chain
70 = make_cleanup (free_current_contents
, &expr
);
72 val
= evaluate_expression (expr
);
73 do_cleanups (old_chain
);
77 /* Evaluate an expression in internal prefix form
78 such as is constructed by expread.y.
80 See expression.h for info on the format of an expression. */
82 static value
evaluate_subexp ();
83 static value
evaluate_subexp_for_address ();
84 static value
evaluate_subexp_for_sizeof ();
85 static value
evaluate_subexp_with_coercion ();
87 /* Values of NOSIDE argument to eval_subexp. */
91 EVAL_AVOID_SIDE_EFFECTS
,
95 evaluate_expression (exp
)
96 struct expression
*exp
;
99 return evaluate_subexp (exp
, &pc
, EVAL_NORMAL
);
102 /* Evaluate an expression, avoiding all memory references
103 and getting a value whose type alone is correct. */
107 struct expression
*exp
;
110 return evaluate_subexp (exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
114 evaluate_subexp (exp
, pos
, noside
)
115 register struct expression
*exp
;
122 register value arg1
, arg2
;
127 op
= exp
->elts
[pc
].opcode
;
133 return value_from_long (exp
->elts
[pc
+ 1].type
,
134 exp
->elts
[pc
+ 2].longconst
);
138 return value_from_double (exp
->elts
[pc
+ 1].type
,
139 exp
->elts
[pc
+ 2].doubleconst
);
143 if (noside
== EVAL_SKIP
)
145 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
149 return access_value_history (exp
->elts
[pc
+ 1].longconst
);
153 return value_of_register (exp
->elts
[pc
+ 1].longconst
);
157 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
161 nargs
= exp
->elts
[pc
+ 1].longconst
;
162 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 1));
163 for (tem
= 0; tem
<= nargs
; tem
++)
165 /* Ensure that array expressions are coerced into pointer objects. */
166 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
168 if (noside
== EVAL_SKIP
)
170 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
171 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
172 return call_function (argvec
[0], nargs
, argvec
+ 1);
175 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
176 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
177 if (noside
== EVAL_SKIP
)
179 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
182 /* Skip third and second args to evaluate the first one. */
183 arg1
= evaluate_subexp (exp
, pos
, noside
);
184 if (value_zerop (arg1
))
186 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
187 return evaluate_subexp (exp
, pos
, noside
);
191 arg2
= evaluate_subexp (exp
, pos
, noside
);
192 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
196 case STRUCTOP_STRUCT
:
197 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
198 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
199 arg1
= evaluate_subexp (exp
, pos
, noside
);
200 if (noside
== EVAL_SKIP
)
202 return value_struct_elt (arg1
, &exp
->elts
[pc
+ 1].string
,
206 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
207 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
208 arg1
= evaluate_subexp (exp
, pos
, noside
);
209 if (noside
== EVAL_SKIP
)
211 return value_struct_elt (arg1
, &exp
->elts
[pc
+ 1].string
,
212 "structure pointer");
215 arg1
= evaluate_subexp (exp
, pos
, noside
);
216 arg2
= evaluate_subexp (exp
, pos
, noside
);
217 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
219 return value_assign (arg1
, arg2
);
221 case BINOP_ASSIGN_MODIFY
:
223 arg1
= evaluate_subexp (exp
, pos
, noside
);
224 arg2
= evaluate_subexp (exp
, pos
, noside
);
225 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
227 op
= exp
->elts
[pc
+ 1].opcode
;
229 arg2
= value_add (arg1
, arg2
);
230 else if (op
== BINOP_SUB
)
231 arg2
= value_sub (arg1
, arg2
);
233 arg2
= value_binop (arg1
, arg2
, op
);
234 return value_assign (arg1
, arg2
);
237 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
238 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
239 if (noside
== EVAL_SKIP
)
241 return value_add (arg1
, arg2
);
244 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
245 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
246 if (noside
== EVAL_SKIP
)
248 return value_sub (arg1
, arg2
);
258 arg1
= evaluate_subexp (exp
, pos
, noside
);
259 arg2
= evaluate_subexp (exp
, pos
, noside
);
260 if (noside
== EVAL_SKIP
)
262 return value_binop (arg1
, arg2
, op
);
264 case BINOP_SUBSCRIPT
:
265 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
266 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
267 if (noside
== EVAL_SKIP
)
269 return value_subscript (arg1
, arg2
, op
);
272 arg1
= evaluate_subexp (exp
, pos
, noside
);
273 tem
= value_zerop (arg1
);
274 arg2
= evaluate_subexp (exp
, pos
,
275 (tem
? EVAL_SKIP
: noside
));
276 return value_from_long (builtin_type_int
,
277 !tem
&& !value_zerop (arg2
));
280 arg1
= evaluate_subexp (exp
, pos
, noside
);
281 tem
= value_zerop (arg1
);
282 arg2
= evaluate_subexp (exp
, pos
,
283 (!tem
? EVAL_SKIP
: noside
));
284 return value_from_long (builtin_type_int
,
285 !tem
|| !value_zerop (arg2
));
288 arg1
= evaluate_subexp (exp
, pos
, noside
);
289 arg2
= evaluate_subexp (exp
, pos
, noside
);
290 if (noside
== EVAL_SKIP
)
292 tem
= value_equal (arg1
, arg2
);
293 return value_from_long (builtin_type_int
, tem
);
296 arg1
= evaluate_subexp (exp
, pos
, noside
);
297 arg2
= evaluate_subexp (exp
, pos
, noside
);
298 if (noside
== EVAL_SKIP
)
300 tem
= value_equal (arg1
, arg2
);
301 return value_from_long (builtin_type_int
, ! tem
);
304 arg1
= evaluate_subexp (exp
, pos
, noside
);
305 arg2
= evaluate_subexp (exp
, pos
, noside
);
306 if (noside
== EVAL_SKIP
)
308 tem
= value_less (arg1
, arg2
);
309 return value_from_long (builtin_type_int
, tem
);
312 arg1
= evaluate_subexp (exp
, pos
, noside
);
313 arg2
= evaluate_subexp (exp
, pos
, noside
);
314 if (noside
== EVAL_SKIP
)
316 tem
= value_less (arg2
, arg1
);
317 return value_from_long (builtin_type_int
, tem
);
320 arg1
= evaluate_subexp (exp
, pos
, noside
);
321 arg2
= evaluate_subexp (exp
, pos
, noside
);
322 if (noside
== EVAL_SKIP
)
324 tem
= value_less (arg1
, arg2
);
325 return value_from_long (builtin_type_int
, ! tem
);
328 arg1
= evaluate_subexp (exp
, pos
, noside
);
329 arg2
= evaluate_subexp (exp
, pos
, noside
);
330 if (noside
== EVAL_SKIP
)
332 tem
= value_less (arg2
, arg1
);
333 return value_from_long (builtin_type_int
, ! tem
);
336 arg1
= evaluate_subexp (exp
, pos
, noside
);
337 arg2
= evaluate_subexp (exp
, pos
, noside
);
338 if (noside
== EVAL_SKIP
)
340 return value_repeat (arg1
, value_as_long (arg2
));
343 evaluate_subexp (exp
, pos
, noside
);
344 return evaluate_subexp (exp
, pos
, noside
);
347 arg1
= evaluate_subexp (exp
, pos
, noside
);
348 if (noside
== EVAL_SKIP
)
350 return value_neg (arg1
);
353 arg1
= evaluate_subexp (exp
, pos
, noside
);
354 if (noside
== EVAL_SKIP
)
356 return value_lognot (arg1
);
359 arg1
= evaluate_subexp (exp
, pos
, noside
);
360 if (noside
== EVAL_SKIP
)
362 return value_from_long (builtin_type_int
, value_zerop (arg1
));
365 arg1
= evaluate_subexp (exp
, pos
, noside
);
366 if (noside
== EVAL_SKIP
)
368 return value_ind (arg1
);
371 if (noside
== EVAL_SKIP
)
373 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
376 return evaluate_subexp_for_address (exp
, pos
, noside
);
379 if (noside
== EVAL_SKIP
)
381 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
384 return evaluate_subexp_for_sizeof (exp
, pos
);
388 arg1
= evaluate_subexp (exp
, pos
, noside
);
389 if (noside
== EVAL_SKIP
)
391 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
395 arg1
= evaluate_subexp (exp
, pos
, noside
);
396 if (noside
== EVAL_SKIP
)
398 return value_at (exp
->elts
[pc
+ 1].type
, value_as_long (arg1
));
400 case UNOP_PREINCREMENT
:
401 arg1
= evaluate_subexp (exp
, pos
, noside
);
402 arg2
= value_add (arg1
, value_from_long (builtin_type_char
, 1));
403 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
405 return value_assign (arg1
, arg2
);
407 case UNOP_PREDECREMENT
:
408 arg1
= evaluate_subexp (exp
, pos
, noside
);
409 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
, 1));
410 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
412 return value_assign (arg1
, arg2
);
414 case UNOP_POSTINCREMENT
:
415 arg1
= evaluate_subexp (exp
, pos
, noside
);
416 arg2
= value_add (arg1
, value_from_long (builtin_type_char
, 1));
417 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
419 value_assign (arg1
, arg2
);
422 case UNOP_POSTDECREMENT
:
423 arg1
= evaluate_subexp (exp
, pos
, noside
);
424 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
, 1));
425 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
427 value_assign (arg1
, arg2
);
432 return value_from_long (builtin_type_long
, 1);
435 /* Evaluate a subexpression of EXP, at index *POS,
436 and return the address of that subexpression.
437 Advance *POS over the subexpression.
438 If the subexpression isn't an lvalue, get an error.
439 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
440 then only the type of the result need be correct. */
443 evaluate_subexp_for_address (exp
, pos
, noside
)
444 register struct expression
*exp
;
452 op
= exp
->elts
[pc
].opcode
;
458 return evaluate_subexp (exp
, pos
, noside
);
462 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
463 evaluate_subexp (exp
, pos
, noside
));
467 return locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
470 return value_addr (evaluate_subexp (exp
, pos
, noside
));
474 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
475 When used in contexts where arrays will be coerced anyway,
476 this is equivalent to `evaluate_subexp'
477 but much faster because it avoids actually fetching array contents. */
480 evaluate_subexp_with_coercion (exp
, pos
, noside
)
481 register struct expression
*exp
;
485 register enum exp_opcode op
;
490 op
= exp
->elts
[pc
].opcode
;
495 if (TYPE_CODE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)) == TYPE_CODE_ARRAY
)
498 val
= locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
499 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
))),
504 return evaluate_subexp (exp
, pos
, noside
);
507 /* Evaluate a subexpression of EXP, at index *POS,
508 and return a value for the size of that subexpression.
509 Advance *POS over the subexpression. */
512 evaluate_subexp_for_sizeof (exp
, pos
)
513 register struct expression
*exp
;
521 op
= exp
->elts
[pc
].opcode
;
525 /* This case is handled specially
526 so that we avoid creating a value for the result type.
527 If the result type is very big, it's desirable not to
528 create a value unnecessarily. */
531 val
= evaluate_subexp (exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
532 return value_from_long (builtin_type_int
,
533 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
537 return value_from_long (builtin_type_int
,
538 TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
542 return value_from_long (builtin_type_int
,
543 TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
546 val
= evaluate_subexp (exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
547 return value_from_long (builtin_type_int
,
548 TYPE_LENGTH (VALUE_TYPE (val
)));