gdb-2.5.1
[binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
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.
9
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.
16
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!
19 */
20
21 #include "defs.h"
22 #include "initialize.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "expression.h"
26
27 START_FILE
28 \f
29 /* Parse the string EXP as a C expression, evaluate it,
30 and return the result as a number. */
31
32 CORE_ADDR
33 parse_and_eval_address (exp)
34 char *exp;
35 {
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);
40
41 addr = value_as_long (evaluate_expression (expr));
42 do_cleanups (old_chain);
43 return addr;
44 }
45
46 /* Like parse_and_eval_address but takes a pointer to a char * variable
47 and advanced that variable across the characters parsed. */
48
49 CORE_ADDR
50 parse_and_eval_address_1 (expptr)
51 char **expptr;
52 {
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);
57
58 addr = value_as_long (evaluate_expression (expr));
59 do_cleanups (old_chain);
60 return addr;
61 }
62
63 value
64 parse_and_eval (exp)
65 char *exp;
66 {
67 struct expression *expr = parse_c_expression (exp);
68 register value val;
69 register struct cleanup *old_chain
70 = make_cleanup (free_current_contents, &expr);
71
72 val = evaluate_expression (expr);
73 do_cleanups (old_chain);
74 return val;
75 }
76
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. */
80
81 value
82 parse_to_comma_and_eval (expp)
83 char **expp;
84 {
85 struct expression *expr = parse_c_1 (expp, 0, 1);
86 register value val;
87 register struct cleanup *old_chain
88 = make_cleanup (free_current_contents, &expr);
89
90 val = evaluate_expression (expr);
91 do_cleanups (old_chain);
92 return val;
93 }
94 \f
95 /* Evaluate an expression in internal prefix form
96 such as is constructed by expread.y.
97
98 See expression.h for info on the format of an expression. */
99
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 ();
104
105 /* Values of NOSIDE argument to eval_subexp. */
106 enum noside
107 { EVAL_NORMAL,
108 EVAL_SKIP,
109 EVAL_AVOID_SIDE_EFFECTS,
110 };
111
112 value
113 evaluate_expression (exp)
114 struct expression *exp;
115 {
116 int pc = 0;
117 return evaluate_subexp (exp, &pc, EVAL_NORMAL);
118 }
119
120 /* Evaluate an expression, avoiding all memory references
121 and getting a value whose type alone is correct. */
122
123 value
124 evaluate_type (exp)
125 struct expression *exp;
126 {
127 int pc = 0;
128 return evaluate_subexp (exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
129 }
130
131 static value
132 evaluate_subexp (exp, pos, noside)
133 register struct expression *exp;
134 register int *pos;
135 enum noside noside;
136 {
137 enum exp_opcode op;
138 int tem;
139 register int pc, pc2, *oldpos;
140 register value arg1, arg2, arg3;
141 int nargs;
142 value *argvec;
143
144 pc = (*pos)++;
145 op = exp->elts[pc].opcode;
146
147 switch (op)
148 {
149 case OP_SCOPE:
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);
154
155 case OP_LONG:
156 (*pos) += 3;
157 return value_from_long (exp->elts[pc + 1].type,
158 exp->elts[pc + 2].longconst);
159
160 case OP_DOUBLE:
161 (*pos) += 3;
162 return value_from_double (exp->elts[pc + 1].type,
163 exp->elts[pc + 2].doubleconst);
164
165 case OP_VAR_VALUE:
166 (*pos) += 2;
167 if (noside == EVAL_SKIP)
168 goto nosideret;
169 return value_of_variable (exp->elts[pc + 1].symbol);
170
171 case OP_LAST:
172 (*pos) += 2;
173 return access_value_history (exp->elts[pc + 1].longconst);
174
175 case OP_REGISTER:
176 (*pos) += 2;
177 return value_of_register (exp->elts[pc + 1].longconst);
178
179 case OP_INTERNALVAR:
180 (*pos) += 2;
181 return value_of_internalvar (exp->elts[pc + 1].internalvar);
182
183 case OP_STRING:
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)
187 goto nosideret;
188 return value_string (&exp->elts[pc + 1].string, tem);
189
190 case TERNOP_COND:
191 /* Skip third and second args to evaluate the first one. */
192 arg1 = evaluate_subexp (exp, pos, noside);
193 if (value_zerop (arg1))
194 {
195 evaluate_subexp (exp, pos, EVAL_SKIP);
196 return evaluate_subexp (exp, pos, noside);
197 }
198 else
199 {
200 arg2 = evaluate_subexp (exp, pos, noside);
201 evaluate_subexp (exp, pos, EVAL_SKIP);
202 return arg2;
203 }
204
205 case OP_FUNCALL:
206 (*pos) += 2;
207 op = exp->elts[*pos].opcode;
208 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
209 {
210 int fnptr;
211 int tem2;
212
213 nargs = exp->elts[pc + 1].longconst + 1;
214 /* First, evaluate the structure into arg2 */
215 pc2 = (*pos)++;
216
217 if (noside == EVAL_SKIP)
218 goto nosideret;
219
220 if (op == STRUCTOP_MEMBER)
221 {
222 arg2 = evaluate_subexp_for_address (exp, pos, noside);
223 }
224 else
225 {
226 arg2 = evaluate_subexp (exp, pos, noside);
227 }
228
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
233 directly. */
234
235 arg1 = evaluate_subexp (exp, pos, noside);
236
237 fnptr = value_as_long (arg1);
238 if (fnptr < 128)
239 {
240 struct type *basetype;
241 int i, j;
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--)
245 {
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)
251 {
252 value vtbl;
253 value base = value_ind (arg2);
254 struct type *fntype = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
255
256 if (TYPE_VPTR_FIELDNO (basetype) < 0)
257 TYPE_VPTR_FIELDNO (basetype)
258 = fill_in_vptr_fieldno (basetype);
259
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;
266 goto got_it;
267 }
268 }
269 if (i < 0)
270 error ("virtual function at index %d not found", fnptr);
271 }
272 else
273 {
274 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
275 }
276 got_it:
277
278 /* Now, say which argument to start evaluating from */
279 tem = 2;
280 }
281 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
282 {
283 /* Hair for method invocations */
284 int tem2;
285
286 nargs = exp->elts[pc + 1].longconst + 1;
287 /* First, evaluate the structure into arg2 */
288 pc2 = (*pos)++;
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)
292 goto nosideret;
293
294 if (op == STRUCTOP_STRUCT)
295 {
296 arg2 = evaluate_subexp_for_address (exp, pos, noside);
297 }
298 else
299 {
300 arg2 = evaluate_subexp (exp, pos, noside);
301 }
302 /* Now, say which argument to start evaluating from */
303 tem = 2;
304 }
305 else
306 {
307 nargs = exp->elts[pc + 1].longconst;
308 tem = 0;
309 }
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);
314
315 /* signal end of arglist */
316 argvec[tem] = 0;
317
318 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
319 {
320 argvec[1] = arg2;
321 argvec[0] =
322 value_struct_elt (arg2, argvec+1, &exp->elts[pc2 + 1].string,
323 op == STRUCTOP_STRUCT
324 ? "structure" : "structure pointer");
325 }
326 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
327 {
328 argvec[1] = arg2;
329 argvec[0] = arg1;
330 }
331
332 if (noside == EVAL_SKIP)
333 goto nosideret;
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);
337
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)
343 goto nosideret;
344 return value_struct_elt (arg1, 0, &exp->elts[pc + 1].string,
345 "structure");
346
347 case STRUCTOP_PTR:
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)
352 goto nosideret;
353 return value_struct_elt (arg1, 0, &exp->elts[pc + 1].string,
354 "structure pointer");
355
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)
360 goto nosideret;
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);
369
370 case STRUCTOP_MPTR:
371 arg1 = evaluate_subexp (exp, pos, noside);
372 arg2 = evaluate_subexp (exp, pos, noside);
373 if (noside == EVAL_SKIP)
374 goto nosideret;
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);
383
384 case BINOP_ASSIGN:
385 arg1 = evaluate_subexp (exp, pos, noside);
386 arg2 = evaluate_subexp (exp, pos, noside);
387 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
388 return arg1;
389 return value_assign (arg1, arg2);
390
391 case BINOP_ASSIGN_MODIFY:
392 (*pos) += 2;
393 arg1 = evaluate_subexp (exp, pos, noside);
394 arg2 = evaluate_subexp (exp, pos, noside);
395 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
396 return arg1;
397 op = exp->elts[pc + 1].opcode;
398 if (op == BINOP_ADD)
399 arg2 = value_add (arg1, arg2);
400 else if (op == BINOP_SUB)
401 arg2 = value_sub (arg1, arg2);
402 else
403 arg2 = value_binop (arg1, arg2, op);
404 return value_assign (arg1, arg2);
405
406 case BINOP_ADD:
407 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
408 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
409 if (noside == EVAL_SKIP)
410 goto nosideret;
411 return value_add (arg1, arg2);
412
413 case BINOP_SUB:
414 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
415 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
416 if (noside == EVAL_SKIP)
417 goto nosideret;
418 return value_sub (arg1, arg2);
419
420 case BINOP_MUL:
421 case BINOP_DIV:
422 case BINOP_REM:
423 case BINOP_LSH:
424 case BINOP_RSH:
425 case BINOP_LOGAND:
426 case BINOP_LOGIOR:
427 case BINOP_LOGXOR:
428 arg1 = evaluate_subexp (exp, pos, noside);
429 arg2 = evaluate_subexp (exp, pos, noside);
430 if (noside == EVAL_SKIP)
431 goto nosideret;
432 return value_binop (arg1, arg2, op);
433
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)
438 goto nosideret;
439 return value_subscript (arg1, arg2, op);
440
441 case BINOP_AND:
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));
448
449 case BINOP_OR:
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));
456
457 case BINOP_EQUAL:
458 arg1 = evaluate_subexp (exp, pos, noside);
459 arg2 = evaluate_subexp (exp, pos, noside);
460 if (noside == EVAL_SKIP)
461 goto nosideret;
462 tem = value_equal (arg1, arg2);
463 return value_from_long (builtin_type_int, tem);
464
465 case BINOP_NOTEQUAL:
466 arg1 = evaluate_subexp (exp, pos, noside);
467 arg2 = evaluate_subexp (exp, pos, noside);
468 if (noside == EVAL_SKIP)
469 goto nosideret;
470 tem = value_equal (arg1, arg2);
471 return value_from_long (builtin_type_int, ! tem);
472
473 case BINOP_LESS:
474 arg1 = evaluate_subexp (exp, pos, noside);
475 arg2 = evaluate_subexp (exp, pos, noside);
476 if (noside == EVAL_SKIP)
477 goto nosideret;
478 tem = value_less (arg1, arg2);
479 return value_from_long (builtin_type_int, tem);
480
481 case BINOP_GTR:
482 arg1 = evaluate_subexp (exp, pos, noside);
483 arg2 = evaluate_subexp (exp, pos, noside);
484 if (noside == EVAL_SKIP)
485 goto nosideret;
486 tem = value_less (arg2, arg1);
487 return value_from_long (builtin_type_int, tem);
488
489 case BINOP_GEQ:
490 arg1 = evaluate_subexp (exp, pos, noside);
491 arg2 = evaluate_subexp (exp, pos, noside);
492 if (noside == EVAL_SKIP)
493 goto nosideret;
494 tem = value_less (arg1, arg2);
495 return value_from_long (builtin_type_int, ! tem);
496
497 case BINOP_LEQ:
498 arg1 = evaluate_subexp (exp, pos, noside);
499 arg2 = evaluate_subexp (exp, pos, noside);
500 if (noside == EVAL_SKIP)
501 goto nosideret;
502 tem = value_less (arg2, arg1);
503 return value_from_long (builtin_type_int, ! tem);
504
505 case BINOP_REPEAT:
506 arg1 = evaluate_subexp (exp, pos, noside);
507 arg2 = evaluate_subexp (exp, pos, noside);
508 if (noside == EVAL_SKIP)
509 goto nosideret;
510 return value_repeat (arg1, value_as_long (arg2));
511
512 case BINOP_COMMA:
513 evaluate_subexp (exp, pos, noside);
514 return evaluate_subexp (exp, pos, noside);
515
516 case UNOP_NEG:
517 arg1 = evaluate_subexp (exp, pos, noside);
518 if (noside == EVAL_SKIP)
519 goto nosideret;
520 return value_neg (arg1);
521
522 case UNOP_LOGNOT:
523 arg1 = evaluate_subexp (exp, pos, noside);
524 if (noside == EVAL_SKIP)
525 goto nosideret;
526 return value_lognot (arg1);
527
528 case UNOP_ZEROP:
529 arg1 = evaluate_subexp (exp, pos, noside);
530 if (noside == EVAL_SKIP)
531 goto nosideret;
532 return value_from_long (builtin_type_int, value_zerop (arg1));
533
534 case UNOP_IND:
535 arg1 = evaluate_subexp (exp, pos, noside);
536 if (noside == EVAL_SKIP)
537 goto nosideret;
538 return value_ind (arg1);
539
540 case UNOP_ADDR:
541 if (noside == EVAL_SKIP)
542 {
543 evaluate_subexp (exp, pos, EVAL_SKIP);
544 goto nosideret;
545 }
546 /* C++: check for and handle pointer to members. */
547
548 op = exp->elts[*pos].opcode;
549 if (op == OP_SCOPE)
550 {
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);
556 if (arg1)
557 return arg1;
558 error ("no field `%s' in structure", name);
559 }
560 else
561 return evaluate_subexp_for_address (exp, pos, noside);
562
563 case UNOP_SIZEOF:
564 if (noside == EVAL_SKIP)
565 {
566 evaluate_subexp (exp, pos, EVAL_SKIP);
567 goto nosideret;
568 }
569 return evaluate_subexp_for_sizeof (exp, pos);
570
571 case UNOP_CAST:
572 (*pos) += 2;
573 arg1 = evaluate_subexp (exp, pos, noside);
574 if (noside == EVAL_SKIP)
575 goto nosideret;
576 return value_cast (exp->elts[pc + 1].type, arg1);
577
578 case UNOP_MEMVAL:
579 (*pos) += 2;
580 arg1 = evaluate_subexp (exp, pos, noside);
581 if (noside == EVAL_SKIP)
582 goto nosideret;
583 return value_at (exp->elts[pc + 1].type, value_as_long (arg1));
584
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)
589 return arg1;
590 return value_assign (arg1, arg2);
591
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)
596 return arg1;
597 return value_assign (arg1, arg2);
598
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)
603 return arg1;
604 value_assign (arg1, arg2);
605 return arg1;
606
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)
611 return arg1;
612 value_assign (arg1, arg2);
613 return arg1;
614
615 case OP_THIS:
616 (*pos) += 1;
617 return value_of_this (1);
618
619 default:
620 error ("internal error: I dont know how to evaluation what you gave me");
621 }
622
623 nosideret:
624 return value_from_long (builtin_type_long, 1);
625 }
626 \f
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. */
633
634 static value
635 evaluate_subexp_for_address (exp, pos, noside)
636 register struct expression *exp;
637 register int *pos;
638 enum noside noside;
639 {
640 enum exp_opcode op;
641 register int pc;
642
643 pc = (*pos);
644 op = exp->elts[pc].opcode;
645
646 switch (op)
647 {
648 case UNOP_IND:
649 (*pos)++;
650 return evaluate_subexp (exp, pos, noside);
651
652 case UNOP_MEMVAL:
653 (*pos) += 3;
654 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
655 evaluate_subexp (exp, pos, noside));
656
657 case OP_VAR_VALUE:
658 (*pos) += 3;
659 return locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
660
661 default:
662 return value_addr (evaluate_subexp (exp, pos, noside));
663 }
664 }
665
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. */
670
671 static value
672 evaluate_subexp_with_coercion (exp, pos, noside)
673 register struct expression *exp;
674 register int *pos;
675 enum noside noside;
676 {
677 register enum exp_opcode op;
678 register int pc;
679 register value val;
680
681 pc = (*pos);
682 op = exp->elts[pc].opcode;
683
684 switch (op)
685 {
686 case OP_VAR_VALUE:
687 if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
688 {
689 (*pos) += 3;
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))),
692 val);
693 }
694 }
695
696 return evaluate_subexp (exp, pos, noside);
697 }
698
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. */
702
703 static value
704 evaluate_subexp_for_sizeof (exp, pos)
705 register struct expression *exp;
706 register int *pos;
707 {
708 enum exp_opcode op;
709 register int pc;
710 value val;
711
712 pc = (*pos);
713 op = exp->elts[pc].opcode;
714
715 switch (op)
716 {
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. */
721 case UNOP_IND:
722 (*pos)++;
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))));
726
727 case UNOP_MEMVAL:
728 (*pos) += 3;
729 return value_from_long (builtin_type_int,
730 TYPE_LENGTH (exp->elts[pc + 1].type));
731
732 case OP_VAR_VALUE:
733 (*pos) += 3;
734 return value_from_long (builtin_type_int,
735 TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
736
737 default:
738 val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
739 return value_from_long (builtin_type_int,
740 TYPE_LENGTH (VALUE_TYPE (val)));
741 }
742 }
743 \f
744 static
745 initialize ()
746 { }
747
748 END_FILE