]>
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)); | |
2a5ec41d | 72 | if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT |
bd5635a1 RP |
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 | ||
8e9a3f3b | 114 | memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type)); |
bd5635a1 RP |
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); | |
8e9a3f3b | 200 | COERCE_REF (toval); |
bd5635a1 RP |
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); | |
8e9a3f3b | 216 | memcpy (virtual_buffer, VALUE_CONTENTS (fromval), |
bd5635a1 RP |
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 | { | |
8e9a3f3b PB |
453 | struct type *type = VALUE_TYPE (arg1); |
454 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
455 | { | |
456 | /* Copy the value, but change the type from (T&) to (T*). | |
457 | We keep the same location information, which is efficient, | |
458 | and allows &(&X) to get the location containing the reference. */ | |
459 | value arg2 = value_copy (arg1); | |
460 | VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type)); | |
461 | return arg2; | |
462 | } | |
bd5635a1 | 463 | if (VALUE_REPEATED (arg1) |
8e9a3f3b | 464 | || TYPE_CODE (type) == TYPE_CODE_ARRAY) |
bd5635a1 | 465 | return value_coerce_array (arg1); |
8e9a3f3b | 466 | if (TYPE_CODE (type) == TYPE_CODE_FUNC) |
bd5635a1 RP |
467 | return value_coerce_function (arg1); |
468 | ||
469 | if (VALUE_LVAL (arg1) != lval_memory) | |
470 | error ("Attempt to take address of value not located in memory."); | |
471 | ||
8e9a3f3b | 472 | return value_from_longest (lookup_pointer_type (type), |
bd5635a1 | 473 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
474 | } |
475 | ||
476 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
477 | ||
478 | value | |
479 | value_ind (arg1) | |
480 | value arg1; | |
481 | { | |
482 | COERCE_ARRAY (arg1); | |
483 | ||
484 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER) | |
485 | error ("not implemented: member types in value_ind"); | |
486 | ||
487 | /* Allow * on an integer so we can cast it to whatever we want. | |
488 | This returns an int, which seems like the most C-like thing | |
489 | to do. "long long" variables are rare enough that | |
490 | BUILTIN_TYPE_LONGEST would seem to be a mistake. */ | |
491 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT) | |
492 | return value_at (builtin_type_int, | |
493 | (CORE_ADDR) value_as_long (arg1)); | |
494 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) | |
495 | return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)), | |
d11c44f1 | 496 | value_as_pointer (arg1)); |
bd5635a1 RP |
497 | error ("Attempt to take contents of a non-pointer value."); |
498 | return 0; /* For lint -- never reached */ | |
499 | } | |
500 | \f | |
501 | /* Pushing small parts of stack frames. */ | |
502 | ||
503 | /* Push one word (the size of object that a register holds). */ | |
504 | ||
505 | CORE_ADDR | |
506 | push_word (sp, buffer) | |
507 | CORE_ADDR sp; | |
508 | REGISTER_TYPE buffer; | |
509 | { | |
510 | register int len = sizeof (REGISTER_TYPE); | |
511 | ||
f2ebc25f | 512 | SWAP_TARGET_AND_HOST (&buffer, len); |
bd5635a1 RP |
513 | #if 1 INNER_THAN 2 |
514 | sp -= len; | |
515 | write_memory (sp, (char *)&buffer, len); | |
516 | #else /* stack grows upward */ | |
517 | write_memory (sp, (char *)&buffer, len); | |
518 | sp += len; | |
519 | #endif /* stack grows upward */ | |
520 | ||
521 | return sp; | |
522 | } | |
523 | ||
524 | /* Push LEN bytes with data at BUFFER. */ | |
525 | ||
526 | CORE_ADDR | |
527 | push_bytes (sp, buffer, len) | |
528 | CORE_ADDR sp; | |
529 | char *buffer; | |
530 | int len; | |
531 | { | |
532 | #if 1 INNER_THAN 2 | |
533 | sp -= len; | |
534 | write_memory (sp, buffer, len); | |
535 | #else /* stack grows upward */ | |
536 | write_memory (sp, buffer, len); | |
537 | sp += len; | |
538 | #endif /* stack grows upward */ | |
539 | ||
540 | return sp; | |
541 | } | |
542 | ||
543 | /* Push onto the stack the specified value VALUE. */ | |
544 | ||
545 | CORE_ADDR | |
546 | value_push (sp, arg) | |
547 | register CORE_ADDR sp; | |
548 | value arg; | |
549 | { | |
550 | register int len = TYPE_LENGTH (VALUE_TYPE (arg)); | |
551 | ||
552 | #if 1 INNER_THAN 2 | |
553 | sp -= len; | |
554 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
555 | #else /* stack grows upward */ | |
556 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
557 | sp += len; | |
558 | #endif /* stack grows upward */ | |
559 | ||
560 | return sp; | |
561 | } | |
562 | ||
563 | /* Perform the standard coercions that are specified | |
564 | for arguments to be passed to C functions. */ | |
565 | ||
566 | value | |
567 | value_arg_coerce (arg) | |
568 | value arg; | |
569 | { | |
570 | register struct type *type; | |
571 | ||
572 | COERCE_ENUM (arg); | |
573 | ||
574 | type = VALUE_TYPE (arg); | |
575 | ||
576 | if (TYPE_CODE (type) == TYPE_CODE_INT | |
2a5ec41d | 577 | && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) |
bd5635a1 RP |
578 | return value_cast (builtin_type_int, arg); |
579 | ||
2a5ec41d JG |
580 | if (TYPE_CODE (type) == TYPE_CODE_FLT |
581 | && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double)) | |
bd5635a1 RP |
582 | return value_cast (builtin_type_double, arg); |
583 | ||
584 | return arg; | |
585 | } | |
586 | ||
587 | /* Push the value ARG, first coercing it as an argument | |
588 | to a C function. */ | |
589 | ||
590 | CORE_ADDR | |
591 | value_arg_push (sp, arg) | |
592 | register CORE_ADDR sp; | |
593 | value arg; | |
594 | { | |
595 | return value_push (sp, value_arg_coerce (arg)); | |
596 | } | |
597 | ||
598 | /* Determine a function's address and its return type from its value. | |
599 | Calls error() if the function is not valid for calling. */ | |
600 | ||
601 | CORE_ADDR | |
602 | find_function_addr (function, retval_type) | |
603 | value function; | |
604 | struct type **retval_type; | |
605 | { | |
606 | register struct type *ftype = VALUE_TYPE (function); | |
607 | register enum type_code code = TYPE_CODE (ftype); | |
608 | struct type *value_type; | |
609 | CORE_ADDR funaddr; | |
610 | ||
611 | /* If it's a member function, just look at the function | |
612 | part of it. */ | |
613 | ||
614 | /* Determine address to call. */ | |
615 | if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD) | |
616 | { | |
617 | funaddr = VALUE_ADDRESS (function); | |
618 | value_type = TYPE_TARGET_TYPE (ftype); | |
619 | } | |
620 | else if (code == TYPE_CODE_PTR) | |
621 | { | |
d11c44f1 | 622 | funaddr = value_as_pointer (function); |
bd5635a1 RP |
623 | if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC |
624 | || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD) | |
625 | value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)); | |
626 | else | |
627 | value_type = builtin_type_int; | |
628 | } | |
629 | else if (code == TYPE_CODE_INT) | |
630 | { | |
631 | /* Handle the case of functions lacking debugging info. | |
632 | Their values are characters since their addresses are char */ | |
633 | if (TYPE_LENGTH (ftype) == 1) | |
d11c44f1 | 634 | funaddr = value_as_pointer (value_addr (function)); |
bd5635a1 RP |
635 | else |
636 | /* Handle integer used as address of a function. */ | |
d11c44f1 | 637 | funaddr = (CORE_ADDR) value_as_long (function); |
bd5635a1 RP |
638 | |
639 | value_type = builtin_type_int; | |
640 | } | |
641 | else | |
642 | error ("Invalid data type for function to be called."); | |
643 | ||
644 | *retval_type = value_type; | |
645 | return funaddr; | |
646 | } | |
647 | ||
648 | #if defined (CALL_DUMMY) | |
649 | /* All this stuff with a dummy frame may seem unnecessarily complicated | |
650 | (why not just save registers in GDB?). The purpose of pushing a dummy | |
651 | frame which looks just like a real frame is so that if you call a | |
652 | function and then hit a breakpoint (get a signal, etc), "backtrace" | |
653 | will look right. Whether the backtrace needs to actually show the | |
654 | stack at the time the inferior function was called is debatable, but | |
655 | it certainly needs to not display garbage. So if you are contemplating | |
656 | making dummy frames be different from normal frames, consider that. */ | |
657 | ||
658 | /* Perform a function call in the inferior. | |
659 | ARGS is a vector of values of arguments (NARGS of them). | |
660 | FUNCTION is a value, the function to be called. | |
661 | Returns a value representing what the function returned. | |
662 | May fail to return, if a breakpoint or signal is hit | |
663 | during the execution of the function. */ | |
664 | ||
665 | value | |
666 | call_function_by_hand (function, nargs, args) | |
667 | value function; | |
668 | int nargs; | |
669 | value *args; | |
670 | { | |
671 | register CORE_ADDR sp; | |
672 | register int i; | |
673 | CORE_ADDR start_sp; | |
f2ebc25f | 674 | /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word |
84d82b1c | 675 | is in host byte order. It is switched to target byte order before calling |
f2ebc25f | 676 | FIX_CALL_DUMMY. */ |
bd5635a1 RP |
677 | static REGISTER_TYPE dummy[] = CALL_DUMMY; |
678 | REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)]; | |
679 | CORE_ADDR old_sp; | |
680 | struct type *value_type; | |
681 | unsigned char struct_return; | |
682 | CORE_ADDR struct_addr; | |
683 | struct inferior_status inf_status; | |
684 | struct cleanup *old_chain; | |
685 | CORE_ADDR funaddr; | |
686 | int using_gcc; | |
687 | ||
688 | save_inferior_status (&inf_status, 1); | |
689 | old_chain = make_cleanup (restore_inferior_status, &inf_status); | |
690 | ||
691 | /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers | |
692 | (and POP_FRAME for restoring them). (At least on most machines) | |
693 | they are saved on the stack in the inferior. */ | |
694 | PUSH_DUMMY_FRAME; | |
695 | ||
696 | old_sp = sp = read_register (SP_REGNUM); | |
697 | ||
698 | #if 1 INNER_THAN 2 /* Stack grows down */ | |
699 | sp -= sizeof dummy; | |
700 | start_sp = sp; | |
701 | #else /* Stack grows up */ | |
702 | start_sp = sp; | |
703 | sp += sizeof dummy; | |
704 | #endif | |
705 | ||
706 | funaddr = find_function_addr (function, &value_type); | |
707 | ||
708 | { | |
709 | struct block *b = block_for_pc (funaddr); | |
710 | /* If compiled without -g, assume GCC. */ | |
711 | using_gcc = b == NULL || BLOCK_GCC_COMPILED (b); | |
712 | } | |
713 | ||
714 | /* Are we returning a value using a structure return or a normal | |
715 | value return? */ | |
716 | ||
717 | struct_return = using_struct_return (function, funaddr, value_type, | |
718 | using_gcc); | |
719 | ||
720 | /* Create a call sequence customized for this function | |
721 | and the number of arguments for it. */ | |
722 | bcopy (dummy, dummy1, sizeof dummy); | |
f2ebc25f JK |
723 | for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++) |
724 | SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE)); | |
bd5635a1 RP |
725 | FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args, |
726 | value_type, using_gcc); | |
727 | ||
728 | #if CALL_DUMMY_LOCATION == ON_STACK | |
729 | write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
730 | ||
731 | #else /* Not on stack. */ | |
732 | #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END | |
733 | /* Convex Unix prohibits executing in the stack segment. */ | |
734 | /* Hope there is empty room at the top of the text segment. */ | |
735 | { | |
84d82b1c | 736 | extern CORE_ADDR text_end; |
bd5635a1 RP |
737 | static checked = 0; |
738 | if (!checked) | |
739 | for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp) | |
740 | if (read_memory_integer (start_sp, 1) != 0) | |
741 | error ("text segment full -- no place to put call"); | |
742 | checked = 1; | |
743 | sp = old_sp; | |
744 | start_sp = text_end - sizeof dummy; | |
745 | write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
746 | } | |
747 | #else /* After text_end. */ | |
748 | { | |
84d82b1c | 749 | extern CORE_ADDR text_end; |
bd5635a1 RP |
750 | int errcode; |
751 | sp = old_sp; | |
752 | start_sp = text_end; | |
753 | errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
754 | if (errcode != 0) | |
755 | error ("Cannot write text segment -- call_function failed"); | |
756 | } | |
757 | #endif /* After text_end. */ | |
758 | #endif /* Not on stack. */ | |
759 | ||
760 | #ifdef lint | |
761 | sp = old_sp; /* It really is used, for some ifdef's... */ | |
762 | #endif | |
763 | ||
764 | #ifdef STACK_ALIGN | |
765 | /* If stack grows down, we must leave a hole at the top. */ | |
766 | { | |
767 | int len = 0; | |
768 | ||
769 | /* Reserve space for the return structure to be written on the | |
770 | stack, if necessary */ | |
771 | ||
772 | if (struct_return) | |
773 | len += TYPE_LENGTH (value_type); | |
774 | ||
775 | for (i = nargs - 1; i >= 0; i--) | |
776 | len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i]))); | |
777 | #ifdef CALL_DUMMY_STACK_ADJUST | |
778 | len += CALL_DUMMY_STACK_ADJUST; | |
779 | #endif | |
780 | #if 1 INNER_THAN 2 | |
781 | sp -= STACK_ALIGN (len) - len; | |
782 | #else | |
783 | sp += STACK_ALIGN (len) - len; | |
784 | #endif | |
785 | } | |
786 | #endif /* STACK_ALIGN */ | |
787 | ||
788 | /* Reserve space for the return structure to be written on the | |
789 | stack, if necessary */ | |
790 | ||
791 | if (struct_return) | |
792 | { | |
793 | #if 1 INNER_THAN 2 | |
794 | sp -= TYPE_LENGTH (value_type); | |
795 | struct_addr = sp; | |
796 | #else | |
797 | struct_addr = sp; | |
798 | sp += TYPE_LENGTH (value_type); | |
799 | #endif | |
800 | } | |
801 | ||
802 | #if defined (REG_STRUCT_HAS_ADDR) | |
803 | { | |
804 | /* This is a machine like the sparc, where we need to pass a pointer | |
805 | to the structure, not the structure itself. */ | |
806 | if (REG_STRUCT_HAS_ADDR (using_gcc)) | |
807 | for (i = nargs - 1; i >= 0; i--) | |
808 | if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT) | |
809 | { | |
810 | CORE_ADDR addr; | |
811 | #if !(1 INNER_THAN 2) | |
812 | /* The stack grows up, so the address of the thing we push | |
813 | is the stack pointer before we push it. */ | |
814 | addr = sp; | |
815 | #endif | |
816 | /* Push the structure. */ | |
817 | sp = value_push (sp, args[i]); | |
818 | #if 1 INNER_THAN 2 | |
819 | /* The stack grows down, so the address of the thing we push | |
820 | is the stack pointer after we push it. */ | |
821 | addr = sp; | |
822 | #endif | |
823 | /* The value we're going to pass is the address of the thing | |
824 | we just pushed. */ | |
06b6c733 JG |
825 | args[i] = value_from_longest (lookup_pointer_type (value_type), |
826 | (LONGEST) addr); | |
bd5635a1 RP |
827 | } |
828 | } | |
829 | #endif /* REG_STRUCT_HAS_ADDR. */ | |
830 | ||
831 | #ifdef PUSH_ARGUMENTS | |
832 | PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr); | |
833 | #else /* !PUSH_ARGUMENTS */ | |
834 | for (i = nargs - 1; i >= 0; i--) | |
835 | sp = value_arg_push (sp, args[i]); | |
836 | #endif /* !PUSH_ARGUMENTS */ | |
837 | ||
838 | #ifdef CALL_DUMMY_STACK_ADJUST | |
839 | #if 1 INNER_THAN 2 | |
840 | sp -= CALL_DUMMY_STACK_ADJUST; | |
841 | #else | |
842 | sp += CALL_DUMMY_STACK_ADJUST; | |
843 | #endif | |
844 | #endif /* CALL_DUMMY_STACK_ADJUST */ | |
845 | ||
846 | /* Store the address at which the structure is supposed to be | |
847 | written. Note that this (and the code which reserved the space | |
848 | above) assumes that gcc was used to compile this function. Since | |
849 | it doesn't cost us anything but space and if the function is pcc | |
850 | it will ignore this value, we will make that assumption. | |
851 | ||
852 | Also note that on some machines (like the sparc) pcc uses a | |
853 | convention like gcc's. */ | |
854 | ||
855 | if (struct_return) | |
856 | STORE_STRUCT_RETURN (struct_addr, sp); | |
857 | ||
858 | /* Write the stack pointer. This is here because the statements above | |
859 | might fool with it. On SPARC, this write also stores the register | |
860 | window into the right place in the new stack frame, which otherwise | |
861 | wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */ | |
862 | write_register (SP_REGNUM, sp); | |
863 | ||
864 | /* Figure out the value returned by the function. */ | |
865 | { | |
866 | char retbuf[REGISTER_BYTES]; | |
867 | ||
868 | /* Execute the stack dummy routine, calling FUNCTION. | |
869 | When it is done, discard the empty frame | |
870 | after storing the contents of all regs into retbuf. */ | |
871 | run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf); | |
872 | ||
873 | do_cleanups (old_chain); | |
874 | ||
875 | return value_being_returned (value_type, retbuf, struct_return); | |
876 | } | |
877 | } | |
878 | #else /* no CALL_DUMMY. */ | |
879 | value | |
880 | call_function_by_hand (function, nargs, args) | |
881 | value function; | |
882 | int nargs; | |
883 | value *args; | |
884 | { | |
885 | error ("Cannot invoke functions on this machine."); | |
886 | } | |
887 | #endif /* no CALL_DUMMY. */ | |
888 | \f | |
889 | /* Create a value for a string constant: | |
890 | Call the function malloc in the inferior to get space for it, | |
891 | then copy the data into that space | |
892 | and then return the address with type char *. | |
893 | PTR points to the string constant data; LEN is number of characters. */ | |
894 | ||
895 | value | |
896 | value_string (ptr, len) | |
897 | char *ptr; | |
898 | int len; | |
899 | { | |
900 | register value val; | |
901 | register struct symbol *sym; | |
902 | value blocklen; | |
903 | register char *copy = (char *) alloca (len + 1); | |
904 | char *i = ptr; | |
905 | register char *o = copy, *ibeg = ptr; | |
906 | register int c; | |
907 | ||
908 | /* Copy the string into COPY, processing escapes. | |
84d82b1c | 909 | We could not conveniently process them in the parser |
bd5635a1 RP |
910 | because the string there wants to be a substring of the input. */ |
911 | ||
912 | while (i - ibeg < len) | |
913 | { | |
914 | c = *i++; | |
915 | if (c == '\\') | |
916 | { | |
917 | c = parse_escape (&i); | |
918 | if (c == -1) | |
919 | continue; | |
920 | } | |
921 | *o++ = c; | |
922 | } | |
923 | *o = 0; | |
924 | ||
925 | /* Get the length of the string after escapes are processed. */ | |
926 | ||
927 | len = o - copy; | |
928 | ||
929 | /* Find the address of malloc in the inferior. */ | |
930 | ||
931 | sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL); | |
932 | if (sym != 0) | |
933 | { | |
934 | if (SYMBOL_CLASS (sym) != LOC_BLOCK) | |
935 | error ("\"malloc\" exists in this program but is not a function."); | |
936 | val = value_of_variable (sym); | |
937 | } | |
938 | else | |
939 | { | |
940 | register int j; | |
06b6c733 JG |
941 | j = lookup_misc_func ("malloc"); |
942 | if (j >= 0) | |
943 | val = value_from_longest ( | |
944 | lookup_pointer_type (lookup_function_type ( | |
945 | lookup_pointer_type (builtin_type_char))), | |
946 | (LONGEST) misc_function_vector[j].address); | |
bd5635a1 RP |
947 | else |
948 | error ("String constants require the program to have a function \"malloc\"."); | |
949 | } | |
950 | ||
06b6c733 | 951 | blocklen = value_from_longest (builtin_type_int, (LONGEST) (len + 1)); |
bd5635a1 RP |
952 | val = target_call_function (val, 1, &blocklen); |
953 | if (value_zerop (val)) | |
954 | error ("No memory available for string constant."); | |
d11c44f1 | 955 | write_memory (value_as_pointer (val), copy, len + 1); |
bd5635a1 RP |
956 | VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char); |
957 | return val; | |
958 | } | |
959 | \f | |
960 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
961 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
2a5ec41d | 962 | and search in it assuming it has (class) type TYPE. |
d3bab255 JK |
963 | If found, return value, else return NULL. |
964 | ||
965 | If LOOKING_FOR_BASECLASS, then instead of looking for struct fields, | |
966 | look for a baseclass named NAME. */ | |
bd5635a1 RP |
967 | |
968 | static value | |
d3bab255 | 969 | search_struct_field (name, arg1, offset, type, looking_for_baseclass) |
bd5635a1 RP |
970 | char *name; |
971 | register value arg1; | |
972 | int offset; | |
973 | register struct type *type; | |
d3bab255 | 974 | int looking_for_baseclass; |
bd5635a1 RP |
975 | { |
976 | int i; | |
977 | ||
978 | check_stub_type (type); | |
979 | ||
d3bab255 JK |
980 | if (! looking_for_baseclass) |
981 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
982 | { | |
983 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
984 | ||
985 | if (t_field_name && !strcmp (t_field_name, name)) | |
986 | { | |
987 | value v = (TYPE_FIELD_STATIC (type, i) | |
988 | ? value_static_field (type, name, i) | |
989 | : value_primitive_field (arg1, offset, i, type)); | |
990 | if (v == 0) | |
991 | error("there is no field named %s", name); | |
992 | return v; | |
993 | } | |
994 | } | |
bd5635a1 RP |
995 | |
996 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
997 | { | |
998 | value v; | |
999 | /* If we are looking for baseclasses, this is what we get when we | |
1000 | hit them. */ | |
d3bab255 JK |
1001 | int found_baseclass = (looking_for_baseclass |
1002 | && !strcmp (name, TYPE_BASECLASS_NAME (type, i))); | |
bd5635a1 RP |
1003 | |
1004 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1005 | { | |
1006 | value v2; | |
d11c44f1 JG |
1007 | baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, |
1008 | &v2, (int *)NULL); | |
bd5635a1 RP |
1009 | if (v2 == 0) |
1010 | error ("virtual baseclass botch"); | |
1011 | if (found_baseclass) | |
1012 | return v2; | |
d3bab255 JK |
1013 | v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i), |
1014 | looking_for_baseclass); | |
bd5635a1 RP |
1015 | if (v) return v; |
1016 | else continue; | |
1017 | } | |
1018 | if (found_baseclass) | |
1019 | v = value_primitive_field (arg1, offset, i, type); | |
1020 | else | |
1021 | v = search_struct_field (name, arg1, | |
1022 | offset + TYPE_BASECLASS_BITPOS (type, i) / 8, | |
d3bab255 JK |
1023 | TYPE_BASECLASS (type, i), |
1024 | looking_for_baseclass); | |
bd5635a1 RP |
1025 | if (v) return v; |
1026 | } | |
1027 | return NULL; | |
1028 | } | |
1029 | ||
1030 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
1031 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
2a5ec41d | 1032 | and search in it assuming it has (class) type TYPE. |
bd5635a1 RP |
1033 | If found, return value, else return NULL. */ |
1034 | ||
1035 | static value | |
1036 | search_struct_method (name, arg1, args, offset, static_memfuncp, type) | |
1037 | char *name; | |
1038 | register value arg1, *args; | |
1039 | int offset, *static_memfuncp; | |
1040 | register struct type *type; | |
1041 | { | |
1042 | int i; | |
1043 | ||
1044 | check_stub_type (type); | |
1045 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--) | |
1046 | { | |
1047 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
1048 | if (t_field_name && !strcmp (t_field_name, name)) | |
1049 | { | |
d3bab255 | 1050 | int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1; |
bd5635a1 RP |
1051 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); |
1052 | ||
d3bab255 JK |
1053 | if (j > 0 && args == 0) |
1054 | error ("cannot resolve overloaded method `%s'", name); | |
1055 | while (j >= 0) | |
bd5635a1 | 1056 | { |
8e9a3f3b | 1057 | if (TYPE_FN_FIELD_STUB (f, j)) |
bd5635a1 RP |
1058 | check_stub_method (type, i, j); |
1059 | if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j), | |
1060 | TYPE_FN_FIELD_ARGS (f, j), args)) | |
1061 | { | |
1062 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
e532974c | 1063 | return (value)value_virtual_fn_field (arg1, f, j, type); |
bd5635a1 RP |
1064 | if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp) |
1065 | *static_memfuncp = 1; | |
7d9884b9 | 1066 | return (value)value_fn_field (f, j); |
bd5635a1 | 1067 | } |
d3bab255 | 1068 | j--; |
bd5635a1 RP |
1069 | } |
1070 | } | |
1071 | } | |
1072 | ||
1073 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1074 | { | |
1075 | value v; | |
1076 | ||
1077 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1078 | { | |
1079 | value v2; | |
d11c44f1 JG |
1080 | baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, |
1081 | &v2, (int *)NULL); | |
bd5635a1 RP |
1082 | if (v2 == 0) |
1083 | error ("virtual baseclass botch"); | |
1084 | v = search_struct_method (name, v2, args, 0, | |
1085 | static_memfuncp, TYPE_BASECLASS (type, i)); | |
1086 | if (v) return v; | |
1087 | else continue; | |
1088 | } | |
1089 | ||
1090 | v = search_struct_method (name, arg1, args, | |
1091 | TYPE_BASECLASS_BITPOS (type, i) / 8, | |
1092 | static_memfuncp, TYPE_BASECLASS (type, i)); | |
1093 | if (v) return v; | |
1094 | } | |
1095 | return NULL; | |
1096 | } | |
1097 | ||
1098 | /* Given *ARGP, a value of type (pointer to a)* structure/union, | |
1099 | extract the component named NAME from the ultimate target structure/union | |
1100 | and return it as a value with its appropriate type. | |
1101 | ERR is used in the error message if *ARGP's type is wrong. | |
1102 | ||
1103 | C++: ARGS is a list of argument types to aid in the selection of | |
1104 | an appropriate method. Also, handle derived types. | |
1105 | ||
1106 | STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location | |
1107 | where the truthvalue of whether the function that was resolved was | |
1108 | a static member function or not is stored. | |
1109 | ||
1110 | ERR is an error message to be printed in case the field is not found. */ | |
1111 | ||
1112 | value | |
1113 | value_struct_elt (argp, args, name, static_memfuncp, err) | |
1114 | register value *argp, *args; | |
1115 | char *name; | |
1116 | int *static_memfuncp; | |
1117 | char *err; | |
1118 | { | |
1119 | register struct type *t; | |
bd5635a1 RP |
1120 | value v; |
1121 | ||
1122 | COERCE_ARRAY (*argp); | |
1123 | ||
1124 | t = VALUE_TYPE (*argp); | |
1125 | ||
1126 | /* Follow pointers until we get to a non-pointer. */ | |
1127 | ||
1128 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1129 | { | |
bd5635a1 | 1130 | *argp = value_ind (*argp); |
f2ebc25f JK |
1131 | /* Don't coerce fn pointer to fn and then back again! */ |
1132 | if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC) | |
1133 | COERCE_ARRAY (*argp); | |
bd5635a1 RP |
1134 | t = VALUE_TYPE (*argp); |
1135 | } | |
1136 | ||
1137 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1138 | error ("not implemented: member type in value_struct_elt"); | |
1139 | ||
2a5ec41d | 1140 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
1141 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1142 | error ("Attempt to extract a component of a value that is not a %s.", err); | |
1143 | ||
1144 | /* Assume it's not, unless we see that it is. */ | |
1145 | if (static_memfuncp) | |
1146 | *static_memfuncp =0; | |
1147 | ||
1148 | if (!args) | |
1149 | { | |
1150 | /* if there are no arguments ...do this... */ | |
1151 | ||
d3bab255 | 1152 | /* Try as a field first, because if we succeed, there |
bd5635a1 | 1153 | is less work to be done. */ |
d3bab255 | 1154 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1155 | if (v) |
1156 | return v; | |
1157 | ||
1158 | /* C++: If it was not found as a data field, then try to | |
1159 | return it as a pointer to a method. */ | |
1160 | ||
1161 | if (destructor_name_p (name, t)) | |
1162 | error ("Cannot get value of destructor"); | |
1163 | ||
1164 | v = search_struct_method (name, *argp, args, 0, static_memfuncp, t); | |
1165 | ||
1166 | if (v == 0) | |
1167 | { | |
1168 | if (TYPE_NFN_FIELDS (t)) | |
1169 | error ("There is no member or method named %s.", name); | |
1170 | else | |
1171 | error ("There is no member named %s.", name); | |
1172 | } | |
1173 | return v; | |
1174 | } | |
1175 | ||
1176 | if (destructor_name_p (name, t)) | |
1177 | { | |
1178 | if (!args[1]) | |
1179 | { | |
1180 | /* destructors are a special case. */ | |
7d9884b9 | 1181 | return (value)value_fn_field (TYPE_FN_FIELDLIST1 (t, 0), |
bd5635a1 RP |
1182 | TYPE_FN_FIELDLIST_LENGTH (t, 0)); |
1183 | } | |
1184 | else | |
1185 | { | |
1186 | error ("destructor should not have any argument"); | |
1187 | } | |
1188 | } | |
1189 | else | |
1190 | v = search_struct_method (name, *argp, args, 0, static_memfuncp, t); | |
1191 | ||
1192 | if (v == 0) | |
1193 | { | |
1194 | /* See if user tried to invoke data as function. If so, | |
1195 | hand it back. If it's not callable (i.e., a pointer to function), | |
1196 | gdb should give an error. */ | |
d3bab255 | 1197 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1198 | } |
1199 | ||
1200 | if (!v) | |
1201 | error ("Structure has no component named %s.", name); | |
1202 | return v; | |
1203 | } | |
1204 | ||
1205 | /* C++: return 1 is NAME is a legitimate name for the destructor | |
1206 | of type TYPE. If TYPE does not have a destructor, or | |
1207 | if NAME is inappropriate for TYPE, an error is signaled. */ | |
1208 | int | |
1209 | destructor_name_p (name, type) | |
1210 | char *name; | |
1211 | struct type *type; | |
1212 | { | |
1213 | /* destructors are a special case. */ | |
1214 | ||
1215 | if (name[0] == '~') | |
1216 | { | |
1217 | char *dname = type_name_no_tag (type); | |
bd5635a1 RP |
1218 | if (strcmp (dname, name+1)) |
1219 | error ("name of destructor must equal name of class"); | |
1220 | else | |
1221 | return 1; | |
1222 | } | |
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | /* Helper function for check_field: Given TYPE, a structure/union, | |
1227 | return 1 if the component named NAME from the ultimate | |
1228 | target structure/union is defined, otherwise, return 0. */ | |
1229 | ||
1230 | static int | |
1231 | check_field_in (type, name) | |
1232 | register struct type *type; | |
1233 | char *name; | |
1234 | { | |
1235 | register int i; | |
1236 | ||
1237 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1238 | { | |
1239 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
1240 | if (t_field_name && !strcmp (t_field_name, name)) | |
1241 | return 1; | |
1242 | } | |
1243 | ||
1244 | /* C++: If it was not found as a data field, then try to | |
1245 | return it as a pointer to a method. */ | |
1246 | ||
1247 | /* Destructors are a special case. */ | |
1248 | if (destructor_name_p (name, type)) | |
1249 | return 1; | |
1250 | ||
1251 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
1252 | { | |
1253 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name)) | |
1254 | return 1; | |
1255 | } | |
1256 | ||
1257 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1258 | if (check_field_in (TYPE_BASECLASS (type, i), name)) | |
1259 | return 1; | |
1260 | ||
1261 | return 0; | |
1262 | } | |
1263 | ||
1264 | ||
1265 | /* C++: Given ARG1, a value of type (pointer to a)* structure/union, | |
1266 | return 1 if the component named NAME from the ultimate | |
1267 | target structure/union is defined, otherwise, return 0. */ | |
1268 | ||
1269 | int | |
1270 | check_field (arg1, name) | |
1271 | register value arg1; | |
1272 | char *name; | |
1273 | { | |
1274 | register struct type *t; | |
1275 | ||
1276 | COERCE_ARRAY (arg1); | |
1277 | ||
1278 | t = VALUE_TYPE (arg1); | |
1279 | ||
1280 | /* Follow pointers until we get to a non-pointer. */ | |
1281 | ||
1282 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1283 | t = TYPE_TARGET_TYPE (t); | |
1284 | ||
1285 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1286 | error ("not implemented: member type in check_field"); | |
1287 | ||
2a5ec41d | 1288 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
1289 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1290 | error ("Internal error: `this' is not an aggregate"); | |
1291 | ||
1292 | return check_field_in (t, name); | |
1293 | } | |
1294 | ||
1295 | /* C++: Given an aggregate type DOMAIN, and a member name NAME, | |
2a5ec41d | 1296 | return the address of this member as a "pointer to member" |
bd5635a1 RP |
1297 | type. If INTYPE is non-null, then it will be the type |
1298 | of the member we are looking for. This will help us resolve | |
2a5ec41d JG |
1299 | "pointers to member functions". This function is only used |
1300 | to resolve user expressions of the form "&class::member". */ | |
bd5635a1 RP |
1301 | |
1302 | value | |
1303 | value_struct_elt_for_address (domain, intype, name) | |
1304 | struct type *domain, *intype; | |
1305 | char *name; | |
1306 | { | |
1307 | register struct type *t = domain; | |
1308 | register int i; | |
1309 | value v; | |
1310 | ||
1311 | struct type *baseclass; | |
1312 | ||
2a5ec41d | 1313 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
1314 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1315 | error ("Internal error: non-aggregate type to value_struct_elt_for_address"); | |
1316 | ||
1317 | baseclass = t; | |
1318 | ||
1319 | while (t) | |
1320 | { | |
1321 | for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--) | |
1322 | { | |
1323 | char *t_field_name = TYPE_FIELD_NAME (t, i); | |
2a5ec41d | 1324 | |
bd5635a1 RP |
1325 | if (t_field_name && !strcmp (t_field_name, name)) |
1326 | { | |
1327 | if (TYPE_FIELD_STATIC (t, i)) | |
1328 | { | |
1329 | char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i); | |
1330 | struct symbol *sym = | |
1331 | lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL); | |
2a5ec41d JG |
1332 | if (! sym) |
1333 | error ( | |
1334 | "Internal error: could not find physical static variable named %s", | |
1335 | phys_name); | |
06b6c733 JG |
1336 | return value_from_longest ( |
1337 | lookup_pointer_type (TYPE_FIELD_TYPE (t, i)), | |
1338 | (LONGEST)SYMBOL_BLOCK_VALUE (sym)); | |
bd5635a1 RP |
1339 | } |
1340 | if (TYPE_FIELD_PACKED (t, i)) | |
1341 | error ("pointers to bitfield members not allowed"); | |
1342 | ||
06b6c733 JG |
1343 | return value_from_longest ( |
1344 | lookup_pointer_type ( | |
1345 | lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass)), | |
bd5635a1 | 1346 | (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3)); |
bd5635a1 RP |
1347 | } |
1348 | } | |
1349 | ||
1350 | if (TYPE_N_BASECLASSES (t) == 0) | |
1351 | break; | |
1352 | ||
1353 | t = TYPE_BASECLASS (t, 0); | |
1354 | } | |
1355 | ||
1356 | /* C++: If it was not found as a data field, then try to | |
1357 | return it as a pointer to a method. */ | |
1358 | t = baseclass; | |
1359 | ||
1360 | /* Destructors are a special case. */ | |
1361 | if (destructor_name_p (name, t)) | |
1362 | { | |
2a5ec41d | 1363 | error ("member pointers to destructors not implemented yet"); |
bd5635a1 RP |
1364 | } |
1365 | ||
1366 | /* Perform all necessary dereferencing. */ | |
1367 | while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR) | |
1368 | intype = TYPE_TARGET_TYPE (intype); | |
1369 | ||
1370 | while (t) | |
1371 | { | |
1372 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) | |
1373 | { | |
1374 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) | |
1375 | { | |
1376 | int j = TYPE_FN_FIELDLIST_LENGTH (t, i); | |
1377 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
1378 | ||
1379 | if (intype == 0 && j > 1) | |
1380 | error ("non-unique member `%s' requires type instantiation", name); | |
1381 | if (intype) | |
1382 | { | |
1383 | while (j--) | |
1384 | if (TYPE_FN_FIELD_TYPE (f, j) == intype) | |
1385 | break; | |
1386 | if (j < 0) | |
1387 | error ("no member function matches that type instantiation"); | |
1388 | } | |
1389 | else | |
1390 | j = 0; | |
1391 | ||
8e9a3f3b | 1392 | if (TYPE_FN_FIELD_STUB (f, j)) |
2a5ec41d | 1393 | check_stub_method (t, i, j); |
bd5635a1 RP |
1394 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) |
1395 | { | |
06b6c733 JG |
1396 | return value_from_longest ( |
1397 | lookup_pointer_type ( | |
1398 | lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
1399 | baseclass)), | |
bd5635a1 RP |
1400 | (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j)); |
1401 | } | |
1402 | else | |
1403 | { | |
1404 | struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), | |
1405 | 0, VAR_NAMESPACE, 0, NULL); | |
1406 | v = locate_var_value (s, 0); | |
2a5ec41d JG |
1407 | VALUE_TYPE (v) = lookup_pointer_type ( |
1408 | lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
1409 | baseclass)); | |
06b6c733 | 1410 | return v; |
bd5635a1 | 1411 | } |
bd5635a1 RP |
1412 | } |
1413 | } | |
1414 | ||
1415 | if (TYPE_N_BASECLASSES (t) == 0) | |
1416 | break; | |
1417 | ||
1418 | t = TYPE_BASECLASS (t, 0); | |
1419 | } | |
1420 | return 0; | |
1421 | } | |
1422 | ||
1423 | /* Compare two argument lists and return the position in which they differ, | |
1424 | or zero if equal. | |
1425 | ||
1426 | STATICP is nonzero if the T1 argument list came from a | |
1427 | static member function. | |
1428 | ||
1429 | For non-static member functions, we ignore the first argument, | |
1430 | which is the type of the instance variable. This is because we want | |
1431 | to handle calls with objects from derived classes. This is not | |
1432 | entirely correct: we should actually check to make sure that a | |
1433 | requested operation is type secure, shouldn't we? FIXME. */ | |
1434 | ||
1435 | int | |
1436 | typecmp (staticp, t1, t2) | |
1437 | int staticp; | |
1438 | struct type *t1[]; | |
1439 | value t2[]; | |
1440 | { | |
1441 | int i; | |
1442 | ||
d3bab255 JK |
1443 | if (t2 == 0) |
1444 | return 1; | |
bd5635a1 RP |
1445 | if (staticp && t1 == 0) |
1446 | return t2[1] != 0; | |
1447 | if (t1 == 0) | |
1448 | return 1; | |
1449 | if (t1[0]->code == TYPE_CODE_VOID) return 0; | |
1450 | if (t1[!staticp] == 0) return 0; | |
1451 | for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++) | |
1452 | { | |
1453 | if (! t2[i] | |
1454 | || t1[i]->code != t2[i]->type->code | |
1455 | /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */ | |
1456 | ) | |
1457 | return i+1; | |
1458 | } | |
1459 | if (!t1[i]) return 0; | |
1460 | return t2[i] ? i+1 : 0; | |
1461 | } | |
1462 | ||
1463 | /* C++: return the value of the class instance variable, if one exists. | |
1464 | Flag COMPLAIN signals an error if the request is made in an | |
1465 | inappropriate context. */ | |
1466 | value | |
1467 | value_of_this (complain) | |
1468 | int complain; | |
1469 | { | |
1470 | extern FRAME selected_frame; | |
1471 | struct symbol *func, *sym; | |
1472 | struct block *b; | |
1473 | int i; | |
1474 | static const char funny_this[] = "this"; | |
1475 | value this; | |
bd5635a1 RP |
1476 | |
1477 | if (selected_frame == 0) | |
1478 | if (complain) | |
1479 | error ("no frame selected"); | |
1480 | else return 0; | |
1481 | ||
1482 | func = get_frame_function (selected_frame); | |
1483 | if (!func) | |
1484 | { | |
1485 | if (complain) | |
1486 | error ("no `this' in nameless context"); | |
1487 | else return 0; | |
1488 | } | |
1489 | ||
1490 | b = SYMBOL_BLOCK_VALUE (func); | |
1491 | i = BLOCK_NSYMS (b); | |
1492 | if (i <= 0) | |
1493 | if (complain) | |
1494 | error ("no args, no `this'"); | |
1495 | else return 0; | |
1496 | ||
1497 | /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER | |
1498 | symbol instead of the LOC_ARG one (if both exist). */ | |
1499 | sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE); | |
1500 | if (sym == NULL) | |
1501 | { | |
1502 | if (complain) | |
1503 | error ("current stack frame not in method"); | |
1504 | else | |
1505 | return NULL; | |
1506 | } | |
1507 | ||
1508 | this = read_var_value (sym, selected_frame); | |
1509 | if (this == 0 && complain) | |
1510 | error ("`this' argument at unknown address"); | |
1511 | return this; | |
1512 | } |