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