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