1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright (C) 1986, 1987, 1989 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. */
32 /* Local functions. */
33 static value
search_struct_field ();
35 /* Cast value ARG2 to type TYPE and return as a value.
36 More general than a C cast: accepts any two types of the same length,
37 and if ARG2 is an lvalue it can be cast into anything at all. */
38 /* In C++, casts may change pointer representations. */
41 value_cast (type
, arg2
)
45 register enum type_code code1
;
46 register enum type_code code2
;
49 /* Coerce arrays but not enums. Enums will work as-is
50 and coercing them would cause an infinite recursion. */
51 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_ENUM
)
54 code1
= TYPE_CODE (type
);
55 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
56 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
57 || code2
== TYPE_CODE_ENUM
);
59 if (code1
== TYPE_CODE_FLT
&& scalar
)
60 return value_from_double (type
, value_as_double (arg2
));
61 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
)
62 && (scalar
|| code2
== TYPE_CODE_PTR
))
63 return value_from_longest (type
, value_as_long (arg2
));
64 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (VALUE_TYPE (arg2
)))
66 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
68 /* Look in the type of the source to see if it contains the
69 type of the target as a superclass. If so, we'll need to
70 offset the pointer rather than just change its type. */
71 struct type
*t1
= TYPE_TARGET_TYPE (type
);
72 struct type
*t2
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
73 if (TYPE_CODE (t1
) == TYPE_CODE_STRUCT
74 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
75 && TYPE_NAME (t1
) != 0) /* if name unknown, can't have supercl */
77 value v
= search_struct_field (type_name_no_tag (t1
),
78 value_ind (arg2
), 0, t2
, 1);
82 VALUE_TYPE (v
) = type
;
86 /* No superclass found, just fall through to change ptr type. */
88 VALUE_TYPE (arg2
) = type
;
91 else if (VALUE_LVAL (arg2
) == lval_memory
)
93 return value_at_lazy (type
, VALUE_ADDRESS (arg2
) + VALUE_OFFSET (arg2
));
95 else if (code1
== TYPE_CODE_VOID
)
97 return value_zero (builtin_type_void
, not_lval
);
101 error ("Invalid cast.");
106 /* Create a value of type TYPE that is zero, and return it. */
109 value_zero (type
, lv
)
113 register value val
= allocate_value (type
);
115 bzero (VALUE_CONTENTS (val
), TYPE_LENGTH (type
));
116 VALUE_LVAL (val
) = lv
;
121 /* Return a value with type TYPE located at ADDR.
123 Call value_at only if the data needs to be fetched immediately;
124 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
125 value_at_lazy instead. value_at_lazy simply records the address of
126 the data and sets the lazy-evaluation-required flag. The lazy flag
127 is tested in the VALUE_CONTENTS macro, which is used if and when
128 the contents are actually required. */
131 value_at (type
, addr
)
135 register value val
= allocate_value (type
);
137 read_memory (addr
, VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (type
));
139 VALUE_LVAL (val
) = lval_memory
;
140 VALUE_ADDRESS (val
) = addr
;
145 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
148 value_at_lazy (type
, addr
)
152 register value val
= allocate_value (type
);
154 VALUE_LVAL (val
) = lval_memory
;
155 VALUE_ADDRESS (val
) = addr
;
156 VALUE_LAZY (val
) = 1;
161 /* Called only from the VALUE_CONTENTS macro, if the current data for
162 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
163 data from the user's process, and clears the lazy flag to indicate
164 that the data in the buffer is valid.
166 This function returns a value because it is used in the VALUE_CONTENTS
167 macro as part of an expression, where a void would not work. The
171 value_fetch_lazy (val
)
174 CORE_ADDR addr
= VALUE_ADDRESS (val
) + VALUE_OFFSET (val
);
176 read_memory (addr
, VALUE_CONTENTS_RAW (val
),
177 TYPE_LENGTH (VALUE_TYPE (val
)));
178 VALUE_LAZY (val
) = 0;
183 /* Store the contents of FROMVAL into the location of TOVAL.
184 Return a new value with the location of TOVAL and contents of FROMVAL. */
187 value_assign (toval
, fromval
)
188 register value toval
, fromval
;
190 register struct type
*type
= VALUE_TYPE (toval
);
192 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
193 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
196 COERCE_ARRAY (fromval
);
198 if (VALUE_LVAL (toval
) != lval_internalvar
)
199 fromval
= value_cast (type
, fromval
);
201 /* If TOVAL is a special machine register requiring conversion
202 of program values to a special raw format,
203 convert FROMVAL's contents now, with result in `raw_buffer',
204 and set USE_BUFFER to the number of bytes to write. */
206 if (VALUE_REGNO (toval
) >= 0
207 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval
)))
209 int regno
= VALUE_REGNO (toval
);
210 if (VALUE_TYPE (fromval
) != REGISTER_VIRTUAL_TYPE (regno
))
211 fromval
= value_cast (REGISTER_VIRTUAL_TYPE (regno
), fromval
);
212 bcopy (VALUE_CONTENTS (fromval
), virtual_buffer
,
213 REGISTER_VIRTUAL_SIZE (regno
));
214 target_convert_from_virtual (regno
, virtual_buffer
, raw_buffer
);
215 use_buffer
= REGISTER_RAW_SIZE (regno
);
218 switch (VALUE_LVAL (toval
))
220 case lval_internalvar
:
221 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
224 case lval_internalvar_component
:
225 set_internalvar_component (VALUE_INTERNALVAR (toval
),
226 VALUE_OFFSET (toval
),
227 VALUE_BITPOS (toval
),
228 VALUE_BITSIZE (toval
),
233 if (VALUE_BITSIZE (toval
))
235 int v
; /* FIXME, this won't work for large bitfields */
236 read_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
238 modify_field (&v
, (int) value_as_long (fromval
),
239 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
240 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
241 (char *)&v
, sizeof v
);
244 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
245 raw_buffer
, use_buffer
);
247 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
248 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
252 if (VALUE_BITSIZE (toval
))
256 read_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
258 modify_field (&v
, (int) value_as_long (fromval
),
259 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
260 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
264 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
265 raw_buffer
, use_buffer
);
267 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
268 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
271 case lval_reg_frame_relative
:
273 /* value is stored in a series of registers in the frame
274 specified by the structure. Copy that value out, modify
275 it, and copy it back in. */
276 int amount_to_copy
= (VALUE_BITSIZE (toval
) ? 1 : TYPE_LENGTH (type
));
277 int reg_size
= REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval
));
278 int byte_offset
= VALUE_OFFSET (toval
) % reg_size
;
279 int reg_offset
= VALUE_OFFSET (toval
) / reg_size
;
281 char *buffer
= (char *) alloca (amount_to_copy
);
285 /* Figure out which frame this is in currently. */
286 for (frame
= get_current_frame ();
287 frame
&& FRAME_FP (frame
) != VALUE_FRAME (toval
);
288 frame
= get_prev_frame (frame
))
292 error ("Value being assigned to is no longer active.");
294 amount_to_copy
+= (reg_size
- amount_to_copy
% reg_size
);
297 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
299 amount_copied
< amount_to_copy
;
300 amount_copied
+= reg_size
, regno
++)
302 get_saved_register (buffer
+ amount_copied
,
303 (int *)NULL
, (CORE_ADDR
)NULL
,
304 frame
, regno
, (enum lval_type
*)NULL
);
307 /* Modify what needs to be modified. */
308 if (VALUE_BITSIZE (toval
))
309 modify_field (buffer
+ byte_offset
,
310 (int) value_as_long (fromval
),
311 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
313 bcopy (raw_buffer
, buffer
+ byte_offset
, use_buffer
);
315 bcopy (VALUE_CONTENTS (fromval
), buffer
+ byte_offset
,
319 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
321 amount_copied
< amount_to_copy
;
322 amount_copied
+= reg_size
, regno
++)
328 /* Just find out where to put it. */
329 get_saved_register ((char *)NULL
,
330 &optim
, &addr
, frame
, regno
, &lval
);
333 error ("Attempt to assign to a value that was optimized out.");
334 if (lval
== lval_memory
)
335 write_memory (addr
, buffer
+ amount_copied
, reg_size
);
336 else if (lval
== lval_register
)
337 write_register_bytes (addr
, buffer
+ amount_copied
, reg_size
);
339 error ("Attempt to assign to an unmodifiable value.");
346 error ("Left side of = operation is not an lvalue.");
349 /* Return a value just like TOVAL except with the contents of FROMVAL
350 (except in the case of the type if TOVAL is an internalvar). */
352 if (VALUE_LVAL (toval
) == lval_internalvar
353 || VALUE_LVAL (toval
) == lval_internalvar_component
)
355 type
= VALUE_TYPE (fromval
);
358 val
= allocate_value (type
);
359 bcopy (toval
, val
, VALUE_CONTENTS_RAW (val
) - (char *) val
);
360 bcopy (VALUE_CONTENTS (fromval
), VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (type
));
361 VALUE_TYPE (val
) = type
;
366 /* Extend a value VAL to COUNT repetitions of its type. */
369 value_repeat (arg1
, count
)
375 if (VALUE_LVAL (arg1
) != lval_memory
)
376 error ("Only values in memory can be extended with '@'.");
378 error ("Invalid number %d of repetitions.", count
);
380 val
= allocate_repeat_value (VALUE_TYPE (arg1
), count
);
382 read_memory (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
),
383 VALUE_CONTENTS_RAW (val
),
384 TYPE_LENGTH (VALUE_TYPE (val
)) * count
);
385 VALUE_LVAL (val
) = lval_memory
;
386 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
);
392 value_of_variable (var
)
397 val
= read_var_value (var
, (FRAME
) 0);
399 error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var
));
403 /* Given a value which is an array, return a value which is
404 a pointer to its first (actually, zeroth) element.
405 FIXME, this should be subtracting the array's lower bound. */
408 value_coerce_array (arg1
)
411 register struct type
*type
;
413 if (VALUE_LVAL (arg1
) != lval_memory
)
414 error ("Attempt to take address of value not located in memory.");
416 /* Get type of elements. */
417 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
)
418 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
420 /* A phony array made by value_repeat.
421 Its type is the type of the elements, not an array type. */
422 type
= VALUE_TYPE (arg1
);
424 return value_from_longest (lookup_pointer_type (type
),
425 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
428 /* Given a value which is a function, return a value which is a pointer
432 value_coerce_function (arg1
)
436 if (VALUE_LVAL (arg1
) != lval_memory
)
437 error ("Attempt to take address of value not located in memory.");
439 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1
)),
440 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
443 /* Return a pointer value for the object for which ARG1 is the contents. */
451 if (VALUE_REPEATED (arg1
)
452 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
)
453 return value_coerce_array (arg1
);
454 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FUNC
)
455 return value_coerce_function (arg1
);
457 if (VALUE_LVAL (arg1
) != lval_memory
)
458 error ("Attempt to take address of value not located in memory.");
460 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1
)),
461 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
464 /* Given a value of a pointer type, apply the C unary * operator to it. */
472 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
473 error ("not implemented: member types in value_ind");
475 /* Allow * on an integer so we can cast it to whatever we want.
476 This returns an int, which seems like the most C-like thing
477 to do. "long long" variables are rare enough that
478 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
479 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
480 return value_at (builtin_type_int
,
481 (CORE_ADDR
) value_as_long (arg1
));
482 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
483 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
484 value_as_pointer (arg1
));
485 error ("Attempt to take contents of a non-pointer value.");
486 return 0; /* For lint -- never reached */
489 /* Pushing small parts of stack frames. */
491 /* Push one word (the size of object that a register holds). */
494 push_word (sp
, buffer
)
496 REGISTER_TYPE buffer
;
498 register int len
= sizeof (REGISTER_TYPE
);
500 SWAP_TARGET_AND_HOST (&buffer
, len
);
503 write_memory (sp
, (char *)&buffer
, len
);
504 #else /* stack grows upward */
505 write_memory (sp
, (char *)&buffer
, len
);
507 #endif /* stack grows upward */
512 /* Push LEN bytes with data at BUFFER. */
515 push_bytes (sp
, buffer
, len
)
522 write_memory (sp
, buffer
, len
);
523 #else /* stack grows upward */
524 write_memory (sp
, buffer
, len
);
526 #endif /* stack grows upward */
531 /* Push onto the stack the specified value VALUE. */
535 register CORE_ADDR sp
;
538 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
542 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
543 #else /* stack grows upward */
544 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
546 #endif /* stack grows upward */
551 /* Perform the standard coercions that are specified
552 for arguments to be passed to C functions. */
555 value_arg_coerce (arg
)
558 register struct type
*type
;
562 type
= VALUE_TYPE (arg
);
564 if (TYPE_CODE (type
) == TYPE_CODE_INT
565 && TYPE_LENGTH (type
) < sizeof (int))
566 return value_cast (builtin_type_int
, arg
);
568 if (type
== builtin_type_float
)
569 return value_cast (builtin_type_double
, arg
);
574 /* Push the value ARG, first coercing it as an argument
578 value_arg_push (sp
, arg
)
579 register CORE_ADDR sp
;
582 return value_push (sp
, value_arg_coerce (arg
));
585 /* Determine a function's address and its return type from its value.
586 Calls error() if the function is not valid for calling. */
589 find_function_addr (function
, retval_type
)
591 struct type
**retval_type
;
593 register struct type
*ftype
= VALUE_TYPE (function
);
594 register enum type_code code
= TYPE_CODE (ftype
);
595 struct type
*value_type
;
598 /* If it's a member function, just look at the function
601 /* Determine address to call. */
602 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
604 funaddr
= VALUE_ADDRESS (function
);
605 value_type
= TYPE_TARGET_TYPE (ftype
);
607 else if (code
== TYPE_CODE_PTR
)
609 funaddr
= value_as_pointer (function
);
610 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_FUNC
611 || TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_METHOD
)
612 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
614 value_type
= builtin_type_int
;
616 else if (code
== TYPE_CODE_INT
)
618 /* Handle the case of functions lacking debugging info.
619 Their values are characters since their addresses are char */
620 if (TYPE_LENGTH (ftype
) == 1)
621 funaddr
= value_as_pointer (value_addr (function
));
623 /* Handle integer used as address of a function. */
624 funaddr
= (CORE_ADDR
) value_as_long (function
);
626 value_type
= builtin_type_int
;
629 error ("Invalid data type for function to be called.");
631 *retval_type
= value_type
;
635 #if defined (CALL_DUMMY)
636 /* All this stuff with a dummy frame may seem unnecessarily complicated
637 (why not just save registers in GDB?). The purpose of pushing a dummy
638 frame which looks just like a real frame is so that if you call a
639 function and then hit a breakpoint (get a signal, etc), "backtrace"
640 will look right. Whether the backtrace needs to actually show the
641 stack at the time the inferior function was called is debatable, but
642 it certainly needs to not display garbage. So if you are contemplating
643 making dummy frames be different from normal frames, consider that. */
645 /* Perform a function call in the inferior.
646 ARGS is a vector of values of arguments (NARGS of them).
647 FUNCTION is a value, the function to be called.
648 Returns a value representing what the function returned.
649 May fail to return, if a breakpoint or signal is hit
650 during the execution of the function. */
653 call_function_by_hand (function
, nargs
, args
)
658 register CORE_ADDR sp
;
661 /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
662 in in host byte order. It is switched to target byte order before calling
664 static REGISTER_TYPE dummy
[] = CALL_DUMMY
;
665 REGISTER_TYPE dummy1
[sizeof dummy
/ sizeof (REGISTER_TYPE
)];
667 struct type
*value_type
;
668 unsigned char struct_return
;
669 CORE_ADDR struct_addr
;
670 struct inferior_status inf_status
;
671 struct cleanup
*old_chain
;
675 save_inferior_status (&inf_status
, 1);
676 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
678 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
679 (and POP_FRAME for restoring them). (At least on most machines)
680 they are saved on the stack in the inferior. */
683 old_sp
= sp
= read_register (SP_REGNUM
);
685 #if 1 INNER_THAN 2 /* Stack grows down */
688 #else /* Stack grows up */
693 funaddr
= find_function_addr (function
, &value_type
);
696 struct block
*b
= block_for_pc (funaddr
);
697 /* If compiled without -g, assume GCC. */
698 using_gcc
= b
== NULL
|| BLOCK_GCC_COMPILED (b
);
701 /* Are we returning a value using a structure return or a normal
704 struct_return
= using_struct_return (function
, funaddr
, value_type
,
707 /* Create a call sequence customized for this function
708 and the number of arguments for it. */
709 bcopy (dummy
, dummy1
, sizeof dummy
);
710 for (i
= 0; i
< sizeof dummy
/ sizeof (REGISTER_TYPE
); i
++)
711 SWAP_TARGET_AND_HOST (&dummy1
[i
], sizeof (REGISTER_TYPE
));
712 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
713 value_type
, using_gcc
);
715 #if CALL_DUMMY_LOCATION == ON_STACK
716 write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
718 #else /* Not on stack. */
719 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
720 /* Convex Unix prohibits executing in the stack segment. */
721 /* Hope there is empty room at the top of the text segment. */
725 for (start_sp
= text_end
- sizeof dummy
; start_sp
< text_end
; ++start_sp
)
726 if (read_memory_integer (start_sp
, 1) != 0)
727 error ("text segment full -- no place to put call");
730 start_sp
= text_end
- sizeof dummy
;
731 write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
733 #else /* After text_end. */
738 errcode
= target_write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
740 error ("Cannot write text segment -- call_function failed");
742 #endif /* After text_end. */
743 #endif /* Not on stack. */
746 sp
= old_sp
; /* It really is used, for some ifdef's... */
750 /* If stack grows down, we must leave a hole at the top. */
754 /* Reserve space for the return structure to be written on the
755 stack, if necessary */
758 len
+= TYPE_LENGTH (value_type
);
760 for (i
= nargs
- 1; i
>= 0; i
--)
761 len
+= TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args
[i
])));
762 #ifdef CALL_DUMMY_STACK_ADJUST
763 len
+= CALL_DUMMY_STACK_ADJUST
;
766 sp
-= STACK_ALIGN (len
) - len
;
768 sp
+= STACK_ALIGN (len
) - len
;
771 #endif /* STACK_ALIGN */
773 /* Reserve space for the return structure to be written on the
774 stack, if necessary */
779 sp
-= TYPE_LENGTH (value_type
);
783 sp
+= TYPE_LENGTH (value_type
);
787 #if defined (REG_STRUCT_HAS_ADDR)
789 /* This is a machine like the sparc, where we need to pass a pointer
790 to the structure, not the structure itself. */
791 if (REG_STRUCT_HAS_ADDR (using_gcc
))
792 for (i
= nargs
- 1; i
>= 0; i
--)
793 if (TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_STRUCT
)
796 #if !(1 INNER_THAN 2)
797 /* The stack grows up, so the address of the thing we push
798 is the stack pointer before we push it. */
801 /* Push the structure. */
802 sp
= value_push (sp
, args
[i
]);
804 /* The stack grows down, so the address of the thing we push
805 is the stack pointer after we push it. */
808 /* The value we're going to pass is the address of the thing
810 args
[i
] = value_from_longest (lookup_pointer_type (value_type
),
814 #endif /* REG_STRUCT_HAS_ADDR. */
816 #ifdef PUSH_ARGUMENTS
817 PUSH_ARGUMENTS(nargs
, args
, sp
, struct_return
, struct_addr
);
818 #else /* !PUSH_ARGUMENTS */
819 for (i
= nargs
- 1; i
>= 0; i
--)
820 sp
= value_arg_push (sp
, args
[i
]);
821 #endif /* !PUSH_ARGUMENTS */
823 #ifdef CALL_DUMMY_STACK_ADJUST
825 sp
-= CALL_DUMMY_STACK_ADJUST
;
827 sp
+= CALL_DUMMY_STACK_ADJUST
;
829 #endif /* CALL_DUMMY_STACK_ADJUST */
831 /* Store the address at which the structure is supposed to be
832 written. Note that this (and the code which reserved the space
833 above) assumes that gcc was used to compile this function. Since
834 it doesn't cost us anything but space and if the function is pcc
835 it will ignore this value, we will make that assumption.
837 Also note that on some machines (like the sparc) pcc uses a
838 convention like gcc's. */
841 STORE_STRUCT_RETURN (struct_addr
, sp
);
843 /* Write the stack pointer. This is here because the statements above
844 might fool with it. On SPARC, this write also stores the register
845 window into the right place in the new stack frame, which otherwise
846 wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */
847 write_register (SP_REGNUM
, sp
);
849 /* Figure out the value returned by the function. */
851 char retbuf
[REGISTER_BYTES
];
853 /* Execute the stack dummy routine, calling FUNCTION.
854 When it is done, discard the empty frame
855 after storing the contents of all regs into retbuf. */
856 run_stack_dummy (start_sp
+ CALL_DUMMY_START_OFFSET
, retbuf
);
858 do_cleanups (old_chain
);
860 return value_being_returned (value_type
, retbuf
, struct_return
);
863 #else /* no CALL_DUMMY. */
865 call_function_by_hand (function
, nargs
, args
)
870 error ("Cannot invoke functions on this machine.");
872 #endif /* no CALL_DUMMY. */
874 /* Create a value for a string constant:
875 Call the function malloc in the inferior to get space for it,
876 then copy the data into that space
877 and then return the address with type char *.
878 PTR points to the string constant data; LEN is number of characters. */
881 value_string (ptr
, len
)
886 register struct symbol
*sym
;
888 register char *copy
= (char *) alloca (len
+ 1);
890 register char *o
= copy
, *ibeg
= ptr
;
893 /* Copy the string into COPY, processing escapes.
894 We could not conveniently process them in expread
895 because the string there wants to be a substring of the input. */
897 while (i
- ibeg
< len
)
902 c
= parse_escape (&i
);
910 /* Get the length of the string after escapes are processed. */
914 /* Find the address of malloc in the inferior. */
916 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0, NULL
);
919 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
920 error ("\"malloc\" exists in this program but is not a function.");
921 val
= value_of_variable (sym
);
926 j
= lookup_misc_func ("malloc");
928 val
= value_from_longest (
929 lookup_pointer_type (lookup_function_type (
930 lookup_pointer_type (builtin_type_char
))),
931 (LONGEST
) misc_function_vector
[j
].address
);
933 error ("String constants require the program to have a function \"malloc\".");
936 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) (len
+ 1));
937 val
= target_call_function (val
, 1, &blocklen
);
938 if (value_zerop (val
))
939 error ("No memory available for string constant.");
940 write_memory (value_as_pointer (val
), copy
, len
+ 1);
941 VALUE_TYPE (val
) = lookup_pointer_type (builtin_type_char
);
945 /* Helper function used by value_struct_elt to recurse through baseclasses.
946 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
947 and treat the result as having type TYPE.
948 If found, return value, else return NULL.
950 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
951 look for a baseclass named NAME. */
954 search_struct_field (name
, arg1
, offset
, type
, looking_for_baseclass
)
958 register struct type
*type
;
959 int looking_for_baseclass
;
963 check_stub_type (type
);
965 if (! looking_for_baseclass
)
966 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
968 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
970 if (t_field_name
&& !strcmp (t_field_name
, name
))
972 value v
= (TYPE_FIELD_STATIC (type
, i
)
973 ? value_static_field (type
, name
, i
)
974 : value_primitive_field (arg1
, offset
, i
, type
));
976 error("there is no field named %s", name
);
981 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
984 /* If we are looking for baseclasses, this is what we get when we
986 int found_baseclass
= (looking_for_baseclass
987 && !strcmp (name
, TYPE_BASECLASS_NAME (type
, i
)));
989 if (BASETYPE_VIA_VIRTUAL (type
, i
))
992 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
995 error ("virtual baseclass botch");
998 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
999 looking_for_baseclass
);
1003 if (found_baseclass
)
1004 v
= value_primitive_field (arg1
, offset
, i
, type
);
1006 v
= search_struct_field (name
, arg1
,
1007 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1008 TYPE_BASECLASS (type
, i
),
1009 looking_for_baseclass
);
1015 /* Helper function used by value_struct_elt to recurse through baseclasses.
1016 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1017 and treat the result as having type TYPE.
1018 If found, return value, else return NULL. */
1021 search_struct_method (name
, arg1
, args
, offset
, static_memfuncp
, type
)
1023 register value arg1
, *args
;
1024 int offset
, *static_memfuncp
;
1025 register struct type
*type
;
1029 check_stub_type (type
);
1030 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1032 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1033 if (t_field_name
&& !strcmp (t_field_name
, name
))
1035 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1036 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1038 if (j
> 0 && args
== 0)
1039 error ("cannot resolve overloaded method `%s'", name
);
1042 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f
, j
)) & TYPE_FLAG_STUB
)
1043 check_stub_method (type
, i
, j
);
1044 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1045 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1047 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1048 return (value
)value_virtual_fn_field (arg1
, f
, j
, type
);
1049 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1050 *static_memfuncp
= 1;
1051 return (value
)value_fn_field (arg1
, i
, j
);
1058 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1062 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1065 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1068 error ("virtual baseclass botch");
1069 v
= search_struct_method (name
, v2
, args
, 0,
1070 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1075 v
= search_struct_method (name
, arg1
, args
,
1076 TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1077 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1083 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1084 extract the component named NAME from the ultimate target structure/union
1085 and return it as a value with its appropriate type.
1086 ERR is used in the error message if *ARGP's type is wrong.
1088 C++: ARGS is a list of argument types to aid in the selection of
1089 an appropriate method. Also, handle derived types.
1091 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1092 where the truthvalue of whether the function that was resolved was
1093 a static member function or not is stored.
1095 ERR is an error message to be printed in case the field is not found. */
1098 value_struct_elt (argp
, args
, name
, static_memfuncp
, err
)
1099 register value
*argp
, *args
;
1101 int *static_memfuncp
;
1104 register struct type
*t
;
1107 COERCE_ARRAY (*argp
);
1109 t
= VALUE_TYPE (*argp
);
1111 /* Follow pointers until we get to a non-pointer. */
1113 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1115 *argp
= value_ind (*argp
);
1116 /* Don't coerce fn pointer to fn and then back again! */
1117 if (TYPE_CODE (VALUE_TYPE (*argp
)) != TYPE_CODE_FUNC
)
1118 COERCE_ARRAY (*argp
);
1119 t
= VALUE_TYPE (*argp
);
1122 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1123 error ("not implemented: member type in value_struct_elt");
1125 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1126 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1127 error ("Attempt to extract a component of a value that is not a %s.", err
);
1129 /* Assume it's not, unless we see that it is. */
1130 if (static_memfuncp
)
1131 *static_memfuncp
=0;
1135 /* if there are no arguments ...do this... */
1137 /* Try as a field first, because if we succeed, there
1138 is less work to be done. */
1139 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1143 /* C++: If it was not found as a data field, then try to
1144 return it as a pointer to a method. */
1146 if (destructor_name_p (name
, t
))
1147 error ("Cannot get value of destructor");
1149 v
= search_struct_method (name
, *argp
, args
, 0, static_memfuncp
, t
);
1153 if (TYPE_NFN_FIELDS (t
))
1154 error ("There is no member or method named %s.", name
);
1156 error ("There is no member named %s.", name
);
1161 if (destructor_name_p (name
, t
))
1165 /* destructors are a special case. */
1166 return (value
)value_fn_field (*argp
, 0,
1167 TYPE_FN_FIELDLIST_LENGTH (t
, 0));
1171 error ("destructor should not have any argument");
1175 v
= search_struct_method (name
, *argp
, args
, 0, static_memfuncp
, t
);
1179 /* See if user tried to invoke data as function. If so,
1180 hand it back. If it's not callable (i.e., a pointer to function),
1181 gdb should give an error. */
1182 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1186 error ("Structure has no component named %s.", name
);
1190 /* C++: return 1 is NAME is a legitimate name for the destructor
1191 of type TYPE. If TYPE does not have a destructor, or
1192 if NAME is inappropriate for TYPE, an error is signaled. */
1194 destructor_name_p (name
, type
)
1198 /* destructors are a special case. */
1202 char *dname
= type_name_no_tag (type
);
1204 if (! TYPE_HAS_DESTRUCTOR (type
))
1205 error ("type `%s' does not have destructor defined", dname
);
1206 if (strcmp (dname
, name
+1))
1207 error ("name of destructor must equal name of class");
1214 /* Helper function for check_field: Given TYPE, a structure/union,
1215 return 1 if the component named NAME from the ultimate
1216 target structure/union is defined, otherwise, return 0. */
1219 check_field_in (type
, name
)
1220 register struct type
*type
;
1225 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1227 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1228 if (t_field_name
&& !strcmp (t_field_name
, name
))
1232 /* C++: If it was not found as a data field, then try to
1233 return it as a pointer to a method. */
1235 /* Destructors are a special case. */
1236 if (destructor_name_p (name
, type
))
1239 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1241 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), name
))
1245 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1246 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
1253 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1254 return 1 if the component named NAME from the ultimate
1255 target structure/union is defined, otherwise, return 0. */
1258 check_field (arg1
, name
)
1259 register value arg1
;
1262 register struct type
*t
;
1264 COERCE_ARRAY (arg1
);
1266 t
= VALUE_TYPE (arg1
);
1268 /* Follow pointers until we get to a non-pointer. */
1270 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1271 t
= TYPE_TARGET_TYPE (t
);
1273 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1274 error ("not implemented: member type in check_field");
1276 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1277 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1278 error ("Internal error: `this' is not an aggregate");
1280 return check_field_in (t
, name
);
1283 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1284 return the address of this member as a pointer to member
1285 type. If INTYPE is non-null, then it will be the type
1286 of the member we are looking for. This will help us resolve
1287 pointers to member functions. */
1290 value_struct_elt_for_address (domain
, intype
, name
)
1291 struct type
*domain
, *intype
;
1294 register struct type
*t
= domain
;
1298 struct type
*baseclass
;
1300 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1301 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1302 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1308 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1310 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1311 if (t_field_name
&& !strcmp (t_field_name
, name
))
1313 if (TYPE_FIELD_STATIC (t
, i
))
1315 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (t
, i
);
1316 struct symbol
*sym
=
1317 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1318 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
1319 return value_from_longest (
1320 lookup_pointer_type (TYPE_FIELD_TYPE (t
, i
)),
1321 (LONGEST
)SYMBOL_BLOCK_VALUE (sym
));
1323 if (TYPE_FIELD_PACKED (t
, i
))
1324 error ("pointers to bitfield members not allowed");
1326 return value_from_longest (
1327 lookup_pointer_type (
1328 lookup_member_type (TYPE_FIELD_TYPE (t
, i
), baseclass
)),
1329 (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1333 if (TYPE_N_BASECLASSES (t
) == 0)
1336 t
= TYPE_BASECLASS (t
, 0);
1339 /* C++: If it was not found as a data field, then try to
1340 return it as a pointer to a method. */
1343 /* Destructors are a special case. */
1344 if (destructor_name_p (name
, t
))
1346 error ("pointers to destructors not implemented yet");
1349 /* Perform all necessary dereferencing. */
1350 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1351 intype
= TYPE_TARGET_TYPE (intype
);
1355 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1357 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1359 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1360 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1362 if (intype
== 0 && j
> 1)
1363 error ("non-unique member `%s' requires type instantiation", name
);
1367 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1370 error ("no member function matches that type instantiation");
1375 check_stub_method (t
, i
, j
);
1376 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1378 return value_from_longest (
1379 lookup_pointer_type (
1380 lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1382 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
1386 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1387 0, VAR_NAMESPACE
, 0, NULL
);
1388 v
= locate_var_value (s
, 0);
1389 VALUE_TYPE (v
) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
), baseclass
));
1395 if (TYPE_N_BASECLASSES (t
) == 0)
1398 t
= TYPE_BASECLASS (t
, 0);
1403 /* Compare two argument lists and return the position in which they differ,
1406 STATICP is nonzero if the T1 argument list came from a
1407 static member function.
1409 For non-static member functions, we ignore the first argument,
1410 which is the type of the instance variable. This is because we want
1411 to handle calls with objects from derived classes. This is not
1412 entirely correct: we should actually check to make sure that a
1413 requested operation is type secure, shouldn't we? FIXME. */
1416 typecmp (staticp
, t1
, t2
)
1425 if (staticp
&& t1
== 0)
1429 if (t1
[0]->code
== TYPE_CODE_VOID
) return 0;
1430 if (t1
[!staticp
] == 0) return 0;
1431 for (i
= !staticp
; t1
[i
] && t1
[i
]->code
!= TYPE_CODE_VOID
; i
++)
1434 || t1
[i
]->code
!= t2
[i
]->type
->code
1435 /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1439 if (!t1
[i
]) return 0;
1440 return t2
[i
] ? i
+1 : 0;
1443 /* C++: return the value of the class instance variable, if one exists.
1444 Flag COMPLAIN signals an error if the request is made in an
1445 inappropriate context. */
1447 value_of_this (complain
)
1450 extern FRAME selected_frame
;
1451 struct symbol
*func
, *sym
;
1454 static const char funny_this
[] = "this";
1457 if (selected_frame
== 0)
1459 error ("no frame selected");
1462 func
= get_frame_function (selected_frame
);
1466 error ("no `this' in nameless context");
1470 b
= SYMBOL_BLOCK_VALUE (func
);
1471 i
= BLOCK_NSYMS (b
);
1474 error ("no args, no `this'");
1477 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1478 symbol instead of the LOC_ARG one (if both exist). */
1479 sym
= lookup_block_symbol (b
, funny_this
, VAR_NAMESPACE
);
1483 error ("current stack frame not in method");
1488 this = read_var_value (sym
, selected_frame
);
1489 if (this == 0 && complain
)
1490 error ("`this' argument at unknown address");