1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* Local functions. */
32 static value
search_struct_field ();
34 /* Cast value ARG2 to type TYPE and return as a value.
35 More general than a C cast: accepts any two types of the same length,
36 and if ARG2 is an lvalue it can be cast into anything at all. */
37 /* In C++, casts may change pointer representations. */
40 value_cast (type
, arg2
)
44 register enum type_code code1
;
45 register enum type_code code2
;
48 /* Coerce arrays but not enums. Enums will work as-is
49 and coercing them would cause an infinite recursion. */
50 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_ENUM
)
53 code1
= TYPE_CODE (type
);
54 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
55 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
56 || code2
== TYPE_CODE_ENUM
);
58 if (code1
== TYPE_CODE_FLT
&& scalar
)
59 return value_from_double (type
, value_as_double (arg2
));
60 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
)
61 && (scalar
|| code2
== TYPE_CODE_PTR
))
62 return value_from_longest (type
, value_as_long (arg2
));
63 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (VALUE_TYPE (arg2
)))
65 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
67 /* Look in the type of the source to see if it contains the
68 type of the target as a superclass. If so, we'll need to
69 offset the pointer rather than just change its type. */
70 struct type
*t1
= TYPE_TARGET_TYPE (type
);
71 struct type
*t2
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
72 if ( TYPE_CODE (t1
) == TYPE_CODE_STRUCT
73 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
74 && TYPE_NAME (t1
) != 0) /* if name unknown, can't have supercl */
76 value v
= search_struct_field (type_name_no_tag (t1
),
77 value_ind (arg2
), 0, t2
, 1);
81 VALUE_TYPE (v
) = type
;
85 /* No superclass found, just fall through to change ptr type. */
87 VALUE_TYPE (arg2
) = type
;
90 else if (VALUE_LVAL (arg2
) == lval_memory
)
92 return value_at_lazy (type
, VALUE_ADDRESS (arg2
) + VALUE_OFFSET (arg2
));
94 else if (code1
== TYPE_CODE_VOID
)
96 return value_zero (builtin_type_void
, not_lval
);
100 error ("Invalid cast.");
105 /* Create a value of type TYPE that is zero, and return it. */
108 value_zero (type
, lv
)
112 register value val
= allocate_value (type
);
114 memset (VALUE_CONTENTS (val
), 0, TYPE_LENGTH (type
));
115 VALUE_LVAL (val
) = lv
;
120 /* Return a value with type TYPE located at ADDR.
122 Call value_at only if the data needs to be fetched immediately;
123 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
124 value_at_lazy instead. value_at_lazy simply records the address of
125 the data and sets the lazy-evaluation-required flag. The lazy flag
126 is tested in the VALUE_CONTENTS macro, which is used if and when
127 the contents are actually required. */
130 value_at (type
, addr
)
134 register value val
= allocate_value (type
);
136 read_memory (addr
, VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (type
));
138 VALUE_LVAL (val
) = lval_memory
;
139 VALUE_ADDRESS (val
) = addr
;
144 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
147 value_at_lazy (type
, addr
)
151 register value val
= allocate_value (type
);
153 VALUE_LVAL (val
) = lval_memory
;
154 VALUE_ADDRESS (val
) = addr
;
155 VALUE_LAZY (val
) = 1;
160 /* Called only from the VALUE_CONTENTS macro, if the current data for
161 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
162 data from the user's process, and clears the lazy flag to indicate
163 that the data in the buffer is valid.
165 If the value is zero-length, we avoid calling read_memory, which would
166 abort. We mark the value as fetched anyway -- all 0 bytes of it.
168 This function returns a value because it is used in the VALUE_CONTENTS
169 macro as part of an expression, where a void would not work. The
173 value_fetch_lazy (val
)
176 CORE_ADDR addr
= VALUE_ADDRESS (val
) + VALUE_OFFSET (val
);
178 if (TYPE_LENGTH (VALUE_TYPE (val
)))
179 read_memory (addr
, VALUE_CONTENTS_RAW (val
),
180 TYPE_LENGTH (VALUE_TYPE (val
)));
181 VALUE_LAZY (val
) = 0;
186 /* Store the contents of FROMVAL into the location of TOVAL.
187 Return a new value with the location of TOVAL and contents of FROMVAL. */
190 value_assign (toval
, fromval
)
191 register value toval
, fromval
;
193 register struct type
*type
= VALUE_TYPE (toval
);
195 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
196 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
199 COERCE_ARRAY (fromval
);
202 if (VALUE_LVAL (toval
) != lval_internalvar
)
203 fromval
= value_cast (type
, fromval
);
205 /* If TOVAL is a special machine register requiring conversion
206 of program values to a special raw format,
207 convert FROMVAL's contents now, with result in `raw_buffer',
208 and set USE_BUFFER to the number of bytes to write. */
210 if (VALUE_REGNO (toval
) >= 0
211 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval
)))
213 int regno
= VALUE_REGNO (toval
);
214 if (VALUE_TYPE (fromval
) != REGISTER_VIRTUAL_TYPE (regno
))
215 fromval
= value_cast (REGISTER_VIRTUAL_TYPE (regno
), fromval
);
216 memcpy (virtual_buffer
, VALUE_CONTENTS (fromval
),
217 REGISTER_VIRTUAL_SIZE (regno
));
218 target_convert_from_virtual (regno
, virtual_buffer
, raw_buffer
);
219 use_buffer
= REGISTER_RAW_SIZE (regno
);
222 switch (VALUE_LVAL (toval
))
224 case lval_internalvar
:
225 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
228 case lval_internalvar_component
:
229 set_internalvar_component (VALUE_INTERNALVAR (toval
),
230 VALUE_OFFSET (toval
),
231 VALUE_BITPOS (toval
),
232 VALUE_BITSIZE (toval
),
237 if (VALUE_BITSIZE (toval
))
239 int v
; /* FIXME, this won't work for large bitfields */
240 read_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
242 modify_field ((char *) &v
, (int) value_as_long (fromval
),
243 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
244 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
245 (char *)&v
, sizeof v
);
248 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
249 raw_buffer
, use_buffer
);
251 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
252 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
256 if (VALUE_BITSIZE (toval
))
260 read_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
261 (char *) &v
, sizeof v
);
262 modify_field ((char *) &v
, (int) value_as_long (fromval
),
263 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
264 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
265 (char *) &v
, sizeof v
);
268 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
269 raw_buffer
, use_buffer
);
271 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
272 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
275 case lval_reg_frame_relative
:
277 /* value is stored in a series of registers in the frame
278 specified by the structure. Copy that value out, modify
279 it, and copy it back in. */
280 int amount_to_copy
= (VALUE_BITSIZE (toval
) ? 1 : TYPE_LENGTH (type
));
281 int reg_size
= REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval
));
282 int byte_offset
= VALUE_OFFSET (toval
) % reg_size
;
283 int reg_offset
= VALUE_OFFSET (toval
) / reg_size
;
285 char *buffer
= (char *) alloca (amount_to_copy
);
289 /* Figure out which frame this is in currently. */
290 for (frame
= get_current_frame ();
291 frame
&& FRAME_FP (frame
) != VALUE_FRAME (toval
);
292 frame
= get_prev_frame (frame
))
296 error ("Value being assigned to is no longer active.");
298 amount_to_copy
+= (reg_size
- amount_to_copy
% reg_size
);
301 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
303 amount_copied
< amount_to_copy
;
304 amount_copied
+= reg_size
, regno
++)
306 get_saved_register (buffer
+ amount_copied
,
307 (int *)NULL
, (CORE_ADDR
)NULL
,
308 frame
, regno
, (enum lval_type
*)NULL
);
311 /* Modify what needs to be modified. */
312 if (VALUE_BITSIZE (toval
))
313 modify_field (buffer
+ byte_offset
,
314 (int) value_as_long (fromval
),
315 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
317 bcopy (raw_buffer
, buffer
+ byte_offset
, use_buffer
);
319 bcopy (VALUE_CONTENTS (fromval
), buffer
+ byte_offset
,
323 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
325 amount_copied
< amount_to_copy
;
326 amount_copied
+= reg_size
, regno
++)
332 /* Just find out where to put it. */
333 get_saved_register ((char *)NULL
,
334 &optim
, &addr
, frame
, regno
, &lval
);
337 error ("Attempt to assign to a value that was optimized out.");
338 if (lval
== lval_memory
)
339 write_memory (addr
, buffer
+ amount_copied
, reg_size
);
340 else if (lval
== lval_register
)
341 write_register_bytes (addr
, buffer
+ amount_copied
, reg_size
);
343 error ("Attempt to assign to an unmodifiable value.");
350 error ("Left side of = operation is not an lvalue.");
353 /* Return a value just like TOVAL except with the contents of FROMVAL
354 (except in the case of the type if TOVAL is an internalvar). */
356 if (VALUE_LVAL (toval
) == lval_internalvar
357 || VALUE_LVAL (toval
) == lval_internalvar_component
)
359 type
= VALUE_TYPE (fromval
);
362 val
= allocate_value (type
);
363 bcopy (toval
, val
, VALUE_CONTENTS_RAW (val
) - (char *) val
);
364 bcopy (VALUE_CONTENTS (fromval
), VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (type
));
365 VALUE_TYPE (val
) = type
;
370 /* Extend a value VAL to COUNT repetitions of its type. */
373 value_repeat (arg1
, count
)
379 if (VALUE_LVAL (arg1
) != lval_memory
)
380 error ("Only values in memory can be extended with '@'.");
382 error ("Invalid number %d of repetitions.", count
);
384 val
= allocate_repeat_value (VALUE_TYPE (arg1
), count
);
386 read_memory (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
),
387 VALUE_CONTENTS_RAW (val
),
388 TYPE_LENGTH (VALUE_TYPE (val
)) * count
);
389 VALUE_LVAL (val
) = lval_memory
;
390 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
);
396 value_of_variable (var
)
401 val
= read_var_value (var
, (FRAME
) 0);
403 error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var
));
407 /* Given a value which is an array, return a value which is
408 a pointer to its first (actually, zeroth) element.
409 FIXME, this should be subtracting the array's lower bound. */
412 value_coerce_array (arg1
)
415 register struct type
*type
;
417 if (VALUE_LVAL (arg1
) != lval_memory
)
418 error ("Attempt to take address of value not located in memory.");
420 /* Get type of elements. */
421 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
)
422 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
424 /* A phony array made by value_repeat.
425 Its type is the type of the elements, not an array type. */
426 type
= VALUE_TYPE (arg1
);
428 return value_from_longest (lookup_pointer_type (type
),
429 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
432 /* Given a value which is a function, return a value which is a pointer
436 value_coerce_function (arg1
)
440 if (VALUE_LVAL (arg1
) != lval_memory
)
441 error ("Attempt to take address of value not located in memory.");
443 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1
)),
444 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
447 /* Return a pointer value for the object for which ARG1 is the contents. */
453 extern value
value_copy ();
454 struct type
*type
= VALUE_TYPE (arg1
);
455 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
457 /* Copy the value, but change the type from (T&) to (T*).
458 We keep the same location information, which is efficient,
459 and allows &(&X) to get the location containing the reference. */
460 value arg2
= value_copy (arg1
);
461 VALUE_TYPE (arg2
) = lookup_pointer_type (TYPE_TARGET_TYPE (type
));
464 if (VALUE_REPEATED (arg1
)
465 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
466 return value_coerce_array (arg1
);
467 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
468 return value_coerce_function (arg1
);
470 if (VALUE_LVAL (arg1
) != lval_memory
)
471 error ("Attempt to take address of value not located in memory.");
473 return value_from_longest (lookup_pointer_type (type
),
474 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
477 /* Given a value of a pointer type, apply the C unary * operator to it. */
485 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
486 error ("not implemented: member types in value_ind");
488 /* Allow * on an integer so we can cast it to whatever we want.
489 This returns an int, which seems like the most C-like thing
490 to do. "long long" variables are rare enough that
491 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
492 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
493 return value_at (builtin_type_int
,
494 (CORE_ADDR
) value_as_long (arg1
));
495 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
496 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
497 value_as_pointer (arg1
));
498 error ("Attempt to take contents of a non-pointer value.");
499 return 0; /* For lint -- never reached */
502 /* Pushing small parts of stack frames. */
504 /* Push one word (the size of object that a register holds). */
507 push_word (sp
, buffer
)
509 REGISTER_TYPE buffer
;
511 register int len
= sizeof (REGISTER_TYPE
);
513 SWAP_TARGET_AND_HOST (&buffer
, len
);
516 write_memory (sp
, (char *)&buffer
, len
);
517 #else /* stack grows upward */
518 write_memory (sp
, (char *)&buffer
, len
);
520 #endif /* stack grows upward */
525 /* Push LEN bytes with data at BUFFER. */
528 push_bytes (sp
, buffer
, len
)
535 write_memory (sp
, buffer
, len
);
536 #else /* stack grows upward */
537 write_memory (sp
, buffer
, len
);
539 #endif /* stack grows upward */
544 /* Push onto the stack the specified value VALUE. */
548 register CORE_ADDR sp
;
551 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
555 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
556 #else /* stack grows upward */
557 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
559 #endif /* stack grows upward */
564 /* Perform the standard coercions that are specified
565 for arguments to be passed to C functions. */
568 value_arg_coerce (arg
)
571 register struct type
*type
;
575 type
= VALUE_TYPE (arg
);
577 if (TYPE_CODE (type
) == TYPE_CODE_INT
578 && TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
579 return value_cast (builtin_type_int
, arg
);
581 if (TYPE_CODE (type
) == TYPE_CODE_FLT
582 && TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_double
))
583 return value_cast (builtin_type_double
, arg
);
588 /* Push the value ARG, first coercing it as an argument
592 value_arg_push (sp
, arg
)
593 register CORE_ADDR sp
;
596 return value_push (sp
, value_arg_coerce (arg
));
599 /* Determine a function's address and its return type from its value.
600 Calls error() if the function is not valid for calling. */
603 find_function_addr (function
, retval_type
)
605 struct type
**retval_type
;
607 register struct type
*ftype
= VALUE_TYPE (function
);
608 register enum type_code code
= TYPE_CODE (ftype
);
609 struct type
*value_type
;
612 /* If it's a member function, just look at the function
615 /* Determine address to call. */
616 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
618 funaddr
= VALUE_ADDRESS (function
);
619 value_type
= TYPE_TARGET_TYPE (ftype
);
621 else if (code
== TYPE_CODE_PTR
)
623 funaddr
= value_as_pointer (function
);
624 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_FUNC
625 || TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_METHOD
)
626 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
628 value_type
= builtin_type_int
;
630 else if (code
== TYPE_CODE_INT
)
632 /* Handle the case of functions lacking debugging info.
633 Their values are characters since their addresses are char */
634 if (TYPE_LENGTH (ftype
) == 1)
635 funaddr
= value_as_pointer (value_addr (function
));
637 /* Handle integer used as address of a function. */
638 funaddr
= (CORE_ADDR
) value_as_long (function
);
640 value_type
= builtin_type_int
;
643 error ("Invalid data type for function to be called.");
645 *retval_type
= value_type
;
649 #if defined (CALL_DUMMY)
650 /* All this stuff with a dummy frame may seem unnecessarily complicated
651 (why not just save registers in GDB?). The purpose of pushing a dummy
652 frame which looks just like a real frame is so that if you call a
653 function and then hit a breakpoint (get a signal, etc), "backtrace"
654 will look right. Whether the backtrace needs to actually show the
655 stack at the time the inferior function was called is debatable, but
656 it certainly needs to not display garbage. So if you are contemplating
657 making dummy frames be different from normal frames, consider that. */
659 /* Perform a function call in the inferior.
660 ARGS is a vector of values of arguments (NARGS of them).
661 FUNCTION is a value, the function to be called.
662 Returns a value representing what the function returned.
663 May fail to return, if a breakpoint or signal is hit
664 during the execution of the function. */
667 call_function_by_hand (function
, nargs
, args
)
672 register CORE_ADDR sp
;
675 /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
676 is in host byte order. It is switched to target byte order before calling
678 static REGISTER_TYPE dummy
[] = CALL_DUMMY
;
679 REGISTER_TYPE dummy1
[sizeof dummy
/ sizeof (REGISTER_TYPE
)];
681 struct type
*value_type
;
682 unsigned char struct_return
;
683 CORE_ADDR struct_addr
;
684 struct inferior_status inf_status
;
685 struct cleanup
*old_chain
;
689 if (!target_has_execution
)
692 save_inferior_status (&inf_status
, 1);
693 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
695 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
696 (and POP_FRAME for restoring them). (At least on most machines)
697 they are saved on the stack in the inferior. */
700 old_sp
= sp
= read_register (SP_REGNUM
);
702 #if 1 INNER_THAN 2 /* Stack grows down */
705 #else /* Stack grows up */
710 funaddr
= find_function_addr (function
, &value_type
);
713 struct block
*b
= block_for_pc (funaddr
);
714 /* If compiled without -g, assume GCC. */
715 using_gcc
= b
== NULL
|| BLOCK_GCC_COMPILED (b
);
718 /* Are we returning a value using a structure return or a normal
721 struct_return
= using_struct_return (function
, funaddr
, value_type
,
724 /* Create a call sequence customized for this function
725 and the number of arguments for it. */
726 bcopy (dummy
, dummy1
, sizeof dummy
);
727 for (i
= 0; i
< sizeof dummy
/ sizeof (REGISTER_TYPE
); i
++)
728 SWAP_TARGET_AND_HOST (&dummy1
[i
], sizeof (REGISTER_TYPE
));
729 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
730 value_type
, using_gcc
);
732 #if CALL_DUMMY_LOCATION == ON_STACK
733 write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
735 #else /* Not on stack. */
736 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
737 /* Convex Unix prohibits executing in the stack segment. */
738 /* Hope there is empty room at the top of the text segment. */
740 extern CORE_ADDR text_end
;
743 for (start_sp
= text_end
- sizeof dummy
; start_sp
< text_end
; ++start_sp
)
744 if (read_memory_integer (start_sp
, 1) != 0)
745 error ("text segment full -- no place to put call");
748 start_sp
= text_end
- sizeof dummy
;
749 write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
751 #else /* After text_end. */
753 extern CORE_ADDR text_end
;
757 errcode
= target_write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
759 error ("Cannot write text segment -- call_function failed");
761 #endif /* After text_end. */
762 #endif /* Not on stack. */
765 sp
= old_sp
; /* It really is used, for some ifdef's... */
769 /* If stack grows down, we must leave a hole at the top. */
773 /* Reserve space for the return structure to be written on the
774 stack, if necessary */
777 len
+= TYPE_LENGTH (value_type
);
779 for (i
= nargs
- 1; i
>= 0; i
--)
780 len
+= TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args
[i
])));
781 #ifdef CALL_DUMMY_STACK_ADJUST
782 len
+= CALL_DUMMY_STACK_ADJUST
;
785 sp
-= STACK_ALIGN (len
) - len
;
787 sp
+= STACK_ALIGN (len
) - len
;
790 #endif /* STACK_ALIGN */
792 /* Reserve space for the return structure to be written on the
793 stack, if necessary */
798 sp
-= TYPE_LENGTH (value_type
);
802 sp
+= TYPE_LENGTH (value_type
);
806 #if defined (REG_STRUCT_HAS_ADDR)
808 /* This is a machine like the sparc, where we need to pass a pointer
809 to the structure, not the structure itself. */
810 if (REG_STRUCT_HAS_ADDR (using_gcc
))
811 for (i
= nargs
- 1; i
>= 0; i
--)
812 if (TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_STRUCT
)
815 #if !(1 INNER_THAN 2)
816 /* The stack grows up, so the address of the thing we push
817 is the stack pointer before we push it. */
820 /* Push the structure. */
821 sp
= value_push (sp
, args
[i
]);
823 /* The stack grows down, so the address of the thing we push
824 is the stack pointer after we push it. */
827 /* The value we're going to pass is the address of the thing
829 args
[i
] = value_from_longest (lookup_pointer_type (value_type
),
833 #endif /* REG_STRUCT_HAS_ADDR. */
835 #ifdef PUSH_ARGUMENTS
836 PUSH_ARGUMENTS(nargs
, args
, sp
, struct_return
, struct_addr
);
837 #else /* !PUSH_ARGUMENTS */
838 for (i
= nargs
- 1; i
>= 0; i
--)
839 sp
= value_arg_push (sp
, args
[i
]);
840 #endif /* !PUSH_ARGUMENTS */
842 #ifdef CALL_DUMMY_STACK_ADJUST
844 sp
-= CALL_DUMMY_STACK_ADJUST
;
846 sp
+= CALL_DUMMY_STACK_ADJUST
;
848 #endif /* CALL_DUMMY_STACK_ADJUST */
850 /* Store the address at which the structure is supposed to be
851 written. Note that this (and the code which reserved the space
852 above) assumes that gcc was used to compile this function. Since
853 it doesn't cost us anything but space and if the function is pcc
854 it will ignore this value, we will make that assumption.
856 Also note that on some machines (like the sparc) pcc uses a
857 convention like gcc's. */
860 STORE_STRUCT_RETURN (struct_addr
, sp
);
862 /* Write the stack pointer. This is here because the statements above
863 might fool with it. On SPARC, this write also stores the register
864 window into the right place in the new stack frame, which otherwise
865 wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */
866 write_register (SP_REGNUM
, sp
);
868 /* Figure out the value returned by the function. */
870 char retbuf
[REGISTER_BYTES
];
872 /* Execute the stack dummy routine, calling FUNCTION.
873 When it is done, discard the empty frame
874 after storing the contents of all regs into retbuf. */
875 run_stack_dummy (start_sp
+ CALL_DUMMY_START_OFFSET
, retbuf
);
877 do_cleanups (old_chain
);
879 return value_being_returned (value_type
, retbuf
, struct_return
);
882 #else /* no CALL_DUMMY. */
884 call_function_by_hand (function
, nargs
, args
)
889 error ("Cannot invoke functions on this machine.");
891 #endif /* no CALL_DUMMY. */
893 /* Create a value for a string constant:
894 Call the function malloc in the inferior to get space for it,
895 then copy the data into that space
896 and then return the address with type char *.
897 PTR points to the string constant data; LEN is number of characters. */
900 value_string (ptr
, len
)
905 register struct symbol
*sym
;
907 register char *copy
= (char *) alloca (len
+ 1);
909 register char *o
= copy
, *ibeg
= ptr
;
912 /* Copy the string into COPY, processing escapes.
913 We could not conveniently process them in the parser
914 because the string there wants to be a substring of the input. */
916 while (i
- ibeg
< len
)
921 c
= parse_escape (&i
);
929 /* Get the length of the string after escapes are processed. */
933 /* Find the address of malloc in the inferior. */
935 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0, NULL
);
938 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
939 error ("\"malloc\" exists in this program but is not a function.");
940 val
= value_of_variable (sym
);
945 j
= lookup_misc_func ("malloc");
947 val
= value_from_longest (
948 lookup_pointer_type (lookup_function_type (
949 lookup_pointer_type (builtin_type_char
))),
950 (LONGEST
) misc_function_vector
[j
].address
);
952 error ("String constants require the program to have a function \"malloc\".");
955 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) (len
+ 1));
956 val
= call_function_by_hand (val
, 1, &blocklen
);
957 if (value_zerop (val
))
958 error ("No memory available for string constant.");
959 write_memory (value_as_pointer (val
), copy
, len
+ 1);
960 VALUE_TYPE (val
) = lookup_pointer_type (builtin_type_char
);
964 /* Helper function used by value_struct_elt to recurse through baseclasses.
965 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
966 and search in it assuming it has (class) type TYPE.
967 If found, return value, else return NULL.
969 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
970 look for a baseclass named NAME. */
973 search_struct_field (name
, arg1
, offset
, type
, looking_for_baseclass
)
977 register struct type
*type
;
978 int looking_for_baseclass
;
982 check_stub_type (type
);
984 if (! looking_for_baseclass
)
985 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
987 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
989 if (t_field_name
&& !strcmp (t_field_name
, name
))
991 value v
= (TYPE_FIELD_STATIC (type
, i
)
992 ? value_static_field (type
, name
, i
)
993 : value_primitive_field (arg1
, offset
, i
, type
));
995 error("there is no field named %s", name
);
1000 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1003 /* If we are looking for baseclasses, this is what we get when we
1005 int found_baseclass
= (looking_for_baseclass
1006 && !strcmp (name
, TYPE_BASECLASS_NAME (type
, i
)));
1008 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1011 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1014 error ("virtual baseclass botch");
1015 if (found_baseclass
)
1017 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
1018 looking_for_baseclass
);
1022 if (found_baseclass
)
1023 v
= value_primitive_field (arg1
, offset
, i
, type
);
1025 v
= search_struct_field (name
, arg1
,
1026 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1027 TYPE_BASECLASS (type
, i
),
1028 looking_for_baseclass
);
1034 /* Helper function used by value_struct_elt to recurse through baseclasses.
1035 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1036 and search in it assuming it has (class) type TYPE.
1037 If found, return value, else return NULL. */
1040 search_struct_method (name
, arg1
, args
, offset
, static_memfuncp
, type
)
1042 register value arg1
, *args
;
1043 int offset
, *static_memfuncp
;
1044 register struct type
*type
;
1048 check_stub_type (type
);
1049 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1051 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1052 if (t_field_name
&& !strcmp (t_field_name
, name
))
1054 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1055 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1057 if (j
> 0 && args
== 0)
1058 error ("cannot resolve overloaded method `%s'", name
);
1061 if (TYPE_FN_FIELD_STUB (f
, j
))
1062 check_stub_method (type
, i
, j
);
1063 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1064 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1066 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1067 return (value
)value_virtual_fn_field (arg1
, f
, j
, type
);
1068 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1069 *static_memfuncp
= 1;
1070 return (value
)value_fn_field (f
, j
);
1077 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1081 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1084 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1087 error ("virtual baseclass botch");
1088 v
= search_struct_method (name
, v2
, args
, 0,
1089 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1094 v
= search_struct_method (name
, arg1
, args
,
1095 TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1096 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1102 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1103 extract the component named NAME from the ultimate target structure/union
1104 and return it as a value with its appropriate type.
1105 ERR is used in the error message if *ARGP's type is wrong.
1107 C++: ARGS is a list of argument types to aid in the selection of
1108 an appropriate method. Also, handle derived types.
1110 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1111 where the truthvalue of whether the function that was resolved was
1112 a static member function or not is stored.
1114 ERR is an error message to be printed in case the field is not found. */
1117 value_struct_elt (argp
, args
, name
, static_memfuncp
, err
)
1118 register value
*argp
, *args
;
1120 int *static_memfuncp
;
1123 register struct type
*t
;
1126 COERCE_ARRAY (*argp
);
1128 t
= VALUE_TYPE (*argp
);
1130 /* Follow pointers until we get to a non-pointer. */
1132 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1134 *argp
= value_ind (*argp
);
1135 /* Don't coerce fn pointer to fn and then back again! */
1136 if (TYPE_CODE (VALUE_TYPE (*argp
)) != TYPE_CODE_FUNC
)
1137 COERCE_ARRAY (*argp
);
1138 t
= VALUE_TYPE (*argp
);
1141 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1142 error ("not implemented: member type in value_struct_elt");
1144 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1145 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1146 error ("Attempt to extract a component of a value that is not a %s.", err
);
1148 /* Assume it's not, unless we see that it is. */
1149 if (static_memfuncp
)
1150 *static_memfuncp
=0;
1154 /* if there are no arguments ...do this... */
1156 /* Try as a field first, because if we succeed, there
1157 is less work to be done. */
1158 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1162 /* C++: If it was not found as a data field, then try to
1163 return it as a pointer to a method. */
1165 if (destructor_name_p (name
, t
))
1166 error ("Cannot get value of destructor");
1168 v
= search_struct_method (name
, *argp
, args
, 0, static_memfuncp
, t
);
1172 if (TYPE_NFN_FIELDS (t
))
1173 error ("There is no member or method named %s.", name
);
1175 error ("There is no member named %s.", name
);
1180 if (destructor_name_p (name
, t
))
1184 /* destructors are a special case. */
1185 return (value
)value_fn_field (TYPE_FN_FIELDLIST1 (t
, 0),
1186 TYPE_FN_FIELDLIST_LENGTH (t
, 0));
1190 error ("destructor should not have any argument");
1194 v
= search_struct_method (name
, *argp
, args
, 0, static_memfuncp
, t
);
1198 /* See if user tried to invoke data as function. If so,
1199 hand it back. If it's not callable (i.e., a pointer to function),
1200 gdb should give an error. */
1201 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1205 error ("Structure has no component named %s.", name
);
1209 /* C++: return 1 is NAME is a legitimate name for the destructor
1210 of type TYPE. If TYPE does not have a destructor, or
1211 if NAME is inappropriate for TYPE, an error is signaled. */
1213 destructor_name_p (name
, type
)
1217 /* destructors are a special case. */
1221 char *dname
= type_name_no_tag (type
);
1222 if (strcmp (dname
, name
+1))
1223 error ("name of destructor must equal name of class");
1230 /* Helper function for check_field: Given TYPE, a structure/union,
1231 return 1 if the component named NAME from the ultimate
1232 target structure/union is defined, otherwise, return 0. */
1235 check_field_in (type
, name
)
1236 register struct type
*type
;
1241 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1243 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1244 if (t_field_name
&& !strcmp (t_field_name
, name
))
1248 /* C++: If it was not found as a data field, then try to
1249 return it as a pointer to a method. */
1251 /* Destructors are a special case. */
1252 if (destructor_name_p (name
, type
))
1255 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1257 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), name
))
1261 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1262 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
1269 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1270 return 1 if the component named NAME from the ultimate
1271 target structure/union is defined, otherwise, return 0. */
1274 check_field (arg1
, name
)
1275 register value arg1
;
1278 register struct type
*t
;
1280 COERCE_ARRAY (arg1
);
1282 t
= VALUE_TYPE (arg1
);
1284 /* Follow pointers until we get to a non-pointer. */
1286 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1287 t
= TYPE_TARGET_TYPE (t
);
1289 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1290 error ("not implemented: member type in check_field");
1292 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1293 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1294 error ("Internal error: `this' is not an aggregate");
1296 return check_field_in (t
, name
);
1299 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1300 return the address of this member as a "pointer to member"
1301 type. If INTYPE is non-null, then it will be the type
1302 of the member we are looking for. This will help us resolve
1303 "pointers to member functions". This function is only used
1304 to resolve user expressions of the form "&class::member". */
1307 value_struct_elt_for_address (domain
, intype
, name
)
1308 struct type
*domain
, *intype
;
1311 register struct type
*t
= domain
;
1315 struct type
*baseclass
;
1317 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1318 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1319 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1325 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1327 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1329 if (t_field_name
&& !strcmp (t_field_name
, name
))
1331 if (TYPE_FIELD_STATIC (t
, i
))
1333 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (t
, i
);
1334 struct symbol
*sym
=
1335 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1338 "Internal error: could not find physical static variable named %s",
1340 return value_from_longest (
1341 lookup_pointer_type (TYPE_FIELD_TYPE (t
, i
)),
1342 (LONGEST
)SYMBOL_BLOCK_VALUE (sym
));
1344 if (TYPE_FIELD_PACKED (t
, i
))
1345 error ("pointers to bitfield members not allowed");
1347 return value_from_longest (
1348 lookup_pointer_type (
1349 lookup_member_type (TYPE_FIELD_TYPE (t
, i
), baseclass
)),
1350 (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1354 if (TYPE_N_BASECLASSES (t
) == 0)
1357 t
= TYPE_BASECLASS (t
, 0);
1360 /* C++: If it was not found as a data field, then try to
1361 return it as a pointer to a method. */
1364 /* Destructors are a special case. */
1365 if (destructor_name_p (name
, t
))
1367 error ("member pointers to destructors not implemented yet");
1370 /* Perform all necessary dereferencing. */
1371 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1372 intype
= TYPE_TARGET_TYPE (intype
);
1376 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1378 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1380 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1381 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1383 if (intype
== 0 && j
> 1)
1384 error ("non-unique member `%s' requires type instantiation", name
);
1388 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1391 error ("no member function matches that type instantiation");
1396 if (TYPE_FN_FIELD_STUB (f
, j
))
1397 check_stub_method (t
, i
, j
);
1398 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1400 return value_from_longest (
1401 lookup_pointer_type (
1402 lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1404 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
1408 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1409 0, VAR_NAMESPACE
, 0, NULL
);
1410 v
= locate_var_value (s
, 0);
1411 VALUE_TYPE (v
) = lookup_pointer_type (
1412 lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1419 if (TYPE_N_BASECLASSES (t
) == 0)
1422 t
= TYPE_BASECLASS (t
, 0);
1427 /* Compare two argument lists and return the position in which they differ,
1430 STATICP is nonzero if the T1 argument list came from a
1431 static member function.
1433 For non-static member functions, we ignore the first argument,
1434 which is the type of the instance variable. This is because we want
1435 to handle calls with objects from derived classes. This is not
1436 entirely correct: we should actually check to make sure that a
1437 requested operation is type secure, shouldn't we? FIXME. */
1440 typecmp (staticp
, t1
, t2
)
1449 if (staticp
&& t1
== 0)
1453 if (t1
[0]->code
== TYPE_CODE_VOID
) return 0;
1454 if (t1
[!staticp
] == 0) return 0;
1455 for (i
= !staticp
; t1
[i
] && t1
[i
]->code
!= TYPE_CODE_VOID
; i
++)
1458 || t1
[i
]->code
!= t2
[i
]->type
->code
1459 /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1463 if (!t1
[i
]) return 0;
1464 return t2
[i
] ? i
+1 : 0;
1467 /* C++: return the value of the class instance variable, if one exists.
1468 Flag COMPLAIN signals an error if the request is made in an
1469 inappropriate context. */
1471 value_of_this (complain
)
1474 extern FRAME selected_frame
;
1475 struct symbol
*func
, *sym
;
1478 static const char funny_this
[] = "this";
1481 if (selected_frame
== 0)
1483 error ("no frame selected");
1486 func
= get_frame_function (selected_frame
);
1490 error ("no `this' in nameless context");
1494 b
= SYMBOL_BLOCK_VALUE (func
);
1495 i
= BLOCK_NSYMS (b
);
1498 error ("no args, no `this'");
1501 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1502 symbol instead of the LOC_ARG one (if both exist). */
1503 sym
= lookup_block_symbol (b
, funny_this
, VAR_NAMESPACE
);
1507 error ("current stack frame not in method");
1512 this = read_var_value (sym
, selected_frame
);
1513 if (this == 0 && complain
)
1514 error ("`this' argument at unknown address");