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