1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
35 /* Local functions. */
37 static int typecmp
PARAMS ((int staticp
, struct type
*t1
[], value_ptr t2
[]));
39 static CORE_ADDR find_function_addr
PARAMS ((value_ptr
, struct type
**));
41 static CORE_ADDR value_push
PARAMS ((CORE_ADDR
, value_ptr
));
43 static CORE_ADDR value_arg_push
PARAMS ((CORE_ADDR
, value_ptr
));
45 static value_ptr search_struct_field
PARAMS ((char *, value_ptr
, int,
48 static value_ptr search_struct_method
PARAMS ((char *, value_ptr
*,
50 int, int *, struct type
*));
52 static int check_field_in
PARAMS ((struct type
*, const char *));
54 static CORE_ADDR allocate_space_in_inferior
PARAMS ((int));
56 static value_ptr f77_cast_into_complex
PARAMS ((struct type
*, value_ptr
));
58 static value_ptr f77_assign_from_literal_string
PARAMS ((value_ptr
,
61 static value_ptr f77_assign_from_literal_complex
PARAMS ((value_ptr
,
64 #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
67 /* Allocate NBYTES of space in the inferior using the inferior's malloc
68 and return a value that is a pointer to the allocated space. */
71 allocate_space_in_inferior (len
)
74 register value_ptr val
;
75 register struct symbol
*sym
;
76 struct minimal_symbol
*msymbol
;
81 /* Find the address of malloc in the inferior. */
83 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0, NULL
);
86 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
88 error ("\"malloc\" exists in this program but is not a function.");
90 val
= value_of_variable (sym
, NULL
);
94 msymbol
= lookup_minimal_symbol ("malloc", (struct objfile
*) NULL
);
97 type
= lookup_pointer_type (builtin_type_char
);
98 type
= lookup_function_type (type
);
99 type
= lookup_pointer_type (type
);
100 maddr
= (LONGEST
) SYMBOL_VALUE_ADDRESS (msymbol
);
101 val
= value_from_longest (type
, maddr
);
105 error ("evaluation of this expression requires the program to have a function \"malloc\".");
109 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) len
);
110 val
= call_function_by_hand (val
, 1, &blocklen
);
111 if (value_logical_not (val
))
113 error ("No memory available to program.");
115 return (value_as_long (val
));
118 /* Cast value ARG2 to type TYPE and return as a value.
119 More general than a C cast: accepts any two types of the same length,
120 and if ARG2 is an lvalue it can be cast into anything at all. */
121 /* In C++, casts may change pointer or object representations. */
124 value_cast (type
, arg2
)
126 register value_ptr arg2
;
128 register enum type_code code1
;
129 register enum type_code code2
;
132 if (VALUE_TYPE (arg2
) == type
)
135 COERCE_VARYING_ARRAY (arg2
);
137 /* Coerce arrays but not enums. Enums will work as-is
138 and coercing them would cause an infinite recursion. */
139 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_ENUM
)
142 code1
= TYPE_CODE (type
);
143 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
145 if (code1
== TYPE_CODE_COMPLEX
)
146 return f77_cast_into_complex (type
, arg2
);
147 if (code1
== TYPE_CODE_BOOL
)
148 code1
= TYPE_CODE_INT
;
149 if (code2
== TYPE_CODE_BOOL
)
150 code2
= TYPE_CODE_INT
;
152 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
153 || code2
== TYPE_CODE_ENUM
|| code2
== TYPE_CODE_RANGE
);
155 if ( code1
== TYPE_CODE_STRUCT
156 && code2
== TYPE_CODE_STRUCT
157 && TYPE_NAME (type
) != 0)
159 /* Look in the type of the source to see if it contains the
160 type of the target as a superclass. If so, we'll need to
161 offset the object in addition to changing its type. */
162 value_ptr v
= search_struct_field (type_name_no_tag (type
),
163 arg2
, 0, VALUE_TYPE (arg2
), 1);
166 VALUE_TYPE (v
) = type
;
170 if (code1
== TYPE_CODE_FLT
&& scalar
)
171 return value_from_double (type
, value_as_double (arg2
));
172 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
173 || code1
== TYPE_CODE_RANGE
)
174 && (scalar
|| code2
== TYPE_CODE_PTR
))
175 return value_from_longest (type
, value_as_long (arg2
));
176 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (VALUE_TYPE (arg2
)))
178 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
180 /* Look in the type of the source to see if it contains the
181 type of the target as a superclass. If so, we'll need to
182 offset the pointer rather than just change its type. */
183 struct type
*t1
= TYPE_TARGET_TYPE (type
);
184 struct type
*t2
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
185 if ( TYPE_CODE (t1
) == TYPE_CODE_STRUCT
186 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
187 && TYPE_NAME (t1
) != 0) /* if name unknown, can't have supercl */
189 value_ptr v
= search_struct_field (type_name_no_tag (t1
),
190 value_ind (arg2
), 0, t2
, 1);
194 VALUE_TYPE (v
) = type
;
198 /* No superclass found, just fall through to change ptr type. */
200 VALUE_TYPE (arg2
) = type
;
203 else if (chill_varying_type (type
))
205 struct type
*range1
, *range2
, *eltype1
, *eltype2
;
208 char *valaddr
, *valaddr_data
;
209 if (code2
== TYPE_CODE_BITSTRING
)
210 error ("not implemented: converting bitstring to varying type");
211 if ((code2
!= TYPE_CODE_ARRAY
&& code2
!= TYPE_CODE_STRING
)
212 || (eltype1
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 1)),
213 eltype2
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)),
214 (TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
215 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
216 error ("Invalid conversion to varying type");
217 range1
= TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type
, 1), 0);
218 range2
= TYPE_FIELD_TYPE (VALUE_TYPE (arg2
), 0);
219 count1
= TYPE_HIGH_BOUND (range1
) - TYPE_LOW_BOUND (range1
) + 1;
220 count2
= TYPE_HIGH_BOUND (range2
) - TYPE_LOW_BOUND (range2
) + 1;
222 error ("target varying type is too small");
223 val
= allocate_value (type
);
224 valaddr
= VALUE_CONTENTS_RAW (val
);
225 valaddr_data
= valaddr
+ TYPE_FIELD_BITPOS (type
, 1) / 8;
226 /* Set val's __var_length field to count2. */
227 store_signed_integer (valaddr
, TYPE_LENGTH (TYPE_FIELD_TYPE (type
, 0)),
229 /* Set the __var_data field to count2 elements copied from arg2. */
230 memcpy (valaddr_data
, VALUE_CONTENTS (arg2
),
231 count2
* TYPE_LENGTH (eltype2
));
232 /* Zero the rest of the __var_data field of val. */
233 memset (valaddr_data
+ count2
* TYPE_LENGTH (eltype2
), '\0',
234 (count1
- count2
) * TYPE_LENGTH (eltype2
));
237 else if (VALUE_LVAL (arg2
) == lval_memory
)
239 return value_at_lazy (type
, VALUE_ADDRESS (arg2
) + VALUE_OFFSET (arg2
));
241 else if (code1
== TYPE_CODE_VOID
)
243 return value_zero (builtin_type_void
, not_lval
);
247 error ("Invalid cast.");
252 /* Create a value of type TYPE that is zero, and return it. */
255 value_zero (type
, lv
)
259 register value_ptr val
= allocate_value (type
);
261 memset (VALUE_CONTENTS (val
), 0, TYPE_LENGTH (type
));
262 VALUE_LVAL (val
) = lv
;
267 /* Return a value with type TYPE located at ADDR.
269 Call value_at only if the data needs to be fetched immediately;
270 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
271 value_at_lazy instead. value_at_lazy simply records the address of
272 the data and sets the lazy-evaluation-required flag. The lazy flag
273 is tested in the VALUE_CONTENTS macro, which is used if and when
274 the contents are actually required. */
277 value_at (type
, addr
)
281 register value_ptr val
;
283 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
284 error ("Attempt to dereference a generic pointer.");
286 val
= allocate_value (type
);
288 read_memory (addr
, VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (type
));
290 VALUE_LVAL (val
) = lval_memory
;
291 VALUE_ADDRESS (val
) = addr
;
296 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
299 value_at_lazy (type
, addr
)
303 register value_ptr val
;
305 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
306 error ("Attempt to dereference a generic pointer.");
308 val
= allocate_value (type
);
310 VALUE_LVAL (val
) = lval_memory
;
311 VALUE_ADDRESS (val
) = addr
;
312 VALUE_LAZY (val
) = 1;
317 /* Called only from the VALUE_CONTENTS macro, if the current data for
318 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
319 data from the user's process, and clears the lazy flag to indicate
320 that the data in the buffer is valid.
322 If the value is zero-length, we avoid calling read_memory, which would
323 abort. We mark the value as fetched anyway -- all 0 bytes of it.
325 This function returns a value because it is used in the VALUE_CONTENTS
326 macro as part of an expression, where a void would not work. The
330 value_fetch_lazy (val
)
331 register value_ptr val
;
333 CORE_ADDR addr
= VALUE_ADDRESS (val
) + VALUE_OFFSET (val
);
335 if (TYPE_LENGTH (VALUE_TYPE (val
)))
336 read_memory (addr
, VALUE_CONTENTS_RAW (val
),
337 TYPE_LENGTH (VALUE_TYPE (val
)));
338 VALUE_LAZY (val
) = 0;
343 /* Store the contents of FROMVAL into the location of TOVAL.
344 Return a new value with the location of TOVAL and contents of FROMVAL. */
347 value_assign (toval
, fromval
)
348 register value_ptr toval
, fromval
;
350 register struct type
*type
;
351 register value_ptr val
;
352 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
355 if (current_language
->la_language
== language_fortran
)
357 /* Deal with literal assignment in F77. All composite (i.e. string
358 and complex number types) types are allocated in the superior
359 NOT the inferior. Therefore assigment is somewhat tricky. */
361 if (TYPE_CODE (VALUE_TYPE (fromval
)) == TYPE_CODE_LITERAL_STRING
)
362 return f77_assign_from_literal_string (toval
, fromval
);
364 if (TYPE_CODE (VALUE_TYPE (fromval
)) == TYPE_CODE_LITERAL_COMPLEX
)
365 return f77_assign_from_literal_complex (toval
, fromval
);
368 if (!toval
->modifiable
)
369 error ("Left operand of assignment is not a modifiable lvalue.");
371 COERCE_ARRAY (fromval
);
374 type
= VALUE_TYPE (toval
);
375 if (VALUE_LVAL (toval
) != lval_internalvar
)
376 fromval
= value_cast (type
, fromval
);
378 /* If TOVAL is a special machine register requiring conversion
379 of program values to a special raw format,
380 convert FROMVAL's contents now, with result in `raw_buffer',
381 and set USE_BUFFER to the number of bytes to write. */
383 #ifdef REGISTER_CONVERTIBLE
384 if (VALUE_REGNO (toval
) >= 0
385 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval
)))
387 int regno
= VALUE_REGNO (toval
);
388 if (REGISTER_CONVERTIBLE (regno
))
390 REGISTER_CONVERT_TO_RAW (VALUE_TYPE (fromval
), regno
,
391 VALUE_CONTENTS (fromval
), raw_buffer
);
392 use_buffer
= REGISTER_RAW_SIZE (regno
);
397 switch (VALUE_LVAL (toval
))
399 case lval_internalvar
:
400 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
403 case lval_internalvar_component
:
404 set_internalvar_component (VALUE_INTERNALVAR (toval
),
405 VALUE_OFFSET (toval
),
406 VALUE_BITPOS (toval
),
407 VALUE_BITSIZE (toval
),
412 if (VALUE_BITSIZE (toval
))
414 char buffer
[sizeof (LONGEST
)];
415 /* We assume that the argument to read_memory is in units of
416 host chars. FIXME: Is that correct? */
417 int len
= (VALUE_BITPOS (toval
)
418 + VALUE_BITSIZE (toval
)
422 if (len
> sizeof (LONGEST
))
423 error ("Can't handle bitfields which don't fit in a %d bit word.",
424 sizeof (LONGEST
) * HOST_CHAR_BIT
);
426 read_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
428 modify_field (buffer
, value_as_long (fromval
),
429 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
430 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
434 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
435 raw_buffer
, use_buffer
);
437 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
438 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
442 if (VALUE_BITSIZE (toval
))
444 char buffer
[sizeof (LONGEST
)];
445 int len
= REGISTER_RAW_SIZE (VALUE_REGNO (toval
));
447 if (len
> sizeof (LONGEST
))
448 error ("Can't handle bitfields in registers larger than %d bits.",
449 sizeof (LONGEST
) * HOST_CHAR_BIT
);
451 if (VALUE_BITPOS (toval
) + VALUE_BITSIZE (toval
)
452 > len
* HOST_CHAR_BIT
)
453 /* Getting this right would involve being very careful about
456 Can't handle bitfield which doesn't fit in a single register.");
458 read_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
460 modify_field (buffer
, value_as_long (fromval
),
461 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
462 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
466 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
467 raw_buffer
, use_buffer
);
470 /* Do any conversion necessary when storing this type to more
471 than one register. */
472 #ifdef REGISTER_CONVERT_FROM_TYPE
473 memcpy (raw_buffer
, VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
474 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval
), type
, raw_buffer
);
475 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
476 raw_buffer
, TYPE_LENGTH (type
));
478 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
479 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
482 /* Assigning to the stack pointer, frame pointer, and other
483 (architecture and calling convention specific) registers may
484 cause the frame cache to be out of date. We just do this
485 on all assignments to registers for simplicity; I doubt the slowdown
487 reinit_frame_cache ();
490 case lval_reg_frame_relative
:
492 /* value is stored in a series of registers in the frame
493 specified by the structure. Copy that value out, modify
494 it, and copy it back in. */
495 int amount_to_copy
= (VALUE_BITSIZE (toval
) ? 1 : TYPE_LENGTH (type
));
496 int reg_size
= REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval
));
497 int byte_offset
= VALUE_OFFSET (toval
) % reg_size
;
498 int reg_offset
= VALUE_OFFSET (toval
) / reg_size
;
501 /* Make the buffer large enough in all cases. */
502 char *buffer
= (char *) alloca (amount_to_copy
504 + MAX_REGISTER_RAW_SIZE
);
507 struct frame_info
*frame
;
509 /* Figure out which frame this is in currently. */
510 for (frame
= get_current_frame ();
511 frame
&& FRAME_FP (frame
) != VALUE_FRAME (toval
);
512 frame
= get_prev_frame (frame
))
516 error ("Value being assigned to is no longer active.");
518 amount_to_copy
+= (reg_size
- amount_to_copy
% reg_size
);
521 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
523 amount_copied
< amount_to_copy
;
524 amount_copied
+= reg_size
, regno
++)
526 get_saved_register (buffer
+ amount_copied
,
527 (int *)NULL
, (CORE_ADDR
*)NULL
,
528 frame
, regno
, (enum lval_type
*)NULL
);
531 /* Modify what needs to be modified. */
532 if (VALUE_BITSIZE (toval
))
533 modify_field (buffer
+ byte_offset
,
534 value_as_long (fromval
),
535 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
537 memcpy (buffer
+ byte_offset
, raw_buffer
, use_buffer
);
539 memcpy (buffer
+ byte_offset
, VALUE_CONTENTS (fromval
),
543 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
545 amount_copied
< amount_to_copy
;
546 amount_copied
+= reg_size
, regno
++)
552 /* Just find out where to put it. */
553 get_saved_register ((char *)NULL
,
554 &optim
, &addr
, frame
, regno
, &lval
);
557 error ("Attempt to assign to a value that was optimized out.");
558 if (lval
== lval_memory
)
559 write_memory (addr
, buffer
+ amount_copied
, reg_size
);
560 else if (lval
== lval_register
)
561 write_register_bytes (addr
, buffer
+ amount_copied
, reg_size
);
563 error ("Attempt to assign to an unmodifiable value.");
570 error ("Left operand of assignment is not an lvalue.");
573 /* Return a value just like TOVAL except with the contents of FROMVAL
574 (except in the case of the type if TOVAL is an internalvar). */
576 if (VALUE_LVAL (toval
) == lval_internalvar
577 || VALUE_LVAL (toval
) == lval_internalvar_component
)
579 type
= VALUE_TYPE (fromval
);
582 val
= allocate_value (type
);
583 memcpy (val
, toval
, VALUE_CONTENTS_RAW (val
) - (char *) val
);
584 memcpy (VALUE_CONTENTS_RAW (val
), VALUE_CONTENTS (fromval
),
586 VALUE_TYPE (val
) = type
;
591 /* Extend a value VAL to COUNT repetitions of its type. */
594 value_repeat (arg1
, count
)
598 register value_ptr val
;
600 if (VALUE_LVAL (arg1
) != lval_memory
)
601 error ("Only values in memory can be extended with '@'.");
603 error ("Invalid number %d of repetitions.", count
);
605 val
= allocate_repeat_value (VALUE_TYPE (arg1
), count
);
607 read_memory (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
),
608 VALUE_CONTENTS_RAW (val
),
609 TYPE_LENGTH (VALUE_TYPE (val
)) * count
);
610 VALUE_LVAL (val
) = lval_memory
;
611 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
);
617 value_of_variable (var
, b
)
622 struct frame_info
*frame
;
625 /* Use selected frame. */
629 frame
= block_innermost_frame (b
);
630 if (frame
== NULL
&& symbol_read_needs_frame (var
))
632 if (BLOCK_FUNCTION (b
) != NULL
633 && SYMBOL_NAME (BLOCK_FUNCTION (b
)) != NULL
)
634 error ("No frame is currently executing in block %s.",
635 SYMBOL_NAME (BLOCK_FUNCTION (b
)));
637 error ("No frame is currently executing in specified block");
640 val
= read_var_value (var
, frame
);
642 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
646 /* Given a value which is an array, return a value which is a pointer to its
647 first element, regardless of whether or not the array has a nonzero lower
650 FIXME: A previous comment here indicated that this routine should be
651 substracting the array's lower bound. It's not clear to me that this
652 is correct. Given an array subscripting operation, it would certainly
653 work to do the adjustment here, essentially computing:
655 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
657 However I believe a more appropriate and logical place to account for
658 the lower bound is to do so in value_subscript, essentially computing:
660 (&array[0] + ((index - lowerbound) * sizeof array[0]))
662 As further evidence consider what would happen with operations other
663 than array subscripting, where the caller would get back a value that
664 had an address somewhere before the actual first element of the array,
665 and the information about the lower bound would be lost because of
666 the coercion to pointer type.
670 value_coerce_array (arg1
)
673 register struct type
*type
;
675 if (VALUE_LVAL (arg1
) != lval_memory
)
676 error ("Attempt to take address of value not located in memory.");
678 /* Get type of elements. */
679 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
680 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRING
)
681 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
683 /* A phony array made by value_repeat.
684 Its type is the type of the elements, not an array type. */
685 type
= VALUE_TYPE (arg1
);
687 return value_from_longest (lookup_pointer_type (type
),
688 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
691 /* Given a value which is a function, return a value which is a pointer
695 value_coerce_function (arg1
)
699 if (VALUE_LVAL (arg1
) != lval_memory
)
700 error ("Attempt to take address of value not located in memory.");
702 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1
)),
703 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
706 /* Return a pointer value for the object for which ARG1 is the contents. */
712 struct type
*type
= VALUE_TYPE (arg1
);
713 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
715 /* Copy the value, but change the type from (T&) to (T*).
716 We keep the same location information, which is efficient,
717 and allows &(&X) to get the location containing the reference. */
718 value_ptr arg2
= value_copy (arg1
);
719 VALUE_TYPE (arg2
) = lookup_pointer_type (TYPE_TARGET_TYPE (type
));
722 if (current_language
->c_style_arrays
723 && (VALUE_REPEATED (arg1
)
724 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
))
725 return value_coerce_array (arg1
);
726 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
727 return value_coerce_function (arg1
);
729 if (VALUE_LVAL (arg1
) != lval_memory
)
730 error ("Attempt to take address of value not located in memory.");
732 return value_from_longest (lookup_pointer_type (type
),
733 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
736 /* Given a value of a pointer type, apply the C unary * operator to it. */
744 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
745 error ("not implemented: member types in value_ind");
747 /* Allow * on an integer so we can cast it to whatever we want.
748 This returns an int, which seems like the most C-like thing
749 to do. "long long" variables are rare enough that
750 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
751 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
752 return value_at (builtin_type_int
,
753 (CORE_ADDR
) value_as_long (arg1
));
754 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
755 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
756 value_as_pointer (arg1
));
757 error ("Attempt to take contents of a non-pointer value.");
758 return 0; /* For lint -- never reached */
761 /* Pushing small parts of stack frames. */
763 /* Push one word (the size of object that a register holds). */
768 unsigned LONGEST word
;
770 register int len
= REGISTER_SIZE
;
771 char buffer
[MAX_REGISTER_RAW_SIZE
];
773 store_unsigned_integer (buffer
, len
, word
);
776 write_memory (sp
, buffer
, len
);
777 #else /* stack grows upward */
778 write_memory (sp
, buffer
, len
);
780 #endif /* stack grows upward */
785 /* Push LEN bytes with data at BUFFER. */
788 push_bytes (sp
, buffer
, len
)
795 write_memory (sp
, buffer
, len
);
796 #else /* stack grows upward */
797 write_memory (sp
, buffer
, len
);
799 #endif /* stack grows upward */
804 /* Push onto the stack the specified value VALUE. */
808 register CORE_ADDR sp
;
811 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
815 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
816 #else /* stack grows upward */
817 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
819 #endif /* stack grows upward */
824 /* Perform the standard coercions that are specified
825 for arguments to be passed to C functions. */
828 value_arg_coerce (arg
)
831 register struct type
*type
;
833 /* FIXME: We should coerce this according to the prototype (if we have
834 one). Right now we do a little bit of this in typecmp(), but that
835 doesn't always get called. For example, if passing a ref to a function
836 without a prototype, we probably should de-reference it. Currently
839 if (TYPE_CODE (VALUE_TYPE (arg
)) == TYPE_CODE_ENUM
)
840 arg
= value_cast (builtin_type_unsigned_int
, arg
);
842 #if 1 /* FIXME: This is only a temporary patch. -fnf */
843 if (current_language
->c_style_arrays
844 && (VALUE_REPEATED (arg
)
845 || TYPE_CODE (VALUE_TYPE (arg
)) == TYPE_CODE_ARRAY
))
846 arg
= value_coerce_array (arg
);
847 if (TYPE_CODE (VALUE_TYPE (arg
)) == TYPE_CODE_FUNC
)
848 arg
= value_coerce_function (arg
);
851 type
= VALUE_TYPE (arg
);
853 if (TYPE_CODE (type
) == TYPE_CODE_INT
854 && TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
855 return value_cast (builtin_type_int
, arg
);
857 if (TYPE_CODE (type
) == TYPE_CODE_FLT
858 && TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_double
))
859 return value_cast (builtin_type_double
, arg
);
864 /* Push the value ARG, first coercing it as an argument
868 value_arg_push (sp
, arg
)
869 register CORE_ADDR sp
;
872 return value_push (sp
, value_arg_coerce (arg
));
875 /* Determine a function's address and its return type from its value.
876 Calls error() if the function is not valid for calling. */
879 find_function_addr (function
, retval_type
)
881 struct type
**retval_type
;
883 register struct type
*ftype
= VALUE_TYPE (function
);
884 register enum type_code code
= TYPE_CODE (ftype
);
885 struct type
*value_type
;
888 /* If it's a member function, just look at the function
891 /* Determine address to call. */
892 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
894 funaddr
= VALUE_ADDRESS (function
);
895 value_type
= TYPE_TARGET_TYPE (ftype
);
897 else if (code
== TYPE_CODE_PTR
)
899 funaddr
= value_as_pointer (function
);
900 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_FUNC
901 || TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_METHOD
)
903 #ifdef CONVERT_FROM_FUNC_PTR_ADDR
904 /* FIXME: This is a workaround for the unusual function
905 pointer representation on the RS/6000, see comment
906 in config/rs6000/tm-rs6000.h */
907 funaddr
= CONVERT_FROM_FUNC_PTR_ADDR (funaddr
);
909 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
912 value_type
= builtin_type_int
;
914 else if (code
== TYPE_CODE_INT
)
916 /* Handle the case of functions lacking debugging info.
917 Their values are characters since their addresses are char */
918 if (TYPE_LENGTH (ftype
) == 1)
919 funaddr
= value_as_pointer (value_addr (function
));
921 /* Handle integer used as address of a function. */
922 funaddr
= (CORE_ADDR
) value_as_long (function
);
924 value_type
= builtin_type_int
;
927 error ("Invalid data type for function to be called.");
929 *retval_type
= value_type
;
933 #if defined (CALL_DUMMY)
934 /* All this stuff with a dummy frame may seem unnecessarily complicated
935 (why not just save registers in GDB?). The purpose of pushing a dummy
936 frame which looks just like a real frame is so that if you call a
937 function and then hit a breakpoint (get a signal, etc), "backtrace"
938 will look right. Whether the backtrace needs to actually show the
939 stack at the time the inferior function was called is debatable, but
940 it certainly needs to not display garbage. So if you are contemplating
941 making dummy frames be different from normal frames, consider that. */
943 /* Perform a function call in the inferior.
944 ARGS is a vector of values of arguments (NARGS of them).
945 FUNCTION is a value, the function to be called.
946 Returns a value representing what the function returned.
947 May fail to return, if a breakpoint or signal is hit
948 during the execution of the function. */
951 call_function_by_hand (function
, nargs
, args
)
956 register CORE_ADDR sp
;
959 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
960 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
961 and remove any extra bytes which might exist because unsigned LONGEST is
962 bigger than REGISTER_SIZE. */
963 static unsigned LONGEST dummy
[] = CALL_DUMMY
;
964 char dummy1
[REGISTER_SIZE
* sizeof dummy
/ sizeof (unsigned LONGEST
)];
966 struct type
*value_type
;
967 unsigned char struct_return
;
968 CORE_ADDR struct_addr
;
969 struct inferior_status inf_status
;
970 struct cleanup
*old_chain
;
975 if (!target_has_execution
)
978 save_inferior_status (&inf_status
, 1);
979 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
981 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
982 (and POP_FRAME for restoring them). (At least on most machines)
983 they are saved on the stack in the inferior. */
986 old_sp
= sp
= read_sp ();
988 #if 1 INNER_THAN 2 /* Stack grows down */
991 #else /* Stack grows up */
996 funaddr
= find_function_addr (function
, &value_type
);
999 struct block
*b
= block_for_pc (funaddr
);
1000 /* If compiled without -g, assume GCC. */
1001 using_gcc
= b
== NULL
|| BLOCK_GCC_COMPILED (b
);
1004 /* Are we returning a value using a structure return or a normal
1007 struct_return
= using_struct_return (function
, funaddr
, value_type
,
1010 /* Create a call sequence customized for this function
1011 and the number of arguments for it. */
1012 for (i
= 0; i
< sizeof dummy
/ sizeof (dummy
[0]); i
++)
1013 store_unsigned_integer (&dummy1
[i
* REGISTER_SIZE
],
1015 (unsigned LONGEST
)dummy
[i
]);
1017 #ifdef GDB_TARGET_IS_HPPA
1018 real_pc
= FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
1019 value_type
, using_gcc
);
1021 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
1022 value_type
, using_gcc
);
1026 #if CALL_DUMMY_LOCATION == ON_STACK
1027 write_memory (start_sp
, (char *)dummy1
, sizeof dummy1
);
1028 #endif /* On stack. */
1030 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1031 /* Convex Unix prohibits executing in the stack segment. */
1032 /* Hope there is empty room at the top of the text segment. */
1034 extern CORE_ADDR text_end
;
1037 for (start_sp
= text_end
- sizeof dummy1
; start_sp
< text_end
; ++start_sp
)
1038 if (read_memory_integer (start_sp
, 1) != 0)
1039 error ("text segment full -- no place to put call");
1042 real_pc
= text_end
- sizeof dummy1
;
1043 write_memory (real_pc
, (char *)dummy1
, sizeof dummy1
);
1045 #endif /* Before text_end. */
1047 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
1049 extern CORE_ADDR text_end
;
1053 errcode
= target_write_memory (real_pc
, (char *)dummy1
, sizeof dummy1
);
1055 error ("Cannot write text segment -- call_function failed");
1057 #endif /* After text_end. */
1059 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1061 #endif /* At entry point. */
1064 sp
= old_sp
; /* It really is used, for some ifdef's... */
1068 /* If stack grows down, we must leave a hole at the top. */
1072 /* Reserve space for the return structure to be written on the
1073 stack, if necessary */
1076 len
+= TYPE_LENGTH (value_type
);
1078 for (i
= nargs
- 1; i
>= 0; i
--)
1079 len
+= TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args
[i
])));
1080 #ifdef CALL_DUMMY_STACK_ADJUST
1081 len
+= CALL_DUMMY_STACK_ADJUST
;
1084 sp
-= STACK_ALIGN (len
) - len
;
1086 sp
+= STACK_ALIGN (len
) - len
;
1089 #endif /* STACK_ALIGN */
1091 /* Reserve space for the return structure to be written on the
1092 stack, if necessary */
1097 sp
-= TYPE_LENGTH (value_type
);
1101 sp
+= TYPE_LENGTH (value_type
);
1105 #if defined (REG_STRUCT_HAS_ADDR)
1107 /* This is a machine like the sparc, where we may need to pass a pointer
1108 to the structure, not the structure itself. */
1109 for (i
= nargs
- 1; i
>= 0; i
--)
1110 if (TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_STRUCT
1111 && REG_STRUCT_HAS_ADDR (using_gcc
, VALUE_TYPE (args
[i
])))
1114 #if !(1 INNER_THAN 2)
1115 /* The stack grows up, so the address of the thing we push
1116 is the stack pointer before we push it. */
1119 /* Push the structure. */
1120 sp
= value_push (sp
, args
[i
]);
1122 /* The stack grows down, so the address of the thing we push
1123 is the stack pointer after we push it. */
1126 /* The value we're going to pass is the address of the thing
1128 args
[i
] = value_from_longest (lookup_pointer_type (value_type
),
1132 #endif /* REG_STRUCT_HAS_ADDR. */
1134 #ifdef PUSH_ARGUMENTS
1135 PUSH_ARGUMENTS(nargs
, args
, sp
, struct_return
, struct_addr
);
1136 #else /* !PUSH_ARGUMENTS */
1137 for (i
= nargs
- 1; i
>= 0; i
--)
1138 sp
= value_arg_push (sp
, args
[i
]);
1139 #endif /* !PUSH_ARGUMENTS */
1141 #ifdef CALL_DUMMY_STACK_ADJUST
1143 sp
-= CALL_DUMMY_STACK_ADJUST
;
1145 sp
+= CALL_DUMMY_STACK_ADJUST
;
1147 #endif /* CALL_DUMMY_STACK_ADJUST */
1149 /* Store the address at which the structure is supposed to be
1150 written. Note that this (and the code which reserved the space
1151 above) assumes that gcc was used to compile this function. Since
1152 it doesn't cost us anything but space and if the function is pcc
1153 it will ignore this value, we will make that assumption.
1155 Also note that on some machines (like the sparc) pcc uses a
1156 convention like gcc's. */
1159 STORE_STRUCT_RETURN (struct_addr
, sp
);
1161 /* Write the stack pointer. This is here because the statements above
1162 might fool with it. On SPARC, this write also stores the register
1163 window into the right place in the new stack frame, which otherwise
1164 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1168 char retbuf
[REGISTER_BYTES
];
1170 struct symbol
*symbol
;
1173 symbol
= find_pc_function (funaddr
);
1176 name
= SYMBOL_SOURCE_NAME (symbol
);
1180 /* Try the minimal symbols. */
1181 struct minimal_symbol
*msymbol
= lookup_minimal_symbol_by_pc (funaddr
);
1185 name
= SYMBOL_SOURCE_NAME (msymbol
);
1191 sprintf (format
, "at %s", local_hex_format ());
1193 /* FIXME-32x64: assumes funaddr fits in a long. */
1194 sprintf (name
, format
, (unsigned long) funaddr
);
1197 /* Execute the stack dummy routine, calling FUNCTION.
1198 When it is done, discard the empty frame
1199 after storing the contents of all regs into retbuf. */
1200 if (run_stack_dummy (real_pc
+ CALL_DUMMY_START_OFFSET
, retbuf
))
1202 /* We stopped somewhere besides the call dummy. */
1204 /* If we did the cleanups, we would print a spurious error message
1205 (Unable to restore previously selected frame), would write the
1206 registers from the inf_status (which is wrong), and would do other
1207 wrong things (like set stop_bpstat to the wrong thing). */
1208 discard_cleanups (old_chain
);
1209 /* Prevent memory leak. */
1210 bpstat_clear (&inf_status
.stop_bpstat
);
1212 /* The following error message used to say "The expression
1213 which contained the function call has been discarded." It
1214 is a hard concept to explain in a few words. Ideally, GDB
1215 would be able to resume evaluation of the expression when
1216 the function finally is done executing. Perhaps someday
1217 this will be implemented (it would not be easy). */
1219 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1220 a C++ name with arguments and stuff. */
1222 The program being debugged stopped while in a function called from GDB.\n\
1223 When the function (%s) is done executing, GDB will silently\n\
1224 stop (instead of continuing to evaluate the expression containing\n\
1225 the function call).", name
);
1228 do_cleanups (old_chain
);
1230 /* Figure out the value returned by the function. */
1231 return value_being_returned (value_type
, retbuf
, struct_return
);
1234 #else /* no CALL_DUMMY. */
1236 call_function_by_hand (function
, nargs
, args
)
1241 error ("Cannot invoke functions on this machine.");
1243 #endif /* no CALL_DUMMY. */
1246 /* Create a value for an array by allocating space in the inferior, copying
1247 the data into that space, and then setting up an array value.
1249 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1250 populated from the values passed in ELEMVEC.
1252 The element type of the array is inherited from the type of the
1253 first element, and all elements must have the same size (though we
1254 don't currently enforce any restriction on their types). */
1257 value_array (lowbound
, highbound
, elemvec
)
1266 struct type
*rangetype
;
1267 struct type
*arraytype
;
1270 /* Validate that the bounds are reasonable and that each of the elements
1271 have the same size. */
1273 nelem
= highbound
- lowbound
+ 1;
1276 error ("bad array bounds (%d, %d)", lowbound
, highbound
);
1278 typelength
= TYPE_LENGTH (VALUE_TYPE (elemvec
[0]));
1279 for (idx
= 0; idx
< nelem
; idx
++)
1281 if (TYPE_LENGTH (VALUE_TYPE (elemvec
[idx
])) != typelength
)
1283 error ("array elements must all be the same size");
1287 /* Allocate space to store the array in the inferior, and then initialize
1288 it by copying in each element. FIXME: Is it worth it to create a
1289 local buffer in which to collect each value and then write all the
1290 bytes in one operation? */
1292 addr
= allocate_space_in_inferior (nelem
* typelength
);
1293 for (idx
= 0; idx
< nelem
; idx
++)
1295 write_memory (addr
+ (idx
* typelength
), VALUE_CONTENTS (elemvec
[idx
]),
1299 /* Create the array type and set up an array value to be evaluated lazily. */
1301 rangetype
= create_range_type ((struct type
*) NULL
, builtin_type_int
,
1302 lowbound
, highbound
);
1303 arraytype
= create_array_type ((struct type
*) NULL
,
1304 VALUE_TYPE (elemvec
[0]), rangetype
);
1305 val
= value_at_lazy (arraytype
, addr
);
1309 /* Create a value for a string constant by allocating space in the inferior,
1310 copying the data into that space, and returning the address with type
1311 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1313 Note that string types are like array of char types with a lower bound of
1314 zero and an upper bound of LEN - 1. Also note that the string may contain
1315 embedded null bytes. */
1318 value_string (ptr
, len
)
1323 struct type
*rangetype
= create_range_type ((struct type
*) NULL
,
1324 builtin_type_int
, 0, len
- 1);
1325 struct type
*stringtype
1326 = create_string_type ((struct type
*) NULL
, rangetype
);
1329 if (current_language
->c_style_arrays
== 0)
1331 val
= allocate_value (stringtype
);
1332 memcpy (VALUE_CONTENTS_RAW (val
), ptr
, len
);
1337 /* Allocate space to store the string in the inferior, and then
1338 copy LEN bytes from PTR in gdb to that address in the inferior. */
1340 addr
= allocate_space_in_inferior (len
);
1341 write_memory (addr
, ptr
, len
);
1343 val
= value_at_lazy (stringtype
, addr
);
1348 value_bitstring (ptr
, len
)
1353 struct type
*domain_type
= create_range_type (NULL
, builtin_type_int
,
1355 struct type
*type
= create_set_type ((struct type
*) NULL
, domain_type
);
1356 TYPE_CODE (type
) = TYPE_CODE_BITSTRING
;
1357 val
= allocate_value (type
);
1358 memcpy (VALUE_CONTENTS_RAW (val
), ptr
, TYPE_LENGTH (type
) / TARGET_CHAR_BIT
);
1362 /* See if we can pass arguments in T2 to a function which takes arguments
1363 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1364 arguments need coercion of some sort, then the coerced values are written
1365 into T2. Return value is 0 if the arguments could be matched, or the
1366 position at which they differ if not.
1368 STATICP is nonzero if the T1 argument list came from a
1369 static member function.
1371 For non-static member functions, we ignore the first argument,
1372 which is the type of the instance variable. This is because we want
1373 to handle calls with objects from derived classes. This is not
1374 entirely correct: we should actually check to make sure that a
1375 requested operation is type secure, shouldn't we? FIXME. */
1378 typecmp (staticp
, t1
, t2
)
1387 if (staticp
&& t1
== 0)
1391 if (TYPE_CODE (t1
[0]) == TYPE_CODE_VOID
) return 0;
1392 if (t1
[!staticp
] == 0) return 0;
1393 for (i
= !staticp
; t1
[i
] && TYPE_CODE (t1
[i
]) != TYPE_CODE_VOID
; i
++)
1395 struct type
*tt1
, *tt2
;
1399 tt2
= VALUE_TYPE(t2
[i
]);
1400 if (TYPE_CODE (tt1
) == TYPE_CODE_REF
1401 /* We should be doing hairy argument matching, as below. */
1402 && (TYPE_CODE (TYPE_TARGET_TYPE (tt1
)) == TYPE_CODE (tt2
)))
1404 t2
[i
] = value_addr (t2
[i
]);
1408 while (TYPE_CODE (tt1
) == TYPE_CODE_PTR
1409 && (TYPE_CODE(tt2
)==TYPE_CODE_ARRAY
|| TYPE_CODE(tt2
)==TYPE_CODE_PTR
))
1411 tt1
= TYPE_TARGET_TYPE(tt1
);
1412 tt2
= TYPE_TARGET_TYPE(tt2
);
1414 if (TYPE_CODE(tt1
) == TYPE_CODE(tt2
)) continue;
1415 /* Array to pointer is a `trivial conversion' according to the ARM. */
1417 /* We should be doing much hairier argument matching (see section 13.2
1418 of the ARM), but as a quick kludge, just check for the same type
1420 if (TYPE_CODE (t1
[i
]) != TYPE_CODE (VALUE_TYPE (t2
[i
])))
1423 if (!t1
[i
]) return 0;
1424 return t2
[i
] ? i
+1 : 0;
1427 /* Helper function used by value_struct_elt to recurse through baseclasses.
1428 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1429 and search in it assuming it has (class) type TYPE.
1430 If found, return value, else return NULL.
1432 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1433 look for a baseclass named NAME. */
1436 search_struct_field (name
, arg1
, offset
, type
, looking_for_baseclass
)
1438 register value_ptr arg1
;
1440 register struct type
*type
;
1441 int looking_for_baseclass
;
1445 check_stub_type (type
);
1447 if (! looking_for_baseclass
)
1448 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1450 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1452 if (t_field_name
&& STREQ (t_field_name
, name
))
1455 if (TYPE_FIELD_STATIC (type
, i
))
1457 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, i
);
1458 struct symbol
*sym
=
1459 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1461 error ("Internal error: could not find physical static variable named %s",
1463 v
= value_at (TYPE_FIELD_TYPE (type
, i
),
1464 (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1467 v
= value_primitive_field (arg1
, offset
, i
, type
);
1469 error("there is no field named %s", name
);
1472 if (t_field_name
&& t_field_name
[0] == '\0'
1473 && TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_UNION
)
1475 /* Look for a match through the fields of an anonymous union. */
1477 v
= search_struct_field (name
, arg1
, offset
,
1478 TYPE_FIELD_TYPE (type
, i
),
1479 looking_for_baseclass
);
1485 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1488 /* If we are looking for baseclasses, this is what we get when we
1489 hit them. But it could happen that the base part's member name
1490 is not yet filled in. */
1491 int found_baseclass
= (looking_for_baseclass
1492 && TYPE_BASECLASS_NAME (type
, i
) != NULL
1493 && STREQ (name
, TYPE_BASECLASS_NAME (type
, i
)));
1495 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1498 /* Fix to use baseclass_offset instead. FIXME */
1499 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1502 error ("virtual baseclass botch");
1503 if (found_baseclass
)
1505 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
1506 looking_for_baseclass
);
1508 else if (found_baseclass
)
1509 v
= value_primitive_field (arg1
, offset
, i
, type
);
1511 v
= search_struct_field (name
, arg1
,
1512 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1513 TYPE_BASECLASS (type
, i
),
1514 looking_for_baseclass
);
1520 /* Helper function used by value_struct_elt to recurse through baseclasses.
1521 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1522 and search in it assuming it has (class) type TYPE.
1523 If found, return value, else if name matched and args not return (value)-1,
1524 else return NULL. */
1527 search_struct_method (name
, arg1p
, args
, offset
, static_memfuncp
, type
)
1529 register value_ptr
*arg1p
, *args
;
1530 int offset
, *static_memfuncp
;
1531 register struct type
*type
;
1535 int name_matched
= 0;
1536 char dem_opname
[64];
1538 check_stub_type (type
);
1539 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1541 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1542 if (strncmp(t_field_name
, "__", 2)==0 ||
1543 strncmp(t_field_name
, "op", 2)==0 ||
1544 strncmp(t_field_name
, "type", 4)==0 )
1546 if (cplus_demangle_opname(t_field_name
, dem_opname
, DMGL_ANSI
))
1547 t_field_name
= dem_opname
;
1548 else if (cplus_demangle_opname(t_field_name
, dem_opname
, 0))
1549 t_field_name
= dem_opname
;
1551 if (t_field_name
&& STREQ (t_field_name
, name
))
1553 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1554 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1557 if (j
> 0 && args
== 0)
1558 error ("cannot resolve overloaded method `%s'", name
);
1561 if (TYPE_FN_FIELD_STUB (f
, j
))
1562 check_stub_method (type
, i
, j
);
1563 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1564 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1566 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1567 return value_virtual_fn_field (arg1p
, f
, j
, type
, offset
);
1568 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1569 *static_memfuncp
= 1;
1570 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1571 if (v
!= NULL
) return v
;
1578 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1582 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1584 base_offset
= baseclass_offset (type
, i
, *arg1p
, offset
);
1585 if (base_offset
== -1)
1586 error ("virtual baseclass botch");
1590 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1592 v
= search_struct_method (name
, arg1p
, args
, base_offset
+ offset
,
1593 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1594 if (v
== (value_ptr
) -1)
1600 /* FIXME-bothner: Why is this commented out? Why is it here? */
1601 /* *arg1p = arg1_tmp;*/
1605 if (name_matched
) return (value_ptr
) -1;
1609 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1610 extract the component named NAME from the ultimate target structure/union
1611 and return it as a value with its appropriate type.
1612 ERR is used in the error message if *ARGP's type is wrong.
1614 C++: ARGS is a list of argument types to aid in the selection of
1615 an appropriate method. Also, handle derived types.
1617 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1618 where the truthvalue of whether the function that was resolved was
1619 a static member function or not is stored.
1621 ERR is an error message to be printed in case the field is not found. */
1624 value_struct_elt (argp
, args
, name
, static_memfuncp
, err
)
1625 register value_ptr
*argp
, *args
;
1627 int *static_memfuncp
;
1630 register struct type
*t
;
1633 COERCE_ARRAY (*argp
);
1635 t
= VALUE_TYPE (*argp
);
1637 /* Follow pointers until we get to a non-pointer. */
1639 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1641 *argp
= value_ind (*argp
);
1642 /* Don't coerce fn pointer to fn and then back again! */
1643 if (TYPE_CODE (VALUE_TYPE (*argp
)) != TYPE_CODE_FUNC
)
1644 COERCE_ARRAY (*argp
);
1645 t
= VALUE_TYPE (*argp
);
1648 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1649 error ("not implemented: member type in value_struct_elt");
1651 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1652 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1653 error ("Attempt to extract a component of a value that is not a %s.", err
);
1655 /* Assume it's not, unless we see that it is. */
1656 if (static_memfuncp
)
1657 *static_memfuncp
=0;
1661 /* if there are no arguments ...do this... */
1663 /* Try as a field first, because if we succeed, there
1664 is less work to be done. */
1665 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1669 /* C++: If it was not found as a data field, then try to
1670 return it as a pointer to a method. */
1672 if (destructor_name_p (name
, t
))
1673 error ("Cannot get value of destructor");
1675 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1677 if (v
== (value_ptr
) -1)
1678 error ("Cannot take address of a method");
1681 if (TYPE_NFN_FIELDS (t
))
1682 error ("There is no member or method named %s.", name
);
1684 error ("There is no member named %s.", name
);
1689 if (destructor_name_p (name
, t
))
1693 /* destructors are a special case. */
1694 v
= value_fn_field (NULL
, TYPE_FN_FIELDLIST1 (t
, 0),
1695 TYPE_FN_FIELDLIST_LENGTH (t
, 0), 0, 0);
1696 if (!v
) error("could not find destructor function named %s.", name
);
1701 error ("destructor should not have any argument");
1705 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1707 if (v
== (value_ptr
) -1)
1709 error("Argument list of %s mismatch with component in the structure.", name
);
1713 /* See if user tried to invoke data as function. If so,
1714 hand it back. If it's not callable (i.e., a pointer to function),
1715 gdb should give an error. */
1716 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1720 error ("Structure has no component named %s.", name
);
1724 /* C++: return 1 is NAME is a legitimate name for the destructor
1725 of type TYPE. If TYPE does not have a destructor, or
1726 if NAME is inappropriate for TYPE, an error is signaled. */
1728 destructor_name_p (name
, type
)
1730 const struct type
*type
;
1732 /* destructors are a special case. */
1736 char *dname
= type_name_no_tag (type
);
1737 char *cp
= strchr (dname
, '<');
1740 /* Do not compare the template part for template classes. */
1742 len
= strlen (dname
);
1745 if (strlen (name
+ 1) != len
|| !STREQN (dname
, name
+ 1, len
))
1746 error ("name of destructor must equal name of class");
1753 /* Helper function for check_field: Given TYPE, a structure/union,
1754 return 1 if the component named NAME from the ultimate
1755 target structure/union is defined, otherwise, return 0. */
1758 check_field_in (type
, name
)
1759 register struct type
*type
;
1764 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1766 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1767 if (t_field_name
&& STREQ (t_field_name
, name
))
1771 /* C++: If it was not found as a data field, then try to
1772 return it as a pointer to a method. */
1774 /* Destructors are a special case. */
1775 if (destructor_name_p (name
, type
))
1778 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1780 if (STREQ (TYPE_FN_FIELDLIST_NAME (type
, i
), name
))
1784 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1785 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
1792 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1793 return 1 if the component named NAME from the ultimate
1794 target structure/union is defined, otherwise, return 0. */
1797 check_field (arg1
, name
)
1798 register value_ptr arg1
;
1801 register struct type
*t
;
1803 COERCE_ARRAY (arg1
);
1805 t
= VALUE_TYPE (arg1
);
1807 /* Follow pointers until we get to a non-pointer. */
1809 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1810 t
= TYPE_TARGET_TYPE (t
);
1812 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1813 error ("not implemented: member type in check_field");
1815 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1816 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1817 error ("Internal error: `this' is not an aggregate");
1819 return check_field_in (t
, name
);
1822 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1823 return the address of this member as a "pointer to member"
1824 type. If INTYPE is non-null, then it will be the type
1825 of the member we are looking for. This will help us resolve
1826 "pointers to member functions". This function is used
1827 to resolve user expressions of the form "DOMAIN::NAME". */
1830 value_struct_elt_for_reference (domain
, offset
, curtype
, name
, intype
)
1831 struct type
*domain
, *curtype
, *intype
;
1835 register struct type
*t
= curtype
;
1839 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1840 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1841 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
1843 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1845 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1847 if (t_field_name
&& STREQ (t_field_name
, name
))
1849 if (TYPE_FIELD_STATIC (t
, i
))
1851 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (t
, i
);
1852 struct symbol
*sym
=
1853 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1855 error ("Internal error: could not find physical static variable named %s",
1857 return value_at (SYMBOL_TYPE (sym
),
1858 (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1860 if (TYPE_FIELD_PACKED (t
, i
))
1861 error ("pointers to bitfield members not allowed");
1863 return value_from_longest
1864 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t
, i
),
1866 offset
+ (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1870 /* C++: If it was not found as a data field, then try to
1871 return it as a pointer to a method. */
1873 /* Destructors are a special case. */
1874 if (destructor_name_p (name
, t
))
1876 error ("member pointers to destructors not implemented yet");
1879 /* Perform all necessary dereferencing. */
1880 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1881 intype
= TYPE_TARGET_TYPE (intype
);
1883 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1885 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (t
, i
);
1886 char dem_opname
[64];
1888 if (strncmp(t_field_name
, "__", 2)==0 ||
1889 strncmp(t_field_name
, "op", 2)==0 ||
1890 strncmp(t_field_name
, "type", 4)==0 )
1892 if (cplus_demangle_opname(t_field_name
, dem_opname
, DMGL_ANSI
))
1893 t_field_name
= dem_opname
;
1894 else if (cplus_demangle_opname(t_field_name
, dem_opname
, 0))
1895 t_field_name
= dem_opname
;
1897 if (t_field_name
&& STREQ (t_field_name
, name
))
1899 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1900 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1902 if (intype
== 0 && j
> 1)
1903 error ("non-unique member `%s' requires type instantiation", name
);
1907 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1910 error ("no member function matches that type instantiation");
1915 if (TYPE_FN_FIELD_STUB (f
, j
))
1916 check_stub_method (t
, i
, j
);
1917 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1919 return value_from_longest
1920 (lookup_reference_type
1921 (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1923 (LONGEST
) METHOD_PTR_FROM_VOFFSET
1924 (TYPE_FN_FIELD_VOFFSET (f
, j
)));
1928 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1929 0, VAR_NAMESPACE
, 0, NULL
);
1936 v
= read_var_value (s
, 0);
1938 VALUE_TYPE (v
) = lookup_reference_type
1939 (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1947 for (i
= TYPE_N_BASECLASSES (t
) - 1; i
>= 0; i
--)
1952 if (BASETYPE_VIA_VIRTUAL (t
, i
))
1955 base_offset
= TYPE_BASECLASS_BITPOS (t
, i
) / 8;
1956 v
= value_struct_elt_for_reference (domain
,
1957 offset
+ base_offset
,
1958 TYPE_BASECLASS (t
, i
),
1967 /* C++: return the value of the class instance variable, if one exists.
1968 Flag COMPLAIN signals an error if the request is made in an
1969 inappropriate context. */
1972 value_of_this (complain
)
1975 struct symbol
*func
, *sym
;
1978 static const char funny_this
[] = "this";
1981 if (selected_frame
== 0)
1983 error ("no frame selected");
1986 func
= get_frame_function (selected_frame
);
1990 error ("no `this' in nameless context");
1994 b
= SYMBOL_BLOCK_VALUE (func
);
1995 i
= BLOCK_NSYMS (b
);
1998 error ("no args, no `this'");
2001 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2002 symbol instead of the LOC_ARG one (if both exist). */
2003 sym
= lookup_block_symbol (b
, funny_this
, VAR_NAMESPACE
);
2007 error ("current stack frame not in method");
2012 this = read_var_value (sym
, selected_frame
);
2013 if (this == 0 && complain
)
2014 error ("`this' argument at unknown address");
2018 /* Create a value for a literal string. We copy data into a local
2019 (NOT inferior's memory) buffer, and then set up an array value.
2021 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
2022 populated from the values passed in ELEMVEC.
2024 The element type of the array is inherited from the type of the
2025 first element, and all elements must have the same size (though we
2026 don't currently enforce any restriction on their types). */
2029 f77_value_literal_string (lowbound
, highbound
, elemvec
)
2037 register value_ptr val
;
2038 struct type
*rangetype
;
2039 struct type
*arraytype
;
2042 /* Validate that the bounds are reasonable and that each of the elements
2043 have the same size. */
2045 nelem
= highbound
- lowbound
+ 1;
2047 error ("bad array bounds (%d, %d)", lowbound
, highbound
);
2048 typelength
= TYPE_LENGTH (VALUE_TYPE (elemvec
[0]));
2049 for (idx
= 0; idx
< nelem
; idx
++)
2051 if (TYPE_LENGTH (VALUE_TYPE (elemvec
[idx
])) != typelength
)
2052 error ("array elements must all be the same size");
2055 /* Make sure we are dealing with characters */
2057 if (typelength
!= 1)
2058 error ("Found a non character type in a literal string ");
2060 /* Allocate space to store the array */
2062 addr
= xmalloc (nelem
);
2063 for (idx
= 0; idx
< nelem
; idx
++)
2065 memcpy (addr
+ (idx
), VALUE_CONTENTS (elemvec
[idx
]), 1);
2068 rangetype
= create_range_type ((struct type
*) NULL
, builtin_type_int
,
2069 lowbound
, highbound
);
2071 arraytype
= f77_create_literal_string_type ((struct type
*) NULL
,
2074 val
= allocate_value (arraytype
);
2076 /* Make sure that this the rest of the world knows that this is
2077 a standard literal string, not one that is a substring of
2080 VALUE_SUBSTRING_MEMADDR (val
) = (CORE_ADDR
)0;
2082 VALUE_LAZY (val
) = 0;
2083 VALUE_LITERAL_DATA (val
) = addr
;
2085 /* Since this is a standard literal string with no real lval,
2086 make sure that value_lval indicates this fact */
2088 VALUE_LVAL (val
) = not_lval
;
2092 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2093 long, starting at LOWBOUND. The result has the same lower bound as
2094 the original ARRAY. */
2097 value_slice (array
, lowbound
, length
)
2099 int lowbound
, length
;
2101 if (TYPE_CODE (VALUE_TYPE (array
)) == TYPE_CODE_BITSTRING
)
2102 error ("not implemented - bitstring slice");
2103 if (TYPE_CODE (VALUE_TYPE (array
)) != TYPE_CODE_ARRAY
2104 && TYPE_CODE (VALUE_TYPE (array
)) != TYPE_CODE_STRING
)
2105 error ("cannot take slice of non-array");
2108 struct type
*slice_range_type
, *slice_type
;
2110 struct type
*range_type
= TYPE_FIELD_TYPE (VALUE_TYPE (array
), 0);
2111 struct type
*element_type
= TYPE_TARGET_TYPE (VALUE_TYPE (array
));
2112 int lowerbound
= TYPE_LOW_BOUND (range_type
);
2113 int upperbound
= TYPE_HIGH_BOUND (range_type
);
2114 int offset
= (lowbound
- lowerbound
) * TYPE_LENGTH (element_type
);
2115 if (lowbound
< lowerbound
|| length
< 0
2116 || lowbound
+ length
- 1 > upperbound
)
2117 error ("slice out of range");
2118 slice_range_type
= create_range_type ((struct type
*) NULL
,
2119 TYPE_TARGET_TYPE (range_type
),
2121 lowerbound
+ length
- 1);
2122 slice_type
= create_array_type ((struct type
*) NULL
, element_type
,
2124 TYPE_CODE (slice_type
) = TYPE_CODE (VALUE_TYPE (array
));
2125 slice
= allocate_value (slice_type
);
2126 if (VALUE_LAZY (array
))
2127 VALUE_LAZY (slice
) = 1;
2129 memcpy (VALUE_CONTENTS (slice
), VALUE_CONTENTS (array
) + offset
,
2130 TYPE_LENGTH (slice_type
));
2131 if (VALUE_LVAL (array
) == lval_internalvar
)
2132 VALUE_LVAL (slice
) = lval_internalvar_component
;
2134 VALUE_LVAL (slice
) = VALUE_LVAL (array
);
2135 VALUE_ADDRESS (slice
) = VALUE_ADDRESS (array
);
2136 VALUE_OFFSET (slice
) = VALUE_OFFSET (array
) + offset
;
2141 /* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2142 value as a fixed-length array. */
2145 varying_to_slice (varray
)
2148 struct type
*vtype
= VALUE_TYPE (varray
);
2149 LONGEST length
= unpack_long (TYPE_FIELD_TYPE (vtype
, 0),
2150 VALUE_CONTENTS (varray
)
2151 + TYPE_FIELD_BITPOS (vtype
, 0) / 8);
2152 return value_slice (value_primitive_field (varray
, 0, 1, vtype
), 0, length
);
2155 /* Create a value for a substring. We copy data into a local
2156 (NOT inferior's memory) buffer, and then set up an array value.
2158 The array bounds for the string are (1:(to-from +1))
2159 The elements of the string are all characters. */
2162 f77_value_substring (str
, from
, to
)
2168 register value_ptr val
;
2169 struct type
*rangetype
;
2170 struct type
*arraytype
;
2171 struct internalvar
*var
;
2174 /* Validate that the bounds are reasonable. */
2176 nelem
= to
- from
+ 1;
2178 error ("bad substring bounds (%d, %d)", from
, to
);
2180 rangetype
= create_range_type ((struct type
*) NULL
, builtin_type_int
,
2183 arraytype
= f77_create_literal_string_type ((struct type
*) NULL
,
2186 val
= allocate_value (arraytype
);
2188 /* Allocate space to store the substring array */
2190 addr
= xmalloc (nelem
);
2192 /* Copy over the data */
2194 /* In case we ever try to use this substring on the LHS of an assignment
2195 remember where the SOURCE substring begins, for lval_memory
2196 types this ptr is to a location in legal inferior memory,
2197 for lval_internalvars it is a ptr. to superior memory. This
2198 helps us out later when we do assigments like:
2200 set var ARR(2:3) = 'ab'
2205 if (VALUE_LVAL (str
) == lval_memory
)
2207 if (VALUE_SUBSTRING_MEMADDR (str
) == (CORE_ADDR
)0)
2209 /* This is a regular lval_memory string located in the
2212 VALUE_SUBSTRING_MEMADDR (val
) = VALUE_ADDRESS (str
) + (from
- 1);
2213 target_read_memory (VALUE_SUBSTRING_MEMADDR (val
), addr
, nelem
);
2219 /* str is a substring allocated in the superior. Just
2222 VALUE_SUBSTRING_MYADDR (val
) = VALUE_LITERAL_DATA(str
)+(from
- 1);
2223 memcpy(addr
, VALUE_SUBSTRING_MYADDR (val
), nelem
);
2225 error ("Cannot get substrings of substrings");
2230 if (VALUE_LVAL(str
) == lval_internalvar
)
2232 /* Internal variables of type TYPE_CODE_LITERAL_STRING
2233 have their data located in the superior
2234 process not the inferior */
2236 var
= VALUE_INTERNALVAR (str
);
2238 if (VALUE_SUBSTRING_MEMADDR (str
) == (CORE_ADDR
)0)
2239 VALUE_SUBSTRING_MYADDR (val
) =
2240 ((char *) VALUE_LITERAL_DATA (var
->value
)) + (from
- 1);
2243 VALUE_SUBSTRING_MYADDR (val
) = VALUE_LITERAL_DATA(str
)+(from
-1);
2245 error ("Cannot get substrings of substrings");
2247 memcpy (addr
, VALUE_SUBSTRING_MYADDR (val
), nelem
);
2250 error ("Substrings can not be applied to this data item");
2252 VALUE_LAZY (val
) = 0;
2253 VALUE_LITERAL_DATA (val
) = addr
;
2255 /* This literal string's *data* is located in the superior BUT
2256 we do need to know where it came from (i.e. was the source
2257 string an internalvar or a regular lval_memory variable), so
2258 we set the lval field to indicate this. This will be useful
2259 when we use this value on the LHS of an expr. */
2261 VALUE_LVAL (val
) = VALUE_LVAL (str
);
2265 /* Create a value for a FORTRAN complex number. Currently most of
2266 the time values are coerced to COMPLEX*16 (i.e. a complex number
2267 composed of 2 doubles. This really should be a smarter routine
2268 that figures out precision inteligently as opposed to assuming
2269 doubles. FIXME: fmb */
2272 f77_value_literal_complex (arg1
, arg2
, size
)
2277 struct type
*complex_type
;
2278 register value_ptr val
;
2281 if (size
!= 8 && size
!= 16 && size
!= 32)
2282 error ("Cannot create number of type 'complex*%d'", size
);
2284 /* If either value comprising a complex number is a non-floating
2285 type, cast to double. */
2287 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_FLT
)
2288 arg1
= value_cast (builtin_type_f_real_s8
, arg1
);
2290 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_FLT
)
2291 arg2
= value_cast (builtin_type_f_real_s8
, arg2
);
2293 complex_type
= f77_create_literal_complex_type (VALUE_TYPE (arg1
),
2296 /* FIXME: does f77_create_literal_complex_type need to do something with
2303 val
= allocate_value (complex_type
);
2305 /* Now create a pointer to enough memory to hold the the two args */
2307 addr
= xmalloc (TYPE_LENGTH (complex_type
));
2309 /* Copy over the two components */
2311 memcpy (addr
, VALUE_CONTENTS_RAW (arg1
), TYPE_LENGTH (VALUE_TYPE (arg1
)));
2313 memcpy (addr
+ TYPE_LENGTH (VALUE_TYPE (arg1
)), VALUE_CONTENTS_RAW (arg2
),
2314 TYPE_LENGTH (VALUE_TYPE (arg2
)));
2316 VALUE_ADDRESS (val
) = 0; /* Not located in the inferior */
2317 VALUE_LAZY (val
) = 0;
2318 VALUE_LITERAL_DATA (val
) = addr
;
2320 /* Since this is a literal value, make sure that value_lval indicates
2323 VALUE_LVAL (val
) = not_lval
;
2327 /* Cast a value into the appropriate complex data type. Only works
2328 if both values are complex. */
2331 f77_cast_into_complex (type
, val
)
2333 register value_ptr val
;
2335 register enum type_code valcode
;
2338 register value_ptr piece1
, piece2
;
2342 valcode
= TYPE_CODE (VALUE_TYPE (val
));
2344 /* This casting will only work if the right hand side is
2345 either a regular complex type or a literal complex type.
2346 I.e: this casting is only for size adjustment of
2347 complex numbers not anything else. */
2349 if ((valcode
!= TYPE_CODE_COMPLEX
) &&
2350 (valcode
!= TYPE_CODE_LITERAL_COMPLEX
))
2351 error ("Cannot cast from a non complex type!");
2353 lenfrom
= TYPE_LENGTH (VALUE_TYPE (val
));
2354 lento
= TYPE_LENGTH (type
);
2356 if (lento
== lenfrom
)
2357 error ("Value to be cast is already of type %s", TYPE_NAME (type
));
2359 if (lento
== 32 || lenfrom
== 32)
2360 error ("Casting into/out of complex*32 unsupported");
2366 /* Since we have excluded lenfrom == 32 and
2367 lenfrom == 16, it MUST be 8 */
2369 if (valcode
== TYPE_CODE_LITERAL_COMPLEX
)
2371 /* Located in superior's memory. Routine should
2372 deal with both real literal complex numbers
2373 as well as internal vars */
2375 /* Grab the two 4 byte reals that make up the complex*8 */
2377 tmp_f
= *((float *) VALUE_LITERAL_DATA (val
));
2379 piece1
= value_from_double(builtin_type_f_real_s8
,tmp_f
);
2381 tmp_f
= *((float *) (((char *) VALUE_LITERAL_DATA (val
))
2384 piece2
= value_from_double (builtin_type_f_real_s8
, tmp_f
);
2388 /* Located in inferior memory, so first we need
2389 to read the 2 floats that make up the 8 byte
2390 complex we are are casting from */
2392 read_memory ((CORE_ADDR
) VALUE_CONTENTS (val
),
2393 (char *) &tmp_f
, sizeof(float));
2395 piece1
= value_from_double (builtin_type_f_real_s8
, tmp_f
);
2397 read_memory ((CORE_ADDR
) VALUE_CONTENTS (val
) + sizeof(float),
2398 (char *) &tmp_f
, sizeof(float));
2400 piece2
= value_from_double (builtin_type_f_real_s8
, tmp_f
);
2402 return f77_value_literal_complex (piece1
, piece2
, 16);
2407 /* Since we have excluded lenfrom == 32 and
2408 lenfrom == 8, it MUST be 16. NOTE: in this
2409 case data may be since we are dropping precison */
2411 if (valcode
== TYPE_CODE_LITERAL_COMPLEX
)
2413 /* Located in superior's memory. Routine should
2414 deal with both real literal complex numbers
2415 as well as internal vars */
2417 /* Grab the two 8 byte reals that make up the complex*16 */
2419 tmp_d
= *((double *) VALUE_LITERAL_DATA (val
));
2421 piece1
= value_from_double (builtin_type_f_real
, tmp_d
);
2423 tmp_d
= *((double *) (((char *) VALUE_LITERAL_DATA (val
))
2426 piece2
= value_from_double (builtin_type_f_real
, tmp_d
);
2430 /* Located in inferior memory, so first we need to read the
2431 2 floats that make up the 8 byte complex we are are
2434 read_memory ((CORE_ADDR
) VALUE_CONTENTS (val
),
2435 (char *) &tmp_d
, sizeof(double));
2437 piece1
= value_from_double (builtin_type_f_real
, tmp_d
);
2439 read_memory ((CORE_ADDR
) VALUE_CONTENTS (val
) + sizeof(double),
2440 (char *) &tmp_f
, sizeof(double));
2442 piece2
= value_from_double (builtin_type_f_real
, tmp_d
);
2444 return f77_value_literal_complex (piece1
, piece2
, 8);
2448 error ("Invalid F77 complex number cast");
2452 /* The following function is called in order to assign
2453 a literal F77 array to either an internal GDB variable
2454 or to a real array variable in the inferior.
2455 This function is necessary because in F77, literal
2456 arrays are allocated in the superior's memory space
2457 NOT the inferior's. This function provides a way to
2458 get the F77 stuff to work without messing with the
2459 way C deals with this issue. NOTE: we are assuming
2460 that all F77 array literals are STRING array literals. F77
2461 users have no good way of expressing non-string
2464 This routine now also handles assignment TO literal strings
2465 in the peculiar case of substring assignments of the
2473 f77_assign_from_literal_string (toval
, fromval
)
2474 register value_ptr toval
, fromval
;
2476 register struct type
*type
= VALUE_TYPE (toval
);
2477 register value_ptr val
;
2478 struct internalvar
*var
;
2483 lenfrom
= TYPE_LENGTH (VALUE_TYPE (fromval
));
2484 lento
= TYPE_LENGTH (VALUE_TYPE (toval
));
2486 if ((VALUE_LVAL (toval
) == lval_internalvar
2487 || VALUE_LVAL (toval
) == lval_memory
)
2488 && VALUE_SUBSTRING_START (toval
) != 0)
2490 /* We are assigning TO a substring type. This is of the form:
2494 The result of this will be a modified toval not a brand new
2495 value. This is high F77 weirdness. */
2497 /* Simply overwrite the relevant memory, wherever it
2498 exists. Use standard F77 character assignment rules
2499 (if len(toval) > len(fromval) pad with blanks,
2500 if len(toval) < len(fromval) truncate else just copy. */
2502 if (VALUE_LVAL (toval
) == lval_internalvar
)
2504 /* Memory in superior. */
2505 var
= VALUE_INTERNALVAR (toval
);
2506 memcpy ((char *) VALUE_SUBSTRING_START (toval
),
2507 (char *) VALUE_LITERAL_DATA (fromval
),
2508 (lento
> lenfrom
) ? lenfrom
: lento
);
2510 /* Check to see if we have to pad. */
2512 if (lento
> lenfrom
)
2514 memset((char *) VALUE_SUBSTRING_START(toval
) + lenfrom
,
2515 ' ', lento
- lenfrom
);
2520 /* Memory in inferior. */
2521 write_memory ((CORE_ADDR
) VALUE_SUBSTRING_START (toval
),
2522 (char *) VALUE_LITERAL_DATA (fromval
),
2523 (lento
> lenfrom
) ? lenfrom
: lento
);
2525 /* Check to see if we have to pad. */
2527 if (lento
> lenfrom
)
2529 c
= alloca (lento
-lenfrom
);
2530 memset (c
, ' ', lento
- lenfrom
);
2532 tmp_addr
= VALUE_SUBSTRING_START (toval
) + lenfrom
;
2533 write_memory (tmp_addr
, c
, lento
- lenfrom
);
2540 if (VALUE_LVAL (toval
) == lval_internalvar
)
2541 type
= VALUE_TYPE (fromval
);
2543 val
= allocate_value (type
);
2545 switch (VALUE_LVAL (toval
))
2547 case lval_internalvar
:
2549 /* Internal variables are funny. Their value information
2550 is stored in the location.internalvar sub structure. */
2552 var
= VALUE_INTERNALVAR (toval
);
2554 /* The item in toval is a regular internal variable
2555 and this assignment is of the form:
2557 set var $foo = 'hello' */
2559 /* First free up any old stuff in this internalvar. */
2561 free (VALUE_LITERAL_DATA (var
->value
));
2562 VALUE_LITERAL_DATA (var
->value
) = 0;
2563 VALUE_LAZY (var
->value
) = 0; /* Disable lazy fetches since this
2564 is not located in inferior. */
2566 /* Copy over the relevant value data from 'fromval' */
2568 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
2570 /* Now replicate the VALUE_LITERAL_DATA field so that
2571 we may later safely de-allocate fromval. */
2573 VALUE_LITERAL_DATA (var
->value
) =
2574 malloc (TYPE_LENGTH (VALUE_TYPE (fromval
)));
2576 memcpy((char *) VALUE_LITERAL_DATA (var
->value
),
2577 (char *) VALUE_LITERAL_DATA (fromval
),
2580 /* Copy over all relevant value data from 'toval'. into
2581 the structure to returned */
2583 memcpy (val
, toval
, sizeof(struct value
));
2585 /* Lastly copy the pointer to the area where the
2586 internalvar data is stored to the VALUE_CONTENTS field.
2587 This will be a helpful shortcut for printout
2590 VALUE_LITERAL_DATA (val
) = VALUE_LITERAL_DATA (var
->value
);
2595 /* We are copying memory from the local (superior)
2596 literal string to a legitimate address in the
2597 inferior. VALUE_ADDRESS is the address in
2598 the inferior. VALUE_OFFSET is not used because
2599 structs do not exist in F77. */
2601 /* Copy over all relevant value data from 'toval'. */
2603 memcpy (val
, toval
, sizeof(struct value
));
2605 write_memory ((CORE_ADDR
) VALUE_ADDRESS (val
),
2606 (char *) VALUE_LITERAL_DATA (fromval
),
2607 (lento
> lenfrom
) ? lenfrom
: lento
);
2609 /* Check to see if we have to pad */
2611 if (lento
> lenfrom
)
2613 c
= alloca (lento
- lenfrom
);
2614 memset (c
, ' ', lento
- lenfrom
);
2615 tmp_addr
= VALUE_ADDRESS (val
) + lenfrom
;
2616 write_memory (tmp_addr
, c
, lento
- lenfrom
);
2621 error ("Unknown lval type in f77_assign_from_literal_string");
2624 /* Now free up the transient literal string's storage. */
2626 free (VALUE_LITERAL_DATA (fromval
));
2628 VALUE_TYPE (val
) = type
;
2635 /* The following function is called in order to assign a literal F77
2636 complex to either an internal GDB variable or to a real complex
2637 variable in the inferior. This function is necessary because in F77,
2638 composite literals are allocated in the superior's memory space
2639 NOT the inferior's. This function provides a way to get the F77 stuff
2640 to work without messing with the way C deals with this issue. */
2643 f77_assign_from_literal_complex (toval
, fromval
)
2644 register value_ptr toval
, fromval
;
2646 register struct type
*type
= VALUE_TYPE (toval
);
2647 register value_ptr val
;
2648 struct internalvar
*var
;
2650 double tmp_double
= 0;
2652 if (VALUE_LVAL (toval
) == lval_internalvar
)
2653 type
= VALUE_TYPE (fromval
);
2655 /* Allocate a value node for the result. */
2657 val
= allocate_value (type
);
2659 if (VALUE_LVAL (toval
) == lval_internalvar
)
2661 /* Internal variables are funny. Their value information
2662 is stored in the location.internalvar sub structure. */
2664 var
= VALUE_INTERNALVAR (toval
);
2666 /* First free up any old stuff in this internalvar. */
2668 free (VALUE_LITERAL_DATA (var
->value
));
2669 VALUE_LITERAL_DATA (var
->value
) = 0;
2670 VALUE_LAZY (var
->value
) = 0; /* Disable lazy fetches since
2671 this is not located in inferior. */
2673 /* Copy over the relevant value data from 'fromval'. */
2675 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
2677 /* Now replicate the VALUE_LITERAL_DATA field so that
2678 we may later safely de-allocate fromval. */
2680 VALUE_LITERAL_DATA (var
->value
) =
2681 malloc (TYPE_LENGTH (VALUE_TYPE (fromval
)));
2683 memcpy ((char *) VALUE_LITERAL_DATA (var
->value
),
2684 (char *) VALUE_LITERAL_DATA (fromval
),
2685 TYPE_LENGTH (VALUE_TYPE (fromval
)));
2687 /* Copy over all relevant value data from 'toval' into the
2688 structure to be returned. */
2690 memcpy (val
, toval
, sizeof(struct value
));
2694 /* We are copying memory from the local (superior) process to a
2695 legitimate address in the inferior. VALUE_ADDRESS is the
2696 address in the inferior. */
2698 /* Copy over all relevant value data from 'toval'. */
2700 memcpy (val
, toval
, sizeof(struct value
));
2702 if (TYPE_LENGTH (VALUE_TYPE (fromval
))
2703 > TYPE_LENGTH (VALUE_TYPE (toval
)))
2705 /* Since all literals are actually complex*16 types, deal with
2706 the case when one tries to assign a literal to a complex*8. */
2708 if ((TYPE_LENGTH(VALUE_TYPE(fromval
)) == 16) &&
2709 (TYPE_LENGTH(VALUE_TYPE(toval
)) == 8))
2711 tmp_double
= *((double *) VALUE_LITERAL_DATA (fromval
));
2713 tmp_float
= (float) tmp_double
;
2715 write_memory (VALUE_ADDRESS(val
),
2716 (char *) &tmp_float
, sizeof(float));
2718 tmp_double
= *((double *)
2719 (((char *) VALUE_LITERAL_DATA (fromval
))
2722 tmp_float
= (float) tmp_double
;
2724 write_memory(VALUE_ADDRESS(val
) + sizeof(float),
2725 (char *) &tmp_float
, sizeof(float));
2728 error ("Cannot assign literal complex to variable!");
2732 write_memory (VALUE_ADDRESS (val
),
2733 (char *) VALUE_LITERAL_DATA (fromval
),
2734 TYPE_LENGTH (VALUE_TYPE (fromval
)));
2738 /* Now free up the transient literal string's storage */
2740 free (VALUE_LITERAL_DATA (fromval
));
2742 VALUE_TYPE (val
) = type
;