* tuples.ch (setnmode); New module.
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
bd5635a1 1/* Perform non-arithmetic operations on values, for GDB.
67e9b3b3
PS
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
06b6c733 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
06b6c733
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
06b6c733 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
06b6c733
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 20
bd5635a1 21#include "defs.h"
bd5635a1 22#include "symtab.h"
01be6913 23#include "gdbtypes.h"
bd5635a1
RP
24#include "value.h"
25#include "frame.h"
26#include "inferior.h"
27#include "gdbcore.h"
28#include "target.h"
2e4964ad 29#include "demangle.h"
54023465 30#include "language.h"
bd5635a1
RP
31
32#include <errno.h>
6d34c236 33#include <string.h>
bd5635a1
RP
34
35/* Local functions. */
01be6913 36
a91a6192 37static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[]));
01be6913 38
a91a6192 39static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **));
01be6913 40
a91a6192 41static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr));
01be6913 42
a91a6192
SS
43static value_ptr search_struct_field PARAMS ((char *, value_ptr, int,
44 struct type *, int));
01be6913 45
a91a6192
SS
46static value_ptr search_struct_method PARAMS ((char *, value_ptr *,
47 value_ptr *,
48 int, int *, struct type *));
01be6913 49
a91a6192 50static int check_field_in PARAMS ((struct type *, const char *));
a163ddec 51
a91a6192 52static CORE_ADDR allocate_space_in_inferior PARAMS ((int));
9ed8604f 53
5222ca60 54static value_ptr cast_into_complex PARAMS ((struct type *, value_ptr));
9ed8604f
PS
55
56#define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
57
bd5635a1 58\f
a163ddec
MT
59/* Allocate NBYTES of space in the inferior using the inferior's malloc
60 and return a value that is a pointer to the allocated space. */
61
62static CORE_ADDR
63allocate_space_in_inferior (len)
64 int len;
65{
a91a6192 66 register value_ptr val;
a163ddec
MT
67 register struct symbol *sym;
68 struct minimal_symbol *msymbol;
69 struct type *type;
a91a6192 70 value_ptr blocklen;
a163ddec
MT
71 LONGEST maddr;
72
73 /* Find the address of malloc in the inferior. */
74
75 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
76 if (sym != NULL)
77 {
78 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
79 {
80 error ("\"malloc\" exists in this program but is not a function.");
81 }
479fdd26 82 val = value_of_variable (sym, NULL);
a163ddec
MT
83 }
84 else
85 {
5222ca60 86 msymbol = lookup_minimal_symbol ("malloc", NULL, NULL);
a163ddec
MT
87 if (msymbol != NULL)
88 {
89 type = lookup_pointer_type (builtin_type_char);
90 type = lookup_function_type (type);
91 type = lookup_pointer_type (type);
92 maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
93 val = value_from_longest (type, maddr);
94 }
95 else
96 {
97 error ("evaluation of this expression requires the program to have a function \"malloc\".");
98 }
99 }
100
101 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
102 val = call_function_by_hand (val, 1, &blocklen);
103 if (value_logical_not (val))
104 {
105 error ("No memory available to program.");
106 }
107 return (value_as_long (val));
108}
109
bd5635a1
RP
110/* Cast value ARG2 to type TYPE and return as a value.
111 More general than a C cast: accepts any two types of the same length,
112 and if ARG2 is an lvalue it can be cast into anything at all. */
54023465 113/* In C++, casts may change pointer or object representations. */
bd5635a1 114
a91a6192 115value_ptr
bd5635a1
RP
116value_cast (type, arg2)
117 struct type *type;
a91a6192 118 register value_ptr arg2;
bd5635a1 119{
f7a69ed7 120 register enum type_code code1 = TYPE_CODE (type);
bd5635a1
RP
121 register enum type_code code2;
122 register int scalar;
123
f91a9e05
PB
124 if (VALUE_TYPE (arg2) == type)
125 return arg2;
126
f7a69ed7 127 COERCE_REF(arg2);
13ffa6be
JL
128
129 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
130 is treated like a cast to (TYPE [N])OBJECT,
131 where N is sizeof(OBJECT)/sizeof(TYPE). */
132 if (code1 == TYPE_CODE_ARRAY
133 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
134 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
135 {
136 struct type *element_type = TYPE_TARGET_TYPE (type);
137 struct type *range_type = TYPE_INDEX_TYPE (type);
138 int low_bound = TYPE_LOW_BOUND (range_type);
139 int val_length = TYPE_LENGTH (VALUE_TYPE (arg2));
140 int new_length = val_length / TYPE_LENGTH (element_type);
141 if (val_length % TYPE_LENGTH (element_type) != 0)
142 warning("array element type size does not divide object size in cast");
143 range_type = create_range_type ((struct type *) NULL,
144 TYPE_TARGET_TYPE (range_type),
145 low_bound, new_length + low_bound - 1);
146 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
147 element_type, range_type);
148 return arg2;
149 }
9ed8604f 150
f7a69ed7
PB
151 if (current_language->c_style_arrays
152 && (VALUE_REPEATED (arg2)
153 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_ARRAY))
154 arg2 = value_coerce_array (arg2); \
155
156 if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FUNC) \
157 arg2 = value_coerce_function (arg2);
158
159 COERCE_VARYING_ARRAY (arg2);
160
161 code2 = TYPE_CODE (VALUE_TYPE (arg2));
162
163 if (code1 == TYPE_CODE_COMPLEX)
164 return cast_into_complex (type, arg2);
165 if (code1 == TYPE_CODE_BOOL)
166 code1 = TYPE_CODE_INT;
167 if (code2 == TYPE_CODE_BOOL)
168 code2 = TYPE_CODE_INT;
169
bd5635a1 170 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
f91a9e05 171 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
bd5635a1 172
54023465
JK
173 if ( code1 == TYPE_CODE_STRUCT
174 && code2 == TYPE_CODE_STRUCT
175 && TYPE_NAME (type) != 0)
176 {
177 /* Look in the type of the source to see if it contains the
178 type of the target as a superclass. If so, we'll need to
179 offset the object in addition to changing its type. */
a91a6192
SS
180 value_ptr v = search_struct_field (type_name_no_tag (type),
181 arg2, 0, VALUE_TYPE (arg2), 1);
54023465
JK
182 if (v)
183 {
184 VALUE_TYPE (v) = type;
185 return v;
186 }
187 }
bd5635a1
RP
188 if (code1 == TYPE_CODE_FLT && scalar)
189 return value_from_double (type, value_as_double (arg2));
f91a9e05
PB
190 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
191 || code1 == TYPE_CODE_RANGE)
bd5635a1 192 && (scalar || code2 == TYPE_CODE_PTR))
06b6c733 193 return value_from_longest (type, value_as_long (arg2));
bd5635a1
RP
194 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
195 {
196 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
197 {
198 /* Look in the type of the source to see if it contains the
199 type of the target as a superclass. If so, we'll need to
200 offset the pointer rather than just change its type. */
201 struct type *t1 = TYPE_TARGET_TYPE (type);
202 struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
2a5ec41d 203 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
bd5635a1
RP
204 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
205 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
206 {
a91a6192
SS
207 value_ptr v = search_struct_field (type_name_no_tag (t1),
208 value_ind (arg2), 0, t2, 1);
bd5635a1
RP
209 if (v)
210 {
211 v = value_addr (v);
212 VALUE_TYPE (v) = type;
213 return v;
214 }
215 }
216 /* No superclass found, just fall through to change ptr type. */
217 }
218 VALUE_TYPE (arg2) = type;
219 return arg2;
220 }
f91a9e05
PB
221 else if (chill_varying_type (type))
222 {
223 struct type *range1, *range2, *eltype1, *eltype2;
224 value_ptr val;
225 int count1, count2;
226 char *valaddr, *valaddr_data;
227 if (code2 == TYPE_CODE_BITSTRING)
228 error ("not implemented: converting bitstring to varying type");
229 if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
230 || (eltype1 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1)),
231 eltype2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2)),
232 (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
233 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
234 error ("Invalid conversion to varying type");
235 range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
236 range2 = TYPE_FIELD_TYPE (VALUE_TYPE (arg2), 0);
237 count1 = TYPE_HIGH_BOUND (range1) - TYPE_LOW_BOUND (range1) + 1;
238 count2 = TYPE_HIGH_BOUND (range2) - TYPE_LOW_BOUND (range2) + 1;
239 if (count2 > count1)
240 error ("target varying type is too small");
241 val = allocate_value (type);
242 valaddr = VALUE_CONTENTS_RAW (val);
243 valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
244 /* Set val's __var_length field to count2. */
245 store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
246 count2);
247 /* Set the __var_data field to count2 elements copied from arg2. */
248 memcpy (valaddr_data, VALUE_CONTENTS (arg2),
249 count2 * TYPE_LENGTH (eltype2));
250 /* Zero the rest of the __var_data field of val. */
251 memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
252 (count1 - count2) * TYPE_LENGTH (eltype2));
253 return val;
254 }
bd5635a1
RP
255 else if (VALUE_LVAL (arg2) == lval_memory)
256 {
257 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
258 }
d11c44f1
JG
259 else if (code1 == TYPE_CODE_VOID)
260 {
261 return value_zero (builtin_type_void, not_lval);
262 }
bd5635a1
RP
263 else
264 {
265 error ("Invalid cast.");
266 return 0;
267 }
268}
269
270/* Create a value of type TYPE that is zero, and return it. */
271
a91a6192 272value_ptr
bd5635a1
RP
273value_zero (type, lv)
274 struct type *type;
275 enum lval_type lv;
276{
a91a6192 277 register value_ptr val = allocate_value (type);
bd5635a1 278
4ed3a9ea 279 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
bd5635a1
RP
280 VALUE_LVAL (val) = lv;
281
282 return val;
283}
284
285/* Return a value with type TYPE located at ADDR.
286
287 Call value_at only if the data needs to be fetched immediately;
288 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
289 value_at_lazy instead. value_at_lazy simply records the address of
290 the data and sets the lazy-evaluation-required flag. The lazy flag
291 is tested in the VALUE_CONTENTS macro, which is used if and when
292 the contents are actually required. */
293
a91a6192 294value_ptr
bd5635a1
RP
295value_at (type, addr)
296 struct type *type;
297 CORE_ADDR addr;
298{
a91a6192
SS
299 register value_ptr val;
300
301 if (TYPE_CODE (type) == TYPE_CODE_VOID)
302 error ("Attempt to dereference a generic pointer.");
303
304 val = allocate_value (type);
bd5635a1
RP
305
306 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
307
308 VALUE_LVAL (val) = lval_memory;
309 VALUE_ADDRESS (val) = addr;
310
311 return val;
312}
313
314/* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
315
a91a6192 316value_ptr
bd5635a1
RP
317value_at_lazy (type, addr)
318 struct type *type;
319 CORE_ADDR addr;
320{
a91a6192
SS
321 register value_ptr val;
322
323 if (TYPE_CODE (type) == TYPE_CODE_VOID)
324 error ("Attempt to dereference a generic pointer.");
325
326 val = allocate_value (type);
bd5635a1
RP
327
328 VALUE_LVAL (val) = lval_memory;
329 VALUE_ADDRESS (val) = addr;
330 VALUE_LAZY (val) = 1;
331
332 return val;
333}
334
335/* Called only from the VALUE_CONTENTS macro, if the current data for
336 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
337 data from the user's process, and clears the lazy flag to indicate
338 that the data in the buffer is valid.
339
9cb602e1
JG
340 If the value is zero-length, we avoid calling read_memory, which would
341 abort. We mark the value as fetched anyway -- all 0 bytes of it.
342
bd5635a1
RP
343 This function returns a value because it is used in the VALUE_CONTENTS
344 macro as part of an expression, where a void would not work. The
345 value is ignored. */
346
347int
348value_fetch_lazy (val)
a91a6192 349 register value_ptr val;
bd5635a1
RP
350{
351 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
352
9cb602e1
JG
353 if (TYPE_LENGTH (VALUE_TYPE (val)))
354 read_memory (addr, VALUE_CONTENTS_RAW (val),
355 TYPE_LENGTH (VALUE_TYPE (val)));
bd5635a1
RP
356 VALUE_LAZY (val) = 0;
357 return 0;
358}
359
360
361/* Store the contents of FROMVAL into the location of TOVAL.
362 Return a new value with the location of TOVAL and contents of FROMVAL. */
363
a91a6192 364value_ptr
bd5635a1 365value_assign (toval, fromval)
a91a6192 366 register value_ptr toval, fromval;
bd5635a1 367{
67e9b3b3 368 register struct type *type;
a91a6192 369 register value_ptr val;
bd5635a1 370 char raw_buffer[MAX_REGISTER_RAW_SIZE];
bd5635a1
RP
371 int use_buffer = 0;
372
30974778
JK
373 if (!toval->modifiable)
374 error ("Left operand of assignment is not a modifiable lvalue.");
375
bd5635a1 376 COERCE_ARRAY (fromval);
8e9a3f3b 377 COERCE_REF (toval);
bd5635a1 378
67e9b3b3 379 type = VALUE_TYPE (toval);
bd5635a1
RP
380 if (VALUE_LVAL (toval) != lval_internalvar)
381 fromval = value_cast (type, fromval);
382
383 /* If TOVAL is a special machine register requiring conversion
384 of program values to a special raw format,
385 convert FROMVAL's contents now, with result in `raw_buffer',
386 and set USE_BUFFER to the number of bytes to write. */
387
ad09cb2b 388#ifdef REGISTER_CONVERTIBLE
bd5635a1
RP
389 if (VALUE_REGNO (toval) >= 0
390 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
391 {
392 int regno = VALUE_REGNO (toval);
ad09cb2b
PS
393 if (REGISTER_CONVERTIBLE (regno))
394 {
395 REGISTER_CONVERT_TO_RAW (VALUE_TYPE (fromval), regno,
396 VALUE_CONTENTS (fromval), raw_buffer);
397 use_buffer = REGISTER_RAW_SIZE (regno);
398 }
bd5635a1 399 }
ad09cb2b 400#endif
bd5635a1
RP
401
402 switch (VALUE_LVAL (toval))
403 {
404 case lval_internalvar:
405 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
406 break;
407
408 case lval_internalvar_component:
409 set_internalvar_component (VALUE_INTERNALVAR (toval),
410 VALUE_OFFSET (toval),
411 VALUE_BITPOS (toval),
412 VALUE_BITSIZE (toval),
413 fromval);
414 break;
415
416 case lval_memory:
417 if (VALUE_BITSIZE (toval))
418 {
4d52ec86
JK
419 char buffer[sizeof (LONGEST)];
420 /* We assume that the argument to read_memory is in units of
421 host chars. FIXME: Is that correct? */
422 int len = (VALUE_BITPOS (toval)
423 + VALUE_BITSIZE (toval)
424 + HOST_CHAR_BIT - 1)
425 / HOST_CHAR_BIT;
ad09cb2b 426
4d52ec86 427 if (len > sizeof (LONGEST))
ad09cb2b
PS
428 error ("Can't handle bitfields which don't fit in a %d bit word.",
429 sizeof (LONGEST) * HOST_CHAR_BIT);
4d52ec86 430
bd5635a1 431 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
4d52ec86
JK
432 buffer, len);
433 modify_field (buffer, value_as_long (fromval),
bd5635a1
RP
434 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
435 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
4d52ec86 436 buffer, len);
bd5635a1
RP
437 }
438 else if (use_buffer)
439 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
440 raw_buffer, use_buffer);
441 else
442 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
443 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
444 break;
445
446 case lval_register:
447 if (VALUE_BITSIZE (toval))
448 {
ad09cb2b 449 char buffer[sizeof (LONGEST)];
4d52ec86 450 int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
ad09cb2b
PS
451
452 if (len > sizeof (LONGEST))
453 error ("Can't handle bitfields in registers larger than %d bits.",
454 sizeof (LONGEST) * HOST_CHAR_BIT);
455
456 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
457 > len * HOST_CHAR_BIT)
458 /* Getting this right would involve being very careful about
459 byte order. */
460 error ("\
461Can't handle bitfield which doesn't fit in a single register.");
462
4d52ec86
JK
463 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
464 buffer, len);
465 modify_field (buffer, value_as_long (fromval),
466 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
467 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
468 buffer, len);
bd5635a1
RP
469 }
470 else if (use_buffer)
471 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
472 raw_buffer, use_buffer);
473 else
54023465
JK
474 {
475 /* Do any conversion necessary when storing this type to more
476 than one register. */
477#ifdef REGISTER_CONVERT_FROM_TYPE
478 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
479 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
480 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
481 raw_buffer, TYPE_LENGTH (type));
482#else
483 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
484 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
485#endif
486 }
79971d11
JK
487 /* Assigning to the stack pointer, frame pointer, and other
488 (architecture and calling convention specific) registers may
489 cause the frame cache to be out of date. We just do this
490 on all assignments to registers for simplicity; I doubt the slowdown
491 matters. */
492 reinit_frame_cache ();
bd5635a1
RP
493 break;
494
495 case lval_reg_frame_relative:
496 {
497 /* value is stored in a series of registers in the frame
498 specified by the structure. Copy that value out, modify
499 it, and copy it back in. */
500 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
501 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
502 int byte_offset = VALUE_OFFSET (toval) % reg_size;
503 int reg_offset = VALUE_OFFSET (toval) / reg_size;
504 int amount_copied;
4d52ec86
JK
505
506 /* Make the buffer large enough in all cases. */
507 char *buffer = (char *) alloca (amount_to_copy
508 + sizeof (LONGEST)
509 + MAX_REGISTER_RAW_SIZE);
510
bd5635a1 511 int regno;
6d34c236 512 struct frame_info *frame;
bd5635a1
RP
513
514 /* Figure out which frame this is in currently. */
515 for (frame = get_current_frame ();
516 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
517 frame = get_prev_frame (frame))
518 ;
519
520 if (!frame)
521 error ("Value being assigned to is no longer active.");
522
523 amount_to_copy += (reg_size - amount_to_copy % reg_size);
524
525 /* Copy it out. */
526 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
527 amount_copied = 0);
528 amount_copied < amount_to_copy;
529 amount_copied += reg_size, regno++)
530 {
531 get_saved_register (buffer + amount_copied,
51b57ded 532 (int *)NULL, (CORE_ADDR *)NULL,
bd5635a1
RP
533 frame, regno, (enum lval_type *)NULL);
534 }
535
536 /* Modify what needs to be modified. */
537 if (VALUE_BITSIZE (toval))
538 modify_field (buffer + byte_offset,
479fdd26 539 value_as_long (fromval),
bd5635a1
RP
540 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
541 else if (use_buffer)
4ed3a9ea 542 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
bd5635a1 543 else
4ed3a9ea
FF
544 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
545 TYPE_LENGTH (type));
bd5635a1
RP
546
547 /* Copy it back. */
548 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
549 amount_copied = 0);
550 amount_copied < amount_to_copy;
551 amount_copied += reg_size, regno++)
552 {
553 enum lval_type lval;
554 CORE_ADDR addr;
555 int optim;
556
557 /* Just find out where to put it. */
558 get_saved_register ((char *)NULL,
559 &optim, &addr, frame, regno, &lval);
560
561 if (optim)
562 error ("Attempt to assign to a value that was optimized out.");
563 if (lval == lval_memory)
564 write_memory (addr, buffer + amount_copied, reg_size);
565 else if (lval == lval_register)
566 write_register_bytes (addr, buffer + amount_copied, reg_size);
567 else
568 error ("Attempt to assign to an unmodifiable value.");
569 }
570 }
571 break;
572
573
574 default:
30974778 575 error ("Left operand of assignment is not an lvalue.");
bd5635a1
RP
576 }
577
578 /* Return a value just like TOVAL except with the contents of FROMVAL
579 (except in the case of the type if TOVAL is an internalvar). */
580
581 if (VALUE_LVAL (toval) == lval_internalvar
582 || VALUE_LVAL (toval) == lval_internalvar_component)
583 {
584 type = VALUE_TYPE (fromval);
585 }
586
587 val = allocate_value (type);
4ed3a9ea
FF
588 memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val);
589 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
590 TYPE_LENGTH (type));
bd5635a1
RP
591 VALUE_TYPE (val) = type;
592
593 return val;
594}
595
596/* Extend a value VAL to COUNT repetitions of its type. */
597
a91a6192 598value_ptr
bd5635a1 599value_repeat (arg1, count)
a91a6192 600 value_ptr arg1;
bd5635a1
RP
601 int count;
602{
a91a6192 603 register value_ptr val;
bd5635a1
RP
604
605 if (VALUE_LVAL (arg1) != lval_memory)
606 error ("Only values in memory can be extended with '@'.");
607 if (count < 1)
608 error ("Invalid number %d of repetitions.", count);
609
610 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
611
612 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
613 VALUE_CONTENTS_RAW (val),
614 TYPE_LENGTH (VALUE_TYPE (val)) * count);
615 VALUE_LVAL (val) = lval_memory;
616 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
617
618 return val;
619}
620
a91a6192 621value_ptr
479fdd26 622value_of_variable (var, b)
bd5635a1 623 struct symbol *var;
479fdd26 624 struct block *b;
bd5635a1 625{
a91a6192 626 value_ptr val;
6d34c236 627 struct frame_info *frame;
bd5635a1 628
479fdd26
JK
629 if (b == NULL)
630 /* Use selected frame. */
6d34c236 631 frame = NULL;
479fdd26
JK
632 else
633 {
6d34c236
PB
634 frame = block_innermost_frame (b);
635 if (frame == NULL && symbol_read_needs_frame (var))
479fdd26
JK
636 {
637 if (BLOCK_FUNCTION (b) != NULL
638 && SYMBOL_NAME (BLOCK_FUNCTION (b)) != NULL)
639 error ("No frame is currently executing in block %s.",
640 SYMBOL_NAME (BLOCK_FUNCTION (b)));
641 else
642 error ("No frame is currently executing in specified block");
643 }
644 }
6d34c236 645 val = read_var_value (var, frame);
bd5635a1 646 if (val == 0)
2e4964ad 647 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
bd5635a1
RP
648 return val;
649}
650
a163ddec
MT
651/* Given a value which is an array, return a value which is a pointer to its
652 first element, regardless of whether or not the array has a nonzero lower
653 bound.
654
655 FIXME: A previous comment here indicated that this routine should be
656 substracting the array's lower bound. It's not clear to me that this
657 is correct. Given an array subscripting operation, it would certainly
658 work to do the adjustment here, essentially computing:
659
660 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
661
662 However I believe a more appropriate and logical place to account for
663 the lower bound is to do so in value_subscript, essentially computing:
664
665 (&array[0] + ((index - lowerbound) * sizeof array[0]))
666
667 As further evidence consider what would happen with operations other
668 than array subscripting, where the caller would get back a value that
669 had an address somewhere before the actual first element of the array,
670 and the information about the lower bound would be lost because of
671 the coercion to pointer type.
672 */
bd5635a1 673
a91a6192 674value_ptr
bd5635a1 675value_coerce_array (arg1)
a91a6192 676 value_ptr arg1;
bd5635a1
RP
677{
678 register struct type *type;
bd5635a1
RP
679
680 if (VALUE_LVAL (arg1) != lval_memory)
681 error ("Attempt to take address of value not located in memory.");
682
683 /* Get type of elements. */
852b3831
PB
684 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
685 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRING)
bd5635a1
RP
686 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
687 else
688 /* A phony array made by value_repeat.
689 Its type is the type of the elements, not an array type. */
690 type = VALUE_TYPE (arg1);
691
06b6c733 692 return value_from_longest (lookup_pointer_type (type),
bd5635a1 693 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
bd5635a1
RP
694}
695
696/* Given a value which is a function, return a value which is a pointer
697 to it. */
698
a91a6192 699value_ptr
bd5635a1 700value_coerce_function (arg1)
a91a6192 701 value_ptr arg1;
bd5635a1 702{
bd5635a1
RP
703
704 if (VALUE_LVAL (arg1) != lval_memory)
705 error ("Attempt to take address of value not located in memory.");
706
06b6c733 707 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
bd5635a1 708 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
bd5635a1
RP
709}
710
711/* Return a pointer value for the object for which ARG1 is the contents. */
712
a91a6192 713value_ptr
bd5635a1 714value_addr (arg1)
a91a6192 715 value_ptr arg1;
bd5635a1 716{
8e9a3f3b
PB
717 struct type *type = VALUE_TYPE (arg1);
718 if (TYPE_CODE (type) == TYPE_CODE_REF)
719 {
720 /* Copy the value, but change the type from (T&) to (T*).
721 We keep the same location information, which is efficient,
722 and allows &(&X) to get the location containing the reference. */
a91a6192 723 value_ptr arg2 = value_copy (arg1);
8e9a3f3b
PB
724 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
725 return arg2;
726 }
f91a9e05
PB
727 if (current_language->c_style_arrays
728 && (VALUE_REPEATED (arg1)
729 || TYPE_CODE (type) == TYPE_CODE_ARRAY))
bd5635a1 730 return value_coerce_array (arg1);
8e9a3f3b 731 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
bd5635a1
RP
732 return value_coerce_function (arg1);
733
734 if (VALUE_LVAL (arg1) != lval_memory)
735 error ("Attempt to take address of value not located in memory.");
736
8e9a3f3b 737 return value_from_longest (lookup_pointer_type (type),
bd5635a1 738 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
bd5635a1
RP
739}
740
741/* Given a value of a pointer type, apply the C unary * operator to it. */
742
a91a6192 743value_ptr
bd5635a1 744value_ind (arg1)
a91a6192 745 value_ptr arg1;
bd5635a1
RP
746{
747 COERCE_ARRAY (arg1);
748
749 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
750 error ("not implemented: member types in value_ind");
751
752 /* Allow * on an integer so we can cast it to whatever we want.
753 This returns an int, which seems like the most C-like thing
754 to do. "long long" variables are rare enough that
755 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
756 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
757 return value_at (builtin_type_int,
758 (CORE_ADDR) value_as_long (arg1));
759 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
760 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
d11c44f1 761 value_as_pointer (arg1));
bd5635a1
RP
762 error ("Attempt to take contents of a non-pointer value.");
763 return 0; /* For lint -- never reached */
764}
765\f
766/* Pushing small parts of stack frames. */
767
768/* Push one word (the size of object that a register holds). */
769
770CORE_ADDR
34df79fc 771push_word (sp, word)
bd5635a1 772 CORE_ADDR sp;
67e9b3b3 773 unsigned LONGEST word;
bd5635a1 774{
67e9b3b3 775 register int len = REGISTER_SIZE;
479fdd26 776 char buffer[MAX_REGISTER_RAW_SIZE];
bd5635a1 777
479fdd26 778 store_unsigned_integer (buffer, len, word);
bd5635a1
RP
779#if 1 INNER_THAN 2
780 sp -= len;
479fdd26 781 write_memory (sp, buffer, len);
bd5635a1 782#else /* stack grows upward */
479fdd26 783 write_memory (sp, buffer, len);
bd5635a1
RP
784 sp += len;
785#endif /* stack grows upward */
786
787 return sp;
788}
789
790/* Push LEN bytes with data at BUFFER. */
791
792CORE_ADDR
793push_bytes (sp, buffer, len)
794 CORE_ADDR sp;
795 char *buffer;
796 int len;
797{
798#if 1 INNER_THAN 2
799 sp -= len;
800 write_memory (sp, buffer, len);
801#else /* stack grows upward */
802 write_memory (sp, buffer, len);
803 sp += len;
804#endif /* stack grows upward */
805
806 return sp;
807}
808
809/* Push onto the stack the specified value VALUE. */
810
01be6913 811static CORE_ADDR
bd5635a1
RP
812value_push (sp, arg)
813 register CORE_ADDR sp;
a91a6192 814 value_ptr arg;
bd5635a1
RP
815{
816 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
817
818#if 1 INNER_THAN 2
819 sp -= len;
820 write_memory (sp, VALUE_CONTENTS (arg), len);
821#else /* stack grows upward */
822 write_memory (sp, VALUE_CONTENTS (arg), len);
823 sp += len;
824#endif /* stack grows upward */
825
826 return sp;
827}
828
829/* Perform the standard coercions that are specified
5222ca60 830 for arguments to be passed to C functions.
bd5635a1 831
5222ca60
PB
832 If PARAM_TYPE is non-NULL, it is the expected parameter type. */
833
834static value_ptr
835value_arg_coerce (arg, param_type)
a91a6192 836 value_ptr arg;
5222ca60 837 struct type *param_type;
bd5635a1 838{
5222ca60 839 register struct type *type = param_type ? param_type : VALUE_TYPE (arg);
bd5635a1 840
5222ca60
PB
841 switch (TYPE_CODE (type))
842 {
843 case TYPE_CODE_REF:
13ffa6be 844 if (TYPE_CODE (VALUE_TYPE (arg)) != TYPE_CODE_REF)
5222ca60
PB
845 {
846 arg = value_addr (arg);
847 VALUE_TYPE (arg) = param_type;
848 return arg;
849 }
850 break;
851 case TYPE_CODE_INT:
852 case TYPE_CODE_CHAR:
853 case TYPE_CODE_BOOL:
854 case TYPE_CODE_ENUM:
855 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
856 type = builtin_type_int;
857 break;
858 case TYPE_CODE_FLT:
859 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
860 type = builtin_type_double;
861 break;
862 case TYPE_CODE_FUNC:
863 type = lookup_pointer_type (type);
864 break;
865 }
479fdd26 866
b5728692 867#if 1 /* FIXME: This is only a temporary patch. -fnf */
f91a9e05
PB
868 if (current_language->c_style_arrays
869 && (VALUE_REPEATED (arg)
870 || TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ARRAY))
b5728692 871 arg = value_coerce_array (arg);
b5728692 872#endif
bd5635a1 873
5222ca60 874 return value_cast (type, arg);
bd5635a1
RP
875}
876
877/* Determine a function's address and its return type from its value.
878 Calls error() if the function is not valid for calling. */
879
01be6913 880static CORE_ADDR
bd5635a1 881find_function_addr (function, retval_type)
a91a6192 882 value_ptr function;
bd5635a1
RP
883 struct type **retval_type;
884{
885 register struct type *ftype = VALUE_TYPE (function);
886 register enum type_code code = TYPE_CODE (ftype);
887 struct type *value_type;
888 CORE_ADDR funaddr;
889
890 /* If it's a member function, just look at the function
891 part of it. */
892
893 /* Determine address to call. */
894 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
895 {
896 funaddr = VALUE_ADDRESS (function);
897 value_type = TYPE_TARGET_TYPE (ftype);
898 }
899 else if (code == TYPE_CODE_PTR)
900 {
d11c44f1 901 funaddr = value_as_pointer (function);
bd5635a1
RP
902 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
903 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
9ed8604f
PS
904 {
905#ifdef CONVERT_FROM_FUNC_PTR_ADDR
906 /* FIXME: This is a workaround for the unusual function
907 pointer representation on the RS/6000, see comment
908 in config/rs6000/tm-rs6000.h */
909 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
910#endif
911 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
912 }
bd5635a1
RP
913 else
914 value_type = builtin_type_int;
915 }
916 else if (code == TYPE_CODE_INT)
917 {
918 /* Handle the case of functions lacking debugging info.
919 Their values are characters since their addresses are char */
920 if (TYPE_LENGTH (ftype) == 1)
d11c44f1 921 funaddr = value_as_pointer (value_addr (function));
bd5635a1
RP
922 else
923 /* Handle integer used as address of a function. */
d11c44f1 924 funaddr = (CORE_ADDR) value_as_long (function);
bd5635a1
RP
925
926 value_type = builtin_type_int;
927 }
928 else
929 error ("Invalid data type for function to be called.");
930
931 *retval_type = value_type;
932 return funaddr;
933}
934
935#if defined (CALL_DUMMY)
936/* All this stuff with a dummy frame may seem unnecessarily complicated
937 (why not just save registers in GDB?). The purpose of pushing a dummy
938 frame which looks just like a real frame is so that if you call a
939 function and then hit a breakpoint (get a signal, etc), "backtrace"
940 will look right. Whether the backtrace needs to actually show the
941 stack at the time the inferior function was called is debatable, but
942 it certainly needs to not display garbage. So if you are contemplating
943 making dummy frames be different from normal frames, consider that. */
944
945/* Perform a function call in the inferior.
946 ARGS is a vector of values of arguments (NARGS of them).
947 FUNCTION is a value, the function to be called.
948 Returns a value representing what the function returned.
949 May fail to return, if a breakpoint or signal is hit
5222ca60
PB
950 during the execution of the function.
951
952 ARGS is modified to contain coerced values. */
bd5635a1 953
a91a6192 954value_ptr
bd5635a1 955call_function_by_hand (function, nargs, args)
a91a6192 956 value_ptr function;
bd5635a1 957 int nargs;
a91a6192 958 value_ptr *args;
bd5635a1
RP
959{
960 register CORE_ADDR sp;
961 register int i;
962 CORE_ADDR start_sp;
67e9b3b3
PS
963 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
964 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
965 and remove any extra bytes which might exist because unsigned LONGEST is
966 bigger than REGISTER_SIZE. */
967 static unsigned LONGEST dummy[] = CALL_DUMMY;
968 char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (unsigned LONGEST)];
bd5635a1
RP
969 CORE_ADDR old_sp;
970 struct type *value_type;
971 unsigned char struct_return;
972 CORE_ADDR struct_addr;
973 struct inferior_status inf_status;
974 struct cleanup *old_chain;
975 CORE_ADDR funaddr;
976 int using_gcc;
9f739abd 977 CORE_ADDR real_pc;
5222ca60 978 struct type *ftype = SYMBOL_TYPE (function);
bd5635a1 979
e17960fb
JG
980 if (!target_has_execution)
981 noprocess();
982
bd5635a1
RP
983 save_inferior_status (&inf_status, 1);
984 old_chain = make_cleanup (restore_inferior_status, &inf_status);
985
986 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
987 (and POP_FRAME for restoring them). (At least on most machines)
988 they are saved on the stack in the inferior. */
989 PUSH_DUMMY_FRAME;
990
54023465 991 old_sp = sp = read_sp ();
bd5635a1
RP
992
993#if 1 INNER_THAN 2 /* Stack grows down */
9ed8604f 994 sp -= sizeof dummy1;
bd5635a1
RP
995 start_sp = sp;
996#else /* Stack grows up */
997 start_sp = sp;
9ed8604f 998 sp += sizeof dummy1;
bd5635a1
RP
999#endif
1000
1001 funaddr = find_function_addr (function, &value_type);
1002
1003 {
1004 struct block *b = block_for_pc (funaddr);
1005 /* If compiled without -g, assume GCC. */
f7a69ed7 1006 using_gcc = b == NULL ? 0 : BLOCK_GCC_COMPILED (b);
bd5635a1
RP
1007 }
1008
1009 /* Are we returning a value using a structure return or a normal
1010 value return? */
1011
1012 struct_return = using_struct_return (function, funaddr, value_type,
1013 using_gcc);
1014
1015 /* Create a call sequence customized for this function
1016 and the number of arguments for it. */
67e9b3b3
PS
1017 for (i = 0; i < sizeof dummy / sizeof (dummy[0]); i++)
1018 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1019 REGISTER_SIZE,
34df79fc 1020 (unsigned LONGEST)dummy[i]);
9f739abd
SG
1021
1022#ifdef GDB_TARGET_IS_HPPA
b5728692
SG
1023 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1024 value_type, using_gcc);
9f739abd 1025#else
bd5635a1
RP
1026 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1027 value_type, using_gcc);
9f739abd
SG
1028 real_pc = start_sp;
1029#endif
bd5635a1
RP
1030
1031#if CALL_DUMMY_LOCATION == ON_STACK
9ed8604f 1032 write_memory (start_sp, (char *)dummy1, sizeof dummy1);
cef4c2e7 1033#endif /* On stack. */
bd5635a1 1034
bd5635a1
RP
1035#if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1036 /* Convex Unix prohibits executing in the stack segment. */
1037 /* Hope there is empty room at the top of the text segment. */
1038 {
84d82b1c 1039 extern CORE_ADDR text_end;
bd5635a1
RP
1040 static checked = 0;
1041 if (!checked)
9ed8604f 1042 for (start_sp = text_end - sizeof dummy1; start_sp < text_end; ++start_sp)
bd5635a1
RP
1043 if (read_memory_integer (start_sp, 1) != 0)
1044 error ("text segment full -- no place to put call");
1045 checked = 1;
1046 sp = old_sp;
9ed8604f
PS
1047 real_pc = text_end - sizeof dummy1;
1048 write_memory (real_pc, (char *)dummy1, sizeof dummy1);
bd5635a1 1049 }
cef4c2e7
PS
1050#endif /* Before text_end. */
1051
1052#if CALL_DUMMY_LOCATION == AFTER_TEXT_END
bd5635a1 1053 {
84d82b1c 1054 extern CORE_ADDR text_end;
bd5635a1
RP
1055 int errcode;
1056 sp = old_sp;
30d20d15 1057 real_pc = text_end;
9ed8604f 1058 errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy1);
bd5635a1
RP
1059 if (errcode != 0)
1060 error ("Cannot write text segment -- call_function failed");
1061 }
1062#endif /* After text_end. */
cef4c2e7
PS
1063
1064#if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1065 real_pc = funaddr;
1066#endif /* At entry point. */
bd5635a1
RP
1067
1068#ifdef lint
1069 sp = old_sp; /* It really is used, for some ifdef's... */
1070#endif
1071
f7a69ed7
PB
1072 if (nargs < TYPE_NFIELDS (ftype))
1073 error ("too few arguments in function call");
1074
5222ca60
PB
1075 for (i = nargs - 1; i >= 0; i--)
1076 {
1077 struct type *param_type;
1078 if (TYPE_NFIELDS (ftype) > i)
1079 param_type = TYPE_FIELD_TYPE (ftype, i);
1080 else
1081 param_type = 0;
1082 args[i] = value_arg_coerce (args[i], param_type);
1083 }
1084
bd5635a1
RP
1085#if defined (REG_STRUCT_HAS_ADDR)
1086 {
a91a6192 1087 /* This is a machine like the sparc, where we may need to pass a pointer
bd5635a1 1088 to the structure, not the structure itself. */
a91a6192 1089 for (i = nargs - 1; i >= 0; i--)
f7a69ed7
PB
1090 if ((TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT
1091 || TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_UNION
1092 || TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_ARRAY
1093 || TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRING)
a91a6192
SS
1094 && REG_STRUCT_HAS_ADDR (using_gcc, VALUE_TYPE (args[i])))
1095 {
1096 CORE_ADDR addr;
f7a69ed7
PB
1097 int len = TYPE_LENGTH (VALUE_TYPE (args[i]));
1098#ifdef STACK_ALIGN
1099 int aligned_len = STACK_ALIGN (len);
1100#else
1101 int aligned_len = len;
1102#endif
bd5635a1 1103#if !(1 INNER_THAN 2)
a91a6192
SS
1104 /* The stack grows up, so the address of the thing we push
1105 is the stack pointer before we push it. */
1106 addr = sp;
f7a69ed7
PB
1107#else
1108 sp -= aligned_len;
bd5635a1 1109#endif
a91a6192 1110 /* Push the structure. */
f7a69ed7 1111 write_memory (sp, VALUE_CONTENTS (args[i]), len);
bd5635a1 1112#if 1 INNER_THAN 2
a91a6192
SS
1113 /* The stack grows down, so the address of the thing we push
1114 is the stack pointer after we push it. */
1115 addr = sp;
f7a69ed7
PB
1116#else
1117 sp += aligned_len;
bd5635a1 1118#endif
a91a6192
SS
1119 /* The value we're going to pass is the address of the thing
1120 we just pushed. */
1121 args[i] = value_from_longest (lookup_pointer_type (value_type),
1122 (LONGEST) addr);
1123 }
bd5635a1
RP
1124 }
1125#endif /* REG_STRUCT_HAS_ADDR. */
1126
f7a69ed7
PB
1127 /* Reserve space for the return structure to be written on the
1128 stack, if necessary */
1129
1130 if (struct_return)
1131 {
1132 int len = TYPE_LENGTH (value_type);
1133#ifdef STACK_ALIGN
1134 len = STACK_ALIGN (len);
1135#endif
1136#if 1 INNER_THAN 2
1137 sp -= len;
1138 struct_addr = sp;
1139#else
1140 struct_addr = sp;
1141 sp += len;
1142#endif
1143 }
1144
1145#ifdef STACK_ALIGN
1146 /* If stack grows down, we must leave a hole at the top. */
1147 {
1148 int len = 0;
1149
1150 for (i = nargs - 1; i >= 0; i--)
1151 len += TYPE_LENGTH (VALUE_TYPE (args[i]));
1152#ifdef CALL_DUMMY_STACK_ADJUST
1153 len += CALL_DUMMY_STACK_ADJUST;
1154#endif
1155#if 1 INNER_THAN 2
1156 sp -= STACK_ALIGN (len) - len;
1157#else
1158 sp += STACK_ALIGN (len) - len;
1159#endif
1160 }
1161#endif /* STACK_ALIGN */
1162
bd5635a1
RP
1163#ifdef PUSH_ARGUMENTS
1164 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1165#else /* !PUSH_ARGUMENTS */
1166 for (i = nargs - 1; i >= 0; i--)
5222ca60 1167 sp = value_push (sp, args[i]);
bd5635a1
RP
1168#endif /* !PUSH_ARGUMENTS */
1169
1170#ifdef CALL_DUMMY_STACK_ADJUST
1171#if 1 INNER_THAN 2
1172 sp -= CALL_DUMMY_STACK_ADJUST;
1173#else
1174 sp += CALL_DUMMY_STACK_ADJUST;
1175#endif
1176#endif /* CALL_DUMMY_STACK_ADJUST */
1177
1178 /* Store the address at which the structure is supposed to be
1179 written. Note that this (and the code which reserved the space
1180 above) assumes that gcc was used to compile this function. Since
1181 it doesn't cost us anything but space and if the function is pcc
1182 it will ignore this value, we will make that assumption.
1183
1184 Also note that on some machines (like the sparc) pcc uses a
1185 convention like gcc's. */
1186
1187 if (struct_return)
1188 STORE_STRUCT_RETURN (struct_addr, sp);
1189
1190 /* Write the stack pointer. This is here because the statements above
1191 might fool with it. On SPARC, this write also stores the register
1192 window into the right place in the new stack frame, which otherwise
5632cd56 1193 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
54023465 1194 write_sp (sp);
bd5635a1 1195
bd5635a1
RP
1196 {
1197 char retbuf[REGISTER_BYTES];
54023465
JK
1198 char *name;
1199 struct symbol *symbol;
1200
1201 name = NULL;
1202 symbol = find_pc_function (funaddr);
1203 if (symbol)
1204 {
1205 name = SYMBOL_SOURCE_NAME (symbol);
1206 }
1207 else
1208 {
1209 /* Try the minimal symbols. */
1210 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1211
1212 if (msymbol)
1213 {
1214 name = SYMBOL_SOURCE_NAME (msymbol);
1215 }
1216 }
1217 if (name == NULL)
1218 {
1219 char format[80];
1220 sprintf (format, "at %s", local_hex_format ());
1221 name = alloca (80);
30974778 1222 /* FIXME-32x64: assumes funaddr fits in a long. */
cef4c2e7 1223 sprintf (name, format, (unsigned long) funaddr);
54023465 1224 }
bd5635a1
RP
1225
1226 /* Execute the stack dummy routine, calling FUNCTION.
1227 When it is done, discard the empty frame
1228 after storing the contents of all regs into retbuf. */
860a1754
JK
1229 if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1230 {
1231 /* We stopped somewhere besides the call dummy. */
1232
1233 /* If we did the cleanups, we would print a spurious error message
1234 (Unable to restore previously selected frame), would write the
1235 registers from the inf_status (which is wrong), and would do other
1236 wrong things (like set stop_bpstat to the wrong thing). */
1237 discard_cleanups (old_chain);
1238 /* Prevent memory leak. */
30d20d15 1239 bpstat_clear (&inf_status.stop_bpstat);
860a1754
JK
1240
1241 /* The following error message used to say "The expression
1242 which contained the function call has been discarded." It
1243 is a hard concept to explain in a few words. Ideally, GDB
1244 would be able to resume evaluation of the expression when
1245 the function finally is done executing. Perhaps someday
1246 this will be implemented (it would not be easy). */
1247
1248 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1249 a C++ name with arguments and stuff. */
1250 error ("\
1251The program being debugged stopped while in a function called from GDB.\n\
1252When the function (%s) is done executing, GDB will silently\n\
1253stop (instead of continuing to evaluate the expression containing\n\
1254the function call).", name);
1255 }
bd5635a1
RP
1256
1257 do_cleanups (old_chain);
1258
860a1754 1259 /* Figure out the value returned by the function. */
bd5635a1
RP
1260 return value_being_returned (value_type, retbuf, struct_return);
1261 }
1262}
1263#else /* no CALL_DUMMY. */
a91a6192 1264value_ptr
bd5635a1 1265call_function_by_hand (function, nargs, args)
a91a6192 1266 value_ptr function;
bd5635a1 1267 int nargs;
a91a6192 1268 value_ptr *args;
bd5635a1
RP
1269{
1270 error ("Cannot invoke functions on this machine.");
1271}
1272#endif /* no CALL_DUMMY. */
a163ddec 1273
bd5635a1 1274\f
a163ddec
MT
1275/* Create a value for an array by allocating space in the inferior, copying
1276 the data into that space, and then setting up an array value.
1277
1278 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1279 populated from the values passed in ELEMVEC.
1280
1281 The element type of the array is inherited from the type of the
1282 first element, and all elements must have the same size (though we
1283 don't currently enforce any restriction on their types). */
bd5635a1 1284
a91a6192 1285value_ptr
a163ddec
MT
1286value_array (lowbound, highbound, elemvec)
1287 int lowbound;
1288 int highbound;
a91a6192 1289 value_ptr *elemvec;
bd5635a1 1290{
a163ddec
MT
1291 int nelem;
1292 int idx;
1293 int typelength;
a91a6192 1294 value_ptr val;
a163ddec
MT
1295 struct type *rangetype;
1296 struct type *arraytype;
1297 CORE_ADDR addr;
bd5635a1 1298
a163ddec
MT
1299 /* Validate that the bounds are reasonable and that each of the elements
1300 have the same size. */
bd5635a1 1301
a163ddec
MT
1302 nelem = highbound - lowbound + 1;
1303 if (nelem <= 0)
bd5635a1 1304 {
a163ddec 1305 error ("bad array bounds (%d, %d)", lowbound, highbound);
bd5635a1 1306 }
a163ddec
MT
1307 typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1308 for (idx = 0; idx < nelem; idx++)
bd5635a1 1309 {
a163ddec
MT
1310 if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1311 {
1312 error ("array elements must all be the same size");
1313 }
bd5635a1
RP
1314 }
1315
a163ddec
MT
1316 /* Allocate space to store the array in the inferior, and then initialize
1317 it by copying in each element. FIXME: Is it worth it to create a
1318 local buffer in which to collect each value and then write all the
1319 bytes in one operation? */
1320
1321 addr = allocate_space_in_inferior (nelem * typelength);
1322 for (idx = 0; idx < nelem; idx++)
1323 {
1324 write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1325 typelength);
1326 }
1327
1328 /* Create the array type and set up an array value to be evaluated lazily. */
1329
1330 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1331 lowbound, highbound);
1332 arraytype = create_array_type ((struct type *) NULL,
1333 VALUE_TYPE (elemvec[0]), rangetype);
1334 val = value_at_lazy (arraytype, addr);
1335 return (val);
1336}
1337
1338/* Create a value for a string constant by allocating space in the inferior,
1339 copying the data into that space, and returning the address with type
1340 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1341 of characters.
1342 Note that string types are like array of char types with a lower bound of
1343 zero and an upper bound of LEN - 1. Also note that the string may contain
1344 embedded null bytes. */
1345
a91a6192 1346value_ptr
a163ddec
MT
1347value_string (ptr, len)
1348 char *ptr;
1349 int len;
1350{
a91a6192 1351 value_ptr val;
5222ca60 1352 int lowbound = current_language->string_lower_bound;
f91a9e05 1353 struct type *rangetype = create_range_type ((struct type *) NULL,
5222ca60
PB
1354 builtin_type_int,
1355 lowbound, len + lowbound - 1);
f91a9e05
PB
1356 struct type *stringtype
1357 = create_string_type ((struct type *) NULL, rangetype);
a163ddec
MT
1358 CORE_ADDR addr;
1359
f91a9e05
PB
1360 if (current_language->c_style_arrays == 0)
1361 {
1362 val = allocate_value (stringtype);
1363 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1364 return val;
1365 }
1366
1367
a163ddec
MT
1368 /* Allocate space to store the string in the inferior, and then
1369 copy LEN bytes from PTR in gdb to that address in the inferior. */
1370
1371 addr = allocate_space_in_inferior (len);
1372 write_memory (addr, ptr, len);
1373
a163ddec
MT
1374 val = value_at_lazy (stringtype, addr);
1375 return (val);
bd5635a1 1376}
6d34c236
PB
1377
1378value_ptr
1379value_bitstring (ptr, len)
1380 char *ptr;
1381 int len;
1382{
1383 value_ptr val;
1384 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1385 0, len - 1);
1386 struct type *type = create_set_type ((struct type*) NULL, domain_type);
1387 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1388 val = allocate_value (type);
1389 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type) / TARGET_CHAR_BIT);
1390 return val;
1391}
bd5635a1 1392\f
479fdd26
JK
1393/* See if we can pass arguments in T2 to a function which takes arguments
1394 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1395 arguments need coercion of some sort, then the coerced values are written
1396 into T2. Return value is 0 if the arguments could be matched, or the
1397 position at which they differ if not.
a163ddec
MT
1398
1399 STATICP is nonzero if the T1 argument list came from a
1400 static member function.
1401
1402 For non-static member functions, we ignore the first argument,
1403 which is the type of the instance variable. This is because we want
1404 to handle calls with objects from derived classes. This is not
1405 entirely correct: we should actually check to make sure that a
1406 requested operation is type secure, shouldn't we? FIXME. */
1407
1408static int
1409typecmp (staticp, t1, t2)
1410 int staticp;
1411 struct type *t1[];
a91a6192 1412 value_ptr t2[];
a163ddec
MT
1413{
1414 int i;
1415
1416 if (t2 == 0)
1417 return 1;
1418 if (staticp && t1 == 0)
1419 return t2[1] != 0;
1420 if (t1 == 0)
1421 return 1;
1422 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1423 if (t1[!staticp] == 0) return 0;
1424 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1425 {
40620258 1426 struct type *tt1, *tt2;
a163ddec
MT
1427 if (! t2[i])
1428 return i+1;
40620258
KH
1429 tt1 = t1[i];
1430 tt2 = VALUE_TYPE(t2[i]);
1431 if (TYPE_CODE (tt1) == TYPE_CODE_REF
479fdd26 1432 /* We should be doing hairy argument matching, as below. */
40620258 1433 && (TYPE_CODE (TYPE_TARGET_TYPE (tt1)) == TYPE_CODE (tt2)))
479fdd26
JK
1434 {
1435 t2[i] = value_addr (t2[i]);
1436 continue;
1437 }
1438
40620258
KH
1439 while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1440 && (TYPE_CODE(tt2)==TYPE_CODE_ARRAY || TYPE_CODE(tt2)==TYPE_CODE_PTR))
1441 {
1442 tt1 = TYPE_TARGET_TYPE(tt1);
1443 tt2 = TYPE_TARGET_TYPE(tt2);
1444 }
1445 if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1446 /* Array to pointer is a `trivial conversion' according to the ARM. */
479fdd26
JK
1447
1448 /* We should be doing much hairier argument matching (see section 13.2
1449 of the ARM), but as a quick kludge, just check for the same type
1450 code. */
a163ddec
MT
1451 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1452 return i+1;
1453 }
1454 if (!t1[i]) return 0;
1455 return t2[i] ? i+1 : 0;
1456}
1457
bd5635a1
RP
1458/* Helper function used by value_struct_elt to recurse through baseclasses.
1459 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2a5ec41d 1460 and search in it assuming it has (class) type TYPE.
d3bab255
JK
1461 If found, return value, else return NULL.
1462
1463 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1464 look for a baseclass named NAME. */
bd5635a1 1465
a91a6192 1466static value_ptr
d3bab255 1467search_struct_field (name, arg1, offset, type, looking_for_baseclass)
bd5635a1 1468 char *name;
a91a6192 1469 register value_ptr arg1;
bd5635a1
RP
1470 int offset;
1471 register struct type *type;
d3bab255 1472 int looking_for_baseclass;
bd5635a1
RP
1473{
1474 int i;
1475
1476 check_stub_type (type);
1477
d3bab255
JK
1478 if (! looking_for_baseclass)
1479 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1480 {
1481 char *t_field_name = TYPE_FIELD_NAME (type, i);
1482
2e4964ad 1483 if (t_field_name && STREQ (t_field_name, name))
d3bab255 1484 {
a91a6192 1485 value_ptr v;
01be6913
PB
1486 if (TYPE_FIELD_STATIC (type, i))
1487 {
1488 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1489 struct symbol *sym =
2e4964ad
FF
1490 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1491 if (sym == NULL)
1492 error ("Internal error: could not find physical static variable named %s",
1493 phys_name);
01be6913
PB
1494 v = value_at (TYPE_FIELD_TYPE (type, i),
1495 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1496 }
1497 else
1498 v = value_primitive_field (arg1, offset, i, type);
d3bab255
JK
1499 if (v == 0)
1500 error("there is no field named %s", name);
1501 return v;
1502 }
6d34c236
PB
1503 if (t_field_name && t_field_name[0] == '\0'
1504 && TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
1505 {
1506 /* Look for a match through the fields of an anonymous union. */
1507 value_ptr v;
1508 v = search_struct_field (name, arg1, offset,
1509 TYPE_FIELD_TYPE (type, i),
1510 looking_for_baseclass);
1511 if (v)
1512 return v;
1513 }
d3bab255 1514 }
bd5635a1
RP
1515
1516 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1517 {
a91a6192 1518 value_ptr v;
bd5635a1 1519 /* If we are looking for baseclasses, this is what we get when we
54023465
JK
1520 hit them. But it could happen that the base part's member name
1521 is not yet filled in. */
d3bab255 1522 int found_baseclass = (looking_for_baseclass
54023465 1523 && TYPE_BASECLASS_NAME (type, i) != NULL
2e4964ad 1524 && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
bd5635a1
RP
1525
1526 if (BASETYPE_VIA_VIRTUAL (type, i))
1527 {
a91a6192 1528 value_ptr v2;
bac89d6c 1529 /* Fix to use baseclass_offset instead. FIXME */
d11c44f1
JG
1530 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1531 &v2, (int *)NULL);
bd5635a1
RP
1532 if (v2 == 0)
1533 error ("virtual baseclass botch");
1534 if (found_baseclass)
1535 return v2;
d3bab255
JK
1536 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1537 looking_for_baseclass);
bd5635a1 1538 }
01be6913 1539 else if (found_baseclass)
bd5635a1
RP
1540 v = value_primitive_field (arg1, offset, i, type);
1541 else
1542 v = search_struct_field (name, arg1,
1543 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
d3bab255
JK
1544 TYPE_BASECLASS (type, i),
1545 looking_for_baseclass);
bd5635a1
RP
1546 if (v) return v;
1547 }
1548 return NULL;
1549}
1550
1551/* Helper function used by value_struct_elt to recurse through baseclasses.
1552 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2a5ec41d 1553 and search in it assuming it has (class) type TYPE.
cef4c2e7 1554 If found, return value, else if name matched and args not return (value)-1,
5b5c6d94 1555 else return NULL. */
bd5635a1 1556
a91a6192 1557static value_ptr
bac89d6c 1558search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
bd5635a1 1559 char *name;
a91a6192 1560 register value_ptr *arg1p, *args;
bd5635a1
RP
1561 int offset, *static_memfuncp;
1562 register struct type *type;
1563{
1564 int i;
a91a6192 1565 value_ptr v;
67e9b3b3 1566 int name_matched = 0;
6ebc9cdd 1567 char dem_opname[64];
bd5635a1
RP
1568
1569 check_stub_type (type);
1570 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1571 {
1572 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
6ebc9cdd
KH
1573 if (strncmp(t_field_name, "__", 2)==0 ||
1574 strncmp(t_field_name, "op", 2)==0 ||
1575 strncmp(t_field_name, "type", 4)==0 )
1576 {
1577 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1578 t_field_name = dem_opname;
1579 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1580 t_field_name = dem_opname;
1581 }
2e4964ad 1582 if (t_field_name && STREQ (t_field_name, name))
bd5635a1 1583 {
d3bab255 1584 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
bd5635a1 1585 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
5b5c6d94 1586 name_matched = 1;
bd5635a1 1587
d3bab255
JK
1588 if (j > 0 && args == 0)
1589 error ("cannot resolve overloaded method `%s'", name);
1590 while (j >= 0)
bd5635a1 1591 {
8e9a3f3b 1592 if (TYPE_FN_FIELD_STUB (f, j))
bd5635a1
RP
1593 check_stub_method (type, i, j);
1594 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1595 TYPE_FN_FIELD_ARGS (f, j), args))
1596 {
1597 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
a91a6192 1598 return value_virtual_fn_field (arg1p, f, j, type, offset);
bd5635a1
RP
1599 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1600 *static_memfuncp = 1;
a91a6192
SS
1601 v = value_fn_field (arg1p, f, j, type, offset);
1602 if (v != NULL) return v;
bd5635a1 1603 }
d3bab255 1604 j--;
bd5635a1
RP
1605 }
1606 }
1607 }
1608
1609 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1610 {
01be6913 1611 int base_offset;
bd5635a1
RP
1612
1613 if (BASETYPE_VIA_VIRTUAL (type, i))
1614 {
9f739abd 1615 base_offset = baseclass_offset (type, i, *arg1p, offset);
bac89d6c 1616 if (base_offset == -1)
bd5635a1 1617 error ("virtual baseclass botch");
bd5635a1 1618 }
01be6913
PB
1619 else
1620 {
01be6913
PB
1621 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1622 }
bac89d6c 1623 v = search_struct_method (name, arg1p, args, base_offset + offset,
bd5635a1 1624 static_memfuncp, TYPE_BASECLASS (type, i));
a91a6192 1625 if (v == (value_ptr) -1)
5b5c6d94
KH
1626 {
1627 name_matched = 1;
1628 }
1629 else if (v)
bac89d6c
FF
1630 {
1631/* FIXME-bothner: Why is this commented out? Why is it here? */
1632/* *arg1p = arg1_tmp;*/
1633 return v;
1634 }
bd5635a1 1635 }
a91a6192 1636 if (name_matched) return (value_ptr) -1;
5b5c6d94 1637 else return NULL;
bd5635a1
RP
1638}
1639
1640/* Given *ARGP, a value of type (pointer to a)* structure/union,
1641 extract the component named NAME from the ultimate target structure/union
1642 and return it as a value with its appropriate type.
1643 ERR is used in the error message if *ARGP's type is wrong.
1644
1645 C++: ARGS is a list of argument types to aid in the selection of
1646 an appropriate method. Also, handle derived types.
1647
1648 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1649 where the truthvalue of whether the function that was resolved was
1650 a static member function or not is stored.
1651
1652 ERR is an error message to be printed in case the field is not found. */
1653
a91a6192 1654value_ptr
bd5635a1 1655value_struct_elt (argp, args, name, static_memfuncp, err)
a91a6192 1656 register value_ptr *argp, *args;
bd5635a1
RP
1657 char *name;
1658 int *static_memfuncp;
1659 char *err;
1660{
1661 register struct type *t;
a91a6192 1662 value_ptr v;
bd5635a1
RP
1663
1664 COERCE_ARRAY (*argp);
1665
1666 t = VALUE_TYPE (*argp);
1667
1668 /* Follow pointers until we get to a non-pointer. */
1669
1670 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1671 {
bd5635a1 1672 *argp = value_ind (*argp);
f2ebc25f
JK
1673 /* Don't coerce fn pointer to fn and then back again! */
1674 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1675 COERCE_ARRAY (*argp);
bd5635a1
RP
1676 t = VALUE_TYPE (*argp);
1677 }
1678
1679 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1680 error ("not implemented: member type in value_struct_elt");
1681
2a5ec41d 1682 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
bd5635a1
RP
1683 && TYPE_CODE (t) != TYPE_CODE_UNION)
1684 error ("Attempt to extract a component of a value that is not a %s.", err);
1685
1686 /* Assume it's not, unless we see that it is. */
1687 if (static_memfuncp)
1688 *static_memfuncp =0;
1689
1690 if (!args)
1691 {
1692 /* if there are no arguments ...do this... */
1693
d3bab255 1694 /* Try as a field first, because if we succeed, there
bd5635a1 1695 is less work to be done. */
d3bab255 1696 v = search_struct_field (name, *argp, 0, t, 0);
bd5635a1
RP
1697 if (v)
1698 return v;
1699
1700 /* C++: If it was not found as a data field, then try to
1701 return it as a pointer to a method. */
1702
1703 if (destructor_name_p (name, t))
1704 error ("Cannot get value of destructor");
1705
bac89d6c 1706 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
bd5635a1 1707
a91a6192 1708 if (v == (value_ptr) -1)
67e9b3b3
PS
1709 error ("Cannot take address of a method");
1710 else if (v == 0)
bd5635a1
RP
1711 {
1712 if (TYPE_NFN_FIELDS (t))
1713 error ("There is no member or method named %s.", name);
1714 else
1715 error ("There is no member named %s.", name);
1716 }
1717 return v;
1718 }
1719
1720 if (destructor_name_p (name, t))
1721 {
1722 if (!args[1])
1723 {
1724 /* destructors are a special case. */
a91a6192
SS
1725 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
1726 TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0);
40620258
KH
1727 if (!v) error("could not find destructor function named %s.", name);
1728 else return v;
bd5635a1
RP
1729 }
1730 else
1731 {
1732 error ("destructor should not have any argument");
1733 }
1734 }
1735 else
bac89d6c 1736 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
bd5635a1 1737
a91a6192 1738 if (v == (value_ptr) -1)
5b5c6d94
KH
1739 {
1740 error("Argument list of %s mismatch with component in the structure.", name);
1741 }
1742 else if (v == 0)
bd5635a1
RP
1743 {
1744 /* See if user tried to invoke data as function. If so,
1745 hand it back. If it's not callable (i.e., a pointer to function),
1746 gdb should give an error. */
d3bab255 1747 v = search_struct_field (name, *argp, 0, t, 0);
bd5635a1
RP
1748 }
1749
1750 if (!v)
1751 error ("Structure has no component named %s.", name);
1752 return v;
1753}
1754
1755/* C++: return 1 is NAME is a legitimate name for the destructor
1756 of type TYPE. If TYPE does not have a destructor, or
1757 if NAME is inappropriate for TYPE, an error is signaled. */
1758int
1759destructor_name_p (name, type)
7919c3ed
JG
1760 const char *name;
1761 const struct type *type;
bd5635a1
RP
1762{
1763 /* destructors are a special case. */
1764
1765 if (name[0] == '~')
1766 {
1767 char *dname = type_name_no_tag (type);
6d34c236
PB
1768 char *cp = strchr (dname, '<');
1769 int len;
1770
1771 /* Do not compare the template part for template classes. */
1772 if (cp == NULL)
1773 len = strlen (dname);
1774 else
1775 len = cp - dname;
1776 if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
bd5635a1
RP
1777 error ("name of destructor must equal name of class");
1778 else
1779 return 1;
1780 }
1781 return 0;
1782}
1783
1784/* Helper function for check_field: Given TYPE, a structure/union,
1785 return 1 if the component named NAME from the ultimate
1786 target structure/union is defined, otherwise, return 0. */
1787
1788static int
1789check_field_in (type, name)
1790 register struct type *type;
01be6913 1791 const char *name;
bd5635a1
RP
1792{
1793 register int i;
1794
1795 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1796 {
1797 char *t_field_name = TYPE_FIELD_NAME (type, i);
2e4964ad 1798 if (t_field_name && STREQ (t_field_name, name))
bd5635a1
RP
1799 return 1;
1800 }
1801
1802 /* C++: If it was not found as a data field, then try to
1803 return it as a pointer to a method. */
1804
1805 /* Destructors are a special case. */
1806 if (destructor_name_p (name, type))
1807 return 1;
1808
1809 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1810 {
2e4964ad 1811 if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
bd5635a1
RP
1812 return 1;
1813 }
1814
1815 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1816 if (check_field_in (TYPE_BASECLASS (type, i), name))
1817 return 1;
1818
1819 return 0;
1820}
1821
1822
1823/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1824 return 1 if the component named NAME from the ultimate
1825 target structure/union is defined, otherwise, return 0. */
1826
1827int
1828check_field (arg1, name)
a91a6192 1829 register value_ptr arg1;
7919c3ed 1830 const char *name;
bd5635a1
RP
1831{
1832 register struct type *t;
1833
1834 COERCE_ARRAY (arg1);
1835
1836 t = VALUE_TYPE (arg1);
1837
1838 /* Follow pointers until we get to a non-pointer. */
1839
1840 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1841 t = TYPE_TARGET_TYPE (t);
1842
1843 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1844 error ("not implemented: member type in check_field");
1845
2a5ec41d 1846 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
bd5635a1
RP
1847 && TYPE_CODE (t) != TYPE_CODE_UNION)
1848 error ("Internal error: `this' is not an aggregate");
1849
1850 return check_field_in (t, name);
1851}
1852
01be6913 1853/* C++: Given an aggregate type CURTYPE, and a member name NAME,
2a5ec41d 1854 return the address of this member as a "pointer to member"
bd5635a1
RP
1855 type. If INTYPE is non-null, then it will be the type
1856 of the member we are looking for. This will help us resolve
01be6913
PB
1857 "pointers to member functions". This function is used
1858 to resolve user expressions of the form "DOMAIN::NAME". */
bd5635a1 1859
a91a6192 1860value_ptr
51b57ded 1861value_struct_elt_for_reference (domain, offset, curtype, name, intype)
01be6913 1862 struct type *domain, *curtype, *intype;
51b57ded 1863 int offset;
bd5635a1
RP
1864 char *name;
1865{
01be6913 1866 register struct type *t = curtype;
bd5635a1 1867 register int i;
a91a6192 1868 value_ptr v;
bd5635a1 1869
2a5ec41d 1870 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
bd5635a1 1871 && TYPE_CODE (t) != TYPE_CODE_UNION)
01be6913 1872 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
bd5635a1 1873
01be6913 1874 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
bd5635a1 1875 {
01be6913
PB
1876 char *t_field_name = TYPE_FIELD_NAME (t, i);
1877
2e4964ad 1878 if (t_field_name && STREQ (t_field_name, name))
bd5635a1 1879 {
01be6913 1880 if (TYPE_FIELD_STATIC (t, i))
bd5635a1 1881 {
01be6913
PB
1882 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1883 struct symbol *sym =
1884 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
2e4964ad
FF
1885 if (sym == NULL)
1886 error ("Internal error: could not find physical static variable named %s",
01be6913
PB
1887 phys_name);
1888 return value_at (SYMBOL_TYPE (sym),
1889 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
bd5635a1 1890 }
01be6913
PB
1891 if (TYPE_FIELD_PACKED (t, i))
1892 error ("pointers to bitfield members not allowed");
1893
1894 return value_from_longest
1895 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
1896 domain)),
51b57ded 1897 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
bd5635a1 1898 }
bd5635a1
RP
1899 }
1900
1901 /* C++: If it was not found as a data field, then try to
1902 return it as a pointer to a method. */
bd5635a1
RP
1903
1904 /* Destructors are a special case. */
1905 if (destructor_name_p (name, t))
1906 {
2a5ec41d 1907 error ("member pointers to destructors not implemented yet");
bd5635a1
RP
1908 }
1909
1910 /* Perform all necessary dereferencing. */
1911 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1912 intype = TYPE_TARGET_TYPE (intype);
1913
01be6913 1914 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
bd5635a1 1915 {
852b3831
PB
1916 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
1917 char dem_opname[64];
1918
1919 if (strncmp(t_field_name, "__", 2)==0 ||
1920 strncmp(t_field_name, "op", 2)==0 ||
1921 strncmp(t_field_name, "type", 4)==0 )
1922 {
1923 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1924 t_field_name = dem_opname;
1925 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1926 t_field_name = dem_opname;
1927 }
1928 if (t_field_name && STREQ (t_field_name, name))
bd5635a1 1929 {
01be6913
PB
1930 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1931 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1932
1933 if (intype == 0 && j > 1)
1934 error ("non-unique member `%s' requires type instantiation", name);
1935 if (intype)
bd5635a1 1936 {
01be6913
PB
1937 while (j--)
1938 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1939 break;
1940 if (j < 0)
1941 error ("no member function matches that type instantiation");
1942 }
1943 else
1944 j = 0;
1945
1946 if (TYPE_FN_FIELD_STUB (f, j))
1947 check_stub_method (t, i, j);
1948 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1949 {
1950 return value_from_longest
1951 (lookup_reference_type
1952 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1953 domain)),
13ffa6be 1954 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
01be6913
PB
1955 }
1956 else
1957 {
1958 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1959 0, VAR_NAMESPACE, 0, NULL);
35fcebce
PB
1960 if (s == NULL)
1961 {
1962 v = 0;
1963 }
1964 else
1965 {
1966 v = read_var_value (s, 0);
01be6913 1967#if 0
35fcebce
PB
1968 VALUE_TYPE (v) = lookup_reference_type
1969 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1970 domain));
01be6913 1971#endif
bd5635a1 1972 }
35fcebce 1973 return v;
bd5635a1
RP
1974 }
1975 }
35fcebce 1976 }
01be6913
PB
1977 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
1978 {
a91a6192 1979 value_ptr v;
51b57ded
FF
1980 int base_offset;
1981
1982 if (BASETYPE_VIA_VIRTUAL (t, i))
1983 base_offset = 0;
1984 else
1985 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
01be6913 1986 v = value_struct_elt_for_reference (domain,
51b57ded 1987 offset + base_offset,
01be6913
PB
1988 TYPE_BASECLASS (t, i),
1989 name,
1990 intype);
1991 if (v)
1992 return v;
bd5635a1
RP
1993 }
1994 return 0;
1995}
1996
bd5635a1
RP
1997/* C++: return the value of the class instance variable, if one exists.
1998 Flag COMPLAIN signals an error if the request is made in an
1999 inappropriate context. */
6d34c236 2000
a91a6192 2001value_ptr
bd5635a1
RP
2002value_of_this (complain)
2003 int complain;
2004{
bd5635a1
RP
2005 struct symbol *func, *sym;
2006 struct block *b;
2007 int i;
2008 static const char funny_this[] = "this";
a91a6192 2009 value_ptr this;
bd5635a1
RP
2010
2011 if (selected_frame == 0)
2012 if (complain)
2013 error ("no frame selected");
2014 else return 0;
2015
2016 func = get_frame_function (selected_frame);
2017 if (!func)
2018 {
2019 if (complain)
2020 error ("no `this' in nameless context");
2021 else return 0;
2022 }
2023
2024 b = SYMBOL_BLOCK_VALUE (func);
2025 i = BLOCK_NSYMS (b);
2026 if (i <= 0)
2027 if (complain)
2028 error ("no args, no `this'");
2029 else return 0;
2030
2031 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2032 symbol instead of the LOC_ARG one (if both exist). */
2033 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
2034 if (sym == NULL)
2035 {
2036 if (complain)
2037 error ("current stack frame not in method");
2038 else
2039 return NULL;
2040 }
2041
2042 this = read_var_value (sym, selected_frame);
2043 if (this == 0 && complain)
2044 error ("`this' argument at unknown address");
2045 return this;
2046}
a91a6192 2047
f91a9e05
PB
2048/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2049 long, starting at LOWBOUND. The result has the same lower bound as
2050 the original ARRAY. */
2051
2052value_ptr
2053value_slice (array, lowbound, length)
2054 value_ptr array;
2055 int lowbound, length;
2056{
2057 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_BITSTRING)
2058 error ("not implemented - bitstring slice");
2059 if (TYPE_CODE (VALUE_TYPE (array)) != TYPE_CODE_ARRAY
2060 && TYPE_CODE (VALUE_TYPE (array)) != TYPE_CODE_STRING)
2061 error ("cannot take slice of non-array");
2062 else
2063 {
2064 struct type *slice_range_type, *slice_type;
2065 value_ptr slice;
2066 struct type *range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
2067 struct type *element_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
2068 int lowerbound = TYPE_LOW_BOUND (range_type);
2069 int upperbound = TYPE_HIGH_BOUND (range_type);
2070 int offset = (lowbound - lowerbound) * TYPE_LENGTH (element_type);
2071 if (lowbound < lowerbound || length < 0
2072 || lowbound + length - 1 > upperbound)
2073 error ("slice out of range");
2074 slice_range_type = create_range_type ((struct type*) NULL,
2075 TYPE_TARGET_TYPE (range_type),
2076 lowerbound,
2077 lowerbound + length - 1);
2078 slice_type = create_array_type ((struct type*) NULL, element_type,
2079 slice_range_type);
2080 TYPE_CODE (slice_type) = TYPE_CODE (VALUE_TYPE (array));
2081 slice = allocate_value (slice_type);
2082 if (VALUE_LAZY (array))
2083 VALUE_LAZY (slice) = 1;
2084 else
2085 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2086 TYPE_LENGTH (slice_type));
2087 if (VALUE_LVAL (array) == lval_internalvar)
2088 VALUE_LVAL (slice) = lval_internalvar_component;
2089 else
2090 VALUE_LVAL (slice) = VALUE_LVAL (array);
2091 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2092 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2093 return slice;
2094 }
2095}
2096
2097/* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2098 value as a fixed-length array. */
2099
2100value_ptr
2101varying_to_slice (varray)
2102 value_ptr varray;
2103{
2104 struct type *vtype = VALUE_TYPE (varray);
2105 LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
2106 VALUE_CONTENTS (varray)
2107 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
2108 return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
2109}
2110
a91a6192
SS
2111/* Create a value for a FORTRAN complex number. Currently most of
2112 the time values are coerced to COMPLEX*16 (i.e. a complex number
2113 composed of 2 doubles. This really should be a smarter routine
2114 that figures out precision inteligently as opposed to assuming
2115 doubles. FIXME: fmb */
2116
2117value_ptr
5222ca60 2118value_literal_complex (arg1, arg2, type)
a91a6192
SS
2119 value_ptr arg1;
2120 value_ptr arg2;
5222ca60 2121 struct type *type;
a91a6192 2122{
a91a6192 2123 register value_ptr val;
5222ca60 2124 struct type *real_type = TYPE_TARGET_TYPE (type);
a91a6192 2125
5222ca60
PB
2126 val = allocate_value (type);
2127 arg1 = value_cast (real_type, arg1);
2128 arg2 = value_cast (real_type, arg2);
a91a6192 2129
5222ca60
PB
2130 memcpy (VALUE_CONTENTS_RAW (val),
2131 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2132 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2133 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
a91a6192
SS
2134 return val;
2135}
9ed8604f 2136
5222ca60 2137/* Cast a value into the appropriate complex data type. */
9ed8604f
PS
2138
2139static value_ptr
5222ca60 2140cast_into_complex (type, val)
9ed8604f
PS
2141 struct type *type;
2142 register value_ptr val;
2143{
5222ca60
PB
2144 struct type *real_type = TYPE_TARGET_TYPE (type);
2145 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
9ed8604f 2146 {
5222ca60
PB
2147 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2148 value_ptr re_val = allocate_value (val_real_type);
2149 value_ptr im_val = allocate_value (val_real_type);
9ed8604f 2150
5222ca60
PB
2151 memcpy (VALUE_CONTENTS_RAW (re_val),
2152 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2153 memcpy (VALUE_CONTENTS_RAW (im_val),
2154 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2155 TYPE_LENGTH (val_real_type));
9ed8604f 2156
5222ca60 2157 return value_literal_complex (re_val, im_val, type);
9ed8604f 2158 }
5222ca60
PB
2159 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2160 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2161 return value_literal_complex (val, value_zero (real_type, not_lval), type);
9ed8604f 2162 else
5222ca60 2163 error ("cannot cast non-number to complex");
9ed8604f 2164}
This page took 0.330951 seconds and 4 git commands to generate.