gdb-2.4+.aux.coff
[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);
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 \f
77 /* Evaluate an expression in internal prefix form
78 such as is constructed by expread.y.
79
80 See expression.h for info on the format of an expression. */
81
82 static value evaluate_subexp ();
83 static value evaluate_subexp_for_address ();
84 static value evaluate_subexp_for_sizeof ();
85 static value evaluate_subexp_with_coercion ();
86
87 /* Values of NOSIDE argument to eval_subexp. */
88 enum noside
89 { EVAL_NORMAL,
90 EVAL_SKIP,
91 EVAL_AVOID_SIDE_EFFECTS,
92 };
93
94 value
95 evaluate_expression (exp)
96 struct expression *exp;
97 {
98 int pc = 0;
99 return evaluate_subexp (exp, &pc, EVAL_NORMAL);
100 }
101
102 /* Evaluate an expression, avoiding all memory references
103 and getting a value whose type alone is correct. */
104
105 value
106 evaluate_type (exp)
107 struct expression *exp;
108 {
109 int pc = 0;
110 return evaluate_subexp (exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
111 }
112
113 static value
114 evaluate_subexp (exp, pos, noside)
115 register struct expression *exp;
116 register int *pos;
117 enum noside noside;
118 {
119 enum exp_opcode op;
120 int tem;
121 register int pc;
122 register value arg1, arg2;
123 int nargs;
124 value *argvec;
125
126 pc = (*pos)++;
127 op = exp->elts[pc].opcode;
128
129 switch (op)
130 {
131 case OP_LONG:
132 (*pos) += 3;
133 return value_from_long (exp->elts[pc + 1].type,
134 exp->elts[pc + 2].longconst);
135
136 case OP_DOUBLE:
137 (*pos) += 3;
138 return value_from_double (exp->elts[pc + 1].type,
139 exp->elts[pc + 2].doubleconst);
140
141 case OP_VAR_VALUE:
142 (*pos) += 2;
143 if (noside == EVAL_SKIP)
144 goto nosideret;
145 return value_of_variable (exp->elts[pc + 1].symbol);
146
147 case OP_LAST:
148 (*pos) += 2;
149 return access_value_history (exp->elts[pc + 1].longconst);
150
151 case OP_REGISTER:
152 (*pos) += 2;
153 return value_of_register (exp->elts[pc + 1].longconst);
154
155 case OP_INTERNALVAR:
156 (*pos) += 2;
157 return value_of_internalvar (exp->elts[pc + 1].internalvar);
158
159 case OP_FUNCALL:
160 (*pos) += 2;
161 nargs = exp->elts[pc + 1].longconst;
162 argvec = (value *) alloca (sizeof (value) * (nargs + 1));
163 for (tem = 0; tem <= nargs; tem++)
164
165 /* Ensure that array expressions are coerced into pointer objects. */
166 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
167
168 if (noside == EVAL_SKIP)
169 goto nosideret;
170 if (noside == EVAL_AVOID_SIDE_EFFECTS)
171 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
172 return call_function (argvec[0], nargs, argvec + 1);
173
174 case OP_STRING:
175 tem = strlen (&exp->elts[pc + 1].string);
176 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
177 if (noside == EVAL_SKIP)
178 goto nosideret;
179 return value_string (&exp->elts[pc + 1].string, tem);
180
181 case TERNOP_COND:
182 /* Skip third and second args to evaluate the first one. */
183 arg1 = evaluate_subexp (exp, pos, noside);
184 if (value_zerop (arg1))
185 {
186 evaluate_subexp (exp, pos, EVAL_SKIP);
187 return evaluate_subexp (exp, pos, noside);
188 }
189 else
190 {
191 arg2 = evaluate_subexp (exp, pos, noside);
192 evaluate_subexp (exp, pos, EVAL_SKIP);
193 return arg2;
194 }
195
196 case STRUCTOP_STRUCT:
197 tem = strlen (&exp->elts[pc + 1].string);
198 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
199 arg1 = evaluate_subexp (exp, pos, noside);
200 if (noside == EVAL_SKIP)
201 goto nosideret;
202 return value_struct_elt (arg1, &exp->elts[pc + 1].string,
203 "structure");
204
205 case STRUCTOP_PTR:
206 tem = strlen (&exp->elts[pc + 1].string);
207 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
208 arg1 = evaluate_subexp (exp, pos, noside);
209 if (noside == EVAL_SKIP)
210 goto nosideret;
211 return value_struct_elt (arg1, &exp->elts[pc + 1].string,
212 "structure pointer");
213
214 case BINOP_ASSIGN:
215 arg1 = evaluate_subexp (exp, pos, noside);
216 arg2 = evaluate_subexp (exp, pos, noside);
217 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
218 return arg1;
219 return value_assign (arg1, arg2);
220
221 case BINOP_ASSIGN_MODIFY:
222 (*pos) += 2;
223 arg1 = evaluate_subexp (exp, pos, noside);
224 arg2 = evaluate_subexp (exp, pos, noside);
225 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
226 return arg1;
227 op = exp->elts[pc + 1].opcode;
228 if (op == BINOP_ADD)
229 arg2 = value_add (arg1, arg2);
230 else if (op == BINOP_SUB)
231 arg2 = value_sub (arg1, arg2);
232 else
233 arg2 = value_binop (arg1, arg2, op);
234 return value_assign (arg1, arg2);
235
236 case BINOP_ADD:
237 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
238 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
239 if (noside == EVAL_SKIP)
240 goto nosideret;
241 return value_add (arg1, arg2);
242
243 case BINOP_SUB:
244 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
245 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
246 if (noside == EVAL_SKIP)
247 goto nosideret;
248 return value_sub (arg1, arg2);
249
250 case BINOP_MUL:
251 case BINOP_DIV:
252 case BINOP_REM:
253 case BINOP_LSH:
254 case BINOP_RSH:
255 case BINOP_LOGAND:
256 case BINOP_LOGIOR:
257 case BINOP_LOGXOR:
258 arg1 = evaluate_subexp (exp, pos, noside);
259 arg2 = evaluate_subexp (exp, pos, noside);
260 if (noside == EVAL_SKIP)
261 goto nosideret;
262 return value_binop (arg1, arg2, op);
263
264 case BINOP_SUBSCRIPT:
265 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
266 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
267 if (noside == EVAL_SKIP)
268 goto nosideret;
269 return value_subscript (arg1, arg2, op);
270
271 case BINOP_AND:
272 arg1 = evaluate_subexp (exp, pos, noside);
273 tem = value_zerop (arg1);
274 arg2 = evaluate_subexp (exp, pos,
275 (tem ? EVAL_SKIP : noside));
276 return value_from_long (builtin_type_int,
277 !tem && !value_zerop (arg2));
278
279 case BINOP_OR:
280 arg1 = evaluate_subexp (exp, pos, noside);
281 tem = value_zerop (arg1);
282 arg2 = evaluate_subexp (exp, pos,
283 (!tem ? EVAL_SKIP : noside));
284 return value_from_long (builtin_type_int,
285 !tem || !value_zerop (arg2));
286
287 case BINOP_EQUAL:
288 arg1 = evaluate_subexp (exp, pos, noside);
289 arg2 = evaluate_subexp (exp, pos, noside);
290 if (noside == EVAL_SKIP)
291 goto nosideret;
292 tem = value_equal (arg1, arg2);
293 return value_from_long (builtin_type_int, tem);
294
295 case BINOP_NOTEQUAL:
296 arg1 = evaluate_subexp (exp, pos, noside);
297 arg2 = evaluate_subexp (exp, pos, noside);
298 if (noside == EVAL_SKIP)
299 goto nosideret;
300 tem = value_equal (arg1, arg2);
301 return value_from_long (builtin_type_int, ! tem);
302
303 case BINOP_LESS:
304 arg1 = evaluate_subexp (exp, pos, noside);
305 arg2 = evaluate_subexp (exp, pos, noside);
306 if (noside == EVAL_SKIP)
307 goto nosideret;
308 tem = value_less (arg1, arg2);
309 return value_from_long (builtin_type_int, tem);
310
311 case BINOP_GTR:
312 arg1 = evaluate_subexp (exp, pos, noside);
313 arg2 = evaluate_subexp (exp, pos, noside);
314 if (noside == EVAL_SKIP)
315 goto nosideret;
316 tem = value_less (arg2, arg1);
317 return value_from_long (builtin_type_int, tem);
318
319 case BINOP_GEQ:
320 arg1 = evaluate_subexp (exp, pos, noside);
321 arg2 = evaluate_subexp (exp, pos, noside);
322 if (noside == EVAL_SKIP)
323 goto nosideret;
324 tem = value_less (arg1, arg2);
325 return value_from_long (builtin_type_int, ! tem);
326
327 case BINOP_LEQ:
328 arg1 = evaluate_subexp (exp, pos, noside);
329 arg2 = evaluate_subexp (exp, pos, noside);
330 if (noside == EVAL_SKIP)
331 goto nosideret;
332 tem = value_less (arg2, arg1);
333 return value_from_long (builtin_type_int, ! tem);
334
335 case BINOP_REPEAT:
336 arg1 = evaluate_subexp (exp, pos, noside);
337 arg2 = evaluate_subexp (exp, pos, noside);
338 if (noside == EVAL_SKIP)
339 goto nosideret;
340 return value_repeat (arg1, value_as_long (arg2));
341
342 case BINOP_COMMA:
343 evaluate_subexp (exp, pos, noside);
344 return evaluate_subexp (exp, pos, noside);
345
346 case UNOP_NEG:
347 arg1 = evaluate_subexp (exp, pos, noside);
348 if (noside == EVAL_SKIP)
349 goto nosideret;
350 return value_neg (arg1);
351
352 case UNOP_LOGNOT:
353 arg1 = evaluate_subexp (exp, pos, noside);
354 if (noside == EVAL_SKIP)
355 goto nosideret;
356 return value_lognot (arg1);
357
358 case UNOP_ZEROP:
359 arg1 = evaluate_subexp (exp, pos, noside);
360 if (noside == EVAL_SKIP)
361 goto nosideret;
362 return value_from_long (builtin_type_int, value_zerop (arg1));
363
364 case UNOP_IND:
365 arg1 = evaluate_subexp (exp, pos, noside);
366 if (noside == EVAL_SKIP)
367 goto nosideret;
368 return value_ind (arg1);
369
370 case UNOP_ADDR:
371 if (noside == EVAL_SKIP)
372 {
373 evaluate_subexp (exp, pos, EVAL_SKIP);
374 goto nosideret;
375 }
376 return evaluate_subexp_for_address (exp, pos, noside);
377
378 case UNOP_SIZEOF:
379 if (noside == EVAL_SKIP)
380 {
381 evaluate_subexp (exp, pos, EVAL_SKIP);
382 goto nosideret;
383 }
384 return evaluate_subexp_for_sizeof (exp, pos);
385
386 case UNOP_CAST:
387 (*pos) += 2;
388 arg1 = evaluate_subexp (exp, pos, noside);
389 if (noside == EVAL_SKIP)
390 goto nosideret;
391 return value_cast (exp->elts[pc + 1].type, arg1);
392
393 case UNOP_MEMVAL:
394 (*pos) += 2;
395 arg1 = evaluate_subexp (exp, pos, noside);
396 if (noside == EVAL_SKIP)
397 goto nosideret;
398 return value_at (exp->elts[pc + 1].type, value_as_long (arg1));
399
400 case UNOP_PREINCREMENT:
401 arg1 = evaluate_subexp (exp, pos, noside);
402 arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
403 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
404 return arg1;
405 return value_assign (arg1, arg2);
406
407 case UNOP_PREDECREMENT:
408 arg1 = evaluate_subexp (exp, pos, noside);
409 arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
410 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
411 return arg1;
412 return value_assign (arg1, arg2);
413
414 case UNOP_POSTINCREMENT:
415 arg1 = evaluate_subexp (exp, pos, noside);
416 arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
417 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
418 return arg1;
419 value_assign (arg1, arg2);
420 return arg1;
421
422 case UNOP_POSTDECREMENT:
423 arg1 = evaluate_subexp (exp, pos, noside);
424 arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
425 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
426 return arg1;
427 value_assign (arg1, arg2);
428 return arg1;
429 }
430
431 nosideret:
432 return value_from_long (builtin_type_long, 1);
433 }
434 \f
435 /* Evaluate a subexpression of EXP, at index *POS,
436 and return the address of that subexpression.
437 Advance *POS over the subexpression.
438 If the subexpression isn't an lvalue, get an error.
439 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
440 then only the type of the result need be correct. */
441
442 static value
443 evaluate_subexp_for_address (exp, pos, noside)
444 register struct expression *exp;
445 register int *pos;
446 enum noside noside;
447 {
448 enum exp_opcode op;
449 register int pc;
450
451 pc = (*pos);
452 op = exp->elts[pc].opcode;
453
454 switch (op)
455 {
456 case UNOP_IND:
457 (*pos)++;
458 return evaluate_subexp (exp, pos, noside);
459
460 case UNOP_MEMVAL:
461 (*pos) += 3;
462 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
463 evaluate_subexp (exp, pos, noside));
464
465 case OP_VAR_VALUE:
466 (*pos) += 3;
467 return locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
468
469 default:
470 return value_addr (evaluate_subexp (exp, pos, noside));
471 }
472 }
473
474 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
475 When used in contexts where arrays will be coerced anyway,
476 this is equivalent to `evaluate_subexp'
477 but much faster because it avoids actually fetching array contents. */
478
479 static value
480 evaluate_subexp_with_coercion (exp, pos, noside)
481 register struct expression *exp;
482 register int *pos;
483 enum noside noside;
484 {
485 register enum exp_opcode op;
486 register int pc;
487 register value val;
488
489 pc = (*pos);
490 op = exp->elts[pc].opcode;
491
492 switch (op)
493 {
494 case OP_VAR_VALUE:
495 if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
496 {
497 (*pos) += 3;
498 val = locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
499 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
500 val);
501 }
502 }
503
504 return evaluate_subexp (exp, pos, noside);
505 }
506
507 /* Evaluate a subexpression of EXP, at index *POS,
508 and return a value for the size of that subexpression.
509 Advance *POS over the subexpression. */
510
511 static value
512 evaluate_subexp_for_sizeof (exp, pos)
513 register struct expression *exp;
514 register int *pos;
515 {
516 enum exp_opcode op;
517 register int pc;
518 value val;
519
520 pc = (*pos);
521 op = exp->elts[pc].opcode;
522
523 switch (op)
524 {
525 /* This case is handled specially
526 so that we avoid creating a value for the result type.
527 If the result type is very big, it's desirable not to
528 create a value unnecessarily. */
529 case UNOP_IND:
530 (*pos)++;
531 val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
532 return value_from_long (builtin_type_int,
533 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
534
535 case UNOP_MEMVAL:
536 (*pos) += 3;
537 return value_from_long (builtin_type_int,
538 TYPE_LENGTH (exp->elts[pc + 1].type));
539
540 case OP_VAR_VALUE:
541 (*pos) += 3;
542 return value_from_long (builtin_type_int,
543 TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
544
545 default:
546 val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
547 return value_from_long (builtin_type_int,
548 TYPE_LENGTH (VALUE_TYPE (val)));
549 }
550 }
551 \f
552 static
553 initialize ()
554 { }
555
556 END_FILE