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, 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 /* Parse up to a comma (or to a closeparen)
78 in the string EXPP as an expression, evaluate it, and return the value.
79 EXPP is advanced to point to the comma. */
82 parse_to_comma_and_eval (expp
)
85 struct expression
*expr
= parse_c_1 (expp
, 0, 1);
87 register struct cleanup
*old_chain
88 = make_cleanup (free_current_contents
, &expr
);
90 val
= evaluate_expression (expr
);
91 do_cleanups (old_chain
);
95 /* Evaluate an expression in internal prefix form
96 such as is constructed by expread.y.
98 See expression.h for info on the format of an expression. */
100 static value
evaluate_subexp ();
101 static value
evaluate_subexp_for_address ();
102 static value
evaluate_subexp_for_sizeof ();
103 static value
evaluate_subexp_with_coercion ();
105 /* Values of NOSIDE argument to eval_subexp. */
109 EVAL_AVOID_SIDE_EFFECTS
,
113 evaluate_expression (exp
)
114 struct expression
*exp
;
117 return evaluate_subexp (exp
, &pc
, EVAL_NORMAL
);
120 /* Evaluate an expression, avoiding all memory references
121 and getting a value whose type alone is correct. */
125 struct expression
*exp
;
128 return evaluate_subexp (exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
132 evaluate_subexp (exp
, pos
, noside
)
133 register struct expression
*exp
;
139 register int pc
, pc2
, *oldpos
;
140 register value arg1
, arg2
, arg3
;
145 op
= exp
->elts
[pc
].opcode
;
150 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
151 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
152 return value_static_field (exp
->elts
[pc
+ 1].type
,
153 &exp
->elts
[pc
+ 2].string
, -1);
157 return value_from_long (exp
->elts
[pc
+ 1].type
,
158 exp
->elts
[pc
+ 2].longconst
);
162 return value_from_double (exp
->elts
[pc
+ 1].type
,
163 exp
->elts
[pc
+ 2].doubleconst
);
167 if (noside
== EVAL_SKIP
)
169 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
173 return access_value_history (exp
->elts
[pc
+ 1].longconst
);
177 return value_of_register (exp
->elts
[pc
+ 1].longconst
);
181 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
184 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
185 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
186 if (noside
== EVAL_SKIP
)
188 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
191 /* Skip third and second args to evaluate the first one. */
192 arg1
= evaluate_subexp (exp
, pos
, noside
);
193 if (value_zerop (arg1
))
195 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
196 return evaluate_subexp (exp
, pos
, noside
);
200 arg2
= evaluate_subexp (exp
, pos
, noside
);
201 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
207 op
= exp
->elts
[*pos
].opcode
;
208 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
213 nargs
= exp
->elts
[pc
+ 1].longconst
+ 1;
214 /* First, evaluate the structure into arg2 */
217 if (noside
== EVAL_SKIP
)
220 if (op
== STRUCTOP_MEMBER
)
222 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
226 arg2
= evaluate_subexp (exp
, pos
, noside
);
229 /* If the function is a virtual function, then the
230 aggregate value (providing the structure) plays
231 its part by providing the vtable. Otherwise,
232 it is just along for the ride: call the function
235 arg1
= evaluate_subexp (exp
, pos
, noside
);
237 fnptr
= value_as_long (arg1
);
240 struct type
*basetype
;
242 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
243 basetype
= TYPE_VPTR_BASETYPE (basetype
);
244 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
246 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
247 /* If one is virtual, then all are virtual. */
248 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
249 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
250 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnptr
)
253 value base
= value_ind (arg2
);
254 struct type
*fntype
= lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
256 if (TYPE_VPTR_FIELDNO (basetype
) < 0)
257 TYPE_VPTR_FIELDNO (basetype
)
258 = fill_in_vptr_fieldno (basetype
);
260 VALUE_TYPE (base
) = basetype
;
261 vtbl
= value_field (base
, TYPE_VPTR_FIELDNO (basetype
));
262 VALUE_TYPE (vtbl
) = lookup_pointer_type (fntype
);
263 VALUE_TYPE (arg1
) = builtin_type_int
;
264 arg1
= value_subscript (vtbl
, arg1
);
265 VALUE_TYPE (arg1
) = fntype
;
270 error ("virtual function at index %d not found", fnptr
);
274 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
278 /* Now, say which argument to start evaluating from */
281 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
283 /* Hair for method invocations */
286 nargs
= exp
->elts
[pc
+ 1].longconst
+ 1;
287 /* First, evaluate the structure into arg2 */
289 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
290 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
291 if (noside
== EVAL_SKIP
)
294 if (op
== STRUCTOP_STRUCT
)
296 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
300 arg2
= evaluate_subexp (exp
, pos
, noside
);
302 /* Now, say which argument to start evaluating from */
307 nargs
= exp
->elts
[pc
+ 1].longconst
;
310 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
311 for (; tem
<= nargs
; tem
++)
312 /* Ensure that array expressions are coerced into pointer objects. */
313 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
315 /* signal end of arglist */
318 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
322 value_struct_elt (arg2
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
323 op
== STRUCTOP_STRUCT
324 ? "structure" : "structure pointer");
326 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
332 if (noside
== EVAL_SKIP
)
334 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
335 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
336 return call_function (argvec
[0], nargs
, argvec
+ 1);
338 case STRUCTOP_STRUCT
:
339 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
340 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
341 arg1
= evaluate_subexp (exp
, pos
, noside
);
342 if (noside
== EVAL_SKIP
)
344 return value_struct_elt (arg1
, 0, &exp
->elts
[pc
+ 1].string
,
348 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
349 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
350 arg1
= evaluate_subexp (exp
, pos
, noside
);
351 if (noside
== EVAL_SKIP
)
353 return value_struct_elt (arg1
, 0, &exp
->elts
[pc
+ 1].string
,
354 "structure pointer");
356 case STRUCTOP_MEMBER
:
357 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
358 arg2
= evaluate_subexp (exp
, pos
, noside
);
359 if (noside
== EVAL_SKIP
)
361 /* Now, convert these values to an address.
362 @@ We do not know what type we are looking for,
363 @@ so we must assume that the value requested is a
364 @@ member address (as opposed to a member function address). */
365 arg3
= value_from_long (builtin_type_long
,
366 value_as_long (arg1
) + value_as_long (arg2
));
367 VALUE_TYPE (arg3
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)));
368 return value_ind (arg3
);
371 arg1
= evaluate_subexp (exp
, pos
, noside
);
372 arg2
= evaluate_subexp (exp
, pos
, noside
);
373 if (noside
== EVAL_SKIP
)
375 /* Now, convert these values to an address.
376 @@ We do not know what type we are looking for,
377 @@ so we must assume that the value requested is a
378 @@ member address (as opposed to a member function address). */
379 arg3
= value_from_long (builtin_type_long
,
380 value_as_long (arg1
) + value_as_long (arg2
));
381 VALUE_TYPE (arg3
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)));
382 return value_ind (arg3
);
385 arg1
= evaluate_subexp (exp
, pos
, noside
);
386 arg2
= evaluate_subexp (exp
, pos
, noside
);
387 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
389 return value_assign (arg1
, arg2
);
391 case BINOP_ASSIGN_MODIFY
:
393 arg1
= evaluate_subexp (exp
, pos
, noside
);
394 arg2
= evaluate_subexp (exp
, pos
, noside
);
395 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
397 op
= exp
->elts
[pc
+ 1].opcode
;
399 arg2
= value_add (arg1
, arg2
);
400 else if (op
== BINOP_SUB
)
401 arg2
= value_sub (arg1
, arg2
);
403 arg2
= value_binop (arg1
, arg2
, op
);
404 return value_assign (arg1
, arg2
);
407 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
408 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
409 if (noside
== EVAL_SKIP
)
411 return value_add (arg1
, arg2
);
414 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
415 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
416 if (noside
== EVAL_SKIP
)
418 return value_sub (arg1
, arg2
);
428 arg1
= evaluate_subexp (exp
, pos
, noside
);
429 arg2
= evaluate_subexp (exp
, pos
, noside
);
430 if (noside
== EVAL_SKIP
)
432 return value_binop (arg1
, arg2
, op
);
434 case BINOP_SUBSCRIPT
:
435 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
436 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
437 if (noside
== EVAL_SKIP
)
439 return value_subscript (arg1
, arg2
, op
);
442 arg1
= evaluate_subexp (exp
, pos
, noside
);
443 tem
= value_zerop (arg1
);
444 arg2
= evaluate_subexp (exp
, pos
,
445 (tem
? EVAL_SKIP
: noside
));
446 return value_from_long (builtin_type_int
,
447 !tem
&& !value_zerop (arg2
));
450 arg1
= evaluate_subexp (exp
, pos
, noside
);
451 tem
= value_zerop (arg1
);
452 arg2
= evaluate_subexp (exp
, pos
,
453 (!tem
? EVAL_SKIP
: noside
));
454 return value_from_long (builtin_type_int
,
455 !tem
|| !value_zerop (arg2
));
458 arg1
= evaluate_subexp (exp
, pos
, noside
);
459 arg2
= evaluate_subexp (exp
, pos
, noside
);
460 if (noside
== EVAL_SKIP
)
462 tem
= value_equal (arg1
, arg2
);
463 return value_from_long (builtin_type_int
, tem
);
466 arg1
= evaluate_subexp (exp
, pos
, noside
);
467 arg2
= evaluate_subexp (exp
, pos
, noside
);
468 if (noside
== EVAL_SKIP
)
470 tem
= value_equal (arg1
, arg2
);
471 return value_from_long (builtin_type_int
, ! tem
);
474 arg1
= evaluate_subexp (exp
, pos
, noside
);
475 arg2
= evaluate_subexp (exp
, pos
, noside
);
476 if (noside
== EVAL_SKIP
)
478 tem
= value_less (arg1
, arg2
);
479 return value_from_long (builtin_type_int
, tem
);
482 arg1
= evaluate_subexp (exp
, pos
, noside
);
483 arg2
= evaluate_subexp (exp
, pos
, noside
);
484 if (noside
== EVAL_SKIP
)
486 tem
= value_less (arg2
, arg1
);
487 return value_from_long (builtin_type_int
, tem
);
490 arg1
= evaluate_subexp (exp
, pos
, noside
);
491 arg2
= evaluate_subexp (exp
, pos
, noside
);
492 if (noside
== EVAL_SKIP
)
494 tem
= value_less (arg1
, arg2
);
495 return value_from_long (builtin_type_int
, ! tem
);
498 arg1
= evaluate_subexp (exp
, pos
, noside
);
499 arg2
= evaluate_subexp (exp
, pos
, noside
);
500 if (noside
== EVAL_SKIP
)
502 tem
= value_less (arg2
, arg1
);
503 return value_from_long (builtin_type_int
, ! tem
);
506 arg1
= evaluate_subexp (exp
, pos
, noside
);
507 arg2
= evaluate_subexp (exp
, pos
, noside
);
508 if (noside
== EVAL_SKIP
)
510 return value_repeat (arg1
, value_as_long (arg2
));
513 evaluate_subexp (exp
, pos
, noside
);
514 return evaluate_subexp (exp
, pos
, noside
);
517 arg1
= evaluate_subexp (exp
, pos
, noside
);
518 if (noside
== EVAL_SKIP
)
520 return value_neg (arg1
);
523 arg1
= evaluate_subexp (exp
, pos
, noside
);
524 if (noside
== EVAL_SKIP
)
526 return value_lognot (arg1
);
529 arg1
= evaluate_subexp (exp
, pos
, noside
);
530 if (noside
== EVAL_SKIP
)
532 return value_from_long (builtin_type_int
, value_zerop (arg1
));
535 arg1
= evaluate_subexp (exp
, pos
, noside
);
536 if (noside
== EVAL_SKIP
)
538 return value_ind (arg1
);
541 if (noside
== EVAL_SKIP
)
543 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
546 /* C++: check for and handle pointer to members. */
548 op
= exp
->elts
[*pos
].opcode
;
551 char *name
= &exp
->elts
[pc
+3].string
;
552 int tem
= strlen (name
);
553 struct type
*domain
= exp
->elts
[pc
+2].type
;
554 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
555 arg1
= value_struct_elt_for_address (domain
, 0, name
);
558 error ("no field `%s' in structure", name
);
561 return evaluate_subexp_for_address (exp
, pos
, noside
);
564 if (noside
== EVAL_SKIP
)
566 evaluate_subexp (exp
, pos
, EVAL_SKIP
);
569 return evaluate_subexp_for_sizeof (exp
, pos
);
573 arg1
= evaluate_subexp (exp
, pos
, noside
);
574 if (noside
== EVAL_SKIP
)
576 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
580 arg1
= evaluate_subexp (exp
, pos
, noside
);
581 if (noside
== EVAL_SKIP
)
583 return value_at (exp
->elts
[pc
+ 1].type
, value_as_long (arg1
));
585 case UNOP_PREINCREMENT
:
586 arg1
= evaluate_subexp (exp
, pos
, noside
);
587 arg2
= value_add (arg1
, value_from_long (builtin_type_char
, 1));
588 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
590 return value_assign (arg1
, arg2
);
592 case UNOP_PREDECREMENT
:
593 arg1
= evaluate_subexp (exp
, pos
, noside
);
594 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
, 1));
595 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
597 return value_assign (arg1
, arg2
);
599 case UNOP_POSTINCREMENT
:
600 arg1
= evaluate_subexp (exp
, pos
, noside
);
601 arg2
= value_add (arg1
, value_from_long (builtin_type_char
, 1));
602 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
604 value_assign (arg1
, arg2
);
607 case UNOP_POSTDECREMENT
:
608 arg1
= evaluate_subexp (exp
, pos
, noside
);
609 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
, 1));
610 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
612 value_assign (arg1
, arg2
);
617 return value_of_this (1);
620 error ("internal error: I dont know how to evaluation what you gave me");
624 return value_from_long (builtin_type_long
, 1);
627 /* Evaluate a subexpression of EXP, at index *POS,
628 and return the address of that subexpression.
629 Advance *POS over the subexpression.
630 If the subexpression isn't an lvalue, get an error.
631 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
632 then only the type of the result need be correct. */
635 evaluate_subexp_for_address (exp
, pos
, noside
)
636 register struct expression
*exp
;
644 op
= exp
->elts
[pc
].opcode
;
650 return evaluate_subexp (exp
, pos
, noside
);
654 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
655 evaluate_subexp (exp
, pos
, noside
));
659 return locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
662 return value_addr (evaluate_subexp (exp
, pos
, noside
));
666 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
667 When used in contexts where arrays will be coerced anyway,
668 this is equivalent to `evaluate_subexp'
669 but much faster because it avoids actually fetching array contents. */
672 evaluate_subexp_with_coercion (exp
, pos
, noside
)
673 register struct expression
*exp
;
677 register enum exp_opcode op
;
682 op
= exp
->elts
[pc
].opcode
;
687 if (TYPE_CODE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)) == TYPE_CODE_ARRAY
)
690 val
= locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
691 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
))),
696 return evaluate_subexp (exp
, pos
, noside
);
699 /* Evaluate a subexpression of EXP, at index *POS,
700 and return a value for the size of that subexpression.
701 Advance *POS over the subexpression. */
704 evaluate_subexp_for_sizeof (exp
, pos
)
705 register struct expression
*exp
;
713 op
= exp
->elts
[pc
].opcode
;
717 /* This case is handled specially
718 so that we avoid creating a value for the result type.
719 If the result type is very big, it's desirable not to
720 create a value unnecessarily. */
723 val
= evaluate_subexp (exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
724 return value_from_long (builtin_type_int
,
725 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
729 return value_from_long (builtin_type_int
,
730 TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
734 return value_from_long (builtin_type_int
,
735 TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
738 val
= evaluate_subexp (exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
739 return value_from_long (builtin_type_int
,
740 TYPE_LENGTH (VALUE_TYPE (val
)));