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 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 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_long (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 element. */
407 value_coerce_array (arg1
)
410 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 /* Get the type of the result. */
425 type
= lookup_pointer_type (type
);
426 val
= value_from_long (builtin_type_long
,
427 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
428 VALUE_TYPE (val
) = type
;
432 /* Given a value which is a function, return a value which is a pointer
436 value_coerce_function (arg1
)
439 register struct type
*type
;
442 if (VALUE_LVAL (arg1
) != lval_memory
)
443 error ("Attempt to take address of value not located in memory.");
445 /* Get the type of the result. */
446 type
= lookup_pointer_type (VALUE_TYPE (arg1
));
447 val
= value_from_long (builtin_type_long
,
448 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
449 VALUE_TYPE (val
) = type
;
453 /* Return a pointer value for the object for which ARG1 is the contents. */
459 register struct type
*type
;
463 /* Taking the address of an array is really a no-op
464 once the array is coerced to a pointer to its first element. */
465 if (VALUE_REPEATED (arg1
)
466 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
)
467 return value_coerce_array (arg1
);
468 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FUNC
)
469 return value_coerce_function (arg1
);
471 if (VALUE_LVAL (arg1
) != lval_memory
)
472 error ("Attempt to take address of value not located in memory.");
474 /* Get the type of the result. */
475 type
= lookup_pointer_type (VALUE_TYPE (arg1
));
476 val
= value_from_long (builtin_type_long
,
477 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
478 VALUE_TYPE (val
) = type
;
482 /* Given a value of a pointer type, apply the C unary * operator to it. */
490 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
491 error ("not implemented: member types in value_ind");
493 /* Allow * on an integer so we can cast it to whatever we want.
494 This returns an int, which seems like the most C-like thing
495 to do. "long long" variables are rare enough that
496 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
497 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
498 return value_at (builtin_type_int
,
499 (CORE_ADDR
) value_as_long (arg1
));
500 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
501 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
502 value_as_pointer (arg1
));
503 error ("Attempt to take contents of a non-pointer value.");
504 return 0; /* For lint -- never reached */
507 /* Pushing small parts of stack frames. */
509 /* Push one word (the size of object that a register holds). */
512 push_word (sp
, buffer
)
514 REGISTER_TYPE buffer
;
516 register int len
= sizeof (REGISTER_TYPE
);
518 SWAP_TARGET_AND_HOST (&buffer
, len
);
521 write_memory (sp
, (char *)&buffer
, len
);
522 #else /* stack grows upward */
523 write_memory (sp
, (char *)&buffer
, len
);
525 #endif /* stack grows upward */
530 /* Push LEN bytes with data at BUFFER. */
533 push_bytes (sp
, buffer
, len
)
540 write_memory (sp
, buffer
, len
);
541 #else /* stack grows upward */
542 write_memory (sp
, buffer
, len
);
544 #endif /* stack grows upward */
549 /* Push onto the stack the specified value VALUE. */
553 register CORE_ADDR sp
;
556 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
560 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
561 #else /* stack grows upward */
562 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
564 #endif /* stack grows upward */
569 /* Perform the standard coercions that are specified
570 for arguments to be passed to C functions. */
573 value_arg_coerce (arg
)
576 register struct type
*type
;
580 type
= VALUE_TYPE (arg
);
582 if (TYPE_CODE (type
) == TYPE_CODE_INT
583 && TYPE_LENGTH (type
) < sizeof (int))
584 return value_cast (builtin_type_int
, arg
);
586 if (type
== builtin_type_float
)
587 return value_cast (builtin_type_double
, arg
);
592 /* Push the value ARG, first coercing it as an argument
596 value_arg_push (sp
, arg
)
597 register CORE_ADDR sp
;
600 return value_push (sp
, value_arg_coerce (arg
));
603 /* Determine a function's address and its return type from its value.
604 Calls error() if the function is not valid for calling. */
607 find_function_addr (function
, retval_type
)
609 struct type
**retval_type
;
611 register struct type
*ftype
= VALUE_TYPE (function
);
612 register enum type_code code
= TYPE_CODE (ftype
);
613 struct type
*value_type
;
616 /* If it's a member function, just look at the function
619 /* Determine address to call. */
620 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
622 funaddr
= VALUE_ADDRESS (function
);
623 value_type
= TYPE_TARGET_TYPE (ftype
);
625 else if (code
== TYPE_CODE_PTR
)
627 funaddr
= value_as_pointer (function
);
628 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_FUNC
629 || TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_METHOD
)
630 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
632 value_type
= builtin_type_int
;
634 else if (code
== TYPE_CODE_INT
)
636 /* Handle the case of functions lacking debugging info.
637 Their values are characters since their addresses are char */
638 if (TYPE_LENGTH (ftype
) == 1)
639 funaddr
= value_as_pointer (value_addr (function
));
641 /* Handle integer used as address of a function. */
642 funaddr
= (CORE_ADDR
) value_as_long (function
);
644 value_type
= builtin_type_int
;
647 error ("Invalid data type for function to be called.");
649 *retval_type
= value_type
;
653 #if defined (CALL_DUMMY)
654 /* All this stuff with a dummy frame may seem unnecessarily complicated
655 (why not just save registers in GDB?). The purpose of pushing a dummy
656 frame which looks just like a real frame is so that if you call a
657 function and then hit a breakpoint (get a signal, etc), "backtrace"
658 will look right. Whether the backtrace needs to actually show the
659 stack at the time the inferior function was called is debatable, but
660 it certainly needs to not display garbage. So if you are contemplating
661 making dummy frames be different from normal frames, consider that. */
663 /* Perform a function call in the inferior.
664 ARGS is a vector of values of arguments (NARGS of them).
665 FUNCTION is a value, the function to be called.
666 Returns a value representing what the function returned.
667 May fail to return, if a breakpoint or signal is hit
668 during the execution of the function. */
671 call_function_by_hand (function
, nargs
, args
)
676 register CORE_ADDR sp
;
679 /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
680 in in host byte order. It is switched to target byte order before calling
682 static REGISTER_TYPE dummy
[] = CALL_DUMMY
;
683 REGISTER_TYPE dummy1
[sizeof dummy
/ sizeof (REGISTER_TYPE
)];
685 struct type
*value_type
;
686 unsigned char struct_return
;
687 CORE_ADDR struct_addr
;
688 struct inferior_status inf_status
;
689 struct cleanup
*old_chain
;
693 save_inferior_status (&inf_status
, 1);
694 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
696 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
697 (and POP_FRAME for restoring them). (At least on most machines)
698 they are saved on the stack in the inferior. */
701 old_sp
= sp
= read_register (SP_REGNUM
);
703 #if 1 INNER_THAN 2 /* Stack grows down */
706 #else /* Stack grows up */
711 funaddr
= find_function_addr (function
, &value_type
);
714 struct block
*b
= block_for_pc (funaddr
);
715 /* If compiled without -g, assume GCC. */
716 using_gcc
= b
== NULL
|| BLOCK_GCC_COMPILED (b
);
719 /* Are we returning a value using a structure return or a normal
722 struct_return
= using_struct_return (function
, funaddr
, value_type
,
725 /* Create a call sequence customized for this function
726 and the number of arguments for it. */
727 bcopy (dummy
, dummy1
, sizeof dummy
);
728 for (i
= 0; i
< sizeof dummy
/ sizeof (REGISTER_TYPE
); i
++)
729 SWAP_TARGET_AND_HOST (&dummy1
[i
], sizeof (REGISTER_TYPE
));
730 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
731 value_type
, using_gcc
);
733 #if CALL_DUMMY_LOCATION == ON_STACK
734 write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
736 #else /* Not on stack. */
737 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
738 /* Convex Unix prohibits executing in the stack segment. */
739 /* Hope there is empty room at the top of the text segment. */
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. */
756 errcode
= target_write_memory (start_sp
, (char *)dummy1
, sizeof dummy
);
758 error ("Cannot write text segment -- call_function failed");
760 #endif /* After text_end. */
761 #endif /* Not on stack. */
764 sp
= old_sp
; /* It really is used, for some ifdef's... */
768 /* If stack grows down, we must leave a hole at the top. */
772 /* Reserve space for the return structure to be written on the
773 stack, if necessary */
776 len
+= TYPE_LENGTH (value_type
);
778 for (i
= nargs
- 1; i
>= 0; i
--)
779 len
+= TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args
[i
])));
780 #ifdef CALL_DUMMY_STACK_ADJUST
781 len
+= CALL_DUMMY_STACK_ADJUST
;
784 sp
-= STACK_ALIGN (len
) - len
;
786 sp
+= STACK_ALIGN (len
) - len
;
789 #endif /* STACK_ALIGN */
791 /* Reserve space for the return structure to be written on the
792 stack, if necessary */
797 sp
-= TYPE_LENGTH (value_type
);
801 sp
+= TYPE_LENGTH (value_type
);
805 #if defined (REG_STRUCT_HAS_ADDR)
807 /* This is a machine like the sparc, where we need to pass a pointer
808 to the structure, not the structure itself. */
809 if (REG_STRUCT_HAS_ADDR (using_gcc
))
810 for (i
= nargs
- 1; i
>= 0; i
--)
811 if (TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_STRUCT
)
814 #if !(1 INNER_THAN 2)
815 /* The stack grows up, so the address of the thing we push
816 is the stack pointer before we push it. */
819 /* Push the structure. */
820 sp
= value_push (sp
, args
[i
]);
822 /* The stack grows down, so the address of the thing we push
823 is the stack pointer after we push it. */
826 /* The value we're going to pass is the address of the thing
828 args
[i
] = value_from_long (builtin_type_long
, (LONGEST
) addr
);
831 #endif /* REG_STRUCT_HAS_ADDR. */
833 #ifdef PUSH_ARGUMENTS
834 PUSH_ARGUMENTS(nargs
, args
, sp
, struct_return
, struct_addr
);
835 #else /* !PUSH_ARGUMENTS */
836 for (i
= nargs
- 1; i
>= 0; i
--)
837 sp
= value_arg_push (sp
, args
[i
]);
838 #endif /* !PUSH_ARGUMENTS */
840 #ifdef CALL_DUMMY_STACK_ADJUST
842 sp
-= CALL_DUMMY_STACK_ADJUST
;
844 sp
+= CALL_DUMMY_STACK_ADJUST
;
846 #endif /* CALL_DUMMY_STACK_ADJUST */
848 /* Store the address at which the structure is supposed to be
849 written. Note that this (and the code which reserved the space
850 above) assumes that gcc was used to compile this function. Since
851 it doesn't cost us anything but space and if the function is pcc
852 it will ignore this value, we will make that assumption.
854 Also note that on some machines (like the sparc) pcc uses a
855 convention like gcc's. */
858 STORE_STRUCT_RETURN (struct_addr
, sp
);
860 /* Write the stack pointer. This is here because the statements above
861 might fool with it. On SPARC, this write also stores the register
862 window into the right place in the new stack frame, which otherwise
863 wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */
864 write_register (SP_REGNUM
, sp
);
866 /* Figure out the value returned by the function. */
868 char retbuf
[REGISTER_BYTES
];
870 /* Execute the stack dummy routine, calling FUNCTION.
871 When it is done, discard the empty frame
872 after storing the contents of all regs into retbuf. */
873 run_stack_dummy (start_sp
+ CALL_DUMMY_START_OFFSET
, retbuf
);
875 do_cleanups (old_chain
);
877 return value_being_returned (value_type
, retbuf
, struct_return
);
880 #else /* no CALL_DUMMY. */
882 call_function_by_hand (function
, nargs
, args
)
887 error ("Cannot invoke functions on this machine.");
889 #endif /* no CALL_DUMMY. */
891 /* Create a value for a string constant:
892 Call the function malloc in the inferior to get space for it,
893 then copy the data into that space
894 and then return the address with type char *.
895 PTR points to the string constant data; LEN is number of characters. */
898 value_string (ptr
, len
)
903 register struct symbol
*sym
;
905 register char *copy
= (char *) alloca (len
+ 1);
907 register char *o
= copy
, *ibeg
= ptr
;
910 /* Copy the string into COPY, processing escapes.
911 We could not conveniently process them in expread
912 because the string there wants to be a substring of the input. */
914 while (i
- ibeg
< len
)
919 c
= parse_escape (&i
);
927 /* Get the length of the string after escapes are processed. */
931 /* Find the address of malloc in the inferior. */
933 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0, NULL
);
936 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
937 error ("\"malloc\" exists in this program but is not a function.");
938 val
= value_of_variable (sym
);
943 for (j
= 0; j
< misc_function_count
; j
++)
944 if (!strcmp (misc_function_vector
[j
].name
, "malloc"))
946 if (j
< misc_function_count
)
947 val
= value_from_long (builtin_type_long
,
948 (LONGEST
) misc_function_vector
[j
].address
);
950 error ("String constants require the program to have a function \"malloc\".");
953 blocklen
= value_from_long (builtin_type_int
, (LONGEST
) (len
+ 1));
954 val
= target_call_function (val
, 1, &blocklen
);
955 if (value_zerop (val
))
956 error ("No memory available for string constant.");
957 write_memory (value_as_pointer (val
), copy
, len
+ 1);
958 VALUE_TYPE (val
) = lookup_pointer_type (builtin_type_char
);
962 /* Helper function used by value_struct_elt to recurse through baseclasses.
963 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
964 and treat the result as having type TYPE.
965 If found, return value, else return NULL.
967 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
968 look for a baseclass named NAME. */
971 search_struct_field (name
, arg1
, offset
, type
, looking_for_baseclass
)
975 register struct type
*type
;
976 int looking_for_baseclass
;
980 check_stub_type (type
);
982 if (! looking_for_baseclass
)
983 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
985 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
987 if (t_field_name
&& !strcmp (t_field_name
, name
))
989 value v
= (TYPE_FIELD_STATIC (type
, i
)
990 ? value_static_field (type
, name
, i
)
991 : value_primitive_field (arg1
, offset
, i
, type
));
993 error("there is no field named %s", name
);
998 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1001 /* If we are looking for baseclasses, this is what we get when we
1003 int found_baseclass
= (looking_for_baseclass
1004 && !strcmp (name
, TYPE_BASECLASS_NAME (type
, i
)));
1006 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1009 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1012 error ("virtual baseclass botch");
1013 if (found_baseclass
)
1015 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
1016 looking_for_baseclass
);
1020 if (found_baseclass
)
1021 v
= value_primitive_field (arg1
, offset
, i
, type
);
1023 v
= search_struct_field (name
, arg1
,
1024 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1025 TYPE_BASECLASS (type
, i
),
1026 looking_for_baseclass
);
1032 /* Helper function used by value_struct_elt to recurse through baseclasses.
1033 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1034 and treat the result as having type TYPE.
1035 If found, return value, else return NULL. */
1038 search_struct_method (name
, arg1
, args
, offset
, static_memfuncp
, type
)
1040 register value arg1
, *args
;
1041 int offset
, *static_memfuncp
;
1042 register struct type
*type
;
1046 check_stub_type (type
);
1047 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1049 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1050 if (t_field_name
&& !strcmp (t_field_name
, name
))
1052 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1053 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1055 if (j
> 0 && args
== 0)
1056 error ("cannot resolve overloaded method `%s'", name
);
1059 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f
, j
)) & TYPE_FLAG_STUB
)
1060 check_stub_method (type
, i
, j
);
1061 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1062 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1064 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1065 return (value
)value_virtual_fn_field (arg1
, f
, j
, type
);
1066 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1067 *static_memfuncp
= 1;
1068 return (value
)value_fn_field (arg1
, i
, j
);
1075 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1079 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1082 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1085 error ("virtual baseclass botch");
1086 v
= search_struct_method (name
, v2
, args
, 0,
1087 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1092 v
= search_struct_method (name
, arg1
, args
,
1093 TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1094 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1100 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1101 extract the component named NAME from the ultimate target structure/union
1102 and return it as a value with its appropriate type.
1103 ERR is used in the error message if *ARGP's type is wrong.
1105 C++: ARGS is a list of argument types to aid in the selection of
1106 an appropriate method. Also, handle derived types.
1108 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1109 where the truthvalue of whether the function that was resolved was
1110 a static member function or not is stored.
1112 ERR is an error message to be printed in case the field is not found. */
1115 value_struct_elt (argp
, args
, name
, static_memfuncp
, err
)
1116 register value
*argp
, *args
;
1118 int *static_memfuncp
;
1121 register struct type
*t
;
1124 COERCE_ARRAY (*argp
);
1126 t
= VALUE_TYPE (*argp
);
1128 /* Follow pointers until we get to a non-pointer. */
1130 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1132 *argp
= value_ind (*argp
);
1133 /* Don't coerce fn pointer to fn and then back again! */
1134 if (TYPE_CODE (VALUE_TYPE (*argp
)) != TYPE_CODE_FUNC
)
1135 COERCE_ARRAY (*argp
);
1136 t
= VALUE_TYPE (*argp
);
1139 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1140 error ("not implemented: member type in value_struct_elt");
1142 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1143 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1144 error ("Attempt to extract a component of a value that is not a %s.", err
);
1146 /* Assume it's not, unless we see that it is. */
1147 if (static_memfuncp
)
1148 *static_memfuncp
=0;
1152 /* if there are no arguments ...do this... */
1154 /* Try as a field first, because if we succeed, there
1155 is less work to be done. */
1156 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1160 /* C++: If it was not found as a data field, then try to
1161 return it as a pointer to a method. */
1163 if (destructor_name_p (name
, t
))
1164 error ("Cannot get value of destructor");
1166 v
= search_struct_method (name
, *argp
, args
, 0, static_memfuncp
, t
);
1170 if (TYPE_NFN_FIELDS (t
))
1171 error ("There is no member or method named %s.", name
);
1173 error ("There is no member named %s.", name
);
1178 if (destructor_name_p (name
, t
))
1182 /* destructors are a special case. */
1183 return (value
)value_fn_field (*argp
, 0,
1184 TYPE_FN_FIELDLIST_LENGTH (t
, 0));
1188 error ("destructor should not have any argument");
1192 v
= search_struct_method (name
, *argp
, args
, 0, static_memfuncp
, t
);
1196 /* See if user tried to invoke data as function. If so,
1197 hand it back. If it's not callable (i.e., a pointer to function),
1198 gdb should give an error. */
1199 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1203 error ("Structure has no component named %s.", name
);
1207 /* C++: return 1 is NAME is a legitimate name for the destructor
1208 of type TYPE. If TYPE does not have a destructor, or
1209 if NAME is inappropriate for TYPE, an error is signaled. */
1211 destructor_name_p (name
, type
)
1215 /* destructors are a special case. */
1219 char *dname
= type_name_no_tag (type
);
1221 if (! TYPE_HAS_DESTRUCTOR (type
))
1222 error ("type `%s' does not have destructor defined", dname
);
1223 if (strcmp (dname
, name
+1))
1224 error ("name of destructor must equal name of class");
1231 /* Helper function for check_field: Given TYPE, a structure/union,
1232 return 1 if the component named NAME from the ultimate
1233 target structure/union is defined, otherwise, return 0. */
1236 check_field_in (type
, name
)
1237 register struct type
*type
;
1242 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1244 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1245 if (t_field_name
&& !strcmp (t_field_name
, name
))
1249 /* C++: If it was not found as a data field, then try to
1250 return it as a pointer to a method. */
1252 /* Destructors are a special case. */
1253 if (destructor_name_p (name
, type
))
1256 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1258 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type
, i
), name
))
1262 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1263 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
1270 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1271 return 1 if the component named NAME from the ultimate
1272 target structure/union is defined, otherwise, return 0. */
1275 check_field (arg1
, name
)
1276 register value arg1
;
1279 register struct type
*t
;
1281 COERCE_ARRAY (arg1
);
1283 t
= VALUE_TYPE (arg1
);
1285 /* Follow pointers until we get to a non-pointer. */
1287 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1288 t
= TYPE_TARGET_TYPE (t
);
1290 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1291 error ("not implemented: member type in check_field");
1293 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1294 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1295 error ("Internal error: `this' is not an aggregate");
1297 return check_field_in (t
, name
);
1300 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1301 return the address of this member as a pointer to member
1302 type. If INTYPE is non-null, then it will be the type
1303 of the member we are looking for. This will help us resolve
1304 pointers to member functions. */
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
);
1328 if (t_field_name
&& !strcmp (t_field_name
, name
))
1330 if (TYPE_FIELD_STATIC (t
, i
))
1332 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (t
, i
);
1333 struct symbol
*sym
=
1334 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1335 if (! sym
) error ("Internal error: could not find physical static variable named %s", phys_name
);
1336 v
= value_from_long(builtin_type_long
,
1337 (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1338 VALUE_TYPE(v
) = lookup_pointer_type (TYPE_FIELD_TYPE (t
, i
));
1341 if (TYPE_FIELD_PACKED (t
, i
))
1342 error ("pointers to bitfield members not allowed");
1344 v
= value_from_long (builtin_type_int
,
1345 (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1347 = lookup_pointer_type (lookup_member_type (TYPE_FIELD_TYPE (t
, i
), baseclass
));
1352 if (TYPE_N_BASECLASSES (t
) == 0)
1355 t
= TYPE_BASECLASS (t
, 0);
1358 /* C++: If it was not found as a data field, then try to
1359 return it as a pointer to a method. */
1362 /* Destructors are a special case. */
1363 if (destructor_name_p (name
, t
))
1365 error ("pointers to destructors not implemented yet");
1368 /* Perform all necessary dereferencing. */
1369 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1370 intype
= TYPE_TARGET_TYPE (intype
);
1374 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1376 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1378 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1379 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1381 if (intype
== 0 && j
> 1)
1382 error ("non-unique member `%s' requires type instantiation", name
);
1386 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1389 error ("no member function matches that type instantiation");
1394 check_stub_method (t
, i
, j
);
1395 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1397 v
= value_from_long (builtin_type_long
,
1398 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
1402 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1403 0, VAR_NAMESPACE
, 0, NULL
);
1404 v
= locate_var_value (s
, 0);
1406 VALUE_TYPE (v
) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
), baseclass
));
1411 if (TYPE_N_BASECLASSES (t
) == 0)
1414 t
= TYPE_BASECLASS (t
, 0);
1419 /* Compare two argument lists and return the position in which they differ,
1422 STATICP is nonzero if the T1 argument list came from a
1423 static member function.
1425 For non-static member functions, we ignore the first argument,
1426 which is the type of the instance variable. This is because we want
1427 to handle calls with objects from derived classes. This is not
1428 entirely correct: we should actually check to make sure that a
1429 requested operation is type secure, shouldn't we? FIXME. */
1432 typecmp (staticp
, t1
, t2
)
1441 if (staticp
&& t1
== 0)
1445 if (t1
[0]->code
== TYPE_CODE_VOID
) return 0;
1446 if (t1
[!staticp
] == 0) return 0;
1447 for (i
= !staticp
; t1
[i
] && t1
[i
]->code
!= TYPE_CODE_VOID
; i
++)
1450 || t1
[i
]->code
!= t2
[i
]->type
->code
1451 /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1455 if (!t1
[i
]) return 0;
1456 return t2
[i
] ? i
+1 : 0;
1459 /* C++: return the value of the class instance variable, if one exists.
1460 Flag COMPLAIN signals an error if the request is made in an
1461 inappropriate context. */
1463 value_of_this (complain
)
1466 extern FRAME selected_frame
;
1467 struct symbol
*func
, *sym
;
1470 static const char funny_this
[] = "this";
1473 if (selected_frame
== 0)
1475 error ("no frame selected");
1478 func
= get_frame_function (selected_frame
);
1482 error ("no `this' in nameless context");
1486 b
= SYMBOL_BLOCK_VALUE (func
);
1487 i
= BLOCK_NSYMS (b
);
1490 error ("no args, no `this'");
1493 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1494 symbol instead of the LOC_ARG one (if both exist). */
1495 sym
= lookup_block_symbol (b
, funny_this
, VAR_NAMESPACE
);
1499 error ("current stack frame not in method");
1504 this = read_var_value (sym
, selected_frame
);
1505 if (this == 0 && complain
)
1506 error ("`this' argument at unknown address");