* Fixing typo that caused infinite loop upon PKE MPG.
[binutils-gdb.git] / gdb / valops.c
blobf902304e811b8b80ee03beb8a02c70d6e039a394
1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "demangle.h"
30 #include "language.h"
32 #include <errno.h>
33 #include "gdb_string.h"
35 /* Default to coercing float to double in function calls only when there is
36 no prototype. Otherwise on targets where the debug information is incorrect
37 for either the prototype or non-prototype case, we can force it by defining
38 COERCE_FLOAT_TO_DOUBLE in the target configuration file. */
40 #ifndef COERCE_FLOAT_TO_DOUBLE
41 #define COERCE_FLOAT_TO_DOUBLE (param_type == NULL)
42 #endif
44 /* Local functions. */
46 static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[]));
48 #ifdef CALL_DUMMY
49 static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **));
50 static value_ptr value_arg_coerce PARAMS ((value_ptr, struct type *));
51 #endif
54 #ifndef PUSH_ARGUMENTS
55 static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr));
56 #endif
58 static value_ptr search_struct_field PARAMS ((char *, value_ptr, int,
59 struct type *, int));
61 static value_ptr search_struct_method PARAMS ((char *, value_ptr *,
62 value_ptr *,
63 int, int *, struct type *));
65 static int check_field_in PARAMS ((struct type *, const char *));
67 static CORE_ADDR allocate_space_in_inferior PARAMS ((int));
69 static value_ptr cast_into_complex PARAMS ((struct type *, value_ptr));
71 #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
73 /* Flag for whether we want to abandon failed expression evals by default. */
75 #if 0
76 static int auto_abandon = 0;
77 #endif
80 /* Find the address of function name NAME in the inferior. */
82 value_ptr
83 find_function_in_inferior (name)
84 char *name;
86 register struct symbol *sym;
87 sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
88 if (sym != NULL)
90 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
92 error ("\"%s\" exists in this program but is not a function.",
93 name);
95 return value_of_variable (sym, NULL);
97 else
99 struct minimal_symbol *msymbol = lookup_minimal_symbol(name, NULL, NULL);
100 if (msymbol != NULL)
102 struct type *type;
103 LONGEST maddr;
104 type = lookup_pointer_type (builtin_type_char);
105 type = lookup_function_type (type);
106 type = lookup_pointer_type (type);
107 maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
108 return value_from_longest (type, maddr);
110 else
112 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
117 /* Allocate NBYTES of space in the inferior using the inferior's malloc
118 and return a value that is a pointer to the allocated space. */
120 value_ptr
121 value_allocate_space_in_inferior (len)
122 int len;
124 value_ptr blocklen;
125 register value_ptr val = find_function_in_inferior ("malloc");
127 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
128 val = call_function_by_hand (val, 1, &blocklen);
129 if (value_logical_not (val))
131 error ("No memory available to program.");
133 return val;
136 static CORE_ADDR
137 allocate_space_in_inferior (len)
138 int len;
140 return value_as_long (value_allocate_space_in_inferior (len));
143 /* Cast value ARG2 to type TYPE and return as a value.
144 More general than a C cast: accepts any two types of the same length,
145 and if ARG2 is an lvalue it can be cast into anything at all. */
146 /* In C++, casts may change pointer or object representations. */
148 value_ptr
149 value_cast (type, arg2)
150 struct type *type;
151 register value_ptr arg2;
153 register enum type_code code1;
154 register enum type_code code2;
155 register int scalar;
156 struct type *type2;
158 if (VALUE_TYPE (arg2) == type)
159 return arg2;
161 CHECK_TYPEDEF (type);
162 code1 = TYPE_CODE (type);
163 COERCE_REF(arg2);
164 type2 = check_typedef (VALUE_TYPE (arg2));
166 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
167 is treated like a cast to (TYPE [N])OBJECT,
168 where N is sizeof(OBJECT)/sizeof(TYPE). */
169 if (code1 == TYPE_CODE_ARRAY)
171 struct type *element_type = TYPE_TARGET_TYPE (type);
172 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
173 if (element_length > 0
174 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
176 struct type *range_type = TYPE_INDEX_TYPE (type);
177 int val_length = TYPE_LENGTH (type2);
178 LONGEST low_bound, high_bound, new_length;
179 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
180 low_bound = 0, high_bound = 0;
181 new_length = val_length / element_length;
182 if (val_length % element_length != 0)
183 warning("array element type size does not divide object size in cast");
184 /* FIXME-type-allocation: need a way to free this type when we are
185 done with it. */
186 range_type = create_range_type ((struct type *) NULL,
187 TYPE_TARGET_TYPE (range_type),
188 low_bound,
189 new_length + low_bound - 1);
190 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
191 element_type, range_type);
192 return arg2;
196 if (current_language->c_style_arrays
197 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
198 arg2 = value_coerce_array (arg2);
200 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
201 arg2 = value_coerce_function (arg2);
203 type2 = check_typedef (VALUE_TYPE (arg2));
204 COERCE_VARYING_ARRAY (arg2, type2);
205 code2 = TYPE_CODE (type2);
207 if (code1 == TYPE_CODE_COMPLEX)
208 return cast_into_complex (type, arg2);
209 if (code1 == TYPE_CODE_BOOL || code1 == TYPE_CODE_CHAR)
210 code1 = TYPE_CODE_INT;
211 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
212 code2 = TYPE_CODE_INT;
214 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
215 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
217 if ( code1 == TYPE_CODE_STRUCT
218 && code2 == TYPE_CODE_STRUCT
219 && TYPE_NAME (type) != 0)
221 /* Look in the type of the source to see if it contains the
222 type of the target as a superclass. If so, we'll need to
223 offset the object in addition to changing its type. */
224 value_ptr v = search_struct_field (type_name_no_tag (type),
225 arg2, 0, type2, 1);
226 if (v)
228 VALUE_TYPE (v) = type;
229 return v;
232 if (code1 == TYPE_CODE_FLT && scalar)
233 return value_from_double (type, value_as_double (arg2));
234 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
235 || code1 == TYPE_CODE_RANGE)
236 && (scalar || code2 == TYPE_CODE_PTR))
237 return value_from_longest (type, value_as_long (arg2));
238 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
240 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
242 /* Look in the type of the source to see if it contains the
243 type of the target as a superclass. If so, we'll need to
244 offset the pointer rather than just change its type. */
245 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
246 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
247 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
248 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
249 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
251 value_ptr v = search_struct_field (type_name_no_tag (t1),
252 value_ind (arg2), 0, t2, 1);
253 if (v)
255 v = value_addr (v);
256 VALUE_TYPE (v) = type;
257 return v;
260 /* No superclass found, just fall through to change ptr type. */
262 VALUE_TYPE (arg2) = type;
263 return arg2;
265 else if (chill_varying_type (type))
267 struct type *range1, *range2, *eltype1, *eltype2;
268 value_ptr val;
269 int count1, count2;
270 LONGEST low_bound, high_bound;
271 char *valaddr, *valaddr_data;
272 if (code2 == TYPE_CODE_BITSTRING)
273 error ("not implemented: converting bitstring to varying type");
274 if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
275 || (eltype1 = check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1))),
276 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)),
277 (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
278 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
279 error ("Invalid conversion to varying type");
280 range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
281 range2 = TYPE_FIELD_TYPE (type2, 0);
282 if (get_discrete_bounds (range1, &low_bound, &high_bound) < 0)
283 count1 = -1;
284 else
285 count1 = high_bound - low_bound + 1;
286 if (get_discrete_bounds (range2, &low_bound, &high_bound) < 0)
287 count1 = -1, count2 = 0; /* To force error before */
288 else
289 count2 = high_bound - low_bound + 1;
290 if (count2 > count1)
291 error ("target varying type is too small");
292 val = allocate_value (type);
293 valaddr = VALUE_CONTENTS_RAW (val);
294 valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
295 /* Set val's __var_length field to count2. */
296 store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
297 count2);
298 /* Set the __var_data field to count2 elements copied from arg2. */
299 memcpy (valaddr_data, VALUE_CONTENTS (arg2),
300 count2 * TYPE_LENGTH (eltype2));
301 /* Zero the rest of the __var_data field of val. */
302 memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
303 (count1 - count2) * TYPE_LENGTH (eltype2));
304 return val;
306 else if (VALUE_LVAL (arg2) == lval_memory)
308 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
309 VALUE_BFD_SECTION (arg2));
311 else if (code1 == TYPE_CODE_VOID)
313 return value_zero (builtin_type_void, not_lval);
315 else
317 error ("Invalid cast.");
318 return 0;
322 /* Create a value of type TYPE that is zero, and return it. */
324 value_ptr
325 value_zero (type, lv)
326 struct type *type;
327 enum lval_type lv;
329 register value_ptr val = allocate_value (type);
331 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
332 VALUE_LVAL (val) = lv;
334 return val;
337 /* Return a value with type TYPE located at ADDR.
339 Call value_at only if the data needs to be fetched immediately;
340 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
341 value_at_lazy instead. value_at_lazy simply records the address of
342 the data and sets the lazy-evaluation-required flag. The lazy flag
343 is tested in the VALUE_CONTENTS macro, which is used if and when
344 the contents are actually required. */
346 value_ptr
347 value_at (type, addr, sect)
348 struct type *type;
349 CORE_ADDR addr;
350 asection *sect;
352 register value_ptr val;
354 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
355 error ("Attempt to dereference a generic pointer.");
357 val = allocate_value (type);
359 #ifdef GDB_TARGET_IS_D10V
360 if (TYPE_TARGET_TYPE(type) && TYPE_CODE(TYPE_TARGET_TYPE(type)) == TYPE_CODE_FUNC)
362 int num;
363 short snum;
364 read_memory (addr, (char *)&snum, 2);
365 num = D10V_MAKE_IADDR(snum);
366 memcpy( VALUE_CONTENTS_RAW (val), &num, 4);
368 else
369 #endif
371 read_memory_section (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type), sect);
373 VALUE_LVAL (val) = lval_memory;
374 VALUE_ADDRESS (val) = addr;
375 VALUE_BFD_SECTION (val) = sect;
377 return val;
380 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
382 value_ptr
383 value_at_lazy (type, addr, sect)
384 struct type *type;
385 CORE_ADDR addr;
386 asection *sect;
388 register value_ptr val;
390 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
391 error ("Attempt to dereference a generic pointer.");
393 val = allocate_value (type);
395 VALUE_LVAL (val) = lval_memory;
396 VALUE_ADDRESS (val) = addr;
397 VALUE_LAZY (val) = 1;
398 VALUE_BFD_SECTION (val) = sect;
400 return val;
403 /* Called only from the VALUE_CONTENTS macro, if the current data for
404 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
405 data from the user's process, and clears the lazy flag to indicate
406 that the data in the buffer is valid.
408 If the value is zero-length, we avoid calling read_memory, which would
409 abort. We mark the value as fetched anyway -- all 0 bytes of it.
411 This function returns a value because it is used in the VALUE_CONTENTS
412 macro as part of an expression, where a void would not work. The
413 value is ignored. */
416 value_fetch_lazy (val)
417 register value_ptr val;
419 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
420 int length = TYPE_LENGTH (VALUE_TYPE (val));
422 #ifdef GDB_TARGET_IS_D10V
423 struct type *type = VALUE_TYPE(val);
424 if (TYPE_TARGET_TYPE(type) && TYPE_CODE(TYPE_TARGET_TYPE(type)) == TYPE_CODE_FUNC)
426 int num;
427 short snum;
428 read_memory (addr, (char *)&snum, 2);
429 num = D10V_MAKE_IADDR(snum);
430 memcpy( VALUE_CONTENTS_RAW (val), &num, 4);
432 else
433 #endif
435 if (length)
436 read_memory_section (addr, VALUE_CONTENTS_RAW (val), length,
437 VALUE_BFD_SECTION (val));
438 VALUE_LAZY (val) = 0;
439 return 0;
443 /* Store the contents of FROMVAL into the location of TOVAL.
444 Return a new value with the location of TOVAL and contents of FROMVAL. */
446 value_ptr
447 value_assign (toval, fromval)
448 register value_ptr toval, fromval;
450 register struct type *type;
451 register value_ptr val;
452 char raw_buffer[MAX_REGISTER_RAW_SIZE];
453 int use_buffer = 0;
455 if (!toval->modifiable)
456 error ("Left operand of assignment is not a modifiable lvalue.");
458 COERCE_REF (toval);
460 type = VALUE_TYPE (toval);
461 if (VALUE_LVAL (toval) != lval_internalvar)
462 fromval = value_cast (type, fromval);
463 else
464 COERCE_ARRAY (fromval);
465 CHECK_TYPEDEF (type);
467 /* If TOVAL is a special machine register requiring conversion
468 of program values to a special raw format,
469 convert FROMVAL's contents now, with result in `raw_buffer',
470 and set USE_BUFFER to the number of bytes to write. */
472 #ifdef REGISTER_CONVERTIBLE
473 if (VALUE_REGNO (toval) >= 0
474 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
476 int regno = VALUE_REGNO (toval);
477 if (REGISTER_CONVERTIBLE (regno))
479 struct type *fromtype = check_typedef (VALUE_TYPE (fromval));
480 REGISTER_CONVERT_TO_RAW (fromtype, regno,
481 VALUE_CONTENTS (fromval), raw_buffer);
482 use_buffer = REGISTER_RAW_SIZE (regno);
485 #endif
487 switch (VALUE_LVAL (toval))
489 case lval_internalvar:
490 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
491 return value_copy (VALUE_INTERNALVAR (toval)->value);
493 case lval_internalvar_component:
494 set_internalvar_component (VALUE_INTERNALVAR (toval),
495 VALUE_OFFSET (toval),
496 VALUE_BITPOS (toval),
497 VALUE_BITSIZE (toval),
498 fromval);
499 break;
501 case lval_memory:
502 if (VALUE_BITSIZE (toval))
504 char buffer[sizeof (LONGEST)];
505 /* We assume that the argument to read_memory is in units of
506 host chars. FIXME: Is that correct? */
507 int len = (VALUE_BITPOS (toval)
508 + VALUE_BITSIZE (toval)
509 + HOST_CHAR_BIT - 1)
510 / HOST_CHAR_BIT;
512 if (len > (int) sizeof (LONGEST))
513 error ("Can't handle bitfields which don't fit in a %d bit word.",
514 sizeof (LONGEST) * HOST_CHAR_BIT);
516 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
517 buffer, len);
518 modify_field (buffer, value_as_long (fromval),
519 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
520 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
521 buffer, len);
523 else if (use_buffer)
524 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
525 raw_buffer, use_buffer);
526 else
527 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
528 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
529 break;
531 case lval_register:
532 if (VALUE_BITSIZE (toval))
534 char buffer[sizeof (LONGEST)];
535 int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
537 if (len > (int) sizeof (LONGEST))
538 error ("Can't handle bitfields in registers larger than %d bits.",
539 sizeof (LONGEST) * HOST_CHAR_BIT);
541 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
542 > len * HOST_CHAR_BIT)
543 /* Getting this right would involve being very careful about
544 byte order. */
545 error ("\
546 Can't handle bitfield which doesn't fit in a single register.");
548 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
549 buffer, len);
550 modify_field (buffer, value_as_long (fromval),
551 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
552 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
553 buffer, len);
555 else if (use_buffer)
556 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
557 raw_buffer, use_buffer);
558 else
560 /* Do any conversion necessary when storing this type to more
561 than one register. */
562 #ifdef REGISTER_CONVERT_FROM_TYPE
563 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
564 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
565 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
566 raw_buffer, TYPE_LENGTH (type));
567 #else
568 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
569 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
570 #endif
572 /* Assigning to the stack pointer, frame pointer, and other
573 (architecture and calling convention specific) registers may
574 cause the frame cache to be out of date. We just do this
575 on all assignments to registers for simplicity; I doubt the slowdown
576 matters. */
577 reinit_frame_cache ();
578 break;
580 case lval_reg_frame_relative:
582 /* value is stored in a series of registers in the frame
583 specified by the structure. Copy that value out, modify
584 it, and copy it back in. */
585 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
586 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
587 int byte_offset = VALUE_OFFSET (toval) % reg_size;
588 int reg_offset = VALUE_OFFSET (toval) / reg_size;
589 int amount_copied;
591 /* Make the buffer large enough in all cases. */
592 char *buffer = (char *) alloca (amount_to_copy
593 + sizeof (LONGEST)
594 + MAX_REGISTER_RAW_SIZE);
596 int regno;
597 struct frame_info *frame;
599 /* Figure out which frame this is in currently. */
600 for (frame = get_current_frame ();
601 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
602 frame = get_prev_frame (frame))
605 if (!frame)
606 error ("Value being assigned to is no longer active.");
608 amount_to_copy += (reg_size - amount_to_copy % reg_size);
610 /* Copy it out. */
611 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
612 amount_copied = 0);
613 amount_copied < amount_to_copy;
614 amount_copied += reg_size, regno++)
616 get_saved_register (buffer + amount_copied,
617 (int *)NULL, (CORE_ADDR *)NULL,
618 frame, regno, (enum lval_type *)NULL);
621 /* Modify what needs to be modified. */
622 if (VALUE_BITSIZE (toval))
623 modify_field (buffer + byte_offset,
624 value_as_long (fromval),
625 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
626 else if (use_buffer)
627 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
628 else
629 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
630 TYPE_LENGTH (type));
632 /* Copy it back. */
633 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
634 amount_copied = 0);
635 amount_copied < amount_to_copy;
636 amount_copied += reg_size, regno++)
638 enum lval_type lval;
639 CORE_ADDR addr;
640 int optim;
642 /* Just find out where to put it. */
643 get_saved_register ((char *)NULL,
644 &optim, &addr, frame, regno, &lval);
646 if (optim)
647 error ("Attempt to assign to a value that was optimized out.");
648 if (lval == lval_memory)
649 write_memory (addr, buffer + amount_copied, reg_size);
650 else if (lval == lval_register)
651 write_register_bytes (addr, buffer + amount_copied, reg_size);
652 else
653 error ("Attempt to assign to an unmodifiable value.");
656 break;
659 default:
660 error ("Left operand of assignment is not an lvalue.");
663 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
664 If the field is signed, and is negative, then sign extend. */
665 if ((VALUE_BITSIZE (toval) > 0)
666 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
668 LONGEST fieldval = value_as_long (fromval);
669 LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
671 fieldval &= valmask;
672 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
673 fieldval |= ~valmask;
675 fromval = value_from_longest (type, fieldval);
678 val = value_copy (toval);
679 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
680 TYPE_LENGTH (type));
681 VALUE_TYPE (val) = type;
683 return val;
686 /* Extend a value VAL to COUNT repetitions of its type. */
688 value_ptr
689 value_repeat (arg1, count)
690 value_ptr arg1;
691 int count;
693 register value_ptr val;
695 if (VALUE_LVAL (arg1) != lval_memory)
696 error ("Only values in memory can be extended with '@'.");
697 if (count < 1)
698 error ("Invalid number %d of repetitions.", count);
700 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
702 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
703 VALUE_CONTENTS_RAW (val),
704 TYPE_LENGTH (VALUE_TYPE (val)));
705 VALUE_LVAL (val) = lval_memory;
706 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
708 return val;
711 value_ptr
712 value_of_variable (var, b)
713 struct symbol *var;
714 struct block *b;
716 value_ptr val;
717 struct frame_info *frame = NULL;
719 if (!b)
720 frame = NULL; /* Use selected frame. */
721 else if (symbol_read_needs_frame (var))
723 frame = block_innermost_frame (b);
724 if (!frame)
725 if (BLOCK_FUNCTION (b)
726 && SYMBOL_NAME (BLOCK_FUNCTION (b)))
727 error ("No frame is currently executing in block %s.",
728 SYMBOL_NAME (BLOCK_FUNCTION (b)));
729 else
730 error ("No frame is currently executing in specified block");
733 val = read_var_value (var, frame);
734 if (!val)
735 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
737 return val;
740 /* Given a value which is an array, return a value which is a pointer to its
741 first element, regardless of whether or not the array has a nonzero lower
742 bound.
744 FIXME: A previous comment here indicated that this routine should be
745 substracting the array's lower bound. It's not clear to me that this
746 is correct. Given an array subscripting operation, it would certainly
747 work to do the adjustment here, essentially computing:
749 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
751 However I believe a more appropriate and logical place to account for
752 the lower bound is to do so in value_subscript, essentially computing:
754 (&array[0] + ((index - lowerbound) * sizeof array[0]))
756 As further evidence consider what would happen with operations other
757 than array subscripting, where the caller would get back a value that
758 had an address somewhere before the actual first element of the array,
759 and the information about the lower bound would be lost because of
760 the coercion to pointer type.
763 value_ptr
764 value_coerce_array (arg1)
765 value_ptr arg1;
767 register struct type *type = check_typedef (VALUE_TYPE (arg1));
769 if (VALUE_LVAL (arg1) != lval_memory)
770 error ("Attempt to take address of value not located in memory.");
772 return value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
773 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
776 /* Given a value which is a function, return a value which is a pointer
777 to it. */
779 value_ptr
780 value_coerce_function (arg1)
781 value_ptr arg1;
783 value_ptr retval;
785 if (VALUE_LVAL (arg1) != lval_memory)
786 error ("Attempt to take address of value not located in memory.");
788 retval = value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
789 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
790 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
791 return retval;
794 /* Return a pointer value for the object for which ARG1 is the contents. */
796 value_ptr
797 value_addr (arg1)
798 value_ptr arg1;
800 value_ptr retval;
802 struct type *type = check_typedef (VALUE_TYPE (arg1));
803 if (TYPE_CODE (type) == TYPE_CODE_REF)
805 /* Copy the value, but change the type from (T&) to (T*).
806 We keep the same location information, which is efficient,
807 and allows &(&X) to get the location containing the reference. */
808 value_ptr arg2 = value_copy (arg1);
809 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
810 return arg2;
812 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
813 return value_coerce_function (arg1);
815 if (VALUE_LVAL (arg1) != lval_memory)
816 error ("Attempt to take address of value not located in memory.");
818 retval = value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
819 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
820 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
821 return retval;
824 /* Given a value of a pointer type, apply the C unary * operator to it. */
826 value_ptr
827 value_ind (arg1)
828 value_ptr arg1;
830 struct type *type1;
831 COERCE_ARRAY (arg1);
832 type1 = check_typedef (VALUE_TYPE (arg1));
834 if (TYPE_CODE (type1) == TYPE_CODE_MEMBER)
835 error ("not implemented: member types in value_ind");
837 /* Allow * on an integer so we can cast it to whatever we want.
838 This returns an int, which seems like the most C-like thing
839 to do. "long long" variables are rare enough that
840 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
841 if (TYPE_CODE (type1) == TYPE_CODE_INT)
842 return value_at (builtin_type_int,
843 (CORE_ADDR) value_as_long (arg1),
844 VALUE_BFD_SECTION (arg1));
845 else if (TYPE_CODE (type1) == TYPE_CODE_PTR)
846 return value_at_lazy (TYPE_TARGET_TYPE (type1), value_as_pointer (arg1),
847 VALUE_BFD_SECTION (arg1));
848 error ("Attempt to take contents of a non-pointer value.");
849 return 0; /* For lint -- never reached */
852 /* Pushing small parts of stack frames. */
854 /* Push one word (the size of object that a register holds). */
856 CORE_ADDR
857 push_word (sp, word)
858 CORE_ADDR sp;
859 ULONGEST word;
861 register int len = REGISTER_SIZE;
862 char buffer[MAX_REGISTER_RAW_SIZE];
864 store_unsigned_integer (buffer, len, word);
865 #if 1 INNER_THAN 2
866 sp -= len;
867 write_memory (sp, buffer, len);
868 #else /* stack grows upward */
869 write_memory (sp, buffer, len);
870 sp += len;
871 #endif /* stack grows upward */
873 return sp;
876 /* Push LEN bytes with data at BUFFER. */
878 CORE_ADDR
879 push_bytes (sp, buffer, len)
880 CORE_ADDR sp;
881 char *buffer;
882 int len;
884 #if 1 INNER_THAN 2
885 sp -= len;
886 write_memory (sp, buffer, len);
887 #else /* stack grows upward */
888 write_memory (sp, buffer, len);
889 sp += len;
890 #endif /* stack grows upward */
892 return sp;
895 /* Push onto the stack the specified value VALUE. */
897 #ifndef PUSH_ARGUMENTS
899 static CORE_ADDR
900 value_push (sp, arg)
901 register CORE_ADDR sp;
902 value_ptr arg;
904 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
906 #if 1 INNER_THAN 2
907 sp -= len;
908 write_memory (sp, VALUE_CONTENTS (arg), len);
909 #else /* stack grows upward */
910 write_memory (sp, VALUE_CONTENTS (arg), len);
911 sp += len;
912 #endif /* stack grows upward */
914 return sp;
917 #endif /* !PUSH_ARGUMENTS */
919 #ifdef CALL_DUMMY
920 /* Perform the standard coercions that are specified
921 for arguments to be passed to C functions.
923 If PARAM_TYPE is non-NULL, it is the expected parameter type. */
925 static value_ptr
926 value_arg_coerce (arg, param_type)
927 value_ptr arg;
928 struct type *param_type;
930 register struct type *arg_type = check_typedef (VALUE_TYPE (arg));
931 register struct type *type
932 = param_type ? check_typedef (param_type) : arg_type;
934 switch (TYPE_CODE (type))
936 case TYPE_CODE_REF:
937 if (TYPE_CODE (arg_type) != TYPE_CODE_REF)
939 arg = value_addr (arg);
940 VALUE_TYPE (arg) = param_type;
941 return arg;
943 break;
944 case TYPE_CODE_INT:
945 case TYPE_CODE_CHAR:
946 case TYPE_CODE_BOOL:
947 case TYPE_CODE_ENUM:
948 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
949 type = builtin_type_int;
950 break;
951 case TYPE_CODE_FLT:
952 /* coerce float to double, unless the function prototype specifies float */
953 if (COERCE_FLOAT_TO_DOUBLE)
955 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
956 type = builtin_type_double;
957 else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
958 type = builtin_type_long_double;
960 break;
961 case TYPE_CODE_FUNC:
962 type = lookup_pointer_type (type);
963 break;
964 case TYPE_CODE_ARRAY:
965 if (current_language->c_style_arrays)
966 type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
967 break;
968 case TYPE_CODE_UNDEF:
969 case TYPE_CODE_PTR:
970 case TYPE_CODE_STRUCT:
971 case TYPE_CODE_UNION:
972 case TYPE_CODE_VOID:
973 case TYPE_CODE_SET:
974 case TYPE_CODE_RANGE:
975 case TYPE_CODE_STRING:
976 case TYPE_CODE_BITSTRING:
977 case TYPE_CODE_ERROR:
978 case TYPE_CODE_MEMBER:
979 case TYPE_CODE_METHOD:
980 case TYPE_CODE_COMPLEX:
981 default:
982 break;
985 return value_cast (type, arg);
988 /* Determine a function's address and its return type from its value.
989 Calls error() if the function is not valid for calling. */
991 static CORE_ADDR
992 find_function_addr (function, retval_type)
993 value_ptr function;
994 struct type **retval_type;
996 register struct type *ftype = check_typedef (VALUE_TYPE (function));
997 register enum type_code code = TYPE_CODE (ftype);
998 struct type *value_type;
999 CORE_ADDR funaddr;
1001 /* If it's a member function, just look at the function
1002 part of it. */
1004 /* Determine address to call. */
1005 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1007 funaddr = VALUE_ADDRESS (function);
1008 value_type = TYPE_TARGET_TYPE (ftype);
1010 else if (code == TYPE_CODE_PTR)
1012 funaddr = value_as_pointer (function);
1013 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
1014 if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
1015 || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
1017 #ifdef CONVERT_FROM_FUNC_PTR_ADDR
1018 /* FIXME: This is a workaround for the unusual function
1019 pointer representation on the RS/6000, see comment
1020 in config/rs6000/tm-rs6000.h */
1021 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
1022 #endif
1023 value_type = TYPE_TARGET_TYPE (ftype);
1025 else
1026 value_type = builtin_type_int;
1028 else if (code == TYPE_CODE_INT)
1030 /* Handle the case of functions lacking debugging info.
1031 Their values are characters since their addresses are char */
1032 if (TYPE_LENGTH (ftype) == 1)
1033 funaddr = value_as_pointer (value_addr (function));
1034 else
1035 /* Handle integer used as address of a function. */
1036 funaddr = (CORE_ADDR) value_as_long (function);
1038 value_type = builtin_type_int;
1040 else
1041 error ("Invalid data type for function to be called.");
1043 *retval_type = value_type;
1044 return funaddr;
1047 /* All this stuff with a dummy frame may seem unnecessarily complicated
1048 (why not just save registers in GDB?). The purpose of pushing a dummy
1049 frame which looks just like a real frame is so that if you call a
1050 function and then hit a breakpoint (get a signal, etc), "backtrace"
1051 will look right. Whether the backtrace needs to actually show the
1052 stack at the time the inferior function was called is debatable, but
1053 it certainly needs to not display garbage. So if you are contemplating
1054 making dummy frames be different from normal frames, consider that. */
1056 /* Perform a function call in the inferior.
1057 ARGS is a vector of values of arguments (NARGS of them).
1058 FUNCTION is a value, the function to be called.
1059 Returns a value representing what the function returned.
1060 May fail to return, if a breakpoint or signal is hit
1061 during the execution of the function.
1063 ARGS is modified to contain coerced values. */
1065 value_ptr
1066 call_function_by_hand (function, nargs, args)
1067 value_ptr function;
1068 int nargs;
1069 value_ptr *args;
1071 register CORE_ADDR sp;
1072 register int i;
1073 CORE_ADDR start_sp;
1074 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
1075 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
1076 and remove any extra bytes which might exist because ULONGEST is
1077 bigger than REGISTER_SIZE. */
1078 static ULONGEST dummy[] = CALL_DUMMY;
1079 char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (ULONGEST)];
1080 CORE_ADDR old_sp;
1081 struct type *value_type;
1082 unsigned char struct_return;
1083 CORE_ADDR struct_addr = 0;
1084 struct inferior_status inf_status;
1085 struct cleanup *old_chain;
1086 CORE_ADDR funaddr;
1087 int using_gcc; /* Set to version of gcc in use, or zero if not gcc */
1088 CORE_ADDR real_pc;
1089 struct type *ftype = check_typedef (SYMBOL_TYPE (function));
1091 if (!target_has_execution)
1092 noprocess();
1094 save_inferior_status (&inf_status, 1);
1095 old_chain = make_cleanup (restore_inferior_status, &inf_status);
1097 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
1098 (and POP_FRAME for restoring them). (At least on most machines)
1099 they are saved on the stack in the inferior. */
1100 PUSH_DUMMY_FRAME;
1102 old_sp = sp = read_sp ();
1104 #if 1 INNER_THAN 2 /* Stack grows down */
1105 sp -= sizeof dummy1;
1106 start_sp = sp;
1107 #else /* Stack grows up */
1108 start_sp = sp;
1109 sp += sizeof dummy1;
1110 #endif
1112 funaddr = find_function_addr (function, &value_type);
1113 CHECK_TYPEDEF (value_type);
1116 struct block *b = block_for_pc (funaddr);
1117 /* If compiled without -g, assume GCC 2. */
1118 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
1121 /* Are we returning a value using a structure return or a normal
1122 value return? */
1124 struct_return = using_struct_return (function, funaddr, value_type,
1125 using_gcc);
1127 /* Create a call sequence customized for this function
1128 and the number of arguments for it. */
1129 for (i = 0; i < (int) (sizeof (dummy) / sizeof (dummy[0])); i++)
1130 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1131 REGISTER_SIZE,
1132 (ULONGEST)dummy[i]);
1134 #ifdef GDB_TARGET_IS_HPPA
1135 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1136 value_type, using_gcc);
1137 #else
1138 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1139 value_type, using_gcc);
1140 real_pc = start_sp;
1141 #endif
1143 #if CALL_DUMMY_LOCATION == ON_STACK
1144 write_memory (start_sp, (char *)dummy1, sizeof dummy1);
1145 #endif /* On stack. */
1147 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1148 /* Convex Unix prohibits executing in the stack segment. */
1149 /* Hope there is empty room at the top of the text segment. */
1151 extern CORE_ADDR text_end;
1152 static checked = 0;
1153 if (!checked)
1154 for (start_sp = text_end - sizeof dummy1; start_sp < text_end; ++start_sp)
1155 if (read_memory_integer (start_sp, 1) != 0)
1156 error ("text segment full -- no place to put call");
1157 checked = 1;
1158 sp = old_sp;
1159 real_pc = text_end - sizeof dummy1;
1160 write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1162 #endif /* Before text_end. */
1164 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
1166 extern CORE_ADDR text_end;
1167 int errcode;
1168 sp = old_sp;
1169 real_pc = text_end;
1170 errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1171 if (errcode != 0)
1172 error ("Cannot write text segment -- call_function failed");
1174 #endif /* After text_end. */
1176 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1177 real_pc = funaddr;
1178 #endif /* At entry point. */
1180 #ifdef lint
1181 sp = old_sp; /* It really is used, for some ifdef's... */
1182 #endif
1184 if (nargs < TYPE_NFIELDS (ftype))
1185 error ("too few arguments in function call");
1187 for (i = nargs - 1; i >= 0; i--)
1189 struct type *param_type;
1190 if (TYPE_NFIELDS (ftype) > i)
1191 param_type = TYPE_FIELD_TYPE (ftype, i);
1192 else
1193 param_type = 0;
1194 args[i] = value_arg_coerce (args[i], param_type);
1197 #if defined (REG_STRUCT_HAS_ADDR)
1199 /* This is a machine like the sparc, where we may need to pass a pointer
1200 to the structure, not the structure itself. */
1201 for (i = nargs - 1; i >= 0; i--)
1203 struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
1204 if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
1205 || TYPE_CODE (arg_type) == TYPE_CODE_UNION
1206 || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1207 || TYPE_CODE (arg_type) == TYPE_CODE_STRING
1208 || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
1209 || TYPE_CODE (arg_type) == TYPE_CODE_SET
1210 || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
1211 && TYPE_LENGTH (arg_type) > 8)
1213 && REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
1215 CORE_ADDR addr;
1216 int len = TYPE_LENGTH (arg_type);
1217 #ifdef STACK_ALIGN
1218 /* MVS 11/22/96: I think at least some of this stack_align code is
1219 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1220 a target-defined manner. */
1221 int aligned_len = STACK_ALIGN (len);
1222 #else
1223 int aligned_len = len;
1224 #endif
1225 #if !(1 INNER_THAN 2)
1226 /* The stack grows up, so the address of the thing we push
1227 is the stack pointer before we push it. */
1228 addr = sp;
1229 #else
1230 sp -= aligned_len;
1231 #endif
1232 /* Push the structure. */
1233 write_memory (sp, VALUE_CONTENTS (args[i]), len);
1234 #if 1 INNER_THAN 2
1235 /* The stack grows down, so the address of the thing we push
1236 is the stack pointer after we push it. */
1237 addr = sp;
1238 #else
1239 sp += aligned_len;
1240 #endif
1241 /* The value we're going to pass is the address of the thing
1242 we just pushed. */
1243 args[i] = value_from_longest (lookup_pointer_type (value_type),
1244 (LONGEST) addr);
1248 #endif /* REG_STRUCT_HAS_ADDR. */
1250 /* Reserve space for the return structure to be written on the
1251 stack, if necessary */
1253 if (struct_return)
1255 int len = TYPE_LENGTH (value_type);
1256 #ifdef STACK_ALIGN
1257 /* MVS 11/22/96: I think at least some of this stack_align code is
1258 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1259 a target-defined manner. */
1260 len = STACK_ALIGN (len);
1261 #endif
1262 #if 1 INNER_THAN 2
1263 sp -= len;
1264 struct_addr = sp;
1265 #else
1266 struct_addr = sp;
1267 sp += len;
1268 #endif
1271 #if defined(STACK_ALIGN) && (1 INNER_THAN 2)
1272 /* MVS 11/22/96: I think at least some of this stack_align code is
1273 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1274 a target-defined manner. */
1276 /* If stack grows down, we must leave a hole at the top. */
1277 int len = 0;
1279 for (i = nargs - 1; i >= 0; i--)
1280 len += TYPE_LENGTH (VALUE_TYPE (args[i]));
1281 #ifdef CALL_DUMMY_STACK_ADJUST
1282 len += CALL_DUMMY_STACK_ADJUST;
1283 #endif
1284 sp -= STACK_ALIGN (len) - len;
1286 #endif /* STACK_ALIGN */
1288 #ifdef PUSH_ARGUMENTS
1289 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1290 #else /* !PUSH_ARGUMENTS */
1291 for (i = nargs - 1; i >= 0; i--)
1292 sp = value_push (sp, args[i]);
1293 #endif /* !PUSH_ARGUMENTS */
1295 #ifdef PUSH_RETURN_ADDRESS /* for targets that use no CALL_DUMMY */
1296 /* There are a number of targets now which actually don't write any
1297 CALL_DUMMY instructions into the target, but instead just save the
1298 machine state, push the arguments, and jump directly to the callee
1299 function. Since this doesn't actually involve executing a JSR/BSR
1300 instruction, the return address must be set up by hand, either by
1301 pushing onto the stack or copying into a return-address register
1302 as appropriate. Formerly this has been done in PUSH_ARGUMENTS,
1303 but that's overloading its functionality a bit, so I'm making it
1304 explicit to do it here. */
1305 sp = PUSH_RETURN_ADDRESS(real_pc, sp);
1306 #endif /* PUSH_RETURN_ADDRESS */
1308 #if defined(STACK_ALIGN) && !(1 INNER_THAN 2)
1310 /* If stack grows up, we must leave a hole at the bottom, note
1311 that sp already has been advanced for the arguments! */
1312 #ifdef CALL_DUMMY_STACK_ADJUST
1313 sp += CALL_DUMMY_STACK_ADJUST;
1314 #endif
1315 sp = STACK_ALIGN (sp);
1317 #endif /* STACK_ALIGN */
1319 /* XXX This seems wrong. For stacks that grow down we shouldn't do
1320 anything here! */
1321 /* MVS 11/22/96: I think at least some of this stack_align code is
1322 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1323 a target-defined manner. */
1324 #ifdef CALL_DUMMY_STACK_ADJUST
1325 #if 1 INNER_THAN 2
1326 sp -= CALL_DUMMY_STACK_ADJUST;
1327 #endif
1328 #endif /* CALL_DUMMY_STACK_ADJUST */
1330 /* Store the address at which the structure is supposed to be
1331 written. Note that this (and the code which reserved the space
1332 above) assumes that gcc was used to compile this function. Since
1333 it doesn't cost us anything but space and if the function is pcc
1334 it will ignore this value, we will make that assumption.
1336 Also note that on some machines (like the sparc) pcc uses a
1337 convention like gcc's. */
1339 if (struct_return)
1340 STORE_STRUCT_RETURN (struct_addr, sp);
1342 /* Write the stack pointer. This is here because the statements above
1343 might fool with it. On SPARC, this write also stores the register
1344 window into the right place in the new stack frame, which otherwise
1345 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1346 write_sp (sp);
1349 char retbuf[REGISTER_BYTES];
1350 char *name;
1351 struct symbol *symbol;
1353 name = NULL;
1354 symbol = find_pc_function (funaddr);
1355 if (symbol)
1357 name = SYMBOL_SOURCE_NAME (symbol);
1359 else
1361 /* Try the minimal symbols. */
1362 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1364 if (msymbol)
1366 name = SYMBOL_SOURCE_NAME (msymbol);
1369 if (name == NULL)
1371 char format[80];
1372 sprintf (format, "at %s", local_hex_format ());
1373 name = alloca (80);
1374 /* FIXME-32x64: assumes funaddr fits in a long. */
1375 sprintf (name, format, (unsigned long) funaddr);
1378 /* Execute the stack dummy routine, calling FUNCTION.
1379 When it is done, discard the empty frame
1380 after storing the contents of all regs into retbuf. */
1381 if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1383 /* We stopped somewhere besides the call dummy. */
1385 /* If we did the cleanups, we would print a spurious error message
1386 (Unable to restore previously selected frame), would write the
1387 registers from the inf_status (which is wrong), and would do other
1388 wrong things (like set stop_bpstat to the wrong thing). */
1389 discard_cleanups (old_chain);
1390 /* Prevent memory leak. */
1391 bpstat_clear (&inf_status.stop_bpstat);
1393 /* The following error message used to say "The expression
1394 which contained the function call has been discarded." It
1395 is a hard concept to explain in a few words. Ideally, GDB
1396 would be able to resume evaluation of the expression when
1397 the function finally is done executing. Perhaps someday
1398 this will be implemented (it would not be easy). */
1400 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1401 a C++ name with arguments and stuff. */
1402 error ("\
1403 The program being debugged stopped while in a function called from GDB.\n\
1404 When the function (%s) is done executing, GDB will silently\n\
1405 stop (instead of continuing to evaluate the expression containing\n\
1406 the function call).", name);
1409 do_cleanups (old_chain);
1411 /* Figure out the value returned by the function. */
1412 return value_being_returned (value_type, retbuf, struct_return);
1415 #else /* no CALL_DUMMY. */
1416 value_ptr
1417 call_function_by_hand (function, nargs, args)
1418 value_ptr function;
1419 int nargs;
1420 value_ptr *args;
1422 error ("Cannot invoke functions on this machine.");
1424 #endif /* no CALL_DUMMY. */
1427 /* Create a value for an array by allocating space in the inferior, copying
1428 the data into that space, and then setting up an array value.
1430 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1431 populated from the values passed in ELEMVEC.
1433 The element type of the array is inherited from the type of the
1434 first element, and all elements must have the same size (though we
1435 don't currently enforce any restriction on their types). */
1437 value_ptr
1438 value_array (lowbound, highbound, elemvec)
1439 int lowbound;
1440 int highbound;
1441 value_ptr *elemvec;
1443 int nelem;
1444 int idx;
1445 unsigned int typelength;
1446 value_ptr val;
1447 struct type *rangetype;
1448 struct type *arraytype;
1449 CORE_ADDR addr;
1451 /* Validate that the bounds are reasonable and that each of the elements
1452 have the same size. */
1454 nelem = highbound - lowbound + 1;
1455 if (nelem <= 0)
1457 error ("bad array bounds (%d, %d)", lowbound, highbound);
1459 typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1460 for (idx = 1; idx < nelem; idx++)
1462 if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1464 error ("array elements must all be the same size");
1468 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1469 lowbound, highbound);
1470 arraytype = create_array_type ((struct type *) NULL,
1471 VALUE_TYPE (elemvec[0]), rangetype);
1473 if (!current_language->c_style_arrays)
1475 val = allocate_value (arraytype);
1476 for (idx = 0; idx < nelem; idx++)
1478 memcpy (VALUE_CONTENTS_RAW (val) + (idx * typelength),
1479 VALUE_CONTENTS (elemvec[idx]),
1480 typelength);
1482 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1483 return val;
1486 /* Allocate space to store the array in the inferior, and then initialize
1487 it by copying in each element. FIXME: Is it worth it to create a
1488 local buffer in which to collect each value and then write all the
1489 bytes in one operation? */
1491 addr = allocate_space_in_inferior (nelem * typelength);
1492 for (idx = 0; idx < nelem; idx++)
1494 write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1495 typelength);
1498 /* Create the array type and set up an array value to be evaluated lazily. */
1500 val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1501 return (val);
1504 /* Create a value for a string constant by allocating space in the inferior,
1505 copying the data into that space, and returning the address with type
1506 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1507 of characters.
1508 Note that string types are like array of char types with a lower bound of
1509 zero and an upper bound of LEN - 1. Also note that the string may contain
1510 embedded null bytes. */
1512 value_ptr
1513 value_string (ptr, len)
1514 char *ptr;
1515 int len;
1517 value_ptr val;
1518 int lowbound = current_language->string_lower_bound;
1519 struct type *rangetype = create_range_type ((struct type *) NULL,
1520 builtin_type_int,
1521 lowbound, len + lowbound - 1);
1522 struct type *stringtype
1523 = create_string_type ((struct type *) NULL, rangetype);
1524 CORE_ADDR addr;
1526 if (current_language->c_style_arrays == 0)
1528 val = allocate_value (stringtype);
1529 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1530 return val;
1534 /* Allocate space to store the string in the inferior, and then
1535 copy LEN bytes from PTR in gdb to that address in the inferior. */
1537 addr = allocate_space_in_inferior (len);
1538 write_memory (addr, ptr, len);
1540 val = value_at_lazy (stringtype, addr, NULL);
1541 return (val);
1544 value_ptr
1545 value_bitstring (ptr, len)
1546 char *ptr;
1547 int len;
1549 value_ptr val;
1550 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1551 0, len - 1);
1552 struct type *type = create_set_type ((struct type*) NULL, domain_type);
1553 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1554 val = allocate_value (type);
1555 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1556 return val;
1559 /* See if we can pass arguments in T2 to a function which takes arguments
1560 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1561 arguments need coercion of some sort, then the coerced values are written
1562 into T2. Return value is 0 if the arguments could be matched, or the
1563 position at which they differ if not.
1565 STATICP is nonzero if the T1 argument list came from a
1566 static member function.
1568 For non-static member functions, we ignore the first argument,
1569 which is the type of the instance variable. This is because we want
1570 to handle calls with objects from derived classes. This is not
1571 entirely correct: we should actually check to make sure that a
1572 requested operation is type secure, shouldn't we? FIXME. */
1574 static int
1575 typecmp (staticp, t1, t2)
1576 int staticp;
1577 struct type *t1[];
1578 value_ptr t2[];
1580 int i;
1582 if (t2 == 0)
1583 return 1;
1584 if (staticp && t1 == 0)
1585 return t2[1] != 0;
1586 if (t1 == 0)
1587 return 1;
1588 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1589 if (t1[!staticp] == 0) return 0;
1590 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1592 struct type *tt1, *tt2;
1593 if (! t2[i])
1594 return i+1;
1595 tt1 = check_typedef (t1[i]);
1596 tt2 = check_typedef (VALUE_TYPE(t2[i]));
1597 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1598 /* We should be doing hairy argument matching, as below. */
1599 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1601 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1602 t2[i] = value_coerce_array (t2[i]);
1603 else
1604 t2[i] = value_addr (t2[i]);
1605 continue;
1608 while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1609 && ( TYPE_CODE (tt2) == TYPE_CODE_ARRAY
1610 || TYPE_CODE (tt2) == TYPE_CODE_PTR))
1612 tt1 = check_typedef (TYPE_TARGET_TYPE(tt1));
1613 tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
1615 if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1616 /* Array to pointer is a `trivial conversion' according to the ARM. */
1618 /* We should be doing much hairier argument matching (see section 13.2
1619 of the ARM), but as a quick kludge, just check for the same type
1620 code. */
1621 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1622 return i+1;
1624 if (!t1[i]) return 0;
1625 return t2[i] ? i+1 : 0;
1628 /* Helper function used by value_struct_elt to recurse through baseclasses.
1629 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1630 and search in it assuming it has (class) type TYPE.
1631 If found, return value, else return NULL.
1633 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1634 look for a baseclass named NAME. */
1636 static value_ptr
1637 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
1638 char *name;
1639 register value_ptr arg1;
1640 int offset;
1641 register struct type *type;
1642 int looking_for_baseclass;
1644 int i;
1646 CHECK_TYPEDEF (type);
1648 if (! looking_for_baseclass)
1649 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1651 char *t_field_name = TYPE_FIELD_NAME (type, i);
1653 if (t_field_name && STREQ (t_field_name, name))
1655 value_ptr v;
1656 if (TYPE_FIELD_STATIC (type, i))
1658 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1659 struct symbol *sym =
1660 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1661 if (sym == NULL)
1662 error ("Internal error: could not find physical static variable named %s",
1663 phys_name);
1664 v = value_at (TYPE_FIELD_TYPE (type, i),
1665 SYMBOL_VALUE_ADDRESS (sym), SYMBOL_BFD_SECTION (sym));
1667 else
1668 v = value_primitive_field (arg1, offset, i, type);
1669 if (v == 0)
1670 error("there is no field named %s", name);
1671 return v;
1674 if (t_field_name
1675 && (t_field_name[0] == '\0'
1676 || (TYPE_CODE (type) == TYPE_CODE_UNION
1677 && STREQ (t_field_name, "else"))))
1679 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1680 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1681 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1683 /* Look for a match through the fields of an anonymous union,
1684 or anonymous struct. C++ provides anonymous unions.
1686 In the GNU Chill implementation of variant record types,
1687 each <alternative field> has an (anonymous) union type,
1688 each member of the union represents a <variant alternative>.
1689 Each <variant alternative> is represented as a struct,
1690 with a member for each <variant field>. */
1692 value_ptr v;
1693 int new_offset = offset;
1695 /* This is pretty gross. In G++, the offset in an anonymous
1696 union is relative to the beginning of the enclosing struct.
1697 In the GNU Chill implementation of variant records,
1698 the bitpos is zero in an anonymous union field, so we
1699 have to add the offset of the union here. */
1700 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1701 || (TYPE_NFIELDS (field_type) > 0
1702 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1703 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1705 v = search_struct_field (name, arg1, new_offset, field_type,
1706 looking_for_baseclass);
1707 if (v)
1708 return v;
1713 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1715 value_ptr v;
1716 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1717 /* If we are looking for baseclasses, this is what we get when we
1718 hit them. But it could happen that the base part's member name
1719 is not yet filled in. */
1720 int found_baseclass = (looking_for_baseclass
1721 && TYPE_BASECLASS_NAME (type, i) != NULL
1722 && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
1724 if (BASETYPE_VIA_VIRTUAL (type, i))
1726 int boffset = VALUE_OFFSET (arg1) + offset;
1727 boffset = baseclass_offset (type, i,
1728 VALUE_CONTENTS (arg1) + boffset,
1729 VALUE_ADDRESS (arg1) + boffset);
1730 if (boffset == -1)
1731 error ("virtual baseclass botch");
1732 if (found_baseclass)
1734 value_ptr v2 = allocate_value (basetype);
1735 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1736 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1737 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + offset + boffset;
1738 if (VALUE_LAZY (arg1))
1739 VALUE_LAZY (v2) = 1;
1740 else
1741 memcpy (VALUE_CONTENTS_RAW (v2),
1742 VALUE_CONTENTS_RAW (arg1) + offset + boffset,
1743 TYPE_LENGTH (basetype));
1744 return v2;
1746 v = search_struct_field (name, arg1, offset + boffset,
1747 TYPE_BASECLASS (type, i),
1748 looking_for_baseclass);
1750 else if (found_baseclass)
1751 v = value_primitive_field (arg1, offset, i, type);
1752 else
1753 v = search_struct_field (name, arg1,
1754 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1755 basetype, looking_for_baseclass);
1756 if (v) return v;
1758 return NULL;
1761 /* Helper function used by value_struct_elt to recurse through baseclasses.
1762 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1763 and search in it assuming it has (class) type TYPE.
1764 If found, return value, else if name matched and args not return (value)-1,
1765 else return NULL. */
1767 static value_ptr
1768 search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
1769 char *name;
1770 register value_ptr *arg1p, *args;
1771 int offset, *static_memfuncp;
1772 register struct type *type;
1774 int i;
1775 value_ptr v;
1776 int name_matched = 0;
1777 char dem_opname[64];
1779 CHECK_TYPEDEF (type);
1780 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1782 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1783 /* FIXME! May need to check for ARM demangling here */
1784 if (strncmp(t_field_name, "__", 2)==0 ||
1785 strncmp(t_field_name, "op", 2)==0 ||
1786 strncmp(t_field_name, "type", 4)==0 )
1788 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1789 t_field_name = dem_opname;
1790 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1791 t_field_name = dem_opname;
1793 if (t_field_name && STREQ (t_field_name, name))
1795 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1796 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1797 name_matched = 1;
1799 if (j > 0 && args == 0)
1800 error ("cannot resolve overloaded method `%s'", name);
1801 while (j >= 0)
1803 if (TYPE_FN_FIELD_STUB (f, j))
1804 check_stub_method (type, i, j);
1805 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1806 TYPE_FN_FIELD_ARGS (f, j), args))
1808 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1809 return value_virtual_fn_field (arg1p, f, j, type, offset);
1810 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1811 *static_memfuncp = 1;
1812 v = value_fn_field (arg1p, f, j, type, offset);
1813 if (v != NULL) return v;
1815 j--;
1820 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1822 int base_offset;
1824 if (BASETYPE_VIA_VIRTUAL (type, i))
1826 base_offset = VALUE_OFFSET (*arg1p) + offset;
1827 base_offset =
1828 baseclass_offset (type, i,
1829 VALUE_CONTENTS (*arg1p) + base_offset,
1830 VALUE_ADDRESS (*arg1p) + base_offset);
1831 if (base_offset == -1)
1832 error ("virtual baseclass botch");
1834 else
1836 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1838 v = search_struct_method (name, arg1p, args, base_offset + offset,
1839 static_memfuncp, TYPE_BASECLASS (type, i));
1840 if (v == (value_ptr) -1)
1842 name_matched = 1;
1844 else if (v)
1846 /* FIXME-bothner: Why is this commented out? Why is it here? */
1847 /* *arg1p = arg1_tmp;*/
1848 return v;
1851 if (name_matched) return (value_ptr) -1;
1852 else return NULL;
1855 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1856 extract the component named NAME from the ultimate target structure/union
1857 and return it as a value with its appropriate type.
1858 ERR is used in the error message if *ARGP's type is wrong.
1860 C++: ARGS is a list of argument types to aid in the selection of
1861 an appropriate method. Also, handle derived types.
1863 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1864 where the truthvalue of whether the function that was resolved was
1865 a static member function or not is stored.
1867 ERR is an error message to be printed in case the field is not found. */
1869 value_ptr
1870 value_struct_elt (argp, args, name, static_memfuncp, err)
1871 register value_ptr *argp, *args;
1872 char *name;
1873 int *static_memfuncp;
1874 char *err;
1876 register struct type *t;
1877 value_ptr v;
1879 COERCE_ARRAY (*argp);
1881 t = check_typedef (VALUE_TYPE (*argp));
1883 /* Follow pointers until we get to a non-pointer. */
1885 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1887 *argp = value_ind (*argp);
1888 /* Don't coerce fn pointer to fn and then back again! */
1889 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1890 COERCE_ARRAY (*argp);
1891 t = check_typedef (VALUE_TYPE (*argp));
1894 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1895 error ("not implemented: member type in value_struct_elt");
1897 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1898 && TYPE_CODE (t) != TYPE_CODE_UNION)
1899 error ("Attempt to extract a component of a value that is not a %s.", err);
1901 /* Assume it's not, unless we see that it is. */
1902 if (static_memfuncp)
1903 *static_memfuncp =0;
1905 if (!args)
1907 /* if there are no arguments ...do this... */
1909 /* Try as a field first, because if we succeed, there
1910 is less work to be done. */
1911 v = search_struct_field (name, *argp, 0, t, 0);
1912 if (v)
1913 return v;
1915 /* C++: If it was not found as a data field, then try to
1916 return it as a pointer to a method. */
1918 if (destructor_name_p (name, t))
1919 error ("Cannot get value of destructor");
1921 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1923 if (v == (value_ptr) -1)
1924 error ("Cannot take address of a method");
1925 else if (v == 0)
1927 if (TYPE_NFN_FIELDS (t))
1928 error ("There is no member or method named %s.", name);
1929 else
1930 error ("There is no member named %s.", name);
1932 return v;
1935 if (destructor_name_p (name, t))
1937 if (!args[1])
1939 /* Destructors are a special case. */
1940 int m_index, f_index;
1942 v = NULL;
1943 if (get_destructor_fn_field (t, &m_index, &f_index))
1945 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1946 f_index, NULL, 0);
1948 if (v == NULL)
1949 error ("could not find destructor function named %s.", name);
1950 else
1951 return v;
1953 else
1955 error ("destructor should not have any argument");
1958 else
1959 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1961 if (v == (value_ptr) -1)
1963 error("Argument list of %s mismatch with component in the structure.", name);
1965 else if (v == 0)
1967 /* See if user tried to invoke data as function. If so,
1968 hand it back. If it's not callable (i.e., a pointer to function),
1969 gdb should give an error. */
1970 v = search_struct_field (name, *argp, 0, t, 0);
1973 if (!v)
1974 error ("Structure has no component named %s.", name);
1975 return v;
1978 /* C++: return 1 is NAME is a legitimate name for the destructor
1979 of type TYPE. If TYPE does not have a destructor, or
1980 if NAME is inappropriate for TYPE, an error is signaled. */
1982 destructor_name_p (name, type)
1983 const char *name;
1984 const struct type *type;
1986 /* destructors are a special case. */
1988 if (name[0] == '~')
1990 char *dname = type_name_no_tag (type);
1991 char *cp = strchr (dname, '<');
1992 unsigned int len;
1994 /* Do not compare the template part for template classes. */
1995 if (cp == NULL)
1996 len = strlen (dname);
1997 else
1998 len = cp - dname;
1999 if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
2000 error ("name of destructor must equal name of class");
2001 else
2002 return 1;
2004 return 0;
2007 /* Helper function for check_field: Given TYPE, a structure/union,
2008 return 1 if the component named NAME from the ultimate
2009 target structure/union is defined, otherwise, return 0. */
2011 static int
2012 check_field_in (type, name)
2013 register struct type *type;
2014 const char *name;
2016 register int i;
2018 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2020 char *t_field_name = TYPE_FIELD_NAME (type, i);
2021 if (t_field_name && STREQ (t_field_name, name))
2022 return 1;
2025 /* C++: If it was not found as a data field, then try to
2026 return it as a pointer to a method. */
2028 /* Destructors are a special case. */
2029 if (destructor_name_p (name, type))
2031 int m_index, f_index;
2033 return get_destructor_fn_field (type, &m_index, &f_index);
2036 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2038 if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
2039 return 1;
2042 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2043 if (check_field_in (TYPE_BASECLASS (type, i), name))
2044 return 1;
2046 return 0;
2050 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2051 return 1 if the component named NAME from the ultimate
2052 target structure/union is defined, otherwise, return 0. */
2055 check_field (arg1, name)
2056 register value_ptr arg1;
2057 const char *name;
2059 register struct type *t;
2061 COERCE_ARRAY (arg1);
2063 t = VALUE_TYPE (arg1);
2065 /* Follow pointers until we get to a non-pointer. */
2067 for (;;)
2069 CHECK_TYPEDEF (t);
2070 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2071 break;
2072 t = TYPE_TARGET_TYPE (t);
2075 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2076 error ("not implemented: member type in check_field");
2078 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
2079 && TYPE_CODE (t) != TYPE_CODE_UNION)
2080 error ("Internal error: `this' is not an aggregate");
2082 return check_field_in (t, name);
2085 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2086 return the address of this member as a "pointer to member"
2087 type. If INTYPE is non-null, then it will be the type
2088 of the member we are looking for. This will help us resolve
2089 "pointers to member functions". This function is used
2090 to resolve user expressions of the form "DOMAIN::NAME". */
2092 value_ptr
2093 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
2094 struct type *domain, *curtype, *intype;
2095 int offset;
2096 char *name;
2098 register struct type *t = curtype;
2099 register int i;
2100 value_ptr v;
2102 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
2103 && TYPE_CODE (t) != TYPE_CODE_UNION)
2104 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2106 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2108 char *t_field_name = TYPE_FIELD_NAME (t, i);
2110 if (t_field_name && STREQ (t_field_name, name))
2112 if (TYPE_FIELD_STATIC (t, i))
2114 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
2115 struct symbol *sym =
2116 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
2117 if (sym == NULL)
2118 error ("Internal error: could not find physical static variable named %s",
2119 phys_name);
2120 return value_at (SYMBOL_TYPE (sym),
2121 SYMBOL_VALUE_ADDRESS (sym),
2122 SYMBOL_BFD_SECTION (sym));
2124 if (TYPE_FIELD_PACKED (t, i))
2125 error ("pointers to bitfield members not allowed");
2127 return value_from_longest
2128 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2129 domain)),
2130 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2134 /* C++: If it was not found as a data field, then try to
2135 return it as a pointer to a method. */
2137 /* Destructors are a special case. */
2138 if (destructor_name_p (name, t))
2140 error ("member pointers to destructors not implemented yet");
2143 /* Perform all necessary dereferencing. */
2144 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2145 intype = TYPE_TARGET_TYPE (intype);
2147 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2149 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2150 char dem_opname[64];
2152 if (strncmp(t_field_name, "__", 2)==0 ||
2153 strncmp(t_field_name, "op", 2)==0 ||
2154 strncmp(t_field_name, "type", 4)==0 )
2156 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
2157 t_field_name = dem_opname;
2158 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
2159 t_field_name = dem_opname;
2161 if (t_field_name && STREQ (t_field_name, name))
2163 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2164 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2166 if (intype == 0 && j > 1)
2167 error ("non-unique member `%s' requires type instantiation", name);
2168 if (intype)
2170 while (j--)
2171 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2172 break;
2173 if (j < 0)
2174 error ("no member function matches that type instantiation");
2176 else
2177 j = 0;
2179 if (TYPE_FN_FIELD_STUB (f, j))
2180 check_stub_method (t, i, j);
2181 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2183 return value_from_longest
2184 (lookup_reference_type
2185 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2186 domain)),
2187 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2189 else
2191 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2192 0, VAR_NAMESPACE, 0, NULL);
2193 if (s == NULL)
2195 v = 0;
2197 else
2199 v = read_var_value (s, 0);
2200 #if 0
2201 VALUE_TYPE (v) = lookup_reference_type
2202 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2203 domain));
2204 #endif
2206 return v;
2210 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2212 value_ptr v;
2213 int base_offset;
2215 if (BASETYPE_VIA_VIRTUAL (t, i))
2216 base_offset = 0;
2217 else
2218 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2219 v = value_struct_elt_for_reference (domain,
2220 offset + base_offset,
2221 TYPE_BASECLASS (t, i),
2222 name,
2223 intype);
2224 if (v)
2225 return v;
2227 return 0;
2230 /* C++: return the value of the class instance variable, if one exists.
2231 Flag COMPLAIN signals an error if the request is made in an
2232 inappropriate context. */
2234 value_ptr
2235 value_of_this (complain)
2236 int complain;
2238 struct symbol *func, *sym;
2239 struct block *b;
2240 int i;
2241 static const char funny_this[] = "this";
2242 value_ptr this;
2244 if (selected_frame == 0)
2245 if (complain)
2246 error ("no frame selected");
2247 else return 0;
2249 func = get_frame_function (selected_frame);
2250 if (!func)
2252 if (complain)
2253 error ("no `this' in nameless context");
2254 else return 0;
2257 b = SYMBOL_BLOCK_VALUE (func);
2258 i = BLOCK_NSYMS (b);
2259 if (i <= 0)
2260 if (complain)
2261 error ("no args, no `this'");
2262 else return 0;
2264 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2265 symbol instead of the LOC_ARG one (if both exist). */
2266 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
2267 if (sym == NULL)
2269 if (complain)
2270 error ("current stack frame not in method");
2271 else
2272 return NULL;
2275 this = read_var_value (sym, selected_frame);
2276 if (this == 0 && complain)
2277 error ("`this' argument at unknown address");
2278 return this;
2281 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2282 long, starting at LOWBOUND. The result has the same lower bound as
2283 the original ARRAY. */
2285 value_ptr
2286 value_slice (array, lowbound, length)
2287 value_ptr array;
2288 int lowbound, length;
2290 struct type *slice_range_type, *slice_type, *range_type;
2291 LONGEST lowerbound, upperbound, offset;
2292 value_ptr slice;
2293 struct type *array_type;
2294 array_type = check_typedef (VALUE_TYPE (array));
2295 COERCE_VARYING_ARRAY (array, array_type);
2296 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2297 && TYPE_CODE (array_type) != TYPE_CODE_STRING
2298 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2299 error ("cannot take slice of non-array");
2300 range_type = TYPE_INDEX_TYPE (array_type);
2301 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2302 error ("slice from bad array or bitstring");
2303 if (lowbound < lowerbound || length < 0
2304 || lowbound + length - 1 > upperbound
2305 /* Chill allows zero-length strings but not arrays. */
2306 || (current_language->la_language == language_chill
2307 && length == 0 && TYPE_CODE (array_type) == TYPE_CODE_ARRAY))
2308 error ("slice out of range");
2309 /* FIXME-type-allocation: need a way to free this type when we are
2310 done with it. */
2311 slice_range_type = create_range_type ((struct type*) NULL,
2312 TYPE_TARGET_TYPE (range_type),
2313 lowbound, lowbound + length - 1);
2314 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2316 int i;
2317 slice_type = create_set_type ((struct type*) NULL, slice_range_type);
2318 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2319 slice = value_zero (slice_type, not_lval);
2320 for (i = 0; i < length; i++)
2322 int element = value_bit_index (array_type,
2323 VALUE_CONTENTS (array),
2324 lowbound + i);
2325 if (element < 0)
2326 error ("internal error accessing bitstring");
2327 else if (element > 0)
2329 int j = i % TARGET_CHAR_BIT;
2330 if (BITS_BIG_ENDIAN)
2331 j = TARGET_CHAR_BIT - 1 - j;
2332 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2335 /* We should set the address, bitssize, and bitspos, so the clice
2336 can be used on the LHS, but that may require extensions to
2337 value_assign. For now, just leave as a non_lval. FIXME. */
2339 else
2341 struct type *element_type = TYPE_TARGET_TYPE (array_type);
2342 offset
2343 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2344 slice_type = create_array_type ((struct type*) NULL, element_type,
2345 slice_range_type);
2346 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2347 slice = allocate_value (slice_type);
2348 if (VALUE_LAZY (array))
2349 VALUE_LAZY (slice) = 1;
2350 else
2351 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2352 TYPE_LENGTH (slice_type));
2353 if (VALUE_LVAL (array) == lval_internalvar)
2354 VALUE_LVAL (slice) = lval_internalvar_component;
2355 else
2356 VALUE_LVAL (slice) = VALUE_LVAL (array);
2357 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2358 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2360 return slice;
2363 /* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2364 value as a fixed-length array. */
2366 value_ptr
2367 varying_to_slice (varray)
2368 value_ptr varray;
2370 struct type *vtype = check_typedef (VALUE_TYPE (varray));
2371 LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
2372 VALUE_CONTENTS (varray)
2373 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
2374 return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
2377 /* Create a value for a FORTRAN complex number. Currently most of
2378 the time values are coerced to COMPLEX*16 (i.e. a complex number
2379 composed of 2 doubles. This really should be a smarter routine
2380 that figures out precision inteligently as opposed to assuming
2381 doubles. FIXME: fmb */
2383 value_ptr
2384 value_literal_complex (arg1, arg2, type)
2385 value_ptr arg1;
2386 value_ptr arg2;
2387 struct type *type;
2389 register value_ptr val;
2390 struct type *real_type = TYPE_TARGET_TYPE (type);
2392 val = allocate_value (type);
2393 arg1 = value_cast (real_type, arg1);
2394 arg2 = value_cast (real_type, arg2);
2396 memcpy (VALUE_CONTENTS_RAW (val),
2397 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2398 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2399 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2400 return val;
2403 /* Cast a value into the appropriate complex data type. */
2405 static value_ptr
2406 cast_into_complex (type, val)
2407 struct type *type;
2408 register value_ptr val;
2410 struct type *real_type = TYPE_TARGET_TYPE (type);
2411 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2413 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2414 value_ptr re_val = allocate_value (val_real_type);
2415 value_ptr im_val = allocate_value (val_real_type);
2417 memcpy (VALUE_CONTENTS_RAW (re_val),
2418 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2419 memcpy (VALUE_CONTENTS_RAW (im_val),
2420 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2421 TYPE_LENGTH (val_real_type));
2423 return value_literal_complex (re_val, im_val, type);
2425 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2426 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2427 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2428 else
2429 error ("cannot cast non-number to complex");
2432 void
2433 _initialize_valops ()
2435 #if 0
2436 add_show_from_set
2437 (add_set_cmd ("abandon", class_support, var_boolean, (char *)&auto_abandon,
2438 "Set automatic abandonment of expressions upon failure.",
2439 &setlist),
2440 &showlist);
2441 #endif