]>
Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* Perform non-arithmetic operations on values, for GDB. |
2 | Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
06b6c733 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
06b6c733 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
06b6c733 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
06b6c733 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 RP |
19 | |
20 | #include <stdio.h> | |
21 | #include "defs.h" | |
22 | #include "param.h" | |
23 | #include "symtab.h" | |
24 | #include "value.h" | |
25 | #include "frame.h" | |
26 | #include "inferior.h" | |
27 | #include "gdbcore.h" | |
28 | #include "target.h" | |
29 | ||
30 | #include <errno.h> | |
31 | ||
32 | /* Local functions. */ | |
33 | static value search_struct_field (); | |
34 | \f | |
35 | /* Cast value ARG2 to type TYPE and return as a value. | |
36 | More general than a C cast: accepts any two types of the same length, | |
37 | and if ARG2 is an lvalue it can be cast into anything at all. */ | |
38 | /* In C++, casts may change pointer representations. */ | |
39 | ||
40 | value | |
41 | value_cast (type, arg2) | |
42 | struct type *type; | |
43 | register value arg2; | |
44 | { | |
45 | register enum type_code code1; | |
46 | register enum type_code code2; | |
47 | register int scalar; | |
48 | ||
49 | /* Coerce arrays but not enums. Enums will work as-is | |
50 | and coercing them would cause an infinite recursion. */ | |
51 | if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM) | |
52 | COERCE_ARRAY (arg2); | |
53 | ||
54 | code1 = TYPE_CODE (type); | |
55 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
56 | scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT | |
57 | || code2 == TYPE_CODE_ENUM); | |
58 | ||
59 | if (code1 == TYPE_CODE_FLT && scalar) | |
60 | return value_from_double (type, value_as_double (arg2)); | |
61 | else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM) | |
62 | && (scalar || code2 == TYPE_CODE_PTR)) | |
06b6c733 | 63 | return value_from_longest (type, value_as_long (arg2)); |
bd5635a1 RP |
64 | else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2))) |
65 | { | |
66 | if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) | |
67 | { | |
68 | /* Look in the type of the source to see if it contains the | |
69 | type of the target as a superclass. If so, we'll need to | |
70 | offset the pointer rather than just change its type. */ | |
71 | struct type *t1 = TYPE_TARGET_TYPE (type); | |
72 | struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2)); | |
73 | if (TYPE_CODE (t1) == TYPE_CODE_STRUCT | |
74 | && TYPE_CODE (t2) == TYPE_CODE_STRUCT | |
75 | && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */ | |
76 | { | |
77 | value v = search_struct_field (type_name_no_tag (t1), | |
d3bab255 | 78 | value_ind (arg2), 0, t2, 1); |
bd5635a1 RP |
79 | if (v) |
80 | { | |
81 | v = value_addr (v); | |
82 | VALUE_TYPE (v) = type; | |
83 | return v; | |
84 | } | |
85 | } | |
86 | /* No superclass found, just fall through to change ptr type. */ | |
87 | } | |
88 | VALUE_TYPE (arg2) = type; | |
89 | return arg2; | |
90 | } | |
91 | else if (VALUE_LVAL (arg2) == lval_memory) | |
92 | { | |
93 | return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2)); | |
94 | } | |
d11c44f1 JG |
95 | else if (code1 == TYPE_CODE_VOID) |
96 | { | |
97 | return value_zero (builtin_type_void, not_lval); | |
98 | } | |
bd5635a1 RP |
99 | else |
100 | { | |
101 | error ("Invalid cast."); | |
102 | return 0; | |
103 | } | |
104 | } | |
105 | ||
106 | /* Create a value of type TYPE that is zero, and return it. */ | |
107 | ||
108 | value | |
109 | value_zero (type, lv) | |
110 | struct type *type; | |
111 | enum lval_type lv; | |
112 | { | |
113 | register value val = allocate_value (type); | |
114 | ||
115 | bzero (VALUE_CONTENTS (val), TYPE_LENGTH (type)); | |
116 | VALUE_LVAL (val) = lv; | |
117 | ||
118 | return val; | |
119 | } | |
120 | ||
121 | /* Return a value with type TYPE located at ADDR. | |
122 | ||
123 | Call value_at only if the data needs to be fetched immediately; | |
124 | if we can be 'lazy' and defer the fetch, perhaps indefinately, call | |
125 | value_at_lazy instead. value_at_lazy simply records the address of | |
126 | the data and sets the lazy-evaluation-required flag. The lazy flag | |
127 | is tested in the VALUE_CONTENTS macro, which is used if and when | |
128 | the contents are actually required. */ | |
129 | ||
130 | value | |
131 | value_at (type, addr) | |
132 | struct type *type; | |
133 | CORE_ADDR addr; | |
134 | { | |
135 | register value val = allocate_value (type); | |
136 | ||
137 | read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type)); | |
138 | ||
139 | VALUE_LVAL (val) = lval_memory; | |
140 | VALUE_ADDRESS (val) = addr; | |
141 | ||
142 | return val; | |
143 | } | |
144 | ||
145 | /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */ | |
146 | ||
147 | value | |
148 | value_at_lazy (type, addr) | |
149 | struct type *type; | |
150 | CORE_ADDR addr; | |
151 | { | |
152 | register value val = allocate_value (type); | |
153 | ||
154 | VALUE_LVAL (val) = lval_memory; | |
155 | VALUE_ADDRESS (val) = addr; | |
156 | VALUE_LAZY (val) = 1; | |
157 | ||
158 | return val; | |
159 | } | |
160 | ||
161 | /* Called only from the VALUE_CONTENTS macro, if the current data for | |
162 | a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the | |
163 | data from the user's process, and clears the lazy flag to indicate | |
164 | that the data in the buffer is valid. | |
165 | ||
9cb602e1 JG |
166 | If the value is zero-length, we avoid calling read_memory, which would |
167 | abort. We mark the value as fetched anyway -- all 0 bytes of it. | |
168 | ||
bd5635a1 RP |
169 | This function returns a value because it is used in the VALUE_CONTENTS |
170 | macro as part of an expression, where a void would not work. The | |
171 | value is ignored. */ | |
172 | ||
173 | int | |
174 | value_fetch_lazy (val) | |
175 | register value val; | |
176 | { | |
177 | CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val); | |
178 | ||
9cb602e1 JG |
179 | if (TYPE_LENGTH (VALUE_TYPE (val))) |
180 | read_memory (addr, VALUE_CONTENTS_RAW (val), | |
181 | TYPE_LENGTH (VALUE_TYPE (val))); | |
bd5635a1 RP |
182 | VALUE_LAZY (val) = 0; |
183 | return 0; | |
184 | } | |
185 | ||
186 | ||
187 | /* Store the contents of FROMVAL into the location of TOVAL. | |
188 | Return a new value with the location of TOVAL and contents of FROMVAL. */ | |
189 | ||
190 | value | |
191 | value_assign (toval, fromval) | |
192 | register value toval, fromval; | |
193 | { | |
194 | register struct type *type = VALUE_TYPE (toval); | |
195 | register value val; | |
196 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; | |
197 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; | |
198 | int use_buffer = 0; | |
199 | ||
200 | COERCE_ARRAY (fromval); | |
201 | ||
202 | if (VALUE_LVAL (toval) != lval_internalvar) | |
203 | fromval = value_cast (type, fromval); | |
204 | ||
205 | /* If TOVAL is a special machine register requiring conversion | |
206 | of program values to a special raw format, | |
207 | convert FROMVAL's contents now, with result in `raw_buffer', | |
208 | and set USE_BUFFER to the number of bytes to write. */ | |
209 | ||
210 | if (VALUE_REGNO (toval) >= 0 | |
211 | && REGISTER_CONVERTIBLE (VALUE_REGNO (toval))) | |
212 | { | |
213 | int regno = VALUE_REGNO (toval); | |
214 | if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno)) | |
215 | fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval); | |
216 | bcopy (VALUE_CONTENTS (fromval), virtual_buffer, | |
217 | REGISTER_VIRTUAL_SIZE (regno)); | |
218 | target_convert_from_virtual (regno, virtual_buffer, raw_buffer); | |
219 | use_buffer = REGISTER_RAW_SIZE (regno); | |
220 | } | |
221 | ||
222 | switch (VALUE_LVAL (toval)) | |
223 | { | |
224 | case lval_internalvar: | |
225 | set_internalvar (VALUE_INTERNALVAR (toval), fromval); | |
226 | break; | |
227 | ||
228 | case lval_internalvar_component: | |
229 | set_internalvar_component (VALUE_INTERNALVAR (toval), | |
230 | VALUE_OFFSET (toval), | |
231 | VALUE_BITPOS (toval), | |
232 | VALUE_BITSIZE (toval), | |
233 | fromval); | |
234 | break; | |
235 | ||
236 | case lval_memory: | |
237 | if (VALUE_BITSIZE (toval)) | |
238 | { | |
239 | int v; /* FIXME, this won't work for large bitfields */ | |
240 | read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
241 | &v, sizeof v); | |
242 | modify_field (&v, (int) value_as_long (fromval), | |
243 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
244 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
245 | (char *)&v, sizeof v); | |
246 | } | |
247 | else if (use_buffer) | |
248 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
249 | raw_buffer, use_buffer); | |
250 | else | |
251 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
252 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
253 | break; | |
254 | ||
255 | case lval_register: | |
256 | if (VALUE_BITSIZE (toval)) | |
257 | { | |
258 | int v; | |
259 | ||
260 | read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
261 | &v, sizeof v); | |
262 | modify_field (&v, (int) value_as_long (fromval), | |
263 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
264 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
265 | &v, sizeof v); | |
266 | } | |
267 | else if (use_buffer) | |
268 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
269 | raw_buffer, use_buffer); | |
270 | else | |
271 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
272 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
273 | break; | |
274 | ||
275 | case lval_reg_frame_relative: | |
276 | { | |
277 | /* value is stored in a series of registers in the frame | |
278 | specified by the structure. Copy that value out, modify | |
279 | it, and copy it back in. */ | |
280 | int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type)); | |
281 | int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval)); | |
282 | int byte_offset = VALUE_OFFSET (toval) % reg_size; | |
283 | int reg_offset = VALUE_OFFSET (toval) / reg_size; | |
284 | int amount_copied; | |
285 | char *buffer = (char *) alloca (amount_to_copy); | |
286 | int regno; | |
287 | FRAME frame; | |
288 | ||
289 | /* Figure out which frame this is in currently. */ | |
290 | for (frame = get_current_frame (); | |
291 | frame && FRAME_FP (frame) != VALUE_FRAME (toval); | |
292 | frame = get_prev_frame (frame)) | |
293 | ; | |
294 | ||
295 | if (!frame) | |
296 | error ("Value being assigned to is no longer active."); | |
297 | ||
298 | amount_to_copy += (reg_size - amount_to_copy % reg_size); | |
299 | ||
300 | /* Copy it out. */ | |
301 | for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset, | |
302 | amount_copied = 0); | |
303 | amount_copied < amount_to_copy; | |
304 | amount_copied += reg_size, regno++) | |
305 | { | |
306 | get_saved_register (buffer + amount_copied, | |
307 | (int *)NULL, (CORE_ADDR)NULL, | |
308 | frame, regno, (enum lval_type *)NULL); | |
309 | } | |
310 | ||
311 | /* Modify what needs to be modified. */ | |
312 | if (VALUE_BITSIZE (toval)) | |
313 | modify_field (buffer + byte_offset, | |
314 | (int) value_as_long (fromval), | |
315 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
316 | else if (use_buffer) | |
317 | bcopy (raw_buffer, buffer + byte_offset, use_buffer); | |
318 | else | |
319 | bcopy (VALUE_CONTENTS (fromval), buffer + byte_offset, | |
320 | TYPE_LENGTH (type)); | |
321 | ||
322 | /* Copy it back. */ | |
323 | for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset, | |
324 | amount_copied = 0); | |
325 | amount_copied < amount_to_copy; | |
326 | amount_copied += reg_size, regno++) | |
327 | { | |
328 | enum lval_type lval; | |
329 | CORE_ADDR addr; | |
330 | int optim; | |
331 | ||
332 | /* Just find out where to put it. */ | |
333 | get_saved_register ((char *)NULL, | |
334 | &optim, &addr, frame, regno, &lval); | |
335 | ||
336 | if (optim) | |
337 | error ("Attempt to assign to a value that was optimized out."); | |
338 | if (lval == lval_memory) | |
339 | write_memory (addr, buffer + amount_copied, reg_size); | |
340 | else if (lval == lval_register) | |
341 | write_register_bytes (addr, buffer + amount_copied, reg_size); | |
342 | else | |
343 | error ("Attempt to assign to an unmodifiable value."); | |
344 | } | |
345 | } | |
346 | break; | |
347 | ||
348 | ||
349 | default: | |
350 | error ("Left side of = operation is not an lvalue."); | |
351 | } | |
352 | ||
353 | /* Return a value just like TOVAL except with the contents of FROMVAL | |
354 | (except in the case of the type if TOVAL is an internalvar). */ | |
355 | ||
356 | if (VALUE_LVAL (toval) == lval_internalvar | |
357 | || VALUE_LVAL (toval) == lval_internalvar_component) | |
358 | { | |
359 | type = VALUE_TYPE (fromval); | |
360 | } | |
361 | ||
362 | val = allocate_value (type); | |
363 | bcopy (toval, val, VALUE_CONTENTS_RAW (val) - (char *) val); | |
364 | bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type)); | |
365 | VALUE_TYPE (val) = type; | |
366 | ||
367 | return val; | |
368 | } | |
369 | ||
370 | /* Extend a value VAL to COUNT repetitions of its type. */ | |
371 | ||
372 | value | |
373 | value_repeat (arg1, count) | |
374 | value arg1; | |
375 | int count; | |
376 | { | |
377 | register value val; | |
378 | ||
379 | if (VALUE_LVAL (arg1) != lval_memory) | |
380 | error ("Only values in memory can be extended with '@'."); | |
381 | if (count < 1) | |
382 | error ("Invalid number %d of repetitions.", count); | |
383 | ||
384 | val = allocate_repeat_value (VALUE_TYPE (arg1), count); | |
385 | ||
386 | read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1), | |
387 | VALUE_CONTENTS_RAW (val), | |
388 | TYPE_LENGTH (VALUE_TYPE (val)) * count); | |
389 | VALUE_LVAL (val) = lval_memory; | |
390 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1); | |
391 | ||
392 | return val; | |
393 | } | |
394 | ||
395 | value | |
396 | value_of_variable (var) | |
397 | struct symbol *var; | |
398 | { | |
399 | value val; | |
400 | ||
401 | val = read_var_value (var, (FRAME) 0); | |
402 | if (val == 0) | |
403 | error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var)); | |
404 | return val; | |
405 | } | |
406 | ||
407 | /* Given a value which is an array, return a value which is | |
06b6c733 JG |
408 | a pointer to its first (actually, zeroth) element. |
409 | FIXME, this should be subtracting the array's lower bound. */ | |
bd5635a1 RP |
410 | |
411 | value | |
412 | value_coerce_array (arg1) | |
413 | value arg1; | |
414 | { | |
415 | register struct type *type; | |
bd5635a1 RP |
416 | |
417 | if (VALUE_LVAL (arg1) != lval_memory) | |
418 | error ("Attempt to take address of value not located in memory."); | |
419 | ||
420 | /* Get type of elements. */ | |
421 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY) | |
422 | type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1)); | |
423 | else | |
424 | /* A phony array made by value_repeat. | |
425 | Its type is the type of the elements, not an array type. */ | |
426 | type = VALUE_TYPE (arg1); | |
427 | ||
06b6c733 | 428 | return value_from_longest (lookup_pointer_type (type), |
bd5635a1 | 429 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
430 | } |
431 | ||
432 | /* Given a value which is a function, return a value which is a pointer | |
433 | to it. */ | |
434 | ||
435 | value | |
436 | value_coerce_function (arg1) | |
437 | value arg1; | |
438 | { | |
bd5635a1 RP |
439 | |
440 | if (VALUE_LVAL (arg1) != lval_memory) | |
441 | error ("Attempt to take address of value not located in memory."); | |
442 | ||
06b6c733 | 443 | return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)), |
bd5635a1 | 444 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
445 | } |
446 | ||
447 | /* Return a pointer value for the object for which ARG1 is the contents. */ | |
448 | ||
449 | value | |
450 | value_addr (arg1) | |
451 | value arg1; | |
452 | { | |
bd5635a1 RP |
453 | |
454 | COERCE_REF(arg1); | |
bd5635a1 RP |
455 | if (VALUE_REPEATED (arg1) |
456 | || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY) | |
457 | return value_coerce_array (arg1); | |
458 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FUNC) | |
459 | return value_coerce_function (arg1); | |
460 | ||
461 | if (VALUE_LVAL (arg1) != lval_memory) | |
462 | error ("Attempt to take address of value not located in memory."); | |
463 | ||
06b6c733 | 464 | return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)), |
bd5635a1 | 465 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
466 | } |
467 | ||
468 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
469 | ||
470 | value | |
471 | value_ind (arg1) | |
472 | value arg1; | |
473 | { | |
474 | COERCE_ARRAY (arg1); | |
475 | ||
476 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER) | |
477 | error ("not implemented: member types in value_ind"); | |
478 | ||
479 | /* Allow * on an integer so we can cast it to whatever we want. | |
480 | This returns an int, which seems like the most C-like thing | |
481 | to do. "long long" variables are rare enough that | |
482 | BUILTIN_TYPE_LONGEST would seem to be a mistake. */ | |
483 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT) | |
484 | return value_at (builtin_type_int, | |
485 | (CORE_ADDR) value_as_long (arg1)); | |
486 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) | |
487 | return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)), | |
d11c44f1 | 488 | value_as_pointer (arg1)); |
bd5635a1 RP |
489 | error ("Attempt to take contents of a non-pointer value."); |
490 | return 0; /* For lint -- never reached */ | |
491 | } | |
492 | \f | |
493 | /* Pushing small parts of stack frames. */ | |
494 | ||
495 | /* Push one word (the size of object that a register holds). */ | |
496 | ||
497 | CORE_ADDR | |
498 | push_word (sp, buffer) | |
499 | CORE_ADDR sp; | |
500 | REGISTER_TYPE buffer; | |
501 | { | |
502 | register int len = sizeof (REGISTER_TYPE); | |
503 | ||
f2ebc25f | 504 | SWAP_TARGET_AND_HOST (&buffer, len); |
bd5635a1 RP |
505 | #if 1 INNER_THAN 2 |
506 | sp -= len; | |
507 | write_memory (sp, (char *)&buffer, len); | |
508 | #else /* stack grows upward */ | |
509 | write_memory (sp, (char *)&buffer, len); | |
510 | sp += len; | |
511 | #endif /* stack grows upward */ | |
512 | ||
513 | return sp; | |
514 | } | |
515 | ||
516 | /* Push LEN bytes with data at BUFFER. */ | |
517 | ||
518 | CORE_ADDR | |
519 | push_bytes (sp, buffer, len) | |
520 | CORE_ADDR sp; | |
521 | char *buffer; | |
522 | int len; | |
523 | { | |
524 | #if 1 INNER_THAN 2 | |
525 | sp -= len; | |
526 | write_memory (sp, buffer, len); | |
527 | #else /* stack grows upward */ | |
528 | write_memory (sp, buffer, len); | |
529 | sp += len; | |
530 | #endif /* stack grows upward */ | |
531 | ||
532 | return sp; | |
533 | } | |
534 | ||
535 | /* Push onto the stack the specified value VALUE. */ | |
536 | ||
537 | CORE_ADDR | |
538 | value_push (sp, arg) | |
539 | register CORE_ADDR sp; | |
540 | value arg; | |
541 | { | |
542 | register int len = TYPE_LENGTH (VALUE_TYPE (arg)); | |
543 | ||
544 | #if 1 INNER_THAN 2 | |
545 | sp -= len; | |
546 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
547 | #else /* stack grows upward */ | |
548 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
549 | sp += len; | |
550 | #endif /* stack grows upward */ | |
551 | ||
552 | return sp; | |
553 | } | |
554 | ||
555 | /* Perform the standard coercions that are specified | |
556 | for arguments to be passed to C functions. */ | |
557 | ||
558 | value | |
559 | value_arg_coerce (arg) | |
560 | value arg; | |
561 | { | |
562 | register struct type *type; | |
563 | ||
564 | COERCE_ENUM (arg); | |
565 | ||
566 | type = VALUE_TYPE (arg); | |
567 | ||
568 | if (TYPE_CODE (type) == TYPE_CODE_INT | |
569 | && TYPE_LENGTH (type) < sizeof (int)) | |
570 | return value_cast (builtin_type_int, arg); | |
571 | ||
572 | if (type == builtin_type_float) | |
573 | return value_cast (builtin_type_double, arg); | |
574 | ||
575 | return arg; | |
576 | } | |
577 | ||
578 | /* Push the value ARG, first coercing it as an argument | |
579 | to a C function. */ | |
580 | ||
581 | CORE_ADDR | |
582 | value_arg_push (sp, arg) | |
583 | register CORE_ADDR sp; | |
584 | value arg; | |
585 | { | |
586 | return value_push (sp, value_arg_coerce (arg)); | |
587 | } | |
588 | ||
589 | /* Determine a function's address and its return type from its value. | |
590 | Calls error() if the function is not valid for calling. */ | |
591 | ||
592 | CORE_ADDR | |
593 | find_function_addr (function, retval_type) | |
594 | value function; | |
595 | struct type **retval_type; | |
596 | { | |
597 | register struct type *ftype = VALUE_TYPE (function); | |
598 | register enum type_code code = TYPE_CODE (ftype); | |
599 | struct type *value_type; | |
600 | CORE_ADDR funaddr; | |
601 | ||
602 | /* If it's a member function, just look at the function | |
603 | part of it. */ | |
604 | ||
605 | /* Determine address to call. */ | |
606 | if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD) | |
607 | { | |
608 | funaddr = VALUE_ADDRESS (function); | |
609 | value_type = TYPE_TARGET_TYPE (ftype); | |
610 | } | |
611 | else if (code == TYPE_CODE_PTR) | |
612 | { | |
d11c44f1 | 613 | funaddr = value_as_pointer (function); |
bd5635a1 RP |
614 | if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC |
615 | || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD) | |
616 | value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)); | |
617 | else | |
618 | value_type = builtin_type_int; | |
619 | } | |
620 | else if (code == TYPE_CODE_INT) | |
621 | { | |
622 | /* Handle the case of functions lacking debugging info. | |
623 | Their values are characters since their addresses are char */ | |
624 | if (TYPE_LENGTH (ftype) == 1) | |
d11c44f1 | 625 | funaddr = value_as_pointer (value_addr (function)); |
bd5635a1 RP |
626 | else |
627 | /* Handle integer used as address of a function. */ | |
d11c44f1 | 628 | funaddr = (CORE_ADDR) value_as_long (function); |
bd5635a1 RP |
629 | |
630 | value_type = builtin_type_int; | |
631 | } | |
632 | else | |
633 | error ("Invalid data type for function to be called."); | |
634 | ||
635 | *retval_type = value_type; | |
636 | return funaddr; | |
637 | } | |
638 | ||
639 | #if defined (CALL_DUMMY) | |
640 | /* All this stuff with a dummy frame may seem unnecessarily complicated | |
641 | (why not just save registers in GDB?). The purpose of pushing a dummy | |
642 | frame which looks just like a real frame is so that if you call a | |
643 | function and then hit a breakpoint (get a signal, etc), "backtrace" | |
644 | will look right. Whether the backtrace needs to actually show the | |
645 | stack at the time the inferior function was called is debatable, but | |
646 | it certainly needs to not display garbage. So if you are contemplating | |
647 | making dummy frames be different from normal frames, consider that. */ | |
648 | ||
649 | /* Perform a function call in the inferior. | |
650 | ARGS is a vector of values of arguments (NARGS of them). | |
651 | FUNCTION is a value, the function to be called. | |
652 | Returns a value representing what the function returned. | |
653 | May fail to return, if a breakpoint or signal is hit | |
654 | during the execution of the function. */ | |
655 | ||
656 | value | |
657 | call_function_by_hand (function, nargs, args) | |
658 | value function; | |
659 | int nargs; | |
660 | value *args; | |
661 | { | |
662 | register CORE_ADDR sp; | |
663 | register int i; | |
664 | CORE_ADDR start_sp; | |
f2ebc25f | 665 | /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word |
84d82b1c | 666 | is in host byte order. It is switched to target byte order before calling |
f2ebc25f | 667 | FIX_CALL_DUMMY. */ |
bd5635a1 RP |
668 | static REGISTER_TYPE dummy[] = CALL_DUMMY; |
669 | REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)]; | |
670 | CORE_ADDR old_sp; | |
671 | struct type *value_type; | |
672 | unsigned char struct_return; | |
673 | CORE_ADDR struct_addr; | |
674 | struct inferior_status inf_status; | |
675 | struct cleanup *old_chain; | |
676 | CORE_ADDR funaddr; | |
677 | int using_gcc; | |
678 | ||
679 | save_inferior_status (&inf_status, 1); | |
680 | old_chain = make_cleanup (restore_inferior_status, &inf_status); | |
681 | ||
682 | /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers | |
683 | (and POP_FRAME for restoring them). (At least on most machines) | |
684 | they are saved on the stack in the inferior. */ | |
685 | PUSH_DUMMY_FRAME; | |
686 | ||
687 | old_sp = sp = read_register (SP_REGNUM); | |
688 | ||
689 | #if 1 INNER_THAN 2 /* Stack grows down */ | |
690 | sp -= sizeof dummy; | |
691 | start_sp = sp; | |
692 | #else /* Stack grows up */ | |
693 | start_sp = sp; | |
694 | sp += sizeof dummy; | |
695 | #endif | |
696 | ||
697 | funaddr = find_function_addr (function, &value_type); | |
698 | ||
699 | { | |
700 | struct block *b = block_for_pc (funaddr); | |
701 | /* If compiled without -g, assume GCC. */ | |
702 | using_gcc = b == NULL || BLOCK_GCC_COMPILED (b); | |
703 | } | |
704 | ||
705 | /* Are we returning a value using a structure return or a normal | |
706 | value return? */ | |
707 | ||
708 | struct_return = using_struct_return (function, funaddr, value_type, | |
709 | using_gcc); | |
710 | ||
711 | /* Create a call sequence customized for this function | |
712 | and the number of arguments for it. */ | |
713 | bcopy (dummy, dummy1, sizeof dummy); | |
f2ebc25f JK |
714 | for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++) |
715 | SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE)); | |
bd5635a1 RP |
716 | FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args, |
717 | value_type, using_gcc); | |
718 | ||
719 | #if CALL_DUMMY_LOCATION == ON_STACK | |
720 | write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
721 | ||
722 | #else /* Not on stack. */ | |
723 | #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END | |
724 | /* Convex Unix prohibits executing in the stack segment. */ | |
725 | /* Hope there is empty room at the top of the text segment. */ | |
726 | { | |
84d82b1c | 727 | extern CORE_ADDR text_end; |
bd5635a1 RP |
728 | static checked = 0; |
729 | if (!checked) | |
730 | for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp) | |
731 | if (read_memory_integer (start_sp, 1) != 0) | |
732 | error ("text segment full -- no place to put call"); | |
733 | checked = 1; | |
734 | sp = old_sp; | |
735 | start_sp = text_end - sizeof dummy; | |
736 | write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
737 | } | |
738 | #else /* After text_end. */ | |
739 | { | |
84d82b1c | 740 | extern CORE_ADDR text_end; |
bd5635a1 RP |
741 | int errcode; |
742 | sp = old_sp; | |
743 | start_sp = text_end; | |
744 | errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
745 | if (errcode != 0) | |
746 | error ("Cannot write text segment -- call_function failed"); | |
747 | } | |
748 | #endif /* After text_end. */ | |
749 | #endif /* Not on stack. */ | |
750 | ||
751 | #ifdef lint | |
752 | sp = old_sp; /* It really is used, for some ifdef's... */ | |
753 | #endif | |
754 | ||
755 | #ifdef STACK_ALIGN | |
756 | /* If stack grows down, we must leave a hole at the top. */ | |
757 | { | |
758 | int len = 0; | |
759 | ||
760 | /* Reserve space for the return structure to be written on the | |
761 | stack, if necessary */ | |
762 | ||
763 | if (struct_return) | |
764 | len += TYPE_LENGTH (value_type); | |
765 | ||
766 | for (i = nargs - 1; i >= 0; i--) | |
767 | len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i]))); | |
768 | #ifdef CALL_DUMMY_STACK_ADJUST | |
769 | len += CALL_DUMMY_STACK_ADJUST; | |
770 | #endif | |
771 | #if 1 INNER_THAN 2 | |
772 | sp -= STACK_ALIGN (len) - len; | |
773 | #else | |
774 | sp += STACK_ALIGN (len) - len; | |
775 | #endif | |
776 | } | |
777 | #endif /* STACK_ALIGN */ | |
778 | ||
779 | /* Reserve space for the return structure to be written on the | |
780 | stack, if necessary */ | |
781 | ||
782 | if (struct_return) | |
783 | { | |
784 | #if 1 INNER_THAN 2 | |
785 | sp -= TYPE_LENGTH (value_type); | |
786 | struct_addr = sp; | |
787 | #else | |
788 | struct_addr = sp; | |
789 | sp += TYPE_LENGTH (value_type); | |
790 | #endif | |
791 | } | |
792 | ||
793 | #if defined (REG_STRUCT_HAS_ADDR) | |
794 | { | |
795 | /* This is a machine like the sparc, where we need to pass a pointer | |
796 | to the structure, not the structure itself. */ | |
797 | if (REG_STRUCT_HAS_ADDR (using_gcc)) | |
798 | for (i = nargs - 1; i >= 0; i--) | |
799 | if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT) | |
800 | { | |
801 | CORE_ADDR addr; | |
802 | #if !(1 INNER_THAN 2) | |
803 | /* The stack grows up, so the address of the thing we push | |
804 | is the stack pointer before we push it. */ | |
805 | addr = sp; | |
806 | #endif | |
807 | /* Push the structure. */ | |
808 | sp = value_push (sp, args[i]); | |
809 | #if 1 INNER_THAN 2 | |
810 | /* The stack grows down, so the address of the thing we push | |
811 | is the stack pointer after we push it. */ | |
812 | addr = sp; | |
813 | #endif | |
814 | /* The value we're going to pass is the address of the thing | |
815 | we just pushed. */ | |
06b6c733 JG |
816 | args[i] = value_from_longest (lookup_pointer_type (value_type), |
817 | (LONGEST) addr); | |
bd5635a1 RP |
818 | } |
819 | } | |
820 | #endif /* REG_STRUCT_HAS_ADDR. */ | |
821 | ||
822 | #ifdef PUSH_ARGUMENTS | |
823 | PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr); | |
824 | #else /* !PUSH_ARGUMENTS */ | |
825 | for (i = nargs - 1; i >= 0; i--) | |
826 | sp = value_arg_push (sp, args[i]); | |
827 | #endif /* !PUSH_ARGUMENTS */ | |
828 | ||
829 | #ifdef CALL_DUMMY_STACK_ADJUST | |
830 | #if 1 INNER_THAN 2 | |
831 | sp -= CALL_DUMMY_STACK_ADJUST; | |
832 | #else | |
833 | sp += CALL_DUMMY_STACK_ADJUST; | |
834 | #endif | |
835 | #endif /* CALL_DUMMY_STACK_ADJUST */ | |
836 | ||
837 | /* Store the address at which the structure is supposed to be | |
838 | written. Note that this (and the code which reserved the space | |
839 | above) assumes that gcc was used to compile this function. Since | |
840 | it doesn't cost us anything but space and if the function is pcc | |
841 | it will ignore this value, we will make that assumption. | |
842 | ||
843 | Also note that on some machines (like the sparc) pcc uses a | |
844 | convention like gcc's. */ | |
845 | ||
846 | if (struct_return) | |
847 | STORE_STRUCT_RETURN (struct_addr, sp); | |
848 | ||
849 | /* Write the stack pointer. This is here because the statements above | |
850 | might fool with it. On SPARC, this write also stores the register | |
851 | window into the right place in the new stack frame, which otherwise | |
852 | wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */ | |
853 | write_register (SP_REGNUM, sp); | |
854 | ||
855 | /* Figure out the value returned by the function. */ | |
856 | { | |
857 | char retbuf[REGISTER_BYTES]; | |
858 | ||
859 | /* Execute the stack dummy routine, calling FUNCTION. | |
860 | When it is done, discard the empty frame | |
861 | after storing the contents of all regs into retbuf. */ | |
862 | run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf); | |
863 | ||
864 | do_cleanups (old_chain); | |
865 | ||
866 | return value_being_returned (value_type, retbuf, struct_return); | |
867 | } | |
868 | } | |
869 | #else /* no CALL_DUMMY. */ | |
870 | value | |
871 | call_function_by_hand (function, nargs, args) | |
872 | value function; | |
873 | int nargs; | |
874 | value *args; | |
875 | { | |
876 | error ("Cannot invoke functions on this machine."); | |
877 | } | |
878 | #endif /* no CALL_DUMMY. */ | |
879 | \f | |
880 | /* Create a value for a string constant: | |
881 | Call the function malloc in the inferior to get space for it, | |
882 | then copy the data into that space | |
883 | and then return the address with type char *. | |
884 | PTR points to the string constant data; LEN is number of characters. */ | |
885 | ||
886 | value | |
887 | value_string (ptr, len) | |
888 | char *ptr; | |
889 | int len; | |
890 | { | |
891 | register value val; | |
892 | register struct symbol *sym; | |
893 | value blocklen; | |
894 | register char *copy = (char *) alloca (len + 1); | |
895 | char *i = ptr; | |
896 | register char *o = copy, *ibeg = ptr; | |
897 | register int c; | |
898 | ||
899 | /* Copy the string into COPY, processing escapes. | |
84d82b1c | 900 | We could not conveniently process them in the parser |
bd5635a1 RP |
901 | because the string there wants to be a substring of the input. */ |
902 | ||
903 | while (i - ibeg < len) | |
904 | { | |
905 | c = *i++; | |
906 | if (c == '\\') | |
907 | { | |
908 | c = parse_escape (&i); | |
909 | if (c == -1) | |
910 | continue; | |
911 | } | |
912 | *o++ = c; | |
913 | } | |
914 | *o = 0; | |
915 | ||
916 | /* Get the length of the string after escapes are processed. */ | |
917 | ||
918 | len = o - copy; | |
919 | ||
920 | /* Find the address of malloc in the inferior. */ | |
921 | ||
922 | sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL); | |
923 | if (sym != 0) | |
924 | { | |
925 | if (SYMBOL_CLASS (sym) != LOC_BLOCK) | |
926 | error ("\"malloc\" exists in this program but is not a function."); | |
927 | val = value_of_variable (sym); | |
928 | } | |
929 | else | |
930 | { | |
931 | register int j; | |
06b6c733 JG |
932 | j = lookup_misc_func ("malloc"); |
933 | if (j >= 0) | |
934 | val = value_from_longest ( | |
935 | lookup_pointer_type (lookup_function_type ( | |
936 | lookup_pointer_type (builtin_type_char))), | |
937 | (LONGEST) misc_function_vector[j].address); | |
bd5635a1 RP |
938 | else |
939 | error ("String constants require the program to have a function \"malloc\"."); | |
940 | } | |
941 | ||
06b6c733 | 942 | blocklen = value_from_longest (builtin_type_int, (LONGEST) (len + 1)); |
bd5635a1 RP |
943 | val = target_call_function (val, 1, &blocklen); |
944 | if (value_zerop (val)) | |
945 | error ("No memory available for string constant."); | |
d11c44f1 | 946 | write_memory (value_as_pointer (val), copy, len + 1); |
bd5635a1 RP |
947 | VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char); |
948 | return val; | |
949 | } | |
950 | \f | |
951 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
952 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
953 | and treat the result as having type TYPE. | |
d3bab255 JK |
954 | If found, return value, else return NULL. |
955 | ||
956 | If LOOKING_FOR_BASECLASS, then instead of looking for struct fields, | |
957 | look for a baseclass named NAME. */ | |
bd5635a1 RP |
958 | |
959 | static value | |
d3bab255 | 960 | search_struct_field (name, arg1, offset, type, looking_for_baseclass) |
bd5635a1 RP |
961 | char *name; |
962 | register value arg1; | |
963 | int offset; | |
964 | register struct type *type; | |
d3bab255 | 965 | int looking_for_baseclass; |
bd5635a1 RP |
966 | { |
967 | int i; | |
968 | ||
969 | check_stub_type (type); | |
970 | ||
d3bab255 JK |
971 | if (! looking_for_baseclass) |
972 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
973 | { | |
974 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
975 | ||
976 | if (t_field_name && !strcmp (t_field_name, name)) | |
977 | { | |
978 | value v = (TYPE_FIELD_STATIC (type, i) | |
979 | ? value_static_field (type, name, i) | |
980 | : value_primitive_field (arg1, offset, i, type)); | |
981 | if (v == 0) | |
982 | error("there is no field named %s", name); | |
983 | return v; | |
984 | } | |
985 | } | |
bd5635a1 RP |
986 | |
987 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
988 | { | |
989 | value v; | |
990 | /* If we are looking for baseclasses, this is what we get when we | |
991 | hit them. */ | |
d3bab255 JK |
992 | int found_baseclass = (looking_for_baseclass |
993 | && !strcmp (name, TYPE_BASECLASS_NAME (type, i))); | |
bd5635a1 RP |
994 | |
995 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
996 | { | |
997 | value v2; | |
d11c44f1 JG |
998 | baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, |
999 | &v2, (int *)NULL); | |
bd5635a1 RP |
1000 | if (v2 == 0) |
1001 | error ("virtual baseclass botch"); | |
1002 | if (found_baseclass) | |
1003 | return v2; | |
d3bab255 JK |
1004 | v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i), |
1005 | looking_for_baseclass); | |
bd5635a1 RP |
1006 | if (v) return v; |
1007 | else continue; | |
1008 | } | |
1009 | if (found_baseclass) | |
1010 | v = value_primitive_field (arg1, offset, i, type); | |
1011 | else | |
1012 | v = search_struct_field (name, arg1, | |
1013 | offset + TYPE_BASECLASS_BITPOS (type, i) / 8, | |
d3bab255 JK |
1014 | TYPE_BASECLASS (type, i), |
1015 | looking_for_baseclass); | |
bd5635a1 RP |
1016 | if (v) return v; |
1017 | } | |
1018 | return NULL; | |
1019 | } | |
1020 | ||
1021 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
1022 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
1023 | and treat the result as having type TYPE. | |
1024 | If found, return value, else return NULL. */ | |
1025 | ||
1026 | static value | |
1027 | search_struct_method (name, arg1, args, offset, static_memfuncp, type) | |
1028 | char *name; | |
1029 | register value arg1, *args; | |
1030 | int offset, *static_memfuncp; | |
1031 | register struct type *type; | |
1032 | { | |
1033 | int i; | |
1034 | ||
1035 | check_stub_type (type); | |
1036 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--) | |
1037 | { | |
1038 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
1039 | if (t_field_name && !strcmp (t_field_name, name)) | |
1040 | { | |
d3bab255 | 1041 | int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1; |
bd5635a1 RP |
1042 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); |
1043 | ||
d3bab255 JK |
1044 | if (j > 0 && args == 0) |
1045 | error ("cannot resolve overloaded method `%s'", name); | |
1046 | while (j >= 0) | |
bd5635a1 RP |
1047 | { |
1048 | if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB) | |
1049 | check_stub_method (type, i, j); | |
1050 | if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j), | |
1051 | TYPE_FN_FIELD_ARGS (f, j), args)) | |
1052 | { | |
1053 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
e532974c | 1054 | return (value)value_virtual_fn_field (arg1, f, j, type); |
bd5635a1 RP |
1055 | if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp) |
1056 | *static_memfuncp = 1; | |
1057 | return (value)value_fn_field (arg1, i, j); | |
1058 | } | |
d3bab255 | 1059 | j--; |
bd5635a1 RP |
1060 | } |
1061 | } | |
1062 | } | |
1063 | ||
1064 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1065 | { | |
1066 | value v; | |
1067 | ||
1068 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1069 | { | |
1070 | value v2; | |
d11c44f1 JG |
1071 | baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, |
1072 | &v2, (int *)NULL); | |
bd5635a1 RP |
1073 | if (v2 == 0) |
1074 | error ("virtual baseclass botch"); | |
1075 | v = search_struct_method (name, v2, args, 0, | |
1076 | static_memfuncp, TYPE_BASECLASS (type, i)); | |
1077 | if (v) return v; | |
1078 | else continue; | |
1079 | } | |
1080 | ||
1081 | v = search_struct_method (name, arg1, args, | |
1082 | TYPE_BASECLASS_BITPOS (type, i) / 8, | |
1083 | static_memfuncp, TYPE_BASECLASS (type, i)); | |
1084 | if (v) return v; | |
1085 | } | |
1086 | return NULL; | |
1087 | } | |
1088 | ||
1089 | /* Given *ARGP, a value of type (pointer to a)* structure/union, | |
1090 | extract the component named NAME from the ultimate target structure/union | |
1091 | and return it as a value with its appropriate type. | |
1092 | ERR is used in the error message if *ARGP's type is wrong. | |
1093 | ||
1094 | C++: ARGS is a list of argument types to aid in the selection of | |
1095 | an appropriate method. Also, handle derived types. | |
1096 | ||
1097 | STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location | |
1098 | where the truthvalue of whether the function that was resolved was | |
1099 | a static member function or not is stored. | |
1100 | ||
1101 | ERR is an error message to be printed in case the field is not found. */ | |
1102 | ||
1103 | value | |
1104 | value_struct_elt (argp, args, name, static_memfuncp, err) | |
1105 | register value *argp, *args; | |
1106 | char *name; | |
1107 | int *static_memfuncp; | |
1108 | char *err; | |
1109 | { | |
1110 | register struct type *t; | |
bd5635a1 RP |
1111 | value v; |
1112 | ||
1113 | COERCE_ARRAY (*argp); | |
1114 | ||
1115 | t = VALUE_TYPE (*argp); | |
1116 | ||
1117 | /* Follow pointers until we get to a non-pointer. */ | |
1118 | ||
1119 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1120 | { | |
bd5635a1 | 1121 | *argp = value_ind (*argp); |
f2ebc25f JK |
1122 | /* Don't coerce fn pointer to fn and then back again! */ |
1123 | if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC) | |
1124 | COERCE_ARRAY (*argp); | |
bd5635a1 RP |
1125 | t = VALUE_TYPE (*argp); |
1126 | } | |
1127 | ||
1128 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1129 | error ("not implemented: member type in value_struct_elt"); | |
1130 | ||
1131 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT | |
1132 | && TYPE_CODE (t) != TYPE_CODE_UNION) | |
1133 | error ("Attempt to extract a component of a value that is not a %s.", err); | |
1134 | ||
1135 | /* Assume it's not, unless we see that it is. */ | |
1136 | if (static_memfuncp) | |
1137 | *static_memfuncp =0; | |
1138 | ||
1139 | if (!args) | |
1140 | { | |
1141 | /* if there are no arguments ...do this... */ | |
1142 | ||
d3bab255 | 1143 | /* Try as a field first, because if we succeed, there |
bd5635a1 | 1144 | is less work to be done. */ |
d3bab255 | 1145 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1146 | if (v) |
1147 | return v; | |
1148 | ||
1149 | /* C++: If it was not found as a data field, then try to | |
1150 | return it as a pointer to a method. */ | |
1151 | ||
1152 | if (destructor_name_p (name, t)) | |
1153 | error ("Cannot get value of destructor"); | |
1154 | ||
1155 | v = search_struct_method (name, *argp, args, 0, static_memfuncp, t); | |
1156 | ||
1157 | if (v == 0) | |
1158 | { | |
1159 | if (TYPE_NFN_FIELDS (t)) | |
1160 | error ("There is no member or method named %s.", name); | |
1161 | else | |
1162 | error ("There is no member named %s.", name); | |
1163 | } | |
1164 | return v; | |
1165 | } | |
1166 | ||
1167 | if (destructor_name_p (name, t)) | |
1168 | { | |
1169 | if (!args[1]) | |
1170 | { | |
1171 | /* destructors are a special case. */ | |
1172 | return (value)value_fn_field (*argp, 0, | |
1173 | TYPE_FN_FIELDLIST_LENGTH (t, 0)); | |
1174 | } | |
1175 | else | |
1176 | { | |
1177 | error ("destructor should not have any argument"); | |
1178 | } | |
1179 | } | |
1180 | else | |
1181 | v = search_struct_method (name, *argp, args, 0, static_memfuncp, t); | |
1182 | ||
1183 | if (v == 0) | |
1184 | { | |
1185 | /* See if user tried to invoke data as function. If so, | |
1186 | hand it back. If it's not callable (i.e., a pointer to function), | |
1187 | gdb should give an error. */ | |
d3bab255 | 1188 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1189 | } |
1190 | ||
1191 | if (!v) | |
1192 | error ("Structure has no component named %s.", name); | |
1193 | return v; | |
1194 | } | |
1195 | ||
1196 | /* C++: return 1 is NAME is a legitimate name for the destructor | |
1197 | of type TYPE. If TYPE does not have a destructor, or | |
1198 | if NAME is inappropriate for TYPE, an error is signaled. */ | |
1199 | int | |
1200 | destructor_name_p (name, type) | |
1201 | char *name; | |
1202 | struct type *type; | |
1203 | { | |
1204 | /* destructors are a special case. */ | |
1205 | ||
1206 | if (name[0] == '~') | |
1207 | { | |
1208 | char *dname = type_name_no_tag (type); | |
1209 | ||
1210 | if (! TYPE_HAS_DESTRUCTOR (type)) | |
1211 | error ("type `%s' does not have destructor defined", dname); | |
1212 | if (strcmp (dname, name+1)) | |
1213 | error ("name of destructor must equal name of class"); | |
1214 | else | |
1215 | return 1; | |
1216 | } | |
1217 | return 0; | |
1218 | } | |
1219 | ||
1220 | /* Helper function for check_field: Given TYPE, a structure/union, | |
1221 | return 1 if the component named NAME from the ultimate | |
1222 | target structure/union is defined, otherwise, return 0. */ | |
1223 | ||
1224 | static int | |
1225 | check_field_in (type, name) | |
1226 | register struct type *type; | |
1227 | char *name; | |
1228 | { | |
1229 | register int i; | |
1230 | ||
1231 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1232 | { | |
1233 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
1234 | if (t_field_name && !strcmp (t_field_name, name)) | |
1235 | return 1; | |
1236 | } | |
1237 | ||
1238 | /* C++: If it was not found as a data field, then try to | |
1239 | return it as a pointer to a method. */ | |
1240 | ||
1241 | /* Destructors are a special case. */ | |
1242 | if (destructor_name_p (name, type)) | |
1243 | return 1; | |
1244 | ||
1245 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
1246 | { | |
1247 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name)) | |
1248 | return 1; | |
1249 | } | |
1250 | ||
1251 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1252 | if (check_field_in (TYPE_BASECLASS (type, i), name)) | |
1253 | return 1; | |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | ||
1259 | /* C++: Given ARG1, a value of type (pointer to a)* structure/union, | |
1260 | return 1 if the component named NAME from the ultimate | |
1261 | target structure/union is defined, otherwise, return 0. */ | |
1262 | ||
1263 | int | |
1264 | check_field (arg1, name) | |
1265 | register value arg1; | |
1266 | char *name; | |
1267 | { | |
1268 | register struct type *t; | |
1269 | ||
1270 | COERCE_ARRAY (arg1); | |
1271 | ||
1272 | t = VALUE_TYPE (arg1); | |
1273 | ||
1274 | /* Follow pointers until we get to a non-pointer. */ | |
1275 | ||
1276 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1277 | t = TYPE_TARGET_TYPE (t); | |
1278 | ||
1279 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1280 | error ("not implemented: member type in check_field"); | |
1281 | ||
1282 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT | |
1283 | && TYPE_CODE (t) != TYPE_CODE_UNION) | |
1284 | error ("Internal error: `this' is not an aggregate"); | |
1285 | ||
1286 | return check_field_in (t, name); | |
1287 | } | |
1288 | ||
1289 | /* C++: Given an aggregate type DOMAIN, and a member name NAME, | |
1290 | return the address of this member as a pointer to member | |
1291 | type. If INTYPE is non-null, then it will be the type | |
1292 | of the member we are looking for. This will help us resolve | |
1293 | pointers to member functions. */ | |
1294 | ||
1295 | value | |
1296 | value_struct_elt_for_address (domain, intype, name) | |
1297 | struct type *domain, *intype; | |
1298 | char *name; | |
1299 | { | |
1300 | register struct type *t = domain; | |
1301 | register int i; | |
1302 | value v; | |
1303 | ||
1304 | struct type *baseclass; | |
1305 | ||
1306 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT | |
1307 | && TYPE_CODE (t) != TYPE_CODE_UNION) | |
1308 | error ("Internal error: non-aggregate type to value_struct_elt_for_address"); | |
1309 | ||
1310 | baseclass = t; | |
1311 | ||
1312 | while (t) | |
1313 | { | |
1314 | for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--) | |
1315 | { | |
1316 | char *t_field_name = TYPE_FIELD_NAME (t, i); | |
1317 | if (t_field_name && !strcmp (t_field_name, name)) | |
1318 | { | |
1319 | if (TYPE_FIELD_STATIC (t, i)) | |
1320 | { | |
1321 | char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i); | |
1322 | struct symbol *sym = | |
1323 | lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL); | |
1324 | if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name); | |
06b6c733 JG |
1325 | return value_from_longest ( |
1326 | lookup_pointer_type (TYPE_FIELD_TYPE (t, i)), | |
1327 | (LONGEST)SYMBOL_BLOCK_VALUE (sym)); | |
bd5635a1 RP |
1328 | } |
1329 | if (TYPE_FIELD_PACKED (t, i)) | |
1330 | error ("pointers to bitfield members not allowed"); | |
1331 | ||
06b6c733 JG |
1332 | return value_from_longest ( |
1333 | lookup_pointer_type ( | |
1334 | lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass)), | |
bd5635a1 | 1335 | (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3)); |
bd5635a1 RP |
1336 | } |
1337 | } | |
1338 | ||
1339 | if (TYPE_N_BASECLASSES (t) == 0) | |
1340 | break; | |
1341 | ||
1342 | t = TYPE_BASECLASS (t, 0); | |
1343 | } | |
1344 | ||
1345 | /* C++: If it was not found as a data field, then try to | |
1346 | return it as a pointer to a method. */ | |
1347 | t = baseclass; | |
1348 | ||
1349 | /* Destructors are a special case. */ | |
1350 | if (destructor_name_p (name, t)) | |
1351 | { | |
1352 | error ("pointers to destructors not implemented yet"); | |
1353 | } | |
1354 | ||
1355 | /* Perform all necessary dereferencing. */ | |
1356 | while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR) | |
1357 | intype = TYPE_TARGET_TYPE (intype); | |
1358 | ||
1359 | while (t) | |
1360 | { | |
1361 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) | |
1362 | { | |
1363 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) | |
1364 | { | |
1365 | int j = TYPE_FN_FIELDLIST_LENGTH (t, i); | |
1366 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
1367 | ||
1368 | if (intype == 0 && j > 1) | |
1369 | error ("non-unique member `%s' requires type instantiation", name); | |
1370 | if (intype) | |
1371 | { | |
1372 | while (j--) | |
1373 | if (TYPE_FN_FIELD_TYPE (f, j) == intype) | |
1374 | break; | |
1375 | if (j < 0) | |
1376 | error ("no member function matches that type instantiation"); | |
1377 | } | |
1378 | else | |
1379 | j = 0; | |
1380 | ||
d3bab255 | 1381 | check_stub_method (t, i, j); |
bd5635a1 RP |
1382 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) |
1383 | { | |
06b6c733 JG |
1384 | return value_from_longest ( |
1385 | lookup_pointer_type ( | |
1386 | lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
1387 | baseclass)), | |
bd5635a1 RP |
1388 | (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j)); |
1389 | } | |
1390 | else | |
1391 | { | |
1392 | struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), | |
1393 | 0, VAR_NAMESPACE, 0, NULL); | |
1394 | v = locate_var_value (s, 0); | |
06b6c733 JG |
1395 | VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass)); |
1396 | return v; | |
bd5635a1 | 1397 | } |
bd5635a1 RP |
1398 | } |
1399 | } | |
1400 | ||
1401 | if (TYPE_N_BASECLASSES (t) == 0) | |
1402 | break; | |
1403 | ||
1404 | t = TYPE_BASECLASS (t, 0); | |
1405 | } | |
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | /* Compare two argument lists and return the position in which they differ, | |
1410 | or zero if equal. | |
1411 | ||
1412 | STATICP is nonzero if the T1 argument list came from a | |
1413 | static member function. | |
1414 | ||
1415 | For non-static member functions, we ignore the first argument, | |
1416 | which is the type of the instance variable. This is because we want | |
1417 | to handle calls with objects from derived classes. This is not | |
1418 | entirely correct: we should actually check to make sure that a | |
1419 | requested operation is type secure, shouldn't we? FIXME. */ | |
1420 | ||
1421 | int | |
1422 | typecmp (staticp, t1, t2) | |
1423 | int staticp; | |
1424 | struct type *t1[]; | |
1425 | value t2[]; | |
1426 | { | |
1427 | int i; | |
1428 | ||
d3bab255 JK |
1429 | if (t2 == 0) |
1430 | return 1; | |
bd5635a1 RP |
1431 | if (staticp && t1 == 0) |
1432 | return t2[1] != 0; | |
1433 | if (t1 == 0) | |
1434 | return 1; | |
1435 | if (t1[0]->code == TYPE_CODE_VOID) return 0; | |
1436 | if (t1[!staticp] == 0) return 0; | |
1437 | for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++) | |
1438 | { | |
1439 | if (! t2[i] | |
1440 | || t1[i]->code != t2[i]->type->code | |
1441 | /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */ | |
1442 | ) | |
1443 | return i+1; | |
1444 | } | |
1445 | if (!t1[i]) return 0; | |
1446 | return t2[i] ? i+1 : 0; | |
1447 | } | |
1448 | ||
1449 | /* C++: return the value of the class instance variable, if one exists. | |
1450 | Flag COMPLAIN signals an error if the request is made in an | |
1451 | inappropriate context. */ | |
1452 | value | |
1453 | value_of_this (complain) | |
1454 | int complain; | |
1455 | { | |
1456 | extern FRAME selected_frame; | |
1457 | struct symbol *func, *sym; | |
1458 | struct block *b; | |
1459 | int i; | |
1460 | static const char funny_this[] = "this"; | |
1461 | value this; | |
bd5635a1 RP |
1462 | |
1463 | if (selected_frame == 0) | |
1464 | if (complain) | |
1465 | error ("no frame selected"); | |
1466 | else return 0; | |
1467 | ||
1468 | func = get_frame_function (selected_frame); | |
1469 | if (!func) | |
1470 | { | |
1471 | if (complain) | |
1472 | error ("no `this' in nameless context"); | |
1473 | else return 0; | |
1474 | } | |
1475 | ||
1476 | b = SYMBOL_BLOCK_VALUE (func); | |
1477 | i = BLOCK_NSYMS (b); | |
1478 | if (i <= 0) | |
1479 | if (complain) | |
1480 | error ("no args, no `this'"); | |
1481 | else return 0; | |
1482 | ||
1483 | /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER | |
1484 | symbol instead of the LOC_ARG one (if both exist). */ | |
1485 | sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE); | |
1486 | if (sym == NULL) | |
1487 | { | |
1488 | if (complain) | |
1489 | error ("current stack frame not in method"); | |
1490 | else | |
1491 | return NULL; | |
1492 | } | |
1493 | ||
1494 | this = read_var_value (sym, selected_frame); | |
1495 | if (this == 0 && complain) | |
1496 | error ("`this' argument at unknown address"); | |
1497 | return this; | |
1498 | } |