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