* command.h, defs.h, eval.h, expression.h, remote-sa.sparc.c,
[binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "symtab.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "frame.h"
27
28 #define NULL_TYPE ((struct type *)0)
29
30 \f
31 /* Parse the string EXP as a C expression, evaluate it,
32 and return the result as a number. */
33
34 CORE_ADDR
35 parse_and_eval_address (exp)
36 char *exp;
37 {
38 struct expression *expr = parse_expression (exp);
39 register CORE_ADDR addr;
40 register struct cleanup *old_chain
41 = make_cleanup (free_current_contents, &expr);
42
43 addr = value_as_pointer (evaluate_expression (expr));
44 do_cleanups (old_chain);
45 return addr;
46 }
47
48 /* Like parse_and_eval_address but takes a pointer to a char * variable
49 and advanced that variable across the characters parsed. */
50
51 CORE_ADDR
52 parse_and_eval_address_1 (expptr)
53 char **expptr;
54 {
55 struct expression *expr = parse_exp_1 (expptr, (struct block *)0, 0);
56 register CORE_ADDR addr;
57 register struct cleanup *old_chain
58 = make_cleanup (free_current_contents, &expr);
59
60 addr = value_as_pointer (evaluate_expression (expr));
61 do_cleanups (old_chain);
62 return addr;
63 }
64
65 value
66 parse_and_eval (exp)
67 char *exp;
68 {
69 struct expression *expr = parse_expression (exp);
70 register value val;
71 register struct cleanup *old_chain
72 = make_cleanup (free_current_contents, &expr);
73
74 val = evaluate_expression (expr);
75 do_cleanups (old_chain);
76 return val;
77 }
78
79 /* Parse up to a comma (or to a closeparen)
80 in the string EXPP as an expression, evaluate it, and return the value.
81 EXPP is advanced to point to the comma. */
82
83 value
84 parse_to_comma_and_eval (expp)
85 char **expp;
86 {
87 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
88 register value val;
89 register struct cleanup *old_chain
90 = make_cleanup (free_current_contents, &expr);
91
92 val = evaluate_expression (expr);
93 do_cleanups (old_chain);
94 return val;
95 }
96 \f
97 /* Evaluate an expression in internal prefix form
98 such as is constructed by parse.y.
99
100 See expression.h for info on the format of an expression. */
101
102 static value evaluate_subexp ();
103 static value evaluate_subexp_for_address ();
104 static value evaluate_subexp_for_sizeof ();
105 static value evaluate_subexp_with_coercion ();
106
107 /* Values of NOSIDE argument to eval_subexp. */
108 enum noside
109 { EVAL_NORMAL,
110 EVAL_SKIP, /* Only effect is to increment pos. */
111 EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
112 call any functions. The value
113 returned will have the correct
114 type, and will have an
115 approximately correct lvalue
116 type (inaccuracy: anything that is
117 listed as being in a register in
118 the function in which it was
119 declared will be lval_register). */
120 };
121
122 value
123 evaluate_expression (exp)
124 struct expression *exp;
125 {
126 int pc = 0;
127 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
128 }
129
130 /* Evaluate an expression, avoiding all memory references
131 and getting a value whose type alone is correct. */
132
133 value
134 evaluate_type (exp)
135 struct expression *exp;
136 {
137 int pc = 0;
138 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
139 }
140
141 static value
142 evaluate_subexp (expect_type, exp, pos, noside)
143 struct type *expect_type;
144 register struct expression *exp;
145 register int *pos;
146 enum noside noside;
147 {
148 enum exp_opcode op;
149 int tem;
150 register int pc, pc2, oldpos;
151 register value arg1, arg2, arg3;
152 int nargs;
153 value *argvec;
154
155 pc = (*pos)++;
156 op = exp->elts[pc].opcode;
157
158 switch (op)
159 {
160 case OP_SCOPE:
161 tem = strlen (&exp->elts[pc + 2].string);
162 (*pos) += 3 + ((tem + sizeof (union exp_element))
163 / sizeof (union exp_element));
164 arg1 = value_static_field (exp->elts[pc + 1].type,
165 &exp->elts[pc + 2].string, -1);
166 if (arg1 == NULL)
167 error ("There is no field named %s", &exp->elts[pc + 2].string);
168 return arg1;
169
170 case OP_LONG:
171 (*pos) += 3;
172 return value_from_longest (exp->elts[pc + 1].type,
173 exp->elts[pc + 2].longconst);
174
175 case OP_DOUBLE:
176 (*pos) += 3;
177 return value_from_double (exp->elts[pc + 1].type,
178 exp->elts[pc + 2].doubleconst);
179
180 case OP_VAR_VALUE:
181 (*pos) += 2;
182 if (noside == EVAL_SKIP)
183 goto nosideret;
184 if (noside == EVAL_AVOID_SIDE_EFFECTS)
185 {
186 struct symbol * sym = exp->elts[pc + 1].symbol;
187 enum lval_type lv;
188
189 switch (SYMBOL_CLASS (sym))
190 {
191 case LOC_CONST:
192 case LOC_LABEL:
193 case LOC_CONST_BYTES:
194 lv = not_lval;
195 break;
196
197 case LOC_REGISTER:
198 case LOC_REGPARM:
199 lv = lval_register;
200 break;
201
202 default:
203 lv = lval_memory;
204 break;
205 }
206
207 return value_zero (SYMBOL_TYPE (sym), lv);
208 }
209 else
210 return value_of_variable (exp->elts[pc + 1].symbol);
211
212 case OP_LAST:
213 (*pos) += 2;
214 return
215 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
216
217 case OP_REGISTER:
218 (*pos) += 2;
219 return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
220
221 case OP_INTERNALVAR:
222 (*pos) += 2;
223 return value_of_internalvar (exp->elts[pc + 1].internalvar);
224
225 case OP_STRING:
226 tem = strlen (&exp->elts[pc + 1].string);
227 (*pos) += 2 + ((tem + sizeof (union exp_element))
228 / sizeof (union exp_element));
229 if (noside == EVAL_SKIP)
230 goto nosideret;
231 return value_string (&exp->elts[pc + 1].string, tem);
232
233 case TERNOP_COND:
234 /* Skip third and second args to evaluate the first one. */
235 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
236 if (value_zerop (arg1))
237 {
238 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
239 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
240 }
241 else
242 {
243 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
244 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
245 return arg2;
246 }
247
248 case OP_FUNCALL:
249 (*pos) += 2;
250 op = exp->elts[*pos].opcode;
251 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
252 {
253 int fnptr;
254
255 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
256 /* First, evaluate the structure into arg2 */
257 pc2 = (*pos)++;
258
259 if (noside == EVAL_SKIP)
260 goto nosideret;
261
262 if (op == STRUCTOP_MEMBER)
263 {
264 arg2 = evaluate_subexp_for_address (exp, pos, noside);
265 }
266 else
267 {
268 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
269 }
270
271 /* If the function is a virtual function, then the
272 aggregate value (providing the structure) plays
273 its part by providing the vtable. Otherwise,
274 it is just along for the ride: call the function
275 directly. */
276
277 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
278
279 fnptr = longest_to_int (value_as_long (arg1));
280 /* FIXME-tiemann: this is way obsolete. */
281 if (fnptr < 128)
282 {
283 struct type *basetype;
284 int i, j;
285 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
286 basetype = TYPE_VPTR_BASETYPE (basetype);
287 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
288 {
289 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
290 /* If one is virtual, then all are virtual. */
291 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
292 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
293 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnptr)
294 {
295 value vtbl;
296 value base = value_ind (arg2);
297 struct type *fntype = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
298
299 if (TYPE_VPTR_FIELDNO (basetype) < 0)
300 fill_in_vptr_fieldno (basetype);
301
302 VALUE_TYPE (base) = basetype;
303 vtbl = value_field (base, TYPE_VPTR_FIELDNO (basetype));
304 VALUE_TYPE (vtbl) = lookup_pointer_type (fntype);
305 VALUE_TYPE (arg1) = builtin_type_int;
306 arg1 = value_subscript (vtbl, arg1);
307 VALUE_TYPE (arg1) = fntype;
308 goto got_it;
309 }
310 }
311 if (i < 0)
312 error ("virtual function at index %d not found", fnptr);
313 }
314 else
315 {
316 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
317 }
318 got_it:
319
320 /* Now, say which argument to start evaluating from */
321 tem = 2;
322 }
323 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
324 {
325 /* Hair for method invocations */
326 int tem2;
327
328 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
329 /* First, evaluate the structure into arg2 */
330 pc2 = (*pos)++;
331 tem2 = strlen (&exp->elts[pc2 + 1].string);
332 *pos += 2 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
333 if (noside == EVAL_SKIP)
334 goto nosideret;
335
336 if (op == STRUCTOP_STRUCT)
337 {
338 arg2 = evaluate_subexp_for_address (exp, pos, noside);
339 }
340 else
341 {
342 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
343 }
344 /* Now, say which argument to start evaluating from */
345 tem = 2;
346 }
347 else
348 {
349 nargs = longest_to_int (exp->elts[pc + 1].longconst);
350 tem = 0;
351 }
352 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
353 for (; tem <= nargs; tem++)
354 /* Ensure that array expressions are coerced into pointer objects. */
355 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
356
357 /* signal end of arglist */
358 argvec[tem] = 0;
359
360 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
361 {
362 int static_memfuncp;
363 value temp = arg2;
364
365 argvec[1] = arg2;
366 argvec[0] =
367 value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 1].string,
368 &static_memfuncp,
369 op == STRUCTOP_STRUCT
370 ? "structure" : "structure pointer");
371 if (VALUE_OFFSET (temp))
372 {
373 arg2 = value_from_longest (lookup_pointer_type (VALUE_TYPE (temp)),
374 value_as_long (arg2)+VALUE_OFFSET (temp));
375 argvec[1] = arg2;
376 }
377 if (static_memfuncp)
378 {
379 argvec[1] = argvec[0];
380 nargs--;
381 argvec++;
382 }
383 }
384 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
385 {
386 argvec[1] = arg2;
387 argvec[0] = arg1;
388 }
389
390 if (noside == EVAL_SKIP)
391 goto nosideret;
392 if (noside == EVAL_AVOID_SIDE_EFFECTS)
393 {
394 /* If the return type doesn't look like a function type, call an
395 error. This can happen if somebody tries to turn a variable into
396 a function call. This is here because people often want to
397 call, eg, strcmp, which gdb doesn't know is a function. If
398 gdb isn't asked for it's opinion (ie. through "whatis"),
399 it won't offer it. */
400
401 struct type *ftype =
402 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
403
404 if (ftype)
405 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
406 else
407 error ("Expression of type other than \"Function returning ...\" used as function");
408 }
409 return target_call_function (argvec[0], nargs, argvec + 1);
410
411 case STRUCTOP_STRUCT:
412 tem = strlen (&exp->elts[pc + 1].string);
413 (*pos) += 2 + ((tem + sizeof (union exp_element))
414 / sizeof (union exp_element));
415 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
416 if (noside == EVAL_SKIP)
417 goto nosideret;
418 if (noside == EVAL_AVOID_SIDE_EFFECTS)
419 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
420 &exp->elts[pc + 1].string,
421 1),
422 lval_memory);
423 else
424 {
425 value temp = arg1;
426 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 1].string,
427 (int *) 0, "structure");
428 }
429
430 case STRUCTOP_PTR:
431 tem = strlen (&exp->elts[pc + 1].string);
432 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
433 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
434 if (noside == EVAL_SKIP)
435 goto nosideret;
436 if (noside == EVAL_AVOID_SIDE_EFFECTS)
437 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
438 (VALUE_TYPE (arg1)),
439 &exp->elts[pc + 1].string,
440 1),
441 lval_memory);
442 else
443 {
444 value temp = arg1;
445 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 1].string,
446 (int *) 0, "structure pointer");
447 }
448
449 case STRUCTOP_MEMBER:
450 arg1 = evaluate_subexp_for_address (exp, pos, noside);
451 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
452 if (noside == EVAL_SKIP)
453 goto nosideret;
454 /* Now, convert these values to an address. */
455 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
456 || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))
457 != TYPE_CODE_MEMBER)
458 && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))
459 != TYPE_CODE_METHOD)))
460 error ("non-pointer-to-member value used in pointer-to-member construct");
461 arg3 = value_from_longest (
462 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))),
463 value_as_long (arg1) + value_as_long (arg2));
464 return value_ind (arg3);
465
466 case STRUCTOP_MPTR:
467 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
468 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
469 if (noside == EVAL_SKIP)
470 goto nosideret;
471 /* Now, convert these values to an address. */
472 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
473 || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_MEMBER
474 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_METHOD))
475 error ("non-pointer-to-member value used in pointer-to-member construct");
476 arg3 = value_from_longest (
477 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))),
478 value_as_long (arg1) + value_as_long (arg2));
479 return value_ind (arg3);
480
481 case BINOP_ASSIGN:
482 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
483 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
484 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
485 return arg1;
486 if (binop_user_defined_p (op, arg1, arg2))
487 return value_x_binop (arg1, arg2, op, OP_NULL);
488 else
489 return value_assign (arg1, arg2);
490
491 case BINOP_ASSIGN_MODIFY:
492 (*pos) += 2;
493 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
494 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
495 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
496 return arg1;
497 op = exp->elts[pc + 1].opcode;
498 if (binop_user_defined_p (op, arg1, arg2))
499 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
500 else if (op == BINOP_ADD)
501 arg2 = value_add (arg1, arg2);
502 else if (op == BINOP_SUB)
503 arg2 = value_sub (arg1, arg2);
504 else
505 arg2 = value_binop (arg1, arg2, op);
506 return value_assign (arg1, arg2);
507
508 case BINOP_ADD:
509 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
510 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
511 if (noside == EVAL_SKIP)
512 goto nosideret;
513 if (binop_user_defined_p (op, arg1, arg2))
514 return value_x_binop (arg1, arg2, op, OP_NULL);
515 else
516 return value_add (arg1, arg2);
517
518 case BINOP_SUB:
519 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
520 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
521 if (noside == EVAL_SKIP)
522 goto nosideret;
523 if (binop_user_defined_p (op, arg1, arg2))
524 return value_x_binop (arg1, arg2, op, OP_NULL);
525 else
526 return value_sub (arg1, arg2);
527
528 case BINOP_MUL:
529 case BINOP_DIV:
530 case BINOP_REM:
531 case BINOP_LSH:
532 case BINOP_RSH:
533 case BINOP_LOGAND:
534 case BINOP_LOGIOR:
535 case BINOP_LOGXOR:
536 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
537 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
538 if (noside == EVAL_SKIP)
539 goto nosideret;
540 if (binop_user_defined_p (op, arg1, arg2))
541 return value_x_binop (arg1, arg2, op, OP_NULL);
542 else
543 if (noside == EVAL_AVOID_SIDE_EFFECTS
544 && op == BINOP_DIV)
545 return value_zero (VALUE_TYPE (arg1), not_lval);
546 else
547 return value_binop (arg1, arg2, op);
548
549 case BINOP_SUBSCRIPT:
550 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
551 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
552 if (noside == EVAL_SKIP)
553 goto nosideret;
554 if (noside == EVAL_AVOID_SIDE_EFFECTS)
555 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
556 VALUE_LVAL (arg1));
557
558 if (binop_user_defined_p (op, arg1, arg2))
559 return value_x_binop (arg1, arg2, op, OP_NULL);
560 else
561 return value_subscript (arg1, arg2);
562
563 case BINOP_AND:
564 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
565 if (noside == EVAL_SKIP)
566 {
567 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
568 goto nosideret;
569 }
570
571 oldpos = *pos;
572 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
573 *pos = oldpos;
574
575 if (binop_user_defined_p (op, arg1, arg2))
576 {
577 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
578 return value_x_binop (arg1, arg2, op, OP_NULL);
579 }
580 else
581 {
582 tem = value_zerop (arg1);
583 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
584 (tem ? EVAL_SKIP : noside));
585 return value_from_longest (builtin_type_int,
586 (LONGEST) (!tem && !value_zerop (arg2)));
587 }
588
589 case BINOP_OR:
590 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
591 if (noside == EVAL_SKIP)
592 {
593 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
594 goto nosideret;
595 }
596
597 oldpos = *pos;
598 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
599 *pos = oldpos;
600
601 if (binop_user_defined_p (op, arg1, arg2))
602 {
603 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
604 return value_x_binop (arg1, arg2, op, OP_NULL);
605 }
606 else
607 {
608 tem = value_zerop (arg1);
609 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
610 (!tem ? EVAL_SKIP : noside));
611 return value_from_longest (builtin_type_int,
612 (LONGEST) (!tem || !value_zerop (arg2)));
613 }
614
615 case BINOP_EQUAL:
616 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
617 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
618 if (noside == EVAL_SKIP)
619 goto nosideret;
620 if (binop_user_defined_p (op, arg1, arg2))
621 {
622 return value_x_binop (arg1, arg2, op, OP_NULL);
623 }
624 else
625 {
626 tem = value_equal (arg1, arg2);
627 return value_from_longest (builtin_type_int, (LONGEST) tem);
628 }
629
630 case BINOP_NOTEQUAL:
631 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
632 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
633 if (noside == EVAL_SKIP)
634 goto nosideret;
635 if (binop_user_defined_p (op, arg1, arg2))
636 {
637 return value_x_binop (arg1, arg2, op, OP_NULL);
638 }
639 else
640 {
641 tem = value_equal (arg1, arg2);
642 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
643 }
644
645 case BINOP_LESS:
646 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
647 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
648 if (noside == EVAL_SKIP)
649 goto nosideret;
650 if (binop_user_defined_p (op, arg1, arg2))
651 {
652 return value_x_binop (arg1, arg2, op, OP_NULL);
653 }
654 else
655 {
656 tem = value_less (arg1, arg2);
657 return value_from_longest (builtin_type_int, (LONGEST) tem);
658 }
659
660 case BINOP_GTR:
661 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
662 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
663 if (noside == EVAL_SKIP)
664 goto nosideret;
665 if (binop_user_defined_p (op, arg1, arg2))
666 {
667 return value_x_binop (arg1, arg2, op, OP_NULL);
668 }
669 else
670 {
671 tem = value_less (arg2, arg1);
672 return value_from_longest (builtin_type_int, (LONGEST) tem);
673 }
674
675 case BINOP_GEQ:
676 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
677 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
678 if (noside == EVAL_SKIP)
679 goto nosideret;
680 if (binop_user_defined_p (op, arg1, arg2))
681 {
682 return value_x_binop (arg1, arg2, op, OP_NULL);
683 }
684 else
685 {
686 tem = value_less (arg1, arg2);
687 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
688 }
689
690 case BINOP_LEQ:
691 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
692 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
693 if (noside == EVAL_SKIP)
694 goto nosideret;
695 if (binop_user_defined_p (op, arg1, arg2))
696 {
697 return value_x_binop (arg1, arg2, op, OP_NULL);
698 }
699 else
700 {
701 tem = value_less (arg2, arg1);
702 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
703 }
704
705 case BINOP_REPEAT:
706 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
707 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
708 if (noside == EVAL_SKIP)
709 goto nosideret;
710 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
711 error ("Non-integral right operand for \"@\" operator.");
712 if (noside == EVAL_AVOID_SIDE_EFFECTS)
713 return allocate_repeat_value (VALUE_TYPE (arg1),
714 longest_to_int (value_as_long (arg2)));
715 else
716 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
717
718 case BINOP_COMMA:
719 evaluate_subexp (NULL_TYPE, exp, pos, noside);
720 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
721
722 case UNOP_NEG:
723 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
724 if (noside == EVAL_SKIP)
725 goto nosideret;
726 if (unop_user_defined_p (op, arg1))
727 return value_x_unop (arg1, op);
728 else
729 return value_neg (arg1);
730
731 case UNOP_LOGNOT:
732 /* C++: check for and handle destructor names. */
733 op = exp->elts[*pos].opcode;
734
735 /* FIXME-tiemann: this is a cop-out. */
736 if (op == OP_SCOPE)
737 error ("destructor in eval");
738
739 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
740 if (noside == EVAL_SKIP)
741 goto nosideret;
742 if (unop_user_defined_p (UNOP_LOGNOT, arg1))
743 return value_x_unop (arg1, UNOP_LOGNOT);
744 else
745 return value_lognot (arg1);
746
747 case UNOP_ZEROP:
748 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
749 if (noside == EVAL_SKIP)
750 goto nosideret;
751 if (unop_user_defined_p (op, arg1))
752 return value_x_unop (arg1, op);
753 else
754 return value_from_longest (builtin_type_int,
755 (LONGEST) value_zerop (arg1));
756
757 case UNOP_IND:
758 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
759 expect_type = TYPE_TARGET_TYPE (expect_type);
760 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
761 if (noside == EVAL_SKIP)
762 goto nosideret;
763 if (noside == EVAL_AVOID_SIDE_EFFECTS)
764 {
765 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
766 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
767 /* In C you can dereference an array to get the 1st elt. */
768 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
769 )
770 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
771 lval_memory);
772 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
773 /* GDB allows dereferencing an int. */
774 return value_zero (builtin_type_int, lval_memory);
775 else
776 error ("Attempt to take contents of a non-pointer value.");
777 }
778 return value_ind (arg1);
779
780 case UNOP_ADDR:
781 /* C++: check for and handle pointer to members. */
782
783 op = exp->elts[*pos].opcode;
784
785 if (noside == EVAL_SKIP)
786 {
787 if (op == OP_SCOPE)
788 {
789 char *name = &exp->elts[pc+3].string;
790 int temm = strlen (name);
791 (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
792 }
793 else
794 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
795 goto nosideret;
796 }
797
798 if (op == OP_SCOPE)
799 {
800 char *name = &exp->elts[pc+3].string;
801 int temm = strlen (name);
802 struct type *domain = exp->elts[pc+2].type;
803 (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
804 arg1 = value_struct_elt_for_address (domain, expect_type, name);
805 if (arg1)
806 return arg1;
807 error ("no field `%s' in structure", name);
808 }
809 else
810 return evaluate_subexp_for_address (exp, pos, noside);
811
812 case UNOP_SIZEOF:
813 if (noside == EVAL_SKIP)
814 {
815 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
816 goto nosideret;
817 }
818 return evaluate_subexp_for_sizeof (exp, pos);
819
820 case UNOP_CAST:
821 (*pos) += 2;
822 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
823 if (noside == EVAL_SKIP)
824 goto nosideret;
825 return value_cast (exp->elts[pc + 1].type, arg1);
826
827 case UNOP_MEMVAL:
828 (*pos) += 2;
829 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
830 if (noside == EVAL_SKIP)
831 goto nosideret;
832 if (noside == EVAL_AVOID_SIDE_EFFECTS)
833 return value_zero (exp->elts[pc + 1].type, lval_memory);
834 else
835 return value_at_lazy (exp->elts[pc + 1].type,
836 value_as_pointer (arg1));
837
838 case UNOP_PREINCREMENT:
839 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
840 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
841 return arg1;
842 else if (unop_user_defined_p (op, arg1))
843 {
844 return value_x_unop (arg1, op);
845 }
846 else
847 {
848 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
849 (LONGEST) 1));
850 return value_assign (arg1, arg2);
851 }
852
853 case UNOP_PREDECREMENT:
854 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
855 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
856 return arg1;
857 else if (unop_user_defined_p (op, arg1))
858 {
859 return value_x_unop (arg1, op);
860 }
861 else
862 {
863 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
864 (LONGEST) 1));
865 return value_assign (arg1, arg2);
866 }
867
868 case UNOP_POSTINCREMENT:
869 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
870 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
871 return arg1;
872 else if (unop_user_defined_p (op, arg1))
873 {
874 return value_x_unop (arg1, op);
875 }
876 else
877 {
878 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
879 (LONGEST) 1));
880 value_assign (arg1, arg2);
881 return arg1;
882 }
883
884 case UNOP_POSTDECREMENT:
885 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
886 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
887 return arg1;
888 else if (unop_user_defined_p (op, arg1))
889 {
890 return value_x_unop (arg1, op);
891 }
892 else
893 {
894 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
895 (LONGEST) 1));
896 value_assign (arg1, arg2);
897 return arg1;
898 }
899
900 case OP_THIS:
901 (*pos) += 1;
902 return value_of_this (1);
903
904 default:
905 error ("internal error: I do not know how to evaluate what you gave me");
906 }
907
908 nosideret:
909 return value_from_longest (builtin_type_long, (LONGEST) 1);
910 }
911 \f
912 /* Evaluate a subexpression of EXP, at index *POS,
913 and return the address of that subexpression.
914 Advance *POS over the subexpression.
915 If the subexpression isn't an lvalue, get an error.
916 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
917 then only the type of the result need be correct. */
918
919 static value
920 evaluate_subexp_for_address (exp, pos, noside)
921 register struct expression *exp;
922 register int *pos;
923 enum noside noside;
924 {
925 enum exp_opcode op;
926 register int pc;
927
928 pc = (*pos);
929 op = exp->elts[pc].opcode;
930
931 switch (op)
932 {
933 case UNOP_IND:
934 (*pos)++;
935 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
936
937 case UNOP_MEMVAL:
938 (*pos) += 3;
939 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
940 evaluate_subexp (NULL_TYPE, exp, pos, noside));
941
942 case OP_VAR_VALUE:
943 (*pos) += 3;
944 if (noside == EVAL_AVOID_SIDE_EFFECTS)
945 {
946 struct type *type =
947 lookup_pointer_type (SYMBOL_TYPE (exp->elts[pc + 1].symbol));
948 enum address_class sym_class =
949 SYMBOL_CLASS (exp->elts[pc + 1].symbol);
950
951 if (sym_class == LOC_CONST
952 || sym_class == LOC_CONST_BYTES
953 || sym_class == LOC_REGISTER
954 || sym_class == LOC_REGPARM)
955 error ("Attempt to take address of register or constant.");
956
957 return
958 value_zero (type, not_lval);
959 }
960 else
961 return locate_var_value (exp->elts[pc + 1].symbol, (FRAME) 0);
962
963 default:
964 if (noside == EVAL_AVOID_SIDE_EFFECTS)
965 {
966 value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
967 if (VALUE_LVAL (x) == lval_memory)
968 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
969 not_lval);
970 else
971 error ("Attempt to take address of non-lval");
972 }
973 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
974 }
975 }
976
977 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
978 When used in contexts where arrays will be coerced anyway,
979 this is equivalent to `evaluate_subexp'
980 but much faster because it avoids actually fetching array contents. */
981
982 static value
983 evaluate_subexp_with_coercion (exp, pos, noside)
984 register struct expression *exp;
985 register int *pos;
986 enum noside noside;
987 {
988 register enum exp_opcode op;
989 register int pc;
990 register value val;
991
992 pc = (*pos);
993 op = exp->elts[pc].opcode;
994
995 switch (op)
996 {
997 case OP_VAR_VALUE:
998 if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
999 {
1000 (*pos) += 3;
1001 val = locate_var_value (exp->elts[pc + 1].symbol, (FRAME) 0);
1002 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
1003 val);
1004 }
1005 default:
1006 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1007 }
1008 }
1009
1010 /* Evaluate a subexpression of EXP, at index *POS,
1011 and return a value for the size of that subexpression.
1012 Advance *POS over the subexpression. */
1013
1014 static value
1015 evaluate_subexp_for_sizeof (exp, pos)
1016 register struct expression *exp;
1017 register int *pos;
1018 {
1019 enum exp_opcode op;
1020 register int pc;
1021 value val;
1022
1023 pc = (*pos);
1024 op = exp->elts[pc].opcode;
1025
1026 switch (op)
1027 {
1028 /* This case is handled specially
1029 so that we avoid creating a value for the result type.
1030 If the result type is very big, it's desirable not to
1031 create a value unnecessarily. */
1032 case UNOP_IND:
1033 (*pos)++;
1034 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1035 return value_from_longest (builtin_type_int, (LONGEST)
1036 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1037
1038 case UNOP_MEMVAL:
1039 (*pos) += 3;
1040 return value_from_longest (builtin_type_int,
1041 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1042
1043 case OP_VAR_VALUE:
1044 (*pos) += 3;
1045 return value_from_longest (builtin_type_int,
1046 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
1047
1048 default:
1049 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1050 return value_from_longest (builtin_type_int,
1051 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1052 }
1053 }
1054
1055 /* Parse a type expression in the string [P..P+LENGTH). */
1056
1057 struct type *
1058 parse_and_eval_type (p, length)
1059 char *p;
1060 int length;
1061 {
1062 char *tmp = (char *)alloca (length + 4);
1063 struct expression *expr;
1064 tmp[0] = '(';
1065 bcopy (p, tmp+1, length);
1066 tmp[length+1] = ')';
1067 tmp[length+2] = '0';
1068 tmp[length+3] = '\0';
1069 expr = parse_expression (tmp);
1070 if (expr->elts[0].opcode != UNOP_CAST)
1071 error ("Internal error in eval_type.");
1072 return expr->elts[1].type;
1073 }