* eval.c (evaluate_subexp, case UNOP_LOGNOT): If following opcode
[binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "expression.h"
26 #include "target.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_c_expression (exp);
39 register CORE_ADDR addr;
40 register struct cleanup *old_chain
41 = make_cleanup (free_current_contents, &expr);
42
43 addr = (CORE_ADDR) value_as_long (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_c_1 (expptr, 0, 0);
56 register CORE_ADDR addr;
57 register struct cleanup *old_chain
58 = make_cleanup (free_current_contents, &expr);
59
60 addr = (CORE_ADDR) value_as_long (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_c_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_c_1 (expp, 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 expread.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_long (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 access_value_history ((int) exp->elts[pc + 1].longconst);
215
216 case OP_REGISTER:
217 (*pos) += 2;
218 return value_of_register ((int) exp->elts[pc + 1].longconst);
219
220 case OP_INTERNALVAR:
221 (*pos) += 2;
222 return value_of_internalvar (exp->elts[pc + 1].internalvar);
223
224 case OP_STRING:
225 tem = strlen (&exp->elts[pc + 1].string);
226 (*pos) += 2 + ((tem + sizeof (union exp_element))
227 / sizeof (union exp_element));
228 if (noside == EVAL_SKIP)
229 goto nosideret;
230 return value_string (&exp->elts[pc + 1].string, tem);
231
232 case TERNOP_COND:
233 /* Skip third and second args to evaluate the first one. */
234 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
235 if (value_zerop (arg1))
236 {
237 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
238 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
239 }
240 else
241 {
242 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
243 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
244 return arg2;
245 }
246
247 case OP_FUNCALL:
248 (*pos) += 2;
249 op = exp->elts[*pos].opcode;
250 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
251 {
252 int fnptr;
253
254 nargs = (int) exp->elts[pc + 1].longconst + 1;
255 /* First, evaluate the structure into arg2 */
256 pc2 = (*pos)++;
257
258 if (noside == EVAL_SKIP)
259 goto nosideret;
260
261 if (op == STRUCTOP_MEMBER)
262 {
263 arg2 = evaluate_subexp_for_address (exp, pos, noside);
264 }
265 else
266 {
267 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
268 }
269
270 /* If the function is a virtual function, then the
271 aggregate value (providing the structure) plays
272 its part by providing the vtable. Otherwise,
273 it is just along for the ride: call the function
274 directly. */
275
276 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
277
278 fnptr = (int) value_as_long (arg1);
279 /* FIXME-tiemann: this is way obsolete. */
280 if (fnptr < 128)
281 {
282 struct type *basetype;
283 int i, j;
284 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
285 basetype = TYPE_VPTR_BASETYPE (basetype);
286 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
287 {
288 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
289 /* If one is virtual, then all are virtual. */
290 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
291 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
292 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnptr)
293 {
294 value vtbl;
295 value base = value_ind (arg2);
296 struct type *fntype = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
297
298 if (TYPE_VPTR_FIELDNO (basetype) < 0)
299 fill_in_vptr_fieldno (basetype);
300
301 VALUE_TYPE (base) = basetype;
302 vtbl = value_field (base, TYPE_VPTR_FIELDNO (basetype));
303 VALUE_TYPE (vtbl) = lookup_pointer_type (fntype);
304 VALUE_TYPE (arg1) = builtin_type_int;
305 arg1 = value_subscript (vtbl, arg1);
306 VALUE_TYPE (arg1) = fntype;
307 goto got_it;
308 }
309 }
310 if (i < 0)
311 error ("virtual function at index %d not found", fnptr);
312 }
313 else
314 {
315 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
316 }
317 got_it:
318
319 /* Now, say which argument to start evaluating from */
320 tem = 2;
321 }
322 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
323 {
324 /* Hair for method invocations */
325 int tem2;
326
327 nargs = (int) exp->elts[pc + 1].longconst + 1;
328 /* First, evaluate the structure into arg2 */
329 pc2 = (*pos)++;
330 tem2 = strlen (&exp->elts[pc2 + 1].string);
331 *pos += 2 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
332 if (noside == EVAL_SKIP)
333 goto nosideret;
334
335 if (op == STRUCTOP_STRUCT)
336 {
337 arg2 = evaluate_subexp_for_address (exp, pos, noside);
338 }
339 else
340 {
341 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
342 }
343 /* Now, say which argument to start evaluating from */
344 tem = 2;
345 }
346 else
347 {
348 nargs = (int) exp->elts[pc + 1].longconst;
349 tem = 0;
350 }
351 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
352 for (; tem <= nargs; tem++)
353 /* Ensure that array expressions are coerced into pointer objects. */
354 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
355
356 /* signal end of arglist */
357 argvec[tem] = 0;
358
359 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
360 {
361 int static_memfuncp;
362 value temp = arg2;
363
364 argvec[1] = arg2;
365 argvec[0] =
366 value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 1].string,
367 &static_memfuncp,
368 op == STRUCTOP_STRUCT
369 ? "structure" : "structure pointer");
370 if (VALUE_OFFSET (temp))
371 {
372 arg2 = value_from_long (builtin_type_long,
373 value_as_long (arg2)+VALUE_OFFSET (temp));
374 VALUE_TYPE (arg2) = lookup_pointer_type (VALUE_TYPE (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_long (builtin_type_long,
462 value_as_long (arg1) + value_as_long (arg2));
463 VALUE_TYPE (arg3) =
464 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))));
465 return value_ind (arg3);
466
467 case STRUCTOP_MPTR:
468 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
469 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
470 if (noside == EVAL_SKIP)
471 goto nosideret;
472 /* Now, convert these values to an address. */
473 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
474 || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_MEMBER
475 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_METHOD))
476 error ("non-pointer-to-member value used in pointer-to-member construct");
477 arg3 = value_from_long (builtin_type_long,
478 value_as_long (arg1) + value_as_long (arg2));
479 VALUE_TYPE (arg3) =
480 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))));
481 return value_ind (arg3);
482
483 case BINOP_ASSIGN:
484 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
485 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
486 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
487 return arg1;
488 if (binop_user_defined_p (op, arg1, arg2))
489 return value_x_binop (arg1, arg2, op, 0);
490 else
491 return value_assign (arg1, arg2);
492
493 case BINOP_ASSIGN_MODIFY:
494 (*pos) += 2;
495 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
496 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
497 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
498 return arg1;
499 op = exp->elts[pc + 1].opcode;
500 if (binop_user_defined_p (op, arg1, arg2))
501 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
502 else if (op == BINOP_ADD)
503 arg2 = value_add (arg1, arg2);
504 else if (op == BINOP_SUB)
505 arg2 = value_sub (arg1, arg2);
506 else
507 arg2 = value_binop (arg1, arg2, op);
508 return value_assign (arg1, arg2);
509
510 case BINOP_ADD:
511 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
512 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
513 if (noside == EVAL_SKIP)
514 goto nosideret;
515 if (binop_user_defined_p (op, arg1, arg2))
516 return value_x_binop (arg1, arg2, op, 0);
517 else
518 return value_add (arg1, arg2);
519
520 case BINOP_SUB:
521 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
522 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
523 if (noside == EVAL_SKIP)
524 goto nosideret;
525 if (binop_user_defined_p (op, arg1, arg2))
526 return value_x_binop (arg1, arg2, op, 0);
527 else
528 return value_sub (arg1, arg2);
529
530 case BINOP_MUL:
531 case BINOP_DIV:
532 case BINOP_REM:
533 case BINOP_LSH:
534 case BINOP_RSH:
535 case BINOP_LOGAND:
536 case BINOP_LOGIOR:
537 case BINOP_LOGXOR:
538 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
539 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
540 if (noside == EVAL_SKIP)
541 goto nosideret;
542 if (binop_user_defined_p (op, arg1, arg2))
543 return value_x_binop (arg1, arg2, op, 0);
544 else
545 if (noside == EVAL_AVOID_SIDE_EFFECTS
546 && op == BINOP_DIV)
547 return value_zero (VALUE_TYPE (arg1), not_lval);
548 else
549 return value_binop (arg1, arg2, op);
550
551 case BINOP_SUBSCRIPT:
552 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
553 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
554 if (noside == EVAL_SKIP)
555 goto nosideret;
556 if (noside == EVAL_AVOID_SIDE_EFFECTS)
557 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
558 VALUE_LVAL (arg1));
559
560 if (binop_user_defined_p (op, arg1, arg2))
561 return value_x_binop (arg1, arg2, op, 0);
562 else
563 return value_subscript (arg1, arg2);
564
565 case BINOP_AND:
566 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
567 if (noside == EVAL_SKIP)
568 {
569 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
570 goto nosideret;
571 }
572
573 oldpos = *pos;
574 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
575 *pos = oldpos;
576
577 if (binop_user_defined_p (op, arg1, arg2))
578 {
579 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
580 return value_x_binop (arg1, arg2, op, 0);
581 }
582 else
583 {
584 tem = value_zerop (arg1);
585 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
586 (tem ? EVAL_SKIP : noside));
587 return value_from_long (builtin_type_int,
588 (LONGEST) (!tem && !value_zerop (arg2)));
589 }
590
591 case BINOP_OR:
592 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
593 if (noside == EVAL_SKIP)
594 {
595 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
596 goto nosideret;
597 }
598
599 oldpos = *pos;
600 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
601 *pos = oldpos;
602
603 if (binop_user_defined_p (op, arg1, arg2))
604 {
605 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
606 return value_x_binop (arg1, arg2, op, 0);
607 }
608 else
609 {
610 tem = value_zerop (arg1);
611 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
612 (!tem ? EVAL_SKIP : noside));
613 return value_from_long (builtin_type_int,
614 (LONGEST) (!tem || !value_zerop (arg2)));
615 }
616
617 case BINOP_EQUAL:
618 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
619 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
620 if (noside == EVAL_SKIP)
621 goto nosideret;
622 if (binop_user_defined_p (op, arg1, arg2))
623 {
624 return value_x_binop (arg1, arg2, op, 0);
625 }
626 else
627 {
628 tem = value_equal (arg1, arg2);
629 return value_from_long (builtin_type_int, (LONGEST) tem);
630 }
631
632 case BINOP_NOTEQUAL:
633 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
634 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
635 if (noside == EVAL_SKIP)
636 goto nosideret;
637 if (binop_user_defined_p (op, arg1, arg2))
638 {
639 return value_x_binop (arg1, arg2, op, 0);
640 }
641 else
642 {
643 tem = value_equal (arg1, arg2);
644 return value_from_long (builtin_type_int, (LONGEST) ! tem);
645 }
646
647 case BINOP_LESS:
648 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
649 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
650 if (noside == EVAL_SKIP)
651 goto nosideret;
652 if (binop_user_defined_p (op, arg1, arg2))
653 {
654 return value_x_binop (arg1, arg2, op, 0);
655 }
656 else
657 {
658 tem = value_less (arg1, arg2);
659 return value_from_long (builtin_type_int, (LONGEST) tem);
660 }
661
662 case BINOP_GTR:
663 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
664 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
665 if (noside == EVAL_SKIP)
666 goto nosideret;
667 if (binop_user_defined_p (op, arg1, arg2))
668 {
669 return value_x_binop (arg1, arg2, op, 0);
670 }
671 else
672 {
673 tem = value_less (arg2, arg1);
674 return value_from_long (builtin_type_int, (LONGEST) tem);
675 }
676
677 case BINOP_GEQ:
678 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
679 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
680 if (noside == EVAL_SKIP)
681 goto nosideret;
682 if (binop_user_defined_p (op, arg1, arg2))
683 {
684 return value_x_binop (arg1, arg2, op, 0);
685 }
686 else
687 {
688 tem = value_less (arg1, arg2);
689 return value_from_long (builtin_type_int, (LONGEST) ! tem);
690 }
691
692 case BINOP_LEQ:
693 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
694 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
695 if (noside == EVAL_SKIP)
696 goto nosideret;
697 if (binop_user_defined_p (op, arg1, arg2))
698 {
699 return value_x_binop (arg1, arg2, op, 0);
700 }
701 else
702 {
703 tem = value_less (arg2, arg1);
704 return value_from_long (builtin_type_int, (LONGEST) ! tem);
705 }
706
707 case BINOP_REPEAT:
708 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
709 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
710 if (noside == EVAL_SKIP)
711 goto nosideret;
712 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
713 error ("Non-integral right operand for \"@\" operator.");
714 if (noside == EVAL_AVOID_SIDE_EFFECTS)
715 return allocate_repeat_value (VALUE_TYPE (arg1),
716 (int) value_as_long (arg2));
717 else
718 return value_repeat (arg1, (int) value_as_long (arg2));
719
720 case BINOP_COMMA:
721 evaluate_subexp (NULL_TYPE, exp, pos, noside);
722 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
723
724 case UNOP_NEG:
725 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
726 if (noside == EVAL_SKIP)
727 goto nosideret;
728 if (unop_user_defined_p (op, arg1))
729 return value_x_unop (arg1, op);
730 else
731 return value_neg (arg1);
732
733 case UNOP_LOGNOT:
734 /* C++: check for and handle destructor names. */
735 op = exp->elts[*pos].opcode;
736
737 /* FIXME-tiemann: this is a cop-out. */
738 if (op == OP_SCOPE)
739 error ("destructor in eval");
740
741 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
742 if (noside == EVAL_SKIP)
743 goto nosideret;
744 if (unop_user_defined_p (UNOP_LOGNOT, arg1))
745 return value_x_unop (arg1, UNOP_LOGNOT);
746 else
747 return value_lognot (arg1);
748
749 case UNOP_ZEROP:
750 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
751 if (noside == EVAL_SKIP)
752 goto nosideret;
753 if (unop_user_defined_p (op, arg1))
754 return value_x_unop (arg1, op);
755 else
756 return value_from_long (builtin_type_int,
757 (LONGEST) value_zerop (arg1));
758
759 case UNOP_IND:
760 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
761 expect_type = TYPE_TARGET_TYPE (expect_type);
762 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
763 if (noside == EVAL_SKIP)
764 goto nosideret;
765 if (noside == EVAL_AVOID_SIDE_EFFECTS)
766 {
767 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
768 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
769 /* In C you can dereference an array to get the 1st elt. */
770 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
771 )
772 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
773 lval_memory);
774 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
775 /* GDB allows dereferencing an int. */
776 return value_zero (builtin_type_int, lval_memory);
777 else
778 error ("Attempt to take contents of a non-pointer value.");
779 }
780 return value_ind (arg1);
781
782 case UNOP_ADDR:
783 /* C++: check for and handle pointer to members. */
784
785 op = exp->elts[*pos].opcode;
786
787 if (noside == EVAL_SKIP)
788 {
789 if (op == OP_SCOPE)
790 {
791 char *name = &exp->elts[pc+3].string;
792 int temm = strlen (name);
793 (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
794 }
795 else
796 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
797 goto nosideret;
798 }
799
800 if (op == OP_SCOPE)
801 {
802 char *name = &exp->elts[pc+3].string;
803 int temm = strlen (name);
804 struct type *domain = exp->elts[pc+2].type;
805 (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
806 arg1 = value_struct_elt_for_address (domain, expect_type, name);
807 if (arg1)
808 return arg1;
809 error ("no field `%s' in structure", name);
810 }
811 else
812 return evaluate_subexp_for_address (exp, pos, noside);
813
814 case UNOP_SIZEOF:
815 if (noside == EVAL_SKIP)
816 {
817 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
818 goto nosideret;
819 }
820 return evaluate_subexp_for_sizeof (exp, pos);
821
822 case UNOP_CAST:
823 (*pos) += 2;
824 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
825 if (noside == EVAL_SKIP)
826 goto nosideret;
827 return value_cast (exp->elts[pc + 1].type, arg1);
828
829 case UNOP_MEMVAL:
830 (*pos) += 2;
831 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
832 if (noside == EVAL_SKIP)
833 goto nosideret;
834 if (noside == EVAL_AVOID_SIDE_EFFECTS)
835 return value_zero (exp->elts[pc + 1].type, lval_memory);
836 else
837 return value_at_lazy (exp->elts[pc + 1].type,
838 (CORE_ADDR) value_as_long (arg1));
839
840 case UNOP_PREINCREMENT:
841 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
842 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
843 return arg1;
844 else if (unop_user_defined_p (op, arg1))
845 {
846 return value_x_unop (arg1, op);
847 }
848 else
849 {
850 arg2 = value_add (arg1, value_from_long (builtin_type_char,
851 (LONGEST) 1));
852 return value_assign (arg1, arg2);
853 }
854
855 case UNOP_PREDECREMENT:
856 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
857 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
858 return arg1;
859 else if (unop_user_defined_p (op, arg1))
860 {
861 return value_x_unop (arg1, op);
862 }
863 else
864 {
865 arg2 = value_sub (arg1, value_from_long (builtin_type_char,
866 (LONGEST) 1));
867 return value_assign (arg1, arg2);
868 }
869
870 case UNOP_POSTINCREMENT:
871 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
872 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
873 return arg1;
874 else if (unop_user_defined_p (op, arg1))
875 {
876 return value_x_unop (arg1, op);
877 }
878 else
879 {
880 arg2 = value_add (arg1, value_from_long (builtin_type_char,
881 (LONGEST) 1));
882 value_assign (arg1, arg2);
883 return arg1;
884 }
885
886 case UNOP_POSTDECREMENT:
887 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
888 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
889 return arg1;
890 else if (unop_user_defined_p (op, arg1))
891 {
892 return value_x_unop (arg1, op);
893 }
894 else
895 {
896 arg2 = value_sub (arg1, value_from_long (builtin_type_char,
897 (LONGEST) 1));
898 value_assign (arg1, arg2);
899 return arg1;
900 }
901
902 case OP_THIS:
903 (*pos) += 1;
904 return value_of_this (1);
905
906 default:
907 error ("internal error: I do not know how to evaluate what you gave me");
908 }
909
910 nosideret:
911 return value_from_long (builtin_type_long, (LONGEST) 1);
912 }
913 \f
914 /* Evaluate a subexpression of EXP, at index *POS,
915 and return the address of that subexpression.
916 Advance *POS over the subexpression.
917 If the subexpression isn't an lvalue, get an error.
918 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
919 then only the type of the result need be correct. */
920
921 static value
922 evaluate_subexp_for_address (exp, pos, noside)
923 register struct expression *exp;
924 register int *pos;
925 enum noside noside;
926 {
927 enum exp_opcode op;
928 register int pc;
929
930 pc = (*pos);
931 op = exp->elts[pc].opcode;
932
933 switch (op)
934 {
935 case UNOP_IND:
936 (*pos)++;
937 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
938
939 case UNOP_MEMVAL:
940 (*pos) += 3;
941 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
942 evaluate_subexp (NULL_TYPE, exp, pos, noside));
943
944 case OP_VAR_VALUE:
945 (*pos) += 3;
946 if (noside == EVAL_AVOID_SIDE_EFFECTS)
947 {
948 struct type *type =
949 lookup_pointer_type (SYMBOL_TYPE (exp->elts[pc + 1].symbol));
950 enum address_class sym_class =
951 SYMBOL_CLASS (exp->elts[pc + 1].symbol);
952
953 if (sym_class == LOC_CONST
954 || sym_class == LOC_CONST_BYTES
955 || sym_class == LOC_REGISTER
956 || sym_class == LOC_REGPARM)
957 error ("Attempt to take address of register or constant.");
958
959 return
960 value_zero (type, not_lval);
961 }
962 else
963 return locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
964
965 default:
966 if (noside == EVAL_AVOID_SIDE_EFFECTS)
967 {
968 value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
969 if (VALUE_LVAL (x) == lval_memory)
970 return value_zero (TYPE_POINTER_TYPE (VALUE_TYPE (x)),
971 not_lval);
972 else
973 error ("Attempt to take address of non-lval");
974 }
975 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
976 }
977 }
978
979 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
980 When used in contexts where arrays will be coerced anyway,
981 this is equivalent to `evaluate_subexp'
982 but much faster because it avoids actually fetching array contents. */
983
984 static value
985 evaluate_subexp_with_coercion (exp, pos, noside)
986 register struct expression *exp;
987 register int *pos;
988 enum noside noside;
989 {
990 register enum exp_opcode op;
991 register int pc;
992 register value val;
993
994 pc = (*pos);
995 op = exp->elts[pc].opcode;
996
997 switch (op)
998 {
999 case OP_VAR_VALUE:
1000 if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
1001 {
1002 (*pos) += 3;
1003 val = locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
1004 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
1005 val);
1006 }
1007 default:
1008 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1009 }
1010 }
1011
1012 /* Evaluate a subexpression of EXP, at index *POS,
1013 and return a value for the size of that subexpression.
1014 Advance *POS over the subexpression. */
1015
1016 static value
1017 evaluate_subexp_for_sizeof (exp, pos)
1018 register struct expression *exp;
1019 register int *pos;
1020 {
1021 enum exp_opcode op;
1022 register int pc;
1023 value val;
1024
1025 pc = (*pos);
1026 op = exp->elts[pc].opcode;
1027
1028 switch (op)
1029 {
1030 /* This case is handled specially
1031 so that we avoid creating a value for the result type.
1032 If the result type is very big, it's desirable not to
1033 create a value unnecessarily. */
1034 case UNOP_IND:
1035 (*pos)++;
1036 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1037 return value_from_long (builtin_type_int, (LONGEST)
1038 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1039
1040 case UNOP_MEMVAL:
1041 (*pos) += 3;
1042 return value_from_long (builtin_type_int,
1043 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1044
1045 case OP_VAR_VALUE:
1046 (*pos) += 3;
1047 return value_from_long (builtin_type_int,
1048 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
1049
1050 default:
1051 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1052 return value_from_long (builtin_type_int,
1053 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1054 }
1055 }